PlyReader::PlyReader(const std::string& filename) : m_filename(filename), m_data(new Data()) { m_data->plyFile = ply_open_for_reading( filename.data(), &m_data->elementCount, &m_data->elementNames, &m_data->file_type, &m_data->version); SURGSIM_LOG_IF(!isValid(), SurgSim::Framework::Logger::getLogger("InputOutput"), WARNING) << "'" << m_filename << "' is an invalid .ply file"; }
MEM_SmartPtr<BSP_TMesh> BSP_PlyLoader:: NewMeshFromFile( char * file_name, MT_Vector3 &min, MT_Vector3 &max ) { min = MT_Vector3(MT_INFINITY,MT_INFINITY,MT_INFINITY); max = MT_Vector3(-MT_INFINITY,-MT_INFINITY,-MT_INFINITY); PlyProperty vert_props[] = { /* list of property information for a vertex */ {"x", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,x), 0, 0, 0, 0}, {"y", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,y), 0, 0, 0, 0}, {"z", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,z), 0, 0, 0, 0}, }; PlyProperty face_props[] = { /* list of property information for a vertex */ {"vertex_indices", PLY_INT, PLY_INT, offsetof(LoadFace,verts), 1, PLY_UCHAR, PLY_UCHAR, offsetof(LoadFace,nverts)}, }; MEM_SmartPtr<BSP_TMesh> mesh = new BSP_TMesh; if (mesh == NULL) return NULL; int i,j; PlyFile *ply; int nelems; char **elist; int file_type; float version; int nprops; int num_elems; PlyProperty **plist; char *elem_name; LoadVertex load_vertex; LoadFace load_face; /* open a PLY file for reading */ ply = ply_open_for_reading( file_name, &nelems, &elist, &file_type, &version ); if (ply == NULL) return NULL; /* go through each kind of element that we learned is in the file */ /* and read them */ for (i = 0; i < nelems; i++) { /* get the description of the first element */ elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); /* print the name of the element, for debugging */ /* if we're on vertex elements, read them in */ if (equal_strings ("vertex", elem_name)) { /* set up for getting vertex elements */ ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); // make some memory for the vertices mesh->VertexSet().reserve(num_elems); /* grab all the vertex elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ ply_get_element (ply, (void *)&load_vertex); // pass the vertex into the mesh builder. if (load_vertex.x < min.x()) { min.x() = load_vertex.x; } else if (load_vertex.x > max.x()) { max.x()= load_vertex.x; } if (load_vertex.y < min.y()) { min.y() = load_vertex.y; } else if (load_vertex.y > max.y()) { max.y()= load_vertex.y; } if (load_vertex.z < min.z()) { min.z() = load_vertex.z; } else if (load_vertex.z > max.z()) { max.z()= load_vertex.z; } BSP_TVertex my_vert; my_vert.m_pos = MT_Vector3(load_vertex.x,load_vertex.y,load_vertex.z); mesh->VertexSet().push_back(my_vert); } } /* if we're on face elements, read them in */ if (equal_strings ("face", elem_name)) { /* set up for getting face elements */ ply_get_property (ply, elem_name, &face_props[0]); /* grab all the face elements */ for (j = 0; j < num_elems; j++) { ply_get_element (ply, (void *)&load_face); int v; for (v = 2; v< load_face.nverts; v++) { BSP_TFace f; f.m_verts[0] = load_face.verts[0]; f.m_verts[1] = load_face.verts[v-1]; f.m_verts[2] = load_face.verts[v]; mesh->BuildNormal(f); mesh->FaceSet().push_back(f); } // free up the memory this pile of shit used to allocate the polygon's vertices free (load_face.verts); } } } /* close the PLY file */ ply_close (ply); return mesh; }
LOD_Decimation_InfoPtr NewVertsFromFile( char * file_name, MT_Vector3 &min, MT_Vector3 &max ) { min = MT_Vector3(MT_INFINITY,MT_INFINITY,MT_INFINITY); max = MT_Vector3(-MT_INFINITY,-MT_INFINITY,-MT_INFINITY); PlyProperty vert_props[] = { /* list of property information for a vertex */ {"x", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,x), 0, 0, 0, 0}, {"y", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,y), 0, 0, 0, 0}, {"z", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,z), 0, 0, 0, 0}, }; PlyProperty face_props[] = { /* list of property information for a vertex */ {"intensity", PLY_UCHAR, PLY_UCHAR, offsetof(LoadFace,intensity), 0, 0, 0, 0}, {"vertex_indices", PLY_INT, PLY_INT, offsetof(LoadFace,verts), 1, PLY_UCHAR, PLY_UCHAR, offsetof(LoadFace,nverts)}, }; #if 0 MEM_SmartPtr<std::vector<float> > verts = new std::vector<float>; MEM_SmartPtr<std::vector<float> > vertex_normals = new std::vector<float>; MEM_SmartPtr<std::vector<int> > faces = new std::vector<int>; #else std::vector<float>* verts = new std::vector<float>; std::vector<float>* vertex_normals = new std::vector<float>; std::vector<int> * faces = new std::vector<int>; #endif int i,j; PlyFile *ply; int nelems; char **elist; int file_type; float version; int nprops; int num_elems; PlyProperty **plist; char *elem_name; LoadVertex load_vertex; LoadFace load_face; /* open a PLY file for reading */ ply = ply_open_for_reading(file_name, &nelems, &elist, &file_type, &version); if (ply == NULL) return NULL; /* go through each kind of element that we learned is in the file */ /* and read them */ for (i = 0; i < nelems; i++) { /* get the description of the first element */ elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); /* print the name of the element, for debugging */ /* if we're on vertex elements, read them in */ if (equal_strings ("vertex", elem_name)) { /* set up for getting vertex elements */ ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); // make some memory for the vertices verts->reserve(num_elems); /* grab all the vertex elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ ply_get_element (ply, (void *)&load_vertex); // pass the vertex into the mesh builder. if (load_vertex.x < min.x()) { min.x() = load_vertex.x; } else if (load_vertex.x > max.x()) { max.x()= load_vertex.x; } if (load_vertex.y < min.y()) { min.y() = load_vertex.y; } else if (load_vertex.y > max.y()) { max.y()= load_vertex.y; } if (load_vertex.z < min.z()) { min.z() = load_vertex.z; } else if (load_vertex.z > max.z()) { max.z()= load_vertex.z; } verts->push_back(load_vertex.x); verts->push_back(load_vertex.y); verts->push_back(load_vertex.z); vertex_normals->push_back(1.0f); vertex_normals->push_back(0.0f); vertex_normals->push_back(0.0f); } } /* if we're on face elements, read them in */ if (equal_strings ("face", elem_name)) { /* set up for getting face elements */ // ply_get_property (ply, elem_name, &face_props[0]); ply_get_property (ply, elem_name, &face_props[1]); /* grab all the face elements */ for (j = 0; j < num_elems; j++) { ply_get_element (ply, (void *)&load_face); faces->push_back(load_face.verts[0]); faces->push_back(load_face.verts[1]); faces->push_back(load_face.verts[2]); // free up the memory this pile of shit used to allocate the polygon's vertices free (load_face.verts); } } } /* close the PLY file */ ply_close (ply); LOD_Decimation_InfoPtr output = new LOD_Decimation_Info; output->vertex_buffer = verts->begin(); output->vertex_num = verts->size()/3; output->triangle_index_buffer = faces->begin(); output->face_num = faces->size()/3; output->intern = NULL; output->vertex_normal_buffer = vertex_normals->begin(); // memory leaks 'r' us #if 0 verts.Release(); vertex_normals.Release(); faces.Release(); #endif return output; }
read_test() { int i,j,k; PlyFile *ply; int nelems; char **elist; int file_type; float version; int nprops; int num_elems; PlyProperty **plist; Vertex **vlist; Face **flist; char *elem_name; int num_comments; char **comments; int num_obj_info; char **obj_info; /* open a PLY file for reading */ ply = ply_open_for_reading("test", &nelems, &elist, &file_type, &version); /* print what we found out about the file */ printf ("version %f\n", version); printf ("type %d\n", file_type); /* go through each kind of element that we learned is in the file */ /* and read them */ for (i = 0; i < nelems; i++) { /* get the description of the first element */ elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); /* print the name of the element, for debugging */ printf ("element %s %d\n", elem_name, num_elems); /* if we're on vertex elements, read them in */ if (equal_strings ("vertex", elem_name)) { /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems); /* set up for getting vertex elements */ ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); /* grab all the vertex elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ vlist[j] = (Vertex *) malloc (sizeof (Vertex)); ply_get_element (ply, (void *) vlist[j]); /* print out vertex x,y,z for debugging */ printf ("vertex: %g %g %g\n", vlist[j]->x, vlist[j]->y, vlist[j]->z); } } /* if we're on face elements, read them in */ if (equal_strings ("face", elem_name)) { /* create a list to hold all the face elements */ flist = (Face **) malloc (sizeof (Face *) * num_elems); /* set up for getting face elements */ ply_get_property (ply, elem_name, &face_props[0]); ply_get_property (ply, elem_name, &face_props[1]); /* grab all the face elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ flist[j] = (Face *) malloc (sizeof (Face)); ply_get_element (ply, (void *) flist[j]); /* print out face info, for debugging */ printf ("face: %d, list = ", flist[j]->intensity); for (k = 0; k < flist[j]->nverts; k++) printf ("%d ", flist[j]->verts[k]); printf ("\n"); } } /* print out the properties we got, for debugging */ for (j = 0; j < nprops; j++) printf ("property %s\n", plist[j]->name); } /* grab and print out the comments in the file */ comments = ply_get_comments (ply, &num_comments); for (i = 0; i < num_comments; i++) printf ("comment = '%s'\n", comments[i]); /* grab and print out the object information */ obj_info = ply_get_obj_info (ply, &num_obj_info); for (i = 0; i < num_obj_info; i++) printf ("obj_info = '%s'\n", obj_info[i]); /* close the PLY file */ ply_close (ply); }
Polygon* Polygon::loadPolygon(const std::string& filename) { int elementCount; char** elementNames; int fileType; float version; // Open a PLY file for reading char* name = strdup(filename.c_str()); PlyFile* ply = ply_open_for_reading(name, &elementCount, &elementNames, &fileType, &version); free(name); if(!ply) { printf("FAILED\n"); return NULL; } // Print what we found out about the file //printf ("version %f\n", version); //printf ("type %d\n", fileType); std::vector<PlyVertex*> plyVertices; std::vector<PlyFace*> plyFaces; // Go through each kind of element that we learned is in the file and read them for (int i = 0; i < elementCount; i++) { // Get the description of the first element char* elementName = elementNames[i]; int entryCount, propertyCount; PlyProperty** properties = ply_get_element_description(ply, elementName, &entryCount, &propertyCount); // Print the name of the element, for debugging //printf("element %s %d\n", elementName, entryCount); // If we're on vertex elements, read them in if (!strcmp("vertex", elementName)) { // Set up for getting vertex elements ply_get_property(ply, elementName, &VERTEX_PROPERTIES[0]); ply_get_property(ply, elementName, &VERTEX_PROPERTIES[1]); ply_get_property(ply, elementName, &VERTEX_PROPERTIES[2]); plyVertices.reserve(entryCount); // Grab all the vertex elements for (int j = 0; j < entryCount; j++) { PlyVertex* v = new PlyVertex(); // Grab an element from the file ply_get_element(ply, v); plyVertices.push_back(v); // Print out vertex x,y,z for debugging //printf("vertex: %d %lf %lf %lf\n", plyVertices.size(), v->x, v->y, v->z); } } // If we're on face elements, read them in if (!strcmp("face", elementName)) { // Set up for getting face elements ply_get_property(ply, elementName, &FACE_PROPERTIES[0]); ply_get_property(ply, elementName, &FACE_PROPERTIES[1]); plyFaces.reserve(entryCount); // Grab all the face elements for (int j = 0; j < entryCount; j++) { PlyFace* f = new PlyFace(); // Grab an element from the file ply_get_element(ply, f); plyFaces.push_back(f); // Print out face info, for debugging /*printf("face: %d, list = ", f->intensity); for (int k = 0; k < f->nverts; k++) { printf("%d ", f->verts[k]); } printf("\n");*/ } } // Print out the properties we got, for debugging /*for (int j = 0; j < propertyCount; j++) { printf ("property %s\n", properties[j]->name); }*/ } Polygon* polygon = new Polygon(); polygon->vertices.reserve(plyVertices.size()); polygon->vertexArray.reserve(plyVertices.size() * 3); polygon->normalArray.reserve(plyVertices.size() * 3); polygon->colorArray.reserve(plyVertices.size() * 4); polygon->shadowVertexArray.reserve(plyVertices.size() * 3); polygon->shadowNormalArray.reserve(plyVertices.size() * 3); polygon->faces.reserve(plyFaces.size()); Vector3 vec3; polygon->minX = plyVertices[0]->x; polygon->maxX = plyVertices[0]->x; polygon->minY = plyVertices[0]->y; polygon->maxY = plyVertices[0]->y; polygon->minZ = plyVertices[0]->z; polygon->maxZ = plyVertices[0]->z; for(size_t i = 0; i < plyVertices.size(); i++) { vec3.setData(plyVertices[i]->x, plyVertices[i]->y, plyVertices[i]->z); Vertex* v = new Vertex(vec3); polygon->addVertex(v); polygon->minX = MIN(plyVertices[i]->x, polygon->minX); polygon->maxX = MAX(plyVertices[i]->x, polygon->maxX); polygon->minY = MIN(plyVertices[i]->y, polygon->minY); polygon->maxY = MAX(plyVertices[i]->y, polygon->maxY); polygon->minZ = MIN(plyVertices[i]->z, polygon->minZ); polygon->maxZ = MAX(plyVertices[i]->z, polygon->maxZ); delete plyVertices[i]; } polygon->width = polygon->maxX - polygon->minX; polygon->height = polygon->maxY - polygon->minY; polygon->depth = polygon->maxZ - polygon->minZ; printf("(%lf, %lf, %lf) -> (%lf, %lf, %lf) = (%lf, %lf, %lf)\n", polygon->minX, polygon->minY, polygon->minZ, polygon->maxX, polygon->maxY, polygon->maxZ, polygon->width, polygon->height, polygon->depth); for(size_t i = 0; i < plyFaces.size(); i++) { Face* face = new Face(); for(size_t j = 0; j < plyFaces[i]->nverts; j++) { int idx = plyFaces[i]->verts[j]; face->addVertex(idx); polygon->vertices[idx]->addAdjacent(face); } polygon->addFace(face); delete plyFaces[i]; } polygon->planeSize = -MAX(polygon->width, polygon->depth) * 1.5; polygon->planeY = polygon->minY - 0.06; // Close the PLY file. ply_close (ply); return polygon; }
void PlyUtility::readPlyFile(char *filename) { int i,j; /* open a PLY file for reading */ ply = ply_open_for_reading(filename, &nelems, &elist, &file_type, &version); /* go through each kind of element that we learned is in the file */ /* and read them */ for (i = 0; i < nelems; i++) { /* get the description of the first element */ elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); /* if we're on vertex elements, read them in */ if (equal_strings ("vertex", elem_name)) { vertexCount=num_elems; /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems); /* set up for getting vertex elements */ ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); /* grab all the vertex elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ vlist[j] = (Vertex *) malloc (sizeof (Vertex)); ply_get_element (ply, (void *) vlist[j]); if(vx_max<vlist[j]->x)vx_max=vlist[j]->x; if(vy_max<vlist[j]->y)vy_max=vlist[j]->y; if(vz_max<vlist[j]->z)vz_max=vlist[j]->z; if(vx_min>vlist[j]->x)vx_min=vlist[j]->x; if(vy_min>vlist[j]->y)vy_min=vlist[j]->y; if(vz_min>vlist[j]->z)vz_min=vlist[j]->z; } } /* if we're on face elements, read them in */ if (equal_strings ("face", elem_name)) { faceCount=num_elems; /* create a list to hold all the face elements */ flist = (Face **) malloc (sizeof (Face *) * num_elems); /* set up for getting face elements */ ply_get_property (ply, elem_name, &face_props[0]); ply_get_property (ply, elem_name, &face_props[1]); /* grab all the face elements */ for (j = 0; j < num_elems; j++) { /* grab and element from the file */ flist[j] = (Face *) malloc (sizeof (Face)); ply_get_element (ply, (void *) flist[j]); } } } /* grab and print out the comments in the file */ comments = ply_get_comments (ply, &num_comments); /* grab and print out the object information */ obj_info = ply_get_obj_info (ply, &num_obj_info); /* close the PLY file */ ply_close (ply); }
void Grid::read_ply_file(char* file_name, const int triangle_type) { // Vertex definition typedef struct Vertex { float x,y,z; // space coordinates } Vertex; // Face definition. This is the same for all files but is placed here to keep all the definitions together typedef struct Face { unsigned char nverts; // number of vertex indices in list int* verts; // vertex index list } Face; // list of property information for a vertex // this varies depending on what you are reading from the file PlyProperty vert_props[] = { {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0}, {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0}, {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0} }; // list of property information for a face. // there is a single property, which is a list // this is the same for all files PlyProperty face_props[] = { {"vertex_indices", PLY_INT, PLY_INT, offsetof(Face,verts), 1, PLY_UCHAR, PLY_UCHAR, offsetof(Face,nverts)} }; // local variables int i,j; PlyFile* ply; int nelems; // number of element types: 2 in our case - vertices and faces char** elist; int file_type; float version; int nprops; // number of properties each element has int num_elems; // number of each type of element: number of vertices or number of faces PlyProperty** plist; Vertex** vlist; Face** flist; char* elem_name; int num_comments; char** comments; int num_obj_info; char** obj_info; // open a ply file for reading ply = ply_open_for_reading(file_name, &nelems, &elist, &file_type, &version); // print what we found out about the file printf ("version %f\n", version); printf ("type %d\n", file_type); // go through each kind of element that we learned is in the file and read them for (i = 0; i < nelems; i++) { // there are only two elements in our files: vertices and faces // get the description of the first element elem_name = elist[i]; plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); // print the name of the element, for debugging cout << "element name " << elem_name << " num elements = " << num_elems << " num properties = " << nprops << endl; // if we're on vertex elements, read in the properties if (equal_strings ("vertex", elem_name)) { // set up for getting vertex elements // the three properties are the vertex coordinates ply_get_property (ply, elem_name, &vert_props[0]); ply_get_property (ply, elem_name, &vert_props[1]); ply_get_property (ply, elem_name, &vert_props[2]); // reserve mesh elements mesh_ptr->num_vertices = num_elems; mesh_ptr->vertices.reserve(num_elems); // grab all the vertex elements for (j = 0; j < num_elems; j++) { Vertex* vertex_ptr = new Vertex; // grab an element from the file ply_get_element (ply, (void *) vertex_ptr); mesh_ptr->vertices.push_back(Point3D(vertex_ptr->x, vertex_ptr->y, vertex_ptr->z)); delete vertex_ptr; } } // if we're on face elements, read them in if (equal_strings ("face", elem_name)) { // set up for getting face elements ply_get_property (ply, elem_name, &face_props[0]); // only one property - a list mesh_ptr->num_triangles = num_elems; objects.reserve(num_elems); // triangles will be stored in Compound::objects // the following code stores the face numbers that are shared by each vertex mesh_ptr->vertex_faces.reserve(mesh_ptr->num_vertices); vector<int> faceList; for (j = 0; j < mesh_ptr->num_vertices; j++) mesh_ptr->vertex_faces.push_back(faceList); // store empty lists so that we can use the [] notation below // grab all the face elements int count = 0; // the number of faces read for (j = 0; j < num_elems; j++) { // grab an element from the file Face* face_ptr = new Face; ply_get_element (ply, (void *) face_ptr); // construct a mesh triangle of the specified type if (triangle_type == flat) { FlatMeshTriangle* triangle_ptr = new FlatMeshTriangle(mesh_ptr, face_ptr->verts[0], face_ptr->verts[1], face_ptr->verts[2]); triangle_ptr->compute_normal(reverse_normal); objects.push_back(triangle_ptr); } if (triangle_type == smooth) { SmoothMeshTriangle* triangle_ptr = new SmoothMeshTriangle(mesh_ptr, face_ptr->verts[0], face_ptr->verts[1], face_ptr->verts[2]); triangle_ptr->compute_normal(reverse_normal); // the "flat triangle" normal is used to compute the average normal at each mesh vertex objects.push_back(triangle_ptr); // it's quicker to do it once here, than have to do it on average 6 times in compute_mesh_normals // the following code stores a list of all faces that share a vertex // it's used for computing the average normal at each vertex in order(num_vertices) time mesh_ptr->vertex_faces[face_ptr->verts[0]].push_back(count); mesh_ptr->vertex_faces[face_ptr->verts[1]].push_back(count); mesh_ptr->vertex_faces[face_ptr->verts[2]].push_back(count); count++; } } if (triangle_type == flat) mesh_ptr->vertex_faces.erase(mesh_ptr->vertex_faces.begin(), mesh_ptr->vertex_faces.end()); } // print out the properties we got, for debugging for (j = 0; j < nprops; j++) printf ("property %s\n", plist[j]->name); } // end of for (i = 0; i < nelems; i++) // grab and print out the comments in the file comments = ply_get_comments (ply, &num_comments); for (i = 0; i < num_comments; i++) printf ("comment = '%s'\n", comments[i]); // grab and print out the object information obj_info = ply_get_obj_info (ply, &num_obj_info); for (i = 0; i < num_obj_info; i++) printf ("obj_info = '%s'\n", obj_info[i]); // close the ply file ply_close (ply); }
cMesh::cMesh(const std::string & Filename, bool doAdjacence) { PlyFile * thePlyFile; int nelems; char **elist; int file_type; float version; int nprops; int num_elems; char *elem_name; thePlyFile = ply_open_for_reading( const_cast<char *>(Filename.c_str()), &nelems, &elist, &file_type, &version); ELISE_ASSERT(thePlyFile != NULL, "cMesh3D.cpp: cMesh::cMesh, cannot open ply file for reading"); for (int i = 0; i < nelems; i++) { elem_name = elist[i]; ply_get_element_description (thePlyFile, elem_name, &num_elems, &nprops); //printf ("element %s %d\n", elem_name, num_elems); if (equal_strings ("vertex", elem_name)) { ply_get_property (thePlyFile, elem_name, &props[0]); ply_get_property (thePlyFile, elem_name, &props[1]); ply_get_property (thePlyFile, elem_name, &props[2]); for (int j = 0; j < num_elems; j++) { sVertex *vert = (sVertex *) malloc (sizeof(sVertex)); ply_get_element (thePlyFile, vert); //ajout du point addPt(Pt3dr(vert->x, vert->y, vert->z)); //printf ("vertex: %g %g %g\n", vert->x, vert->y, vert->z); } } else if (equal_strings ("face", elem_name)) { ply_get_property ( thePlyFile, elem_name, &face_props[0]); for (int j = 0; j < num_elems; j++) { sFace *theFace = (sFace *) malloc (sizeof (sFace)); ply_get_element (thePlyFile, theFace); vector <int> vIndx; for (int aK =0; aK < theFace->nverts; ++aK) vIndx.push_back(theFace->verts[aK]); //ajout du triangle addTriangle(cTriangle(this, vIndx, j)); } } } if (doAdjacence) //remplissage du graphe d'adjacence { int cpt; int id0a, id1a, id2a; int id0b, id1b, id2b; int idc0, idc1; //index des sommets communs id0a = id1a = id2a = idc0 = idc1 = -1; id0b = id1b = id2b = -2; for (int aK = 0; aK < getFacesNumber(); ++aK) { mTriangles[aK].getVertexesIndexes(id0a, id1a, id2a); for (int bK=aK; bK < getFacesNumber(); ++bK) { mTriangles[bK].getVertexesIndexes(id0b, id1b, id2b); cpt = 0; if((id0b == id0a)||(id1b == id0a)||(id2b == id0a)) {cpt++; idc0 = id0a;} if((id0b == id1a)||(id1b == id1a)||(id2b == id1a)) { if (cpt) idc1 = id1a; else idc0 = id1a; cpt++; } if((id0b == id2a)||(id1b == id2a)||(id2b == id2a)) { if (cpt) idc1 = id2a; else idc0 = id2a; cpt++; } if (cpt == 2) { #ifdef _DEBUG printf ("found adjacent triangles : %d %d - vertex : %d %d\n", aK, bK, idc0, idc1); #endif addEdge(cEdge(aK, bK, idc0, idc1)); int idx = getEdgesNumber() - 1; //cout << "adding edge " << idx << endl; mTriangles[aK].addEdge(idx); mTriangles[bK].addEdge(idx); } } } } ply_close (thePlyFile); }
GlCloud* GlCloud::loadPly(string i_filename ,int* incre) { int type = 0; vector <GlVertex> ptList; PlyFile * thePlyFile; int nelems; char **elist; int file_type; float version; int nprops; int num_elems; char *elem_name; PlyProperty ** plist = NULL; (void)plist; thePlyFile = ply_open_for_reading( const_cast<char *>(i_filename.c_str()), &nelems, &elist, &file_type, &version); elem_name = elist[0]; plist = ply_get_element_description (thePlyFile, elem_name, &num_elems, &nprops); #ifdef _DEBUG printf ("file %s\n" , i_filename.c_str()); printf ("version %f\n" , version); printf ("type %d\n" , file_type); printf ("nb elem %d\n" , nelems); printf ("num elems %d\n", num_elems); #endif for (int i = 0; i < nelems; i++) { // get the description of the first element elem_name = elist[i]; plist = ply_get_element_description (thePlyFile, elem_name, &num_elems, &nprops); // print the name of the element, for debugging #ifdef _DEBUG printf ("element %s %d %d\n", elem_name, num_elems, nprops); #endif if (equal_strings ("vertex", elem_name)) { switch(nprops) { case 10: // x y z nx ny nz r g b a { type = 5; for (int j = 0; j < nprops ;++j) ply_get_property (thePlyFile, elem_name, &oriented_colored_alpha_vert_props[j]); sPlyOrientedColoredAlphaVertex *vertex = (sPlyOrientedColoredAlphaVertex *) malloc (sizeof (sPlyOrientedColoredAlphaVertex)); // grab all the vertex elements for (int j = 0; j < num_elems; j++) { if (incre) *incre = 100.0f*(float)j/num_elems; ply_get_element (thePlyFile, (void *) vertex); #ifdef _DEBUG printf ("vertex--: %g %g %g %g %g %g %u %u %u %u\n", vertex->x, vertex->y, vertex->z, vertex->nx, vertex->ny, vertex->nz, vertex->red, vertex->green, vertex->blue, vertex->alpha); #endif ptList.push_back( GlVertex (QVector3D ( vertex->x, vertex->y, vertex->z ), QColor( vertex->red, vertex->green, vertex->blue, vertex->alpha ), QVector3D(vertex->nx, vertex->ny, vertex->nz))); } break; } case 9: // x y z nx ny nz r g b { type = 4; for (int j = 0; j < nprops ;++j) ply_get_property (thePlyFile, elem_name, &oriented_colored_vert_props[j]); sPlyOrientedColoredVertex *vertex = (sPlyOrientedColoredVertex *) malloc (sizeof (sPlyOrientedColoredVertex)); // grab all the vertex elements for (int j = 0; j < num_elems; j++) { if (incre) *incre = 100.0f*(float)j/num_elems; ply_get_element (thePlyFile, (void *) vertex); #ifdef _DEBUG printf ("vertex--: %g %g %g %g %g %g %u %u %u\n", vertex->x, vertex->y, vertex->z, vertex->nx, vertex->ny, vertex->nz, vertex->red, vertex->green, vertex->blue); #endif ptList.push_back( GlVertex (QVector3D ( vertex->x, vertex->y, vertex->z ), QColor( vertex->red, vertex->green, vertex->blue ), QVector3D(vertex->nx, vertex->ny, vertex->nz))); } break; } case 7: { type = 2; // setup for getting vertex elements for (int j = 0; j < nprops ;++j) ply_get_property (thePlyFile, elem_name, &colored_a_vert_props[j]); sPlyColoredVertexWithAlpha * vertex = (sPlyColoredVertexWithAlpha *) malloc (sizeof (sPlyColoredVertexWithAlpha)); // grab all the vertex elements for (int j = 0; j < num_elems; j++) { if (incre) *incre = 100.0f*(float)j/num_elems; // grab an element from the file ply_get_element (thePlyFile, (void *) vertex); #ifdef _DEBUG printf ("vertex--: %g %g %g %u %u %u %u\n", vertex->x, vertex->y, vertex->z, vertex->red, vertex->green, vertex->blue, vertex->alpha); #endif ptList.push_back( GlVertex (QVector3D ( vertex->x, vertex->y, vertex->z ), QColor( vertex->red, vertex->green, vertex->blue, vertex->alpha ))); } break; } case 6: { // can be (x y z r g b) or (x y z nx ny nz) bool wNormales = false; PlyElement *elem = NULL; for (int i = 0; i < nelems; i++) if (equal_strings ("vertex", thePlyFile->elems[i]->name)) elem = thePlyFile->elems[i]; for (int i = 0; i < nprops; i++) if ( "nx"==elem->props[i]->name ) wNormales = true; if (!wNormales) { type = 1; for (int j = 0; j < nprops ;++j) ply_get_property (thePlyFile, elem_name, &colored_vert_props[j]); sPlyColoredVertex *vertex = (sPlyColoredVertex *) malloc (sizeof (sPlyColoredVertex)); for (int j = 0; j < num_elems; j++) { if (incre) *incre = 100.0f*(float)j/num_elems; ply_get_element (thePlyFile, (void *) vertex); #ifdef _DEBUG printf ("vertex: %g %g %g %u %u %u\n", vertex->x, vertex->y, vertex->z, vertex->red, vertex->green, vertex->blue); #endif ptList.push_back( GlVertex (QVector3D ( vertex->x, vertex->y, vertex->z ), QColor( vertex->red, vertex->green, vertex->blue ))); } } else { type = 3; for (int j = 0; j < nprops ;++j) ply_get_property (thePlyFile, elem_name, &oriented_vert_props[j]); sPlyOrientedVertex *vertex = (sPlyOrientedVertex *) malloc (sizeof (sPlyOrientedVertex)); for (int j = 0; j < num_elems; j++) { if (incre) *incre = 100.0f*(float)j/num_elems; ply_get_element (thePlyFile, (void *) vertex); #ifdef _DEBUG printf ("vertex: %g %g %g %g %g %g\n", vertex->x, vertex->y, vertex->z, vertex->nx, vertex->ny, vertex->nz); #endif ptList.push_back( GlVertex (QVector3D ( vertex->x, vertex->y, vertex->z ), Qt::white, QVector3D(vertex->nx, vertex->ny, vertex->nz))); } } break; } case 3: { for (int j = 0; j < nprops ;++j) ply_get_property (thePlyFile, elem_name, &vert_props[j]); sVertex *vertex = (sVertex *) malloc (sizeof (sVertex)); for (int j = 0; j < num_elems; j++) { if (incre) *incre = 100.0f*(float)j/num_elems; ply_get_element (thePlyFile, (void *) vertex); #ifdef _DEBUG printf ("vertex: %g %g %g\n", vertex->x, vertex->y, vertex->z); #endif ptList.push_back( GlVertex (QVector3D ( vertex->x, vertex->y, vertex->z ))); } break; } default: { printf("unable to load a ply unless number of properties is 3, 6, 7, 9 or 10\n"); break; } } } } #ifdef _DEBUG printf("check - point number in cloud: %d\n", (int) ptList.size() ); #endif ply_close (thePlyFile); if(incre) *incre = 0; return new GlCloud(ptList, type); }