00001
00002 #include <stdio.h>
00003
00004
00005 #include <vector>
00006
00007
00008
00009
00010 #include<vcg/simplex/vertex/base.h>
00011 #include<vcg/simplex/face/base.h>
00012 #include<vcg/simplex/face/component_rt.h>
00013
00014 #include<vcg/simplex/face/distance.h>
00015 #include<vcg/complex/trimesh/base.h>
00016 #include <vcg/complex/trimesh/create/platonic.h>
00017 #include <vcg/complex/trimesh/update/normal.h>
00018 #include <vcg/complex/trimesh/update/edges.h>
00019 #include <vcg/complex/trimesh/update/flag.h>
00020 #include <vcg/space/intersection3.h>
00021
00022 #include <vcg/space/index/aabb_binary_tree/aabb_binary_tree.h>
00023
00024 typedef float AScalarType;
00025
00026 using namespace vcg;
00027
00028 class AVertex;
00029 class AFace;
00030
00031 struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<AVertex> ::AsVertexType,
00032 vcg::Use<AFace> ::AsFaceType>{};
00033
00034 class AVertex : public Vertex< MyUsedTypes, vertex::Normal3f, vertex::Coord3f,vertex::BitFlags >{};
00035 class AFace : public Face< MyUsedTypes, face::VertexRef, face::Normal3f, face::EdgePlane, face::BitFlags> {};
00036
00037
00038
00039
00040 class AMesh : public vcg::tri::TriMesh< std::vector<AVertex>, std::vector<AFace> > { };
00041
00042 typedef vcg::AABBBinaryTreeIndex<AFace, AScalarType, vcg::EmptyClass> AIndex;
00043
00044 static AMesh gMesh;
00045 static AIndex gIndex;
00046
00047 static void CreateMesh(void) {
00048 vcg::tri::Dodecahedron<AMesh>(gMesh);
00049
00050 vcg::tri::UpdateFlags<AMesh>::Clear(gMesh);
00051 vcg::tri::UpdateNormals<AMesh>::PerVertexNormalized(gMesh);
00052 vcg::tri::UpdateEdges<AMesh>::Set(gMesh);
00053 }
00054
00055 static void SetIndex(void) {
00056 gIndex.Set(gMesh.face.begin(), gMesh.face.end());
00057 }
00058
00059 static void TestClosest(void) {
00060 vcg::face::PointDistanceFunctor<AIndex::ScalarType> getPtDist;
00061 const AIndex::CoordType queryPoint((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
00062 const AIndex::ScalarType maxDist = std::numeric_limits<AIndex::ScalarType>::max();
00063
00064 AIndex::ObjPtr closestFace;
00065 AIndex::ScalarType closestDist;
00066 AIndex::CoordType closestPoint;
00067
00068 vcg::EmptyClass a;
00069 closestFace = gIndex.GetClosest(getPtDist, a, queryPoint, maxDist, closestDist, closestPoint);
00070
00071 printf("GetClosest Test:\n");
00072
00073 if (closestFace != 0) {
00074 printf("\tface : 0x%p\n", closestFace);
00075 printf("\tdistance : %f\n", closestDist);
00076 printf("\tpoint : [%f, %f, %f]\n", closestPoint[0], closestPoint[1], closestPoint[2]);
00077 }
00078 else {
00079 printf("\tno object found (index is probably empty).\n");
00080 }
00081 }
00082
00083 static void TestKClosest(void) {
00084 vcg::face::PointDistanceFunctor<AIndex::ScalarType> getPtDist;
00085 const unsigned int k = 10;
00086 const AIndex::CoordType queryPoint((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
00087 const AIndex::ScalarType maxDist = std::numeric_limits<AIndex::ScalarType>::max();
00088
00089 std::vector<AIndex::ObjPtr> closestObjects;
00090 std::vector<AIndex::ScalarType> closestDistances;
00091 std::vector<AIndex::CoordType> closestPoints;
00092
00093 vcg::EmptyClass a;
00094 unsigned int rk = gIndex.GetKClosest(getPtDist, a, k, queryPoint, maxDist, closestObjects, closestDistances, closestPoints);
00095
00096 printf("GetKClosest Test:\n");
00097 printf("\tfound %d objects\n", rk);
00098 }
00099
00100 static void TestRay(void) {
00101 const bool TEST_BACK_FACES = true;
00102
00103 vcg::RayTriangleIntersectionFunctor<TEST_BACK_FACES> rayIntersector;
00104 const AIndex::ScalarType maxDist = std::numeric_limits<AIndex::ScalarType>::max();
00105 const AIndex::CoordType rayOrigin((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
00106 const AIndex::CoordType rayDirection((AIndex::ScalarType)1, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
00107 const vcg::Ray3<AIndex::ScalarType, false> ray(rayOrigin, rayDirection);
00108
00109 AIndex::ObjPtr isectFace;
00110 AIndex::ScalarType rayT;
00111 AIndex::CoordType isectPt;
00112
00113 vcg::EmptyClass a;
00114 isectFace = gIndex.DoRay(rayIntersector, a , ray, maxDist, rayT);
00115
00116 printf("DoRay Test:\n");
00117 if (isectFace != 0) {
00118 printf("\tface : 0x%p\n", isectFace);
00119 printf("\tray t : %f\n", rayT);
00120 }
00121 else {
00122 printf("\tno object found (index is probably empty).\n");
00123 }
00124 }
00125
00126 int main (int , char ** ) {
00127 CreateMesh();
00128
00129 SetIndex();
00130
00131 printf("Spatial Index Tests\n");
00132 printf("---\n");
00133 TestClosest();
00134 printf("---\n");
00135 TestKClosest();
00136 printf("---\n");
00137 TestRay();
00138 printf("---\n");
00139
00140 return (0);
00141 }