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 "BVHLineSegmentVisitor.hxx"
20 #include <simgear/math/SGGeometry.hxx>
22 #include "BVHVisitor.hxx"
24 #include "BVHNode.hxx"
25 #include "BVHGroup.hxx"
26 #include "BVHTransform.hxx"
27 #include "BVHMotionTransform.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"
40 BVHLineSegmentVisitor::apply(BVHGroup& group)
42 if (!intersects(_lineSegment, group.getBoundingSphere()))
44 group.traverse(*this);
48 BVHLineSegmentVisitor::apply(BVHTransform& transform)
50 if (!intersects(_lineSegment, transform.getBoundingSphere()))
53 bool haveHit = _haveHit;
56 // Push the line segment
57 SGLineSegmentd lineSegment = getLineSegment();
58 _lineSegment = transform.lineSegmentToLocal(lineSegment);
60 transform.traverse(*this);
63 _linearVelocity = transform.vecToWorld(_linearVelocity);
64 _angularVelocity = transform.vecToWorld(_angularVelocity);
65 SGVec3d point(transform.ptToWorld(_lineSegment.getEnd()));
66 _lineSegment.set(lineSegment.getStart(), point);
67 _normal = transform.vecToWorld(_normal);
69 _lineSegment = lineSegment;
75 BVHLineSegmentVisitor::apply(BVHMotionTransform& transform)
77 if (!intersects(_lineSegment, transform.getBoundingSphere()))
80 bool haveHit = _haveHit;
83 // Push the line segment
84 SGLineSegmentd lineSegment = getLineSegment();
85 SGMatrixd toLocal = transform.getToLocalTransform(_time);
86 _lineSegment = lineSegment.transform(toLocal);
88 transform.traverse(*this);
91 SGMatrixd toWorld = transform.getToWorldTransform(_time);
92 SGVec3d localStart = _lineSegment.getStart();
93 _linearVelocity += transform.getLinearVelocityAt(localStart);
94 _angularVelocity += transform.getAngularVelocity();
95 _linearVelocity = toWorld.xformVec(_linearVelocity);
96 _angularVelocity = toWorld.xformVec(_angularVelocity);
97 SGVec3d localEnd = _lineSegment.getEnd();
98 _lineSegment.set(lineSegment.getStart(), toWorld.xformPt(localEnd));
99 _normal = toWorld.xformVec(_normal);
101 _id = transform.getId();
103 _lineSegment = lineSegment;
109 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
114 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
116 if (!intersects(_lineSegment, node.getBoundingSphere()))
118 node.traverse(*this);
122 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
123 const BVHStaticData& data)
125 if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
128 // The first box to enter is the one the startpoint is in.
129 // this increases the probability, that on exit of that box we do not
130 // even need to walk the other one, since the line segment is
131 // then already short enough to not intersect the other one anymore.
132 node.traverse(*this, data, _lineSegment.getStart());
136 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
137 const BVHStaticData& data)
139 SGTrianglef tri = triangle.getTriangle(data);
141 if (!intersects(point, tri, SGLineSegmentf(_lineSegment), 1e-4f))
143 setLineSegmentEnd(SGVec3d(point));
144 _normal = SGVec3d(tri.getNormal());
145 _linearVelocity = SGVec3d::zeros();
146 _angularVelocity = SGVec3d::zeros();
147 _material = data.getMaterial(triangle.getMaterialIndex());