void write_file(const char* strName) { int i; PlyFile *ply; int num_elem_types; /*** Write out the transformed PLY object ***/ FILE* pFile = fopen(strName,"w"); ply = write_ply (pFile, nelems, elem_names, PLY_ASCII); /* describe what properties go into the vertex elements */ describe_element_ply (ply, "vertex", nverts); describe_property_ply (ply, &vert_props[0]); describe_property_ply (ply, &vert_props[1]); describe_property_ply (ply, &vert_props[2]); if (has_normals) { describe_property_ply (ply, &vert_props[4]); describe_property_ply (ply, &vert_props[5]); describe_property_ply (ply, &vert_props[6]); } if (texture_coords) { describe_property_ply (ply, &vert_props[7]); describe_property_ply (ply, &vert_props[8]); } describe_element_ply (ply, "face", nfaces); describe_property_ply (ply, &face_props[0]); for (i = 0; i < ncomments; i++) append_comment_ply (ply, comments[i]); append_comment_ply (ply, "converted from OBJ by obj2ply"); header_complete_ply (ply); /* set up and write the vertex elements */ put_element_setup_ply (ply, "vertex"); for (i = 0; i < nverts; i++) put_element_ply (ply, &vVertex[i]);//(void *) &vlist[i]); /* set up and write the face elements */ put_element_setup_ply (ply, "face"); for (i = 0; i < nfaces; i++){ int idx[3]; memcpy(idx,&vIndex[i*3],sizeof(int)*3); Face f; f.nverts = 3; f.verts = idx; put_element_ply (ply, (void *)&f);// &flist[i]); } close_ply (ply); free_ply (ply); fclose(pFile); }
write_file() { int i; PlyFile *ply; char **elist; int num_elem_types; /*** Write out the transformed PLY object ***/ elist = get_element_list_ply (in_ply, &num_elem_types); ply = write_ply (stdout, num_elem_types, elist, in_ply->file_type); /* describe what properties go into the vertex elements */ describe_element_ply (ply, "vertex", nverts); describe_property_ply (ply, &vert_props[0]); describe_property_ply (ply, &vert_props[1]); describe_property_ply (ply, &vert_props[2]); describe_other_properties_ply (ply, vert_other, offsetof(Vertex,other_props)); describe_other_elements_ply (ply, in_ply->other_elems); copy_comments_ply (ply, in_ply); append_comment_ply (ply, "modified by xformply"); copy_obj_info_ply (ply, in_ply); header_complete_ply (ply); /* set up and write the vertex elements */ put_element_setup_ply (ply, "vertex"); for (i = 0; i < nverts; i++) put_element_ply (ply, (void *) vlist[i]); put_other_elements_ply (ply); close_ply (ply); free_ply (ply); }
write_file() { int i; PlyFile *ply; int num_elem_types; /*** Write out the transformed PLY object ***/ ply = write_ply (stdout, nelems, elem_names, PLY_ASCII); /* describe what properties go into the vertex elements */ describe_element_ply (ply, "vertex", nverts); describe_property_ply (ply, &vert_props[0]); describe_property_ply (ply, &vert_props[1]); describe_property_ply (ply, &vert_props[2]); describe_element_ply (ply, "face", nfaces); describe_property_ply (ply, &face_props[0]); append_comment_ply (ply, "created by platoply"); header_complete_ply (ply); /* set up and write the vertex elements */ put_element_setup_ply (ply, "vertex"); for (i = 0; i < nverts; i++) put_element_ply (ply, (void *) &vlist[i]); /* set up and write the face elements */ put_element_setup_ply (ply, "face"); for (i = 0; i < nfaces; i++) put_element_ply (ply, (void *) &flist[i]); close_ply (ply); free_ply (ply); }
void read_file(void) { int i,j; int elem_count; char *elem_name; PlyFile *in_ply; /*** Read in the original PLY object ***/ in_ply = read_ply (stdin); for (i = 0; i < in_ply->num_elem_types; i++) { /* prepare to read the i'th list of elements */ elem_name = setup_element_read_ply (in_ply, i, &elem_count); if (equal_strings ("vertex", elem_name)) { /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count); nverts = elem_count; /* set up for getting vertex elements */ setup_property_ply (in_ply, &vert_props[0]); setup_property_ply (in_ply, &vert_props[1]); setup_property_ply (in_ply, &vert_props[2]); for (j = 0; j < in_ply->elems[i]->nprops; j++) { PlyProperty *prop; prop = in_ply->elems[i]->props[j]; if (equal_strings ("r", prop->name)) { setup_property_ply (in_ply, &vert_props[3]); per_vertex_color = 1; } if (equal_strings ("g", prop->name)) { setup_property_ply (in_ply, &vert_props[4]); per_vertex_color = 1; } if (equal_strings ("b", prop->name)) { setup_property_ply (in_ply, &vert_props[5]); per_vertex_color = 1; } if (equal_strings ("nx", prop->name)) { setup_property_ply (in_ply, &vert_props[6]); has_normals = 1; } if (equal_strings ("ny", prop->name)) { setup_property_ply (in_ply, &vert_props[7]); has_normals = 1; } if (equal_strings ("nz", prop->name)) { setup_property_ply (in_ply, &vert_props[8]); has_normals = 1; } } vert_other = get_other_properties_ply (in_ply, offsetof(Vertex,other_props)); /* grab all the vertex elements */ for (j = 0; j < elem_count; j++) { vlist[j] = (Vertex *) malloc (sizeof (Vertex)); vlist[j]->r = 1; vlist[j]->g = 1; vlist[j]->b = 1; get_element_ply (in_ply, (void *) vlist[j]); } } else if (equal_strings ("face", elem_name)) { /* create a list to hold all the face elements */ flist = (Face **) malloc (sizeof (Face *) * elem_count); nfaces = elem_count; /* set up for getting face elements */ setup_property_ply (in_ply, &face_props[0]); face_other = get_other_properties_ply (in_ply, offsetof(Face,other_props)); /* grab all the face elements */ for (j = 0; j < elem_count; j++) { flist[j] = (Face *) malloc (sizeof (Face)); get_element_ply (in_ply, (void *) flist[j]); } } else get_other_element_ply (in_ply); } close_ply (in_ply); free_ply (in_ply); }
void write_file ( void ) /******************************************************************************/ /* Purpose: WRITE_FILE writes the PLY file to the standard output unit. Modified: 17 January 2011 Author: Greg Turk */ { int i; int num_elem_types; PlyFile *ply; /* Write out the transformed PLY object. */ ply = write_ply (stdout, nelems, elem_names, PLY_ASCII); /* Describe what properties go into the vertex elements. */ describe_element_ply (ply, "vertex", nverts); describe_property_ply (ply, &vert_props[0]); describe_property_ply (ply, &vert_props[1]); describe_property_ply (ply, &vert_props[2]); if ( has_normals ) { describe_property_ply (ply, &vert_props[3]); describe_property_ply (ply, &vert_props[4]); describe_property_ply (ply, &vert_props[5]); } if (texture_coords) { describe_property_ply (ply, &vert_props[6]); describe_property_ply (ply, &vert_props[7]); } describe_element_ply (ply, "face", nfaces); describe_property_ply (ply, &face_props[0]); /* Insert the comments. */ for (i = 0; i < ncomments; i++) { append_comment_ply (ply, comments[i]); } append_comment_ply (ply, "converted from OBJ by obj2ply"); header_complete_ply (ply); /* Set up and write the vertex elements. */ put_element_setup_ply (ply, "vertex"); for (i = 0; i < nverts; i++) { put_element_ply (ply, (void *) &vlist[i]); } /* Set up and write the face elements. */ put_element_setup_ply (ply, "face"); for (i = 0; i < nfaces; i++) { put_element_ply (ply, (void *) &flist[i]); } close_ply ( ply ); free_ply ( ply ); return; }
bool TrisetObject::loadPLY(QString flnm) { m_position = Vec(0,0,0); m_scale = Vec(1,1,1); typedef struct Vertex { float x,y,z; float r,g,b; float nx,ny,nz; void *other_props; /* other properties */ } Vertex; typedef struct Face { unsigned char nverts; /* number of vertex indices in list */ int *verts; /* vertex index list */ void *other_props; /* other properties */ } Face; PlyProperty vert_props[] = { /* list of property information for a vertex */ {"x", Float32, Float32, offsetof(Vertex,x), 0, 0, 0, 0}, {"y", Float32, Float32, offsetof(Vertex,y), 0, 0, 0, 0}, {"z", Float32, Float32, offsetof(Vertex,z), 0, 0, 0, 0}, {"red", Float32, Float32, offsetof(Vertex,r), 0, 0, 0, 0}, {"green", Float32, Float32, offsetof(Vertex,g), 0, 0, 0, 0}, {"blue", Float32, Float32, offsetof(Vertex,b), 0, 0, 0, 0}, {"nx", Float32, Float32, offsetof(Vertex,nx), 0, 0, 0, 0}, {"ny", Float32, Float32, offsetof(Vertex,ny), 0, 0, 0, 0}, {"nz", Float32, Float32, offsetof(Vertex,nz), 0, 0, 0, 0}, }; PlyProperty face_props[] = { /* list of property information for a face */ {"vertex_indices", Int32, Int32, offsetof(Face,verts), 1, Uint8, Uint8, offsetof(Face,nverts)}, }; /*** the PLY object ***/ int nverts,nfaces; Vertex **vlist; Face **flist; PlyOtherProp *vert_other,*face_other; bool per_vertex_color = false; bool has_normals = false; int i,j; int elem_count; char *elem_name; PlyFile *in_ply; /*** Read in the original PLY object ***/ FILE *fp = fopen(flnm.toAscii().data(), "rb"); in_ply = read_ply (fp); for (i = 0; i < in_ply->num_elem_types; i++) { /* prepare to read the i'th list of elements */ elem_name = setup_element_read_ply (in_ply, i, &elem_count); if (equal_strings ("vertex", elem_name)) { /* create a vertex list to hold all the vertices */ vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count); nverts = elem_count; /* set up for getting vertex elements */ setup_property_ply (in_ply, &vert_props[0]); setup_property_ply (in_ply, &vert_props[1]); setup_property_ply (in_ply, &vert_props[2]); for (j = 0; j < in_ply->elems[i]->nprops; j++) { PlyProperty *prop; prop = in_ply->elems[i]->props[j]; if (equal_strings ("r", prop->name) || equal_strings ("red", prop->name)) { setup_property_ply (in_ply, &vert_props[3]); per_vertex_color = true; } if (equal_strings ("g", prop->name) || equal_strings ("green", prop->name)) { setup_property_ply (in_ply, &vert_props[4]); per_vertex_color = true; } if (equal_strings ("b", prop->name) || equal_strings ("blue", prop->name)) { setup_property_ply (in_ply, &vert_props[5]); per_vertex_color = true; } if (equal_strings ("nx", prop->name)) { setup_property_ply (in_ply, &vert_props[6]); has_normals = true; } if (equal_strings ("ny", prop->name)) { setup_property_ply (in_ply, &vert_props[7]); has_normals = true; } if (equal_strings ("nz", prop->name)) { setup_property_ply (in_ply, &vert_props[8]); has_normals = true; } } vert_other = get_other_properties_ply (in_ply, offsetof(Vertex,other_props)); /* grab all the vertex elements */ for (j = 0; j < elem_count; j++) { vlist[j] = (Vertex *) malloc (sizeof (Vertex)); get_element_ply (in_ply, (void *) vlist[j]); } } else if (equal_strings ("face", elem_name)) { /* create a list to hold all the face elements */ flist = (Face **) malloc (sizeof (Face *) * elem_count); nfaces = elem_count; /* set up for getting face elements */ setup_property_ply (in_ply, &face_props[0]); face_other = get_other_properties_ply (in_ply, offsetof(Face,other_props)); /* grab all the face elements */ for (j = 0; j < elem_count; j++) { flist[j] = (Face *) malloc (sizeof (Face)); get_element_ply (in_ply, (void *) flist[j]); } } else get_other_element_ply (in_ply); } close_ply (in_ply); free_ply (in_ply); if (Global::volumeType() == Global::DummyVolume) { float minX, maxX; float minY, maxY; float minZ, maxZ; minX = maxX = vlist[0]->x; minY = maxY = vlist[0]->y; minZ = maxZ = vlist[0]->z; for(int i=0; i<nverts; i++) { minX = qMin(minX, vlist[i]->x); maxX = qMax(maxX, vlist[i]->x); minY = qMin(minY, vlist[i]->y); maxY = qMax(maxY, vlist[i]->y); minZ = qMin(minZ, vlist[i]->z); maxZ = qMax(maxZ, vlist[i]->z); } minX = floor(minX); minY = floor(minY); minZ = floor(minZ); maxX = ceil(maxX); maxY = ceil(maxY); maxZ = ceil(maxZ); int h = maxX-minX+1; int w = maxY-minY+1; int d = maxZ-minZ+1; m_nX = d; m_nY = w; m_nZ = h; m_position = Vec(-minX, -minY, -minZ); // bool ok; // QString text = QInputDialog::getText(0, // "Please enter grid size", // "Grid Size", // QLineEdit::Normal, // QString("%1 %2 %3").\ // arg(d).arg(w).arg(h), // &ok); // if (!ok || text.isEmpty()) // { // QMessageBox::critical(0, "Cannot load PLY", "No grid"); // return false; // } // // int nx=0; // int ny=0; // int nz=0; // QStringList gs = text.split(" ", QString::SkipEmptyParts); // if (gs.count() > 0) nx = gs[0].toInt(); // if (gs.count() > 1) ny = gs[1].toInt(); // if (gs.count() > 2) nz = gs[2].toInt(); // if (nx > 0 && ny > 0 && nz > 0) // { // m_nX = nx; // m_nY = ny; // m_nZ = nz; // } // else // { // QMessageBox::critical(0, "Cannot load triset", "No grid"); // return false; // } // // if (d == m_nX && w == m_nY && h == m_nZ) // m_position = Vec(-minX, -minY, -minZ); } else { Vec dim = VolumeInformation::volumeInformation(0).dimensions; m_nZ = dim.x; m_nY = dim.y; m_nX = dim.z; } m_vertices.resize(nverts); for(int i=0; i<nverts; i++) m_vertices[i] = Vec(vlist[i]->x, vlist[i]->y, vlist[i]->z); m_normals.clear(); if (has_normals) { m_normals.resize(nverts); for(int i=0; i<nverts; i++) m_normals[i] = Vec(vlist[i]->nx, vlist[i]->ny, vlist[i]->nz); } m_vcolor.clear(); if (per_vertex_color) { m_vcolor.resize(nverts); for(int i=0; i<nverts; i++) m_vcolor[i] = Vec(vlist[i]->r/255.0f, vlist[i]->g/255.0f, vlist[i]->b/255.0f); } // only triangles considered int ntri=0; for (int i=0; i<nfaces; i++) { if (flist[i]->nverts >= 3) ntri++; } m_triangles.resize(3*ntri); int tri=0; for(int i=0; i<nfaces; i++) { if (flist[i]->nverts >= 3) { m_triangles[3*tri+0] = flist[i]->verts[0]; m_triangles[3*tri+1] = flist[i]->verts[1]; m_triangles[3*tri+2] = flist[i]->verts[2]; tri++; } } m_tvertices.resize(nverts); m_tnormals.resize(nverts); m_texValues.resize(nverts); Vec bmin = m_vertices[0]; Vec bmax = m_vertices[0]; for(int i=0; i<nverts; i++) { bmin = StaticFunctions::minVec(bmin, m_vertices[i]); bmax = StaticFunctions::maxVec(bmax, m_vertices[i]); } m_centroid = (bmin + bmax)/2; m_enclosingBox[0] = Vec(bmin.x, bmin.y, bmin.z); m_enclosingBox[1] = Vec(bmax.x, bmin.y, bmin.z); m_enclosingBox[2] = Vec(bmax.x, bmax.y, bmin.z); m_enclosingBox[3] = Vec(bmin.x, bmax.y, bmin.z); m_enclosingBox[4] = Vec(bmin.x, bmin.y, bmax.z); m_enclosingBox[5] = Vec(bmax.x, bmin.y, bmax.z); m_enclosingBox[6] = Vec(bmax.x, bmax.y, bmax.z); m_enclosingBox[7] = Vec(bmin.x, bmax.y, bmax.z); m_pointStep = qMax(1, nverts/50000); // QMessageBox::information(0, "", QString("%1 %2 %3\n%4 %5"). \ // arg(m_nX).arg(m_nY).arg(m_nZ). \ // arg(m_vertices.count()). \ // arg(m_triangles.count()/3)); m_fileName = flnm; return true; }
void TrisetObject::save() { bool has_normals = (m_normals.count() > 0); bool per_vertex_color = (m_vcolor.count() > 0); QString flnm = QFileDialog::getSaveFileName(0, "Export mesh to file", Global::previousDirectory(), "*.ply"); if (flnm.size() == 0) return; typedef struct PlyFace { unsigned char nverts; /* number of Vertex indices in list */ int *verts; /* Vertex index list */ } PlyFace; typedef struct { float x, y, z; /**< Vertex coordinates */ float nx, ny, nz; /**< Vertex normal */ uchar r, g, b; } myVertex ; PlyProperty vert_props[] = { /* list of property information for a PlyVertex */ {"x", Float32, Float32, offsetof( myVertex,x ), 0, 0, 0, 0}, {"y", Float32, Float32, offsetof( myVertex,y ), 0, 0, 0, 0}, {"z", Float32, Float32, offsetof( myVertex,z ), 0, 0, 0, 0}, {"nx", Float32, Float32, offsetof( myVertex,nx ), 0, 0, 0, 0}, {"ny", Float32, Float32, offsetof( myVertex,ny ), 0, 0, 0, 0}, {"nz", Float32, Float32, offsetof( myVertex,nz ), 0, 0, 0, 0}, {"red", Uint8, Uint8, offsetof( myVertex,r ), 0, 0, 0, 0}, {"green", Uint8, Uint8, offsetof( myVertex,g ), 0, 0, 0, 0}, {"blue", Uint8, Uint8, offsetof( myVertex,b ), 0, 0, 0, 0} }; PlyProperty face_props[] = { /* list of property information for a PlyFace */ {"vertex_indices", Int32, Int32, offsetof( PlyFace,verts ), 1, Uint8, Uint8, offsetof( PlyFace,nverts )}, }; PlyFile *ply; FILE *fp = fopen(flnm.toAscii().data(), bin ? "wb" : "w"); PlyFace face ; int verts[3] ; char *elem_names[] = { "vertex", "face" }; ply = write_ply (fp, 2, elem_names, bin? PLY_BINARY_LE : PLY_ASCII ); int nvertices = m_vertices.count(); /* describe what properties go into the PlyVertex elements */ describe_element_ply ( ply, "vertex", nvertices ); describe_property_ply ( ply, &vert_props[0] ); describe_property_ply ( ply, &vert_props[1] ); describe_property_ply ( ply, &vert_props[2] ); describe_property_ply ( ply, &vert_props[3] ); describe_property_ply ( ply, &vert_props[4] ); describe_property_ply ( ply, &vert_props[5] ); describe_property_ply ( ply, &vert_props[6] ); describe_property_ply ( ply, &vert_props[7] ); describe_property_ply ( ply, &vert_props[8] ); /* describe PlyFace properties (just list of PlyVertex indices) */ int ntriangles = m_triangles.count()/3; describe_element_ply ( ply, "face", ntriangles ); describe_property_ply ( ply, &face_props[0] ); header_complete_ply ( ply ); /* set up and write the PlyVertex elements */ put_element_setup_ply ( ply, "vertex" ); for(int i=0; i<m_vertices.count(); i++) { myVertex vertex; vertex.x = m_vertices[i].x*m_scale.x; vertex.y = m_vertices[i].y*m_scale.y; vertex.z = m_vertices[i].z*m_scale.z; if (has_normals) { vertex.nx = m_normals[i].x; vertex.ny = m_normals[i].y; vertex.nz = m_normals[i].z; } if (per_vertex_color) { vertex.r = 255*m_vcolor[i].x; vertex.g = 255*m_vcolor[i].y; vertex.b = 255*m_vcolor[i].z; } put_element_ply ( ply, ( void * ) &vertex ); } put_element_setup_ply ( ply, "face" ); face.nverts = 3 ; face.verts = verts ; for(int i=0; i<m_triangles.count()/3; i++) { int v0 = m_triangles[3*i]; int v1 = m_triangles[3*i+1]; int v2 = m_triangles[3*i+2]; face.verts[0] = v0; face.verts[1] = v1; face.verts[2] = v2; put_element_ply ( ply, ( void * ) &face ); } close_ply ( ply ); free_ply ( ply ); fclose( fp ) ; QMessageBox::information(0, "Save Mesh", "done"); }
bool MeshGenerator::loadPLY(QString flnm) { PlyProperty vert_props[] = { /* list of property information for a vertex */ {"x", Float32, Float32, offsetof(Vertex,x), 0, 0, 0, 0}, {"y", Float32, Float32, offsetof(Vertex,y), 0, 0, 0, 0}, {"z", Float32, Float32, offsetof(Vertex,z), 0, 0, 0, 0}, {"nx", Float32, Float32, offsetof(Vertex,nx), 0, 0, 0, 0}, {"ny", Float32, Float32, offsetof(Vertex,ny), 0, 0, 0, 0}, {"nz", Float32, Float32, offsetof(Vertex,nz), 0, 0, 0, 0}, {"red", Uint8, Uint8, offsetof(Vertex,r), 0, 0, 0, 0}, {"green", Uint8, Uint8, offsetof(Vertex,g), 0, 0, 0, 0}, {"blue", Uint8, Uint8, offsetof(Vertex,b), 0, 0, 0, 0}, }; PlyProperty face_props[] = { /* list of property information for a face */ {"vertex_indices", Int32, Int32, offsetof(Face,verts), 1, Uint8, Uint8, offsetof(Face,nverts)}, }; /*** the PLY object ***/ PlyOtherProp *vert_other,*face_other; bool per_vertex_color = false; bool has_normals = false; int i,j; int elem_count; char *elem_name; PlyFile *in_ply; /*** Read in the original PLY object ***/ FILE *fp = fopen(flnm.toLatin1().data(), "rb"); in_ply = read_ply (fp); for (i = 0; i < in_ply->num_elem_types; i++) { /* prepare to read the i'th list of elements */ elem_name = setup_element_read_ply (in_ply, i, &elem_count); if (equal_strings ("vertex", elem_name)) { /* create a vertex list to hold all the vertices */ m_vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count); m_nverts = elem_count; /* set up for getting vertex elements */ setup_property_ply (in_ply, &vert_props[0]); setup_property_ply (in_ply, &vert_props[1]); setup_property_ply (in_ply, &vert_props[2]); for (j = 0; j < in_ply->elems[i]->nprops; j++) { PlyProperty *prop; prop = in_ply->elems[i]->props[j]; if (equal_strings ("r", prop->name) || equal_strings ("red", prop->name)) { setup_property_ply (in_ply, &vert_props[3]); per_vertex_color = true; } if (equal_strings ("g", prop->name) || equal_strings ("green", prop->name)) { setup_property_ply (in_ply, &vert_props[4]); per_vertex_color = true; } if (equal_strings ("b", prop->name) || equal_strings ("blue", prop->name)) { setup_property_ply (in_ply, &vert_props[5]); per_vertex_color = true; } if (equal_strings ("nx", prop->name)) { setup_property_ply (in_ply, &vert_props[6]); has_normals = true; } if (equal_strings ("ny", prop->name)) { setup_property_ply (in_ply, &vert_props[7]); has_normals = true; } if (equal_strings ("nz", prop->name)) { setup_property_ply (in_ply, &vert_props[8]); has_normals = true; } } /* grab all the vertex elements */ for (j = 0; j < elem_count; j++) { m_vlist[j] = (Vertex *) malloc (sizeof (Vertex)); get_element_ply (in_ply, (void *) m_vlist[j]); } } else if (equal_strings ("face", elem_name)) { /* create a list to hold all the face elements */ m_flist = (Face **) malloc (sizeof (Face *) * elem_count); m_nfaces = elem_count; /* set up for getting face elements */ setup_property_ply (in_ply, &face_props[0]); /* grab all the face elements */ for (j = 0; j < elem_count; j++) { m_flist[j] = (Face *) malloc (sizeof (Face)); get_element_ply (in_ply, (void *) m_flist[j]); } } else get_other_element_ply (in_ply); } close_ply (in_ply); free_ply (in_ply); vcolor = new float[m_nverts*3]; for(int i=0; i<m_nverts; i++) { vcolor[3*i+0] = m_vlist[i]->r/255.0f; vcolor[3*i+1] = m_vlist[i]->g/255.0f; vcolor[3*i+2] = m_vlist[i]->b/255.0f; } QMessageBox::information(0, "", QString("Loaded input mesh : vertices %1 faces %2").\ arg(m_nverts).\ arg(m_nfaces)); return true; }
void MeshGenerator::savePLY(QString flnm) { m_meshLog->moveCursor(QTextCursor::End); m_meshLog->insertPlainText("Saving Mesh"); PlyProperty vert_props[] = { /* list of property information for a PlyVertex */ {"x", Float32, Float32, offsetof( Vertex,x ), 0, 0, 0, 0}, {"y", Float32, Float32, offsetof( Vertex,y ), 0, 0, 0, 0}, {"z", Float32, Float32, offsetof( Vertex,z ), 0, 0, 0, 0}, {"nx", Float32, Float32, offsetof( Vertex,nx ), 0, 0, 0, 0}, {"ny", Float32, Float32, offsetof( Vertex,ny ), 0, 0, 0, 0}, {"nz", Float32, Float32, offsetof( Vertex,nz ), 0, 0, 0, 0}, {"red", Uint8, Uint8, offsetof( Vertex,r ), 0, 0, 0, 0}, {"green", Uint8, Uint8, offsetof( Vertex,g ), 0, 0, 0, 0}, {"blue", Uint8, Uint8, offsetof( Vertex,b ), 0, 0, 0, 0} }; PlyProperty face_props[] = { /* list of property information for a PlyFace */ {"vertex_indices", Int32, Int32, offsetof( Face,verts ), 1, Uint8, Uint8, offsetof( Face,nverts )}, }; PlyFile *ply; FILE *fp = fopen(flnm.toLatin1().data(), "wb"); Face face ; int verts[3] ; char *elem_names[] = { "vertex", "face" }; ply = write_ply (fp, 2, elem_names, PLY_BINARY_LE ); /* describe what properties go into the PlyVertex elements */ describe_element_ply ( ply, "vertex", m_nverts ); describe_property_ply ( ply, &vert_props[0] ); describe_property_ply ( ply, &vert_props[1] ); describe_property_ply ( ply, &vert_props[2] ); describe_property_ply ( ply, &vert_props[3] ); describe_property_ply ( ply, &vert_props[4] ); describe_property_ply ( ply, &vert_props[5] ); describe_property_ply ( ply, &vert_props[6] ); describe_property_ply ( ply, &vert_props[7] ); describe_property_ply ( ply, &vert_props[8] ); /* describe PlyFace properties (just list of PlyVertex indices) */ describe_element_ply ( ply, "face", m_nfaces ); describe_property_ply ( ply, &face_props[0] ); header_complete_ply ( ply ); /* set up and write the PlyVertex elements */ put_element_setup_ply ( ply, "vertex" ); for(int ni=0; ni<m_nverts; ni++) put_element_ply ( ply, ( void * ) m_vlist[ni] ); /* set up and write the PlyFace elements */ put_element_setup_ply ( ply, "face" ); for(int ni=0; ni<m_nfaces; ni++) put_element_ply ( ply, ( void * ) m_flist[ni] ); close_ply ( ply ); free_ply ( ply ); m_meshProgress->setValue(100); }
write_file() { int i; PlyFile *ply; char **elist; int num_elem_types; /*** Write out the transformed PLY object ***/ elist = get_element_list_ply (in_ply, &num_elem_types); ply = write_ply (stdout, num_elem_types, elist, in_ply->file_type); /* describe what properties go into the vertex elements */ /* (position x,y,z and normals nx,ny,nz if they were provided) */ describe_element_ply (ply, "vertex", nverts); describe_property_ply (ply, &vert_props[0]); describe_property_ply (ply, &vert_props[1]); describe_property_ply (ply, &vert_props[2]); if (has_nx) describe_property_ply (ply, &vert_props[3]); if (has_ny) describe_property_ply (ply, &vert_props[4]); if (has_nz) describe_property_ply (ply, &vert_props[5]); /* all other vertex properties besides position and normal */ describe_other_properties_ply (ply, vert_other, offsetof(Vertex,other_props)); /* describe face properties (just list of vertex indices) */ describe_element_ply (ply, "face", nfaces); describe_property_ply (ply, &face_props[0]); describe_other_properties_ply (ply, face_other, offsetof(Face,other_props)); /* all other properties that we tucked away are mentioned here */ describe_other_elements_ply (ply, in_ply->other_elems); /* copy the comments and other textual object information */ copy_comments_ply (ply, in_ply); append_comment_ply (ply, "modified by flipply"); copy_obj_info_ply (ply, in_ply); /* we've told the routines enough information so that the file header */ /* can be written out now */ header_complete_ply (ply); /* set up and write the vertex elements */ put_element_setup_ply (ply, "vertex"); for (i = 0; i < nverts; i++) put_element_ply (ply, (void *) vlist[i]); /* set up and write the face elements */ put_element_setup_ply (ply, "face"); for (i = 0; i < nfaces; i++) put_element_ply (ply, (void *) flist[i]); /* the other properties that we tucked away are written out here */ put_other_elements_ply (ply); /* close the file and free up the memory */ close_ply (ply); free_ply (ply); }