SegmentPlacer 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. //osgParticle - Copyright (C) 2002 Marco Jez
  14. #ifndef OSGPARTICLE_SEGMENT_PLACER
  15. #define OSGPARTICLE_SEGMENT_PLACER 1
  16. #include <osgParticle/Placer>
  17. #include <osgParticle/Particle>
  18. #include <osg/CopyOp>
  19. #include <osg/Object>
  20. #include <osg/Vec3>
  21. namespace osgParticle {
  22. /** A segment-shaped particle placer.
  23. To use this placer you have to define a segment, by setting its two vertices (<B>A</B> and <B>B</B>);
  24. when an emitter requests a <CODE>SegmentPlacer</CODE> to place a particle, the position is chosen randomly
  25. within that segment.
  26. */
  27. class SegmentPlacer: public Placer {
  28. public:
  29. inline SegmentPlacer();
  30. inline SegmentPlacer(const SegmentPlacer& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  31. META_Object(osgParticle, SegmentPlacer);
  32. /// get vertex <B>A</B>.
  33. inline const osg::Vec3& getVertexA() const;
  34. /// Set vertex <B>A</B> of the segment as a vector.
  35. inline void setVertexA(const osg::Vec3& v);
  36. /// Set vertex <B>A</B> of the segment as three floats.
  37. inline void setVertexA(float x, float y, float z);
  38. /// get vertex <B>B</B>.
  39. inline const osg::Vec3& getVertexB() const;
  40. /// Set vertex <B>B</B> of the segment as a vector.
  41. inline void setVertexB(const osg::Vec3& v);
  42. /// Set vertex <B>B</B> of the segment as three floats.
  43. inline void setVertexB(float x, float y, float z);
  44. /// Set both vertices.
  45. inline void setSegment(const osg::Vec3& A, const osg::Vec3& B);
  46. /// Place a particle. This method is called by <CODE>ModularEmitter</CODE>, do not call it manually.
  47. inline void place(Particle* P) const;
  48. /// return the length of the segment
  49. inline float volume() const;
  50. /// return the control position
  51. inline osg::Vec3 getControlPosition() const;
  52. protected:
  53. virtual ~SegmentPlacer() {}
  54. SegmentPlacer& operator=(const SegmentPlacer&) { return *this; }
  55. private:
  56. osg::Vec3 _vertexA;
  57. osg::Vec3 _vertexB;
  58. };
  59. // INLINE FUNCTIONS
  60. inline SegmentPlacer::SegmentPlacer()
  61. : Placer(), _vertexA(-1, 0, 0), _vertexB(1, 0, 0)
  62. {
  63. }
  64. inline SegmentPlacer::SegmentPlacer(const SegmentPlacer& copy, const osg::CopyOp& copyop)
  65. : Placer(copy, copyop), _vertexA(copy._vertexA), _vertexB(copy._vertexB)
  66. {
  67. }
  68. inline const osg::Vec3& SegmentPlacer::getVertexA() const
  69. {
  70. return _vertexA;
  71. }
  72. inline const osg::Vec3& SegmentPlacer::getVertexB() const
  73. {
  74. return _vertexB;
  75. }
  76. inline void SegmentPlacer::setSegment(const osg::Vec3& A, const osg::Vec3& B)
  77. {
  78. _vertexA = A;
  79. _vertexB = B;
  80. }
  81. inline void SegmentPlacer::place(Particle* P) const
  82. {
  83. P->setPosition(rangev3(_vertexA, _vertexB).get_random());
  84. }
  85. inline float SegmentPlacer::volume() const
  86. {
  87. return (_vertexB - _vertexA).length();
  88. }
  89. inline void SegmentPlacer::setVertexA(const osg::Vec3& v)
  90. {
  91. _vertexA = v;
  92. }
  93. inline void SegmentPlacer::setVertexA(float x, float y, float z)
  94. {
  95. _vertexA.set(x, y, z);
  96. }
  97. inline void SegmentPlacer::setVertexB(const osg::Vec3& v)
  98. {
  99. _vertexB = v;
  100. }
  101. inline void SegmentPlacer::setVertexB(float x, float y, float z)
  102. {
  103. _vertexB.set(x, y, z);
  104. }
  105. inline osg::Vec3 SegmentPlacer::getControlPosition() const
  106. {
  107. return (_vertexA+_vertexB)*0.5f;
  108. }
  109. }
  110. #endif