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"
37 using namespace simgear;
40 buildSingleTriangle(const SGVec3f& v1, const SGVec3f& v2, const SGVec3f& v3)
42 BVHStaticData* staticData = new BVHStaticData;
43 unsigned indices[3] = {
44 staticData->addVertex(v1),
45 staticData->addVertex(v2),
46 staticData->addVertex(v3)
48 BVHStaticTriangle* staticTriangle = new BVHStaticTriangle(~0u, indices);
49 return new BVHStaticGeometry(staticTriangle, staticData);
53 testLineIntersections()
55 SGVec3f v1(-1, -1, 0);
58 SGSharedPtr<BVHNode> node = buildSingleTriangle(v1, v2, v3);
60 SGLineSegmentd lineSegment(SGVec3d(0, 0, -1), SGVec3d(0, 0, 1));
62 BVHLineSegmentVisitor lineSegmentVisitor(lineSegment);
63 node->accept(lineSegmentVisitor);
64 if (lineSegmentVisitor.empty())
66 if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
70 SGVec3d position(1000, 1000, 1000);
71 SGMatrixd matrix(position);
72 SGSharedPtr<BVHTransform> transform1 = new BVHTransform;
73 transform1->setToWorldTransform(matrix);
74 transform1->addChild(node);
76 SGSharedPtr<BVHTransform> transform2 = new BVHTransform;
77 transform2->setToLocalTransform(matrix);
78 transform2->addChild(transform1);
81 BVHLineSegmentVisitor lineSegmentVisitor(lineSegment);
82 transform2->accept(lineSegmentVisitor);
83 if (lineSegmentVisitor.empty())
85 if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
89 SGSharedPtr<BVHMotionTransform> transform3 = new BVHMotionTransform;
90 transform3->setLinearVelocity(SGVec3d(0, 0, 1));
91 transform3->setAngularVelocity(SGVec3d(1, 0, 0));
92 transform3->addChild(node);
95 BVHLineSegmentVisitor lineSegmentVisitor(lineSegment, 0);
96 transform3->accept(lineSegmentVisitor);
97 if (lineSegmentVisitor.empty())
99 if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
101 if (!equivalent(lineSegmentVisitor.getLinearVelocity(),
104 if (!equivalent(lineSegmentVisitor.getAngularVelocity(),
113 main(int argc, char** argv)
115 if (!testLineIntersections())