]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/BVHLineSegmentVisitor.cxx
simgear/scene/sky/sky.cxx: Include sg_inlines.h with simgear/ prefix as all other...
[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 <simgear/math/SGMath.hxx>
19 #include "BVHLineSegmentVisitor.hxx"
20
21 #include <simgear/math/SGGeometry.hxx>
22
23 #include "BVHVisitor.hxx"
24
25 #include "BVHNode.hxx"
26 #include "BVHGroup.hxx"
27 #include "BVHTransform.hxx"
28 #include "BVHMotionTransform.hxx"
29 #include "BVHLineGeometry.hxx"
30 #include "BVHStaticGeometry.hxx"
31
32 #include "BVHStaticData.hxx"
33
34 #include "BVHStaticNode.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         if (!_id)
102             _id = transform.getId();
103     } else {
104         _lineSegment = lineSegment;
105         _haveHit = haveHit;
106     }
107 }
108
109 void
110 BVHLineSegmentVisitor::apply(BVHLineGeometry&)
111 {
112 }
113     
114 void
115 BVHLineSegmentVisitor::apply(BVHStaticGeometry& node)
116 {
117     if (!intersects(_lineSegment, node.getBoundingSphere()))
118         return;
119     node.traverse(*this);
120 }
121
122 void
123 BVHLineSegmentVisitor::apply(const BVHStaticBinary& node,
124                              const BVHStaticData& data)
125 {
126     if (!intersects(SGLineSegmentf(_lineSegment), node.getBoundingBox()))
127         return;
128     
129     // The first box to enter is the one the startpoint is in.
130     // this increases the probability, that on exit of that box we do not
131     // even need to walk the other one, since the line segment is
132     // then already short enough to not intersect the other one anymore.
133     node.traverse(*this, data, _lineSegment.getStart());
134 }
135
136 void
137 BVHLineSegmentVisitor::apply(const BVHStaticTriangle& triangle,
138                              const BVHStaticData& data)
139 {
140     SGTrianglef tri = triangle.getTriangle(data);
141     SGVec3f point;
142     if (!intersects(point, tri, SGLineSegmentf(_lineSegment), 1e-4f))
143         return;
144     setLineSegmentEnd(SGVec3d(point));
145     _normal = SGVec3d(tri.getNormal());
146     _linearVelocity = SGVec3d::zeros();
147     _angularVelocity = SGVec3d::zeros();
148     _material = data.getMaterial(triangle.getMaterialIndex());
149     _id = 0;
150     _haveHit = true;
151 }
152
153
154 }