49 #include <OpenMesh/Core/IO/MeshIO.hh> 50 #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh> 53 #include <OpenMesh/Tools/Utils/getopt.h> 69 void usage_and_exit(
int xcode)
74 cout <<
"\nUsage: mconvert [option] <input> [<output>]\n\n";
75 cout <<
" Convert from one 3D geometry format to another.\n" 76 <<
" Or simply display some information about the object\n" 77 <<
" stored in <input>.\n" 81 cout <<
" -b\tUse binary mode if supported by target format.\n" << endl;
82 cout <<
" -l\tStore least significant bit first (LSB, little endian).\n" << endl;
83 cout <<
" -m\tStore most significant bit first (MSB, big endian).\n" << endl;
84 cout <<
" -s\tSwap byte order.\n" << endl;
85 cout <<
" -B\tUse binary mode if supported by source format.\n" << endl;
86 cout <<
" -S\tSwap byte order of input data.\n" << endl;
87 cout <<
" -c\tCopy vertex color if provided by input.\n" << endl;
88 cout <<
" -d\tCopy face color if provided by input.\n" << endl;
89 cout <<
" -C\tTranslate object in its center-of-gravity.\n" << endl;
90 cout <<
" -n\tCopy vertex normals if provided by input. Else compute normals.\n" << endl;
91 cout <<
" -N\tReverse normal directions.\n" << endl;
92 cout <<
" -t\tCopy vertex texture coordinates if provided by input file.\n" << endl;
93 cout <<
" -T \"x y z\"\tTranslate object by vector (x, y, z)'\"\n" 102 template <
typename T>
struct Option : std::pair< T, bool >
104 typedef std::pair< T, bool > Base;
107 { Base::second =
false; }
109 bool is_valid()
const {
return Base::second; }
110 bool is_empty()
const {
return !Base::second; }
112 operator T& () {
return Base::first; }
113 operator const T& ()
const {
return Base::first; }
115 Option& operator = (
const T& _rhs )
116 { Base::first = _rhs; Base::second=
true;
return *
this; }
118 bool operator == (
const T& _rhs )
const 119 {
return Base::first == _rhs; }
121 bool operator != (
const T& _rhs )
const 122 {
return Base::first != _rhs; }
125 template <
typename T>
126 std::ostream& operator << (std::ostream& _os, const Option<T>& _opt )
128 if (_opt.second) _os << _opt.first;
else _os <<
"<null>";
132 template <
typename T>
133 std::istream& operator >> (std::istream& _is,
Option<T>& _opt )
135 _is >> _opt.first; _opt.second =
true;
141 int main(
int argc,
char *argv[] )
146 std::string ifname, ofname;
147 bool rev_normals =
false;
148 bool obj_center =
false;
153 while ( (c=getopt(argc, argv,
"bBcdCi:hlmnNo:sStT:"))!=-1 )
164 case 'N': rev_normals =
true;
break;
165 case 'C': obj_center =
true;
break;
171 std::cout << optarg << std::endl;
172 std::stringstream str; str << optarg;
174 std::cout << tvec << std::endl;
177 case 'i': ifname = optarg;
break;
178 case 'o': ofname = optarg;
break;
191 ifname = argv[optind++];
201 std::cout <<
"reading.." << std::endl;
208 std::cout <<
" read in " << timer.
as_string() << std::endl;
211 std::cout <<
" read failed\n" << std::endl;
220 ?
" source is binary\n" 221 :
" source is ascii\n");
223 std::cout <<
" #V " << mesh.n_vertices() << std::endl;
224 std::cout <<
" #E " << mesh.n_edges() << std::endl;
225 std::cout <<
" #F " << mesh.n_faces() << std::endl;
227 if (ropt.vertex_has_texcoord())
228 std::cout <<
" has texture coordinates" << std::endl;
230 if (ropt.vertex_has_normal())
231 std::cout <<
" has vertex normals" << std::endl;
233 if (ropt.vertex_has_color())
234 std::cout <<
" has vertex colors" << std::endl;
236 if (ropt.face_has_normal())
237 std::cout <<
" has face normals" << std::endl;
239 if (ropt.face_has_color())
240 std::cout <<
" has face colors" << std::endl;
246 ofname = argv[optind++];
254 if ( opt.vertex_has_normal() && !ropt.vertex_has_normal())
256 std::cout <<
"compute normals" << std::endl;
261 std::cout <<
" " << mesh.n_faces()
262 <<
" face normals in " << timer.
as_string() << std::endl;
268 std::cout <<
" " << mesh.n_vertices()
269 <<
" vertex normals in " << timer.
as_string() << std::endl;
275 if ( rev_normals && ropt.vertex_has_normal() )
277 std::cout <<
"reverse normal directions" << std::endl;
279 MyMesh::VertexIter vit = mesh.vertices_begin();
280 for (; vit != mesh.vertices_end(); ++vit)
281 mesh.set_normal( *vit, -mesh.normal( *vit ) );
283 std::cout <<
" " << mesh.n_vertices()
284 <<
" vertex normals in " << timer.
as_string() << std::endl;
295 std::cout <<
"center object" << std::endl;
297 MyMesh::VertexIter vit = mesh.vertices_begin();
298 for (; vit != mesh.vertices_end(); ++vit)
299 cog += mesh.point( *vit );
301 nv = mesh.n_vertices();
302 cog *= 1.0f/mesh.n_vertices();
303 std::cout <<
" cog = [" << cog <<
"]'" << std::endl;
306 vit = mesh.vertices_begin();
308 for (; vit != mesh.vertices_end(); ++vit)
309 mesh.set_point( *vit , mesh.point( *vit )-cog );
311 nv += mesh.n_vertices();
314 std::cout <<
" already centered!" << std::endl;
315 std::cout <<
" visited " << nv
316 <<
" vertices in " << timer.
as_string() << std::endl;
322 if ( tvec.is_valid() )
324 std::cout <<
"Translate object by " << tvec << std::endl;
327 MyMesh::VertexIter vit = mesh.vertices_begin();
328 for (; vit != mesh.vertices_end(); ++vit)
329 mesh.set_point( *vit , mesh.point( *vit ) + tvec.first );
331 std::cout <<
" moved " << mesh.n_vertices()
332 <<
" vertices in " << timer.
as_string() << std::endl;
339 std::cout <<
"Color vertices" << std::endl;
341 double d = 256.0/double(mesh.n_vertices());
343 double r = 0.0, g = 0.0, b = 255.0;
345 MyMesh::VertexIter vit = mesh.vertices_begin();
346 for (; vit != mesh.vertices_end(); ++vit)
348 mesh.set_color( *vit ,
MyMesh::Color( std::min((
int)(r+0.5),255),
349 std::min((
int)(g+0.5),255),
350 std::max((
int)(b+0.5),0) ) );
356 std::cout <<
" colored " << mesh.n_vertices()
357 <<
" vertices in " << timer.
as_string() << std::endl;
364 std::cout <<
"Color faces" << std::endl;
366 double d = 256.0/double(mesh.n_faces());
368 double r = 0.0, g = 50.0, b = 255.0;
370 MyMesh::FaceIter it = mesh.faces_begin();
371 for (; it != mesh.faces_end(); ++it)
373 mesh.set_color( *it ,
MyMesh::Color( std::min((
int)(r+0.5),255),
374 std::min((
int)(g+0.5),255),
375 std::max((
int)(b+0.5),0) ) );
381 std::cout <<
" colored " << mesh.n_faces()
382 <<
" faces in " << timer.
as_string() << std::endl;
387 std::cout <<
"writing.." << std::endl;
396 std::cerr <<
" error writing mesh!" << std::endl;
407 if ( opt.vertex_has_normal() )
408 std::cout <<
" with vertex normals" << std::endl;
409 if ( opt.vertex_has_color() )
410 std::cout <<
" with vertex colors" << std::endl;
411 if ( opt.vertex_has_texcoord() )
412 std::cout <<
" with vertex texcoord" << std::endl;
413 if ( opt.face_has_normal() )
414 std::cout <<
" with face normals" << std::endl;
415 if ( opt.face_has_color() )
416 std::cout <<
" with face colors" << std::endl;
417 std::cout <<
" wrote in " << timer.
as_string() << std::endl;
Swap byte order in binary mode.
void reset(void)
Reset the timer.
Assume big endian byte ordering.
size_t binary_size(const Mesh &_mesh, const std::string &_ext, Options _opt=Options::Default)
Get binary size of data.
Kernel::Color Color
Color type.
Add storage for previous halfedge (halfedges). The bit is set by default in the DefaultTraits.
Has (r) / store (w) vertex colors.
Has (r) / store (w) face colors.
bool write_mesh(const Mesh &_mesh, const std::string &_filename, Options _opt=Options::Default, std::streamsize _precision=6)
Write a mesh to the file _filename.
std::string as_string(Format format=Automatic)
Add 2D texture coordinates (vertices, halfedges)
Add normals to mesh item (vertices/faces)
void stop(void)
Stop measurement.
Add colors to mesh item (vertices/faces/edges)
void cont(void)
Continue measurement.
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
void update_vertex_normals()
Update normal vectors for all vertices.
Set options for reader/writer modules.
void update_face_normals()
Update normal vectors for all faces.
Has (r) / store (w) vertex normals.
decltype(std::declval< S >() *std::declval< S >()) sqrnorm() const
compute squared euclidean norm
void start(void)
Start measurement.
Assume little endian byte ordering.
Has (r) / store (w) texture coordinates.