tuto2.cpp 7.56 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

27
#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
28
29
30
31
#include "Topology/map/map2.h"
#include "Topology/generic/mapBrowser.h"

#include "Algo/Geometry/boundingbox.h"
32
#include "Algo/Render/GL2/mapRender.h"
33
#include "Utils/Shaders/shaderSimpleColor.h"
Sylvain Thery's avatar
Sylvain Thery committed
34

35
#include "Utils/drawer.h"
Sylvain Thery's avatar
Sylvain Thery committed
36

37
#include "tuto2.h"
Sylvain Thery's avatar
Sylvain Thery committed
38

Pierre Kraemer's avatar
Pierre Kraemer committed
39
40
using namespace CGoGN ;

41
42
struct PFP: public PFP_STANDARD
{
Pierre Kraemer's avatar
Pierre Kraemer committed
43
	// definition de la carte
44
	typedef Map2 MAP;
Pierre Kraemer's avatar
Pierre Kraemer committed
45
46
47
48
49
50
51
52
53
54
};

PFP::MAP myMap;

SelectorTrue allDarts;

PFP::TVEC3 position ;
PFP::TVEC3 normal ;
AttributeHandler<Geom::Vec4f> color ;

Sylvain Thery's avatar
Sylvain Thery committed
55
void MyQT::cb_initGL()
Pierre Kraemer's avatar
Pierre Kraemer committed
56
{
Sylvain Thery's avatar
Sylvain Thery committed
57
58
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);
Pierre Kraemer's avatar
Pierre Kraemer committed
59

Sylvain Thery's avatar
Sylvain Thery committed
60
61
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
Pierre Kraemer's avatar
Pierre Kraemer committed
62

Sylvain Thery's avatar
Sylvain Thery committed
63
64
	// create VBO for position
	m_positionVBO = new Utils::VBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
65

Sylvain Thery's avatar
Sylvain Thery committed
66
67
	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
68
	m_shader->setAttributePosition(m_positionVBO);
Pierre Kraemer's avatar
Pierre Kraemer committed
69

Sylvain Thery's avatar
Sylvain Thery committed
70
71
72
	m_color = Geom::Vec4f(0.,1.,0.,0.);
	m_shader->setColor(m_color);

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
73
	registerShader(m_shader);
74

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
75
76
	m_with_lines = true;
	m_line_width = 4.0;
Sylvain Thery's avatar
Sylvain Thery committed
77
78
79
80
}

void MyQT::cb_redraw()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
81
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Sylvain Thery's avatar
Sylvain Thery committed
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
	glEnable(GL_LIGHTING);
	if (m_shader)
	{
		if (m_with_lines)
		{
			glLineWidth(m_line_width);
			m_shader->setColor(Geom::Vec4f(1.,1.,0.,0.));
			m_render->draw(m_shader, Algo::Render::GL2::LINES);
		}

		glEnable(GL_POLYGON_OFFSET_FILL);
		glPolygonOffset(1.0f, 1.0f);

		m_shader->setColor(m_color);
		m_render->draw(m_shader, Algo::Render::GL2::TRIANGLES);

		glDisable(GL_POLYGON_OFFSET_FILL);
	}
}

void MyQT::lines_slot(bool x)
{
	m_with_lines = x;
	updateGL();
}
Pierre Kraemer's avatar
Pierre Kraemer committed
107

Sylvain Thery's avatar
Sylvain Thery committed
108
109
110
111
112
void MyQT::line_width_slot(int x)
{

	m_line_width=x;
	updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
113
114
}

Sylvain Thery's avatar
Sylvain Thery committed
115
116
117
118
119
120
121
122
123
124
125
void MyQT::color_slot()
{
	QColor col = QColorDialog::getColor ();
	if (col.isValid())
	{

		m_color = Geom::Vec4f(float(col.red())/255.0f,float(col.green())/255.0f,float(col.blue())/255.0f,0.0f);
		updateGL();
	}
	else
	{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
126
		CGoGNout << "Cancel Color" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
127
128
129
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
130
131
132
133
134
135
136
// Algorithme qui parcours une carte et affiche l'attribut position de chaque brin
template<typename PFP>
void TestDeParcoursAFF(typename PFP::MAP& m, MapBrowser& mb, const typename PFP::TVEC3& pos)
{
	for (Dart d = mb.begin(); d != mb.end(); mb.next(d))
	{
		typename PFP::VEC3 P = pos[d];
137
		CGoGNout << "P "<< P << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
138
139
140
141
142
143
144
145
146
	}
}

int main(int argc, char **argv)
{
	/// Utilisation des MapBrowsers

	/// on reprend la carte de tuto1

147
148
	Dart d2 = myMap.newFace(3);
	Dart d3 = myMap.newFace(4);
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
149
	myMap.sewFaces(d2, d3);
Pierre Kraemer's avatar
Pierre Kraemer committed
150

151
	position = myMap.addAttribute<Geom::Vec3f>(VERTEX, "position");
Pierre Kraemer's avatar
Pierre Kraemer committed
152
153
154
155
156
157
158
159
160
161
162
163
164

	position[d2] = PFP::VEC3(0.0f, 0.0f, 0.0f);
	d2 = myMap.phi1(d2);
	position[d2] = PFP::VEC3(2.0f, 0.0f, 0.0f);
	d2 = myMap.phi1(d2);
	position[d2] = PFP::VEC3(1.0f, 3.0f, 0.0f);
	d2 = myMap.phi1(d2);
	d3 = myMap.phi<11>(d3);
	position[d3] = PFP::VEC3(0.0f, -2.0f, 0.0f);
	d3 = myMap.phi1(d3);
	position[d3] = PFP::VEC3(2.0f, -2.0f, 0.0f);
	d3 = myMap.phi1(d3);

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
165

Pierre Kraemer's avatar
Pierre Kraemer committed
166
	// MapBrowser: la carte elle meme
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
167
	CGoGNout << "Parcours avec la carte" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
168
169
170
171
	TestDeParcoursAFF<PFP>(myMap, myMap, position);

	// MapBrowserLinkedAuto
	// sous-carte dans une liste avec attribut gere par le browser
172
	CGoGNout << "Parcours avec le browser (les sommets)"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
173
174
175
176
177

	// creation d'un browser avec attribut cree a la volee
	MapBrowserLinkedAuto<PFP::MAP>mbl(myMap);

	// on ajoute un brin par sommet dans le browser
178
	myMap.foreach_orbit(VERTEX, mbl);
Pierre Kraemer's avatar
Pierre Kraemer committed
179
180
181
182
183
184
185

	// et on parcours la sous-carte avec ce browser
	TestDeParcoursAFF<PFP>(myMap, mbl, position);


	// MapBrowserLinkedAttr
	// sous-carte dans une liste avec attribut gere par l'appelant
186
	CGoGNout << "Parcours avec le browser (le triangle)"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
187
188

	// on cree un attribut Dart pour la liste
189
	AutoAttributeHandler<Dart> tableLink(myMap, DART);
Pierre Kraemer's avatar
Pierre Kraemer committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
	// le browser
	MapBrowserLinkedAttr<PFP::MAP>mbl2(tableLink);
	// que l'on remplit a la main
	Dart d = d2;
	mbl2.add(d);
	d =  myMap.phi1(d);
	mbl2.add(d);
	d =  myMap.phi1(d);
	mbl2.add(d);

	// et on parcours la sous-carte avec ce browser
	TestDeParcoursAFF<PFP>(myMap, mbl2, position);


	// MapBrowserSelector
	// sous-carte  avec des brins marques (equivalent de l'utilisation de good dans les algos)
206
	CGoGNout << "Parcours avec le browser selector"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

	// on marque 2 brins pour le test
	DartMarker mk(myMap);
	mk.mark(d2);
	mk.mark(d3);

	// le selector qui selectionne les "bons" brins (les autres sont reconduits a la frontiere ;)
	SelectorMarked selector(mk);

//	// le browser
//	MapBrowserSelector<PFP::MAP>mbsel(myMap,selector);

//	// et on parcours la sous-carte avec ce browser
//	TestDeParcoursAFF<PFP>(myMap,mbsel,position);


Sylvain Thery's avatar
Sylvain Thery committed
223
224
225
226
227
228
229
230
231
232
	// interface:
	QApplication app(argc, argv);
	MyQT sqt;

    Utils::QT::uiDockInterface dock;
    sqt.setDock(&dock);

	sqt.setCallBack( dock.checkLines, SIGNAL(toggled(bool)), SLOT(lines_slot(bool)) );
	sqt.setCallBack( dock.color_button , SIGNAL(pressed()), SLOT(color_slot()) );
	sqt.setCallBack( dock.dial_line_width , SIGNAL(valueChanged(int)), SLOT(line_width_slot(int)) );
Pierre Kraemer's avatar
Pierre Kraemer committed
233

Sylvain Thery's avatar
Sylvain Thery committed
234
235
236
237
238
239
	// message d'aide
	sqt.setHelpMsg("Second Tuto");

	dock.number_of_darts->display(int(myMap.getNbDarts()));

    //  bounding box
Pierre Kraemer's avatar
Pierre Kraemer committed
240
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
Sylvain Thery's avatar
Sylvain Thery committed
241
242
243
244
245
246
247
248
    float lWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
    Geom::Vec3f lPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);

    // envoit info BB a l'interface
	sqt.setParamObject(lWidthObj,lPosObj.data());

	// show 1 pour GL context
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
249

Sylvain Thery's avatar
Sylvain Thery committed
250
251
	// update du VBO position (context GL necessaire)
	sqt.m_positionVBO->updateData(position);
Pierre Kraemer's avatar
Pierre Kraemer committed
252

Sylvain Thery's avatar
Sylvain Thery committed
253
254
255
256
	// update des primitives du renderer
	SelectorTrue allDarts;
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
257

Sylvain Thery's avatar
Sylvain Thery committed
258
259
	// show final pour premier redraw
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
260

Sylvain Thery's avatar
Sylvain Thery committed
261
262
	// et on attend la fin.
	return app.exec();
Pierre Kraemer's avatar
Pierre Kraemer committed
263
}