tuto5.cpp 15.4 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
#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"
47
#include "Utils/text3d.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51 52 53

//#include "testMaps.h"


using namespace CGoGN ;

54 55
struct PFP: public PFP_STANDARD
{
Pierre Kraemer's avatar
Pierre Kraemer committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
	// 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;

70 71 72 73




Pierre Kraemer's avatar
Pierre Kraemer committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
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;

Sylvain Thery's avatar
Sylvain Thery committed
88 89
    Algo::Render::GL2::MapRender_VBO* m_render;
    Algo::Render::GL2::topo3_VBORenderMapD* m_render_topo;
Pierre Kraemer's avatar
Pierre Kraemer committed
90

91 92
    Utils::Strings3D m_strings;

93 94
    void updateVBO();

95 96
    void storeVerticesInfo();

Pierre Kraemer's avatar
Pierre Kraemer committed
97 98 99 100 101 102 103 104 105 106 107 108
 	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);
};

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
void myGlutWin::storeVerticesInfo()
{

	CellMarker mv(myMap,VERTEX_CELL);
	for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
	{
		if (!mv.isMarked(d))
		{
			mv.mark(d);
			std::stringstream ss;
			ss << d << " : "<< position[d];
			m_strings.addString(ss.str(),position[d]);
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
125 126 127 128 129 130 131 132 133 134 135
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]);


Sylvain Thery's avatar
Sylvain Thery committed
136

Pierre Kraemer's avatar
Pierre Kraemer committed
137 138 139
	glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
	glDisable(GL_LIGHTING);

Sylvain Thery's avatar
Sylvain Thery committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	glLineWidth(3.0);
	glBegin(GL_LINES);
	glColor3f(1.,0.,0.);
	glVertex3f(0.0,0.0,0.0);
	glVertex3f(1.0,0.0,0.0);

	glColor3f(0.,1.,0.);
	glVertex3f(0.0,0.0,0.0);
	glVertex3f(0.0,1.0,0.0);

	glColor3f(0.,0.,1.);
	glVertex3f(0.0,0.0,0.0);
	glVertex3f(0.0,0.0,1.0);

	glEnd();

Pierre Kraemer's avatar
Pierre Kraemer committed
156
	glColor3f(0.0f,1.0f,.0f);
Sylvain Thery's avatar
Sylvain Thery committed
157
	m_render->draw(Algo::Render::GL2::POINTS);
Pierre Kraemer's avatar
Pierre Kraemer committed
158 159 160 161 162 163

	if(render_line)
	{
		// on trace les ligne devant
		glDisable( GL_POLYGON_OFFSET_FILL );
		glColor3f(1.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
164
		m_render->draw(Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166 167 168 169 170 171 172 173
	}
	// 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);
Sylvain Thery's avatar
Sylvain Thery committed
174
		m_render->draw(Algo::Render::GL2::TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
		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);

	}

189 190 191 192 193 194 195 196 197
//	m_strings.predraw(Geom::Vec3f(0.0,1.0,1.0));
//	m_strings.draw(0,Geom::Vec3f(0.5,0.5,0.5));
//	m_strings.draw(1,Geom::Vec3f(-0.5,0.5,0.5));
//	m_strings.draw(2,Geom::Vec3f(-0.5,-0.5,0.5));
//	m_strings.postdraw();


	m_strings.drawAll(Geom::Vec3f(0.0,1.0,1.0));

Pierre Kraemer's avatar
Pierre Kraemer committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    //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();


}

214 215
void myGlutWin::updateVBO()
{
Sylvain Thery's avatar
Sylvain Thery committed
216 217 218 219
	m_render->updateData(Algo::Render::GL2::POSITIONS, position );
	m_render->initPrimitives<PFP>(myMap,allDarts,Algo::Render::GL2::LINES);
	m_render->initPrimitives<PFP>(myMap,allDarts,Algo::Render::GL2::POINTS);
	m_render->initPrimitives<PFP>(myMap,allDarts,Algo::Render::GL2::TRIANGLES);
220 221 222 223
	m_render_topo->updateData<PFP>(myMap, allDarts, position,  0.9, 0.9, 0.9);
}


Pierre Kraemer's avatar
Pierre Kraemer committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/// 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;
Sylvain Thery's avatar
Sylvain Thery committed
241

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        	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);
Sylvain Thery's avatar
Sylvain Thery committed
258

259 260 261 262
        		glPopMatrix();

        		m_render_topo->popColors();

Sylvain Thery's avatar
Sylvain Thery committed
263
         		if (d != Dart::nil())
264
        		{
Sylvain Thery's avatar
Sylvain Thery committed
265
        			m_render_topo->setDartColor(d,1.0,0.0,0.0);
266 267 268 269 270 271
        			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();
Sylvain Thery's avatar
Sylvain Thery committed
272
        			std::cout << "Pick dart:" << d << " pos= "<< position[d] << std::endl;
273
        		}
Sylvain Thery's avatar
Sylvain Thery committed
274

275 276 277 278

        		break;
        	}

Pierre Kraemer's avatar
Pierre Kraemer committed
279 280 281 282 283 284 285 286 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 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 346 347
        	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;

348
        			updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
        			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);

371
						updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
						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);

394
						updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
						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);

412
						updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
        				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);
438
                		updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
439 440 441 442 443
						glutPostRedisplay();
                        break;
                }
                case '2' : {
						Algo::Modelisation::Tetrahedron::swap4To4<PFP>(myMap, dglobal, position);
444
						updateVBO();						glutPostRedisplay();
Pierre Kraemer's avatar
Pierre Kraemer committed
445 446 447 448
						break;
                }
                case '3' : {
						Algo::Modelisation::Tetrahedron::swap2To3<PFP>(myMap, dglobal, position);
449
						updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
450 451 452 453 454
						glutPostRedisplay();
                		break;
                }
                case '4' : {
                		Algo::Modelisation::Tetrahedron::swap3To2<PFP>(myMap, dglobal, position);
455
                		updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469
                		glutPostRedisplay();
                		break;
                }
                default : {
                		std::cout << "not implemented" << std::endl;
                		break;
                }

        }
}


int main(int argc, char **argv)
{
Sylvain Thery's avatar
bug  
Sylvain Thery committed
470 471 472 473 474 475 476 477 478 479 480 481 482
//	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);
Sylvain Thery's avatar
Sylvain Thery committed
483 484


Pierre Kraemer's avatar
Pierre Kraemer committed
485 486 487 488 489 490
//	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
491 492 493 494 495 496 497 498 499 500 501 502 503
//	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
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

//	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
Sylvain Thery's avatar
Sylvain Thery committed
537 538
    mgw.m_render = new Algo::Render::GL2::MapRender_VBO();
    mgw.m_render_topo = new Algo::Render::GL2::topo3_VBORenderMapD();
Pierre Kraemer's avatar
Pierre Kraemer committed
539

540
    mgw.updateVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
541

542 543 544
    mgw.m_strings.init();
    mgw.storeVerticesInfo();
    mgw.m_strings.sendToVBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
545 546 547 548 549 550 551

    mgw.mainLoop();

    delete mgw.m_render;

    return 0;
}