tp_master.cpp 14.2 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 <time.h>
#include <algorithm>

29
#include "tp_master.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30

31
#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
32
33
34
35
36
37
#include "Topology/map/map2.h"
#include "Topology/generic/embeddedMap2.h"

#include "Geometry/vector_gen.h"
#include "Geometry/matrix.h"

38
39
#include "Algo/Render/GL2/mapRender.h"
#include "Algo/Render/GL2/topoRender.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
40
41
42
43
44
45
46
#include "Algo/Geometry/normal.h"

#include "Algo/Selection/raySelector.h"

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

47
48
49
#include "Utils/shaderSimpleColor.h"
#include "Utils/shaderPhong.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
50

Pierre Kraemer's avatar
Pierre Kraemer committed
51
52
53
54
55
56
/// pour simplifier l'ecriture du code
using namespace CGoGN;


/// definition de la structure qui decrit le type de carte utilise

57
struct PFP: public PFP_STANDARD
Pierre Kraemer's avatar
Pierre Kraemer committed
58
{
Pierre Kraemer's avatar
Pierre Kraemer committed
59
	// definition of the map
Pierre Kraemer's avatar
Pierre Kraemer committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	typedef EmbeddedMap2<Map2> MAP;
};


/// definition de la carte en  global, plus facile 
PFP::MAP myMap;

// handler d'attribut de position par sommet
AttributeHandler<PFP::VEC3> position;

// handler d'attribut de normale par sommet
AttributeHandler<PFP::VEC3> normal;

/// fonction qui renvoit vrai (appliquée à un brin)
74
//SelectorTrue allDarts;
Pierre Kraemer's avatar
Pierre Kraemer committed
75

Pierre Kraemer's avatar
Pierre Kraemer committed
76
/// encore 1 typedef pour simplifier l'ecriture du code
Pierre Kraemer's avatar
Pierre Kraemer committed
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
typedef PFP::VEC3 Point3D;

/// pile des brins selectionnes (6 max)
std::vector<Dart> selected_darts;

/// 3 brins de la carte encore en global pour pouvoir utiliser le callback clavier de glut !
Dart d_carre;
Dart d_tri;
Dart d_maison;

///Fonctions a connaitre:
///
/// phi1(d), phi2(d) : evident ?
/// phi<21>(d) equivalent a phi2(phi1(d)), phi<121212> equivalent a ....
///
///  Creer un face: newOrientedFace(nb_cotes); renvoit un Dart
///






///  Fonction Carre: construit un carre et renvoit un brin
///

Dart Carre()
{
sylvain thery's avatar
sylvain thery committed
105
	return Dart::nil();
Pierre Kraemer's avatar
Pierre Kraemer committed
106
107
108
109
110
111
112
}


///  Fonction Triangle: construit un triangle et renvoit un brin

Dart Triangle()
{
sylvain thery's avatar
sylvain thery committed
113
	return Dart::nil();
Pierre Kraemer's avatar
Pierre Kraemer committed
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
}


///  Fonction Colle: effectue la couture du carre et du triangle, le carre ne bouge pas
/// Attention aux plongements des sommets topologiquement modifies
/// Fonction a utiliser: sewFaces(d,e): colle deux faces par d et e

/// appel clavier touche 'c'

void Colle(Dart d, Dart e)
{
}


///  Fonction Colle: effectue la couture du carre et du triangle, les points de l'arete commune
/// se placent a mi-chemin

/// appel clavier touche 'C'

void ColleMilieu(Dart d, Dart e)
{

}



/// Fonction qui construite une "maison" (un carré et un triangle collé)
/// faire la topologie puis plonger le tout
/// on renvoit le brin du carre qui est colle au triangle
/// Utiliser embedCell

Dart Maison()
{
sylvain thery's avatar
sylvain thery committed
147
	return Dart::nil();
Pierre Kraemer's avatar
Pierre Kraemer committed
148
149
150
151
152
153
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
}


/// decouper le carre de la maison en 2 carre (un a gauche un a droite)
/// le triangle du haut devient un carre (de forme triangulaire
/// essayer de couper d'abord uniquement les arêtes pour voir ce qui se passe.
/// deplacer les deux nouveaux sommets 
/// Fonctions à utiliser:
/// 	cutEdge(brin): coupe une arête en deux: *---  devient *---*---
///		cutFace(b1,b2): coupe une face en deux, la nouvelle arête relie les deux sommets portes par b1 et b2

///touche 'x' on selectionne les 2 aretes a couper

void Decoupage(Dart d, Dart dd)
{
}



/// appliquer une homothetie a une face (0.95 par ex) a partir d'un brin
/// Attention on ne connait pas le nombre de cotes de face !!
/// Indice: 2 passes: calculer le centre, puis bouger les sommets
/// appel clavier  touche 'r' pout le carre
/// appel clavier  touche 'R' pout le triangle
void Reduction(Dart e)
{


}


/// faire touner une face !!!
/// chaque sommet avance vers le suivant
/// appel clavier touche 'T' pout le carre
/// appel clavier touche 'U' pout le triangle


void Tourne(Dart d)
{

}



/// Fonction de creation de la carte (appelee par le main)

void createMap()
{
//	d_carre = Carre();
//	d_tri = Triangle();
// 	d_maison = Maison();

}


/// Coupe une face carre en 2 en utilisant cutEdge et splitFace 
/// splitFace(d1,d2): le suivant (phi1) de d1 devient celui de d2
/// et inversement: faire un dessin

void coupe_carre(Dart dd)
{

}





/// Variables pour le picking
PFP::VEC3 rayA;
PFP::VEC3 rayB;
std::vector<PFP::VEC3> inters;
std::vector<Dart> d_faces;
std::vector<Dart> d_edges;
std::vector<Dart> d_vertices;



226
void MyQT::drawSelected()
Pierre Kraemer's avatar
Pierre Kraemer committed
227
{
228
	/*
Pierre Kraemer's avatar
Pierre Kraemer committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
	typedef Dart Dart;

// FACES
	glLineWidth(5.0f);
	for(unsigned int i=0; i < d_faces.size(); ++i)
	{
		// fait varier la couleur du plus pres au plus loin
		float c = float (i) / float (d_faces.size());

		glBegin(GL_LINE_LOOP);
		glColor3f(1.0f - c , c ,0.);
		Dart d = d_faces[i];
		do
		{
			const PFP::VEC3& P = position[d];
			glVertex3fv(P.data());
			d = myMap.phi1(d);
		} while (d!=d_faces[i]);
		glEnd();
	}

//edges
	glLineWidth(7.0f);
	glBegin(GL_LINES);

	for(unsigned int i=0; i < d_edges.size(); ++i)
	{
		// fait varier la couleur du plus pres au plus loin
		float c = float (i) / float (d_edges.size());
		glColor3f(1.0f - c , c ,0.);

		Dart d = d_edges[i];
		const PFP::VEC3& P = position[d];
		glVertex3fv(P.data());
		d = myMap.phi1(d);
		const PFP::VEC3& Q =  position[d];
		glVertex3fv(Q.data());
	}
	glEnd();

//VERTICES
//edges
	glPointSize(9.0f);
	glBegin(GL_POINTS);

	for(unsigned int i=0; i < d_vertices.size(); ++i)
	{
		// fait varier la couleur du plus pres au plus loin
		float c = float (i) / float (d_vertices.size());
		glColor3f(1.0f - c , c ,0.);
		Dart d = d_vertices[i];
		const PFP::VEC3& P = position[d];
		glVertex3fv(P.data());
	}
	glEnd();
	
	glLineWidth(7.0f);
286
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
	for(unsigned int i=0; i < selected_darts.size(); ++i)
	{
		// fait varier la couleur du plus pres au plus loin
		Dart d = selected_darts[i];
		switch(i)
		{
			case 0:
				m_render_topo->overdrawDart(d, 5, 1.0f,0.0f,0.0f);
				break;
			case 1:
				m_render_topo->overdrawDart(d, 5, 0.0f,1.0f,0.0f);
				break;
			case 2:
				m_render_topo->overdrawDart(d, 5, 0.0f,0.0f,1.0f);
				break;
			case 3:
				m_render_topo->overdrawDart(d, 5, 1.0f,1.0f,0.0f);
				break;
			case 4:
				m_render_topo->overdrawDart(d, 5, 1.0f,0.0f,1.0f);
				break;
			case 5:
				m_render_topo->overdrawDart(d, 5, 0.0f,1.0f,1.0f);
				break;
			default:
				break;
		}
	}

}


319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
void MyQT::cb_initGL()
{
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);

	// create the render
	m_render = new Algo::Render::GL2::MapRender();
	m_render_topo = new Algo::Render::GL2::TopoRenderMapD() ;

	// create VBO for position
	m_positionVBO = new Utils::VBO();
	m_normalVBO = new Utils::VBO();

	m_shader = new Utils::ShaderPhong();
	m_shader->setAttributePosition(m_positionVBO);
	m_shader->setAttributeNormal(m_normalVBO);
	m_shader->setDiffuse(Geom::Vec4f(0.,0.6,0.,0.));
//	m_shader->setShininess(10000.0);
	m_shader->setSpecular(Geom::Vec4f(0.,0.0,0.,0.));

	// using simple shader with color
	m_shader2 = new Utils::ShaderSimpleColor();
	m_shader2->setAttributePosition(m_positionVBO);
	m_shader2->setColor(Geom::Vec4f(0.,0.1,0.,0.));
	registerRunning(m_shader);
	registerRunning(m_shader2);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
346
347
348
349




350
void MyQT::cb_redraw()
Pierre Kraemer's avatar
Pierre Kraemer committed
351
352
353
354
{

	drawSelected();

355
356
357
358
359
360
	if (renderTopo)
	{
		glEnable( GL_POLYGON_OFFSET_FILL );
		glPolygonOffset( 0.2f, 0.2f );
		m_render_topo->drawTopo();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
361
362
363
364

	/// decalage pour surlignage non clignotant
	glEnable( GL_POLYGON_OFFSET_FILL );
	glPolygonOffset( 1.0f, 1.0f );
365
366
	glLineWidth(1.0f);
	m_render->draw(m_shader2,Algo::Render::GL2::LINES) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
367
368

	/// Rendu faces pleines
369
370
	glEnable(GL_CULL_FACE);
	glFrontFace(GL_CCW);
Pierre Kraemer's avatar
Pierre Kraemer committed
371
372
373
	glPolygonMode(GL_FRONT,GL_FILL);
	glPolygonMode(GL_BACK,GL_NONE);

374
	m_render->draw(m_shader,Algo::Render::GL2::TRIANGLES) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
375
376
377
378

	glDisable( GL_POLYGON_OFFSET_FILL );
}

379
void MyQT::cb_keyPress(int keycode)
Pierre Kraemer's avatar
Pierre Kraemer committed
380
{
381
382
383
384
	int x,y;
	glMousePosition(x,y);
	std::cout << x << " , "<< y << std::endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
385
386
387
388
	switch(keycode)
	{
	case 't':
		renderTopo = !renderTopo;
389
390
391
392
393
		if (renderTopo)
			statusMsg("Render Topology");
		else
			statusMsg("");
		updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
394
395
396
397
		break;

	case '0':
		selected_darts.clear();
398
399
		statusMsg("Cleanning selected darts");
		updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
400
401
402
403
404
		break;

	case 'd':
	{
		if (selected_darts.size()>5) 
405
406
		{
			statusMsg("Already six darts selected");
Pierre Kraemer's avatar
Pierre Kraemer committed
407
			break;
408
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
409
410
411
412
413
414
415
416
417
418
419


		/// calcul du rayon
		getOrthoScreenRay(x,y,rayA,rayB);
		PFP::VEC3 AB = rayB-rayA;

		/// recuperation des faces intersectees
		d_faces.clear();
		d_edges.clear();
		d_vertices.clear();
		std::vector<Dart> darts;
420
		Algo::Selection::dartsRaySelection<PFP>(myMap, position, rayA, AB, darts, SelectorTrue());
421
		selected_darts.push_back(darts[0]);
Pierre Kraemer's avatar
Pierre Kraemer committed
422

423
		updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
		break;
	}

	case 'D':
	{
		/// calcul du rayon
		getOrthoScreenRay(x,y,rayA,rayB);
		PFP::VEC3 AB = rayB-rayA;

		/// recuperation des faces intersectees
		d_faces.clear();
		d_edges.clear();
		d_vertices.clear();
		std::vector<Dart> darts;
438
		Algo::Selection::dartsRaySelection<PFP>(myMap, position, rayA, AB, darts, SelectorTrue());
Pierre Kraemer's avatar
Pierre Kraemer committed
439
440
441
442
443
444
445
446
447
448

		if (!darts.empty()) 
		{
			std::stringstream ss;
			Dart d1 = myMap.phi1(darts[0]);
			Dart d2 = myMap.phi2(darts[0]);
			ss << "dart:" << darts[0].index<<" /phi1:"<< d1.index<<" /phi2:"<< d2.index;

			const PFP::VEC3& P = position[darts[0]];
			ss << " /Emb:" << P;
449
			statusMsg(ss.str().c_str());
Pierre Kraemer's avatar
Pierre Kraemer committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
		}
		break;
	}

	case 'f':
	{
		/// calcul du rayon
		getOrthoScreenRay(x,y,rayA,rayB);
		PFP::VEC3 AB = rayB-rayA;

		/// recuperation des faces intersectees
		d_faces.clear();
		d_edges.clear();
		d_vertices.clear();
	
465
		Algo::Selection:: facesRaySelection<PFP>(myMap, position, SelectorTrue(), rayA, AB, d_faces);
Pierre Kraemer's avatar
Pierre Kraemer committed
466
467


468
469
		if (!d_faces.empty())
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
470
			std::stringstream ss;
471
472
			ss << "Face " << d_faces[0].index/3;
			statusMsg(ss.str().c_str());
Pierre Kraemer's avatar
Pierre Kraemer committed
473
474
475
476
477
478
479
		}
		break;
	}

	case 'a':
	{
		/// calcul du rayon
480
		float dist = getOrthoScreenRay(x,y,rayA,rayB);
Pierre Kraemer's avatar
Pierre Kraemer committed
481
482
483
484
485
486
		PFP::VEC3 AB = rayB-rayA;

		d_faces.clear();
		d_edges.clear();
		d_vertices.clear();

487
		Algo::Selection::edgesRaySelection<PFP>(myMap, position, SelectorTrue(), rayA, AB, d_edges,dist);
Pierre Kraemer's avatar
Pierre Kraemer committed
488
489
490
491
492

		if (!d_edges.empty())
		{
			std::stringstream ss;
			Dart dd = myMap.phi2(d_edges[0]);
493
			ss << "Arete:  dart: " << d_edges[0].index<<" phi1: "<< myMap.phi1(d_edges[0]).index;
Pierre Kraemer's avatar
Pierre Kraemer committed
494
			if (dd != d_edges[0])
495
496
				ss << std::endl<< " phi2: " << dd.index<<" phi1: "<< myMap.phi1(dd).index;
			statusMsg(ss.str().c_str());
Pierre Kraemer's avatar
Pierre Kraemer committed
497
498
499
500
501
502
503
504
		}

		break;
	}

	case 's':
	{
		/// Rayon
505
		float dist = getOrthoScreenRay(x,y,rayA,rayB);
Pierre Kraemer's avatar
Pierre Kraemer committed
506
507
508
509
510
511
		PFP::VEC3 AB = rayB-rayA;

		d_faces.clear();
		d_edges.clear();
		d_vertices.clear();

512
		Algo::Selection::verticesRaySelection<PFP>(myMap, position,rayA, AB, d_vertices,dist,SelectorTrue());
Pierre Kraemer's avatar
Pierre Kraemer committed
513
514
515
516

		if (!d_vertices.empty())
		{
			std::stringstream ss;
517
518
			ss << "Sommet:  dart: " << d_vertices[0].index << ": " << position[d_vertices[0]];
			statusMsg(ss.str().c_str());
Pierre Kraemer's avatar
Pierre Kraemer committed
519
520
521
522
523
524
525
526
527
		}

		break;
	}

	case 'r':
		if (!selected_darts.empty())
		{
			Reduction(selected_darts[0]);
528
			updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
529
530
531
532
533
534
535
		}
		break;

	case 'T':
		if (!selected_darts.empty())
		{
			Tourne(selected_darts[0]);
536
			updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
537
538
539
540
541
542
543
544
		}
		break;

	case 'c':
		
		if (selected_darts.size()>=2)
		{
			Colle(selected_darts[0], selected_darts[1]);
545
			updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
546
547
548
549
550
551
552
		}
		break;

	case 'C':
		if (selected_darts.size()>=2)
		{
			ColleMilieu(selected_darts[0], selected_darts[1]);
553
			updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
554
555
556
557
558
559
560
		}
		break;

	case 'x':
		if (selected_darts.size()>=2)
		{
			Decoupage(selected_darts[0], selected_darts[1]);
561
			updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
562
563
564
565
566
567
568
569
		}
		break;

	}
}

int main(int argc, char **argv)
{
570
571
572
573
	// interface:
	QApplication app(argc, argv);
	MyQT sqt;

Pierre Kraemer's avatar
Pierre Kraemer committed
574

Pierre Kraemer's avatar
Pierre Kraemer committed
575
	if (argc == 2)
Pierre Kraemer's avatar
Pierre Kraemer committed
576
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
577
578
579
580
		std::vector<std::string> attrNames ;
		Algo::Import::importMesh<PFP>(myMap, argv[1], attrNames) ;
		position = myMap.getAttribute<PFP::VEC3>(VERTEX_ORBIT, attrNames[0]) ;
		normal = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "normal");
581
		Algo::Geometry::computeNormalVertices<PFP>(myMap, position, normal) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
582
583
584
	}
	else
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
585
586
		position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position");
		normal = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "normal");
Pierre Kraemer's avatar
Pierre Kraemer committed
587
588
589
590
591
592
		createMap();
	}

	if (myMap.getNbDarts()==0)
		exit(0);

593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
	// caclul de la bounding box de la scene a afficher
	Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
	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 optenir le contexte GL
	sqt.show();

	// update du VBO position (contexte GL necessaire)
	sqt.m_positionVBO->updateData(position);
	sqt.m_normalVBO->updateData(normal);

	// 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);

	sqt.m_render_topo->updateData<PFP>(myMap, position, 0.9f, 0.9f);
Pierre Kraemer's avatar
Pierre Kraemer committed
614

615
616
	// show final pour premier redraw
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
617

618
619
	// et on attend la fin.
	return app.exec();
Pierre Kraemer's avatar
Pierre Kraemer committed
620
}