DomainOperator 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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. // Written by Wang Rui, (C) 2010
  14. #ifndef OSGPARTICLE_DOMAINOPERATOR
  15. #define OSGPARTICLE_DOMAINOPERATOR
  16. #include <osg/Plane>
  17. #include <osgParticle/Operator>
  18. #include <osgParticle/Particle>
  19. namespace osgParticle
  20. {
  21. /** A domain operator which accepts different domain shapes as the parameter.
  22. It can be derived to implement operators that require particles interacting with domains.
  23. Refer to David McAllister's Particle System API (http://www.particlesystems.org)
  24. */
  25. class OSGPARTICLE_EXPORT DomainOperator : public Operator
  26. {
  27. public:
  28. struct Domain
  29. {
  30. enum Type
  31. {
  32. UNDEFINED_DOMAIN = 0,
  33. POINT_DOMAIN,
  34. LINE_DOMAIN,
  35. TRI_DOMAIN,
  36. RECT_DOMAIN,
  37. PLANE_DOMAIN,
  38. SPHERE_DOMAIN,
  39. BOX_DOMAIN,
  40. DISK_DOMAIN
  41. };
  42. Domain( Type t ) : r1(0.0f), r2(0.0f), type(t) {}
  43. osg::Plane plane;
  44. osg::Vec3 v1;
  45. osg::Vec3 v2;
  46. osg::Vec3 v3;
  47. osg::Vec3 s1;
  48. osg::Vec3 s2;
  49. float r1;
  50. float r2;
  51. Type type;
  52. };
  53. DomainOperator()
  54. : Operator()
  55. {}
  56. DomainOperator( const DomainOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
  57. : Operator(copy, copyop), _domains(copy._domains), _backupDomains(copy._backupDomains)
  58. {}
  59. META_Object( osgParticle, DomainOperator );
  60. /// Add a point domain
  61. inline void addPointDomain( const osg::Vec3& p );
  62. /// Add a line segment domain
  63. inline void addLineSegmentDomain( const osg::Vec3& v1, const osg::Vec3& v2 );
  64. /// Add a triangle domain
  65. inline void addTriangleDomain( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3 );
  66. /// Add a rectangle domain
  67. inline void addRectangleDomain( const osg::Vec3& corner, const osg::Vec3& w, const osg::Vec3& h );
  68. /// Add a plane domain
  69. inline void addPlaneDomain( const osg::Plane& plane );
  70. /// Add a sphere domain
  71. inline void addSphereDomain( const osg::Vec3& c, float r );
  72. /// Add a box domain
  73. inline void addBoxDomain( const osg::Vec3& min, const osg::Vec3& max );
  74. /// Add a disk domain
  75. inline void addDiskDomain( const osg::Vec3& c, const osg::Vec3& n, float r1, float r2=0.0f );
  76. /// Add a domain object directly, used by the .osg wrappers and serializers.
  77. void addDomain( const Domain& domain ) { _domains.push_back(domain); }
  78. /// Get a domain object directly, used by the .osg wrappers and serializers.
  79. const Domain& getDomain( unsigned int i ) const { return _domains[i]; }
  80. /// Remove a domain at specific index
  81. void removeDomain( unsigned int i )
  82. { if (i<_domains.size()) _domains.erase(_domains.begin() + i); }
  83. /// Remove all existing domains
  84. void removeAllDomains() { _domains.clear(); }
  85. /// Get number of domains
  86. unsigned int getNumDomains() const { return _domains.size(); }
  87. /// Apply the acceleration to a particle. Do not call this method manually.
  88. void operate( Particle* P, double dt );
  89. /// Perform some initializations. Do not call this method manually.
  90. void beginOperate( Program* prg );
  91. /// Perform some post-operations. Do not call this method manually.
  92. void endOperate();
  93. protected:
  94. virtual ~DomainOperator() {}
  95. DomainOperator& operator=( const DomainOperator& ) { return *this; }
  96. virtual void handlePoint( const Domain& /*domain*/, Particle* /*P*/, double /*dt*/ ) { ignore("Point"); }
  97. virtual void handleLineSegment( const Domain& /*domain*/, Particle* /*P*/, double /*dt*/ ) { ignore("LineSegment"); }
  98. virtual void handleTriangle( const Domain& /*domain*/, Particle* /*P*/, double /*dt*/ ) { ignore("Triangle"); }
  99. virtual void handleRectangle( const Domain& /*domain*/, Particle* /*P*/, double /*dt*/ ) { ignore("Rectangle"); }
  100. virtual void handlePlane( const Domain& /*domain*/, Particle* /*P*/, double /*dt*/ ) { ignore("Plane"); }
  101. virtual void handleSphere( const Domain& /*domain*/, Particle* /*P*/, double /*dt*/ ) { ignore("Sphere"); }
  102. virtual void handleBox( const Domain& /*domain*/, Particle* /*P*/, double /*dt*/ ) { ignore("Box"); }
  103. virtual void handleDisk( const Domain& /*domain*/, Particle* /*P*/, double /*dt*/ ) { ignore("Disk"); }
  104. inline void computeNewBasis( const osg::Vec3&, const osg::Vec3&, osg::Vec3&, osg::Vec3& );
  105. inline void ignore( const std::string& func );
  106. std::vector<Domain> _domains;
  107. std::vector<Domain> _backupDomains;
  108. };
  109. // INLINE METHODS
  110. inline void DomainOperator::addPointDomain( const osg::Vec3& p )
  111. {
  112. Domain domain( Domain::POINT_DOMAIN );
  113. domain.v1 = p;
  114. _domains.push_back( domain );
  115. }
  116. inline void DomainOperator::addLineSegmentDomain( const osg::Vec3& v1, const osg::Vec3& v2 )
  117. {
  118. Domain domain( Domain::LINE_DOMAIN );
  119. domain.v1 = v1;
  120. domain.v2 = v2;
  121. domain.r1 = (v2 - v1).length();
  122. _domains.push_back( domain );
  123. }
  124. inline void DomainOperator::addTriangleDomain( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3 )
  125. {
  126. Domain domain( Domain::TRI_DOMAIN );
  127. domain.v1 = v1;
  128. domain.v2 = v2;
  129. domain.v3 = v3;
  130. domain.plane.set(v1, v2, v3);
  131. computeNewBasis( v2-v1, v3-v1, domain.s1, domain.s2 );
  132. _domains.push_back( domain );
  133. }
  134. inline void DomainOperator::addRectangleDomain( const osg::Vec3& corner, const osg::Vec3& w, const osg::Vec3& h )
  135. {
  136. Domain domain( Domain::RECT_DOMAIN );
  137. domain.v1 = corner;
  138. domain.v2 = w;
  139. domain.v3 = h;
  140. domain.plane.set(corner, corner+w, corner+h);
  141. computeNewBasis( w, h, domain.s1, domain.s2 );
  142. _domains.push_back( domain );
  143. }
  144. inline void DomainOperator::addPlaneDomain( const osg::Plane& plane )
  145. {
  146. Domain domain( Domain::PLANE_DOMAIN );
  147. domain.plane.set(plane);
  148. _domains.push_back( domain );
  149. }
  150. inline void DomainOperator::addSphereDomain( const osg::Vec3& c, float r )
  151. {
  152. Domain domain( Domain::SPHERE_DOMAIN );
  153. domain.v1 = c;
  154. domain.r1 = r;
  155. _domains.push_back( domain );
  156. }
  157. inline void DomainOperator::addBoxDomain( const osg::Vec3& min, const osg::Vec3& max )
  158. {
  159. Domain domain( Domain::BOX_DOMAIN );
  160. domain.v1 = min;
  161. domain.v2 = max;
  162. _domains.push_back( domain );
  163. }
  164. inline void DomainOperator::addDiskDomain( const osg::Vec3& c, const osg::Vec3& n, float r1, float r2 )
  165. {
  166. Domain domain( Domain::DISK_DOMAIN );
  167. domain.v1 = c;
  168. domain.v2 = n;
  169. domain.r1 = r1;
  170. domain.r2 = r2;
  171. domain.plane.set(n, c);
  172. _domains.push_back( domain );
  173. }
  174. inline void DomainOperator::computeNewBasis( const osg::Vec3& u, const osg::Vec3& v, osg::Vec3& s1, osg::Vec3& s2 )
  175. {
  176. // Copied from David McAllister's Particle System API (http://www.particlesystems.org), pDomain.h
  177. osg::Vec3 w = u ^ v;
  178. float det = w.z()*u.x()*v.y() - w.z()*u.y()*v.x() - u.z()*w.x()*v.y() -
  179. u.x()*v.z()*w.y() + v.z()*w.x()*u.y() + u.z()*v.x()*w.y();
  180. det = 1.0f / det;
  181. s1.set( v.y()*w.z() - v.z()*w.y(), v.z()*w.x() - v.x()*w.z(), v.x()*w.y() - v.y()*w.x() );
  182. s1 = s1 * det;
  183. s2.set( u.y()*w.z() - u.z()*w.y(), u.z()*w.x() - u.x()*w.z(), u.x()*w.y() - u.y()*w.x() );
  184. s2 = s2 * (-det);
  185. }
  186. inline void DomainOperator::ignore( const std::string& func )
  187. {
  188. OSG_NOTICE << className() << ": " << func << " domain not yet implemented. " << std::endl;
  189. }
  190. }
  191. #endif