VCG Library
trimesh_voronoiclustering.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_ply.h>
28 #include<vcg/complex/algorithms/point_sampling.h>
29 #include<vcg/complex/algorithms/voronoi_processing.h>
30 
31 
32 using namespace vcg;
33 using namespace std;
34 
35 class MyEdge;
36 class MyFace;
37 class MyVertex;
38 struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
39  Use<MyEdge> ::AsEdgeType,
40  Use<MyFace> ::AsFaceType>{};
41 
42 class MyVertex : public Vertex<MyUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::VFAdj , vertex::Qualityf, vertex::Color4b, vertex::BitFlags >{};
43 class MyFace : public Face< MyUsedTypes, face::VertexRef, face::BitFlags, face::VFAdj > {};
44 class MyEdge : public Edge< MyUsedTypes>{};
45 class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyFace> , vector<MyEdge> > {};
46 
47 
48 
49 int main( int argc, char **argv )
50 {
51  MyMesh baseMesh, clusteredMesh;
52  if(argc < 4 )
53  {
54  printf("Usage trimesh_voronoiclustering mesh region_num iterNum\n");
55  return -1;
56  }
57  int seedNum = atoi(argv[2]);
58  int iterNum = atoi(argv[3]);
59  printf("Reading %s and sampling %i \n",argv[1],seedNum);
60  int ret= tri::io::ImporterPLY<MyMesh>::Open(baseMesh,argv[1]);
61  if(ret!=0)
62  {
63  printf("Unable to open %s for '%s'\n",argv[1],tri::io::ImporterPLY<MyMesh>::ErrorMsg(ret));
64  return -1;
65  }
66 
67  int randSeed=time(0);
69  tri::TrivialPointerSampler<MyMesh> cs;
70  tri::SurfaceSampling<MyMesh, tri::TrivialPointerSampler<MyMesh> >::SamplingRandomGenerator().initialize(randSeed);
71  tri::SurfaceSampling<MyMesh, tri::TrivialPointerSampler<MyMesh> >::VertexUniform(baseMesh,cs,seedNum);
72  tri::VoronoiProcessingParameter vpp;
73  tri::EuclideanDistance<MyMesh> df;
74  tri::VoronoiProcessing<MyMesh>::VoronoiRelaxing(baseMesh, cs.sampleVec, iterNum, df, vpp);
75  tri::VoronoiProcessing<MyMesh>::TopologicalVertexColoring(baseMesh, cs.sampleVec);
76  tri::VoronoiProcessing<MyMesh>::ConvertDelaunayTriangulationToMesh(baseMesh,clusteredMesh,cs.sampleVec);
77 
78  tri::io::ExporterPLY<MyMesh>::Save(baseMesh,"base.ply",tri::io::Mask::IOM_VERTCOLOR );
79  tri::io::ExporterPLY<MyMesh>::Save(clusteredMesh,"clustered.ply");
80  return 0;
81 }