]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHNearestPointVisitor.hxx
Remove the StaticLeaf visitor slot.
[simgear.git] / simgear / scene / bvh / BVHNearestPointVisitor.hxx
1 // Copyright (C) 2008 - 2009  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
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 GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifndef BVHNearestPointVisitor_hxx
19 #define BVHNearestPointVisitor_hxx
20
21 #include <simgear/math/SGGeometry.hxx>
22
23 #include "BVHVisitor.hxx"
24
25 #include "BVHNode.hxx"
26 #include "BVHGroup.hxx"
27 #include "BVHTransform.hxx"
28 #include "BVHLineGeometry.hxx"
29 #include "BVHStaticGeometry.hxx"
30
31 #include "BVHStaticData.hxx"
32
33 #include "BVHStaticNode.hxx"
34 #include "BVHStaticTriangle.hxx"
35 #include "BVHStaticBinary.hxx"
36
37 namespace simgear {
38
39 class BVHNearestPointVisitor : public BVHVisitor {
40 public:
41     BVHNearestPointVisitor(const SGSphered& sphere, const double& t) :
42         _sphere(sphere),
43         _time(t),
44         _material(0),
45         _havePoint(false)
46     { }
47     
48     virtual void apply(BVHGroup& leaf)
49     {
50         if (!intersects(_sphere, leaf.getBoundingSphere()))
51             return;
52         leaf.traverse(*this);
53     }
54     virtual void apply(BVHTransform& transform)
55     {
56         if (!intersects(_sphere, transform.getBoundingSphere()))
57             return;
58         
59         SGSphered sphere = _sphere;
60         _sphere = transform.sphereToLocal(sphere);
61         bool havePoint = _havePoint;
62         _havePoint = false;
63         
64         transform.traverse(*this);
65         
66         if (_havePoint) {
67             _point = transform.ptToWorld(_point);
68             _linearVelocity = transform.vecToWorld(_linearVelocity);
69             _angularVelocity = transform.vecToWorld(_angularVelocity);
70         }
71         _havePoint |= havePoint;
72         _sphere.setCenter(sphere.getCenter());
73     }
74     virtual void apply(BVHMotionTransform& transform)
75     {
76         if (!intersects(_sphere, transform.getBoundingSphere()))
77             return;
78         
79         SGSphered sphere = _sphere;
80         _sphere = transform.sphereToLocal(sphere, _time);
81         bool havePoint = _havePoint;
82         _havePoint = false;
83         
84         transform.traverse(*this);
85         
86         if (_havePoint) {
87             SGMatrixd toWorld = transform.getToWorldTransform(_time);
88             SGVec3d localCenter = _sphere.getCenter();
89             _linearVelocity += transform.getLinearVelocityAt(localCenter);
90             _angularVelocity += transform.getAngularVelocity();
91             _linearVelocity = toWorld.xformVec(_linearVelocity);
92             _angularVelocity = toWorld.xformVec(_angularVelocity);
93             _point = toWorld.xformPt(_point);
94         }
95         _havePoint |= havePoint;
96         _sphere.setCenter(sphere.getCenter());
97     }
98     virtual void apply(BVHLineGeometry& node)
99     { }
100     virtual void apply(BVHStaticGeometry& node)
101     {
102         if (!intersects(_sphere, node.getBoundingSphere()))
103             return;
104         node.traverse(*this);
105     }
106     
107     virtual void apply(const BVHStaticBinary& node, const BVHStaticData& data)
108     {
109         if (!intersects(_sphere, node.getBoundingBox()))
110             return;
111         node.traverse(*this, data, _sphere.getCenter());
112     }
113     virtual void apply(const BVHStaticTriangle& node, const BVHStaticData& data)
114     {
115         SGVec3f center(_sphere.getCenter());
116         SGVec3d closest(closestPoint(node.getTriangle(data), center));
117         if (!intersects(_sphere, closest))
118             return;
119         _point = closest;
120         _linearVelocity = SGVec3d::zeros();
121         _angularVelocity = SGVec3d::zeros();
122         _material = data.getMaterial(node.getMaterialIndex());
123         // The trick is to decrease the radius of the search sphere.
124         _sphere.setRadius(length(closest - _sphere.getCenter()));
125         _havePoint = true;
126     }
127     
128     void setSphere(const SGSphered& sphere)
129     { _sphere = sphere; }
130     const SGSphered& getSphere() const
131     { return _sphere; }
132     
133     const SGVec3d& getPoint() const
134     { return _point; }
135     const SGVec3d& getLinearVelocity() const
136     { return _linearVelocity; }
137     const SGVec3d& getAngularVelocity() const
138     { return _angularVelocity; }
139     const SGMaterial* getMaterial() const
140     { return _material; }
141     
142     bool getHavePoint() const
143     { return _havePoint; }
144     bool empty() const
145     { return !_havePoint; }
146     
147 private:
148     SGSphered _sphere;
149     double _time;
150
151     SGVec3d _point;
152     SGVec3d _linearVelocity;
153     SGVec3d _angularVelocity;
154     const SGMaterial* _material;
155     
156     bool _havePoint;
157 };
158
159 }
160
161 #endif