]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHLineSegmentVisitor.cxx
Initial commit of the bounding volume tree implementation.
[simgear.git] / simgear / scene / bvh / BVHLineSegmentVisitor.cxx
1 // Copyright (C) 2008 - 2009  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
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.
7 //
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.
12 //
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.
16 //
17
18 #include "BVHLineSegmentVisitor.hxx"
19
20 #include <simgear/math/SGGeometry.hxx>
21
22 #include "BVHVisitor.hxx"
23
24 #include "BVHNode.hxx"
25 #include "BVHGroup.hxx"
26 #include "BVHTransform.hxx"
27 #include "BVHMotionTransform.hxx"
28 #include "BVHLineGeometry.hxx"
29 #include "BVHStaticGeometry.hxx"
30
31 #include "BVHStaticData.hxx"
32
33 #include "BVHStaticNode.hxx"
34 #include "BVHStaticLeaf.hxx"
35 #include "BVHStaticTriangle.hxx"
36 #include "BVHStaticBinary.hxx"
37
38 namespace simgear {
39
40 void
41 BVHLineSegmentVisitor::apply(BVHGroup& group)
42 {
43     if (!intersects(_lineSegment, group.getBoundingSphere()))
44         return;
45     group.traverse(*this);
46 }
47     
48 void
49 BVHLineSegmentVisitor::apply(BVHTransform& transform)
50 {
51     if (!intersects(_lineSegment, transform.getBoundingSphere()))
52         return;
53     
54     bool haveHit = _haveHit;
55     _haveHit = false;
56     
57     // Push the line segment
58     SGLineSegmentd lineSegment = getLineSegment();
59     _lineSegment = transform.lineSegmentToLocal(lineSegment);
60     
61     transform.traverse(*this);
62     
63     if (_haveHit) {
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);
69     } else {
70         _lineSegment = lineSegment;
71         _haveHit = haveHit;
72     }
73 }
74
75 void
76 BVHLineSegmentVisitor::apply(BVHMotionTransform& transform)
77 {
78     if (!intersects(_lineSegment, transform.getBoundingSphere()))
79         return;
80     
81     bool haveHit = _haveHit;
82     _haveHit = false;
83
84     // Push the line segment
85     SGLineSegmentd lineSegment = getLineSegment();
86     SGMatrixd toLocal = transform.getToLocalTransform(_time);
87     _lineSegment = lineSegment.transform(toLocal);
88     
89     transform.traverse(*this);
90     
91     if (_haveHit) {
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);
101     } else {
102         _lineSegment = lineSegment;
103         _haveHit = haveHit;
104     }
105 }
106
107 void
108 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
109 {
110 }
111     
112 void
113 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
114 {
115     if (!intersects(_lineSegment, node.getBoundingSphere()))
116         return;
117     node.traverse(*this);
118 }
119
120 void
121 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
122                              const BVHStaticData& data)
123 {
124     if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
125         return;
126     
127     // The first box to enter is the one the startpoint is in.
128     // this increases the probability, that on exit of that box we do not
129     // even need to walk the other one, since the line segment is
130     // then already short enough to not intersect the other one anymore.
131     node.traverse(*this, data, _lineSegment.getStart());
132 }
133
134 void
135 BVHLineSegmentVisitor::apply(const BVHStaticLeaf& node,
136                              const BVHStaticData& data)
137 {
138 }
139
140 void
141 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
142                              const BVHStaticData& data)
143 {
144     SGTrianglef tri = triangle.getTriangle(data);
145     SGVec3f point;
146     if (!intersects(point, tri, SGLineSegmentf(_lineSegment), 1e-4f))
147         return;
148     setLineSegmentEnd(SGVec3d(point));
149     _normal = SGVec3d(tri.getNormal());
150     _linearVelocity = SGVec3d::zeros();
151     _angularVelocity = SGVec3d::zeros();
152     _material = data.getMaterial(triangle.getMaterialIndex());
153     _haveHit = true;
154 }
155
156
157 }