VCG Library
trimesh_ball_pivoting.cpp
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 ****************************************************************************/
23 #include<vcg/complex/complex.h>
24 #include <vcg/complex/algorithms/create/ball_pivoting.h>
25 
26 // input output
27 #include <wrap/io_trimesh/import_ply.h>
28 #include <wrap/io_trimesh/export_ply.h>
29 
30 using namespace vcg;
31 using namespace std;
32 
33 class MyFace;
34 class MyVertex;
35 
36 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
37  Use<MyFace> ::AsFaceType>{};
38 
39 class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags, vertex::Mark>{};
40 class MyFace : public Face < MyUsedTypes, face::VertexRef, face::Normal3f, face::BitFlags > {};
41 class MyMesh : public vcg::tri::TriMesh< vector<MyVertex>, vector<MyFace> > {};
42 
43 bool callback(int percent, const char *str) {
44  cout << "str: " << str << " " << percent << "%\n";
45  return true;
46 }
47 
48 int main(int argc, char **argv)
49 {
50  if(argc<3)
51  {
52  printf(
53  "Usage: trimesh_ball_pivoting filein.ply fileout.ply [opt]\n"
54  "options: \n"
55  "-r <val> radius of the rolling ball\n"
56  "-c <val> clustering radius (as fraction of radius) default: 0.05\n"
57  );
58  exit(0);
59  }
60 
61  float radius = 0.0f;
62  float clustering = 0.05;
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 'r' : radius = atof(argv[++i]); printf("Using %f sphere radius\n",radius); break;
71  case 'c' : clustering = atof(argv[++i]); printf("Using %f clustering radius\n",clustering); break;
72 
73  default : {printf("Error unable to parse option '%s'\n",argv[i]); exit(0);}
74  }
75  ++i;
76  }
77  if(radius == 0)
78  printf("Autodetecting ball radius...\n");
79 
80  MyMesh m;
81 
82  if(vcg::tri::io::ImporterPLY<MyMesh>::Open(m,argv[1])!=0)
83  {
84  printf("Error reading file %s\n",argv[1]);
85  exit(0);
86  }
89  printf("Input mesh vn:%i fn:%i\n",m.VN(),m.FN());
90 
91  int t0=clock();
92  // Initialization
93  tri::BallPivoting<MyMesh> pivot(m, radius, clustering);
94  printf("Ball radius: %f\nClustering points withing %f radii\n", pivot.radius, clustering);
95 
96  int t1=clock();
97  // the main processing
98  pivot.BuildMesh(callback);
99 
100  int t2=clock();
101 
102  printf("Output mesh vn:%i fn:%i\n",m.VN(),m.FN());
103  printf("Created in :%i msec (%i+%i)\n",t2-t0,t1-t0,t2-t1);
104 
105  vcg::tri::io::PlyInfo pi;
106  vcg::tri::io::ExporterPLY<MyMesh>::Save(m,argv[2],pi.mask);
107  return 0;
108 
109 }