/* prepares the conversion */ static void setup_callbacks(p_ply iply, p_ply oply) { p_ply_element element = NULL; /* iterate over all elements in input file */ while ((element = ply_get_next_element(iply, element))) { p_ply_property property = NULL; gint32 ninstances = 0; const char *element_name; ply_get_element_info(element, &element_name, &ninstances); /* add this element to output file */ if (!ply_add_element(oply, element_name, ninstances)) error("Unable to add output element '%s'", element_name); /* iterate over all properties of current element */ while ((property = ply_get_next_property(element, property))) { const char *property_name; e_ply_type type, length_type, value_type; ply_get_property_info(property, &property_name, &type, &length_type, &value_type); /* setup input callback for this property */ if (!ply_set_read_cb(iply, element_name, property_name, callback, oply, 0)) error("Unable to setup input callback for property '%s'", property_name); /* add this property to output file */ if (!ply_add_property(oply, property_name, type, length_type, value_type)) error("Unable to add output property '%s'", property_name); } } }
static int setup_callbacks(p_ply iply, p_ply oply) { p_ply_element element = NULL; /* iterate over all elements in input file */ while ((element = ply_get_next_element(iply, element))) { p_ply_property property = NULL; long nelems = 0; const char *element_name; ply_get_element_info(element, &element_name, &nelems); /* add this element to output file */ if (!ply_add_element(oply, element_name, nelems)) return 0; /* iterate over all properties of current element */ while ((property = ply_get_next_property(element, property))) { const char *property_name; e_ply_type type, length_type, value_type; ply_get_property_info(property, &property_name, &type, &length_type, &value_type); /* setup input callback for this property */ if (!ply_set_read_cb(iply, element_name, property_name, callback, oply, 0)) return 0; /* add this property to output file */ if (!ply_add_property(oply, property_name, type, length_type, value_type)) return 0; } } return 1; }
bool save(const std::string& filename) { if(map_ == nil) { Logger::err("PlyMeshSave") << "mesh is null" << std::endl ; return false ; } p_ply ply = ply_create(filename.c_str(), PLY_LITTLE_ENDIAN, nil, 0, nil) ; if(ply == nil) { Logger::err("PlyMeshSave") << filename << ": could not open" << std::endl ; return false ; } ////////////////////////////////////////////////////////////////////////// if (!ply_add_comment(ply, "saved by [email protected]")) { Logger::err("PlyMeshSave") << "unable to add comment" << std::endl ; ply_close(ply) ; return false ; } int num_v = map_->size_of_vertices(); if (!ply_add_element(ply, "vertex", num_v)) { Logger::err("PlyMeshSave") << "unable to add element \'vertex\'" << std::endl ; ply_close(ply) ; return false ; } e_ply_type length_type, value_type; length_type = value_type = static_cast<e_ply_type>(-1); std::string pos[3] = { "x", "y", "z" }; for (unsigned int i=0; i<3; ++i) { if (!ply_add_property(ply, pos[i].c_str(), PLY_FLOAT, length_type, value_type)) { Logger::err("PlyMeshSave") << "unable to add property \'" << pos[i] << "\'" << std::endl ; ply_close(ply) ; return false ; } } MapVertexAttribute<Color> vertex_color; vertex_color.bind_if_defined(const_cast<Map*>(map_), "color") ; if (vertex_color.is_bound()) { std::string color[4] = { "red", "green", "blue", "alpha" }; for (unsigned int i=0; i<4; ++i) { if (!ply_add_property(ply, color[i].c_str(), PLY_UCHAR, length_type, value_type)) { Logger::err("PlyMeshSave") << "unable to add property \'" << color[i] << "\'" << std::endl ; ply_close(ply) ; return false ; } } } int num_f = map_->size_of_facets(); if (!ply_add_element(ply, "face", num_f)) { Logger::err("PlyMeshSave") << "unable to add element \'face\'" << std::endl ; ply_close(ply) ; return false ; } if (!ply_add_property(ply, "vertex_indices", PLY_LIST, PLY_UCHAR, PLY_INT)) { Logger::err("PlyMeshSave") << "unable to add property \'vertex_indices\'" << std::endl ; ply_close(ply) ; return false ; } if(!ply_write_header(ply)) { Logger::err("PlyMeshSave") << filename << ": invalid PLY file" << std::endl ; ply_close(ply) ; return false ; } ////////////////////////////////////////////////////////////////////////// FOR_EACH_VERTEX_CONST(Map, map_, it) { const vec3& p = it->point(); ply_write(ply, p.x); ply_write(ply, p.y); ply_write(ply, p.z); if (vertex_color.is_bound()) { const Color& c = vertex_color[it]; double r = c.r() * color_mult_; ogf_clamp(r, 0.0, 255.0); double g = c.g() * color_mult_; ogf_clamp(g, 0.0, 255.0); double b = c.b() * color_mult_; ogf_clamp(b, 0.0, 255.0); double a = c.a() * color_mult_; ogf_clamp(a, 0.0, 255.0); ply_write(ply, r); ply_write(ply, g); ply_write(ply, b); ply_write(ply, a); } } // ply files numbering starts with 0 Attribute<Map::Vertex, int> vertex_id(map_->vertex_attribute_manager()); MapEnumerator::enumerate_vertices(const_cast<Map*>(map_), vertex_id, 0); FOR_EACH_FACET_CONST(Map, map_, it) { ply_write(ply, it->nb_vertices()); Map::Halfedge* h = it->halfedge(); do { int id = vertex_id[h->vertex()]; ply_write(ply, id); h = h->next(); } while (h != it->halfedge()); }
/******************************************************************************* * Name: main * Description: ******************************************************************************/ int main( void ) { int i; /* Create new PLY file. */ p_ply oply = ply_create( "new.ply", PLY_ASCII, NULL, 0, NULL ); if ( !oply ) { fprintf( stderr, "ERROR: Could not create »new.ply«\n" ); return EXIT_FAILURE; } /* Add object information to PLY. */ if ( !ply_add_obj_info( oply, "This is just a test." ) ) { fprintf( stderr, "ERROR: Could not add object info.\n" ); return EXIT_FAILURE; } /* Add a comment, too. */ if ( !ply_add_comment( oply, "Just some comment…" ) ) { fprintf( stderr, "ERROR: Could not add comment.\n" ); return EXIT_FAILURE; } /* Add vertex element. We want to add 9999 vertices. */ if ( !ply_add_element( oply, "vertex", 99999 ) ) { fprintf( stderr, "ERROR: Could not add element.\n" ); return EXIT_FAILURE; } /* Add vertex properties: x, y, z, r, g, b */ if ( !ply_add_property( oply, "x", PLY_FLOAT, 0, 0 ) ) { fprintf( stderr, "ERROR: Could not add property x.\n" ); return EXIT_FAILURE; } if ( !ply_add_property( oply, "y", PLY_FLOAT, 0, 0 ) ) { fprintf( stderr, "ERROR: Could not add property x.\n" ); return EXIT_FAILURE; } if ( !ply_add_property( oply, "z", PLY_FLOAT, 0, 0 ) ) { fprintf( stderr, "ERROR: Could not add property x.\n" ); return EXIT_FAILURE; } if ( !ply_add_property( oply, "red", PLY_UCHAR, 0, 0 ) ) { fprintf( stderr, "ERROR: Could not add property x.\n" ); return EXIT_FAILURE; } if ( !ply_add_property( oply, "green", PLY_UCHAR, 0, 0 ) ) { fprintf( stderr, "ERROR: Could not add property x.\n" ); return EXIT_FAILURE; } if ( !ply_add_property( oply, "blue", PLY_UCHAR, 0, 0 ) ) { fprintf( stderr, "ERROR: Could not add property x.\n" ); return EXIT_FAILURE; } /* Write header to file */ if ( !ply_write_header( oply ) ) { fprintf( stderr, "ERROR: Could not write header.\n" ); return EXIT_FAILURE; } /* Now we generate random data to add to the file: */ srand ( time( NULL ) ); for ( i = 0; i < 99999; i++ ) { ply_write( oply, (double) ( rand() % 10000 ) / 10.0 ); /* x */ ply_write( oply, (double) ( rand() % 10000 ) / 10.0 ); /* y */ ply_write( oply, (double) ( rand() % 10000 ) / 10.0 ); /* z */ ply_write( oply, rand() % 256 ); /* red */ ply_write( oply, rand() % 256 ); /* blue */ ply_write( oply, rand() % 256 ); /* green */ } if ( !ply_close( oply ) ) { fprintf( stderr, "ERROR: Could not close file.\n" ); return EXIT_FAILURE; } return EXIT_SUCCESS; }