]> git.mxchange.org Git - simgear.git/blob - simgear/bvh/BVHLineSegmentVisitor.cxx
Fix PagedLOD for random objects.
[simgear.git] / simgear / 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 "BVHLineSegmentVisitor.hxx"
23
24 #include <simgear/math/SGGeometry.hxx>
25
26 #include "BVHVisitor.hxx"
27
28 #include "BVHNode.hxx"
29 #include "BVHGroup.hxx"
30 #include "BVHPageNode.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(BVHPageNode& pageNode)
54 {
55     if (!intersects(_lineSegment, pageNode.getBoundingSphere()))
56         return;
57     pageNode.traverse(*this);
58 }
59     
60 void
61 BVHLineSegmentVisitor::apply(BVHTransform& transform)
62 {
63     if (!intersects(_lineSegment, transform.getBoundingSphere()))
64         return;
65     
66     bool haveHit = _haveHit;
67     _haveHit = false;
68     
69     // Push the line segment
70     SGLineSegmentd lineSegment = getLineSegment();
71     _lineSegment = transform.lineSegmentToLocal(lineSegment);
72     
73     transform.traverse(*this);
74     
75     if (_haveHit) {
76         _linearVelocity = transform.vecToWorld(_linearVelocity);
77         _angularVelocity = transform.vecToWorld(_angularVelocity);
78         SGVec3d point(transform.ptToWorld(_lineSegment.getEnd()));
79         _lineSegment.set(lineSegment.getStart(), point);
80         _normal = transform.vecToWorld(_normal);
81     } else {
82         _lineSegment = lineSegment;
83         _haveHit = haveHit;
84     }
85 }
86
87 void
88 BVHLineSegmentVisitor::apply(BVHMotionTransform& transform)
89 {
90     if (!intersects(_lineSegment, transform.getBoundingSphere()))
91         return;
92     
93     bool haveHit = _haveHit;
94     _haveHit = false;
95
96     // Push the line segment
97     SGLineSegmentd lineSegment = getLineSegment();
98     SGMatrixd toLocal = transform.getToLocalTransform(_time);
99     _lineSegment = lineSegment.transform(toLocal);
100     
101     transform.traverse(*this);
102     
103     if (_haveHit) {
104         SGMatrixd toWorld = transform.getToWorldTransform(_time);
105         SGVec3d localStart = _lineSegment.getStart();
106         _linearVelocity += transform.getLinearVelocityAt(localStart);
107         _angularVelocity += transform.getAngularVelocity();
108         _linearVelocity = toWorld.xformVec(_linearVelocity);
109         _angularVelocity = toWorld.xformVec(_angularVelocity);
110         SGVec3d localEnd = _lineSegment.getEnd();
111         _lineSegment.set(lineSegment.getStart(), toWorld.xformPt(localEnd));
112         _normal = toWorld.xformVec(_normal);
113         if (!_id)
114             _id = transform.getId();
115     } else {
116         _lineSegment = lineSegment;
117         _haveHit = haveHit;
118     }
119 }
120
121 void
122 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
123 {
124 }
125     
126 void
127 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
128 {
129     if (!intersects(_lineSegment, node.getBoundingSphere()))
130         return;
131     node.traverse(*this);
132 }
133
134 void
135 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
136                              const BVHStaticData& data)
137 {
138     if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
139         return;
140     
141     // The first box to enter is the one the startpoint is in.
142     // this increases the probability, that on exit of that box we do not
143     // even need to walk the other one, since the line segment is
144     // then already short enough to not intersect the other one anymore.
145     node.traverse(*this, data, _lineSegment.getStart());
146 }
147
148 void
149 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
150                              const BVHStaticData& data)
151 {
152     SGTrianglef tri = triangle.getTriangle(data);
153     SGVec3f point;
154     if (!intersects(point, tri, SGLineSegmentf(_lineSegment), 1e-4f))
155         return;
156     setLineSegmentEnd(SGVec3d(point));
157     _normal = SGVec3d(tri.getNormal());
158     _linearVelocity = SGVec3d::zeros();
159     _angularVelocity = SGVec3d::zeros();
160     _material = data.getMaterial(triangle.getMaterialIndex());
161     _id = 0;
162     _haveHit = true;
163 }
164
165
166 }