VCG Library
trimesh_volumesampling.cpp
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2009 \/)\/ *
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 
24 #include<vcg/complex/complex.h>
25 #include<vcg/complex/algorithms/create/platonic.h>
26 #include<wrap/io_trimesh/import_ply.h>
27 #include<wrap/io_trimesh/export_off.h>
28 #include<wrap/io_trimesh/export_ply.h>
29 #include<wrap/io_trimesh/export_dxf.h>
30 #include<vcg/complex/algorithms/point_sampling.h>
31 #include<vcg/complex/algorithms/voronoi_processing.h>
32 #include<vcg/complex/algorithms/voronoi_volume_sampling.h>
33 
34 
35 using namespace vcg;
36 using namespace std;
37 
38 class MyEdge;
39 class MyFace;
40 class MyVertex;
41 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
42  Use<MyEdge> ::AsEdgeType,
43  Use<MyFace> ::AsFaceType>{};
44 
45 class MyVertex : public Vertex<MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::VFAdj, vertex::Qualityf, vertex::Color4b, vertex::BitFlags >{};
46 class MyFace : public Face< MyUsedTypes, face::VertexRef, face::Normal3f, face::BitFlags, face::Mark, face::VFAdj, face::FFAdj > {};
47 class MyEdge : public Edge< MyUsedTypes, edge::VertexRef, edge::BitFlags>{};
48 class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyEdge>, vector<MyFace> > {};
49 
50 class EmEdge;
51 class EmFace;
52 class EmVertex;
53 struct EmUsedTypes : public UsedTypes< Use<EmVertex> ::AsVertexType,
54  Use<EmEdge> ::AsEdgeType,
55  Use<EmFace> ::AsFaceType>{};
56 
57 class EmVertex : public Vertex<EmUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::VFAdj , vertex::Qualityf, vertex::Color4b, vertex::BitFlags >{};
58 class EmFace : public Face< EmUsedTypes, face::VertexRef, face::BitFlags, face::VFAdj > {};
59 class EmEdge : public Edge< EmUsedTypes, edge::VertexRef> {};
60 class EmMesh : public tri::TriMesh< vector<EmVertex>, vector<EmEdge>, vector<EmFace> > {};
61 
62 
63 int main( int argc, char **argv )
64 {
65  MyMesh mSur; // the original surface
66  MyMesh mOff; // the offsetted surface
67  if(argc < 3 )
68  {
69  printf("Usage trimesh_voronoisampling origSurf offsetSurf radius \n"
70  "radius is expressed as a perc bbox diag (e.g. 0.1 -> 10 % of bbox diag) \n");
71  return -1;
72  }
73 
74  tri::io::ImporterPLY<MyMesh>::Open(mSur,argv[1]);
75  tri::io::ImporterPLY<MyMesh>::Open(mOff,argv[2]);
78 
79  std::vector<Point3f> sampleSurVec, sampleOffVec;
80 // MontecarloSampling(mSur, sampleSurVec,50000);
81  MontecarloSampling(mOff, sampleOffVec,50000);
82  sampleSurVec.insert( sampleSurVec.end(), sampleOffVec.begin(), sampleOffVec.end() );
83 
84 
85  printf("Read %i vn %i fn \n",mOff.vn, mOff.fn);
86 
87  float poissonRadius = mOff.bbox.Diag()*atof(argv[3]);
88 // float poissonRadius = mOff.bbox.Diag()/10;
89  printf("Poisson Radius %f\n",poissonRadius);
90 
91  float sampleSurfRadius=mOff.bbox.Diag()/100.0f;
92  int montecarloSampleNum = 100000;
93 
94  MyMesh seedM;
95  VoronoiVolumeSampling<MyMesh> vvs(mOff, seedM);
96  printf("Sampling Surface at a radius %f ",sampleSurfRadius);
97  vvs.Init(sampleSurfRadius);
98  tri::BuildMeshFromCoordVector(vvs.seedDomainMesh, sampleSurVec);
99  printf("Sampled\n");
100  vvs.BuildVolumeSampling(montecarloSampleNum,0,poissonRadius);
101 
102 
103  tri::io::ExporterPLY<MyMesh>::Save(vvs.seedDomainMesh,"seedDomainMesh.ply");
104  tri::io::ExporterPLY<MyMesh>::Save(vvs.poissonSurfaceMesh,"poissonSurfaceMesh.ply");
105  tri::io::ExporterPLY<MyMesh>::Save(vvs.montecarloVolumeMesh,"montecarloVolumeMesh.ply");
106  tri::io::ExporterPLY<MyMesh>::Save(seedM,"seedMesh0.ply");
107 // vvs.restrictedRelaxationFlag=true;
108 // vvs.BarycentricRelaxVoronoiSamples(10);
109  vvs.QuadricRelaxVoronoiSamples(10);
110 
112  tri::io::ExporterPLY<MyMesh>::Save(seedM,"seedMesh1.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY );
113  vvs.QuadricRelaxVoronoiSamples(10);
114 
116  tri::io::ExporterPLY<MyMesh>::Save(seedM,"seedMesh2.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY );
117  printf("\n Saved %i points \n",seedM.vn);
118  // Second Pipeline
119  return true;
120 
121  return 0;
122 }