00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <iostream>
00019 #include <simgear/structure/SGSharedPtr.hxx>
00020
00021 #include "BVHNode.hxx"
00022 #include "BVHGroup.hxx"
00023 #include "BVHTransform.hxx"
00024
00025 #include "BVHStaticData.hxx"
00026
00027 #include "BVHStaticNode.hxx"
00028 #include "BVHStaticLeaf.hxx"
00029 #include "BVHStaticTriangle.hxx"
00030 #include "BVHStaticBinary.hxx"
00031 #include "BVHStaticGeometry.hxx"
00032
00033 #include "BVHBoundingBoxVisitor.hxx"
00034 #include "BVHSubTreeCollector.hxx"
00035 #include "BVHLineSegmentVisitor.hxx"
00036 #include "BVHNearestPointVisitor.hxx"
00037
00038 using namespace simgear;
00039
00040 BVHNode*
00041 buildSingleTriangle(const SGVec3f& v1, const SGVec3f& v2, const SGVec3f& v3)
00042 {
00043 BVHStaticData* staticData = new BVHStaticData;
00044 unsigned indices[3] = {
00045 staticData->addVertex(v1),
00046 staticData->addVertex(v2),
00047 staticData->addVertex(v3)
00048 };
00049 BVHStaticTriangle* staticTriangle = new BVHStaticTriangle(~0u, indices);
00050 return new BVHStaticGeometry(staticTriangle, staticData);
00051 }
00052
00053 bool
00054 testLineIntersections()
00055 {
00056 SGVec3f v1(-1, -1, 0);
00057 SGVec3f v2(1, -1, 0);
00058 SGVec3f v3(-1, 1, 0);
00059 SGSharedPtr<BVHNode> node = buildSingleTriangle(v1, v2, v3);
00060
00061 SGLineSegmentd lineSegment(SGVec3d(0, 0, -1), SGVec3d(0, 0, 1));
00062 {
00063 BVHLineSegmentVisitor lineSegmentVisitor(lineSegment);
00064 node->accept(lineSegmentVisitor);
00065 if (lineSegmentVisitor.empty())
00066 return false;
00067 if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
00068 return false;
00069 }
00070
00071 SGVec3d position(1000, 1000, 1000);
00072 SGMatrixd matrix(position);
00073 SGSharedPtr<BVHTransform> transform1 = new BVHTransform;
00074 transform1->setToWorldTransform(matrix);
00075 transform1->addChild(node);
00076
00077 SGSharedPtr<BVHTransform> transform2 = new BVHTransform;
00078 transform2->setToLocalTransform(matrix);
00079 transform2->addChild(transform1);
00080
00081 {
00082 BVHLineSegmentVisitor lineSegmentVisitor(lineSegment);
00083 transform2->accept(lineSegmentVisitor);
00084 if (lineSegmentVisitor.empty())
00085 return false;
00086 if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
00087 return false;
00088 }
00089
00090 SGSharedPtr<BVHMotionTransform> transform3 = new BVHMotionTransform;
00091 transform3->setLinearVelocity(SGVec3d(0, 0, 1));
00092 transform3->setAngularVelocity(SGVec3d(1, 0, 0));
00093 transform3->addChild(node);
00094
00095 {
00096 BVHLineSegmentVisitor lineSegmentVisitor(lineSegment, 0);
00097 transform3->accept(lineSegmentVisitor);
00098 if (lineSegmentVisitor.empty())
00099 return false;
00100 if (!equivalent(lineSegmentVisitor.getPoint(), SGVec3d(0, 0, 0)))
00101 return false;
00102 if (!equivalent(lineSegmentVisitor.getLinearVelocity(),
00103 SGVec3d(0, 1, 1)))
00104 return false;
00105 if (!equivalent(lineSegmentVisitor.getAngularVelocity(),
00106 SGVec3d(1, 0, 0)))
00107 return false;
00108 }
00109
00110 return true;
00111 }
00112
00113 bool
00114 testNearestPoint()
00115 {
00116 SGVec3f v1(-1, -1, 0);
00117 SGVec3f v2(1, -1, 0);
00118 SGVec3f v3(-1, 1, 0);
00119 SGSharedPtr<BVHNode> node = buildSingleTriangle(v1, v2, v3);
00120
00121 SGSphered sphere(SGVec3d(0, 0, -1), 2);
00122 {
00123 BVHNearestPointVisitor nearestPointVisitor(sphere, 0);
00124 node->accept(nearestPointVisitor);
00125 if (nearestPointVisitor.empty())
00126 return false;
00127 if (!equivalent(nearestPointVisitor.getPoint(), SGVec3d(0, 0, 0)))
00128 return false;
00129 }
00130
00131 return true;
00132 }
00133
00134 int
00135 main(int argc, char** argv)
00136 {
00137 if (!testLineIntersections())
00138 return EXIT_FAILURE;
00139 if (!testNearestPoint())
00140 return EXIT_FAILURE;
00141 return EXIT_SUCCESS;
00142 }