vcglib/apps/trimeshinfo/trimeshinfo.cpp

541 lines
14 KiB
C++
Raw Normal View History

/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2005 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
2005-12-16 12:16:36 +01:00
Revision 1.20 2005/12/15 11:20:00 corsini
Add vertex-face topology
2005-12-15 12:20:00 +01:00
Revision 1.19 2005/12/14 14:05:37 corsini
Adjust comments
2005-12-14 15:05:37 +01:00
Revision 1.18 2005/12/14 12:15:37 corsini
Re-add clean mesh saving feature
2005-12-14 13:15:37 +01:00
Revision 1.17 2005/12/13 15:46:30 corsini
Restructuring code
2005-12-13 16:46:30 +01:00
Revision 1.16 2005/12/12 12:09:08 cignoni
Changed names of clean function and tested inertia.h
Revision 1.15 2005/12/12 11:29:21 corsini
Minor changes
2005-12-12 12:29:21 +01:00
Revision 1.14 2005/12/12 10:48:16 corsini
Fix indentation
2005-12-12 11:48:16 +01:00
Revision 1.13 2005/11/17 00:42:03 cignoni
Changed include order
removed clean::initialize
Revision 1.12 2005/11/16 16:45:51 rita_borgo
Minor changes
2005-11-16 17:45:51 +01:00
Revision 1.11 2005/11/16 15:59:46 cignoni
Changed name of the component from <Flag> to <BitFlags>
Revision 1.10 2005/11/14 09:21:07 cignoni
Heavily restructured the code of Trimeshinfo.
Now divided the collecting part from the reporting one (xml and ascii)
Revision 1.9 2005/11/04 15:37:57 rita_borgo
Removed Debug option
2005-11-04 16:37:58 +01:00
Revision 1.8 2005/10/11 16:03:40 rita_borgo
Moved all the main functions inside clean.h
Revision 1.7 2005/09/30 15:48:46 rita_borgo
Fixed manifold Test
2005-09-30 17:48:46 +02:00
Revision 1.6 2005/09/30 14:13:01 rita_borgo
Problem: Text not aligned
2005-09-30 16:13:01 +02:00
Revision 1.5 2005/09/30 13:29:40 rita_borgo
Fixed Manifold Test
2005-09-30 15:29:40 +02:00
Revision 1.4 2005/09/29 14:48:15 rita_borgo
Fixed code related to creation of the XML file
Revision 1.3 2005/09/28 13:57:09 rita_borgo
Fixed some printout not alligned
2005-09-28 15:57:09 +02:00
Revision 1.2 2005/09/28 10:46:04 rita_borgo
Added possibility of saving File in OFF format
Revision 1.1 2005/09/20 10:15:27 rita_borgo
Changed file name to uniform with other solution projects,
before was main.cpp
Revision 1.8 2005/02/15 12:26:06 rita_borgo
Minor changes to self-intersection
Revision 1.7 2005/02/07 15:44:31 rita_borgo
Fixed Color and Volume
Revision 1.6 2005/02/01 17:37:53 rita_borgo
Fixed Volume and Color
Revision 1.5 2005/01/18 16:33:12 rita_borgo
Added OFF file Option
Revision 1.4 2005/01/17 18:19:00 rita_borgo
Added new routines.
Self-intersection first release
Revision 1.2 2005/01/03 16:13:09 rita_borgo
Added Standard comments
****************************************************************************/
2005-12-13 16:46:30 +01:00
// Standard headers
#include <fstream>
#include <vector>
2005-09-30 17:48:46 +02:00
#include <string>
#include <stack>
2005-12-12 11:48:16 +01:00
using namespace std;
2005-11-16 17:45:51 +01:00
2005-12-13 16:46:30 +01:00
// VCG headers
2005-12-12 11:48:16 +01:00
#include <vcg/complex/trimesh/base.h>
#include <vcg/complex/trimesh/update/topology.h>
#include <vcg/complex/trimesh/update/edges.h>
#include <vcg/complex/trimesh/update/bounding.h>
#include <vcg/complex/trimesh/update/flag.h>
#include <vcg/complex/trimesh/clean.h>
#include <vcg/space/intersection/triangle_triangle3.h>
#include <vcg/math/histogram.h>
#include <wrap/io_trimesh/import.h>
#include <wrap/io_trimesh/export.h>
2005-12-12 11:48:16 +01:00
#include <vcg/simplex/vertexplus/base.h>
#include <vcg/simplex/vertexplus/component.h>
2005-12-12 11:48:16 +01:00
#include <vcg/simplex/faceplus/base.h>
#include <vcg/simplex/faceplus/component.h>
#include <vcg/simplex/face/pos.h>
#include <vcg/complex/trimesh/inertia.h>
#include "XMLTree.h"
#include <vcg/space/index/grid_static_ptr.h>
#include "defs.h"
2005-12-12 11:48:16 +01:00
using namespace vcg;
2005-12-12 11:48:16 +01:00
class CFace;
class CEdge;
2005-12-15 12:20:00 +01:00
class CVertex : public VertexSimp2< CVertex, CEdge, CFace, vert::VFAdj, vert::Coord3f,
vert::BitFlags, vert::Normal3f > {};
class CFace : public FaceSimp2< CVertex, CEdge, CFace, face::FFAdj, face::VFAdj,
face::VertexRef, face::Normal3f, face::BitFlags, face::Mark > {};
2005-12-12 11:48:16 +01:00
class CMesh : public vcg::tri::TriMesh< vector<CVertex>, vector<CFace> > {};
2005-12-12 11:48:16 +01:00
typedef CMesh::VertexPointer VertexPointer;
typedef CMesh::VertexIterator VertexIterator;
typedef Point3<CMesh::ScalarType> Point3x;
typedef vector<Point3x> Hole;
2005-12-12 11:48:16 +01:00
typedef CMesh::VertexPointer VertexPointer;
typedef CMesh::VertexIterator VertexIterator;
typedef CMesh::FaceContainer FaceContainer;
typedef CMesh::ScalarType ScalarType;
struct MeshInfo
{
2005-12-12 11:48:16 +01:00
string FileName;
int vn,fn;
bool Manifold;
int count_e,boundary_e,count_fd,count_uv,numholes;
int BEdges;
float Volume;
int numcomponents,Genus;
bool Regular,Semiregular;
bool Orientable,Oriented;
int dv;
bool SelfIntersect;
};
2005-12-13 16:46:30 +01:00
void OpenMesh(const char *filename, CMesh &m)
{
printf(" Mesh loading...");
int err = tri::io::Importer<CMesh>::Open(m,filename);
if (err)
{
printf("\n Error during loading %s: '%s'\n",filename,
tri::io::Importer<CMesh>::ErrorMsg(err));
exit(-1);
}
else
printf(" done.\n\n");
}
2005-11-16 17:45:51 +01:00
void initMeshInfo(MeshInfo &mi)
{
2005-12-12 12:29:21 +01:00
memset(&mi, 0, sizeof(mi));
2005-11-16 17:45:51 +01:00
}
2005-12-12 12:29:21 +01:00
void PrintMeshInfo(MeshInfo &mi)
{
2005-12-13 16:46:30 +01:00
printf(" *** Mesh information ***\n\n");
printf(" Mesh: '%s' \n", mi.FileName.c_str());
printf(" Number of vertices: %d \n", mi.vn);
printf(" Number of faces: %d \n", mi.fn);
printf(" Number of edges: %d \n", mi.count_e);
printf(" Number of internal edges: %d \n", mi.count_e-mi.boundary_e);
printf(" Number of boundary edges: %i \n", mi.boundary_e);
printf(" Number of degenerated faces: %d\n", mi.count_fd);
printf(" Number of unreferenced vertices: %d\n",mi.count_uv);
printf(" Number of holes/boundaries: %d \n", mi.numholes);
if ((mi.Manifold)&&(mi.Oriented)&&(!mi.numholes))
printf(" Volume: %f \n", mi.Volume);
else
2005-12-14 15:05:37 +01:00
printf(" Volume: UNDEFINED (a closed oriented manifold is required)\n");
2005-12-13 16:46:30 +01:00
printf(" Number of connected components: %d\n", mi.numcomponents);
2005-12-13 16:46:30 +01:00
// Orientation
if (!mi.Manifold)
{
printf(" Orientable Mesh: NO\n");
printf(" Oriented Mesh: NO\n");
}
else
{
if (mi.Orientable)
printf(" Orientable Mesh: YES\n");
else
printf(" Orientable Mesh: NO\n");
if (mi.Oriented)
printf(" Oriented Mesh: YES\n");
else
printf(" Oriented Mesh: NO\n");
}
// Manifold
2005-12-12 12:29:21 +01:00
if (!mi.Manifold)
2005-12-13 16:46:30 +01:00
printf(" Manifold: NO\n");
2005-12-12 11:48:16 +01:00
else
2005-12-13 16:46:30 +01:00
printf(" Manifold: YES\n");
2005-12-12 11:48:16 +01:00
2005-12-13 16:46:30 +01:00
// Genus
2005-12-12 11:48:16 +01:00
if (mi.Manifold)
2005-12-13 16:46:30 +01:00
printf(" Genus: %d \n", mi.Genus);
2005-12-12 11:48:16 +01:00
else
2005-12-16 12:16:36 +01:00
printf(" Genus: N/A \n");
2005-12-12 11:48:16 +01:00
2005-12-13 16:46:30 +01:00
// Mesh Type
2005-12-12 11:48:16 +01:00
if (mi.Regular)
2005-12-13 16:46:30 +01:00
printf(" Mesh Type: REGULAR\n");
2005-12-12 11:48:16 +01:00
else if (mi.Semiregular)
2005-12-13 16:46:30 +01:00
printf(" Mesh Type: SEMIREGULAR\n");
2005-12-12 11:48:16 +01:00
else
2005-12-13 16:46:30 +01:00
printf(" Mesh Type: IRREGULAR\n");
2005-12-12 11:48:16 +01:00
2005-12-13 16:46:30 +01:00
// Further details
printf(" Number of duplicated vertices found: %d\n", mi.dv);
printf(" Self Intersection: %s\n", mi.SelfIntersect?"Yes":"No");
}
2005-12-12 12:29:21 +01:00
void SaveXMLInfo(MeshInfo &mi)
{
XMLTree doc;
2005-12-12 11:48:16 +01:00
doc.initializeMain();
char s[256];
sprintf(s,"%d",mi.vn);
2005-12-12 12:29:21 +01:00
doc.addNode(s, VALUE_INTEGER, "Number of Vertices");
2005-12-12 11:48:16 +01:00
sprintf(s,"%d",mi.fn);
2005-12-12 12:29:21 +01:00
doc.addNode(s, VALUE_INTEGER, "Number of Faces");
2005-12-12 11:48:16 +01:00
if(mi.Manifold)
doc.addNode("No", VALUE_BOOL,"Manifold");
else
doc.addNode("Yes", VALUE_BOOL,"Manifold");
sprintf(s,"%d",mi.count_e);
doc.addNode(s, VALUE_INTEGER,"Number of Edges");
sprintf(s,"%d",mi.count_fd);
doc.addNode(s, VALUE_INTEGER,"Number of Degenerated Faces");
sprintf(s,"%d",mi.count_uv);
doc.addNode(s, VALUE_INTEGER,"Number of unreferenced vertices");
sprintf(s,"%d",mi.numholes);
doc.addNode(s, VALUE_INTEGER,"Number of Holes");
sprintf(s,"%d",mi.BEdges);
doc.addNode(s, VALUE_INTEGER,"Number of Border Edges");
sprintf(s,"%f",mi.Volume);
doc.addNode(s, VALUE_FLOAT,"Volume");
sprintf(s,"%d",mi.numcomponents);
doc.addNode(s, VALUE_INTEGER,"Number of Connected Components");
2005-12-12 12:29:21 +01:00
sprintf(s,"%d",mi.Genus);
doc.addNode(s, VALUE_INTEGER,"Genus");
2005-12-12 11:48:16 +01:00
if (mi.Regular)
doc.addNode("REGULAR", VALUE_STRING,"Type of Mesh");
else if (mi.Semiregular)
doc.addNode("SEMIREGULAR", VALUE_STRING,"Type of Mesh");
else
2005-12-12 12:29:21 +01:00
doc.addNode("IRREGULAR", VALUE_STRING,"Type of Mesh");
2005-09-30 17:48:46 +02:00
2005-12-12 11:48:16 +01:00
if (!mi.Manifold)
{
doc.addNode("NO", VALUE_STRING,"Orientable Mesh");
doc.addNode("NO", VALUE_STRING,"Oriented Mesh");
}
else
{
doc.addNode(mi.Orientable?"Yes":"No", VALUE_STRING,"Orientable Mesh");
2005-12-13 16:46:30 +01:00
doc.addNode(mi.Oriented?"Yes":"No", VALUE_STRING,"Oriented Mesh");
2005-12-12 11:48:16 +01:00
}
2005-12-12 12:29:21 +01:00
sprintf(s,"%d",mi.dv);
doc.addNode(s, VALUE_INTEGER,"Duplicated Vertices");
2005-12-13 16:46:30 +01:00
doc.addNode(mi.SelfIntersect?"Yes":"No", VALUE_STRING,"Self Intersection");
2005-12-12 11:48:16 +01:00
doc.finalizeMain();
doc.printXMLTree();
}
2005-12-12 12:29:21 +01:00
void SaveHtmlInfo(MeshInfo &mi)
{
2005-12-13 16:46:30 +01:00
ofstream fout;
fout.open("result.txt", ios::out);
if (!fout)
{
printf("\n Impossible to create the HTML output file.\n");
}
else
{
char dquotes = 32; // double quotes
fout << "<table>" << endl;
fout << "...todo..." << endl;
fout << "<\table>";
fout.close();
}
2005-12-12 12:29:21 +01:00
}
int main(int argc, char ** argv)
{
CMesh m;
2005-12-14 13:15:37 +01:00
bool saveCleanMeshFlag = false; // Save the clean mesh
bool verboseFlag = true; // Verbose mode on/off
bool XmlFlag= false; // XML output enabled/disabled
bool HtmlFlag = false; // HTML output enabled/disabled
2005-12-12 11:48:16 +01:00
string SaveName;
2005-12-12 11:48:16 +01:00
MeshInfo mi;
2005-11-16 17:45:51 +01:00
initMeshInfo(mi);
2005-12-13 16:46:30 +01:00
printf("\n -------------------------------\n"
" TriMeshInfo V.1.2 \n"
" http://vcg.isti.cnr.it\n"
" release date: "__DATE__"\n"
" -------------------------------\n\n\n");
2005-12-12 11:48:16 +01:00
2005-12-13 16:46:30 +01:00
// Parsing arguments
///////////////////////////////////////////////
2005-12-12 11:48:16 +01:00
if (argc <= 1)
{
printf(MSG_ERR_N_ARGS);
exit(-1);
}
2005-12-13 16:46:30 +01:00
mi.FileName = argv[1];
2005-12-14 13:15:37 +01:00
int i = 2;
while (i < argc)
2005-12-12 11:48:16 +01:00
{
2005-12-13 16:46:30 +01:00
if (argv[i][0] == '-')
2005-12-12 11:48:16 +01:00
{
switch(argv[i][1])
{
2005-12-13 16:46:30 +01:00
case 'q' :
// Quiet mode, disable verbose mode
verboseFlag = false;
2005-12-12 11:48:16 +01:00
break;
2005-12-12 12:29:21 +01:00
2005-12-14 13:15:37 +01:00
case 's':
// Save the clean mesh with the name specified
saveCleanMeshFlag = true;
// Check clean mesh name (minimal check)
if (i+1 >= argc)
{
printf(" Invalid output mesh name.\n\n");
exit(-1);
}
else if (argv[i+1][0] != '-')
SaveName = argv[i+1];
else
{
printf(" Invalid output mesh name.\n\n");
exit(-1);
}
i++;
break;
2005-12-12 11:48:16 +01:00
case 'x' :
2005-12-13 16:46:30 +01:00
// Enable XML output
XmlFlag = true;
2005-12-12 11:48:16 +01:00
break;
2005-12-12 12:29:21 +01:00
2005-12-13 16:46:30 +01:00
case 'h' :
// Enable HTML output
HtmlFlag = true;
2005-12-12 11:48:16 +01:00
break;
2005-12-12 12:29:21 +01:00
2005-12-12 11:48:16 +01:00
default:
printf(MSG_ERR_INVALID_OPTION, argv[i]);
exit(0);
2005-12-12 12:29:21 +01:00
break;
2005-12-12 11:48:16 +01:00
}
2005-12-12 12:29:21 +01:00
}
2005-12-14 13:15:37 +01:00
i++;
};
2005-12-12 11:48:16 +01:00
2005-12-13 16:46:30 +01:00
// Mesh loading
//////////////////////////////////////////
OpenMesh(mi.FileName.c_str(),m);
2005-12-13 16:46:30 +01:00
// Mesh processing
//////////////////////////////////////////
printf(" Mesh processing...\n\n");
// Number of vertices
mi.vn = m.vn;
// Number of faces
mi.fn = m.fn;
// DEGENERATED FACES => (faces with area zero)
mi.count_fd = tri::Clean<CMesh>::RemoveZeroAreaFace(m);
2005-11-16 17:45:51 +01:00
2005-12-12 12:29:21 +01:00
// UNREFERENCED VERTEX
mi.count_uv = tri::Clean<CMesh>::RemoveUnreferencedVertex(m);
2005-11-16 17:45:51 +01:00
2005-12-15 12:20:00 +01:00
// Update topology (face-to-face)
tri::UpdateTopology<CMesh>::FaceFace(m);
tri::UpdateTopology<CMesh>::VertexFace(m);
2005-12-13 16:46:30 +01:00
// Update flags
tri::UpdateFlags<CMesh>::Clear(m);
2005-12-12 12:29:21 +01:00
// IS MANIFOLD
mi.Manifold = tri::Clean<CMesh>::IsComplexManifold(m);
// COUNT EDGES
2005-12-12 12:29:21 +01:00
tri::Clean<CMesh>::CountEdges(m, mi.count_e, mi.boundary_e);
2005-09-30 17:48:46 +02:00
// HOLES COUNT
if(mi.Manifold)
{
mi.numholes = tri::Clean<CMesh>::CountHoles(m);
2005-12-12 11:48:16 +01:00
mi.BEdges = tri::Clean<CMesh>::BorderEdges(m, mi.numholes);
}
// CONNECTED COMPONENTS
2005-12-12 11:48:16 +01:00
mi.numcomponents = tri::Clean<CMesh>::ConnectedComponents(m);
2005-12-13 16:46:30 +01:00
// ORIENTATION
if (mi.Manifold)
tri::Clean<CMesh>::IsOrientedMesh(m, mi.Oriented, mi.Orientable);
2005-12-16 12:16:36 +01:00
else
{
mi.Oriented = false;
mi.Orientable = false;
}
2005-12-13 16:46:30 +01:00
// VOLUME (require a closed oriented manifold)
if ((mi.Manifold)&&(mi.Oriented)&&(!mi.numholes))
2005-12-14 13:15:37 +01:00
{
tri::Inertia<CMesh> mm;
mm.Compute(m);
mi.Volume = mm.Mass();
}
2005-12-13 16:46:30 +01:00
// GENUS
2005-12-12 11:48:16 +01:00
if(mi.Manifold)
2005-12-12 12:29:21 +01:00
mi.Genus = tri::Clean<CMesh>::MeshGenus(m,mi.count_uv, mi.numholes,
mi.numcomponents, mi.count_e);
// REGULARITY
2005-12-16 12:16:36 +01:00
if (mi.Manifold)
tri::Clean<CMesh>::IsRegularMesh(m, mi.Regular, mi.Semiregular);
else
{
mi.Regular = false;
mi.Semiregular = false;
}
2005-12-13 16:46:30 +01:00
// DUPLICATED VERTICES
2005-12-12 11:48:16 +01:00
mi.dv = tri::Clean<CMesh>::RemoveDuplicateVertex(m);
// SELF INTERSECTION
mi.SelfIntersect = tri::Clean<CMesh>::SelfIntersections(m);
2005-12-13 16:46:30 +01:00
// Mesh Information Output
//////////////////////////////////////////
2005-12-12 12:29:21 +01:00
// Print mesh information
if(verboseFlag)
PrintMeshInfo(mi);
// Save mesh information in XML format
if(XmlFlag)
SaveXMLInfo(mi);
// Save mesh information in HTML format
if (HtmlFlag)
SaveHtmlInfo(mi);
2005-12-14 13:15:37 +01:00
// Save the clean mesh
if (saveCleanMeshFlag)
{
printf(" Save the 'clean' mesh...");
tri::io::Exporter<CMesh>::Save(m, SaveName.c_str());
printf(" done.\n\n");
}
return 0;
}