VCG Library
polygonmesh_smooth.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 <vcg/complex/algorithms/clean.h>
27 #include <vcg/complex/algorithms/create/platonic.h>
28 #include <vcg/complex/algorithms/polygonal_algorithms.h>
29 
30 #include <wrap/io_trimesh/export_obj.h>
31 #include <wrap/io_trimesh/import_ply.h>
32 #include <wrap/io_trimesh/export_off.h>
33 #include <wrap/io_trimesh/import_off.h>
34 
35 using namespace vcg;
36 using namespace std;
37 
38 // forward declarations
39 class TFace;
40 class TVertex;
41 
42 struct TUsedTypes: public vcg::UsedTypes< vcg::Use<TVertex>::AsVertexType, vcg::Use<TFace>::AsFaceType >{};
43 
44 /* Definition of a mesh of triangles
45 */
46 class TVertex : public Vertex< TUsedTypes,
47  vertex::BitFlags,
48  vertex::Coord3f,
49  vertex::Normal3f,
50  vertex::Mark >{};
51 
52 class TFace : public Face< TUsedTypes,
53  face::VertexRef, // three pointers to vertices
54  face::Normal3f, // normal
55  face::BitFlags, // flags
56  face::Mark, // incremental mark
57  face::FFAdj // three pointers to adjacent faces
58 > {};
59 
60 /* the mesh is a container of vertices and a container of faces */
61 class TMesh : public vcg::tri::TriMesh< vector<TVertex>, vector<TFace> > {};
62 
63 
64 /* Definition of a mesh of polygons that also supports half-edges
65 */
66 class PFace;
67 class PVertex;
68 
69 struct PUsedTypes: public vcg::UsedTypes<vcg::Use<PVertex> ::AsVertexType,
70  vcg::Use<PFace> ::AsFaceType>{};
71 
72 class PVertex:public vcg::Vertex< PUsedTypes,
73  vcg::vertex::Coord3f,
74  vcg::vertex::Normal3f,
75  vcg::vertex::Mark,
76  vcg::vertex::Qualityf,
77  vcg::vertex::Mark,
78  vcg::vertex::BitFlags>{} ;
79 
80 class PFace:public vcg::Face<
81  PUsedTypes
82  ,vcg::face::PolyInfo // this is necessary if you use component in vcg/simplex/face/component_polygon.h
83  // It says "this class is a polygon and the memory for its components (e.g. pointer to its vertices
84  // will be allocated dynamically")
85  ,vcg::face::PFVAdj // Pointer to the vertices (just like FVAdj )
86  ,vcg::face::PFVAdj
87  ,vcg::face::PFFAdj // Pointer to edge-adjacent face (just like FFAdj )
88  ,vcg::face::BitFlags // bit flags
89  ,vcg::face::Qualityf // quality
90  ,vcg::face::Mark // incremental mark
91  ,vcg::face::Normal3f // normal
92 > {};
93 
94 class PMesh: public
95  vcg::tri::TriMesh<
96  std::vector<PVertex>, // the vector of vertices
97  std::vector<PFace > // the vector of faces
98  >{};
99 
100 using namespace vcg;
101 
102 int main(int, char **)
103 {
104  TMesh tm;
105  PMesh pm;
106  math::MarsenneTwisterRNG RndGen;
107  tri::io::ImporterOFF<PMesh>::Open(pm,"../../meshes/fertility_quad.off");
110  // randomly displace half of the vertices
111  float randScale = pm.bbox.Diag()/200.0f;
112  ForEachVertex(pm,[&](PVertex &v){
113  if(v.P()[0] < pm.bbox.Center()[0])
114  v.P() += math::GeneratePointInUnitBallUniform<float,math::MarsenneTwisterRNG>(RndGen)*randScale;
115  });
116 
117  // Select half of the vertices
118  ForEachVertex(pm,[&](PVertex &v){
119  if(v.P()[2] < pm.bbox.Center()[2])
120  v.SetS();
121  });
122  printf("Input quad mesh has %i %i (sel %i)\n",pm.VN(), pm.FN(), tri::UpdateSelection<PMesh>::VertexCount(pm) );
123 
124  tri::io::ImporterPLY<TMesh>::Open(tm,"../../meshes/fertility_tri.ply");
126  printf("Input tri mesh has %i %i\n",tm.VN(), tm.FN());
127  tri::io::ExporterOFF<PMesh>::Save(pm,"./smooth.off");
128  PolygonalAlgorithm<PMesh>::LaplacianReproject(pm,tm,100,0.6,0.3,true);
129  tri::io::ExporterOFF<PMesh>::Save(pm,"./smooth2.off");
130 }
131 
132