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