triangulation.cpp 6.63 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library 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 Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

#include "Utils/glutwin.h"

29
30

#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "Topology/map/map2.h"
#include "Topology/generic/embeddedMap2.h"

#include "Geometry/vector_gen.h"

#include "Algo/Import/import.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Render/vbo_MapRender.h"


using namespace CGoGN ;


/**
 * Struct that contains some informations about the types of the manipulated objects
 * Mainly here to be used by the algorithms that are parameterized by it
 */
48
struct PFP: public PFP_STANDARD
Pierre Kraemer's avatar
Pierre Kraemer committed
49
{
Pierre Kraemer's avatar
Pierre Kraemer committed
50
	// definition of the map
Pierre Kraemer's avatar
Pierre Kraemer committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	typedef EmbeddedMap2<Map2> MAP;
};

/**
 * A class for a little interface and rendering
 */
class MyGlutWin: public Utils::SimpleGlutWin
{
public:
     void myRedraw();

     PFP::REAL gWidthObj;
     PFP::VEC3 gPosObj;

Sylvain Thery's avatar
Sylvain Thery committed
65
     Algo::Render::GL2::MapRender_VBO* m_render;
Pierre Kraemer's avatar
Pierre Kraemer committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

 	MyGlutWin(int* argc, char **argv, int winX, int winY) : SimpleGlutWin(argc, argv, winX, winY) {}
 	~MyGlutWin()
 	{
 		delete m_render ;
 	}
};

// Routine d'affichage
void MyGlutWin::myRedraw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();

	// center the object
	float sc = 50.0f / gWidthObj;
	glScalef(sc, sc, sc);
	glTranslatef(-gPosObj[0], -gPosObj[1], -gPosObj[2]);

	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glDisable(GL_LIGHTING);

	// draw the lines
	glColor3f(1.0f, 1.0f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
90
	m_render->draw(Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
91
92
93
94
95

	// draw the faces
	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(1.0f, 1.0f);
	glColor3f(0.0f, 0.5f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
96
	m_render->draw(Algo::Render::GL2::TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
97
98
99
100
101
102
103
104
105
106
107
108
	glDisable(GL_POLYGON_OFFSET_FILL);

	glPopMatrix();
}

template<typename PFP>
void TriangulateFromIndices(const AttributeHandler<typename PFP::VEC3>& position, const std::vector<unsigned int> indices, std::vector<unsigned int>& triangles)
{
	// pour eviter le typename ...
	typedef typename PFP::VEC3 VEC3F;

	// combien de sommets ?
109
	CGoGNout << "Nombre de sommets: "<< indices.size()<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
110
111
112
113
114
115

	//juste une boucle pour les afficher
	unsigned int num=0;
	for (std::vector<unsigned int>::const_iterator it = indices.begin(); it != indices.end(); ++it)
	{
		VEC3F P = position[*it];
116
		CGoGNout << "Sommet num "<< num++ << " : indice "<< *it << " = "<< P << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
117
118
119
120
121
122
123
124
125
126
127
128
129
	}

}

template<typename PFP>
void TriangulateFromDart(typename PFP::MAP& map, const AttributeHandler<typename PFP::VEC3>& position, Dart d, std::vector<unsigned int>& triangles)
{
	std::vector<unsigned int> indices;

	// fait le tour de la face et stock les indices des points
	Dart e = d;
	do
	{
130
		indices.push_back( map.getEmbedding(e,VERTEX) ); // recupere l'indice du point dans le handler
Pierre Kraemer's avatar
Pierre Kraemer committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
		e = map.phi1(e); // brin suivant dans la face
	} while (e != d); // tq par revenu au debut

	// appel à la fonction independante de la carte
	TriangulateFromIndices<PFP>(position,indices, triangles);
}






int main(int argc, char **argv)
{
	// declaration of the map
	PFP::MAP myMap;

	// this selector is going to select all the darts
	SelectorTrue allDarts;

	// creation of a new attribute on vertices of type 3D vector
	// a handler to this attribute is returned
153
	AttributeHandler<PFP::VEC3> position = myMap.addAttribute<PFP::VEC3>(VERTEX, "position");
Pierre Kraemer's avatar
Pierre Kraemer committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

	// creation face 1 (convexe)
	Dart d1 = myMap.newFace(5);

	// affecte les positions aux sommets
	Dart d = d1;

	position[d] = PFP::VEC3(-5, 0, 0);
	d = myMap.phi1(d);
	position[d] = PFP::VEC3(-1, 0, 0.3);
	d = myMap.phi1(d);
	position[d] = PFP::VEC3(-1, 3, 0.1);
	d = myMap.phi1(d);
	position[d] = PFP::VEC3(-3, 6, 0.7);
	d = myMap.phi1(d);
	position[d] = PFP::VEC3(-5, 4, 0.2);
	d = myMap.phi1(d);

	// seconde face (concave)
	Dart d2 = myMap.newFace(5);
	d = d2;
	position[d] = PFP::VEC3(1, 0, 0);
	d = myMap.phi1(d);
	position[d] = PFP::VEC3(5, 0, 0.4);
	d = myMap.phi1(d);
	position[d] = PFP::VEC3(5, 4, 0.1);
	d = myMap.phi1(d);
	position[d] = PFP::VEC3(2, 2, 0);
	d = myMap.phi1(d);
	position[d] = PFP::VEC3(1, 3, 0.2);
	d = myMap.phi1(d);

	// vecteur d'indices pour les triangles
	std::vector<unsigned int> triangles;

	// triangule la deuxieme face
	TriangulateFromDart<PFP>(myMap, position, d2, triangles);

    // instanciation of the interface
	MyGlutWin mgw(&argc, argv, 800, 800);

    // computation of the bounding box
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
    mgw.gWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
    mgw.gPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);

    // instanciation of the renderer (here using VBOs)
Sylvain Thery's avatar
Sylvain Thery committed
201
    mgw.m_render = new Algo::Render::GL2::MapRender_VBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
202
203

    // update the renderer (primitives and geometry)
Sylvain Thery's avatar
Sylvain Thery committed
204
205
206
    mgw.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
    mgw.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
    mgw.m_render->updateData(Algo::Render::GL2::POSITIONS, position);
Pierre Kraemer's avatar
Pierre Kraemer committed
207
208
209
210
211

    mgw.mainLoop();

    return 0;
}