// standard headers #include // stl headers #include // vcg headers //#include //#include #include #include #include #include #include #include #include #include #include #include #include typedef float AScalarType; using namespace vcg; class AEdge; class AFace; class AVertex : public VertexSimp2< AVertex, AEdge, AFace, vertex::Normal3f, vertex::Coord3f,vertex::BitFlags >{}; class AFace : public FaceSimp2< AVertex, AEdge, AFace, face::VertexRef, face::Normal3f, face::EdgePlane, face::BitFlags> {}; //class AVertex : public vcg::Vertex< AScalarType, AEdge, AFace > { }; //class AFace : public vcg::FaceRTFMFN< AVertex, AEdge, AFace > { }; class AMesh : public vcg::tri::TriMesh< std::vector, std::vector > { }; typedef vcg::AABBBinaryTreeIndex AIndex; static AMesh gMesh; static AIndex gIndex; static void CreateMesh(void) { vcg::tri::Dodecahedron(gMesh); vcg::tri::UpdateFlags::Clear(gMesh); vcg::tri::UpdateNormals::PerVertexNormalized(gMesh); vcg::tri::UpdateEdges::Set(gMesh); } static void SetIndex(void) { gIndex.Set(gMesh.face.begin(), gMesh.face.end()); } static void TestClosest(void) { vcg::face::PointDistanceFunctor getPtDist; const AIndex::CoordType queryPoint((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0); const AIndex::ScalarType maxDist = std::numeric_limits::max(); AIndex::ObjPtr closestFace; AIndex::ScalarType closestDist; AIndex::CoordType closestPoint; vcg::EmptyClass a; closestFace = gIndex.GetClosest(getPtDist, a, queryPoint, maxDist, closestDist, closestPoint); printf("GetClosest Test:\n"); if (closestFace != 0) { printf("\tface : 0x%p\n", closestFace); printf("\tdistance : %f\n", closestDist); printf("\tpoint : [%f, %f, %f]\n", closestPoint[0], closestPoint[1], closestPoint[2]); } else { printf("\tno object found (index is probably empty).\n"); } } static void TestKClosest(void) { vcg::face::PointDistanceFunctor getPtDist; const unsigned int k = 10; const AIndex::CoordType queryPoint((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0); const AIndex::ScalarType maxDist = std::numeric_limits::max(); std::vector closestObjects; std::vector closestDistances; std::vector closestPoints; vcg::EmptyClass a; unsigned int rk = gIndex.GetKClosest(getPtDist, a, k, queryPoint, maxDist, closestObjects, closestDistances, closestPoints); printf("GetKClosest Test:\n"); printf("\tfound %d objects\n", rk); } static void TestRay(void) { const bool TEST_BACK_FACES = true; vcg::RayTriangleIntersectionFunctor rayIntersector; const AIndex::ScalarType maxDist = std::numeric_limits::max(); const AIndex::CoordType rayOrigin((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0); const AIndex::CoordType rayDirection((AIndex::ScalarType)1, (AIndex::ScalarType)0, (AIndex::ScalarType)0); const vcg::Ray3 ray(rayOrigin, rayDirection); AIndex::ObjPtr isectFace; AIndex::ScalarType rayT; AIndex::CoordType isectPt; vcg::EmptyClass a; isectFace = gIndex.DoRay(rayIntersector, a , ray, maxDist, rayT); printf("DoRay Test:\n"); if (isectFace != 0) { printf("\tface : 0x%p\n", isectFace); printf("\tray t : %f\n", rayT); } else { printf("\tno object found (index is probably empty).\n"); } } int main (int /*argc*/, char ** /*argv*/) { CreateMesh(); SetIndex(); printf("Spatial Index Tests\n"); printf("---\n"); TestClosest(); printf("---\n"); TestKClosest(); printf("---\n"); TestRay(); printf("---\n"); return (0); }