test_traversor2.cpp 11.2 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, 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: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "test_traversor2.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Tiling/Surface/square.h"
#include "Algo/Geometry/centroid.h"
#include "Algo/Import/import.h"
#include "Algo/Export/export.h"

using namespace CGoGN ;

int main(int argc, char **argv)
{
	//	// interface
	QApplication app(argc, argv);
	MyQT sqt;

    sqt.setDock(& sqt.dock);
	sqt.setCallBack( sqt.dock.listTravers, SIGNAL(currentRowChanged(int)), SLOT(traversors(int)) );
	sqt.setCallBack( sqt.dock.withBoundary, SIGNAL(clicked()), SLOT(updateMap()) );
    sqt.setCallBack( sqt.dock.svg, SIGNAL(clicked()), SLOT(svg()) );
    sqt.setCallBack( sqt.dock.widthSlider, SIGNAL(valueChanged(int)), SLOT(width(int)) );
Pierre Kraemer's avatar
Pierre Kraemer committed
45 46
	int n = 3;
	if (argc == 2)
Sylvain Thery's avatar
Sylvain Thery committed
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 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
		n = atoi(argv[1]);

	// example code itself
	sqt.createMap(n);

	sqt.width(5);

	// set help message in menu
	sqt.setHelpMsg("First Tuto: \nCreate two faces\nsew them\nand affect positions");
	// final show for redraw
	sqt.show();
	// and wait for the end
	return app.exec();
}

void MyQT::traversors(int x)
{
	// update all color to grey
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
	{
		colorDarts[d] =  Geom::Vec3f(0.5f,0.5f,0.5f);
		m_render_topo->setDartColor(d,0.5f,0.5f,0.5f);
	}

	switch(x)
	{
	case 0:
	{
		Traversor2VVaE<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 1:
	{
		Traversor2VVaF<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 2:
	{
		Traversor2EEaV<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 3:
	{
		Traversor2EEaF<MAP> trav(myMap,m_selected);
		float c=0.0f;
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,c);
	}
		break;
	case 4:
	{
		Traversor2FFaV<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 5:
	{
		Traversor2FFaE<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 6:
	{
		Traversor2FV<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 7:
	{
		Traversor2FE<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 8:
	{
		Traversor2EV<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 9:
	{
		Traversor2EF<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 10:
	{
		Traversor2VE<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	case 11:
	{
		Traversor2VF<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
			m_render_topo->setDartColor(b,0.0f,1.0f,0.0f);
	}
		break;
	default:
		break;
	}

	updateGL();
}

void MyQT::createMap(int n)
{
	position = myMap.addAttribute<VEC3, VERTEX>("position");
	colorDarts = myMap.addAttribute<VEC3, DART>("color");

    Algo::Surface::Tilings::Square::Grid<PFP> grid(myMap, n, n, true);
    grid.embedIntoGrid(position, 1.,1.,0.);

    //  bounding box of scene
	Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position) ;
	setParamObject(bb.maxSize(), bb.center().data()) ;
	m_shift = bb.maxSize()/200.0f;

	// first show for be sure that GL context is binded
	show();

	dm.markAll();

	m_render_topo->setDartWidth(4.0f);
	m_render_topo->setInitialDartsColor(0.5f,0.5f,0.5f);
	m_render_topo->setInitialBoundaryDartsColor(0.3f,0.3f,0.3f);

Pierre Kraemer's avatar
Pierre Kraemer committed
187
	m_render_topo->updateData(myMap, position, 0.9f, 0.9f, dock.withBoundary->isChecked());
Sylvain Thery's avatar
Sylvain Thery committed
188

Pierre Kraemer's avatar
Pierre Kraemer committed
189
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
Sylvain Thery's avatar
Sylvain Thery committed
190
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
191
		if (dm.isMarked(d) && (!myMap.isBoundaryMarked<2>(d)))
Sylvain Thery's avatar
Sylvain Thery committed
192 193 194 195 196 197 198 199 200 201
		{
			colorDarts[d] =  Geom::Vec3f(0.5f,0.5f,0.5f);
			m_render_topo->setDartColor(d,0.5f,0.5f,0.5f);
		}
	}
}

void MyQT::updateMap()
{
	m_render_topo->setInitialBoundaryDartsColor(0.0f,0.0f,0.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
202 203
	m_render_topo->updateData(myMap, position, 0.9f, 0.9f, dock.withBoundary->isChecked());
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
Sylvain Thery's avatar
Sylvain Thery committed
204
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
205
		if (dm.isMarked(d) && (!myMap.isBoundaryMarked<2>(d)))
Sylvain Thery's avatar
Sylvain Thery committed
206 207 208 209 210 211 212 213 214 215 216 217 218
		{
			const Geom::Vec3f& C = colorDarts[d];
			if (C*C != 0.0f)
				m_render_topo->setDartColor(d,C[0],C[1],C[2]);
		}
	}
	updateGL();
}

// initialization GL callback
void MyQT::cb_initGL()
{
	glClearColor(1.0f,1.0f,1.0f,1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220 221 222 223
#ifdef USE_GMAP
	m_render_topo = new Algo::Render::GL2::TopoRenderGMap<PFP>(0.01f) ;
#else
	m_render_topo = new Algo::Render::GL2::TopoRenderMap<PFP>(0.01f) ;
#endif
Sylvain Thery's avatar
Sylvain Thery committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

// redraw GL callback (clear and swap already done)
void MyQT::cb_redraw()
{
	glEnable( GL_POLYGON_OFFSET_FILL );
	glPolygonOffset( 1.0f, 1.0f );

	m_render_topo->drawTopo();

	glDisable( GL_POLYGON_OFFSET_FILL );

	if (m_selected != NIL)
		m_render_topo->overdrawDart(m_selected, 11, 1.0f, 0.0f, 0.0f);

	if (m_selected2 != NIL)
		m_render_topo->overdrawDart(m_selected2, 11, 0.0f, 1.0f, 0.0f);
}

void MyQT::cb_mousePress(int button, int x, int y)
{
	if (Shift())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
247
		Dart d = m_render_topo->picking(myMap, x,y); // nb
Sylvain Thery's avatar
Sylvain Thery committed
248 249 250 251 252 253
		if (button == Qt::LeftButton)
		{
			if (d != Dart::nil())
				m_selected = d;

			if (d != Dart::nil())
Pierre Kraemer's avatar
Pierre Kraemer committed
254
				std::cout << "DART=" << d.index << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
		}
		if (button == Qt::RightButton)
		{
			if (d != Dart::nil())
				m_selected2 = d;
		}
		updateGL();
	}
}

void MyQT::cb_keyPress(int keycode)
{
	switch(keycode)
	{
	case 'c':
		for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
272
			if (!myMap.isBoundaryMarked<2>(d))
Sylvain Thery's avatar
Sylvain Thery committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
			{
				int n = rand();
				float r = float(n&0x7f)/255.0f + 0.25f;
				float g = float((n>>8)&0x7f)/255.0f + 0.25f;
				float b = float((n>>16)&0x7f)/255.0 + 0.25f;
				colorDarts[d] =  Geom::Vec3f(r,g,b);
				m_render_topo->setDartColor(d,r,g,b);
			}
		}
		break;
	case 'g':
		for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
		{
			colorDarts[d] =  Geom::Vec3f(0.5f,0.5f,0.5f);
			m_render_topo->setDartColor(d,0.5f,0.5f,0.5f);
		}
		break;

	case 'h':
		for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
294
			if (!myMap.isBoundaryMarked<2>(d))
Sylvain Thery's avatar
Sylvain Thery committed
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 348 349 350 351 352 353 354 355 356 357 358 359 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 385
			{
				colorDarts[d] =  Geom::Vec3f(0.0f,0.0f,0.0f);
				m_render_topo->setDartColor(d,0.0f,0.0f,0.0f);
			}
		}
		break;
	case Qt::Key_Up:
		if (m_selected!=NIL)
			position[m_selected][1] += m_shift;
		updateMap();
		updateGL();
		break;
	case Qt::Key_Down:
		if (m_selected!=NIL)
			position[m_selected][1] -= m_shift;
		updateMap();
		updateGL();
		break;
	case Qt::Key_Left:
		if (m_selected!=NIL)
			position[m_selected][0] -= m_shift;
		updateMap();
		updateGL();
		break;
	case Qt::Key_Right:
		if (m_selected!=NIL)
			position[m_selected][0] += m_shift;
		updateMap();
		updateGL();
		break;

	default:
		break;
	}
	updateGL();
}

void MyQT::svg()
{
	if (m_selected!=NIL)
		m_render_topo->setDartColor(m_selected,0.8f,0.0f,0.0f);
	if (m_selected2!=NIL)
		m_render_topo->setDartColor(m_selected2,0.0f,0.8f,0.0f);

	std::string filename = selectFileSave("snapshot file", ".", "(*.svg)");
	m_render_topo->svgout2D(filename, modelViewMatrix(),projectionMatrix());
}

void MyQT::cb_Open()
{
	std::string filters("all (*.*);; trian (*.trian);; off (*.off);; ply (*.ply);; map (*.map)") ;
	std::string filename = selectFile("Open Mesh", "", filters) ;
	if (!filename.empty())
		importMesh(filename);
	updateGL();
}

void MyQT::cb_Save()
{
	std::string filename = selectFileSave("Export Off file ",".","(*.off)");
	Algo::Surface::Export::exportOFF<PFP>(myMap,position,filename.c_str());
	//std::cout << "could not save file : " << filename << std::endl;
}

void MyQT::importMesh(std::string& filename)
{
	myMap.clear(true) ;

	size_t pos = filename.rfind(".");    // position of "." in filename
	std::string extension = filename.substr(pos);

	if (extension == std::string(".map"))
	{
		myMap.loadMapBin(filename);
		position = myMap.getAttribute<VEC3, VERTEX>("position") ;
	}
	else
	{
		std::vector<std::string> attrNames ;
		if(!Algo::Surface::Import::importMesh<PFP>(myMap, filename.c_str(), attrNames))
		{
			CGoGNerr << "could not import " << filename << CGoGNendl ;
			return;
		}
		position = myMap.getAttribute<VEC3, VERTEX>(attrNames[0]) ;
	}

	colorDarts = myMap.getAttribute<VEC3, DART>("color");
	if (!colorDarts.isValid())
	{
		colorDarts = myMap.addAttribute<VEC3, DART>("color");
Pierre Kraemer's avatar
Pierre Kraemer committed
386
		for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
Sylvain Thery's avatar
Sylvain Thery committed
387
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
388
			if (dm.isMarked(d) && (!myMap.isBoundaryMarked<2>(d)))
Sylvain Thery's avatar
Sylvain Thery committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
			{
				int n = rand();
				float r = float(n&0x7f)/255.0f + 0.25f;
				float g = float((n>>8)&0x7f)/255.0f + 0.25f;
				float b = float((n>>16)&0x7f)/255.0 + 0.25f;
				colorDarts[d] =  Geom::Vec3f(r,g,b);
				m_render_topo->setDartColor(d,r,g,b);
			}
		}
	}

	m_selected  = NIL;
	m_selected2 = NIL;

	Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position) ;
	setParamObject(bb.maxSize(), bb.center().data()) ;
	m_shift = bb.maxSize()/200.0f;

	updateMap();
	updateGLMatrices() ;
}

void MyQT::width(int w)
{
	m_render_topo->setDartWidth(w);
	m_render_topo->setRelationWidth(w);
	updateGL();
}