1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
#include <CGAL/basic.h> // include basic.h before testing #defines
#include <CGAL/Cartesian.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/Parameterization_polyhedron_adaptor_3.h>
#include <CGAL/parameterize.h>
#include <CGAL/Discrete_authalic_parameterizer_3.h>
#include <CGAL/Square_border_parameterizer_3.h>
#include <CGAL/Parameterization_mesh_patch_3.h>
#include <CGAL/Taucs_solver_traits.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
// ----------------------------------------------------------------------------
// Private types
// ----------------------------------------------------------------------------
typedef CGAL::Cartesian<double> Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
// Polyhedron adaptor
typedef CGAL::Parameterization_polyhedron_adaptor_3<Polyhedron>
Parameterization_polyhedron_adaptor;
// Type describing a border or seam as a vertex list
typedef std::list<Parameterization_polyhedron_adaptor::Vertex_handle>
Seam;
// ----------------------------------------------------------------------------
// Private functions
// ----------------------------------------------------------------------------
// 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;
}
// Dump parameterized mesh to an eps file
static bool write_file_eps(const Parameterization_polyhedron_adaptor& mesh_adaptor,
const char *pFilename,
double scale = 500.0)
{
const Polyhedron& mesh = mesh_adaptor.get_adapted_mesh();
std::ofstream out(pFilename);
if(!out)
return false;
CGAL::set_ascii_mode(out);
// compute bounding box
double xmin,xmax,ymin,ymax;
xmin = ymin = xmax = ymax = 0;
Polyhedron::Halfedge_const_iterator pHalfedge;
for (pHalfedge = mesh.halfedges_begin();
pHalfedge != mesh.halfedges_end();
pHalfedge++)
{
double x1 = scale * mesh_adaptor.info(pHalfedge->prev())->uv().x();
double y1 = scale * mesh_adaptor.info(pHalfedge->prev())->uv().y();
double x2 = scale * mesh_adaptor.info(pHalfedge)->uv().x();
double y2 = scale * mesh_adaptor.info(pHalfedge)->uv().y();
xmin = (std::min)(xmin,x1);
xmin = (std::min)(xmin,x2);
xmax = (std::max)(xmax,x1);
xmax = (std::max)(xmax,x2);
ymax = (std::max)(ymax,y1);
ymax = (std::max)(ymax,y2);
ymin = (std::min)(ymin,y1);
ymin = (std::min)(ymin,y2);
}
out << "%!PS-Adobe-2.0 EPSF-2.0" << std::endl;
out << "%%BoundingBox: " << int(xmin+0.5) << " "
<< int(ymin+0.5) << " "
<< int(xmax+0.5) << " "
<< int(ymax+0.5) << std::endl;
out << "%%HiResBoundingBox: " << xmin << " "
<< ymin << " "
<< xmax << " "
<< ymax << std::endl;
out << "%%EndComments" << std::endl;
out << "gsave" << std::endl;
out << "0.1 setlinewidth" << std::endl;
// color macros
out << std::endl;
out << "% RGB color command - r g b C" << std::endl;
out << "/C { setrgbcolor } bind def" << std::endl;
out << "/white { 1 1 1 C } bind def" << std::endl;
out << "/black { 0 0 0 C } bind def" << std::endl;
// edge macro -> E
out << std::endl;
out << "% Black stroke - x1 y1 x2 y2 E" << std::endl;
out << "/E {moveto lineto stroke} bind def" << std::endl;
out << "black" << std::endl << std::endl;
// for each halfedge
for (pHalfedge = mesh.halfedges_begin();
pHalfedge != mesh.halfedges_end();
pHalfedge++)
{
double x1 = scale * mesh_adaptor.info(pHalfedge->prev())->uv().x();
double y1 = scale * mesh_adaptor.info(pHalfedge->prev())->uv().y();
double x2 = scale * mesh_adaptor.info(pHalfedge)->uv().x();
double y2 = scale * mesh_adaptor.info(pHalfedge)->uv().y();
out << x1 << " " << y1 << " " << x2 << " " << y2 << " E" << std::endl;
}
/* Emit EPS trailer. */
out << "grestore" << std::endl;
out << std::endl;
out << "showpage" << std::endl;
return true;
}
// ----------------------------------------------------------------------------
// main()
// ----------------------------------------------------------------------------
int main(int argc, char * argv[])
{
std::cerr << "PARAMETERIZATION" << std::endl;
std::cerr << " Discrete Authalic Parameterization" << std::endl;
std::cerr << " Square border" << std::endl;
std::cerr << " TAUCS 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 TAUCS solver
//***************************************
// Border parameterizer
typedef CGAL::Square_border_arc_length_parameterizer_3<Mesh_patch_polyhedron>
Border_parameterizer;
// TAUCS solver
typedef CGAL::Taucs_solver_traits<double> Solver;
// Discrete Authalic Parameterization (square border)
// with TAUCS solver
typedef CGAL::Discrete_authalic_parameterizer_3<Mesh_patch_polyhedron,
Border_parameterizer,
Solver> 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;
}
|