1 // Copyright (C) 2008 - 2009 Mathias Froehlich - Mathias.Froehlich@web.de
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.
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.
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.
18 #ifndef BVHNearestPointVisitor_hxx
19 #define BVHNearestPointVisitor_hxx
21 #include <simgear/math/SGGeometry.hxx>
23 #include "BVHVisitor.hxx"
25 #include "BVHNode.hxx"
26 #include "BVHGroup.hxx"
27 #include "BVHPageNode.hxx"
28 #include "BVHTransform.hxx"
29 #include "BVHLineGeometry.hxx"
30 #include "BVHStaticGeometry.hxx"
32 #include "BVHStaticData.hxx"
34 #include "BVHStaticNode.hxx"
35 #include "BVHStaticTriangle.hxx"
36 #include "BVHStaticBinary.hxx"
40 class BVHNearestPointVisitor : public BVHVisitor {
42 BVHNearestPointVisitor(const SGSphered& sphere, const double& t) :
50 virtual void apply(BVHGroup& leaf)
52 if (!intersects(_sphere, leaf.getBoundingSphere()))
56 virtual void apply(BVHPageNode& leaf)
58 if (!intersects(_sphere, leaf.getBoundingSphere()))
62 virtual void apply(BVHTransform& transform)
64 if (!intersects(_sphere, transform.getBoundingSphere()))
67 SGSphered sphere = _sphere;
68 _sphere = transform.sphereToLocal(sphere);
69 bool havePoint = _havePoint;
72 transform.traverse(*this);
75 _point = transform.ptToWorld(_point);
76 _linearVelocity = transform.vecToWorld(_linearVelocity);
77 _angularVelocity = transform.vecToWorld(_angularVelocity);
79 _havePoint |= havePoint;
80 _sphere.setCenter(sphere.getCenter());
82 virtual void apply(BVHMotionTransform& transform)
84 if (!intersects(_sphere, transform.getBoundingSphere()))
87 SGSphered sphere = _sphere;
88 _sphere = transform.sphereToLocal(sphere, _time);
89 bool havePoint = _havePoint;
92 transform.traverse(*this);
95 SGMatrixd toWorld = transform.getToWorldTransform(_time);
96 SGVec3d localCenter = _sphere.getCenter();
97 _linearVelocity += transform.getLinearVelocityAt(localCenter);
98 _angularVelocity += transform.getAngularVelocity();
99 _linearVelocity = toWorld.xformVec(_linearVelocity);
100 _angularVelocity = toWorld.xformVec(_angularVelocity);
101 _point = toWorld.xformPt(_point);
103 _id = transform.getId();
105 _havePoint |= havePoint;
106 _sphere.setCenter(sphere.getCenter());
108 virtual void apply(BVHLineGeometry& node)
110 virtual void apply(BVHStaticGeometry& node)
112 if (!intersects(_sphere, node.getBoundingSphere()))
114 node.traverse(*this);
117 virtual void apply(const BVHStaticBinary& node, const BVHStaticData& data)
119 if (!intersects(_sphere, node.getBoundingBox()))
121 node.traverse(*this, data, _sphere.getCenter());
123 virtual void apply(const BVHStaticTriangle& node, const BVHStaticData& data)
125 SGVec3f center(_sphere.getCenter());
126 SGVec3d closest(closestPoint(node.getTriangle(data), center));
127 if (!intersects(_sphere, closest))
130 _linearVelocity = SGVec3d::zeros();
131 _angularVelocity = SGVec3d::zeros();
132 _material = data.getMaterial(node.getMaterialIndex());
133 // The trick is to decrease the radius of the search sphere.
134 _sphere.setRadius(length(closest - _sphere.getCenter()));
139 void setSphere(const SGSphered& sphere)
140 { _sphere = sphere; }
141 const SGSphered& getSphere() const
144 const SGVec3d& getPoint() const
146 const SGVec3d& getLinearVelocity() const
147 { return _linearVelocity; }
148 const SGVec3d& getAngularVelocity() const
149 { return _angularVelocity; }
150 const BVHMaterial* getMaterial() const
151 { return _material; }
152 BVHNode::Id getId() const
156 { return !_havePoint; }
163 SGVec3d _linearVelocity;
164 SGVec3d _angularVelocity;
165 const BVHMaterial* _material;