]> git.mxchange.org Git - simgear.git/blob - simgear/scene/bvh/bvhtest.cxx
Initial commit of the bounding volume tree implementation.
[simgear.git] / simgear / scene / bvh / bvhtest.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 <iostream>
19 #include <simgear/structure/SGSharedPtr.hxx>
20
21 #include "BVHNode.hxx"
22 #include "BVHGroup.hxx"
23 #include "BVHTransform.hxx"
24
25 #include "BVHStaticData.hxx"
26
27 #include "BVHStaticNode.hxx"
28 #include "BVHStaticLeaf.hxx"
29 #include "BVHStaticTriangle.hxx"
30 #include "BVHStaticBinary.hxx"
31 #include "BVHStaticGeometry.hxx"
32
33 #include "BVHBoundingBoxVisitor.hxx"
34 #include "BVHSubTreeCollector.hxx"
35 #include "BVHLineSegmentVisitor.hxx"
36
37 using namespace simgear;
38
39 BVHNode*
40 buildSingleTriangle(const SGVec3f& v1, const SGVec3f& v2, const SGVec3f& v3)
41 {
42     BVHStaticData* staticData = new BVHStaticData;
43     unsigned indices[3] = {
44         staticData->addVertex(v1),
45         staticData->addVertex(v2),
46         staticData->addVertex(v3)
47     };
48     BVHStaticTriangle* staticTriangle = new BVHStaticTriangle(~0u, indices);
49     return new BVHStaticGeometry(staticTriangle, staticData);
50 }
51
52 bool
53 testLineIntersections()
54 {
55     SGVec3f v1(-1, -1, 0);
56     SGVec3f v2(1, -1, 0);
57     SGVec3f v3(-1, 1, 0);
58     SGSharedPtr<BVHNode> node = buildSingleTriangle(v1, v2, v3);
59
60     SGLineSegmentd lineSegment(SGVec3d(0, 0, -1), SGVec3d(0, 0, 1));
61     {
62         BVHLineSegmentVisitor lineSegmentVisitor(lineSegment);
63         node->accept(lineSegmentVisitor);
64         if (lineSegmentVisitor.empty())
65             return false;
66         if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
67             return false;
68     }
69
70     SGVec3d position(1000, 1000, 1000);
71     SGMatrixd matrix(position);
72     SGSharedPtr<BVHTransform> transform1 = new BVHTransform;
73     transform1->setToWorldTransform(matrix);
74     transform1->addChild(node);
75
76     SGSharedPtr<BVHTransform> transform2 = new BVHTransform;
77     transform2->setToLocalTransform(matrix);
78     transform2->addChild(transform1);
79
80     {
81         BVHLineSegmentVisitor lineSegmentVisitor(lineSegment);
82         transform2->accept(lineSegmentVisitor);
83         if (lineSegmentVisitor.empty())
84             return false;
85         if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
86             return false;
87     }
88
89     SGSharedPtr<BVHMotionTransform> transform3 = new BVHMotionTransform;
90     transform3->setLinearVelocity(SGVec3d(0, 0, 1));
91     transform3->setAngularVelocity(SGVec3d(1, 0, 0));
92     transform3->addChild(node);
93
94     {
95         BVHLineSegmentVisitor lineSegmentVisitor(lineSegment, 0);
96         transform3->accept(lineSegmentVisitor);
97         if (lineSegmentVisitor.empty())
98             return false;
99         if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
100             return false;
101         if (!equivalent(lineSegmentVisitor.getLinearVelocity(),
102                         SGVec3d(0, 1, 1)))
103             return false;
104         if (!equivalent(lineSegmentVisitor.getAngularVelocity(),
105                         SGVec3d(1, 0, 0)))
106             return false;
107     }
108
109     return true;
110 }
111
112 int
113 main(int argc, char** argv)
114 {
115     if (!testLineIntersections())
116         return EXIT_FAILURE;
117     return EXIT_SUCCESS;
118 }