SectorPlacer 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_SECTOR_PLACER
  15. #define OSGPARTICLE_SECTOR_PLACER 1
  16. #include <osgParticle/CenteredPlacer>
  17. #include <osgParticle/Particle>
  18. #include <osgParticle/range>
  19. #include <osg/CopyOp>
  20. #include <osg/Object>
  21. #include <osg/Vec3>
  22. #include <osg/Math>
  23. namespace osgParticle
  24. {
  25. /** A sector-shaped particle placer.
  26. This placer sets the initial position of incoming particle by choosing a random position
  27. within a circular sector; this sector is defined by three parameters: a <I>center point</I>,
  28. which is inherited directly from <CODE>osgParticle::CenteredPlacer</CODE>, a range of values
  29. for <I>radius</I>, and a range of values for the <I>central angle</I> (sometimes called <B>phi</B>).
  30. */
  31. class SectorPlacer: public CenteredPlacer {
  32. public:
  33. inline SectorPlacer();
  34. inline SectorPlacer(const SectorPlacer& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  35. /// Get the range of possible values for radius.
  36. inline const rangef& getRadiusRange() const;
  37. /// Set the range of possible values for radius.
  38. inline void setRadiusRange(const rangef& r);
  39. /// Set the range of possible values for radius.
  40. inline void setRadiusRange(float r1, float r2);
  41. /// Get the range of possible values for the central angle.
  42. inline const rangef& getPhiRange() const;
  43. /// Set the range of possible values for the central angle.
  44. inline void setPhiRange(const rangef& r);
  45. /// Set the range of possible values for the central angle.
  46. inline void setPhiRange(float r1, float r2);
  47. META_Object(osgParticle, SectorPlacer);
  48. /// Place a particle. Do not call it manually.
  49. inline void place(Particle* P) const;
  50. /// return the area of the sector
  51. inline float volume() const;
  52. /// return the control position
  53. inline osg::Vec3 getControlPosition() const;
  54. protected:
  55. virtual ~SectorPlacer() {}
  56. SectorPlacer& operator=(const SectorPlacer&) { return *this; }
  57. private:
  58. rangef _rad_range;
  59. rangef _phi_range;
  60. };
  61. // INLINE FUNCTIONS
  62. inline SectorPlacer::SectorPlacer()
  63. : CenteredPlacer(), _rad_range(0, 1), _phi_range(0, osg::PI*2)
  64. {
  65. }
  66. inline SectorPlacer::SectorPlacer(const SectorPlacer& copy, const osg::CopyOp& copyop)
  67. : CenteredPlacer(copy, copyop), _rad_range(copy._rad_range), _phi_range(copy._phi_range)
  68. {
  69. }
  70. inline const rangef& SectorPlacer::getRadiusRange() const
  71. {
  72. return _rad_range;
  73. }
  74. inline const rangef& SectorPlacer::getPhiRange() const
  75. {
  76. return _phi_range;
  77. }
  78. inline void SectorPlacer::setRadiusRange(const rangef& r)
  79. {
  80. _rad_range = r;
  81. }
  82. inline void SectorPlacer::setRadiusRange(float r1, float r2)
  83. {
  84. _rad_range.minimum = r1;
  85. _rad_range.maximum = r2;
  86. }
  87. inline void SectorPlacer::setPhiRange(const rangef& r)
  88. {
  89. _phi_range = r;
  90. }
  91. inline void SectorPlacer::setPhiRange(float r1, float r2)
  92. {
  93. _phi_range.minimum = r1;
  94. _phi_range.maximum = r2;
  95. }
  96. inline void SectorPlacer::place(Particle* P) const
  97. {
  98. float rad = _rad_range.get_random_sqrtf();
  99. float phi = _phi_range.get_random();
  100. osg::Vec3 pos(
  101. getCenter().x() + rad * cosf(phi),
  102. getCenter().y() + rad * sinf(phi),
  103. getCenter().z());
  104. P->setPosition(pos);
  105. }
  106. inline float SectorPlacer::volume() const
  107. {
  108. return 0.5f * (_phi_range.maximum - _phi_range.minimum) *
  109. (_rad_range.maximum*_rad_range.maximum - _rad_range.minimum*_rad_range.minimum);
  110. }
  111. inline osg::Vec3 SectorPlacer::getControlPosition() const
  112. {
  113. return getCenter();
  114. }
  115. }
  116. #endif