test_traversor2.cpp 9.6 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
		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();
}

62 63 64 65 66 67 68 69 70 71 72
void fonct(const Cell<VERTEX>& v)
{
	std::cout << v.dart << std::endl;
}

void fonct2(Dart d)
{
	std::cout << d << std::endl;
}


Sylvain Thery's avatar
Sylvain Thery committed
73 74 75 76 77
void MyQT::traversors(int x)
{
	// update all color to grey
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
	{
78
		m_render_topo->addColoredDart(d,0.5f,0.5f,0.5f);
Sylvain Thery's avatar
Sylvain Thery committed
79 80 81 82 83 84 85 86
	}

	switch(x)
	{
	case 0:
	{
		Traversor2VVaE<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
87
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
88

Sylvain Thery's avatar
Sylvain Thery committed
89 90 91 92
	}
		break;
	case 1:
	{
93

Sylvain Thery's avatar
Sylvain Thery committed
94 95
		Traversor2VVaF<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
96
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
97 98 99 100 101 102
	}
		break;
	case 2:
	{
		Traversor2EEaV<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
103
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
104 105 106 107 108 109 110
	}
		break;
	case 3:
	{
		Traversor2EEaF<MAP> trav(myMap,m_selected);
		float c=0.0f;
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
111
			m_render_topo->addColoredDart(b,0.0f,1.0f,c);
Sylvain Thery's avatar
Sylvain Thery committed
112 113 114 115 116 117
	}
		break;
	case 4:
	{
		Traversor2FFaV<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
118
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
119 120 121 122 123 124
	}
		break;
	case 5:
	{
		Traversor2FFaE<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
125
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
126 127 128 129 130 131
	}
		break;
	case 6:
	{
		Traversor2FV<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
132
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
133 134 135 136 137 138
	}
		break;
	case 7:
	{
		Traversor2FE<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
139
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
140 141 142 143 144 145
	}
		break;
	case 8:
	{
		Traversor2EV<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
146
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
147 148 149 150 151 152
	}
		break;
	case 9:
	{
		Traversor2EF<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
153
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
154 155 156 157 158 159
	}
		break;
	case 10:
	{
		Traversor2VE<MAP> trav(myMap,m_selected);
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
160
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
161 162 163 164
	}
		break;
	case 11:
	{
165
		Traversor2VF<MAP> trav(myMap,m_selected);
Sylvain Thery's avatar
Sylvain Thery committed
166
		for(Dart b=trav.begin(); b!= trav.end(); b=trav.next())
167
			m_render_topo->addColoredDart(b,0.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
168 169 170 171 172 173 174 175 176 177 178
	}
		break;
	default:
		break;
	}

	updateGL();
}

void MyQT::createMap(int n)
{
179
	position = myMap.addAttribute<VEC3, VERTEX, MAP>("position");
Sylvain Thery's avatar
Sylvain Thery committed
180

181
	Algo::Surface::Tilings::Square::Grid<PFP> grid(myMap, n, n, true);
Sylvain Thery's avatar
Sylvain Thery committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    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);

198 199 200 201 202 203 204 205 206 207
	std::cout << "markAll OK"<< std::endl;


	#ifdef USE_GMAP
	m_render_topo->updateDataGMap<PFP>(myMap, position, 0.9f, 0.9f, dock.withBoundary->isChecked());
	#else
	m_render_topo->updateData<PFP>(myMap, position, 0.9f, 0.9f, dock.withBoundary->isChecked());
	#endif

	std::cout << "markAll OK"<< std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
208 209 210 211 212 213

}

void MyQT::updateMap()
{
	m_render_topo->setInitialBoundaryDartsColor(0.0f,0.0f,0.0f);
214 215 216 217 218 219
	#ifdef USE_GMAP
	m_render_topo->updateData<PFP>(myMap, position, 0.9f, 0.9f, dock.withBoundary->isChecked());
	#else
	m_render_topo->updateDataGMap<PFP>(myMap, position, 0.9f, 0.9f, dock.withBoundary->isChecked());
	#endif

Sylvain Thery's avatar
Sylvain Thery committed
220 221 222 223 224 225 226
	updateGL();
}

// initialization GL callback
void MyQT::cb_initGL()
{
	glClearColor(1.0f,1.0f,1.0f,1.0f);
227
	m_render_topo = new Algo::Render::GL2::TopoRender(0.01f) ;
Sylvain Thery's avatar
Sylvain Thery committed
228 229 230 231 232 233
}

// redraw GL callback (clear and swap already done)
void MyQT::cb_redraw()
{

234
	glDepthFunc(GL_LESS);
Sylvain Thery's avatar
Sylvain Thery committed
235 236
	m_render_topo->drawTopo();

237 238
	glDepthFunc(GL_LEQUAL);
	m_render_topo->drawColoredDarts(myMap);
Sylvain Thery's avatar
Sylvain Thery committed
239 240

	if (m_selected != NIL)
241
		m_render_topo->overdrawDart(myMap, m_selected, 11, 1.0f, 0.0f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
242 243

	if (m_selected2 != NIL)
244
		m_render_topo->overdrawDart(myMap, m_selected2, 11, 0.0f, 1.0f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
245 246 247 248 249 250
}

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

			if (d != Dart::nil())
Pierre Kraemer's avatar
Pierre Kraemer committed
258
				std::cout << "DART=" << d.index << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
		}
		if (button == Qt::RightButton)
		{
			if (d != Dart::nil())
				m_selected2 = d;
		}
		updateGL();
	}
}

void MyQT::cb_keyPress(int keycode)
{
	switch(keycode)
	{
	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()
{
	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);
336
		position = myMap.getAttribute<VEC3, VERTEX, MAP>("position") ;
Sylvain Thery's avatar
Sylvain Thery committed
337 338 339 340 341 342 343 344 345
	}
	else
	{
		std::vector<std::string> attrNames ;
		if(!Algo::Surface::Import::importMesh<PFP>(myMap, filename.c_str(), attrNames))
		{
			CGoGNerr << "could not import " << filename << CGoGNendl ;
			return;
		}
346
		position = myMap.getAttribute<VEC3, VERTEX, MAP>(attrNames[0]) ;
Sylvain Thery's avatar
Sylvain Thery committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	}


	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();
}