Closest Point Find the Closest Point on a mesh

- Main Page
- Classes
- Files

- File List

src/Mesh.cpp

    00001 #include <Mesh.h>
    00002 #include <iostream>
    00003 #include <exception>
    00004
    00005 Mesh::Mesh(const Mesh &obj){
    00006     vertex_list = obj.vertex_list;
    00007     face_list = obj.face_list;
    00008     nverts = obj.nverts;
    00009     nfaces = obj.nfaces;
    00010 }
    00011
    00012 /*Using Ply file lib to populate vertex and face lists*/
    00013 bool Mesh::readPly(char *filename){
    00014
    00015 typedef struct face_struct {
    00016
    00017   unsigned char nverts;
    00018   int *verts;
    00019   float nx,ny,nz;
    00020 } face_struct;
    00021
    00022 int vertices;
    00023 PlyFile *plyFile;
    00024 PlyElement **elems;
    00025 PlyProperty *property,**ele_prop;
    00026 FILE *fp;
    00027
    00028 PlyProperty vert_props[] = {
    00029   {"x", Float32, Float32, offsetof(Vec3f,x), 0, 0, 0, 0},
    00030   {"y", Float32, Float32, offsetof(Vec3f,y), 0, 0, 0, 0},
    00031   {"z", Float32, Float32, offsetof(Vec3f,z), 0, 0, 0, 0},
    00032 };
    00033
    00034 PlyProperty face_props[] = {
    00035   {"vertex_indices",Uint8 , Int32, offsetof(face_struct,verts), 1, Uint8, Uint8,offsetof(face_struct,nverts)}
    00036 };
    00037
    00038 float arr;
    00039 int numOfElements,nprops,i,k,j;
    00040 char *ele_name;
    00041 char **elements;
    00042
    00043   fp = fopen(filename,"r");
    00044   plyFile = read_ply(fp);
    00045   if (fp == NULL){
    00046     cout<< "Error reading ply file "<< filename<<endl;
    00047     return false;
    00048   }
    00049
    00050 try{
    00051   for(i=0; i< plyFile->num_elem_types; i++){
    00052      elems = plyFile->elems;
    00053      ele_name = elems[i]->name;
    00054      numOfElements=elems[i]->num;
    00055
    00056      cout<< "Number of "<< ele_name<< ": "<<elems[i]->num<<endl;
    00057      nprops = elems[i]->nprops;
    00058      ele_prop = elems[i]->props;
    00059      //cout<<ele_prop[0]->name;
    00060
    00061   if(equal_strings ("vertex", ele_name)){
    00062     vertices=elems[i]->num;
    00063     //vlist = (Vertex **) malloc (sizeof (Vertex *) * elems[i]->num);
    00064
    00065     ply_get_property (plyFile, ele_name, &vert_props[0]);
    00066     ply_get_property (plyFile, ele_name, &vert_props[1]);
    00067     ply_get_property (plyFile, ele_name, &vert_props[2]);
    00068
    00069     float* vlist = (float*) calloc (3,sizeof (float));
    00070     Vec3f temp;
    00071     for (j = 0; j < numOfElements; j++) {
    00072
    00073       ply_get_element (plyFile, (void *) vlist);
    00074
    00075         temp.x = vlist[0];
    00076         temp.y = vlist[1];
    00077         temp.z = vlist[2];
    00078
    00079       vertex_list.push_back(temp);
    00080       //cout << vertex_list[j].x << vertex_list[j].y << vertex_list[j].z<<endl;
    00081        }
    00082      }
    00083
    00084   if (equal_strings ("face", ele_name)) {
    00085     ply_get_property (plyFile, ele_name, &face_props[0]);
    00086     //cout << "face_props" << face_props[0];
    00087      //= (Face*) malloc (sizeof (Face));
    00088     face_struct *flist = (face_struct*) malloc (sizeof (face_struct));
    00089     Face face_obj;
    00090     int faces = elems[i]->num;
    00091     for (j = 0; j < elems[i]->num; j++) {
    00092       //flist[j] = (Face *) malloc (sizeof (Face));
    00093       ply_get_element (plyFile,(void*)flist);
    00094
    00095       //cout << "face" << (float)flist->verts[0]<<endl;
    00096          face_obj.nverts = (float)flist->nverts;
    00097          face_obj.vid = flist->verts;
    00098          face_obj.v1 = vertex_list[(int)flist->verts[0]];
    00099          face_obj.v2 = vertex_list[(int)flist->verts[1]];
    00100          face_obj.v3 = vertex_list[(int)flist->verts[2]];
    00101          face_obj.id = j;
    00102          face_list.push_back(face_obj);
    00103          //cout << "id" <<face_obj.vid[0]<<face_obj.vid[2]<<endl;
    00104      }
    00105   }
    00106   }
    00107   nverts = vertex_list.size();
    00108   nfaces = face_list.size();
    00109  }
    00110  catch(exception& e){
    00111     cout << "Hit exception during ply parse, only ASCII formats supported "<<e.what();
    00112  }
    00113   return true;
    00114 }
    00115
    00116

All Classes