miniTest.cpp 25 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-2012, 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.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

#include "Utils/glutwin_atb.h"

29
#include "Topology/generic/parameters.h"
30
#include "Topology/map/embeddedMap2.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44

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

#include "Algo/Import/import.h"
#include "Algo/Export/export.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Render/map_glRender.h"
#include "Algo/Render/vbo_MapRender.h"
#include "Algo/Render/topo_vboRender.h"

#include "Algo/Geometry/laplacian.h"
#include "Algo/Modelisation/subdivision.h"
#include "Algo/Decimation/decimation.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
45
#include "Algo/Remeshing/pliant.h"
46
#include "Algo/Geometry/feature.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
47

Pierre Kraemer's avatar
Pierre Kraemer committed
48 49
using namespace CGoGN ;

50
struct PFP: public PFP_STANDARD
Pierre Kraemer's avatar
Pierre Kraemer committed
51
{
Pierre Kraemer's avatar
Pierre Kraemer committed
52
	// definition of the map
53
	typedef EmbeddedMap2 MAP;
Pierre Kraemer's avatar
Pierre Kraemer committed
54 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 83 84 85 86 87 88 89 90 91 92 93
};

typedef PFP::MAP MAP;

MAP myMap ;

class MyGlutWin : public Utils::GlutWin_ATB
{
public:
	TwBar* viewer_bar ;

	enum renderMode { FLAT, GOURAUD, PHONG, LINES, NORMALS, LAPLACIANS, TOPO } ;

	Geom::Vec4f colDif ;
	Geom::Vec4f colSpec ;
	Geom::Vec4f colClear ;
	Geom::Vec4f colNormal ;

	float shininess ;

	PFP::VEC3 gPosObj ;
	float gWidthObj ;
	float normalBaseSize ;
	float normalScaleFactor ;
	float faceShrinkage ;

	int renderStyle ;
	bool renderObject ;
	bool renderLines ;
	bool renderPoints ;
	bool renderNormals ;
	bool renderTopo;
	bool renderLaplacians ;

	unsigned int m_nbVertices ;

	PFP::TVEC3 position ;
	PFP::TVEC3 normal ;
	PFP::TVEC3 laplacian ;

Sylvain Thery's avatar
Sylvain Thery committed
94 95
	Algo::Render::GL2::MapRender_VBO* vbo_render ;
	Algo::Render::GL2::topo_VBORenderMapD* topo_render ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 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 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	GLuint dl_norm, dl_lapl ;

	MyGlutWin(int* argc, char **argv, int winX, int winY) ;

	void init() ;

	void initGUI() ;

	void myRedraw() ;

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

	void updateVBOprimitives(int upType) ;

	void updateVBOdata(int upType, bool recompute = true) ;

	void initDL(int renderMode) ;
} ;


/**********************************************************************************************
 *                                 AntTweakBar CALLBACKS                                      *
 **********************************************************************************************/

void TW_CALL setRenderStyleCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->renderStyle = *(GLenum*)value ;
	if(mgw->renderStyle == MyGlutWin::FLAT)
		TwDefine("Viewer/FaceShrinkage visible=true") ;
	else
		TwDefine("Viewer/FaceShrinkage visible=false") ;
	glutPostRedisplay() ;
}
void TW_CALL getRenderStyleCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLenum*)value = mgw->renderStyle ;
}

void TW_CALL setShininessCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->shininess = *(float*)value ;
	glutPostRedisplay() ;
}
void TW_CALL getShininessCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(float*)value = mgw->shininess ;
}

void TW_CALL setFocalCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->setFocal(*(float*)value) ;
	mgw->reshape(-1, -1) ;
	glutPostRedisplay() ;
}
void TW_CALL getFocalCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(float*)value = mgw->getFocal() ;
}

void TW_CALL setRenderObjectCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->renderObject = *(GLboolean*)value ;
	glutPostRedisplay() ;
}
void TW_CALL getRenderObjectCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLboolean*)value = mgw->renderObject ;
}

void TW_CALL setRenderLinesCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->renderLines = *(GLboolean*)value ;
	glutPostRedisplay() ;
}
void TW_CALL getRenderLinesCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLboolean*)value = mgw->renderLines ;
}

void TW_CALL setRenderPointsCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->renderPoints = *(GLboolean*)value ;
	glutPostRedisplay() ;
}
void TW_CALL getRenderPointsCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLboolean*)value = mgw->renderPoints ;
}

void TW_CALL setRenderNormalsCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->renderNormals = *(GLboolean*)value ;
	if(mgw->renderNormals)
	{
		mgw->initDL(MyGlutWin::NORMALS) ;
		TwDefine("Viewer/NormalScaleFactor visible=true") ;
	}
	else
		TwDefine("Viewer/NormalScaleFactor visible=false") ;
	glutPostRedisplay() ;
}
void TW_CALL getRenderNormalsCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLboolean*)value = mgw->renderNormals ;
}

void TW_CALL setRenderLaplaciansCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->renderLaplacians = *(GLboolean*)value ;
	if(mgw->renderLaplacians)
	{
		mgw->initDL(MyGlutWin::LAPLACIANS) ;
		TwDefine("Viewer/NormalScaleFactor visible=true") ;
	}
	else
		TwDefine("Viewer/NormalScaleFactor visible=false") ;
	glutPostRedisplay() ;
}
void TW_CALL getRenderLaplaciansCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLboolean*)value = mgw->renderLaplacians ;
}

void TW_CALL setRenderTopoCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->renderTopo = *(GLboolean*)value ;
	glutPostRedisplay()  ;
}
void TW_CALL getRenderTopoCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLboolean*)value = mgw->renderTopo ;
}

void TW_CALL setNormalScaleFactorCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->normalScaleFactor = *(float*)value ;
	if(mgw->renderNormals) mgw->initDL(MyGlutWin::NORMALS) ;
	if(mgw->renderLaplacians) mgw->initDL(MyGlutWin::LAPLACIANS) ;
	glutPostRedisplay() ;
}
void TW_CALL getNormalScaleFactorCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(float*)value = mgw->normalScaleFactor ;
}

void TW_CALL setFaceShrinkageCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->faceShrinkage = *(float*)value ;
	glutPostRedisplay() ;
}
void TW_CALL getFaceShrinkageCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(float*)value = mgw->faceShrinkage ;
}


/**********************************************************************************************
 *                                 MyGlutWin IMPLEMENTATION                                   *
 **********************************************************************************************/

MyGlutWin::MyGlutWin(int* argc, char **argv, int winX, int winY) :
	GlutWin_ATB(argc, argv, winX, winY),
	renderStyle(FLAT),
	renderObject(true),
	renderLines(false),
	renderPoints(false),
	renderNormals(false),
	renderTopo(false),
	renderLaplacians(false),
	vbo_render(NULL),
	topo_render(NULL)
{
	normalScaleFactor = 1.0f ;
	faceShrinkage = 0.0f ;

	colClear = Geom::Vec4f(0.2f, 0.2f, 0.2f, 0.1f) ;
	colDif = Geom::Vec4f(0.8f, 0.9f, 0.7f, 1.0f) ;
	colSpec = Geom::Vec4f(0.9f, 0.9f, 0.9f, 1.0f) ;
	colNormal = Geom::Vec4f(1.0f, 0.0f, 0.0f, 1.0f) ;
	shininess = 80.0f ;

	setFocal(5.0f) ;

	dl_norm = glGenLists(1) ;
	dl_lapl = glGenLists(1) ;
	if(this->shaderOk)
	{
		shaders[0].loadShaders("phong_vs.txt", "phong_ps.txt") ;
		shaders[1].loadShaders("flat_vs.txt", "flat_ps.txt", "flat_gs.txt") ;
	}
}

void MyGlutWin::init()
{
312 313
	normal = myMap.addAttribute<PFP::VEC3>(VERTEX, "normal") ;
	laplacian = myMap.addAttribute<PFP::VEC3>(VERTEX, "laplacian") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
314

Sylvain Thery's avatar
Sylvain Thery committed
315 316 317
	vbo_render = new Algo::Render::GL2::MapRender_VBO() ;
	updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
	updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
318

Sylvain Thery's avatar
Sylvain Thery committed
319
	topo_render = new Algo::Render::GL2::topo_VBORenderMapD() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 348 349 350
	topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;
}

void MyGlutWin::initGUI()
{
	viewer_bar = TwNewBar("Viewer") ;
	TwDefine("Viewer position='16 16' size='220 450' valueswidth='85'") ;

	TwType tw_render_style = TwDefineEnum("Shading","Flat,Gouraud,Phong") ;
	TwAddVarCB(viewer_bar, "RenderStyle", tw_render_style, setRenderStyleCB, getRenderStyleCB, this, "") ;
	TwAddVarCB(viewer_bar, "Shininess", TW_TYPE_FLOAT, setShininessCB, getShininessCB, this, "min=1 max=255 step=1") ;
	TwAddVarCB(viewer_bar, "Focal", TW_TYPE_FLOAT, setFocalCB, getFocalCB, this, "min=0.6 max=5.0 step=0.1") ;
	TwAddVarCB(viewer_bar, "Show Object", TW_TYPE_BOOL8, setRenderObjectCB, getRenderObjectCB, this, "") ;
	TwAddVarCB(viewer_bar, "Show Lines", TW_TYPE_BOOL8, setRenderLinesCB, getRenderLinesCB, this, "") ;
	TwAddVarCB(viewer_bar, "Show Points", TW_TYPE_BOOL8, setRenderPointsCB, getRenderPointsCB, this, "") ;
	TwAddVarCB(viewer_bar, "Show Normals", TW_TYPE_BOOL8, setRenderNormalsCB, getRenderNormalsCB, this, "") ;
	TwAddVarCB(viewer_bar, "Show Laplacians", TW_TYPE_BOOL8, setRenderLaplaciansCB, getRenderLaplaciansCB, this, "") ;
	TwAddVarCB(viewer_bar, "NormalScaleFactor", TW_TYPE_FLOAT, setNormalScaleFactorCB, getNormalScaleFactorCB, this, "min=0.1 max=5.0 step=0.1") ;
	TwDefine("Viewer/NormalScaleFactor visible=false") ;
	TwAddVarCB(viewer_bar, "Show Topo", TW_TYPE_BOOL8, setRenderTopoCB, getRenderTopoCB, this, "") ;
	TwAddVarCB(viewer_bar, "FaceShrinkage", TW_TYPE_FLOAT, setFaceShrinkageCB, getFaceShrinkageCB, this, "min=0.0 max=0.9 step=0.01") ;
}

void MyGlutWin::myRedraw()
{
	GLfloat black[4] = { 0.0f, 0.0f, 0.0f, 1.0f } ;
	GLfloat amb[4] = { 0.2f, 0.1f, 0.1f, 1.0f } ;
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;

	glPushMatrix() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
351
	float sc = 50.0f / gWidthObj ;
Pierre Kraemer's avatar
Pierre Kraemer committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365
	glScalef(sc, sc, sc) ;
	glTranslatef(-gPosObj[0], -gPosObj[1], -gPosObj[2]) ;

	if (renderTopo)
	{
		glDisable(GL_LIGHTING);
		topo_render->drawTopo();
	}

	if(renderLines)
	{
		glDisable(GL_LIGHTING);
		glLineWidth(1.0f) ;
		glColor3f(0.0f,0.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
366
		vbo_render->draw(Algo::Render::GL2::LINES) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
367 368 369 370 371 372 373
	}

	if(renderPoints)
	{
		glPointSize(2.0f) ;
		glDisable(GL_LIGHTING);
		glColor3f(0.3f,0.3f,0.8f);
Sylvain Thery's avatar
Sylvain Thery committed
374
		vbo_render->draw(Algo::Render::GL2::POINTS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	}

	if(renderNormals)
		glCallList(dl_norm) ;

	if(renderLaplacians)
		glCallList(dl_lapl) ;

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

	if(renderObject)
	{
		glMaterialfv(GL_FRONT, GL_DIFFUSE, colDif.data()) ;
		glMaterialfv(GL_FRONT, GL_AMBIENT, amb) ;
		if(renderStyle != PHONG)
		{
			glMaterialfv(GL_FRONT, GL_SPECULAR, black) ;
			glMaterialf(GL_FRONT, GL_SHININESS, 1.0f) ;
		}
		else
		{
			glMaterialfv(GL_FRONT, GL_SPECULAR, colSpec.data()) ;
			glMaterialf(GL_FRONT, GL_SHININESS, shininess) ;
		}

		glEnable(GL_LIGHTING) ;
		glEnable(GL_CULL_FACE) ;
		glPolygonMode(GL_FRONT, GL_FILL) ;
		if(renderStyle == FLAT)
		{
			glShadeModel(GL_FLAT) ;
			shaders[1].bind() ;
			glUniform1fARB(glGetUniformLocationARB(shaders[1].program_handler(),"explode"), faceShrinkage) ;
Sylvain Thery's avatar
Sylvain Thery committed
409
			vbo_render->draw(Algo::Render::GL2::TRIANGLES) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
410 411 412 413 414 415 416
			shaders[1].unbind() ;
		}
		else
		{
			glShadeModel(GL_SMOOTH) ;
			if(renderStyle == PHONG)
				shaders[0].bind() ;
Sylvain Thery's avatar
Sylvain Thery committed
417
			vbo_render->draw(Algo::Render::GL2::TRIANGLES) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
			if(renderStyle == PHONG)
				shaders[0].unbind() ;
		}
	}

	glDisable(GL_POLYGON_OFFSET_FILL) ;

	glPopMatrix() ;
}

void MyGlutWin::myKeyboard(unsigned char keycode, int x, int y)
{
	switch(keycode)
	{
		case 'l':
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
434 435 436 437 438 439
			GLint t1 = glutGet(GLUT_ELAPSED_TIME) ;

			Algo::Modelisation::LoopSubdivision<PFP>(myMap, position) ;

			GLint t2 = glutGet(GLUT_ELAPSED_TIME) ;
			GLfloat seconds = (t2 - t1) / 1000.0f ;
440
			CGoGNout << "loop: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
441 442

			t1 = glutGet(GLUT_ELAPSED_TIME) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
443

Sylvain Thery's avatar
Sylvain Thery committed
444 445
			updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
			updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
446 447
			topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
448 449
			t2 = glutGet(GLUT_ELAPSED_TIME) ;
			seconds = (t2 - t1) / 1000.0f ;
450
			CGoGNout << "display update: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
451

Pierre Kraemer's avatar
Pierre Kraemer committed
452
			glutPostRedisplay() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
453 454 455 456 457
			break ;
		}

		case 'c':
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
458 459 460 461 462 463
			GLint t1 = glutGet(GLUT_ELAPSED_TIME) ;

			Algo::Modelisation::CatmullClarkSubdivision<PFP>(myMap, position) ;

			GLint t2 = glutGet(GLUT_ELAPSED_TIME) ;
			GLfloat seconds = (t2 - t1) / 1000.0f ;
464
			CGoGNout << "catmull-clark: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
465 466

			t1 = glutGet(GLUT_ELAPSED_TIME) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
467

Sylvain Thery's avatar
Sylvain Thery committed
468 469
			updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
			updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
470
			topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
471 472 473

			t2 = glutGet(GLUT_ELAPSED_TIME) ;
			seconds = (t2 - t1) / 1000.0f ;
474
			CGoGNout << "display update: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
475 476

			glutPostRedisplay() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
477 478 479 480 481 482 483 484 485 486 487
			break ;
		}

		case '3':
		{
			GLint t1 = glutGet(GLUT_ELAPSED_TIME) ;

			Algo::Modelisation::Sqrt3Subdivision<PFP>(myMap, position) ;

			GLint t2 = glutGet(GLUT_ELAPSED_TIME) ;
			GLfloat seconds = (t2 - t1) / 1000.0f ;
488
			CGoGNout << "sqrt3: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
489

490
			normal = myMap.getAttribute<PFP::VEC3>(VERTEX, "normal") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
491
			if(!normal.isValid())
492 493
				normal = myMap.addAttribute<PFP::VEC3>(VERTEX, "normal") ;
			laplacian = myMap.getAttribute<PFP::VEC3>(VERTEX, "laplacian") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
494
			if(!laplacian.isValid())
495
				laplacian = myMap.addAttribute<PFP::VEC3>(VERTEX, "laplacian") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
496

Pierre Kraemer's avatar
Pierre Kraemer committed
497 498
			t1 = glutGet(GLUT_ELAPSED_TIME) ;

Sylvain Thery's avatar
Sylvain Thery committed
499 500
			updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
			updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
501 502 503 504
			topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;

			t2 = glutGet(GLUT_ELAPSED_TIME) ;
			seconds = (t2 - t1) / 1000.0f ;
505
			CGoGNout << "display update: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
506 507

			glutPostRedisplay() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
508 509 510 511 512
			break ;
		}

		case 't':
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
513 514
			GLint t1 = glutGet(GLUT_ELAPSED_TIME) ;

515
			Algo::Modelisation::trianguleFaces<PFP>(myMap, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
516

Pierre Kraemer's avatar
Pierre Kraemer committed
517 518
			GLint t2 = glutGet(GLUT_ELAPSED_TIME) ;
			GLfloat seconds = (t2 - t1) / 1000.0f ;
519
			CGoGNout << "triangulation: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
520 521 522

			t1 = glutGet(GLUT_ELAPSED_TIME) ;

Sylvain Thery's avatar
Sylvain Thery committed
523 524
			updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
			updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
525
			topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
526 527 528

			t2 = glutGet(GLUT_ELAPSED_TIME) ;
			seconds = (t2 - t1) / 1000.0f ;
529
			CGoGNout << "display update: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
530 531 532 533 534 535 536

			glutPostRedisplay() ;
			break ;
		}

		case 'q':
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
537 538
			GLint t1 = glutGet(GLUT_ELAPSED_TIME) ;

539
			Algo::Modelisation::quadranguleFaces<PFP>(myMap, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
540

Pierre Kraemer's avatar
Pierre Kraemer committed
541 542
			GLint t2 = glutGet(GLUT_ELAPSED_TIME) ;
			GLfloat seconds = (t2 - t1) / 1000.0f ;
543
			CGoGNout << "quadrangulation: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
544 545 546

			t1 = glutGet(GLUT_ELAPSED_TIME) ;

Sylvain Thery's avatar
Sylvain Thery committed
547 548
			updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
			updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
549
			topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
550 551 552

			t2 = glutGet(GLUT_ELAPSED_TIME) ;
			seconds = (t2 - t1) / 1000.0f ;
553
			CGoGNout << "display update: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
554 555 556 557 558 559 560

			glutPostRedisplay() ;
			break ;
		}

		case 's':
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
561
			unsigned int nbVertices = myMap.getNbOrbits<VERTEX>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
562

Pierre Kraemer's avatar
Pierre Kraemer committed
563 564
			GLint t1 = glutGet(GLUT_ELAPSED_TIME) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
565
			Algo::Decimation::decimate<PFP>(myMap, Algo::Decimation::S_QEM, Algo::Decimation::A_QEM, position, nbVertices * 0.75) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
566

Pierre Kraemer's avatar
Pierre Kraemer committed
567 568
			GLint t2 = glutGet(GLUT_ELAPSED_TIME) ;
			GLfloat seconds = (t2 - t1) / 1000.0f ;
569
			CGoGNout << "decimation: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
570 571 572

			t1 = glutGet(GLUT_ELAPSED_TIME) ;

Sylvain Thery's avatar
Sylvain Thery committed
573 574
			updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
			updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
575
			topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
576 577 578

			t2 = glutGet(GLUT_ELAPSED_TIME) ;
			seconds = (t2 - t1) / 1000.0f ;
579
			CGoGNout << "display update: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
580 581 582 583 584

			glutPostRedisplay() ;
			break ;
		}

585
		case 'd':
Pierre Kraemer's avatar
Pierre Kraemer committed
586
		{
587
			AttributeHandler<PFP::VEC3> positionF = myMap.getAttribute<PFP::VEC3>(FACE, "position") ;
588
			if(!positionF.isValid())
589
				positionF = myMap.addAttribute<PFP::VEC3>(FACE, "position") ;
590 591
			Algo::Geometry::computeCentroidFaces<PFP>(myMap, position, positionF) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
592 593
			GLint t1 = glutGet(GLUT_ELAPSED_TIME) ;

594
			Algo::Modelisation::computeDual<PFP>(myMap) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
595 596 597

			GLint t2 = glutGet(GLUT_ELAPSED_TIME) ;
			GLfloat seconds = (t2 - t1) / 1000.0f ;
598
			CGoGNout << "dual computation: "<< seconds << "sec" << CGoGNendl ;
599 600 601

			position = positionF ;

602
			normal = myMap.getAttribute<PFP::VEC3>(VERTEX, "normal") ;
603
			if(!normal.isValid())
604 605
				normal = myMap.addAttribute<PFP::VEC3>(VERTEX, "normal") ;
			laplacian = myMap.getAttribute<PFP::VEC3>(VERTEX, "laplacian") ;
606
			if(!laplacian.isValid())
607
				laplacian = myMap.addAttribute<PFP::VEC3>(VERTEX, "laplacian") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
608 609 610

			t1 = glutGet(GLUT_ELAPSED_TIME) ;

Sylvain Thery's avatar
Sylvain Thery committed
611 612
			updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
			updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
613 614 615 616
			topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;

			t2 = glutGet(GLUT_ELAPSED_TIME) ;
			seconds = (t2 - t1) / 1000.0f ;
617
			CGoGNout << "display update: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
618 619 620 621 622

			glutPostRedisplay() ;
			break ;
		}

623
		case 'r':
Pierre Kraemer's avatar
Pierre Kraemer committed
624 625
		{

Pierre Kraemer's avatar
Pierre Kraemer committed
626
			GLint t1 = glutGet(GLUT_ELAPSED_TIME) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
627

628
			Algo::Remeshing::pliantRemeshing<PFP>(myMap, position, normal) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
629

Pierre Kraemer's avatar
Pierre Kraemer committed
630 631
			GLint t2 = glutGet(GLUT_ELAPSED_TIME) ;
			GLfloat seconds = (t2 - t1) / 1000.0f ;
632
			CGoGNout << "pliant remeshing: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
633

Pierre Kraemer's avatar
Pierre Kraemer committed
634 635
			t1 = glutGet(GLUT_ELAPSED_TIME) ;

Sylvain Thery's avatar
Sylvain Thery committed
636 637
			updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
			updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
638 639 640 641
			topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;

			t2 = glutGet(GLUT_ELAPSED_TIME) ;
			seconds = (t2 - t1) / 1000.0f ;
642
			CGoGNout << "display update: "<< seconds << "sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
643 644 645 646 647

			glutPostRedisplay() ;
			break ;
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
648
		case '9':
649
		{
650
			CellMarker markVisit(myMap, VERTEX) ;
651

652
			CellMarker markNoDelete(myMap, VERTEX) ;
653 654

			std::vector<Dart> visited ;
655
			visited.reserve(myMap.getNbCells(VERTEX)) ;
656 657 658 659

			visited.push_back(myMap.begin()) ;
			markVisit.mark(myMap.begin()) ;

660
			for(unsigned int i = 0; i < visited.size(); ++i)
661 662
			{
				bool deleteV = true ;
663
				if(markNoDelete.isMarked(visited[i]))
664 665
					deleteV = false ;

666
				Dart vit = visited[i] ;
667 668 669 670 671 672 673 674 675 676 677
				do	// add all vertex neighbors to the table
				{
					Dart nv = myMap.phi1(vit) ;
					if(!markVisit.isMarked(nv))
					{
						visited.push_back(nv) ;
						markVisit.mark(nv) ;
					}
					if(deleteV)
						markNoDelete.mark(nv) ;
					vit = myMap.alpha1(vit) ;
678
				} while(vit != visited[i]) ;
679 680

				if(deleteV)
681
					myMap.deleteVertex(visited[i]) ;
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
			}

//			for(Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
//			{
//				if(!markVisit.isMarked(d))
//				{
//					Dart dd = myMap.phi1(d) ;
//					myMap.deleteVertex(d) ;
//					Dart fit = dd ;
//					do
//					{
//						markVisit.mark(fit) ;
//						fit = myMap.phi1(fit) ;
//					} while(fit != dd) ;
//				}
//			}

Sylvain Thery's avatar
Sylvain Thery committed
699 700
			updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;
			updateVBOdata(Algo::Render::GL2::POSITIONS | Algo::Render::GL2::NORMALS) ;
701
			topo_render->updateData<PFP>(myMap, position, 0.9f, 0.9f) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
702

703 704 705 706
			glutPostRedisplay() ;
			break ;
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
707 708 709 710 711 712 713 714 715 716 717
		case 'a':
		{
			Algo::Geometry::computeLaplacianVertices<PFP>(myMap, Algo::Geometry::TOPOLOGICAL, position, laplacian) ;
			break ;
		}

		case 'x':
		{
			Algo::Export::exportOFF<PFP>(myMap, position, "export.off") ;
			break ;
		}
untereiner's avatar
untereiner committed
718 719 720 721 722

		case 'n':
		{
			myMap.saveMapBin("plop");
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
723 724 725 726 727
	}
}

void MyGlutWin::updateVBOprimitives(int upType)
{
Sylvain Thery's avatar
Sylvain Thery committed
728
	if(upType & Algo::Render::GL2::TRIANGLES)
Pierre Kraemer's avatar
Pierre Kraemer committed
729
	{
Sylvain Thery's avatar
Sylvain Thery committed
730
		vbo_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
731 732
	}

Sylvain Thery's avatar
Sylvain Thery committed
733
	if(upType & Algo::Render::GL2::LINES)
Pierre Kraemer's avatar
Pierre Kraemer committed
734
	{
Sylvain Thery's avatar
Sylvain Thery committed
735
		vbo_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
736 737
	}

Sylvain Thery's avatar
Sylvain Thery committed
738
	if(upType & Algo::Render::GL2::POINTS)
Pierre Kraemer's avatar
Pierre Kraemer committed
739
	{
Sylvain Thery's avatar
Sylvain Thery committed
740
		vbo_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::POINTS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
741 742 743 744 745
	}
}

void MyGlutWin::updateVBOdata(int upType, bool recompute)
{
Sylvain Thery's avatar
Sylvain Thery committed
746 747
	if(upType & Algo::Render::GL2::POSITIONS)
		vbo_render->updateData(Algo::Render::GL2::POSITIONS, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
748

Sylvain Thery's avatar
Sylvain Thery committed
749
	if(upType & Algo::Render::GL2::NORMALS)
Pierre Kraemer's avatar
Pierre Kraemer committed
750 751 752
	{
		if(recompute)
			Algo::Geometry::computeNormalVertices<PFP>(myMap, position, normal) ;
Sylvain Thery's avatar
Sylvain Thery committed
753
		vbo_render->updateData(Algo::Render::GL2::NORMALS, normal) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
		if(renderNormals)
			initDL(MyGlutWin::NORMALS) ;
	}
}

void MyGlutWin::initDL(int renderMode)
{
	switch(renderMode)
	{
		case NORMALS : {
			glNewList(dl_norm, GL_COMPILE) ;
			glLineWidth(1.0f) ;
			glDisable(GL_LIGHTING) ;
			glColor3fv(colNormal.data()) ;
			Algo::Render::Direct::renderNormalVertices<PFP>(myMap, position, normal, normalScaleFactor * normalBaseSize) ;
			glEndList() ;
			break ;
		}

		case LAPLACIANS : {
			glNewList(dl_lapl, GL_COMPILE) ;
			glLineWidth(1.0f) ;
			glDisable(GL_LIGHTING) ;
			glColor3f(0.0f, 1.0f, 0.0f) ;
			Algo::Render::Direct::renderNormalVertices<PFP>(myMap, position, laplacian, 1.0f) ;
			glEndList() ;
			break ;
		}
	}
}

/**********************************************************************************************
 *                                      MAIN FUNCTION                                         *
 **********************************************************************************************/

int main(int argc, char** argv)
{
	MyGlutWin* mgw = new MyGlutWin(&argc, argv, 1200, 800) ;

	if(argc < 2)
	{
		Dart d1 = myMap.newFace(3) ;
		Dart d2 = myMap.newFace(3) ;
		myMap.sewFaces(myMap.begin(), d2) ;
		myMap.mergeFaces(myMap.begin()) ;
	}
	else
	{
		char* filename = argv[1] ;

		GLint t1 = glutGet(GLUT_ELAPSED_TIME) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
805 806
		std::vector<std::string> attrNames ;
		if(!Algo::Import::importMesh<PFP>(myMap, filename, attrNames))
Pierre Kraemer's avatar
Pierre Kraemer committed
807
		{
808
			CGoGNerr << "could not import " << filename << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
809 810
			return 1 ;
		}
811

812
		mgw->position = myMap.getAttribute<PFP::VEC3>(VERTEX, attrNames[0]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
813 814
		GLint t2 = glutGet(GLUT_ELAPSED_TIME) ;
		GLfloat seconds = (t2 - t1) / 1000.0f ;
815
		CGoGNout << "import: " << seconds << " sec" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
816 817 818 819 820

//		DartMarker m(myMap) ;
//		myMap.closeMap(m) ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
821 822 823
	CGoGNout << myMap.getNbOrbits<VERTEX>() << CGoGNendl ;
	CGoGNout << myMap.getNbOrbits<EDGE>() << CGoGNendl ;
	CGoGNout << myMap.getNbOrbits<FACE>() << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
824 825 826

	// compute width and position of object for centering
	Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, mgw->position) ;
827
	CGoGNout << bb << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842

	mgw->gPosObj = bb.center() ;
	float tailleX = bb.size(0) ;
	float tailleY = bb.size(1) ;
	float tailleZ = bb.size(2) ;

	mgw->gWidthObj = std::max<float>(std::max<float>(tailleX, tailleY), tailleZ) ;
	mgw->normalBaseSize = std::min<float>(std::min<float>(tailleX,tailleY),tailleZ) / 50.0f ;

	mgw->init() ;
	mgw->initGUI() ;
	mgw->mainLoop() ;

	return 0 ;
}