SinkOperator 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_SINKOPERATOR
  15. #define OSGPARTICLE_SINKOPERATOR
  16. #include <osgParticle/Particle>
  17. #include <osgParticle/DomainOperator>
  18. namespace osgParticle
  19. {
  20. /** A sink operator kills particles if positions or velocities inside/outside the specified domain.
  21. Refer to David McAllister's Particle System API (http://www.particlesystems.org)
  22. */
  23. class OSGPARTICLE_EXPORT SinkOperator : public DomainOperator
  24. {
  25. public:
  26. enum SinkTarget { SINK_POSITION, SINK_VELOCITY, SINK_ANGULAR_VELOCITY };
  27. enum SinkStrategy { SINK_INSIDE, SINK_OUTSIDE };
  28. SinkOperator()
  29. : DomainOperator(), _sinkTarget(SINK_POSITION), _sinkStrategy(SINK_INSIDE)
  30. {}
  31. SinkOperator( const SinkOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
  32. : DomainOperator(copy, copyop), _sinkTarget(copy._sinkTarget), _sinkStrategy(copy._sinkStrategy)
  33. {}
  34. META_Object( osgParticle, SinkOperator );
  35. /// Set the sink strategy
  36. void setSinkTarget( SinkTarget so ) { _sinkTarget = so; }
  37. /// Get the sink strategy
  38. SinkTarget getSinkTarget() const { return _sinkTarget; }
  39. /// Set the sink strategy
  40. void setSinkStrategy( SinkStrategy ss ) { _sinkStrategy = ss; }
  41. /// Get the sink strategy
  42. SinkStrategy getSinkStrategy() const { return _sinkStrategy; }
  43. /// Perform some initializations. Do not call this method manually.
  44. void beginOperate( Program* prg );
  45. protected:
  46. virtual ~SinkOperator() {}
  47. SinkOperator& operator=( const SinkOperator& ) { return *this; }
  48. virtual void handlePoint( const Domain& domain, Particle* P, double dt );
  49. virtual void handleLineSegment( const Domain& domain, Particle* P, double dt );
  50. virtual void handleTriangle( const Domain& domain, Particle* P, double dt );
  51. virtual void handleRectangle( const Domain& domain, Particle* P, double dt );
  52. virtual void handlePlane( const Domain& domain, Particle* P, double dt );
  53. virtual void handleSphere( const Domain& domain, Particle* P, double dt );
  54. virtual void handleBox( const Domain& domain, Particle* P, double dt );
  55. virtual void handleDisk( const Domain& domain, Particle* P, double dt );
  56. inline const osg::Vec3& getValue( Particle* P );
  57. inline void kill( Particle* P, bool insideDomain );
  58. SinkTarget _sinkTarget;
  59. SinkStrategy _sinkStrategy;
  60. };
  61. // INLINE METHODS
  62. inline const osg::Vec3& SinkOperator::getValue( Particle* P )
  63. {
  64. switch ( _sinkTarget )
  65. {
  66. case SINK_VELOCITY: return P->getVelocity();
  67. case SINK_ANGULAR_VELOCITY: return P->getAngularVelocity();
  68. case SINK_POSITION: default: return P->getPosition();
  69. }
  70. }
  71. inline void SinkOperator::kill( Particle* P, bool insideDomain )
  72. {
  73. if ( !((_sinkStrategy==SINK_INSIDE) ^ insideDomain) )
  74. P->kill();
  75. }
  76. }
  77. #endif