VCG Library
trimesh_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_off.h>
26 #include<wrap/io_trimesh/export_off.h>
27 
28 #include<vcg/complex/algorithms/point_sampling.h>
29 #include<vcg/complex/algorithms/create/platonic.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<2)
49  {
50  printf("Usage trimesh_base <meshfilename.obj> radius\n");
51  return -1;
52  }
53 
54  MyMesh m;
55 
56  if(tri::io::ImporterOFF<MyMesh>::Open(m,argv[1])!=0)
57  {
58  printf("Error reading file %s\n",argv[1]);
59  exit(0);
60  }
61  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::SamplingRandomGenerator().initialize(time(0));
62 
63  //----------------------------------------------------------------------
64  // Basic Sample of a mesh surface
65  // Build a point cloud with points with a plain poisson disk distribution
66 
67  int t0=clock();
68  vector<Point3f> pointVec;
69  float rad=0;
70  if(argc>2) rad=atof(argv[2]);
71  int sampleNum=rad?0:1000;
72  tri::PoissonSampling<MyMesh>(m,pointVec,sampleNum,rad);
73  int t1=clock();
74  MyMesh BasicPoissonMesh;
75  tri::BuildMeshFromCoordVector(BasicPoissonMesh,pointVec);
76 
77  tri::io::ExporterOFF<MyMesh>::Save(BasicPoissonMesh,"BasicPoissonMesh.off");
78  printf("Computed a basic poisson disk distribution of %i vertices radius is %6.3f in %5.2f sec\n",BasicPoissonMesh.VN(),rad,float(t1-t0)/CLOCKS_PER_SEC);
79 
80  //----------------------------------------------------------------------
81  // Advanced Sample
82  // Make a feature dependent Poisson Disk sampling
83  MyMesh MontecarloSurfaceMesh;
84  MyMesh MontecarloEdgeMesh;
85  MyMesh PoissonEdgeMesh;
86  MyMesh PoissonMesh;
87 
88  std::vector<Point3f> sampleVec;
89  tri::TrivialSampler<MyMesh> mps(sampleVec);
92  tri::UpdateFlags<MyMesh>::FaceFauxCrease(m,math::ToRad(40.0f));
93  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::EdgeMontecarlo(m,mps,10000,false);
94  tri::BuildMeshFromCoordVector(MontecarloEdgeMesh,sampleVec);
95  tri::io::ExporterOFF<MyMesh>::Save(MontecarloEdgeMesh,"MontecarloEdgeMesh.off");
96 
97  sampleVec.clear();
99  tri::BuildMeshFromCoordVector(PoissonEdgeMesh,sampleVec);
100  tri::io::ExporterOFF<MyMesh>::Save(PoissonEdgeMesh,"VertexCreaseMesh.off");
101 
103  pp.preGenMesh = &PoissonEdgeMesh;
104  pp.preGenFlag=true;
105  sampleVec.clear();
106  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::PoissonDiskPruning(mps, MontecarloEdgeMesh, rad, pp);
107  tri::BuildMeshFromCoordVector(PoissonEdgeMesh,sampleVec);
108  tri::io::ExporterOFF<MyMesh>::Save(PoissonEdgeMesh,"PoissonEdgeMesh.off");
109 
110  sampleVec.clear();
112  tri::BuildMeshFromCoordVector(MontecarloSurfaceMesh,sampleVec);
113  tri::io::ExporterOFF<MyMesh>::Save(MontecarloSurfaceMesh,"MontecarloSurfaceMesh.off");
114 
115  pp.preGenMesh = &PoissonEdgeMesh;
116  pp.preGenFlag=true;
117  sampleVec.clear();
118  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::PoissonDiskPruning(mps, MontecarloSurfaceMesh, rad, pp);
119  tri::BuildMeshFromCoordVector(PoissonMesh,sampleVec);
120  tri::io::ExporterOFF<MyMesh>::Save(PoissonMesh,"PoissonMesh.off");
121  printf("Computed a feature aware poisson disk distribution of %i vertices radius is %6.3f\n",PoissonMesh.VN(),rad);
122 
123  return 0;
124 }