VCG Library
trimesh_voronoisampling.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 
33 
34 using namespace vcg;
35 using namespace std;
36 
37 class MyEdge;
38 class MyFace;
39 class MyVertex;
40 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
41  Use<MyEdge> ::AsEdgeType,
42  Use<MyFace> ::AsFaceType>{};
43 
44 class MyVertex : public Vertex<MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::VFAdj, vertex::Qualityf, vertex::Color4b, vertex::BitFlags >{};
45 class MyFace : public Face< MyUsedTypes, face::VertexRef, face::Normal3f, face::BitFlags, face::Mark, face::VFAdj, face::FFAdj > {};
46 class MyEdge : public Edge< MyUsedTypes, edge::VertexRef, edge::BitFlags>{};
47 class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyEdge>, vector<MyFace> > {};
48 
49 class EmEdge;
50 class EmFace;
51 class EmVertex;
52 struct EmUsedTypes : public UsedTypes< Use<EmVertex> ::AsVertexType,
53  Use<EmEdge> ::AsEdgeType,
54  Use<EmFace> ::AsFaceType>{};
55 
56 class EmVertex : public Vertex<EmUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::VFAdj , vertex::Qualityf, vertex::Color4b, vertex::BitFlags >{};
57 class EmFace : public Face< EmUsedTypes, face::VertexRef, face::BitFlags, face::VFAdj > {};
58 class EmEdge : public Edge< EmUsedTypes, edge::VertexRef> {};
59 class EmMesh : public tri::TriMesh< vector<EmVertex>, vector<EmEdge>, vector<EmFace> > {};
60 
61 
62 int main( int argc, char **argv )
63 {
64  MyMesh baseMesh;
65  if(argc < 4 )
66  {
67  printf("Usage trimesh_voronoisampling mesh sampleNum iterNum\n");
68  return -1;
69  }
70  int sampleNum = atoi(argv[2]);
71  int iterNum = atoi(argv[3]);
72 
73  bool fixCornerFlag=true;
74  bool uniformEdgeSamplingFlag = true;
75 
76  printf("Reading %s and sampling %i points with %i iteration\n",argv[1],sampleNum,iterNum);
77  int ret= tri::io::ImporterPLY<MyMesh>::Open(baseMesh,argv[1]);
78  if(ret!=0)
79  {
80  printf("Unable to open %s for '%s'\n",argv[1],tri::io::ImporterPLY<MyMesh>::ErrorMsg(ret));
81  return -1;
82  }
83 
84  tri::VoronoiProcessingParameter vpp;
85 
89 
91  float radius = tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::ComputePoissonDiskRadius(baseMesh,sampleNum);
92  tri::VoronoiProcessing<MyMesh>::PreprocessForVoronoi(baseMesh,radius,vpp);
93 
96 
97 
98  // -- Build a sampling with just corners (Poisson filtered)
99  MyMesh poissonCornerMesh;
100  std::vector<Point3f> sampleVec;
101  tri::TrivialSampler<MyMesh> mps(sampleVec);
102  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::VertexBorderCorner(baseMesh,mps,math::ToRad(150.f));
103  tri::BuildMeshFromCoordVector(poissonCornerMesh,sampleVec);
104  tri::io::ExporterPLY<MyMesh>::Save(poissonCornerMesh,"cornerMesh.ply");
105  sampleVec.clear();
106  MyMesh borderMesh,poissonBorderMesh;
107 
108 
109  if(uniformEdgeSamplingFlag)
110  {
111 
112  }
113  else
114  {
115  if(fixCornerFlag)
116  {
117  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::PoissonDiskPruning(mps, poissonCornerMesh, radius, pp);
118  tri::BuildMeshFromCoordVector(poissonCornerMesh,sampleVec);
119  tri::io::ExporterPLY<MyMesh>::Save(poissonCornerMesh,"poissonCornerMesh.ply");
120  // Now save the corner as Fixed Seeds for later...
121  std::vector<MyVertex *> fixedSeedVec;
122  tri::VoronoiProcessing<MyMesh>::SeedToVertexConversion(baseMesh,sampleVec,fixedSeedVec);
123  tri::VoronoiProcessing<MyMesh, tri::EuclideanDistance<MyMesh> >::MarkVertexVectorAsFixed(baseMesh,fixedSeedVec);
124  vpp.preserveFixedSeed=true;
125  }
126 
127  // -- Build a sampling with points on the border
128  sampleVec.clear();
129  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::VertexBorder(baseMesh,mps);
130  tri::BuildMeshFromCoordVector(borderMesh,sampleVec);
131  tri::io::ExporterPLY<MyMesh>::Save(borderMesh,"borderMesh.ply");
132 
133  // -- and then prune the border sampling with poisson strategy using the precomputed corner vertexes.
134  pp.preGenMesh = &poissonCornerMesh;
135  pp.preGenFlag=true;
136  sampleVec.clear();
137  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::PoissonDiskPruning(mps, borderMesh, radius*0.8f, pp);
138  tri::BuildMeshFromCoordVector(poissonBorderMesh,sampleVec);
139  }
140 
141  tri::io::ExporterPLY<MyMesh>::Save(poissonBorderMesh,"PoissonEdgeMesh.ply");
142 
143  // -- Build the montercarlo sampling of the surface
144  MyMesh MontecarloSurfaceMesh;
145  sampleVec.clear();
146  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::Montecarlo(baseMesh,mps,50000);
147  tri::BuildMeshFromCoordVector(MontecarloSurfaceMesh,sampleVec);
148  tri::io::ExporterPLY<MyMesh>::Save(MontecarloSurfaceMesh,"MontecarloSurfaceMesh.ply");
149 
150  // -- Prune the montecarlo sampling with poisson strategy using the precomputed vertexes on the border.
151  pp.preGenMesh = &poissonBorderMesh;
152  sampleVec.clear();
153  tri::SurfaceSampling<MyMesh,tri::TrivialSampler<MyMesh> >::PoissonDiskPruning(mps, MontecarloSurfaceMesh, radius, pp);
154  MyMesh PoissonMesh;
155  tri::BuildMeshFromCoordVector(PoissonMesh,sampleVec);
156  tri::io::ExporterPLY<MyMesh>::Save(PoissonMesh,"PoissonMesh.ply");
157 
158  std::vector<MyVertex *> seedVec;
159  tri::VoronoiProcessing<MyMesh>::SeedToVertexConversion(baseMesh,sampleVec,seedVec);
160 
161  // Select all the vertexes on the border to define a constrained domain.
162  // In our case we select the border vertexes to make sure that the seeds on the border
163  // relax themselves remaining on the border
164  for(size_t i=0;i<baseMesh.vert.size();++i){
165  if(baseMesh.vert[i].IsB())
166  baseMesh.vert[i].SetS();
167  }
168 
169 // vpp.deleteUnreachedRegionFlag=true;
170  vpp.deleteUnreachedRegionFlag=false;
171  vpp.triangulateRegion = false;
172  vpp.geodesicRelaxFlag=false;
173  vpp.constrainSelectedSeed=true;
174 
175  tri::EuclideanDistance<MyMesh> dd;
176  int t0=clock();
177  // And now, at last, the relaxing procedure!
178  int actualIter = tri::VoronoiProcessing<MyMesh, tri::EuclideanDistance<MyMesh> >::VoronoiRelaxing(baseMesh, seedVec, iterNum, dd, vpp);
179  int t1=clock();
180 
181  MyMesh voroMesh, voroPoly, delaMesh;
182  // Get the result in some pleasant form converting it to a real voronoi diagram.
183  if(tri::VoronoiProcessing<MyMesh>::CheckVoronoiTopology(baseMesh,seedVec))
184  tri::VoronoiProcessing<MyMesh>::ConvertVoronoiDiagramToMesh(baseMesh,voroMesh,voroPoly,seedVec, vpp);
185  else
186  printf("WARNING some voronoi region are not disk like; the resulting delaunay triangulation is not manifold.\n");
187 
188  tri::io::ExporterPLY<MyMesh>::Save(baseMesh,"base.ply",tri::io::Mask::IOM_VERTCOLOR + tri::io::Mask::IOM_VERTQUALITY );
189  tri::io::ExporterPLY<MyMesh>::Save(voroMesh,"voroMesh.ply",tri::io::Mask::IOM_VERTCOLOR + tri::io::Mask::IOM_FLAGS );
190  tri::io::ExporterPLY<MyMesh>::Save(voroPoly,"voroPoly.ply",tri::io::Mask::IOM_VERTCOLOR| tri::io::Mask::IOM_EDGEINDEX ,false);
191 
192  tri::VoronoiProcessing<MyMesh>::ConvertDelaunayTriangulationToMesh(baseMesh,delaMesh, seedVec);
193  tri::io::ExporterPLY<MyMesh>::Save(delaMesh,"delaMesh.ply",tri::io::Mask::IOM_VERTCOLOR + tri::io::Mask::IOM_VERTQUALITY );
194  tri::VoronoiProcessing<MyMesh>::RelaxRefineTriangulationSpring(baseMesh,delaMesh,2,10);
195  tri::io::ExporterPLY<MyMesh>::Save(delaMesh,"delaMeshRef.ply",tri::io::Mask::IOM_VERTCOLOR + tri::io::Mask::IOM_VERTQUALITY );
196 
197 
198 // tri::io::ImporterPLY<MyMesh>::Open(baseMesh,argv[1]);
199 // tri::UpdateTopology<MyMesh>::VertexFace(baseMesh);
200 // tri::PoissonSampling<MyMesh>(baseMesh,pointVec,sampleNum,radius,radiusVariance);
201 // tri::VoronoiProcessing<MyMesh>::SeedToVertexConversion(baseMesh,pointVec,seedVec);
202 // tri::IsotropicDistance<MyMesh> id(baseMesh,radiusVariance);
203 // tri::VoronoiProcessing<MyMesh, tri::IsotropicDistance<MyMesh> >::VoronoiRelaxing(baseMesh, seedVec, iterNum,id,vpp);
204 // tri::VoronoiProcessing<MyMesh, tri::IsotropicDistance<MyMesh> >::ConvertVoronoiDiagramToMesh(baseMesh,outMesh,polyMesh,seedVec, id, vpp);
205 
206 // tri::io::ExporterPLY<MyMesh>::Save(outMesh,"outW.ply",tri::io::Mask::IOM_VERTCOLOR );
207 // tri::io::ExporterPLY<MyMesh>::Save(polyMesh,"polyW.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_EDGEINDEX,false);
208 // tri::io::ExporterDXF<MyMesh>::Save(polyMesh,"outW.dxf");
209  printf("Completed! %i (%i) iterations in %f sec for %lu seeds \n",actualIter, iterNum,float(t1-t0)/CLOCKS_PER_SEC,seedVec.size());
210  return 0;
211 }