bool write_off(const Surface_mesh& mesh, const std::string& filename) { FILE* out = fopen(filename.c_str(), "w"); if (!out) return false; // header fprintf(out, "OFF\n%d %d 0\n", mesh.n_vertices(), mesh.n_faces()); // vertices Surface_mesh::Vertex_property<Surface_mesh::Point> points = mesh.get_vertex_property<Point>("v:point"); for (Surface_mesh::Vertex_iterator vit=mesh.vertices_begin(); vit!=mesh.vertices_end(); ++vit) { const Point& p = points[vit]; fprintf(out, "%.10f %.10f %.10f\n", p[0], p[1], p[2]); } // faces for (Surface_mesh::Face_iterator fit=mesh.faces_begin(); fit!=mesh.faces_end(); ++fit) { int nV = mesh.valence(fit); fprintf(out, "%d", nV); Surface_mesh::Vertex_around_face_circulator fvit=mesh.vertices(fit), fvend=fvit; do { fprintf(out, " %d", ((Surface_mesh::Vertex)fvit).idx()); } while (++fvit != fvend); fprintf(out, "\n"); } fclose(out); return true; }
bool write_obj(const Surface_mesh& mesh, const std::string& filename) { FILE* out = fopen(filename.c_str(), "w"); if (!out) return false; // comment fprintf(out, "# OBJ export from Surface_mesh\n"); //vertices Surface_mesh::Vertex_property<Point> points = mesh.get_vertex_property<Point>("v:point"); for (Surface_mesh::Vertex_iterator vit=mesh.vertices_begin(); vit!=mesh.vertices_end(); ++vit) { const Point& p = points[*vit]; fprintf(out, "v %.10f %.10f %.10f\n", p[0], p[1], p[2]); } //normals Surface_mesh::Vertex_property<Point> normals = mesh.get_vertex_property<Point>("v:normal"); for (Surface_mesh::Vertex_iterator vit=mesh.vertices_begin(); vit!=mesh.vertices_end(); ++vit) { const Point& p = normals[*vit]; fprintf(out, "vn %.10f %.10f %.10f\n", p[0], p[1], p[2]); } //optionally texture coordinates // do we have them? std::vector<std::string> h_props= mesh.halfedge_properties(); bool with_tex_coord = false; std::vector<std::string>::iterator h_prop_end = h_props.end(); std::vector<std::string>::iterator h_prop_start= h_props.begin(); while(h_prop_start!=h_prop_end) { if(0==(*h_prop_start).compare("h:texcoord")) { with_tex_coord=true; } ++h_prop_start; } //if so then add if(with_tex_coord) { Surface_mesh::Halfedge_property<Texture_coordinate> tex_coord = mesh.get_halfedge_property<Texture_coordinate>("h:texcoord"); for (Surface_mesh::Halfedge_iterator hit=mesh.halfedges_begin(); hit!=mesh.halfedges_end(); ++hit) { const Texture_coordinate& pt = tex_coord[*hit]; fprintf(out, "vt %.10f %.10f %.10f\n", pt[0], pt[1], pt[2]); } } //faces for (Surface_mesh::Face_iterator fit=mesh.faces_begin(); fit!=mesh.faces_end(); ++fit) { fprintf(out, "f"); Surface_mesh::Vertex_around_face_circulator fvit=mesh.vertices(*fit), fvend=fvit; Surface_mesh::Halfedge_around_face_circulator fhit=mesh.halfedges(*fit); do { if(with_tex_coord) { // write vertex index, tex_coord index and normal index fprintf(out, " %d/%d/%d", (*fvit).idx()+1, (*fhit).idx()+1, (*fvit).idx()+1); ++fhit; } else { // write vertex index and normal index fprintf(out, " %d//%d", (*fvit).idx()+1, (*fvit).idx()+1); } } while (++fvit != fvend); fprintf(out, "\n"); } fclose(out); return true; }