File: polygonmesh_zonohedra.cpp

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (121 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/****************************************************************************
 * VCGLib                                                            o o     *
 * Visual and Computer Graphics Library                            o     o   *
 *                                                                _   O  _   *
 * Copyright(C) 2004                                                \/)\/    *
 * Visual Computing Lab                                            /\/|      *
 * ISTI - Italian National Research Council                           |      *
 *                                                                    \      *
 * All rights reserved.                                                      *
 *                                                                           *
 * This program is free software; you can redistribute it and/or modify      *
 * it under the terms of the GNU General Public License as published by      *
 * the Free Software Foundation; either version 2 of the License, or         *
 * (at your option) any later version.                                       *
 *                                                                           *
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
 * for more details.                                                         *
 *                                                                           *
 ****************************************************************************/

#include <vector>
#include <stdio.h>

#include <vcg/simplex/vertex/base.h>
#include <vcg/simplex/face/base.h>
#include <vcg/simplex/edge/base.h>
#include <vcg/complex/complex.h>

#include <vcg/complex/algorithms/create/zonohedron.h>

#include <vcg/complex/algorithms/polygon_support.h>

#include <wrap/io_trimesh/export_off.h>



class MyVertex;
class MyEdge;
class MyFace;

struct MyUsedTypes: public vcg::UsedTypes<vcg::Use<MyVertex>::AsVertexType,vcg::Use<MyEdge>::AsEdgeType,vcg::Use<MyFace>::AsFaceType>{};

class MyVertex : public vcg::Vertex< MyUsedTypes,vcg::vertex::Coord3f,vcg::vertex::BitFlags >{};

class MyEdge : public vcg::Edge< MyUsedTypes > {};

class MyFace : public vcg::Face< MyUsedTypes,
	vcg::face::FFAdj,
	vcg::face::VertexRef,
  vcg::face::Normal3f,
	vcg::face::BitFlags > {};

// the main mesh class
class MyMesh : public vcg::tri::TriMesh<std::vector<MyVertex>, std::vector<MyFace> > {};



// example 1: build a cube as a Zonohedron
void example1(){

	vcg::tri::Zonohedron<float> z;
	z.addVector( 0,0,1 );
	z.addVector( 0,1,0 );
	z.addVector( 1,0,0 );

	MyMesh m;
	z.createMesh(m); // this will be a cube

	vcg::tri::UpdateTopology<MyMesh>::FaceFace(m); // needed by exporter

	int savemask = vcg::tri::io::Mask::IOM_BITPOLYGONAL;
	vcg::tri::io::ExporterOFF<MyMesh>::Save(m,"cube.off",savemask);
}


// example2: reads input file, builds zonohedra as described there
void example2(){

	FILE* f = fopen("input.txt","rt");
	if (!f) return;

	while (1) {

		// read mesh name
		char meshFilename[1024], fullMeshFilename[1024];
		if (fscanf(f,"%s",meshFilename)!=1) break;
		sprintf(fullMeshFilename,"%s.off",meshFilename);

		// build input vector
		vcg::tri::Zonohedron<float> z;
		while (1) {
			float a,b,c;
			if (fscanf(f,"%f %f %f",&a, &b, &c)!=3) break;
			z.addVector(a,b,c);
		}

		printf("Building %s from %d vectors...\n",fullMeshFilename, z.vectors().size() );

		MyMesh m;
		z.createMesh(m);

		vcg::tri::UpdateTopology<MyMesh>::FaceFace(m); // needed by exporter

		// normally, faces with more than 4sides are split into parallelograms
		// this merges them (optional, try removing it!)
		vcg::tri::PolygonSupport<MyMesh,int>::MergeFlatFaces(m);

		int savemask = vcg::tri::io::Mask::IOM_BITPOLYGONAL;
		vcg::tri::io::ExporterOFF<MyMesh>::Save(m,fullMeshFilename,savemask);
	}

}

int main(int argc, char *argv[]){
	example1();
	example2();
	return 0;
}