decimationVolumique.cpp 14.4 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
/*
 * TODO
 * - pb dans l'ordre des coeff shrinkage du rendu topo
 * - rendu explod object
 * - modifier le update du rendu topo
 * - shader pour l'affichage
 */

#include "squelette3carte.h"

#include "Algo/DecimationVolumique/decimator.h"


#include "Utils/GLSLShader.h"

/**********************************************************************************************
 *                                 AntTweakBar CALLBACKS                                      *
 **********************************************************************************************/
void TW_CALL setRenderObjectCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->render_obj = *(GLboolean*)value ;
	if(mgw->render_obj) {
		mgw->initRender() ;
	}
	glutPostRedisplay() ;
}
void TW_CALL getRenderObjectCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLboolean*)value = mgw->render_obj ;
}

void TW_CALL setRenderObjStyleCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->renderObjStyle = *(MyGlutWin::renderObjStyleType*)value ;
	glutPostRedisplay() ;
}
void TW_CALL getRenderObjStyleCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLenum*)value = mgw->renderObjStyle ;
}

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

void TW_CALL setRenderTopoStyleCB(const void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	mgw->renderTopoStyle = *(MyGlutWin::renderTopoStyleType*)value;

	if(mgw->renderTopoStyle == MyGlutWin::T_ALL)
	{
		TwDefine("Viewer/phi1_explod visible=false");
		TwDefine("Viewer/phi2_explod visible=false");
		TwDefine("Viewer/phi3_explod visible=false");
	}
	else if(mgw->renderTopoStyle == MyGlutWin::T_EXPLOD)
	{
		TwDefine("Viewer/phi1_explod visible=true");
		TwDefine("Viewer/phi2_explod visible=true");
		TwDefine("Viewer/phi3_explod visible=true");
	}

	glutPostRedisplay() ;
}
void TW_CALL getRenderTopoStyleCB(void* value, void* clientData)
{
	MyGlutWin* mgw = static_cast<MyGlutWin*>(clientData) ;
	*(GLenum*)value = mgw->renderTopoStyle ;
}


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


MyGlutWin::MyGlutWin(int* argc, char **argv, int winX, int winY) :
	GlutWin_ATB(argc, argv, winX, winY),
	renderObjStyle(O_ALL),
	renderTopoStyle(T_ALL),
	render_obj(true),
	render_topo(false),
	render_help(true)
{
	//AJOUT Shader
	if (this->shaderOk)
	{
		shaders[0].loadShaders("clippingSphere_vs.txt","clippingSphere_ps.txt","clippingSphere_gs.txt");
		shaders[1].loadShaders("vol_clip_vs.txt","vol_clip_ps.txt","vol_clip_gs.txt");
		shaders[2].loadShaders("flat_vs.txt","flat_ps.txt","flat_gs.txt");
		shaders[3].loadShaders("flat.vs","flat.fs","flat.gs");
	}
}


void MyGlutWin::myRedraw(void)
{
	glClearColor(backCol[0],backCol[1],backCol[2],backCol[3]);
	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);


	if(render_obj)
	{
Sylvain Thery's avatar
Sylvain Thery committed
150 151
		m_render->updateData(Algo::Render::GL2::POSITIONS, position );
		//m_render->initPrimitives<PFP>(myMap, good, Algo::Render::GL2::POINTS);
Pierre Kraemer's avatar
Pierre Kraemer committed
152 153 154

		if(renderObjStyle == O_ALL)
		{
Sylvain Thery's avatar
Sylvain Thery committed
155 156
			m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
		    m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
157 158 159 160 161 162

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

			glColor3f(0.0f,0.5f,0.0f);
			//shaders[3].bind();
Sylvain Thery's avatar
Sylvain Thery committed
163
			m_render->draw(Algo::Render::GL2::TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165 166 167 168
			//shaders[3].unbind();
			glDisable( GL_POLYGON_OFFSET_FILL );
			// on trace les ligne devant
			glColor3f(1.0f,1.0f,0.0f);

Sylvain Thery's avatar
Sylvain Thery committed
169
			m_render->draw(Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
170 171 172 173

		}
		else if(renderObjStyle == O_FACES)
		{
Sylvain Thery's avatar
Sylvain Thery committed
174
			m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
175 176 177 178 179

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

			glColor3f(0.0f,0.5f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
180
			m_render->draw(Algo::Render::GL2::TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183 184
			glDisable( GL_POLYGON_OFFSET_FILL );
		}
		else if(renderObjStyle == O_LINE)
		{
Sylvain Thery's avatar
Sylvain Thery committed
185
			m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
186 187 188 189

			// on trace les ligne devant
			glDisable( GL_POLYGON_OFFSET_FILL );
			glColor3f(1.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
190
			m_render->draw(Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		}
		else if(renderObjStyle == O_EXPLOD)
		{

		}
	}

	if(render_topo)
	{
		if(renderTopoStyle == T_ALL)
		{
			glDisable( GL_POLYGON_OFFSET_FILL );
			glColor3f(1.0f,1.0f,0.0f);
			m_render_topo->updateData<PFP>(myMap,allDarts,position,0.9,0.9,0.9);
			m_render_topo->drawTopo();
		}
		else if(renderTopoStyle == T_EXPLOD)
		{
			glDisable( GL_POLYGON_OFFSET_FILL );
			glColor3f(1.0f,1.0f,0.0f);
			m_render_topo->updateData<PFP>(myMap,allDarts,position,coefTopoExplod[0],coefTopoExplod[1],coefTopoExplod[2]);
			m_render_topo->drawTopo();
		}
	}

	if(render_help)
	{
        glColor3f(1.0f,1.0f,1.0f);
        int x = getWinWidth() - 200 ;
        printString2D(x,20,
        		"Keys:\n"
        		"d: decimation\n"
                "h: affiche cette aide\n"
        );
	}


	glPopMatrix();
}

/// Gestion des touches clavier
void MyGlutWin::myKeyboard(unsigned char keycode, int x, int y)
{
        switch(keycode) {
                /** affichage **/
        		case 'd' :
        		{
238
        				int nbVertices = myMap.getNbOrbits(VERTEX) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
239

240
        				CGoGNout << "nb darts = " << myMap.getNbOrbits(DART) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243 244 245

        				GLint t1 = glutGet(GLUT_ELAPSED_TIME);
        			 	Algo::DecimationVolumique::decimate<PFP>(myMap, Algo::DecimationVolumique::S_Random, Algo::DecimationVolumique::A_Centroid, position, nbVertices * 0.75);
        				GLint t2 = glutGet(GLUT_ELAPSED_TIME);
        				GLfloat seconds = (t2 - t1) / 1000.0f;
246
        				CGoGNout << "decimation: "<< seconds << "sec" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
247 248 249 250 251 252 253 254 255
        				break;
        		}
        		case 'h':
        		{
        				render_help = !render_help;
        				break;
        		}
                default :
                {
256
                		CGoGNout << "not implemented" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
                		break;
                }

        }
}

void MyGlutWin::init()
{
	coefObjExplod = Geom::Vec3f(1.,1.,1.);
	coefTopoExplod = Geom::Vec3f(0.9,0.9,0.9);
	backCol = Geom::Vec4f(0.533, 0.560, 0.984, 0.984);
}

void MyGlutWin::initRender()
{
    // maj des donnees de position
	GLint t1 = glutGet(GLUT_ELAPSED_TIME);
Sylvain Thery's avatar
Sylvain Thery committed
274
    m_render->updateData(Algo::Render::GL2::POSITIONS, position );
Pierre Kraemer's avatar
Pierre Kraemer committed
275 276
	GLint t2 = glutGet(GLUT_ELAPSED_TIME);
	GLfloat seconds = (t2 - t1) / 1000.0f;
277
	CGoGNout << "updateData: "<< seconds << "sec" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
278 279
    // creation des primitives de rendu a partir de la carte
	t1 = glutGet(GLUT_ELAPSED_TIME);
Sylvain Thery's avatar
Sylvain Thery committed
280 281
    m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
    m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
282 283
    t2 = glutGet(GLUT_ELAPSED_TIME);
    seconds = (t2 - t1) / 1000.0f;
284
    CGoGNout << "initPrimitives: "<< seconds << "sec" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
285 286 287 288 289 290

    // creation des primitives de rendu de la topologie a partir de la carte
    t1 = glutGet(GLUT_ELAPSED_TIME);
	//m_render_topo->updateData<PFP>(myMap,allDarts,position,coefTopoExplod[0],coefTopoExplod[1],coefTopoExplod[2]);
    t2 = glutGet(GLUT_ELAPSED_TIME);
    seconds = (t2 - t1) / 1000.0f;
291
    CGoGNout << "map update: "<< seconds << "sec" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
}

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

	//Rendering
	TwAddVarCB(viewer_bar, "Show Object", TW_TYPE_BOOL8, setRenderObjectCB, getRenderObjectCB, this, "");
	TwType tw_render_style = TwDefineEnum("Shading","All,Faces,Lines,Explod") ;
	TwAddVarCB(viewer_bar, "Render Style", tw_render_style, setRenderObjStyleCB, getRenderObjStyleCB, this, "") ;
	TwAddVarCB(viewer_bar, "Show Topology", TW_TYPE_BOOL8, setRenderTopoCB, getRenderTopoCB, this, "") ;
	TwType tw_render_topo = TwDefineEnum("TopoShading","All,Explod");
	TwAddVarCB(viewer_bar, "Render Topology", tw_render_topo, setRenderTopoStyleCB, getRenderTopoStyleCB, this, "") ;
	TwAddVarRW(viewer_bar, "phi1_explod", TW_TYPE_FLOAT, &coefTopoExplod[1], "min=0 max=1 step=0.1 label='Phi1 Explod Coeff'");
	TwAddVarRW(viewer_bar, "phi2_explod", TW_TYPE_FLOAT, &coefTopoExplod[2], "min=0 max=1 step=0.1 label='Phi2 Explod Coeff'");
	TwAddVarRW(viewer_bar, "phi3_explod", TW_TYPE_FLOAT, &coefTopoExplod[0], "min=0 max=1 step=0.1 label='Phi3 Explod Coeff'");
	TwDefine("Viewer/phi1_explod visible=false");
	TwDefine("Viewer/phi2_explod visible=false");
	TwDefine("Viewer/phi3_explod visible=false");

	TwAddSeparator(viewer_bar, "", "") ;

	//Visualisation
	TwAddVarRW(viewer_bar, "background color", TW_TYPE_COLOR4F, &backCol, "");
}

/******** Partie a supprimer ********/
void maillageTest()
{
	/// on creer une carte
324 325
	Dart dd = Algo::Modelisation::Polyhedron<PFP>::createPolyhedron(myMap,4);
	Dart ee = Algo::Modelisation::Polyhedron<PFP>::createPolyhedron(myMap,4);
Pierre Kraemer's avatar
Pierre Kraemer committed
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 351 352 353
    myMap.sewVolumes(dd,ee);

    // plongement
    Dart t1 = dd;
    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(dd));
    position[t1] = PFP::VEC3(0.5f, 0.5f, 4.0f);

    Dart t2 = ee;
    position[t2] = PFP::VEC3(0.0f, 1.0f, 3.0f);
    t2 = myMap.phi1(t2);
    position[t2] = PFP::VEC3(0.0f, 0.0f, 3.0f);
    t2 = myMap.phi1(t2);
    position[t2] = PFP::VEC3(1.0f, 0.5f, 3.0f);
    t2 = myMap.phi_1(myMap.phi2(ee));
    position[t2] = PFP::VEC3(0.5f, 0.5f, 2.0f);
}
/***************************/

int main(int argc, char **argv)
{
	/*
	 * Chargement de maillages
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
354
	std::vector<std::string> attrNames ;
Pierre Kraemer's avatar
Pierre Kraemer committed
355 356
	if(argc < 2)
	{
357 358 359 360 361
			CGoGNerr << "usage : " << argv[0] << " -off" << CGoGNendl;
			CGoGNerr << "or" << CGoGNendl;
			CGoGNerr << "usage : " << argv[0] << " -tet" << CGoGNendl;
			CGoGNerr << "or" << CGoGNendl;
			CGoGNerr << "usage : " << argv[0] << " -ts" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
362 363 364 365
			return 1;
	}
	else if(std::string(argv[1]) == "-off")
	{
366
		CGoGNout << argv[1] << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
367
		if (argc != 4) {
368
			CGoGNerr << "usage : " << argv[0] << " -off <filename.off> <filename.ele>" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
369 370 371
			return 1;
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
372
		Algo::Import::importOFFWithELERegions<PFP>(myMap,argv[2],argv[3],attrNames);
373
		position = myMap.getAttribute<PFP::VEC3>(VERTEX, attrNames[0]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
374 375 376 377
	}
	else if(std::string(argv[1]) == "-tet")
	{
			if (argc != 3) {
378
				CGoGNerr << "usage : " << argv[0] << " -tet <filename.tet>" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
379 380 381
				return 1;
			}

Pierre Kraemer's avatar
Pierre Kraemer committed
382
			Algo::Import::importTet<PFP>(myMap, argv[2], attrNames);
383
			position = myMap.getAttribute<PFP::VEC3>(VERTEX, attrNames[0]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
384 385 386 387
	}
	else if(std::string(argv[1]) == "-ts")
	{
			if (argc != 3) {
388
				CGoGNerr << "usage : " << argv[0] << " -ts <filename.ts>" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
389 390 391
				return 1;
			}

Pierre Kraemer's avatar
Pierre Kraemer committed
392
			Algo::Import::importTs<PFP>(myMap,argv[2],attrNames);
393
			position = myMap.getAttribute<PFP::VEC3>(VERTEX, attrNames[0]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
394

395
			//scalar = myMap.getAttribute<PFP::REAL>(VERTEX, "scalar");
Pierre Kraemer's avatar
Pierre Kraemer committed
396 397 398 399 400 401
	}
	else
	{
		maillageTest();
	}

402 403 404
	//CGoGNout << "Nb Tetrahedron = " << myMap.getNbOrbits(VOLUME) << CGoGNendl;
	//		" / Nb Edges = " << myMap.getNbOrbits(EDGE) <<
	//		" / Nb Vertices = " << myMap.getNbOrbits(VERTEX) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
405 406 407 408 409 410 411 412 413 414 415 416

    // un peu d'interface
	MyGlutWin* mgw = new MyGlutWin(&argc,argv,1200,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
417 418 419
    mgw->m_render = new Algo::Render::GL2::MapRender_VBO();
    //mgw->m_render_topo = new Algo::Render::GL2::topo3_VBORenderMap<PFP::MAP>();
    mgw->m_render_topo = new Algo::Render::GL2::topo3_VBORenderMapD();
Pierre Kraemer's avatar
Pierre Kraemer committed
420 421 422 423 424 425 426 427 428 429 430 431

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

    delete mgw->m_render;
    delete mgw->m_render_topo;
    delete mgw;

    return 0;
}