BoxPlacer 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. //Build by Zach Deedler
  14. #ifndef OSGPARTICLE_BOX_PLACER
  15. #define OSGPARTICLE_BOX_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 box-shaped particle placer.
  26. This placer sets the initial position of incoming particle by choosing a random position
  27. within the volume of a box; this placer is defined by four parameters: a <I>center point</I>,
  28. which is inherited directly from <CODE>osgParticle::CenteredPlacer</CODE>, and three ranges of values
  29. for the valid <I>X</I>, <I>Y</I>, and <I>Z</I> coordinates.
  30. */
  31. class BoxPlacer: public CenteredPlacer {
  32. public:
  33. inline BoxPlacer();
  34. inline BoxPlacer(const BoxPlacer& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  35. /// Get the range of possible values along the X axis.
  36. inline const rangef& getXRange() const;
  37. /// Set the range of possible values along the X axis.
  38. inline void setXRange(const rangef& r);
  39. /// Set the range of possible values along the X axis.
  40. inline void setXRange(float r1, float r2);
  41. /// Get the range of possible values along the Y axis.
  42. inline const rangef& getYRange() const;
  43. /// Set the range of possible values along the Y axis.
  44. inline void setYRange(const rangef& r);
  45. /// Set the range of possible values along the Y axis.
  46. inline void setYRange(float r1, float r2);
  47. /// Get the range of possible values along the Z axis.
  48. inline const rangef& getZRange() const;
  49. /// Set the range of possible values along the Z axis.
  50. inline void setZRange(const rangef& r);
  51. /// Set the range of possible values along the Z axis.
  52. inline void setZRange(float r1, float r2);
  53. META_Object(osgParticle, BoxPlacer);
  54. /// Place a particle. Do not call it manually.
  55. inline void place(Particle* P) const;
  56. /// return the volume of the box
  57. inline float volume() const;
  58. /// return the control position
  59. inline osg::Vec3 getControlPosition() const;
  60. protected:
  61. virtual ~BoxPlacer() {}
  62. BoxPlacer& operator=(const BoxPlacer&) { return *this; }
  63. private:
  64. rangef _x_range;
  65. rangef _y_range;
  66. rangef _z_range;
  67. };
  68. // INLINE FUNCTIONS
  69. inline BoxPlacer::BoxPlacer()
  70. : CenteredPlacer(), _x_range(-1, 1), _y_range(-1, 1), _z_range(-1, 1)
  71. {
  72. }
  73. inline BoxPlacer::BoxPlacer(const BoxPlacer& copy, const osg::CopyOp& copyop)
  74. : CenteredPlacer(copy, copyop),
  75. _x_range(copy._x_range), _y_range(copy._y_range), _z_range(copy._z_range)
  76. {
  77. }
  78. inline const rangef& BoxPlacer::getXRange() const
  79. {
  80. return _x_range;
  81. }
  82. inline void BoxPlacer::setXRange(const rangef& r)
  83. {
  84. _x_range = r;
  85. }
  86. inline void BoxPlacer::setXRange(float r1, float r2)
  87. {
  88. _x_range.minimum = r1;
  89. _x_range.maximum = r2;
  90. }
  91. inline const rangef& BoxPlacer::getYRange() const
  92. {
  93. return _y_range;
  94. }
  95. inline void BoxPlacer::setYRange(const rangef& r)
  96. {
  97. _y_range = r;
  98. }
  99. inline void BoxPlacer::setYRange(float r1, float r2)
  100. {
  101. _y_range.minimum = r1;
  102. _y_range.maximum = r2;
  103. }
  104. inline const rangef& BoxPlacer::getZRange() const
  105. {
  106. return _z_range;
  107. }
  108. inline void BoxPlacer::setZRange(const rangef& r)
  109. {
  110. _z_range = r;
  111. }
  112. inline void BoxPlacer::setZRange(float r1, float r2)
  113. {
  114. _z_range.minimum = r1;
  115. _z_range.maximum = r2;
  116. }
  117. inline void BoxPlacer::place(Particle* P) const
  118. {
  119. osg::Vec3 pos(
  120. getCenter().x() + _x_range.get_random(),
  121. getCenter().y() + _y_range.get_random(),
  122. getCenter().z() + _z_range.get_random());
  123. P->setPosition(pos);
  124. }
  125. inline float BoxPlacer::volume() const
  126. {
  127. return (_x_range.maximum - _x_range.minimum) *
  128. (_y_range.maximum - _y_range.minimum) *
  129. (_z_range.maximum - _z_range.minimum);
  130. }
  131. inline osg::Vec3 BoxPlacer::getControlPosition() const
  132. {
  133. return getCenter();
  134. }
  135. }
  136. #endif