VCG Library
trimesh_voronoi.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 #include <stdio.h>
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::Mark, face::BitFlags, 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 int main( int argc, char **argv )
50 {
51  MyMesh baseMesh,voronoiMesh, voronoiPoly, delaunayMesh;
52  if(argc < 6 )
53  {
54  printf("Usage: trimesh_voronoi mesh [sampleNum] voronoiRelaxIter delaunayRefinementStep delaunayRelaxStep \n");
55  return -1;
56  }
57  int sampleNum = atoi(argv[2]);
58  int iterNum = atoi(argv[3]);
59 
60  int refineStep = atoi(argv[4]);
61  int relaxStep = atoi(argv[5]);
62 
63  int t0=clock();
64  int ret= tri::io::ImporterPLY<MyMesh>::Open(baseMesh,argv[1]);
65  if(ret!=0)
66  {
67  printf("Unable to open %s for '%s'\n",argv[1],tri::io::ImporterPLY<MyMesh>::ErrorMsg(ret));
68  return -1;
69  }
70  tri::VoronoiProcessingParameter vpp;
71 
72  tri::io::ImporterPLY<MyMesh>::Open(baseMesh,argv[1]);
73  int t1=clock();
74  printf("Read %30s (%7i vn %7i fn) in %6.3f \n",argv[1],baseMesh.vn,baseMesh.fn,float(t1-t0)/CLOCKS_PER_SEC);
75 
76 
77  vector<Point3f> pointVec;
78  float radius;
79  tri::PoissonSampling<MyMesh>(baseMesh,pointVec,sampleNum,radius);
80  vector<MyVertex *> seedVec;
81  tri::VoronoiProcessing<MyMesh>::PreprocessForVoronoi(baseMesh,radius,vpp);
82  tri::VoronoiProcessing<MyMesh>::SeedToVertexConversion(baseMesh,pointVec,seedVec);
83 
84  int t2=clock();
85  printf("Preprocessed %30s (%7i vn %7i fn) Computed %i seed (asked %i) (radius %f) in %6.3f\n",
86  argv[1],baseMesh.vn,baseMesh.fn, seedVec.size(), sampleNum, radius, float(t2-t1)/CLOCKS_PER_SEC);
87 
88  tri::EuclideanDistance<MyMesh> df;
89  vpp.geodesicRelaxFlag=false;
90  int actualIter = tri::VoronoiProcessing<MyMesh>::VoronoiRelaxing(baseMesh, seedVec, iterNum, df, vpp);
91 
92  int t3=clock();
93  printf("relaxed %lu seeds for %i(up to %i) iterations in %f secs\n",
94  seedVec.size(), actualIter, iterNum,float(t3-t2)/CLOCKS_PER_SEC);
95 
96  tri::io::ExporterPLY<MyMesh>::Save(baseMesh,"baseMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY );
97  if(tri::VoronoiProcessing<MyMesh>::CheckVoronoiTopology(baseMesh,seedVec))
98  {
99  tri::VoronoiProcessing<MyMesh>::ConvertVoronoiDiagramToMesh(baseMesh,voronoiMesh,voronoiPoly,seedVec, vpp);
100  }
101  else
102  {
103  printf("WARNING some voronoi region are not disk like; the resulting delaunay triangulation is not manifold.\n");
104  refineStep=1;
105  }
106 
107  tri::VoronoiProcessing<MyMesh>::ConvertDelaunayTriangulationToMesh(baseMesh,delaunayMesh,seedVec,true);
108  tri::io::ExporterPLY<MyMesh>::Save(delaunayMesh,"delaunayBaseMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTFLAGS,false );
109  tri::VoronoiProcessing<MyMesh>::RelaxRefineTriangulationSpring(baseMesh,delaunayMesh,refineStep,relaxStep);
110 
111  int t4=clock();
112  printf("Refined %i times and relaxed %i to a %i v %i f mesh in %f secs\n",
113  refineStep, relaxStep, delaunayMesh.vn,delaunayMesh.fn,float(t4-t3)/CLOCKS_PER_SEC);
114 
115  tri::io::ExporterPLY<MyMesh>::Save(baseMesh,"baseMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTQUALITY );
116  tri::io::ExporterPLY<MyMesh>::Save(voronoiMesh,"voronoiMesh.ply",tri::io::Mask::IOM_VERTCOLOR );
117  tri::io::ExporterPLY<MyMesh>::Save(delaunayMesh,"delaunayMesh.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_VERTFLAGS,false );
118  tri::io::ExporterPLY<MyMesh>::Save(voronoiPoly,"voronoiPoly.ply",tri::io::Mask::IOM_VERTCOLOR | tri::io::Mask::IOM_EDGEINDEX,false);
119  return 0;
120 }