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 | #include <osg/Notify> |
---|
16 | #include <osgParticle/ModularProgram> |
---|
17 | #include <osgParticle/SinkOperator> |
---|
18 | |
---|
19 | #define SINK_EPSILON 1e-3 |
---|
20 | |
---|
21 | using namespace osgParticle; |
---|
22 | |
---|
23 | void SinkOperator::beginOperate( Program* prg ) |
---|
24 | { |
---|
25 | // Don't transform domains if they are used for sinking velocities |
---|
26 | if ( _sinkTarget==SINK_POSITION ) |
---|
27 | DomainOperator::beginOperate(prg ); |
---|
28 | } |
---|
29 | |
---|
30 | void SinkOperator::handlePoint( const Domain& domain, Particle* P, double dt ) |
---|
31 | { |
---|
32 | const osg::Vec3& value = getValue(P); |
---|
33 | kill( P, (domain.v1==value) ); |
---|
34 | } |
---|
35 | |
---|
36 | void SinkOperator::handleLineSegment( const Domain& domain, Particle* P, double dt ) |
---|
37 | { |
---|
38 | const osg::Vec3& value = getValue(P); |
---|
39 | osg::Vec3 offset = value - domain.v1, normal = domain.v2 - domain.v1; |
---|
40 | normal.normalize(); |
---|
41 | |
---|
42 | float diff = fabs(normal*offset - offset.length()) / domain.r1; |
---|
43 | kill( P, (diff<SINK_EPSILON) ); |
---|
44 | } |
---|
45 | |
---|
46 | void SinkOperator::handleTriangle( const Domain& domain, Particle* P, double dt ) |
---|
47 | { |
---|
48 | bool insideDomain = false; |
---|
49 | const osg::Vec3& value = getValue(P); |
---|
50 | osg::Vec3 offset = value - domain.v1; |
---|
51 | if ( offset*domain.plane.getNormal()>SINK_EPSILON ) |
---|
52 | insideDomain = false; |
---|
53 | else |
---|
54 | { |
---|
55 | float upos = offset * domain.s1; |
---|
56 | float vpos = offset * domain.s2; |
---|
57 | insideDomain = !(upos<0.0f || vpos<0.0f || (upos+vpos)>1.0f); |
---|
58 | } |
---|
59 | kill( P, insideDomain ); |
---|
60 | } |
---|
61 | |
---|
62 | void SinkOperator::handleRectangle( const Domain& domain, Particle* P, double dt ) |
---|
63 | { |
---|
64 | bool insideDomain = false; |
---|
65 | const osg::Vec3& value = getValue(P); |
---|
66 | osg::Vec3 offset = value - domain.v1; |
---|
67 | if ( offset*domain.plane.getNormal()>SINK_EPSILON ) |
---|
68 | insideDomain = false; |
---|
69 | else |
---|
70 | { |
---|
71 | float upos = offset * domain.s1; |
---|
72 | float vpos = offset * domain.s2; |
---|
73 | insideDomain = !(upos<0.0f || upos>1.0f || vpos<0.0f || vpos>1.0f); |
---|
74 | } |
---|
75 | kill( P, insideDomain ); |
---|
76 | } |
---|
77 | |
---|
78 | void SinkOperator::handlePlane( const Domain& domain, Particle* P, double dt ) |
---|
79 | { |
---|
80 | const osg::Vec3& value = getValue(P); |
---|
81 | bool insideDomain = (domain.plane.getNormal()*value>=-domain.plane[3]); |
---|
82 | kill( P, insideDomain ); |
---|
83 | } |
---|
84 | |
---|
85 | void SinkOperator::handleSphere( const Domain& domain, Particle* P, double dt ) |
---|
86 | { |
---|
87 | const osg::Vec3& value = getValue(P); |
---|
88 | float r = (value - domain.v1).length(); |
---|
89 | kill( P, (r<=domain.r1) ); |
---|
90 | } |
---|
91 | |
---|
92 | void SinkOperator::handleBox( const Domain& domain, Particle* P, double dt ) |
---|
93 | { |
---|
94 | const osg::Vec3& value = getValue(P); |
---|
95 | bool insideDomain = !( |
---|
96 | (value.x() < domain.v1.x()) || (value.x() > domain.v2.x()) || |
---|
97 | (value.y() < domain.v1.y()) || (value.y() > domain.v2.y()) || |
---|
98 | (value.z() < domain.v1.z()) || (value.z() > domain.v2.z()) |
---|
99 | ); |
---|
100 | kill( P, insideDomain ); |
---|
101 | } |
---|
102 | |
---|
103 | void SinkOperator::handleDisk( const Domain& domain, Particle* P, double dt ) |
---|
104 | { |
---|
105 | bool insideDomain = false; |
---|
106 | const osg::Vec3& value = getValue(P); |
---|
107 | osg::Vec3 offset = value - domain.v1; |
---|
108 | if ( offset*domain.v2>SINK_EPSILON ) |
---|
109 | insideDomain = false; |
---|
110 | else |
---|
111 | { |
---|
112 | float length = offset.length(); |
---|
113 | insideDomain = (length<=domain.r1 && length>=domain.r2); |
---|
114 | } |
---|
115 | kill( P, insideDomain ); |
---|
116 | } |
---|