2005-10-01 23:24:00 +02:00
|
|
|
#include <vector>
|
2005-10-03 02:00:31 +02:00
|
|
|
#include <limits>
|
|
|
|
|
2005-10-01 23:24:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
// stuff to define the mesh
|
2008-12-19 11:43:36 +01:00
|
|
|
#include <vcg/simplex/vertex/base.h>
|
|
|
|
#include <vcg/simplex/face/base.h>
|
2009-01-21 18:26:48 +01:00
|
|
|
#include <vcg/simplex/edge/base.h>
|
2011-04-01 19:09:03 +02:00
|
|
|
#include <vcg/complex/complex.h>
|
2006-10-10 12:01:11 +02:00
|
|
|
|
|
|
|
#include <vcg/math/quadric.h>
|
2011-04-01 19:09:03 +02:00
|
|
|
#include <vcg/complex/algorithms/clean.h>
|
2005-10-01 23:24:00 +02:00
|
|
|
|
|
|
|
// io
|
|
|
|
#include <wrap/io_trimesh/import.h>
|
|
|
|
#include <wrap/io_trimesh/export_ply.h>
|
|
|
|
|
|
|
|
// update
|
2011-04-01 19:09:03 +02:00
|
|
|
#include <vcg/complex/algorithms/update/topology.h>
|
2006-10-10 12:01:11 +02:00
|
|
|
|
2005-10-01 23:24:00 +02:00
|
|
|
// local optimization
|
2011-04-01 19:09:03 +02:00
|
|
|
#include <vcg/complex/algorithms/local_optimization.h>
|
|
|
|
#include <vcg/complex/algorithms/local_optimization/tri_edge_collapse_quadric.h>
|
2005-10-01 23:24:00 +02:00
|
|
|
|
|
|
|
using namespace vcg;
|
|
|
|
using namespace tri;
|
|
|
|
|
2006-10-10 12:01:11 +02:00
|
|
|
/**********************************************************
|
|
|
|
Mesh Classes for Quadric Edge collapse based simplification
|
|
|
|
|
|
|
|
For edge collpases we need verteses with:
|
|
|
|
- V->F adjacency
|
|
|
|
- per vertex incremental mark
|
|
|
|
- per vertex Normal
|
|
|
|
|
|
|
|
|
|
|
|
Moreover for using a quadric based collapse the vertex class
|
|
|
|
must have also a Quadric member Q();
|
|
|
|
Otherwise the user have to provide an helper function object
|
|
|
|
to recover the quadric.
|
|
|
|
|
|
|
|
******************************************************/
|
|
|
|
// The class prototypes.
|
2005-10-01 23:24:00 +02:00
|
|
|
class MyVertex;
|
2011-06-06 02:13:51 +02:00
|
|
|
class MyEdge;
|
2006-10-10 12:01:11 +02:00
|
|
|
class MyFace;
|
|
|
|
|
2010-03-18 14:25:20 +01:00
|
|
|
struct MyUsedTypes: public UsedTypes<Use<MyVertex>::AsVertexType,Use<MyEdge>::AsEdgeType,Use<MyFace>::AsFaceType>{};
|
2005-10-01 23:24:00 +02:00
|
|
|
|
2010-03-18 14:25:20 +01:00
|
|
|
class MyVertex : public Vertex< MyUsedTypes,
|
2011-06-06 02:13:51 +02:00
|
|
|
vertex::VFAdj,
|
|
|
|
vertex::Coord3f,
|
|
|
|
vertex::Normal3f,
|
|
|
|
vertex::Mark,
|
2009-01-21 18:26:48 +01:00
|
|
|
vertex::BitFlags >{
|
2011-06-06 02:13:51 +02:00
|
|
|
public:
|
2006-10-10 12:01:11 +02:00
|
|
|
vcg::math::Quadric<double> &Qd() {return q;}
|
|
|
|
private:
|
|
|
|
math::Quadric<double> q;
|
|
|
|
};
|
2009-01-21 18:26:48 +01:00
|
|
|
|
2011-06-06 02:13:51 +02:00
|
|
|
class MyEdge : public Edge< MyUsedTypes> {};
|
2005-10-01 23:24:00 +02:00
|
|
|
|
2011-06-06 02:13:51 +02:00
|
|
|
typedef BasicVertexPair<MyVertex> VertexPair;
|
2006-10-10 12:01:11 +02:00
|
|
|
|
2010-03-18 14:25:20 +01:00
|
|
|
class MyFace : public Face< MyUsedTypes,
|
2011-06-06 02:13:51 +02:00
|
|
|
face::VFAdj,
|
|
|
|
face::VertexRef,
|
2006-10-10 12:01:11 +02:00
|
|
|
face::BitFlags > {};
|
|
|
|
|
2011-06-06 02:13:51 +02:00
|
|
|
// the main mesh class
|
2007-03-22 11:38:52 +01:00
|
|
|
class MyMesh : public vcg::tri::TriMesh<std::vector<MyVertex>, std::vector<MyFace> > {};
|
2006-10-10 12:01:11 +02:00
|
|
|
|
2005-10-01 23:24:00 +02:00
|
|
|
|
2011-06-06 02:13:51 +02:00
|
|
|
class MyTriEdgeCollapse: public vcg::tri::TriEdgeCollapseQuadric< MyMesh, VertexPair, MyTriEdgeCollapse, QInfoStandard<MyVertex> > {
|
|
|
|
public:
|
|
|
|
typedef vcg::tri::TriEdgeCollapseQuadric< MyMesh, VertexPair, MyTriEdgeCollapse, QInfoStandard<MyVertex> > TECQ;
|
2006-10-10 12:01:11 +02:00
|
|
|
typedef MyMesh::VertexType::EdgeType EdgeType;
|
2011-06-06 02:13:51 +02:00
|
|
|
inline MyTriEdgeCollapse( const VertexPair &p, int i, BaseParameterClass *pp) :TECQ(p,i,pp){}
|
2005-10-01 23:24:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void Usage()
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"---------------------------------\n"
|
|
|
|
" TriSimp V.1.0 \n"
|
|
|
|
" http://vcg.isti.cnr.it\n"
|
|
|
|
" http://vcg.sourceforge.net\n"
|
|
|
|
" release date: "__DATE__"\n"
|
|
|
|
"---------------------------------\n\n"
|
|
|
|
"TriDecimator 1.0 \n"__DATE__"\n"
|
2011-10-05 17:04:40 +02:00
|
|
|
"Copyright 2003-2012 Visual Computing Lab I.S.T.I. C.N.R.\n"
|
2005-10-01 23:24:00 +02:00
|
|
|
"\nUsage: "\
|
2011-10-05 17:04:40 +02:00
|
|
|
"tridecimator fileIn fileOut face_num [opt]\n"\
|
2005-10-01 23:24:00 +02:00
|
|
|
"Where opt can be:\n"\
|
|
|
|
" -e# QuadricError threshold (range [0,inf) default inf)\n"
|
|
|
|
" -b# Boundary Weight (default .5)\n"
|
2005-10-03 02:00:31 +02:00
|
|
|
" -q# Quality threshold (range [0.0, 0.866], default .3 )\n"
|
2005-10-01 23:24:00 +02:00
|
|
|
" -n# Normal threshold (degree range [0,180] default 90)\n"
|
|
|
|
" -E# Minimal admitted quadric value (default 1e-15, must be >0)\n"
|
|
|
|
" -Q[y|n] Use or not Quality Threshold (default yes)\n"
|
|
|
|
" -N[y|n] Use or not Normal Threshold (default no)\n"
|
|
|
|
" -A[y|n] Use or not Area Weighted Quadrics (default yes)\n"
|
|
|
|
" -O[y|n] Use or not vertex optimal placement (default yes)\n"
|
|
|
|
" -S[y|n] Use or not Scale Independent quadric measure(default yes) \n"
|
|
|
|
" -B[y|n] Preserve or not mesh boundary (default no)\n"
|
|
|
|
" -T[y|n] Preserve or not Topology (default no)\n"
|
|
|
|
" -H[y|n] Use or not Safe Heap Update (default no)\n"
|
2005-10-03 02:00:31 +02:00
|
|
|
" -P Before simplification, remove duplicate & unreferenced vertices\n"
|
|
|
|
);
|
2005-10-01 23:24:00 +02:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2006-10-10 12:01:11 +02:00
|
|
|
// mesh to simplify
|
|
|
|
MyMesh mesh;
|
2005-10-01 23:24:00 +02:00
|
|
|
|
|
|
|
int main(int argc ,char**argv){
|
|
|
|
if(argc<4) Usage();
|
|
|
|
|
|
|
|
int FinalSize=atoi(argv[3]);
|
2011-06-06 02:13:51 +02:00
|
|
|
//int t0=clock();
|
2005-10-01 23:24:00 +02:00
|
|
|
int err=vcg::tri::io::Importer<MyMesh>::Open(mesh,argv[1]);
|
2011-06-06 02:13:51 +02:00
|
|
|
if(err)
|
2005-10-01 23:24:00 +02:00
|
|
|
{
|
|
|
|
printf("Unable to open mesh %s : '%s'\n",argv[1],vcg::tri::io::Importer<MyMesh>::ErrorMsg(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
2011-06-06 02:13:51 +02:00
|
|
|
printf("mesh loaded %d %d \n",mesh.vn,mesh.fn);
|
2006-10-10 12:01:11 +02:00
|
|
|
|
2011-06-06 02:13:51 +02:00
|
|
|
TriEdgeCollapseQuadricParameter qparams;
|
2006-10-10 12:01:11 +02:00
|
|
|
qparams.QualityThr =.3;
|
2011-06-06 02:13:51 +02:00
|
|
|
float TargetError=std::numeric_limits<float>::max();
|
2005-10-03 02:00:31 +02:00
|
|
|
bool CleaningFlag =false;
|
2005-10-01 23:24:00 +02:00
|
|
|
// parse command line.
|
2011-06-06 02:13:51 +02:00
|
|
|
for(int i=4; i < argc;)
|
2005-10-01 23:24:00 +02:00
|
|
|
{
|
|
|
|
if(argv[i][0]=='-')
|
|
|
|
switch(argv[i][1])
|
2011-06-06 02:13:51 +02:00
|
|
|
{
|
|
|
|
case 'H' : qparams.SafeHeapUpdate=true; printf("Using Safe heap option\n"); break;
|
2006-10-10 12:01:11 +02:00
|
|
|
case 'Q' : if(argv[i][2]=='y') { qparams.QualityCheck = true; printf("Using Quality Checking\n"); }
|
2011-06-06 02:13:51 +02:00
|
|
|
else { qparams.QualityCheck = false; printf("NOT Using Quality Checking\n"); } break;
|
|
|
|
case 'N' : if(argv[i][2]=='y') { qparams.NormalCheck = true; printf("Using Normal Deviation Checking\n"); }
|
|
|
|
else { qparams.NormalCheck = false; printf("NOT Using Normal Deviation Checking\n"); } break;
|
|
|
|
case 'O' : if(argv[i][2]=='y') { qparams.OptimalPlacement = true; printf("Using OptimalPlacement\n"); }
|
|
|
|
else { qparams.OptimalPlacement = false; printf("NOT Using OptimalPlacement\n"); } break;
|
|
|
|
case 'S' : if(argv[i][2]=='y') { qparams.ScaleIndependent = true; printf("Using ScaleIndependent\n"); }
|
|
|
|
else { qparams.ScaleIndependent = false; printf("NOT Using ScaleIndependent\n"); } break;
|
|
|
|
case 'B' : if(argv[i][2]=='y') { qparams.PreserveBoundary = true; printf("Preserving Boundary\n"); }
|
|
|
|
else { qparams.PreserveBoundary = false; printf("NOT Preserving Boundary\n"); } break;
|
|
|
|
case 'T' : if(argv[i][2]=='y') { qparams.PreserveTopology = true; printf("Preserving Topology\n"); }
|
|
|
|
else { qparams.PreserveTopology = false; printf("NOT Preserving Topology\n"); } break;
|
|
|
|
case 'q' : qparams.QualityThr = atof(argv[i]+2); printf("Setting Quality Thr to %f\n",atof(argv[i]+2)); break;
|
|
|
|
case 'n' : qparams.NormalThrRad = math::ToRad(atof(argv[i]+2)); printf("Setting Normal Thr to %f deg\n",atof(argv[i]+2)); break;
|
|
|
|
case 'b' : qparams.BoundaryWeight = atof(argv[i]+2); printf("Setting Boundary Weight to %f\n",atof(argv[i]+2)); break;
|
|
|
|
case 'e' : TargetError = float(atof(argv[i]+2)); printf("Setting TargetError to %g\n",atof(argv[i]+2)); break;
|
|
|
|
case 'P' : CleaningFlag=true; printf("Cleaning mesh before simplification\n"); break;
|
|
|
|
|
|
|
|
default : printf("Unknown option '%s'\n", argv[i]);
|
2005-10-01 23:24:00 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-03 02:00:31 +02:00
|
|
|
|
|
|
|
if(CleaningFlag){
|
|
|
|
int dup = tri::Clean<MyMesh>::RemoveDuplicateVertex(mesh);
|
|
|
|
int unref = tri::Clean<MyMesh>::RemoveUnreferencedVertex(mesh);
|
|
|
|
printf("Removed %i duplicate and %i unreferenced vertices from mesh \n",dup,unref);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf("reducing it to %i\n",FinalSize);
|
|
|
|
|
2011-06-06 02:13:51 +02:00
|
|
|
vcg::tri::UpdateBounding<MyMesh>::Box(mesh);
|
2005-10-03 02:00:31 +02:00
|
|
|
|
2011-06-06 02:13:51 +02:00
|
|
|
// decimator initialization
|
|
|
|
vcg::LocalOptimization<MyMesh> DeciSession(mesh,&qparams);
|
2005-10-03 02:00:31 +02:00
|
|
|
|
2011-06-06 02:13:51 +02:00
|
|
|
int t1=clock();
|
|
|
|
DeciSession.Init<MyTriEdgeCollapse>();
|
|
|
|
int t2=clock();
|
|
|
|
printf("Initial Heap Size %i\n",int(DeciSession.h.size()));
|
2005-10-01 23:24:00 +02:00
|
|
|
|
2011-06-06 02:13:51 +02:00
|
|
|
DeciSession.SetTargetSimplices(FinalSize);
|
|
|
|
DeciSession.SetTimeBudget(0.5f);
|
|
|
|
if(TargetError< std::numeric_limits<float>::max() ) DeciSession.SetTargetMetric(TargetError);
|
2005-10-01 23:24:00 +02:00
|
|
|
|
2005-10-03 02:00:31 +02:00
|
|
|
while(DeciSession.DoOptimization() && mesh.fn>FinalSize && DeciSession.currMetric < TargetError)
|
2011-06-06 02:13:51 +02:00
|
|
|
printf("Current Mesh size %7i heap sz %9i err %9g \r",mesh.fn, int(DeciSession.h.size()),DeciSession.currMetric);
|
|
|
|
|
|
|
|
int t3=clock();
|
2005-10-03 02:00:31 +02:00
|
|
|
printf("mesh %d %d Error %g \n",mesh.vn,mesh.fn,DeciSession.currMetric);
|
|
|
|
printf("\nCompleted in (%i+%i) msec\n",t2-t1,t3-t2);
|
|
|
|
|
2005-10-01 23:24:00 +02:00
|
|
|
vcg::tri::io::ExporterPLY<MyMesh>::Save(mesh,argv[2]);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|