#include <CGAL/Timer.h>
#include <CGAL/parameterize.h>
#include <CGAL/Parameterization_mesh_patch_3.h>
#include <CGAL/Circular_border_parameterizer_3.h>
#include <CGAL/Square_border_parameterizer_3.h>
#include <CGAL/Two_vertices_parameterizer_3.h>
#include <CGAL/Barycentric_mapping_parameterizer_3.h>
#include <CGAL/Discrete_conformal_map_parameterizer_3.h>
#include <CGAL/Discrete_authalic_parameterizer_3.h>
#include <CGAL/Mean_value_coordinates_parameterizer_3.h>
#include <CGAL/LSCM_parameterizer_3.h>
#include <CGAL/Parameterization_mesh_feature_extractor.h>
#include <CGAL/OpenNL/linear_solver.h>
#include "Polyhedron_ex.h"
#include "Mesh_cutter.h"
#include "Parameterization_polyhedron_adaptor_ex.h"
#include <iostream>
#include <string.h>
#include <ctype.h>
#include <fstream>
#include <cassert>
#ifdef CGAL_USE_BOOST_PROGRAM_OPTIONS
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#endif
typedef Polyhedron_ex Polyhedron;
typedef Parameterization_polyhedron_adaptor_ex Parameterization_polyhedron_adaptor;
Mesh_patch_polyhedron;
typedef std::list<Parameterization_polyhedron_adaptor::Vertex_handle>
Seam;
static Seam cut_mesh(Parameterization_polyhedron_adaptor& mesh_adaptor)
{
Mesh_feature_extractor;
typedef Mesh_cutter::Backbone Backbone;
Seam seam;
Polyhedron& mesh = mesh_adaptor.get_adapted_mesh();
Mesh_feature_extractor feature_extractor(mesh_adaptor);
int nb_borders = feature_extractor.get_nb_borders();
int genus = feature_extractor.get_genus();
if (genus == 0 && nb_borders > 0)
{
seam = feature_extractor.get_longest_border();
}
else
{
Backbone seamingBackbone;
Backbone::iterator he;
mesh.compute_facet_centers();
Mesh_cutter cutter(mesh);
if (genus == 0)
{
assert (nb_borders == 0);
cutter.cut(seamingBackbone);
}
else
{
cutter.cut_genus(seamingBackbone);
}
if (seamingBackbone.begin() == seamingBackbone.end())
return seam;
mesh.tag_halfedges(0);
for (he = seamingBackbone.begin(); he != seamingBackbone.end(); he++)
{
Backbone::iterator next_he = he;
next_he++;
if (next_he == seamingBackbone.end())
next_he = seamingBackbone.begin();
if ((*he)->vertex() != (*next_he)->opposite()->vertex())
return seam;
(*he)->tag( (*he)->tag()+1 );
}
for (he = seamingBackbone.begin(); he != seamingBackbone.end(); he++)
{
if ((*he)->tag() != 1 || (*he)->opposite()->tag() != 1)
return seam;
}
for (he = seamingBackbone.begin(); he != seamingBackbone.end(); he++)
seam.push_back((*he)->vertex());
}
return seam;
}
template<
class GeneralSparseLinearAlgebraTraits_d,
class SymmetricSparseLinearAlgebraTraits_d
>
const std::string& type,
const std::string& border)
{
if ( (type == std::string("floater")) && (border == std::string("circle")) )
{
mesh,
GeneralSparseLinearAlgebraTraits_d
>());
}
else if ( (type == std::string("floater")) && (border == std::string("square")) )
{
mesh,
GeneralSparseLinearAlgebraTraits_d
>());
}
else if ( (type == std::string("barycentric")) && (border == std::string("circle")) )
{
mesh,
GeneralSparseLinearAlgebraTraits_d
>());
}
else if ( (type == std::string("barycentric")) && (border == std::string("square")) )
{
mesh,
GeneralSparseLinearAlgebraTraits_d
>());
}
else if ( (type == std::string("conformal")) && (border == std::string("circle")) )
{
mesh,
GeneralSparseLinearAlgebraTraits_d
>());
}
else if ( (type == std::string("conformal")) && (border == std::string("square")) )
{
mesh,
GeneralSparseLinearAlgebraTraits_d
>());
}
else if ( (type == std::string("authalic")) && (border == std::string("circle")) )
{
mesh,
GeneralSparseLinearAlgebraTraits_d
>());
}
else if ( (type == std::string("authalic")) && (border == std::string("square")) )
{
mesh,
GeneralSparseLinearAlgebraTraits_d
>());
}
else if ( (type == std::string("lscm")) && (border == std::string("2pts")) )
{
mesh,
SymmetricSparseLinearAlgebraTraits_d
>());
}
else
{
std::cerr << "Error: invalid parameters combination " << type << " + " << border << std::endl;
}
return err;
}
#ifdef CGAL_USE_BOOST_PROGRAM_OPTIONS
int main(int argc, char * argv[])
#else
int main()
#endif
{
CGAL::Timer total_timer;
total_timer.start();
std::cerr << "PARAMETERIZATION" << std::endl;
std::string type;
std::string border;
std::string solver;
std::string input;
std::string output;
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;
}
CGAL::Timer task_timer;
task_timer.start();
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();
Parameterization_polyhedron_adaptor mesh_adaptor(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;
}
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();
std::cerr << "Parameterization..." << std::endl;
Parameterizer::Error_code err;
if (solver == std::string("opennl"))
{
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;
}
switch(err) {
case Parameterizer::OK:
break;
case Parameterizer::ERROR_EMPTY_MESH:
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:
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();
std::string extension = output.substr(output.find_last_of('.'));
if (extension == ".eps" || extension == ".EPS")
{
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")
{
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;
}