VCG Library
polygonmesh_optimize.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 
24 #include <vcg/complex/complex.h>
25 
26 /*include the algorithms for updating: */
27 #include <vcg/complex/algorithms/update/bounding.h>
28 #include <vcg/complex/algorithms/update/normal.h>
29 
30 #include <vcg/complex/algorithms/clean.h>
31 #include <vcg/complex/algorithms/create/platonic.h>
32 
33 #include <wrap/io_trimesh/export_obj.h>
34 #include <wrap/io_trimesh/import_ply.h>
35 
36 #include <vcg/complex/algorithms/dual_meshing.h>
37 
38 #include <vcg/complex/algorithms/polygon_support.h>
39 
40 #include <vcg/complex/algorithms/polygonal_algorithms.h>
41 
42 #include <vcg/complex/algorithms/update/quality.h>
43 
44 using namespace vcg;
45 using namespace std;
46 
47 // forward declarations
48 class TFace;
49 class TVertex;
50 
51 struct TUsedTypes: public vcg::UsedTypes< vcg::Use<TVertex>::AsVertexType, vcg::Use<TFace>::AsFaceType >{};
52 
53 /* Definition of a mesh of triangles
54 */
55 class TVertex : public Vertex< TUsedTypes,
56  vertex::BitFlags,
57  vertex::Coord3f,
58  vertex::Normal3f,
59  vertex::Mark >{};
60 
61 class TFace : public Face< TUsedTypes,
62  face::VertexRef, // three pointers to vertices
63  face::Normal3f, // normal
64  face::BitFlags, // flags
65  face::FFAdj // three pointers to adjacent faces
66 > {};
67 
68 /* the mesh is a container of vertices and a container of faces */
69 class TMesh : public vcg::tri::TriMesh< vector<TVertex>, vector<TFace> > {};
70 
71 
72 /* Definition of a mesh of polygons that also supports half-edges
73 */
74 class PFace;
75 class PVertex;
76 
77 struct PUsedTypes: public vcg::UsedTypes<vcg::Use<PVertex> ::AsVertexType,
78  vcg::Use<PFace> ::AsFaceType>{};
79 
80 class PVertex:public vcg::Vertex< PUsedTypes,
81  vcg::vertex::Coord3f,
82  vcg::vertex::Normal3f,
83  vcg::vertex::Mark,
84  vcg::vertex::BitFlags>{} ;
85 
86 class PFace:public vcg::Face<
87  PUsedTypes
88  ,vcg::face::PolyInfo // this is necessary if you use component in vcg/simplex/face/component_polygon.h
89  // It says "this class is a polygon and the memory for its components (e.g. pointer to its vertices
90  // will be allocated dynamically")
91  ,vcg::face::PFVAdj // Pointer to the vertices (just like FVAdj )
92  ,vcg::face::PFVAdj
93  ,vcg::face::PFFAdj // Pointer to edge-adjacent face (just like FFAdj )
94  ,vcg::face::BitFlags // bit flags
95  ,vcg::face::Normal3f // normal
96  ,face::Qualityd // face quality
97 > {};
98 
99 class PMesh: public
100  vcg::tri::TriMesh<
101  std::vector<PVertex>, // the vector of vertices
102  std::vector<PFace > // the vector of faces
103  >{};
104 
105 TMesh primalT;
106 PMesh primal,dual,dual_opt,dual_flat;
107 
108 int main(int argc, char *argv[])
109 {
110  assert(argc>1);
111 
112  std::cout<<"Opening file " << argv[0] << std::endl;
113 
114  int res=vcg::tri::io::ImporterPLY<TMesh>::Open(primalT,argv[1]);
115  assert(res==vcg::ply::E_NOERROR);
116  vcg::tri::PolygonSupport<TMesh,PMesh>::ImportFromTriMesh(primal,primalT);
117 
118  vcg::tri::DualMeshing<PMesh>::MakeDual(primal,dual);
119  vcg::tri::io::ExporterOBJ<PMesh>::Save(dual,"./dual_no_optimized.obj",vcg::tri::io::Mask::IOM_BITPOLYGONAL);
120 
121  //copy the mesh
123  vcg::tri::Append<PMesh,PMesh>::Mesh(dual_flat,dual);
124 
125  vcg::PolygonalAlgorithm<PMesh>::SmoothReprojectPCA(dual_opt);
126 
127  vcg::tri::io::ExporterOBJ<PMesh>::Save(dual_opt,"./dual_optimized.obj",vcg::tri::io::Mask::IOM_BITPOLYGONAL);
128 
129  vcg::PolygonalAlgorithm<PMesh>::FlattenFaces(dual_flat);
130  vcg::tri::io::ExporterOBJ<PMesh>::Save(dual_flat,"./dual_flattened.obj",vcg::tri::io::Mask::IOM_BITPOLYGONAL);
131 
132  //update the quality as template
133  std::pair<typename PMesh::ScalarType,typename PMesh::ScalarType> minmax_dual,minmax_dual_opt,minmax_dual_flat;
134  typename PMesh::ScalarType Avg_dual,Avg_dual_opt,Avg_dual_flat;
135 
136  vcg::PolygonalAlgorithm<PMesh>::UpdateQuality(dual,PolygonalAlgorithm<PMesh>::QTemplate);
137  minmax_dual=tri::Stat<PMesh>::ComputePerFaceQualityMinMax(dual);
138  Avg_dual=tri::Stat<PMesh>::ComputePerFaceQualityAvg(dual);
139 
140  vcg::PolygonalAlgorithm<PMesh>::UpdateQuality(dual_opt,PolygonalAlgorithm<PMesh>::QTemplate);
141  minmax_dual_opt=tri::Stat<PMesh>::ComputePerFaceQualityMinMax(dual_opt);
142  Avg_dual_opt=tri::Stat<PMesh>::ComputePerFaceQualityAvg(dual_opt);
143 
144  vcg::PolygonalAlgorithm<PMesh>::UpdateQuality(dual_flat,PolygonalAlgorithm<PMesh>::QTemplate);
145  minmax_dual_flat=tri::Stat<PMesh>::ComputePerFaceQualityMinMax(dual_flat);
146  Avg_dual_flat=tri::Stat<PMesh>::ComputePerFaceQualityAvg(dual_flat);
147 
148  std::cout<<std::endl<<std::endl<<"Template Quality No Optimized min / max " <<minmax_dual.first<<" / "<<minmax_dual.second<<std::endl;
149  std::cout<<"Template Quality Optimized min / max " <<minmax_dual_opt.first<<" / "<<minmax_dual_opt.second<<std::endl;
150  std::cout<<"Template Quality Flattened min / max " <<minmax_dual_flat.first<<" / "<<minmax_dual_flat.second<<std::endl<<std::endl;
151 
152  std::cout<<"Template Quality No Optimized Average " <<Avg_dual<<std::endl;
153  std::cout<<"Template Quality Optimized Average " <<Avg_dual_opt<<std::endl;
154  std::cout<<"Template Quality Flattened Average " <<Avg_dual_flat<<std::endl<<std::endl<<std::endl<<std::endl;
155 
156 
157  vcg::PolygonalAlgorithm<PMesh>::UpdateQuality(dual,PolygonalAlgorithm<PMesh>::QPlanar);
158  minmax_dual=tri::Stat<PMesh>::ComputePerFaceQualityMinMax(dual);
159  Avg_dual=tri::Stat<PMesh>::ComputePerFaceQualityAvg(dual);
160 
161  vcg::PolygonalAlgorithm<PMesh>::UpdateQuality(dual_opt,PolygonalAlgorithm<PMesh>::QPlanar);
162  minmax_dual_opt=tri::Stat<PMesh>::ComputePerFaceQualityMinMax(dual_opt);
163  Avg_dual_opt=tri::Stat<PMesh>::ComputePerFaceQualityAvg(dual_opt);
164 
165  vcg::PolygonalAlgorithm<PMesh>::UpdateQuality(dual_flat,PolygonalAlgorithm<PMesh>::QPlanar);
166  minmax_dual_flat=tri::Stat<PMesh>::ComputePerFaceQualityMinMax(dual_flat);
167  Avg_dual_flat=tri::Stat<PMesh>::ComputePerFaceQualityAvg(dual_flat);
168 
169  std::cout<<"Flatness Quality No Optimized min / max " <<minmax_dual.first<<" / "<<minmax_dual.second<<std::endl;
170  std::cout<<"Flatness Quality Optimized min / max " <<minmax_dual_opt.first<<" / "<<minmax_dual_opt.second<<std::endl;
171  std::cout<<"Flatness Quality Flattened min / max " <<minmax_dual_flat.first<<" / "<<minmax_dual_flat.second<<std::endl<<std::endl;
172 
173  std::cout<<"Flatness Quality No Optimized Average " <<Avg_dual<<std::endl;
174  std::cout<<"Flatness Quality Optimized Average " <<Avg_dual_opt<<std::endl;
175  std::cout<<"Flatness Quality Flattened Average " <<Avg_dual_flat<<std::endl;
176 
177 }
178 
179 
180