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 #include <simgear/Math/SGMath.hxx>
19 #include "BVHLineSegmentVisitor.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 "BVHMotionTransform.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"
41 BVHLineSegmentVisitor::apply(BVHGroup& group)
43 if (!intersects(_lineSegment, group.getBoundingSphere()))
45 group.traverse(*this);
49 BVHLineSegmentVisitor::apply(BVHTransform& transform)
51 if (!intersects(_lineSegment, transform.getBoundingSphere()))
54 bool haveHit = _haveHit;
57 // Push the line segment
58 SGLineSegmentd lineSegment = getLineSegment();
59 _lineSegment = transform.lineSegmentToLocal(lineSegment);
61 transform.traverse(*this);
64 _linearVelocity = transform.vecToWorld(_linearVelocity);
65 _angularVelocity = transform.vecToWorld(_angularVelocity);
66 SGVec3d point(transform.ptToWorld(_lineSegment.getEnd()));
67 _lineSegment.set(lineSegment.getStart(), point);
68 _normal = transform.vecToWorld(_normal);
70 _lineSegment = lineSegment;
76 BVHLineSegmentVisitor::apply(BVHMotionTransform& transform)
78 if (!intersects(_lineSegment, transform.getBoundingSphere()))
81 bool haveHit = _haveHit;
84 // Push the line segment
85 SGLineSegmentd lineSegment = getLineSegment();
86 SGMatrixd toLocal = transform.getToLocalTransform(_time);
87 _lineSegment = lineSegment.transform(toLocal);
89 transform.traverse(*this);
92 SGMatrixd toWorld = transform.getToWorldTransform(_time);
93 SGVec3d localStart = _lineSegment.getStart();
94 _linearVelocity += transform.getLinearVelocityAt(localStart);
95 _angularVelocity += transform.getAngularVelocity();
96 _linearVelocity = toWorld.xformVec(_linearVelocity);
97 _angularVelocity = toWorld.xformVec(_angularVelocity);
98 SGVec3d localEnd = _lineSegment.getEnd();
99 _lineSegment.set(lineSegment.getStart(), toWorld.xformPt(localEnd));
100 _normal = toWorld.xformVec(_normal);
102 _id = transform.getId();
104 _lineSegment = lineSegment;
110 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
115 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
117 if (!intersects(_lineSegment, node.getBoundingSphere()))
119 node.traverse(*this);
123 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
124 const BVHStaticData& data)
126 if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
129 // The first box to enter is the one the startpoint is in.
130 // this increases the probability, that on exit of that box we do not
131 // even need to walk the other one, since the line segment is
132 // then already short enough to not intersect the other one anymore.
133 node.traverse(*this, data, _lineSegment.getStart());
137 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
138 const BVHStaticData& data)
140 SGTrianglef tri = triangle.getTriangle(data);
142 if (!intersects(point, tri, SGLineSegmentf(_lineSegment), 1e-4f))
144 setLineSegmentEnd(SGVec3d(point));
145 _normal = SGVec3d(tri.getNormal());
146 _linearVelocity = SGVec3d::zeros();
147 _angularVelocity = SGVec3d::zeros();
148 _material = data.getMaterial(triangle.getMaterialIndex());