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.
19 # include <simgear_config.h>
22 #include "BVHLineSegmentVisitor.hxx"
24 #include <simgear/math/SGGeometry.hxx>
26 #include "BVHVisitor.hxx"
28 #include "BVHNode.hxx"
29 #include "BVHGroup.hxx"
30 #include "BVHPageNode.hxx"
31 #include "BVHTransform.hxx"
32 #include "BVHMotionTransform.hxx"
33 #include "BVHLineGeometry.hxx"
34 #include "BVHStaticGeometry.hxx"
36 #include "BVHStaticData.hxx"
38 #include "BVHStaticNode.hxx"
39 #include "BVHStaticTriangle.hxx"
40 #include "BVHStaticBinary.hxx"
45 BVHLineSegmentVisitor::apply(BVHGroup& group)
47 if (!intersects(_lineSegment, group.getBoundingSphere()))
49 group.traverse(*this);
53 BVHLineSegmentVisitor::apply(BVHPageNode& pageNode)
55 if (!intersects(_lineSegment, pageNode.getBoundingSphere()))
57 pageNode.traverse(*this);
61 BVHLineSegmentVisitor::apply(BVHTransform& transform)
63 if (!intersects(_lineSegment, transform.getBoundingSphere()))
66 bool haveHit = _haveHit;
69 // Push the line segment
70 SGLineSegmentd lineSegment = getLineSegment();
71 _lineSegment = transform.lineSegmentToLocal(lineSegment);
73 transform.traverse(*this);
76 _linearVelocity = transform.vecToWorld(_linearVelocity);
77 _angularVelocity = transform.vecToWorld(_angularVelocity);
78 SGVec3d point(transform.ptToWorld(_lineSegment.getEnd()));
79 _lineSegment.set(lineSegment.getStart(), point);
80 _normal = transform.vecToWorld(_normal);
82 _lineSegment = lineSegment;
88 BVHLineSegmentVisitor::apply(BVHMotionTransform& transform)
90 if (!intersects(_lineSegment, transform.getBoundingSphere()))
93 bool haveHit = _haveHit;
96 // Push the line segment
97 SGLineSegmentd lineSegment = getLineSegment();
98 SGMatrixd toLocal = transform.getToLocalTransform(_time);
99 _lineSegment = lineSegment.transform(toLocal);
101 transform.traverse(*this);
104 SGMatrixd toWorld = transform.getToWorldTransform(_time);
105 SGVec3d localStart = _lineSegment.getStart();
106 _linearVelocity += transform.getLinearVelocityAt(localStart);
107 _angularVelocity += transform.getAngularVelocity();
108 _linearVelocity = toWorld.xformVec(_linearVelocity);
109 _angularVelocity = toWorld.xformVec(_angularVelocity);
110 SGVec3d localEnd = _lineSegment.getEnd();
111 _lineSegment.set(lineSegment.getStart(), toWorld.xformPt(localEnd));
112 _normal = toWorld.xformVec(_normal);
114 _id = transform.getId();
116 _lineSegment = lineSegment;
122 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
127 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
129 if (!intersects(_lineSegment, node.getBoundingSphere()))
131 node.traverse(*this);
135 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
136 const BVHStaticData& data)
138 if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
141 // The first box to enter is the one the startpoint is in.
142 // this increases the probability, that on exit of that box we do not
143 // even need to walk the other one, since the line segment is
144 // then already short enough to not intersect the other one anymore.
145 node.traverse(*this, data, _lineSegment.getStart());
149 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
150 const BVHStaticData& data)
152 SGTrianglef tri = triangle.getTriangle(data);
154 if (!intersects(point, tri, SGLineSegmentf(_lineSegment), 1e-4f))
156 setLineSegmentEnd(SGVec3d(point));
157 _normal = SGVec3d(tri.getNormal());
158 _linearVelocity = SGVec3d::zeros();
159 _angularVelocity = SGVec3d::zeros();
160 _material = data.getMaterial(triangle.getMaterialIndex());