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