VCG Library
trimesh_clustering.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
29 #include<vcg/complex/complex.h>
30 
31 #include <vcg/complex/algorithms/clustering.h>
32 
33 #include <wrap/io_trimesh/import.h>
34 #include <wrap/io_trimesh/export.h>
35 
36 class MyFace;
37 class MyVertex;
38 
39 struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<MyVertex>::AsVertexType, vcg::Use<MyFace>::AsFaceType>{};
40 
41 class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f, vcg::vertex::Normal3f, vcg::vertex::BitFlags >{};
42 class MyFace : public vcg::Face < MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f, vcg::face::BitFlags > {};
43 class MyMesh : public vcg::tri::TriMesh< std::vector<MyVertex>, std::vector<MyFace> > {};
44 
45 int main(int argc, char **argv)
46 {
47  if(argc<3)
48  {
49  printf(
50  "Usage: trimesh_clustering filein.ply fileout.ply [opt] \n"
51  "options: \n"
52  "-k cellnum approx number of cluster that should be defined; (default 10e5)\n"
53  "-s size in absolute units the size of the clustering cell (override the previous param)\n"
54  "-d enable the duplication of faces for double surfaces\n"
55  );
56  exit(0);
57  }
58 
59  int CellNum=100000;
60  float CellSize=0;
61  bool DupFace=false;
62 
63  int i=3;
64  while(i<argc)
65  {
66  if(argv[i][0]!='-')
67  { printf("Error unable to parse option '%s'\n",argv[i]); exit(0); }
68  switch(argv[i][1])
69  {
70  case 'k' : CellNum=atoi(argv[i+1]); ++i; printf("Using %i clustering cells\n",CellNum); break;
71  case 's' : CellSize=atof(argv[i+1]); ++i; printf("Using %5f as clustering cell size\n",CellSize); break;
72  case 'd' : DupFace=true; printf("Enabling the duplication of faces for double surfaces\n"); break;
73 
74  default : {printf("Error unable to parse option '%s'\n",argv[i]); exit(0);}
75  }
76  ++i;
77  }
78 
79  MyMesh m;
80  if(vcg::tri::io::ImporterPLY<MyMesh>::Open(m,argv[1])!=0)
81  {
82  printf("Error reading file %s\n",argv[1]);
83  exit(0);
84  }
85 
88  printf("Input mesh vn:%i fn:%i\n",m.VN(),m.FN());
89  vcg::tri::Clustering<MyMesh, vcg::tri::AverageColorCell<MyMesh> > Grid;
90  Grid.DuplicateFaceParam=DupFace;
91  Grid.Init(m.bbox,CellNum,CellSize);
92 
93  printf("Clustering to %i cells\n",Grid.Grid.siz[0]*Grid.Grid.siz[1]*Grid.Grid.siz[2] );
94  printf("Grid of %i x %i x %i cells\n",Grid.Grid.siz[0],Grid.Grid.siz[1],Grid.Grid.siz[2]);
95  printf("with cells size of %.2f x %.2f x %.2f units\n",Grid.Grid.voxel[0],Grid.Grid.voxel[1],Grid.Grid.voxel[2]);
96 
97  Grid.AddMesh(m);
98  Grid.ExtractMesh(m);
99  printf("Output mesh vn:%i fn:%i\n",m.VN(),m.FN());
100 
101  vcg::tri::io::ExporterPLY<MyMesh>::Save(m,argv[2]);
102  return 0;
103 }