VCG Library
trimesh_pointcloud_sampling.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 
25 #include<wrap/io_trimesh/import.h>
26 #include<wrap/io_trimesh/export.h>
27 
28 #include<vcg/complex/algorithms/point_sampling.h>
29 #include<vcg/complex/algorithms/clustering.h>
30 
31 using namespace vcg;
32 using namespace std;
33 
34 class MyEdge;
35 class MyFace;
36 class MyVertex;
37 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
38  Use<MyEdge> ::AsEdgeType,
39  Use<MyFace> ::AsFaceType>{};
40 
41 class MyVertex : public Vertex<MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags >{};
42 class MyFace : public Face< MyUsedTypes, face::FFAdj, face::Normal3f, face::VertexRef, face::BitFlags > {};
43 class MyEdge : public Edge<MyUsedTypes>{};
44 class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyFace> , vector<MyEdge> > {};
45 
46 int main( int argc, char **argv )
47 {
48  if(argc<3)
49  {
50  printf("Usage trimesh_base <meshfilename> radius (as perc) (\n");
51  return -1;
52  }
53 
54  MyMesh m;
55  MyMesh subM;
56  MyMesh cluM;
57  MyMesh rndM;
58 
59  tri::MeshSampler<MyMesh> mps(subM);
60  tri::MeshSampler<MyMesh> mrs(rndM);
61 
62  if(tri::io::Importer<MyMesh>::Open(m,argv[1])!=0)
63  {
64  printf("Error reading file %s\n",argv[1]);
65  exit(0);
66  }
67  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::SamplingRandomGenerator().initialize(time(0));
68  float perc = atof(argv[2]);
69 
70  float radius = m.bbox.Diag() * perc;
71  printf("Subsampling a PointCloud of %i vert with %f radius\n",m.VN(),radius);
73  pp.bestSampleChoiceFlag=false;
74  tri::SurfaceSampling<MyMesh,tri::MeshSampler<MyMesh> >::PoissonDiskPruning(mps, m, radius, pp);
75  tri::io::ExporterPLY<MyMesh>::Save(subM,"PoissonMesh.ply");
76  printf("Sampled %i vertices in %5.2f\n",subM.VN(), float(pp.pds.pruneTime+pp.pds.gridTime)/float(CLOCKS_PER_SEC));
77 
78  int t0=clock();
79  tri::Clustering<MyMesh, vcg::tri::AverageColorCell<MyMesh> > ClusteringGrid;
80  ClusteringGrid.Init(m.bbox,100000,radius*sqrt(2.0f));
81  ClusteringGrid.AddPointSet(m);
82  ClusteringGrid.ExtractMesh(cluM);
83  int t1=clock();
84  tri::io::ExporterPLY<MyMesh>::Save(cluM,"ClusterMesh.ply");
85  printf("Sampled %i vertices in %5.2f\n",cluM.VN(), float(t1-t0)/float(CLOCKS_PER_SEC));
86 
87  int t2=clock();
88  int sampleNum = (cluM.VN()+subM.VN())/2;
89  tri::SurfaceSampling<MyMesh,tri::MeshSampler<MyMesh> >::VertexUniform(m, mrs,sampleNum);
90  int t3=clock();
91  tri::io::ExporterPLY<MyMesh>::Save(rndM,"RandomMesh.ply");
92  printf("Sampled %i vertices in %5.2f\n",rndM.VN(), float(t3-t2)/float(CLOCKS_PER_SEC));
93 
94 
95  return 0;
96 }