VCG Library
polygonmesh_zonohedra.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 <stdio.h>
25 #include <vcg/complex/complex.h>
26 #include <vcg/complex/algorithms/create/zonohedron.h>
27 #include <vcg/complex/algorithms/polygon_support.h>
28 #include <wrap/io_trimesh/export_off.h>
29 
30 
31 
32 class MyVertex;
33 class MyEdge;
34 class MyFace;
35 
36 struct MyUsedTypes: public vcg::UsedTypes<vcg::Use<MyVertex>::AsVertexType,vcg::Use<MyEdge>::AsEdgeType,vcg::Use<MyFace>::AsFaceType>{};
37 
38 class MyVertex : public vcg::Vertex< MyUsedTypes,vcg::vertex::Coord3f,vcg::vertex::BitFlags >{};
39 
40 class MyEdge : public vcg::Edge< MyUsedTypes > {};
41 
42 class MyFace : public vcg::Face< MyUsedTypes,
43  vcg::face::FFAdj,
44  vcg::face::VertexRef,
45  vcg::face::Normal3f,
46  vcg::face::BitFlags > {};
47 
48 // the main mesh class
49 class MyMesh : public vcg::tri::TriMesh<std::vector<MyVertex>, std::vector<MyFace> > {};
50 
51 
52 
53 // example 1: build a cube as a Zonohedron
54 void example1(){
55 
56  vcg::tri::Zonohedron<float> z;
57  z.addVector( 0,0,1 );
58  z.addVector( 0,1,0 );
59  z.addVector( 1,0,0 );
60 
61  MyMesh m;
62  z.createMesh(m); // this will be a cube
63 
64  vcg::tri::UpdateTopology<MyMesh>::FaceFace(m); // needed by exporter
65 
66  int savemask = vcg::tri::io::Mask::IOM_BITPOLYGONAL;
67  vcg::tri::io::ExporterOFF<MyMesh>::Save(m,"cube.off",savemask);
68 }
69 
70 
71 // example2: reads input file, builds zonohedra as described there
72 void example2(){
73 
74  FILE* f = fopen("input.txt","rt");
75  if (!f) return;
76 
77  while (1) {
78 
79  // read mesh name
80  char meshFilename[1024], fullMeshFilename[1024];
81  if (fscanf(f,"%s",meshFilename)!=1) break;
82  sprintf(fullMeshFilename,"%s.off",meshFilename);
83 
84  // build input vector
85  vcg::tri::Zonohedron<float> z;
86  while (1) {
87  float a,b,c;
88  if (fscanf(f,"%f %f %f",&a, &b, &c)!=3) break;
89  z.addVector(a,b,c);
90  }
91 
92  printf("Building %s from %d vectors...\n",fullMeshFilename, z.vectors().size() );
93 
94  MyMesh m;
95  z.createMesh(m);
96 
97  vcg::tri::UpdateTopology<MyMesh>::FaceFace(m); // needed by exporter
98 
99  // normally, faces with more than 4sides are split into parallelograms
100  // this merges them (optional, try removing it!)
101  vcg::tri::PolygonSupport<MyMesh,MyMesh>::MergeFlatFaces(m);
102 
103  int savemask = vcg::tri::io::Mask::IOM_BITPOLYGONAL;
104  vcg::tri::io::ExporterOFF<MyMesh>::Save(m,fullMeshFilename,savemask);
105  }
106 
107 }
108 
109 int main(int argc, char *argv[]){
110  example1();
111  example2();
112  return 0;
113 }