00001
00002
00003
00004
00005
00006
00007 #include<vcg/simplex/vertex/base.h>
00008 #include<vcg/simplex/face/base.h>
00009 #include<vcg/simplex/face/topology.h>
00010
00011 #include<vcg/complex/trimesh/base.h>
00012
00013 #include <vcg/complex/trimesh/update/bounding.h>
00014 #include <vcg/complex/trimesh/update/topology.h>
00015 #include <vcg/complex/trimesh/update/normal.h>
00016 #include <vcg/complex/trimesh/update/flag.h>
00017 #include <vcg/complex/trimesh/clustering.h>
00018
00019
00020 #include <wrap/io_trimesh/import_ply.h>
00021 #include <wrap/io_trimesh/export_ply.h>
00022
00023
00024 #include <vector>
00025 #include <time.h>
00026
00027 using namespace vcg;
00028 using namespace std;
00029
00030 class MyFace;
00031 class MyVertex;
00032
00033 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
00034 Use<MyFace> ::AsFaceType>{};
00035
00036 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags >{};
00037 class MyFace : public Face < MyUsedTypes, face::VertexRef, face::Normal3f, face::BitFlags > {};
00038 class MyMesh : public vcg::tri::TriMesh< vector<MyVertex>, vector<MyFace> > {};
00039
00040 int main(int argc, char **argv)
00041 {
00042 if(argc<3)
00043 {
00044 printf(
00045 "\n trimesh_clustering ("__DATE__")\n"
00046 " Visual Computing Group I.S.T.I. C.N.R.\n"
00047 "Usage: PlyRefine filein.ply fileout.ply [opt] \n"
00048 "options: \n"
00049 "-k cellnum approx number of cluster that should be defined; (default 10e5)\n"
00050 "-s size in absolute units the size of the clustering cell (override the previous param)\n"
00051 "-d enable the duplication of faces for double surfaces\n"
00052 );
00053 exit(0);
00054 }
00055
00056 int i=3;
00057 int CellNum=100000;
00058 float CellSize=0;
00059 bool DupFace=false;
00060
00061 while(i<argc)
00062 {
00063 if(argv[i][0]!='-')
00064 {printf("Error unable to parse option '%s'\n",argv[i]); exit(0);}
00065 switch(argv[i][1])
00066 {
00067 case 'k' : CellNum=atoi(argv[i+1]); ++i; printf("Using %i clustering cells\n",CellNum); break;
00068 case 's' : CellSize=atof(argv[i+1]); ++i; printf("Using %5f as clustering cell size\n",CellSize); break;
00069 case 'd' : DupFace=true; printf("Enabling the duplication of faces for double surfaces\n"); break;
00070
00071 default : {printf("Error unable to parse option '%s'\n",argv[i]); exit(0);}
00072 }
00073 ++i;
00074 }
00075
00076 MyMesh m;
00077
00078 if(vcg::tri::io::ImporterPLY<MyMesh>::Open(m,argv[1])!=0)
00079 {
00080 printf("Error reading file %s\n",argv[1]);
00081 exit(0);
00082 }
00083 vcg::tri::UpdateBounding<MyMesh>::Box(m);
00084 vcg::tri::UpdateNormals<MyMesh>::PerFace(m);
00085 printf("Input mesh vn:%i fn:%i\n",m.vn,m.fn);
00086 vcg::tri::Clustering<MyMesh, vcg::tri::AverageColorCell<MyMesh> > Grid;
00087 Grid.DuplicateFaceParam=DupFace;
00088 Grid.Init(m.bbox,CellNum,CellSize);
00089
00090 printf("Clustering to %i cells\n",Grid.Grid.siz[0]*Grid.Grid.siz[1]*Grid.Grid.siz[2] );
00091 printf("Grid of %i x %i x %i cells\n",Grid.Grid.siz[0],Grid.Grid.siz[1],Grid.Grid.siz[2]);
00092 printf("with cells size of %.2f x %.2f x %.2f units\n",Grid.Grid.voxel[0],Grid.Grid.voxel[1],Grid.Grid.voxel[2]);
00093
00094 int t0=clock();
00095 Grid.AddMesh(m);
00096 int t1=clock();
00097 Grid.ExtractMesh(m);
00098 int t2=clock();
00099 printf("Output mesh vn:%i fn:%i\n",m.vn,m.fn);
00100 printf("Simplified in :%i msec (%i+%i)\n",t2-t0,t1-t0,t2-t1);
00101
00102 vcg::tri::io::PlyInfo pi;
00103 vcg::tri::io::ExporterPLY<MyMesh>::Save(m,argv[2],pi.mask);
00104 return 0;
00105 }