// If the mesh is a topological disk, extract its longest border, // else compute a very simple cut to make it homeomorphic to a disk. // Return the border of this region (empty on error) // // CAUTION: this cutting algorithm is very naive. Write your own! static Seam cut_mesh(Parameterization_polyhedron_adaptor& mesh_adaptor) { // Helper class to compute genus or extract borders typedef CGAL::Parameterization_mesh_feature_extractor<Parameterization_polyhedron_adaptor> Mesh_feature_extractor; Seam seam; // returned list // Get reference to Polyhedron_3 mesh Polyhedron& mesh = mesh_adaptor.get_adapted_mesh(); // Extract mesh borders and compute genus Mesh_feature_extractor feature_extractor(mesh_adaptor); int nb_borders = feature_extractor.get_nb_borders(); int genus = feature_extractor.get_genus(); // If mesh is a topological disk if (genus == 0 && nb_borders > 0) { // Pick the longest border seam = feature_extractor.get_longest_border(); } else // if mesh is *not* a topological disk, create a virtual cut { const int CUT_LENGTH = 6; // Build consecutive halfedges array Polyhedron::Halfedge_handle seam_halfedges[CUT_LENGTH]; seam_halfedges[0] = mesh.halfedges_begin(); if (seam_halfedges[0] == NULL) return seam; // return empty list int i; for (i=1; i<CUT_LENGTH; i++) { seam_halfedges[i] = seam_halfedges[i-1]->next()->opposite()->next(); if (seam_halfedges[i] == NULL) return seam; // return empty list } // Convert halfedges array to two-ways vertices list for (i=0; i<CUT_LENGTH; i++) seam.push_back(seam_halfedges[i]->vertex()); for (i=CUT_LENGTH-1; i>=0; i--) seam.push_back(seam_halfedges[i]->opposite()->vertex()); } return seam; }
// ---------------------------------------------------------------------------- // main() // ---------------------------------------------------------------------------- int main(int argc, char * argv[]) { std::cerr << "PARAMETERIZATION" << std::endl; std::cerr << " Discrete Authalic Parameterization" << std::endl; std::cerr << " Circular arclength border" << std::endl; std::cerr << " Eigen solver" << std::endl; std::cerr << " Very simple cut if model is not a topological disk" << std::endl; std::cerr << " Output: EPS" << std::endl; //*************************************** // decode parameters //*************************************** if (argc-1 != 2) { std::cerr << "Usage: " << argv[0] << " input_file.off output_file.eps" << std::endl; return(EXIT_FAILURE); } // File names are: const char* input_filename = argv[1]; const char* output_filename = argv[2]; //*************************************** // Read the mesh //*************************************** // Read the mesh std::ifstream stream(input_filename); Polyhedron mesh; stream >> mesh; if(!stream || !mesh.is_valid() || mesh.empty()) { std::cerr << "Error: cannot read OFF file " << input_filename << std::endl; return EXIT_FAILURE; } //*************************************** // Create Polyhedron adaptor //*************************************** Parameterization_polyhedron_adaptor mesh_adaptor(mesh); //*************************************** // Virtually cut mesh //*************************************** // The parameterization methods support only meshes that // are topological disks => we need to compute a "cutting" of the mesh // that makes it homeomorphic to a disk Seam seam = cut_mesh(mesh_adaptor); if (seam.empty()) { std::cerr << "Input mesh not supported: the example cutting algorithm is too simple to cut this shape" << std::endl; return EXIT_FAILURE; } // Create a second adaptor that virtually "cuts" the mesh following the 'seam' path typedef CGAL::Parameterization_mesh_patch_3<Parameterization_polyhedron_adaptor> Mesh_patch_polyhedron; Mesh_patch_polyhedron mesh_patch(mesh_adaptor, seam.begin(), seam.end()); if (!mesh_patch.is_valid()) { std::cerr << "Input mesh not supported: non manifold shape or invalid cutting" << std::endl; return EXIT_FAILURE; } //*************************************** // Discrete Authalic Parameterization (square border) // with Eigen solver //*************************************** // Border parameterizer typedef CGAL::Circular_border_arc_length_parameterizer_3<Mesh_patch_polyhedron> Border_parameterizer; // Discrete Authalic Parameterization (square border) // with Eigen solver typedef CGAL::Discrete_authalic_parameterizer_3<Mesh_patch_polyhedron, Border_parameterizer> Parameterizer; Parameterizer::Error_code err = CGAL::parameterize(mesh_patch, Parameterizer()); switch(err) { case Parameterizer::OK: // Success break; case Parameterizer::ERROR_EMPTY_MESH: // Input mesh not supported case Parameterizer::ERROR_NON_TRIANGULAR_MESH: case Parameterizer::ERROR_NO_TOPOLOGICAL_DISC: case Parameterizer::ERROR_BORDER_TOO_SHORT: std::cerr << "Input mesh not supported: " << Parameterizer::get_error_message(err) << std::endl; return EXIT_FAILURE; break; default: // Error std::cerr << "Error: " << Parameterizer::get_error_message(err) << std::endl; return EXIT_FAILURE; break; }; //*************************************** // Output //*************************************** // Write Postscript file if ( ! write_file_eps(mesh_adaptor, output_filename) ) { std::cerr << "Error: cannot write file " << output_filename << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }
// Cut the mesh to make it homeomorphic to a disk // or extract a region homeomorphic to a disc. // Return the border of this region (empty on error) // // CAUTION: // This method is provided "as is". It is very buggy and simply part of this example. // Developers using this package should implement a more robust cut algorithm! static Seam cut_mesh(Parameterization_polyhedron_adaptor& mesh_adaptor) { // Helper class to compute genus or extract borders typedef CGAL::Parameterization_mesh_feature_extractor<Parameterization_polyhedron_adaptor_ex> Mesh_feature_extractor; typedef Mesh_cutter::Backbone Backbone; Seam seam; // returned list // Get refererence to Polyhedron_3 mesh Polyhedron& mesh = mesh_adaptor.get_adapted_mesh(); // Extract mesh borders and compute genus Mesh_feature_extractor feature_extractor(mesh_adaptor); int nb_borders = feature_extractor.get_nb_borders(); int genus = feature_extractor.get_genus(); // If mesh is a topological disk if (genus == 0 && nb_borders > 0) { // Pick the longest border seam = feature_extractor.get_longest_border(); } else // if mesh is *not* a topological disk, create a virtual cut { Backbone seamingBackbone; // result of cutting Backbone::iterator he; // Compute a cutting path that makes the mesh a "virtual" topological disk mesh.compute_facet_centers(); Mesh_cutter cutter(mesh); if (genus == 0) { // no border, we need to cut the mesh assert (nb_borders == 0); cutter.cut(seamingBackbone); // simple cut } else // genus > 0 -> cut the mesh { cutter.cut_genus(seamingBackbone); } // The Mesh_cutter class is quite buggy // => we check that seamingBackbone is valid // // 1) Check that seamingBackbone is not empty if (seamingBackbone.begin() == seamingBackbone.end()) return seam; // return empty list // // 2) Check that seamingBackbone is a loop and // count occurences of seam halfedges mesh.tag_halfedges(0); // Reset counters for (he = seamingBackbone.begin(); he != seamingBackbone.end(); he++) { // Get next halfedge iterator (looping) Backbone::iterator next_he = he; next_he++; if (next_he == seamingBackbone.end()) next_he = seamingBackbone.begin(); // Check that seamingBackbone is a loop: check that // end of current HE == start of next one if ((*he)->vertex() != (*next_he)->opposite()->vertex()) return seam; // return empty list // Increment counter (in "tag" field) of seam halfedges (*he)->tag( (*he)->tag()+1 ); } // // 3) check that the seamingBackbone is a two-way list for (he = seamingBackbone.begin(); he != seamingBackbone.end(); he++) { // Counter of halfedge and opposite halfedge must be 1 if ((*he)->tag() != 1 || (*he)->opposite()->tag() != 1) return seam; // return empty list } // Convert list of halfedges to a list of vertices for (he = seamingBackbone.begin(); he != seamingBackbone.end(); he++) seam.push_back((*he)->vertex()); } return seam; }
int main() #endif { CGAL::Timer total_timer; total_timer.start(); std::cerr << "PARAMETERIZATION" << std::endl; //*************************************** // Read options on the command line //*************************************** std::string type; // default: Floater param std::string border; // default: circular border param. std::string solver; // default: OpenNL solver std::string input; // required std::string output; // default: out.eps try { #ifdef CGAL_USE_BOOST_PROGRAM_OPTIONS po::options_description desc("Allowed options"); desc.add_options() ("help,h", "prints this help message") ("type,t", po::value<std::string>(&type)->default_value("floater"), "parameterization method: floater, conformal, barycentric, authalic or lscm") ("border,b", po::value<std::string>(&border)->default_value("circle"), "border shape: circle, square or 2pts (lscm only)") ("solver,s", po::value<std::string>(&solver)->default_value("opennl"), "solver: opennl") ("input,i", po::value<std::string>(&input)->default_value(""), "input mesh (OFF)") ("output,o", po::value<std::string>(&output)->default_value("out.eps"), "output file (EPS or OBJ)") ; po::positional_options_description p; p.add("input", 1); p.add("output", 1); po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << "\n"; return 1; } #else std::cerr << "Command-line options require Boost.ProgramOptions" << std::endl; std::cerr << "Use hard-coded options" << std::endl; border = "square"; type = "floater"; solver = "opennl"; input = "data/rotor.off"; output = "rotor_floater_square_opennl_parameterized.obj"; #endif } catch(std::exception& e) { std::cerr << "error: " << e.what() << "\n"; return 1; } catch(...) { std::cerr << "Exception of unknown type!\n"; throw; } //*************************************** // Read the mesh //*************************************** CGAL::Timer task_timer; task_timer.start(); // Read the mesh std::ifstream stream(input.c_str()); Polyhedron mesh; stream >> mesh; if(!stream || !mesh.is_valid() || mesh.empty()) { std::cerr << "Error: cannot read OFF file " << input << std::endl; return EXIT_FAILURE; } std::cerr << "Read file " << input << ": " << task_timer.time() << " seconds " << "(" << mesh.size_of_facets() << " facets, " << mesh.size_of_vertices() << " vertices)" << std::endl; task_timer.reset(); //*************************************** // Create mesh adaptor //*************************************** // The Surface_mesh_parameterization package needs an adaptor to handle Polyhedron_ex meshes Parameterization_polyhedron_adaptor mesh_adaptor(mesh); // The parameterization methods support only meshes that // are topological disks => we need to compute a cutting path // that makes the mesh a "virtual" topological disk // // 1) Cut the mesh Seam seam = cut_mesh(mesh_adaptor); if (seam.empty()) { std::cerr << "Input mesh not supported: the example cutting algorithm is too simple to cut this shape" << std::endl; return EXIT_FAILURE; } // // 2) Create adaptor that virtually "cuts" a patch in a Polyhedron_ex mesh Mesh_patch_polyhedron mesh_patch(mesh_adaptor, seam.begin(), seam.end()); if (!mesh_patch.is_valid()) { std::cerr << "Input mesh not supported: non manifold shape or invalid cutting" << std::endl; return EXIT_FAILURE; } std::cerr << "Mesh cutting: " << task_timer.time() << " seconds." << std::endl; task_timer.reset(); //*************************************** // switch parameterization //*************************************** std::cerr << "Parameterization..." << std::endl; // Defines the error codes typedef CGAL::Parameterizer_traits_3<Mesh_patch_polyhedron> Parameterizer; Parameterizer::Error_code err; if (solver == std::string("opennl")) { err = parameterize<Mesh_patch_polyhedron, OpenNL::DefaultLinearSolverTraits<double>, OpenNL::SymmetricLinearSolverTraits<double> >(mesh_patch, type, border); } else { std::cerr << "Error: invalid solver parameter " << solver << std::endl; err = Parameterizer::ERROR_WRONG_PARAMETER; } // Report errors switch(err) { case Parameterizer::OK: // Success break; case Parameterizer::ERROR_EMPTY_MESH: // Input mesh not supported case Parameterizer::ERROR_NON_TRIANGULAR_MESH: case Parameterizer::ERROR_NO_TOPOLOGICAL_DISC: case Parameterizer::ERROR_BORDER_TOO_SHORT: std::cerr << "Input mesh not supported: " << Parameterizer::get_error_message(err) << std::endl; return EXIT_FAILURE; break; default: // Error std::cerr << "Error: " << Parameterizer::get_error_message(err) << std::endl; return EXIT_FAILURE; break; }; std::cerr << "Parameterization: " << task_timer.time() << " seconds." << std::endl; task_timer.reset(); //*************************************** // Output //*************************************** // get output file's extension std::string extension = output.substr(output.find_last_of('.')); // Save mesh if (extension == ".eps" || extension == ".EPS") { // write Postscript file if ( ! mesh.write_file_eps(output.c_str()) ) { std::cerr << "Error: cannot write file " << output << std::endl; return EXIT_FAILURE; } } else if (extension == ".obj" || extension == ".OBJ") { // write Wavefront obj file if ( ! mesh.write_file_obj(output.c_str()) ) { std::cerr << "Error: cannot write file " << output << std::endl; return EXIT_FAILURE; } } else { std::cerr << "Error: output format not supported" << output << std::endl; err = Parameterizer::ERROR_WRONG_PARAMETER; return EXIT_FAILURE; } std::cerr << "Write file " << output << ": " << task_timer.time() << " seconds " << std::endl; return EXIT_SUCCESS; }
int main(int argc, char * argv[]) { std::cerr << "PARAMETERIZATION" << std::endl; std::cerr << " Floater parameterization" << std::endl; std::cerr << " Circle border" << std::endl; std::cerr << " OpenNL solver" << std::endl; std::cerr << " Very simple cut if model is not a topological disk" << std::endl; //*************************************** // decode parameters //*************************************** if (argc-1 != 1) { std::cerr << "Usage: " << argv[0] << " input_file.off" << std::endl; return(EXIT_FAILURE); } // File name is: const char* input_filename = argv[1]; //*************************************** // Read the mesh //*************************************** // Read the mesh std::ifstream stream(input_filename); Polyhedron mesh; stream >> mesh; if(!stream || !mesh.is_valid() || mesh.empty()) { std::cerr << "Error: cannot read OFF file " << input_filename << std::endl; return EXIT_FAILURE; } //*************************************** // Create Polyhedron adaptor //*************************************** Parameterization_polyhedron_adaptor mesh_adaptor(mesh); //*************************************** // Virtually cut mesh //*************************************** // The parameterization methods support only meshes that // are topological disks => we need to compute a "cutting" of the mesh // that makes it homeomorphic to a disk Seam seam = cut_mesh(mesh_adaptor); if (seam.empty()) { std::cerr << "Input mesh not supported: the example cutting algorithm is too simple to cut this shape" << std::endl; return EXIT_FAILURE; } // Create a second adaptor that virtually "cuts" the mesh following the 'seam' path typedef CGAL::Parameterization_mesh_patch_3<Parameterization_polyhedron_adaptor> Mesh_patch_polyhedron; Mesh_patch_polyhedron mesh_patch(mesh_adaptor, seam.begin(), seam.end()); if (!mesh_patch.is_valid()) { std::cerr << "Input mesh not supported: non manifold shape or invalid cutting" << std::endl; return EXIT_FAILURE; } //*************************************** // Floater Mean Value Coordinates parameterization //*************************************** typedef CGAL::Parameterizer_traits_3<Mesh_patch_polyhedron> Parameterizer; // Type that defines the error codes Parameterizer::Error_code err = CGAL::parameterize(mesh_patch); switch(err) { case Parameterizer::OK: // Success break; case Parameterizer::ERROR_EMPTY_MESH: // Input mesh not supported case Parameterizer::ERROR_NON_TRIANGULAR_MESH: case Parameterizer::ERROR_NO_TOPOLOGICAL_DISC: case Parameterizer::ERROR_BORDER_TOO_SHORT: std::cerr << "Input mesh not supported: " << Parameterizer::get_error_message(err) << std::endl; return EXIT_FAILURE; break; default: // Error std::cerr << "Error: " << Parameterizer::get_error_message(err) << std::endl; return EXIT_FAILURE; break; }; //*************************************** // Output //*************************************** // Raw output: dump (u,v) pairs Polyhedron::Vertex_const_iterator pVertex; for (pVertex = mesh.vertices_begin(); pVertex != mesh.vertices_end(); pVertex++) { // (u,v) pair is stored in any halfedge double u = mesh_adaptor.info(pVertex->halfedge())->uv().x(); double v = mesh_adaptor.info(pVertex->halfedge())->uv().y(); std::cout << "(u,v) = (" << u << "," << v << ")" << std::endl; } return EXIT_SUCCESS; }
int Stitcher::ZipSeam(Seam & seam) { Mesh * M = seam.M; Vector<int>::iterator bdryA, bdryB; // Important Step ! seam.LineupSeams(bdryA, bdryB); int i, i2, j, j2; Vec pi, pi2, pj, pj2; double len1, len2; int fIndex = M->numberOfFaces(); bool a_done = false, b_done = false; int starti = *bdryA; // DEBUG: Vector<Vec> red, blue; //StdSet<Face*> green, yellow; //foreach(int vi, seam.boundryA) blue.push_back(*M->v(vi)); //foreach(int vi, seam.boundryB) red.push_back(*M->v(vi)); M->redPoints.push_back(red); M->bluePoints.push_back(blue); while (!a_done && !b_done) { bool advanceA = false; if(bdryA + 1 == seam.boundryA.end()) a_done = true; if(bdryB + 1 == seam.boundryB.end()) b_done = true; if(!a_done && !b_done) { i = *bdryA; i2 = *(bdryA+1); // i + 1 j = *bdryB; j2 = *(bdryB+1); // j + 1 pi = M->vec(i); pi2 = M->vec(i2); pj = M->vec(j); pj2 = M->vec(j2); len1 = (pi - pi2).norm() + (pj - pi2).norm(); len2 = (pi - pj2).norm() + (pj - pj2).norm(); double minAngleA = Vertex::minAngle(pi, pi2, pj); double minAngleB = Vertex::minAngle(pi, pj2, pj); // Length criterion if(len1 < len2) advanceA = true; if(advanceA) { if(minAngleA < theta && minAngleB > minAngleA && minAngleB > theta) advanceA = false; } else { if(minAngleB < theta && minAngleA > minAngleB && minAngleA > theta) advanceA = true; } // Avoid almost and self-intersects if(advanceA) { // Create plane from future face, test if other points are projected on it Plane p(pi, pi2, pj); if(p.IsInTri(pj2, pi, pi2, pj)) {advanceA = false;} } else { Plane p(pi, pj2, pj); if(p.IsInTri(pi2, pi, pj2, pj)) {advanceA = true;} } if(advanceA) { M->addFace(i2, j, i, fIndex++, true); bdryA++; } else { M->addFace(j2, j, i, fIndex++, true); bdryB++; //green.insert(&M->face.back()); } addedFaces.push_back(&(M->facesList()->back())); // Debug: //testPoints2.push_back(addedFaces.back()->center()); } } //M->greenFaces.push_back(green); // Fill last hole fillSmallHole(M, starti); // Smooth vertices with large angles > 150 FairSeams(); return 1; }