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