tuto5.cpp 14.5 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
#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "Topology/map/map3.h"
#include "Topology/generic/embeddedMap3.h"

#include "Geometry/matrix.h"
#include "Geometry/vector_gen.h"
#include "Algo/Import/import.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Render/map_glRender.h"
#include "Algo/Render/vbo_MapRender.h"
#include "Algo/Modelisation/tetrahedron.h"
#include "Algo/Modelisation/primitives3d.h"
#include "Algo/Modelisation/polyhedron.h"
#include "Algo/Modelisation/subdivision.h"

#include "Algo/Render/topo3_vboRender.h"

#include "Topology/generic/cellmarker.h"

//#include "testMaps.h"


using namespace CGoGN ;

53
54
struct PFP: public PFP_STANDARD
{
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	// definition de la carte
	typedef EmbeddedMap3<Map3> MAP;
	//typedef Map3 MAP;

};

PFP::MAP myMap;
SelectorTrue allDarts;
PFP::TVEC3 position ;
Dart dglobal;

unsigned int idNorm;
unsigned int idCol;

class myGlutWin: public Utils::SimpleGlutWin
{
public:

     void myRedraw();

     float gWidthObj;
     Geom::Vec3f gPosObj;

     bool aff_help;
     bool render_line;
     bool render_volume;
     bool render_topo;

83
    Algo::Render::VBO::MapRender_VBO* m_render;
Pierre Kraemer's avatar
Pierre Kraemer committed
84
85
    Algo::Render::VBO::topo3_VBORenderMapD* m_render_topo;

86
87
    void updateVBO();

Pierre Kraemer's avatar
Pierre Kraemer committed
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
 	myGlutWin(	int* argc, char **argv, int winX, int winY):SimpleGlutWin(argc,argv,winX,winY)
    {
 	   aff_help = false;
 	   render_line = true;
 	   render_volume = false;
 	   render_topo = true;

    }

 	void myKeyboard(unsigned char keycode, int x, int y);
};

void myGlutWin::myRedraw(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();

	// met l'objet au milieu de l'ecran a la bonne taille
	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);

	glColor3f(0.0f,1.0f,.0f);
	m_render->draw(Algo::Render::VBO::POINTS);

	if(render_line)
	{
		// on trace les ligne devant
		glDisable( GL_POLYGON_OFFSET_FILL );
		glColor3f(1.0f,1.0f,0.0f);
		m_render->draw(Algo::Render::VBO::LINES);
	}
	// et on decale les faces vers l'arriere

	if(render_volume)
	{
		glEnable( GL_POLYGON_OFFSET_FILL );
		glPolygonOffset( 1.0f, 1.0f );

		glColor3f(0.0f,0.5f,0.0f);
		m_render->draw(Algo::Render::VBO::TRIANGLES);
		glDisable( GL_POLYGON_OFFSET_FILL );
	}

	if(render_topo)
	{
		glDisable( GL_POLYGON_OFFSET_FILL );
		glColor3f(1.0f,1.0f,0.0f);
		m_render_topo->drawTopo();

		Dart d = myMap.phi2( myMap.begin());
		m_render_topo->overdrawDart(d,5,1.0f,0.0f,1.0f);

	}

    //affichage de l'aide
    if (aff_help) {
            glColor3f(1.0f,1.0f,1.0f);
            printString2D(10,20,"Keys:\n\
                    l: affichage des lignes de bordures\n\
                    v: affichage des volumes\n\
                    h: affiche cette aide\n\n\n\n\
                    1 : swap 2->2\n");
    }


    glPopMatrix();


}

163
164
165
166
167
168
169
170
171
172
void myGlutWin::updateVBO()
{
	m_render->updateData(Algo::Render::VBO::POSITIONS, position );
	m_render->initPrimitives<PFP>(myMap,allDarts,Algo::Render::VBO::LINES);
	m_render->initPrimitives<PFP>(myMap,allDarts,Algo::Render::VBO::POINTS);
	m_render->initPrimitives<PFP>(myMap,allDarts,Algo::Render::VBO::TRIANGLES);
	m_render_topo->updateData<PFP>(myMap, allDarts, position,  0.9, 0.9, 0.9);
}


Pierre Kraemer's avatar
Pierre Kraemer committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/// Gestion des touches clavier
void myGlutWin::myKeyboard(unsigned char keycode, int x, int y)
{
        switch(keycode) {
                /** affichage **/
        	case 'D':
        	{
				Dart d = myMap.begin();
				m_render_topo->setDartColor(d, 1.0f,0.0f,0.0f);
				d = myMap.phi1(d);
				m_render_topo->setDartColor(d, 0.0f,1.0f,0.0f);
				d = myMap.phi1(d);
				m_render_topo->setDartColor(d, 0.0f,0.0f,1.0f);
				glutPostRedisplay();

        	}
        		break;
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
        	case 'x':
        	{
        		// push/pop color is only needed for dart coloring conservation
        		// can bee long long huge meshes
        		m_render_topo->pushColors();

        		// setDartsIdColor can be done only once if dart coloring not used
        		m_render_topo->setDartsIdColor<PFP>(myMap,allDarts);

        		// transform as in drawing cb
        		glPushMatrix();
        		float sc = 50.0f/gWidthObj;
        		glScalef(sc,sc,sc);
        		glTranslatef(-gPosObj[0],-gPosObj[1],-gPosObj[2]);
        		//pick
        		Dart d = m_render_topo->picking(x,H-y);
        		glPopMatrix();

        		m_render_topo->popColors();

        		if (d != Dart::nil())
        		{
        			redraw();
        			std::stringstream ss;
        			ss << "Pick dart:" << d << std::endl<<"pos="<< position[d];
        			glColor3f(1.,1.,0.);
        			printString2D(x+12,y+22,ss.str());
        			glutSwapBuffers();
//        			std::cout << "Pick dart:" << d << " position= "<< position[d] << std::endl;
        			std::cout << "Pick dart:" << d << std::endl;
        		}
        		// in console:

        		break;
        	}

Pierre Kraemer's avatar
Pierre Kraemer committed
226
227
228
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
286
287
288
289
290
291
292
293
294
        	case 'Q':
        		m_render_topo->setAllDartsColor(1.0f,1.0f,1.0f);
        		glutPostRedisplay();
        	break;

        		case 'c' : { //Cut Edge OK
						Dart d = 9;
						//Dart d = dglobal;

						Dart f = myMap.phi1(d);
						myMap.cutEdge(d);
						Dart e = myMap.phi1(d);
						position[e] = position[d];
						position[e] += position[f];
						position[e] *= 0.5;

						m_render_topo->updateData<PFP>(myMap, allDarts, position, 0.9, 0.9, 0.9);
						m_render_topo->setDartColor(d, 0.0f,1.0f,0.0f);
						glutPostRedisplay();
						break;
        		}
        		case 's' : { //Split Face OK
						Dart d = 25;
						//Dart d = dglobal;

						myMap.splitFace(d, myMap.phi1(myMap.phi1(d)));

						m_render_topo->updateData<PFP>(myMap, allDarts, position, 0.9, 0.9, 0.9);
						glutPostRedisplay();
						break;
        		}
        		case 'f' : { //Flip Edge
						//Dart d = 25;
        				//Dart d = myMap.phi2(25);
						Dart d = dglobal;

						Dart r = myMap.phi2(d);
						Dart e = myMap.phi2(myMap.phi3(d));

						unsigned int p1 = myMap.getDartEmbedding(VERTEX_ORBIT, myMap.phi_1(e));
						unsigned int p2 = myMap.getDartEmbedding(VERTEX_ORBIT, myMap.phi_1(r));


						myMap.flipFace(d);

						myMap.setDartEmbedding(VERTEX_ORBIT, r, p1 );
						myMap.setDartEmbedding(VERTEX_ORBIT, e, p2 );

						Dart dd = d;
						do {
							Dart e = myMap.phi2(dd);
							Dart e2= myMap.phi<32>(dd);//myMap.phi2(myMap.phi3(dd));
							myMap.setDartEmbedding(VERTEX_ORBIT, dd, myMap.getDartEmbedding(VERTEX_ORBIT,e));
							myMap.setDartEmbedding(VERTEX_ORBIT, myMap.phi3(dd), myMap.getDartEmbedding(VERTEX_ORBIT, e2));
							dd = myMap.phi1(dd);
						} while( dd!=d);


						m_render_topo->updateData<PFP>(myMap, allDarts, position, 0.9, 0.9, 0.5);
						glutPostRedisplay();
						break;
        		}
        		case 'b' : {
        			Dart d = dglobal;
        			//Dart d = myMap.phi<323>(myMap.phi_1(myMap.phi<232>(myMap.phi1(myMap.phi<112>(dglobal)))));


        			std::cout << "boundary : " << myMap.isBoundaryVolume(d) << std::endl;

295
        			updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
        			glutPostRedisplay();
        			break;
        		}
        		case 'E' : { //Collapse Edge
						//Dart d = 10;
						//Dart d = myMap.phi2(25);
						Dart d = dglobal;

        				//Dart d = myMap.phi<23>(myMap.phi1(myMap.phi<232>(dglobal)));

//						Dart dd = myMap.phi2(d) ;
//
//						PFP::VEC3 a = position[d];
//						PFP::VEC3 v2 = position[dd] ;
//
//						// Compute the approximated position
//						a =  (a + v2) / PFP::REAL(2);
//
//						position[d] = a;

						myMap.collapseEdge(d);

318
						updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
						glutPostRedisplay();
						break;
        		}
        		case 'F' : { //Collapse Face OK
						Dart d = 10;
						//Dart d = myMap.phi2(25);
						//Dart d = myMap.phi<232>(myMap.phi1(myMap.phi<232>(dglobal)));
        				//Dart d = dglobal;


//        				Dart d1 = myMap.phi1(d);
//        				Dart d11 = myMap.phi1(myMap.phi1(d));
//        				Dart d111 = myMap.phi1(myMap.phi1(myMap.phi1(d)));
//
//        				std::cout << "d=" << d << " d1=" << d1 << " d11=" << d11 << " d111=" << d111 << std::endl;

						myMap.collapseFace(d);

//        				myMap.collapseEdge(d);
//       				myMap.collapseEdge(d1);
//        				myMap.collapseEdge(d11,false,false);

341
						updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
						glutPostRedisplay();
						break;
        		}
        		case 'V' : { //Collapse Volume
        				m_render_topo->updateData<PFP>(myMap, allDarts, position,  0.9, 0.9, 0.9);
        				glutPostRedisplay();
        				break;
        		}
        		case 'd' : { //Volume Degree OK
        				std::cout << "degree = " << myMap.volumeDegree(dglobal) << std::endl;
        				break;
        		}
        		case 'C' : {
        				Dart d = dglobal;

						Algo::Modelisation::hexaCutVolume<PFP, AttributeHandler<PFP::VEC3>, PFP::VEC3>(myMap,d , position);

359
						updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
        				glutPostRedisplay();
        				break;
        		}
                case 'l' : {
                        render_line = !render_line;
                        glutPostRedisplay();
                        break;
                }
                case 'v' : {
                		render_volume = !render_volume;
                		glutPostRedisplay();
                		break;
                }
                case 't' : {
                		render_topo = !render_topo;
                		glutPostRedisplay();
                		break;
                }
                case 'h': {
                        aff_help = !aff_help;
                        glutPostRedisplay();
                        break;
                }
                case '1': {
                		Algo::Modelisation::Tetrahedron::swap2To2<PFP>(myMap, dglobal, position);
385
                		updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
386
387
388
389
390
						glutPostRedisplay();
                        break;
                }
                case '2' : {
						Algo::Modelisation::Tetrahedron::swap4To4<PFP>(myMap, dglobal, position);
391
						updateVBO();						glutPostRedisplay();
Pierre Kraemer's avatar
Pierre Kraemer committed
392
393
394
395
						break;
                }
                case '3' : {
						Algo::Modelisation::Tetrahedron::swap2To3<PFP>(myMap, dglobal, position);
396
						updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
397
398
399
400
401
						glutPostRedisplay();
                		break;
                }
                case '4' : {
                		Algo::Modelisation::Tetrahedron::swap3To2<PFP>(myMap, dglobal, position);
402
                		updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
                		glutPostRedisplay();
                		break;
                }
                default : {
                		std::cout << "not implemented" << std::endl;
                		break;
                }

        }
}


int main(int argc, char **argv)
{
Sylvain Thery's avatar
bug    
Sylvain Thery committed
417
418
419
420
421
422
423
424
425
426
427
428
429
//	std::vector<std::string> attrNames ;
//	Algo::Import::importInESS<PFP>(myMap, argv[1], attrNames);
//	position = myMap.getAttribute<PFP::VEC3>(VERTEX_ORBIT, attrNames[0]) ;

	position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT,"position");


	Algo::Modelisation::Primitive3D<PFP> prim(myMap,position);
	int nb=3;
	if (argc>1)
		nb = atoi(argv[1]);
	dglobal = prim.hexaGrid_topo(nb,nb,nb);
	prim.embedHexaGrid(1.0f,1.0f,1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
430
431
432
433
434
435
//	Geom::Matrix44f mat;
//	mat.identity();
//	Geom::scale(2.0f, 2.0f,2.0f,mat);
//	prim.transform(mat);


Sylvain Thery's avatar
bug    
Sylvain Thery committed
436
437
438
439
440
441
442
443
444
445
446
447
448
//	Dart d = Algo::Modelisation::Polyhedron<PFP>::createOrientedPolyhedron(myMap,6);
//	dglobal=d;
//
//	position[d] = PFP::VEC3(0);
//	position[myMap.phi1(d)] = PFP::VEC3(1,0,0);
//	position[myMap.phi1(myMap.phi1(d))] = PFP::VEC3(1,0,1);
//	position[myMap.phi_1(d)] = PFP::VEC3(0,0,1);
//
//	d = myMap.phi_1(myMap.phi2(myMap.phi_1(myMap.phi_1(myMap.phi2(myMap.phi_1(d))))));
//	position[d] = PFP::VEC3(1,1,0);
//	position[myMap.phi1(d)] = PFP::VEC3(0,1,0);
//	position[myMap.phi1(myMap.phi1(d))] = PFP::VEC3(0,1,1);
//	position[myMap.phi_1(d)] = PFP::VEC3(1,1,1);
Pierre Kraemer's avatar
Pierre Kraemer committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

//	Dart d = Algo::Modelisation::Polyhedron<PFP>::createOrientedPolyhedron(myMap,4);
//	dglobal=d;
//
//	Dart t1 = d;
//	position[t1] = PFP::VEC3(0.0f, 0.0f, 3.0f);
//	t1 = myMap.phi1(t1);
//	position[t1] = PFP::VEC3(0.0f, 1.0f, 3.0f);
//	t1 = myMap.phi1(t1);
//	position[t1] = PFP::VEC3(1.0f, 0.5f, 3.0f);
//	t1 = myMap.phi_1(myMap.phi2(d));
//	position[t1] = PFP::VEC3(0.5f, 0.5f, 4.0f);

	//Dart d = Algo::Modelisation::Polyhedron<PFP>::createOrientedPrism(myMap);
//	Algo::Modelisation::Polyhedron<PFP> p(myMap, position);
//	dglobal = p.cube_topo(3,1,1);
//	p.embedCube(1.0f,1.0f,1.0f);


	//dglobal = createMap9<PFP>(myMap,position);


    // un peu d'interface
	myGlutWin mgw(&argc,argv,800,800);

    // calcul de la bounding box
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap,position);
    // pour l'affichage
    mgw.gWidthObj = std::max<float>( std::max<float>(bb.size(0),bb.size(1)),bb.size(2));
    mgw.gPosObj =  (bb.min() +  bb.max()) /2.0f;


    // allocation des objets necessaires pour le rendu
482
    mgw.m_render = new Algo::Render::VBO::MapRender_VBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
483
484
485
    mgw.m_render_topo = new Algo::Render::VBO::topo3_VBORenderMapD();


486
    mgw.updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
487
488
489
490
491
492
493
494
495



    mgw.mainLoop();

    delete mgw.m_render;

    return 0;
}