]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHLineSegmentVisitor.cxx
Suppress warnings
[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         if (!_id)
101             _id = transform.getId();
102     } else {
103         _lineSegment = lineSegment;
104         _haveHit = haveHit;
105     }
106 }
107
108 void
109 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
110 {
111 }
112     
113 void
114 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
115 {
116     if (!intersects(_lineSegment, node.getBoundingSphere()))
117         return;
118     node.traverse(*this);
119 }
120
121 void
122 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
123                              const BVHStaticData& data)
124 {
125     if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
126         return;
127     
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());
133 }
134
135 void
136 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
137                              const BVHStaticData& data)
138 {
139     SGTrianglef tri = triangle.getTriangle(data);
140     SGVec3f point;
141     if (!intersects(point, tri, SGLineSegmentf(_lineSegment), 1e-4f))
142         return;
143     setLineSegmentEnd(SGVec3d(point));
144     _normal = SGVec3d(tri.getNormal());
145     _linearVelocity = SGVec3d::zeros();
146     _angularVelocity = SGVec3d::zeros();
147     _material = data.getMaterial(triangle.getMaterialIndex());
148     _id = 0;
149     _haveHit = true;
150 }
151
152
153 }