VCG Library
polygonmesh.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 
29 #include <wrap/io_trimesh/import.h>
30 #include <wrap/io_trimesh/export_ply.h>
31 
32 /* include the support for half edges */
33 #include <vcg/complex/algorithms/update/halfedge_indexed.h>
34 
35 
36 using namespace vcg;
37 using namespace std;
38 
39 // forward declarations
40 class TFace;
41 class TVertex;
42 
43 struct TUsedTypes: public vcg::UsedTypes< vcg::Use<TVertex>::AsVertexType, vcg::Use<TFace>::AsFaceType >{};
44 
45 
46 
47 /* Definition of a mesh of triangles
48 */
49 class TVertex : public Vertex< TUsedTypes,
50  vertex::BitFlags,
51  vertex::Coord3f,
52  vertex::Normal3f,
53  vertex::Mark >{};
54 
55 class TFace : public Face< TUsedTypes,
56  face::VertexRef, // three pointers to vertices
57  face::Normal3f, // normal
58  face::BitFlags, // flags
59  face::FFAdj // three pointers to adjacent faces
60 > {};
61 
62 /* the mesh is a container of vertices and a container of faces */
63 class TMesh : public vcg::tri::TriMesh< vector<TVertex>, vector<TFace> > {};
64 
65 
66 /* Definition of a mesh of polygons that also supports half-edges
67 */
68 class PFace;
69 class PVertex;
70 class PHEdge;
71 class PEdge;
72 
73 struct PUsedTypes: public vcg::UsedTypes<vcg::Use<PVertex> ::AsVertexType,
74  vcg::Use<PEdge> ::AsEdgeType,
75  vcg::Use<PHEdge>::AsHEdgeType,
76  vcg::Use<PFace> ::AsFaceType
77  >{};
78 
79 //class DummyEdge: public vcg::Edge<PolyUsedTypes>{};
80 class PVertex:public vcg::Vertex< PUsedTypes,
81  vcg::vertex::Coord3f,
82  vcg::vertex::Normal3f,
83  vcg::vertex::Mark,
84  vcg::vertex::BitFlags,
85  vcg::vertex::VHAdj>{} ;
86 
87 class PEdge : public Edge<PUsedTypes>{};
88 class PHEdge : public HEdge< PUsedTypes, hedge::BitFlags,
89  //hedge::HFAdj, // pointer to the face
90  //hedge::HOppAdj, // pointer to the opposite edge
91  //hedge::HVAdj, // pointer to the vertex
92  //hedge::HNextAdj, // pointer to the next halfedge
93  hedge::HEdgeData // the previous 4 components (just more handy, you can comment this and uncomment the previous four lines)
94  //,hedge::HPrevAdj // pointer to the previous halfedge
95 >{};
96 
97 class PFace:public vcg::Face<
98  PUsedTypes
99  ,vcg::face::PolyInfo // this is necessary if you use component in vcg/simplex/face/component_polygon.h
100  // It says "this class is a polygon and the memory for its components (e.g. pointer to its vertices
101  // will be allocated dynamically")
102  ,vcg::face::PFVAdj // Pointer to the vertices (just like FVAdj )
103  ,vcg::face::PFVAdj
104  ,vcg::face::PFFAdj // Pointer to edge-adjacent face (just like FFAdj )
105  ,vcg::face::PFHAdj // Pointer its half -edges ( you may need this if you use half edges)
106  ,vcg::face::BitFlags // bit flags
107  ,vcg::face::Normal3f // normal
108 > {};
109 
110 class PMesh: public
111  vcg::tri::TriMesh<
112  std::vector<PVertex>, // the vector of vertices
113  std::vector<PFace >, // the vector of faces
114  std::vector<PHEdge> , // the vector of edges
115  std::vector<PEdge> // the vector of edges
116  >{};
117 
118 PMesh pm;
119 TMesh tm0;
120 
121 int main(int /*argc*/, char *argv[]) {
122 
123  int loadmask;
124 
125 if(true){
126  /*
127  first way:
128  1) read a polygon mesh that will be automatically converted in a triangle mesh tagging
129  the internal edges (i.e. the edges that have been added for triangulating the polygons)
130  2) make some cleaning
131  3) import the tagged triangle mesh in a polygon mesh
132  */
133 // vcg::tri::io::ImporterOBJ<CMesh>::Open(mesh,argv[1],loadmask);
134 // vcg::tri::io::ImporterOFF<TMesh>::Open(tm0,argv[1],loadmask);
135  vcg::tri::Hexahedron(tm0);
143 
144  // create a polygon meshe from a trimesh with tagged faces
145  vcg::tri::PolygonSupport<TMesh,PMesh>::ImportFromTriMesh(pm,tm0);
146 }
147 else
148 {
149  /* second way:
150  Load into a polygon mesh straight away.
151  */
152  vcg::tri::io::ImporterOBJ<PMesh>::Open(pm,argv[1],loadmask);
157 }
158 
159 
160  // compute the half edges because I'm a half-edge programmer
162 
163  // .... my half edge based code ......
164 
165  // check for consistency
167 
168  int size = pm.face.size();
169 
170  // add a face to each face with more than 3 vertices ( just one pass)
171 
172  for(int i = 0; i < size; ++i)
173  if(!(pm.face[i].IsD()))
174  if(pm.face[i].VN()>3){
175  PMesh::HEdgePointer ef = pm.face[i].FHp();
176  PMesh::HEdgePointer ef1 = ef -> HNp();
177  ef1 = ef1->HNp();
179  }
181  size = pm.face.size();
182 
183  // remove an edge for each face
184 
185  for(int i = 0; i < size; ++i)
186  if(!(pm.face[i].IsD() ))
187  {
188  PMesh::HEdgePointer ef = pm.face[i].FHp();
189  if( ef->HOp()->HFp() !=NULL){
191  }
192  }
193 
194  // check for consistency
196 
197  // recompute indexed data structure from the half edge data structure
198 // vcg::tri::UpdateIndexed<PMesh>::FromHalfEdges(pm );
199 
200  // create a triangle mesh from a polygon mesh
201  TMesh tm1;
202  vcg::tri::PolygonSupport<TMesh,PMesh>::ImportFromPolyMesh(tm1,pm);
203 
204  vcg::tri::io::PlyInfo pi;
205  vcg::tri::io::ExporterPLY<TMesh>::Save(tm1,"converted_tri.ply",false,pi);
206  vcg::tri::io::ExporterPLY<PMesh>::Save(pm,"converted_poly.ply",false,pi);
207 }
208 
209 
210