]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHLineSegmentVisitor.cxx
Random buildings - initial commit.
[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 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21
22 #include <simgear/math/SGMath.hxx>
23 #include "BVHLineSegmentVisitor.hxx"
24
25 #include <simgear/math/SGGeometry.hxx>
26
27 #include "BVHVisitor.hxx"
28
29 #include "BVHNode.hxx"
30 #include "BVHGroup.hxx"
31 #include "BVHTransform.hxx"
32 #include "BVHMotionTransform.hxx"
33 #include "BVHLineGeometry.hxx"
34 #include "BVHStaticGeometry.hxx"
35
36 #include "BVHStaticData.hxx"
37
38 #include "BVHStaticNode.hxx"
39 #include "BVHStaticTriangle.hxx"
40 #include "BVHStaticBinary.hxx"
41
42 namespace simgear {
43
44 void
45 BVHLineSegmentVisitor::apply(BVHGroup& group)
46 {
47     if (!intersects(_lineSegment, group.getBoundingSphere()))
48         return;
49     group.traverse(*this);
50 }
51     
52 void
53 BVHLineSegmentVisitor::apply(BVHTransform& transform)
54 {
55     if (!intersects(_lineSegment, transform.getBoundingSphere()))
56         return;
57     
58     bool haveHit = _haveHit;
59     _haveHit = false;
60     
61     // Push the line segment
62     SGLineSegmentd lineSegment = getLineSegment();
63     _lineSegment = transform.lineSegmentToLocal(lineSegment);
64     
65     transform.traverse(*this);
66     
67     if (_haveHit) {
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);
73     } else {
74         _lineSegment = lineSegment;
75         _haveHit = haveHit;
76     }
77 }
78
79 void
80 BVHLineSegmentVisitor::apply(BVHMotionTransform& transform)
81 {
82     if (!intersects(_lineSegment, transform.getBoundingSphere()))
83         return;
84     
85     bool haveHit = _haveHit;
86     _haveHit = false;
87
88     // Push the line segment
89     SGLineSegmentd lineSegment = getLineSegment();
90     SGMatrixd toLocal = transform.getToLocalTransform(_time);
91     _lineSegment = lineSegment.transform(toLocal);
92     
93     transform.traverse(*this);
94     
95     if (_haveHit) {
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);
105         if (!_id)
106             _id = transform.getId();
107     } else {
108         _lineSegment = lineSegment;
109         _haveHit = haveHit;
110     }
111 }
112
113 void
114 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
115 {
116 }
117     
118 void
119 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
120 {
121     if (!intersects(_lineSegment, node.getBoundingSphere()))
122         return;
123     node.traverse(*this);
124 }
125
126 void
127 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
128                              const BVHStaticData& data)
129 {
130     if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
131         return;
132     
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());
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     _id = 0;
154     _haveHit = true;
155 }
156
157
158 }