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 <simgear/math/SGMath.hxx>
23 #include "BVHLineSegmentVisitor.hxx"
25 #include <simgear/math/SGGeometry.hxx>
27 #include "BVHVisitor.hxx"
29 #include "BVHNode.hxx"
30 #include "BVHGroup.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(BVHTransform& transform)
55 if (!intersects(_lineSegment, transform.getBoundingSphere()))
58 bool haveHit = _haveHit;
61 // Push the line segment
62 SGLineSegmentd lineSegment = getLineSegment();
63 _lineSegment = transform.lineSegmentToLocal(lineSegment);
65 transform.traverse(*this);
68 _linearVelocity = transform.vecToWorld(_linearVelocity);
69 _angularVelocity = transform.vecToWorld(_angularVelocity);
70 SGVec3d point(transform.ptToWorld(_lineSegment.getEnd()));
71 _lineSegment.set(lineSegment.getStart(), point);
72 _normal = transform.vecToWorld(_normal);
74 _lineSegment = lineSegment;
80 BVHLineSegmentVisitor::apply(BVHMotionTransform& transform)
82 if (!intersects(_lineSegment, transform.getBoundingSphere()))
85 bool haveHit = _haveHit;
88 // Push the line segment
89 SGLineSegmentd lineSegment = getLineSegment();
90 SGMatrixd toLocal = transform.getToLocalTransform(_time);
91 _lineSegment = lineSegment.transform(toLocal);
93 transform.traverse(*this);
96 SGMatrixd toWorld = transform.getToWorldTransform(_time);
97 SGVec3d localStart = _lineSegment.getStart();
98 _linearVelocity += transform.getLinearVelocityAt(localStart);
99 _angularVelocity += transform.getAngularVelocity();
100 _linearVelocity = toWorld.xformVec(_linearVelocity);
101 _angularVelocity = toWorld.xformVec(_angularVelocity);
102 SGVec3d localEnd = _lineSegment.getEnd();
103 _lineSegment.set(lineSegment.getStart(), toWorld.xformPt(localEnd));
104 _normal = toWorld.xformVec(_normal);
106 _id = transform.getId();
108 _lineSegment = lineSegment;
114 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
119 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
121 if (!intersects(_lineSegment, node.getBoundingSphere()))
123 node.traverse(*this);
127 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
128 const BVHStaticData& data)
130 if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
133 // The first box to enter is the one the startpoint is in.
134 // this increases the probability, that on exit of that box we do not
135 // even need to walk the other one, since the line segment is
136 // then already short enough to not intersect the other one anymore.
137 node.traverse(*this, data, _lineSegment.getStart());
141 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
142 const BVHStaticData& data)
144 SGTrianglef tri = triangle.getTriangle(data);
146 if (!intersects(point, tri, SGLineSegmentf(_lineSegment), 1e-4f))
148 setLineSegmentEnd(SGVec3d(point));
149 _normal = SGVec3d(tri.getNormal());
150 _linearVelocity = SGVec3d::zeros();
151 _angularVelocity = SGVec3d::zeros();
152 _material = data.getMaterial(triangle.getMaterialIndex());