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 "BVHTransform.hxx"
28 #include "BVHLineGeometry.hxx"
29 #include "BVHStaticGeometry.hxx"
31 #include "BVHStaticData.hxx"
33 #include "BVHStaticNode.hxx"
34 #include "BVHStaticTriangle.hxx"
35 #include "BVHStaticBinary.hxx"
39 class BVHNearestPointVisitor : public BVHVisitor {
41 BVHNearestPointVisitor(const SGSphered& sphere, const double& t) :
48 virtual void apply(BVHGroup& leaf)
50 if (!intersects(_sphere, leaf.getBoundingSphere()))
54 virtual void apply(BVHTransform& transform)
56 if (!intersects(_sphere, transform.getBoundingSphere()))
59 SGSphered sphere = _sphere;
60 _sphere = transform.sphereToLocal(sphere);
61 bool havePoint = _havePoint;
64 transform.traverse(*this);
67 _point = transform.ptToWorld(_point);
68 _linearVelocity = transform.vecToWorld(_linearVelocity);
69 _angularVelocity = transform.vecToWorld(_angularVelocity);
71 _havePoint |= havePoint;
72 _sphere.setCenter(sphere.getCenter());
74 virtual void apply(BVHMotionTransform& transform)
76 if (!intersects(_sphere, transform.getBoundingSphere()))
79 SGSphered sphere = _sphere;
80 _sphere = transform.sphereToLocal(sphere, _time);
81 bool havePoint = _havePoint;
84 transform.traverse(*this);
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);
95 _havePoint |= havePoint;
96 _sphere.setCenter(sphere.getCenter());
98 virtual void apply(BVHLineGeometry& node)
100 virtual void apply(BVHStaticGeometry& node)
102 if (!intersects(_sphere, node.getBoundingSphere()))
104 node.traverse(*this);
107 virtual void apply(const BVHStaticBinary& node, const BVHStaticData& data)
109 if (!intersects(_sphere, node.getBoundingBox()))
111 node.traverse(*this, data, _sphere.getCenter());
113 virtual void apply(const BVHStaticTriangle& node, const BVHStaticData& data)
115 SGVec3f center(_sphere.getCenter());
116 SGVec3d closest(closestPoint(node.getTriangle(data), center));
117 if (!intersects(_sphere, 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()));
128 void setSphere(const SGSphered& sphere)
129 { _sphere = sphere; }
130 const SGSphered& getSphere() const
133 const SGVec3d& getPoint() const
135 const SGVec3d& getLinearVelocity() const
136 { return _linearVelocity; }
137 const SGVec3d& getAngularVelocity() const
138 { return _angularVelocity; }
139 const SGMaterial* getMaterial() const
140 { return _material; }
142 bool getHavePoint() const
143 { return _havePoint; }
145 { return !_havePoint; }
152 SGVec3d _linearVelocity;
153 SGVec3d _angularVelocity;
154 const SGMaterial* _material;