tuto_oper2.cpp 11.2 KB
Newer Older
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
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.unistra.fr/                                           *
21
22
23
24
25
26
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "tuto_oper2.h"
#include "Algo/Geometry/boundingbox.h"
untereiner's avatar
untereiner committed
27
#include "Algo/Tiling/Surface/square.h"
28
#include "Algo/Geometry/centroid.h"
Sylvain Thery's avatar
Sylvain Thery committed
29
30
31
#include "Algo/Import/import.h"
#include "Algo/Export/export.h"

32
33
34
35
36
37
38
39
40
41
42
using namespace CGoGN ;

int main(int argc, char **argv)
{
	//	// interface
	QApplication app(argc, argv);
	MyQT sqt;


    sqt.setDock(& sqt.dock);
    sqt.setCallBack( sqt.dock.listOper, SIGNAL(currentRowChanged(int)), SLOT(operation(int)) );
43
44
    sqt.setCallBack( sqt.dock.svg, SIGNAL(clicked()), SLOT(svg()) );
    sqt.setCallBack( sqt.dock.widthSlider, SIGNAL(valueChanged(int)), SLOT(width(int)) );
45
46
47
48
49
50
51
	int n=3;
	if (argc==2)
		n = atoi(argv[1]);

	// example code itself
	sqt.createMap(n);

52
53
	sqt.width(5);

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	// set help message in menu
	sqt.setHelpMsg("First Tuto: \nCreate two faces\nsew them\nand affect positions");
	// final show for redraw
	sqt.show();
	// and wait for the end
	return app.exec();
}

void MyQT::operation(int x)
{
	switch(x)
	{
	case 0:
		CGoGNout <<"split vertex"<<CGoGNendl;
		if ((m_selected != NIL) && (m_selected2 != NIL))
		{
			dm.markAll();
			myMap.splitVertex(m_selected,m_selected2);
72
73
			PFP::VEC3 c1 = Algo::Surface::Geometry::faceCentroid<PFP>(myMap, m_selected, position);
			PFP::VEC3 c2 = Algo::Surface::Geometry::faceCentroid<PFP>(myMap, m_selected2, position);
74
75
76
77
78
79
80
81
82
83
			position[m_selected] = position[m_selected] * 0.7f + c1*0.3f;
			position[m_selected2] = position[m_selected2] * 0.7f + c2*0.3f;
			updateMap();
		}
		break;
	case 1:
		CGoGNout <<"delete vertex"<<CGoGNendl;
		if (m_selected != NIL)
		{
			dm.markAll();
Pierre Kraemer's avatar
Pierre Kraemer committed
84
			m_selected = myMap.deleteVertex(m_selected);
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
			updateMap();
		}
		break;
	case 2:
		CGoGNout <<"cut edge"<<CGoGNendl;
		if (m_selected != NIL)
		{
			dm.markAll();
			PFP::VEC3 Q = position[myMap.phi1(m_selected)];
			myMap.cutEdge(m_selected);
			position[myMap.phi1(m_selected)] = (position[m_selected] + Q)/2.0f;
			updateMap();
		}
		break;
	case 3:
		CGoGNout <<"uncut edge"<<CGoGNendl;
		if (m_selected != NIL)
		{
			myMap.uncutEdge(m_selected);
			updateMap();
		}
		break;
	case 4:
		CGoGNout <<"collapse edge"<<CGoGNendl;
		if (m_selected != NIL)
		{
			PFP::VEC3 M = (position[m_selected] + position[myMap.phi1(m_selected)])/2.0f;
112
113
			m_selected = myMap.collapseEdge(m_selected,true);
			position[m_selected] = M;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
			updateMap();
		}
		break;
	case 5:
		CGoGNout <<"flip edge"<<CGoGNendl;
		if (m_selected != NIL)
		{
			myMap.flipEdge(m_selected);
			updateMap();
		}
		break;
	case 6:
		CGoGNout <<"flip back edge"<<CGoGNendl;
		if (m_selected != NIL)
		{
			myMap.flipBackEdge(m_selected);
			updateMap();
		}
		break;
	case 7:
		CGoGNout <<"split face"<<CGoGNendl;
		if (m_selected != NIL)
		{
			myMap.splitFace(m_selected,m_selected2);
			updateMap();
		}
		break;
	case 8:
		CGoGNout <<"merge face"<<CGoGNendl;
		if (m_selected != NIL)
		{
			myMap.mergeFaces(m_selected);
			updateMap();
			m_selected=NIL;
		}
		break;
Sylvain Thery's avatar
Sylvain Thery committed
150
151
152
153
154
155
156
157
158
159
160
	case 9:
		CGoGNout <<"delete face"<<CGoGNendl;
		if (m_selected != NIL)
		{
			myMap.deleteFace(m_selected);
			updateMap();
			m_selected=NIL;
			m_selected2=NIL;
		}
		break;

161
162
163
164
165
166
167
168
169
170
	default:
		break;
	}

	updateGL();
	dock.listOper->setCurrentRow(-1);
}

void MyQT::createMap(int n)
{
171
172
	position = myMap.addAttribute<VEC3, VERTEX>("position");
	colorDarts = myMap.addAttribute<VEC3, DART>("color");
173

untereiner's avatar
untereiner committed
174
175
    Algo::Surface::Tilings::Square::Grid<PFP> grid(myMap, n, n, true);
    grid.embedIntoGrid(position, 1.,1.,0.);
176
177

    //  bounding box of scene
Sylvain Thery's avatar
Sylvain Thery committed
178
179
180
	Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position) ;
	setParamObject(bb.maxSize(), bb.center().data()) ;
	m_shift = bb.maxSize()/200.0f;
181
182
183
184
185
186
187
188

	// first show for be sure that GL context is binded
	show();

	// render the topo of the map without boundary darts

	dm.markAll();

189
	m_render_topo->setDartWidth(4.0f);
190
	m_render_topo->setInitialDartsColor(0.0f,0.0f,0.0f);
191
	m_render_topo->setInitialBoundaryDartsColor(0.0f,0.0f,0.0f);
untereiner's avatar
untereiner committed
192
193

#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
194
	m_render_topo->updateData(myMap, position, 0.9);
untereiner's avatar
untereiner committed
195
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
196
   m_render_topo->updateData(myMap, position, 0.9f, 0.9f,true);
untereiner's avatar
untereiner committed
197
#endif
198

Pierre Kraemer's avatar
Pierre Kraemer committed
199
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
200
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
201
		if (dm.isMarked(d) && (!myMap.isBoundaryMarked<2>(d)))
202
		{
unknown's avatar
unknown committed
203
			int n = rand();
204
205
206
207
208
209
210
211
212
213
214
			float r = float(n&0x7f)/255.0f + 0.25f;
			float g = float((n>>8)&0x7f)/255.0f + 0.25f;
			float b = float((n>>16)&0x7f)/255.0 + 0.25f;
			colorDarts[d] =  Geom::Vec3f(r,g,b);
			m_render_topo->setDartColor(d,r,g,b);
		}
	}
}

void MyQT::updateMap()
{
215
	m_render_topo->setInitialBoundaryDartsColor(0.0f,0.0f,0.0f);
untereiner's avatar
untereiner committed
216
#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
217
	m_render_topo->updateData(myMap, position, 0.9);
untereiner's avatar
untereiner committed
218
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
219
	m_render_topo->updateData(myMap, position, 0.9f, 0.9f,true);
untereiner's avatar
untereiner committed
220
221
#endif

222
223
	for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
224
		if (dm.isMarked(d) && (!myMap.isBoundaryMarked<2>(d)))
225
226
227
228
229
230
231
232
233
234
235
236
		{
			const Geom::Vec3f& C = colorDarts[d];
			if (C*C != 0.0f)
				m_render_topo->setDartColor(d,C[0],C[1],C[2]);
		}
	}
}

// initialization GL callback
void MyQT::cb_initGL()
{
	glClearColor(1.0f,1.0f,1.0f,1.0f);
untereiner's avatar
untereiner committed
237
#ifdef PRIMAL_TOPO
Pierre Kraemer's avatar
Pierre Kraemer committed
238
	m_render_topo = new Algo::Render::GL2::TopoPrimalRender<PFP>() ;
untereiner's avatar
untereiner committed
239
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
240
	m_render_topo = new Algo::Render::GL2::TopoRenderMap<PFP>(0.01f) ;
untereiner's avatar
untereiner committed
241
#endif
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
}

// redraw GL callback (clear and swap already done)
void MyQT::cb_redraw()
{
	glEnable( GL_POLYGON_OFFSET_FILL );
	glPolygonOffset( 1.0f, 1.0f );

	m_render_topo->drawTopo();

	glDisable( GL_POLYGON_OFFSET_FILL );

	if (m_selected != NIL)
		m_render_topo->overdrawDart(m_selected, 11, 1.0f, 0.0f, 0.0f);

	if (m_selected2 != NIL)
		m_render_topo->overdrawDart(m_selected2, 11, 0.0f, 1.0f, 0.0f);
}

void MyQT::cb_mousePress(int button, int x, int y)
{
	if (Shift())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
265
		Dart d = m_render_topo->picking(myMap, x,y); // nb
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
		if (button == Qt::LeftButton)
		{
			if (d != Dart::nil())
				m_selected = d;
		}
		if (button == Qt::RightButton)
		{
			if (d != Dart::nil())
				m_selected2 = d;
		}
		updateGL();
	}
}

void MyQT::cb_keyPress(int keycode)
{
	switch(keycode)
	{
	case 'c':
285
		for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
286
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
287
			if (!myMap.isBoundaryMarked<2>(d))
288
			{
unknown's avatar
unknown committed
289
				int n = rand();
290
291
292
293
294
295
296
297
298
				float r = float(n&0x7f)/255.0f + 0.25f;
				float g = float((n>>8)&0x7f)/255.0f + 0.25f;
				float b = float((n>>16)&0x7f)/255.0 + 0.25f;
				colorDarts[d] =  Geom::Vec3f(r,g,b);
				m_render_topo->setDartColor(d,r,g,b);
			}
		}
		break;
	case 'g':
299
		for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
300
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
301
			if (!myMap.isBoundaryMarked<2>(d))
302
303
304
305
306
307
			{
				colorDarts[d] =  Geom::Vec3f(0.5f,0.5f,0.5f);
				m_render_topo->setDartColor(d,0.5f,0.5f,0.5f);
			}
		}
		break;
308

309
	case 'b':
310
		for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
311
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
312
			if (!myMap.isBoundaryMarked<2>(d))
313
314
315
316
317
			{
				colorDarts[d] =  Geom::Vec3f(0.0f,0.0f,0.0f);
				m_render_topo->setDartColor(d,0.0f,0.0f,0.0f);
			}
		}
318
		break;
Sylvain Thery's avatar
Sylvain Thery committed
319
320
	case Qt::Key_Up:
		if (m_selected!=NIL)
321
			position[m_selected][1] += m_shift;
Sylvain Thery's avatar
Sylvain Thery committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
		updateMap();
		updateGL();
		break;
	case Qt::Key_Down:
		if (m_selected!=NIL)
			position[m_selected][1] -= m_shift;
		updateMap();
		updateGL();
		break;
	case Qt::Key_Left:
		if (m_selected!=NIL)
			position[m_selected][0] -= m_shift;
		updateMap();
		updateGL();
		break;
	case Qt::Key_Right:
		if (m_selected!=NIL)
			position[m_selected][0] += m_shift;
		updateMap();
		updateGL();
		break;
343
	}
344
345
	updateGL();
}
346

347
348
void MyQT::svg()
{
Sylvain Thery's avatar
Sylvain Thery committed
349
350
351
352
353
	if (m_selected!=NIL)
		m_render_topo->setDartColor(m_selected,0.8f,0.0f,0.0f);
	if (m_selected2!=NIL)
		m_render_topo->setDartColor(m_selected2,0.0f,0.8f,0.0f);

354
355
356
357
	std::string filename = selectFileSave("snapshot file", ".", "(*.svg)");
	m_render_topo->svgout2D(filename, modelViewMatrix(),projectionMatrix());
}

Sylvain Thery's avatar
Sylvain Thery committed
358
359
360
361
362
363
364
365
366
367
368
void MyQT::cb_Open()
{
	std::string filters("all (*.*);; trian (*.trian);; off (*.off);; ply (*.ply);; map (*.map)") ;
	std::string filename = selectFile("Open Mesh", "", filters) ;
	if (!filename.empty())
		importMesh(filename);
	updateGL();
}

void MyQT::cb_Save()
{
369
370
371
	std::string filename = selectFileSave("Export Off file ",".","(*.off)");
	Algo::Surface::Export::exportOFF<PFP>(myMap,position,filename.c_str());
	//std::cout << "could not save file : " << filename << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
372
373
374
375
376
377
378
379
380
381
382
383
}

void MyQT::importMesh(std::string& filename)
{
	myMap.clear(true) ;

	size_t pos = filename.rfind(".");    // position of "." in filename
	std::string extension = filename.substr(pos);

	if (extension == std::string(".map"))
	{
		myMap.loadMapBin(filename);
384
		position = myMap.getAttribute<VEC3, VERTEX>("position") ;
Sylvain Thery's avatar
Sylvain Thery committed
385
386
387
388
	}
	else
	{
		std::vector<std::string> attrNames ;
389
		if(!Algo::Surface::Import::importMesh<PFP>(myMap, filename.c_str(), attrNames))
Sylvain Thery's avatar
Sylvain Thery committed
390
391
392
393
		{
			CGoGNerr << "could not import " << filename << CGoGNendl ;
			return;
		}
394
		position = myMap.getAttribute<VEC3, VERTEX>(attrNames[0]) ;
Sylvain Thery's avatar
Sylvain Thery committed
395
396
	}

397
	colorDarts = myMap.getAttribute<VEC3, DART>("color");
Sylvain Thery's avatar
Sylvain Thery committed
398
399
	if (!colorDarts.isValid())
	{
400
		colorDarts = myMap.addAttribute<VEC3, DART>("color");
Sylvain Thery's avatar
Sylvain Thery committed
401
402
		for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
403
			if (dm.isMarked(d) && (!myMap.isBoundaryMarked<2>(d)))
Sylvain Thery's avatar
Sylvain Thery committed
404
			{
unknown's avatar
unknown committed
405
				int n = rand();
Sylvain Thery's avatar
Sylvain Thery committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
				float r = float(n&0x7f)/255.0f + 0.25f;
				float g = float((n>>8)&0x7f)/255.0f + 0.25f;
				float b = float((n>>16)&0x7f)/255.0 + 0.25f;
				colorDarts[d] =  Geom::Vec3f(r,g,b);
				m_render_topo->setDartColor(d,r,g,b);
			}
		}
	}

	m_selected  = NIL;
	m_selected2 = NIL;

	Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position) ;
	setParamObject(bb.maxSize(), bb.center().data()) ;
	m_shift = bb.maxSize()/200.0f;

	updateMap();
	updateGLMatrices() ;
}

426
427
428
429
void MyQT::width(int w)
{
	m_render_topo->setDartWidth(w);
	m_render_topo->setRelationWidth(w);
430
431
	updateGL();
}