00001 #include <vector>
00002 #include <limits>
00003
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006
00007
00008 #include <vcg/simplex/vertex/base.h>
00009 #include <vcg/simplex/face/base.h>
00010 #include <vcg/simplex/edge/base.h>
00011 #include <vcg/complex/complex.h>
00012
00013 #include <vcg/math/quadric.h>
00014 #include <vcg/complex/algorithms/clean.h>
00015
00016
00017 #include <wrap/io_trimesh/import.h>
00018 #include <wrap/io_trimesh/export_ply.h>
00019
00020
00021 #include <vcg/complex/algorithms/update/topology.h>
00022 #include <vcg/complex/algorithms/update/bounding.h>
00023 #include <vcg/complex/algorithms/smooth.h>
00024
00025
00026 #include <vcg/complex/algorithms/local_optimization.h>
00027 #include <vcg/complex/algorithms/local_optimization/tri_edge_collapse.h>
00028 #include <vcg/complex/algorithms/local_optimization/tri_edge_collapse_quadric_tex.h>
00029
00030 using namespace vcg;
00031 using namespace tri;
00032
00033
00034 class MyVertex;
00035 class MyEdge;
00036 class MyFace;
00037
00038 struct MyUsedTypes: public UsedTypes<Use<MyVertex>::AsVertexType, Use<MyEdge>::AsEdgeType, Use<MyFace>::AsFaceType>{};
00039
00040 class MyVertex : public Vertex< MyUsedTypes,
00041 vertex::VFAdj,
00042 vertex::Coord3f,
00043 vertex::Normal3f,
00044 vertex::Mark,
00045 vertex::BitFlags >{
00046 };
00047
00048 class MyEdge : public Edge< MyUsedTypes> {};
00049
00050 typedef BasicVertexPair<MyVertex> VertexPair;
00051
00052 class MyFace : public Face< MyUsedTypes,
00053 face::VFAdj,
00054 face::VertexRef,
00055 face::BitFlags,
00056 face::WedgeTexCoord2f> {};
00057
00058
00059 class MyMesh : public vcg::tri::TriMesh<std::vector<MyVertex>, std::vector<MyFace> > {};
00060
00061 class MyTriEdgeCollapseQTex: public TriEdgeCollapseQuadricTex< MyMesh, VertexPair, MyTriEdgeCollapseQTex, QuadricTexHelper<MyMesh> > {
00062 public:
00063 typedef TriEdgeCollapseQuadricTex< MyMesh, VertexPair, MyTriEdgeCollapseQTex, QuadricTexHelper<MyMesh> > TECQ;
00064 inline MyTriEdgeCollapseQTex( const VertexPair &p, int i,BaseParameterClass *pp) :TECQ(p,i,pp){}
00065 };
00066
00067
00068 void TexDecimation(MyMesh &m, bool CleaningFlag,int TargetFaceNum)
00069 {
00070 tri::TriEdgeCollapseQuadricTexParameter pp;
00071
00072 pp.SetDefaultParams();
00073 if(CleaningFlag){
00074 int dup = tri::Clean<MyMesh>::RemoveDuplicateVertex(m);
00075 int unref = tri::Clean<MyMesh>::RemoveUnreferencedVertex(m);
00076 printf("Removed %i duplicate and %i unreferenced vertices from mesh \n",dup,unref);
00077 }
00078
00079 printf("reducing it to %i\n",TargetFaceNum);
00080 int t1=clock();
00081
00082 tri::UpdateBounding<MyMesh>::Box(m);
00083 math::Quadric<double> QZero;
00084 QZero.SetZero();
00085 QuadricTexHelper<MyMesh>::QuadricTemp TD3(m.vert,QZero);
00086 QuadricTexHelper<MyMesh>::TDp3()=&TD3;
00087
00088 std::vector<std::pair<vcg::TexCoord2<float>,Quadric5<double> > > qv;
00089
00090 QuadricTexHelper<MyMesh>::Quadric5Temp TD(m.vert,qv);
00091 QuadricTexHelper<MyMesh>::TDp()=&TD;
00092
00093
00094 vcg::LocalOptimization<MyMesh> DeciSession(m, &pp);
00095
00096 DeciSession.Init<MyTriEdgeCollapseQTex>();
00097
00098 DeciSession.SetTargetSimplices(TargetFaceNum);
00099 DeciSession.SetTimeBudget(0.1f);
00100
00101
00102 int faceToDel=m.fn-TargetFaceNum;
00103 int t2=clock();
00104
00105 while( DeciSession.DoOptimization() && m.fn>TargetFaceNum )
00106 {
00107 printf("Simplifing heap size %i ops %i\n",int(DeciSession.h.size()),DeciSession.nPerfmormedOps);
00108 };
00109
00110 DeciSession.Finalize<MyTriEdgeCollapseQTex>();
00111
00112 int t3=clock();
00113 printf("mesh %d %d Error %g \n",m.vn,m.fn,DeciSession.currMetric);
00114 printf("\nCompleted in (%i+%i) msec\n",t2-t1,t3-t2);
00115 }
00116
00117
00118
00119 int main(int argc, char**argv){
00120
00121 int meshNum=argc-1;
00122
00123
00124
00125 MyMesh meshVec[10];
00126 int tt0=clock();
00127 char buf[255];
00128 int i;
00129
00130 for(i=0;i<meshNum;++i)
00131 {
00132 int err=vcg::tri::io::Importer<MyMesh>::Open(meshVec[i],argv[i+1]);
00133 if(err)
00134 {
00135 printf("Unable to open mesh %s : '%s'\n",argv[i+1], vcg::tri::io::Importer<MyMesh>::ErrorMsg(err));
00136 exit(-1);
00137 }
00138 printf("mesh loaded %d %d \n",meshVec[i].vn,meshVec[i].fn);
00139
00140 int t1=clock();
00141 tri::Smooth<MyMesh>::VertexCoordLaplacian(meshVec[i],5*i);
00142
00143 TexDecimation(meshVec[i],true,meshVec[i].fn/2);
00144 int t2=clock();
00145 printf("%i %5.3f sec\n",i,float(t2-t1)/CLOCKS_PER_SEC);
00146 sprintf(buf,"out%i.ply",i);
00147 tri::io::ExporterPLY<MyMesh>::Save(meshVec[i],buf,false);
00148 }
00149
00150 int tt1=clock();
00151 printf("---Total %5.3f sec\n",float(tt1-tt0)/CLOCKS_PER_SEC);
00152
00153 for(int i=0;i<meshNum;++i)
00154 {
00155 char buf[255];
00156 sprintf(buf,"out%i.ply",i);
00157 tri::io::ExporterPLY<MyMesh>::Save(meshVec[i],buf,tri::io::Mask::IOM_WEDGTEXCOORD,false);
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 return 0;
00229
00230 }