1 // Copyright (C) 2008 - 2009 Mathias Froehlich - Mathias.Froehlich@web.de
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.
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.
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.
19 #include <simgear/structure/SGSharedPtr.hxx>
21 #include "BVHNode.hxx"
22 #include "BVHGroup.hxx"
23 #include "BVHTransform.hxx"
25 #include "BVHStaticData.hxx"
27 #include "BVHStaticNode.hxx"
28 #include "BVHStaticLeaf.hxx"
29 #include "BVHStaticTriangle.hxx"
30 #include "BVHStaticBinary.hxx"
31 #include "BVHStaticGeometry.hxx"
33 #include "BVHBoundingBoxVisitor.hxx"
34 #include "BVHSubTreeCollector.hxx"
35 #include "BVHLineSegmentVisitor.hxx"
36 #include "BVHNearestPointVisitor.hxx"
38 using namespace simgear;
41 buildSingleTriangle(const SGVec3f& v1, const SGVec3f& v2, const SGVec3f& v3)
43 BVHStaticData* staticData = new BVHStaticData;
44 unsigned indices[3] = {
45 staticData->addVertex(v1),
46 staticData->addVertex(v2),
47 staticData->addVertex(v3)
49 BVHStaticTriangle* staticTriangle = new BVHStaticTriangle(~0u, indices);
50 return new BVHStaticGeometry(staticTriangle, staticData);
54 testLineIntersections()
56 SGVec3f v1(-1, -1, 0);
59 SGSharedPtr<BVHNode> node = buildSingleTriangle(v1, v2, v3);
61 SGLineSegmentd lineSegment(SGVec3d(0, 0, -1), SGVec3d(0, 0, 1));
63 BVHLineSegmentVisitor lineSegmentVisitor(lineSegment);
64 node->accept(lineSegmentVisitor);
65 if (lineSegmentVisitor.empty())
67 if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
71 SGVec3d position(1000, 1000, 1000);
72 SGMatrixd matrix(position);
73 SGSharedPtr<BVHTransform> transform1 = new BVHTransform;
74 transform1->setToWorldTransform(matrix);
75 transform1->addChild(node);
77 SGSharedPtr<BVHTransform> transform2 = new BVHTransform;
78 transform2->setToLocalTransform(matrix);
79 transform2->addChild(transform1);
82 BVHLineSegmentVisitor lineSegmentVisitor(lineSegment);
83 transform2->accept(lineSegmentVisitor);
84 if (lineSegmentVisitor.empty())
86 if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
90 SGSharedPtr<BVHMotionTransform> transform3 = new BVHMotionTransform;
91 transform3->setLinearVelocity(SGVec3d(0, 0, 1));
92 transform3->setAngularVelocity(SGVec3d(1, 0, 0));
93 transform3->addChild(node);
96 BVHLineSegmentVisitor lineSegmentVisitor(lineSegment, 0);
97 transform3->accept(lineSegmentVisitor);
98 if (lineSegmentVisitor.empty())
100 if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
102 if (!equivalent(lineSegmentVisitor.getLinearVelocity(),
105 if (!equivalent(lineSegmentVisitor.getAngularVelocity(),
116 SGVec3f v1(-1, -1, 0);
117 SGVec3f v2(1, -1, 0);
118 SGVec3f v3(-1, 1, 0);
119 SGSharedPtr<BVHNode> node = buildSingleTriangle(v1, v2, v3);
121 SGSphered sphere(SGVec3d(0, 0, -1), 2);
123 BVHNearestPointVisitor nearestPointVisitor(sphere, 0);
124 node->accept(nearestPointVisitor);
125 if (nearestPointVisitor.empty())
127 if (!equivalent(nearestPointVisitor.getPoint(), SGVec3d(0, 0, 0)))
135 main(int argc, char** argv)
137 if (!testLineIntersections())
139 if (!testNearestPoint())