VCG Library
polygonmesh_polychord_collapse.cpp
1 #include <iostream>
2 #include <vcg/complex/complex.h>
3 #include <vcg/complex/algorithms/clean.h>
4 #include <wrap/io_trimesh/import.h>
5 #include <wrap/io_trimesh/export.h>
6 #include <vcg/complex/algorithms/polygon_support.h>
7 #include <vcg/complex/algorithms/polygon_polychord_collapse.h>
8 
9 using namespace vcg;
10 
11 class PolyVertex;
12 class PolyFace;
13 
14 struct PolyUsedTypes : public vcg::UsedTypes<
15  vcg::Use<PolyVertex>::AsVertexType,
16  vcg::Use<PolyFace>::AsFaceType
17  > {};
18 
19 class PolyVertex : public vcg::Vertex<
20  PolyUsedTypes,
21  vcg::vertex::Coord3f,
22  vcg::vertex::Normal3f,
23  vcg::vertex::BitFlags
24  > {};
25 
26 class PolyFace : public vcg::Face<
27  PolyUsedTypes,
28  vcg::face::PolyInfo,
29  vcg::face::Normal3f,
30  vcg::face::BitFlags,
31  vcg::face::PFVAdj,
32  vcg::face::PFFAdj
33  > {};
34 
35 class PolyMesh : public vcg::tri::TriMesh<
36  std::vector<PolyVertex>,
37  std::vector<PolyFace>
38  > {
39 
40 public:
47  static int openMesh (PolyMesh &mesh, const char *filename)
48  {
49  // try to load the mesh from the file
50  int err = vcg::tri::io::Importer<PolyMesh>::Open(mesh, filename);
51 
52  // check if successfully loaded
53  if (err == 0)
54  {
55  // update bounding box
57  // update topology
59  // update normals TODO: compute average normal in the polygon
61  // update flags
63  }
64 
65  return err;
66  }
67 
74  static int saveMesh (PolyMesh &mesh, const char *filename) {
75  // try to write the mesh into the file
76  return vcg::tri::io::Exporter<PolyMesh>::Save(mesh, filename);
77  }
78 };
79 
80 int main(int argc, char *argv[])
81 {
82  if (argc < 2) {
83  std::cout << "Error. Usage: " << argv[0] << " meshfilename" << std::endl;
84  return -1;
85  }
86 
87  PolyMesh mesh;
88 
89  // open mesh
90  int err = PolyMesh::openMesh(mesh, argv[1]);
91  if (err != 0)
92  return err;
93 
94  // collapse **********************************************************************************************
95  vcg::tri::PolychordCollapse<PolyMesh>::CollapseAllPolychords(mesh, true);
96 
97  // these don't work with polygonal meshes:
100 
101  // save mesh
102  PolyMesh::saveMesh(mesh, "output.obj");
103 
104  return 0;
105 }
106