|
Closest Point
Find the Closest Point on a mesh
|
00001 #include "ClosestPoint.h" 00002 #include <fstream> 00003 #include <cmath> 00004 #include <boost/test/minimal.hpp> 00005 00006 #define INFINITY_FLOAT std::numeric_limits<float>::infinity(); 00007 00008 /*Test case to validate if the point is in bounds*/ 00009 bool pointInSphere(Vec3f result, Vec3f queryPoint, float radius){ 00010 if (result == NULL) 00011 return true; 00012 return (pow(result.x-queryPoint.x,2) + 00013 pow(result.y-queryPoint.y,2) + 00014 pow(result.z-queryPoint.z,2) <= pow(radius,2)); 00015 } 00016 00017 /*Helper function*/ 00018 bool barycentric (Vec3f a, Vec3f b, Vec3f c, Vec3f p){ 00019 Vec3f v0 = c -a ; 00020 Vec3f v1 = b -a ; 00021 Vec3f v2 = p -a ; 00022 00023 float dot00 = v0.dot(v0); 00024 float dot01 = v0.dot(v1); 00025 float dot02 = v0.dot(v2); 00026 float dot11 = v1.dot(v1); 00027 float dot12 = v1.dot(v2); 00028 00029 double invDenom = 1 / (dot00 * dot11 - dot01 * dot01); 00030 double u = (dot11 * dot02 - dot01 * dot12) * invDenom; 00031 double v = (dot00 * dot12 - dot01 * dot02) * invDenom; 00032 00033 return (u >= 0) && (v >= 0) && (u + v <= 1); 00034 } 00035 00036 /*Test case to check if the point actually exists on the mesh*/ 00037 bool pointOnMesh(Vec3f output,Mesh m){ 00038 if (output == NULL) 00039 return true; 00040 vector<Face> face = m.get_faceList(); 00041 for (int i=0; i<face.size() ; i++){ 00042 Face temp = face[i]; 00043 00044 if(barycentric(temp.v1,temp.v2,temp.v3,output)) 00045 return true; 00046 } 00047 return false; 00048 } 00049 00050 /*Boost Test Main*/ 00051 int test_main(int argc, char** argv){ 00052 Mesh mesh; 00053 char* filename = "./data/bunny.ply"; //Default input mesh 00054 00055 if(argc ==2){ 00056 std::string argv1 = argv[1]; 00057 if((argv1 == "-h" )|| (argv1 == "--help")){ 00058 cout <<"Usage: ./ClosestPoint [path to ply file]"<<endl; 00059 cout << "Default ply: ./data/bunny.ply"<<endl; 00060 exit(0); 00061 } 00062 else 00063 filename = argv[1]; 00064 } 00065 00066 cout << "Reading file: "<<filename<<endl; 00067 00068 /*Populate Mesh Data structure from the Ply file*/ 00069 00070 if (!mesh.readPly(filename)){ 00071 cout <<"Failed to read ply file "<< filename<<endl; 00072 return 1; 00073 } 00074 00075 ClosestPoint cp = ClosestPoint(mesh); 00076 00077 /*Read query points from a file and run the algorithm!*/ 00078 std::ifstream infile("./data/queryPoints.txt"); 00079 float x,y,z,searchRadius; 00080 00081 while(infile >>x >> y >> z >> searchRadius){ 00082 Vec3f queryPoint = Vec3f(x,y,z); 00083 Vec3f output = cp(queryPoint,searchRadius); 00084 00085 BOOST_CHECK(pointInSphere(output,queryPoint,searchRadius) == true); 00086 BOOST_CHECK(pointOnMesh(output,cp.getMesh()) == true); 00087 } 00088 return 0; 00089 }
1.7.6.1