MultiSegmentPlacer 4.0 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_MULTISEGMENT_PLACER
  15. #define OSGPARTICLE_MULTISEGMENT_PLACER 1
  16. #include <osgParticle/Export>
  17. #include <osgParticle/Placer>
  18. #include <osgParticle/Particle>
  19. #include <vector>
  20. #include <utility>
  21. #include <osg/Notify>
  22. #include <osg/CopyOp>
  23. #include <osg/Object>
  24. #include <osg/Vec3>
  25. namespace osgParticle {
  26. /** A polyline-shaped particle placer.
  27. This placer class sets the position of incoming particles by choosing a random point on the
  28. specified sequence of connected segments.
  29. */
  30. class OSGPARTICLE_EXPORT MultiSegmentPlacer: public Placer {
  31. public:
  32. MultiSegmentPlacer();
  33. MultiSegmentPlacer(const MultiSegmentPlacer& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  34. META_Object(osgParticle, MultiSegmentPlacer);
  35. /// Get the number of vertices which define the segments.
  36. inline int numVertices() const;
  37. /// Get a vertex.
  38. inline const osg::Vec3& getVertex(int i) const;
  39. /// Set a vertex.
  40. inline void setVertex(int i, const osg::Vec3& v);
  41. /// Set a vertex.
  42. inline void setVertex(int i, float x, float y, float z);
  43. /// Add a vertex.
  44. inline void addVertex(const osg::Vec3& v);
  45. /// Add a vertex.
  46. inline void addVertex(float x, float y, float z);
  47. /// Remove a vertex.
  48. inline void removeVertex(int i);
  49. /// Place a partice. Called automatically by <CODE>ModularEmitter</CODE>, do not call this method manually.
  50. void place(Particle* P) const;
  51. /// return the length of the multi-segment
  52. inline float volume() const;
  53. /// return the control position
  54. inline osg::Vec3 getControlPosition() const;
  55. protected:
  56. virtual ~MultiSegmentPlacer() {}
  57. MultiSegmentPlacer& operator=(const MultiSegmentPlacer&) { return *this; }
  58. private:
  59. typedef std::pair<osg::Vec3, float> Vertex_data;
  60. typedef std::vector<Vertex_data> Vertex_vector;
  61. Vertex_vector _vx;
  62. float _total_length;
  63. void recompute_length();
  64. };
  65. // INLINE FUNCTIONS
  66. inline int MultiSegmentPlacer::numVertices() const
  67. {
  68. return static_cast<int>(_vx.size());
  69. }
  70. inline const osg::Vec3& MultiSegmentPlacer::getVertex(int i) const
  71. {
  72. return _vx[i].first;
  73. }
  74. inline void MultiSegmentPlacer::setVertex(int i, const osg::Vec3& v)
  75. {
  76. _vx[i].first = v;
  77. recompute_length();
  78. }
  79. inline void MultiSegmentPlacer::setVertex(int i, float x, float y, float z)
  80. {
  81. _vx[i].first.set(x, y, z);
  82. recompute_length();
  83. }
  84. inline void MultiSegmentPlacer::addVertex(const osg::Vec3& v)
  85. {
  86. float l = 0;
  87. if (_vx.size() > 0) {
  88. l = (v - _vx.back().first).length();
  89. }
  90. _total_length += l;
  91. _vx.push_back(std::make_pair(v, _total_length));
  92. }
  93. inline void MultiSegmentPlacer::addVertex(float x, float y, float z)
  94. {
  95. addVertex(osg::Vec3(x, y, z));
  96. }
  97. inline void MultiSegmentPlacer::removeVertex(int i)
  98. {
  99. _vx.erase(_vx.begin()+i);
  100. recompute_length();
  101. }
  102. inline float MultiSegmentPlacer::volume() const
  103. {
  104. return _total_length;
  105. }
  106. inline osg::Vec3 MultiSegmentPlacer::getControlPosition() const
  107. {
  108. return _vx.empty() ? osg::Vec3(0.0f,0.0f,0.0f) : _vx[0].first;
  109. }
  110. }
  111. #endif