testPlaneCutting.cpp 13.3 KB
Newer Older
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/*******************************************************************************
* 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: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "testPlaneCutting.h"

#include "Algo/Geometry/distances.h"
#include "Algo/Modelisation/subdivision.h"
#include "Utils/chrono.h"
#include "Algo/Modelisation/planeCutting.h"

Viewer::Viewer() :
	m_renderStyle(FLAT),
	m_drawVertices(false),
	m_drawEdges(false),
	m_drawFaces(true),
	m_drawNormals(false),
	m_drawTopo(false),
	m_drawBoundaryTopo(true),
	m_render(NULL),
	m_phongShader(NULL),
	m_flatShader(NULL),
	m_vectorShader(NULL),
	m_simpleColorShader(NULL),
	m_pointSprite(NULL)
{
	normalScaleFactor = 1.0f ;
	vertexScaleFactor = 0.1f ;
	faceShrinkage = 1.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 ;
}

void Viewer::initGUI()
{
untereiner's avatar
untereiner committed
60
	setDock(&dock) ;
61

untereiner's avatar
untereiner committed
62 63 64 65
	dock.check_drawVertices->setChecked(false) ;
	dock.check_drawEdges->setChecked(false) ;
	dock.check_drawFaces->setChecked(true) ;
	dock.check_drawNormals->setChecked(false) ;
66

untereiner's avatar
untereiner committed
67 68
	dock.slider_verticesSize->setVisible(false) ;
	dock.slider_normalsSize->setVisible(false) ;
69

untereiner's avatar
untereiner committed
70 71
	dock.slider_verticesSize->setSliderPosition(50) ;
	dock.slider_normalsSize->setSliderPosition(50) ;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

	setCallBack( dock.check_drawVertices, SIGNAL(toggled(bool)), SLOT(slot_drawVertices(bool)) ) ;
	setCallBack( dock.slider_verticesSize, SIGNAL(valueChanged(int)), SLOT(slot_verticesSize(int)) ) ;
	setCallBack( dock.check_drawEdges, SIGNAL(toggled(bool)), SLOT(slot_drawEdges(bool)) ) ;
	setCallBack( dock.check_drawFaces, SIGNAL(toggled(bool)), SLOT(slot_drawFaces(bool)) ) ;
	setCallBack( dock.combo_faceLighting, SIGNAL(currentIndexChanged(int)), SLOT(slot_faceLighting(int)) ) ;
	setCallBack( dock.check_drawTopo, SIGNAL(toggled(bool)), SLOT(slot_drawTopo(bool)) ) ;
	setCallBack( dock.check_drawNormals, SIGNAL(toggled(bool)), SLOT(slot_drawNormals(bool)) ) ;
	setCallBack( dock.slider_normalsSize, SIGNAL(valueChanged(int)), SLOT(slot_normalsSize(int)) ) ;
}

void Viewer::cb_initGL()
{
	Utils::GLSLShader::setCurrentOGLVersion(2) ;

	m_render = new Algo::Render::GL2::MapRender() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
88
	m_topoRender = new Algo::Render::GL2::TopoRenderMap<PFP>() ;
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 187 188 189 190 191 192 193 194 195 196 197 198

	m_topoRender->setInitialDartsColor(0.25f, 0.25f, 0.25f) ;

	m_positionVBO = new Utils::VBO() ;
	m_normalVBO = new Utils::VBO() ;

	m_phongShader = new Utils::ShaderPhong() ;
	m_phongShader->setAttributePosition(m_positionVBO) ;
	m_phongShader->setAttributeNormal(m_normalVBO) ;
	m_phongShader->setAmbiant(colClear) ;
	m_phongShader->setDiffuse(colDif) ;
	m_phongShader->setSpecular(colSpec) ;
	m_phongShader->setShininess(shininess) ;

	m_flatShader = new Utils::ShaderFlat() ;
	m_flatShader->setAttributePosition(m_positionVBO) ;
	m_flatShader->setAmbiant(colClear) ;
	m_flatShader->setDiffuse(colDif) ;
	m_flatShader->setExplode(faceShrinkage) ;

	m_vectorShader = new Utils::ShaderVectorPerVertex() ;
	m_vectorShader->setAttributePosition(m_positionVBO) ;
	m_vectorShader->setAttributeVector(m_normalVBO) ;
	m_vectorShader->setColor(colNormal) ;

	m_simpleColorShader = new Utils::ShaderSimpleColor() ;
	m_simpleColorShader->setAttributePosition(m_positionVBO) ;
	Geom::Vec4f c(0.0f, 0.0f, 0.0f, 1.0f) ;
	m_simpleColorShader->setColor(c) ;

	m_pointSprite = new Utils::PointSprite() ;
	m_pointSprite->setAttributePosition(m_positionVBO) ;
	m_pointSprite->setColor(Geom::Vec4f(0.0f, 0.0f, 1.0f, 1.0f)) ;

	registerShader(m_phongShader) ;
	registerShader(m_flatShader) ;
	registerShader(m_vectorShader) ;
	registerShader(m_simpleColorShader) ;
	registerShader(m_pointSprite) ;
}

void Viewer::cb_redraw()
{
	if(m_drawVertices)
	{
		m_pointSprite->setSize(vertexScaleFactor) ;
		m_render->draw(m_pointSprite, Algo::Render::GL2::POINTS) ;
	}

	if(m_drawEdges)
	{
		glLineWidth(1.0f) ;
		m_render->draw(m_simpleColorShader, Algo::Render::GL2::LINES) ;
	}

	if(m_drawFaces)
	{
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) ;
		glEnable(GL_LIGHTING) ;
		glEnable(GL_POLYGON_OFFSET_FILL) ;
		glPolygonOffset(1.0f, 1.0f) ;
		switch(m_renderStyle)
		{
			case FLAT :
				m_flatShader->setExplode(faceShrinkage) ;
				m_render->draw(m_flatShader, Algo::Render::GL2::TRIANGLES) ;
				break ;
			case PHONG :
				m_render->draw(m_phongShader, Algo::Render::GL2::TRIANGLES) ;
				break ;
		}
		glDisable(GL_POLYGON_OFFSET_FILL) ;
	}

	if(m_drawTopo)
	{
		m_topoRender->drawTopo() ;
	}

	if(m_drawNormals)
	{
		float size = normalBaseSize * normalScaleFactor ;
		m_vectorShader->setScale(size) ;
		glLineWidth(1.0f) ;
		m_render->draw(m_vectorShader, Algo::Render::GL2::POINTS) ;
	}
}

void Viewer::cb_Open()
{
	std::string filters("all (*.*);; trian (*.trian);; ctm (*.ctm);; off (*.off);; ply (*.ply)") ;
	std::string filename = selectFile("Open Mesh", "", filters) ;
	if (filename.empty())
		return ;

	importMesh(filename) ;
	updateGL() ;
}

void Viewer::cb_Save()
{
	std::string filters("all (*.*);; map (*.map);; off (*.off);; ply (*.ply)") ;
	std::string filename = selectFileSave("Save Mesh", "", filters) ;

	if (!filename.empty())
		exportMesh(filename) ;
}

void Viewer::cb_keyPress(int keycode)
{
untereiner's avatar
untereiner committed
199
	switch(keycode)
200
	{
untereiner's avatar
untereiner committed
201 202 203
		case 'c' :
			myMap.check();
			break;
204

untereiner's avatar
untereiner committed
205 206
		case 'p':
		{
207

untereiner's avatar
untereiner committed
208 209 210
			std::cout << "PlaneCut"<< std::endl;
			Geom::Vec3f n(0.1,0.1,1.0);
			Geom::Vec3f o = bb.center();
211

untereiner's avatar
untereiner committed
212
			Geom::Plane3D<PFP::REAL> plan(n,o);
213

Pierre Kraemer's avatar
Pierre Kraemer committed
214
			CellMarker<MAP, FACE> over(myMap);
untereiner's avatar
untereiner committed
215
			Algo::Surface::Modelisation::planeCut<PFP>(myMap, position, plan, over, true, true);
216

untereiner's avatar
untereiner committed
217 218
			std::cout << "PlaneCut Ok"<< std::endl;
			n *= bb.diagSize()/20.0f;
219

untereiner's avatar
untereiner committed
220 221 222 223 224 225
			TraversorV<PFP::MAP> trav(myMap);
			for (Dart d=trav.begin(); d!=trav.end(); d=trav.next())
			{
				if (over.isMarked(d))
					position[d]+= n;
			}
226

untereiner's avatar
untereiner committed
227 228 229
			m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::POINTS) ;
			m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::LINES) ;
			m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES) ;
230

untereiner's avatar
untereiner committed
231 232 233
			Algo::Surface::Geometry::computeNormalVertices<PFP>(myMap, position, normal) ;
			m_positionVBO->updateData(position) ;
			m_normalVBO->updateData(normal) ;
234

Pierre Kraemer's avatar
Pierre Kraemer committed
235
			m_topoRender->updateData(myMap, position, 0.85f, 0.85f, m_drawBoundaryTopo) ;
untereiner's avatar
untereiner committed
236
			updateGL();
237

untereiner's avatar
untereiner committed
238 239 240
			break;
		}
		case 'P':
241 242
		{

untereiner's avatar
untereiner committed
243 244 245 246
			std::cout << "PlaneCut"<< std::endl;
			Geom::Vec3f n(0.1,0.1,1.0);
			Geom::Vec3f o = bb.center();

Lionel Untereiner's avatar
Lionel Untereiner committed
247
            Geom::Plane3D<PFP::REAL> plan(n,o);
untereiner's avatar
untereiner committed
248

Pierre Kraemer's avatar
Pierre Kraemer committed
249
			CellMarker<MAP, FACE> over(myMap);
untereiner's avatar
untereiner committed
250 251 252 253 254
			Algo::Surface::Modelisation::planeCut2<PFP>(myMap, position, plan, over, true);

			std::cout << "PlaneCut Ok"<< std::endl;
			n *= bb.diagSize()/20.0f;

Pierre Kraemer's avatar
Pierre Kraemer committed
255
			TraversorV<MAP> trav(myMap);
untereiner's avatar
untereiner committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269
			for (Dart d=trav.begin(); d!=trav.end(); d=trav.next())
			{
				if (over.isMarked(d))
					position[d]+= n;
			}

			m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::POINTS) ;
			m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::LINES) ;
			m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES) ;

			Algo::Surface::Geometry::computeNormalVertices<PFP>(myMap, position, normal) ;
			m_positionVBO->updateData(position) ;
			m_normalVBO->updateData(normal) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
270
			m_topoRender->updateData(myMap, position, 0.85f, 0.85f, m_drawBoundaryTopo) ;
untereiner's avatar
untereiner committed
271 272 273 274
			updateGL();

			break;
		}
Lionel Untereiner's avatar
Lionel Untereiner committed
275 276 277 278 279 280 281 282 283 284
        case 'S':
        {
            Geom::Vec3f p1(0.0,1.0,0.0);
            Geom::Vec3f p2(1.0,0.0,0.0);
            Geom::Vec3f p3(1.0,1.0,0.0);

            Geom::Plane3D<PFP::REAL> plan(p1, p2, p3);


        }
untereiner's avatar
untereiner committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

		case 'd':
		{
			Utils::Chrono ch;
			ch.start();
			VEC3 P(0.6f,0.55f,0.51f);
			float dist = 10000.0f;
			TraversorF<PFP::MAP> trav(myMap);
			unsigned int nb=0;
			for (Dart d=trav.begin(); d != trav.end(); d=trav.next())
			{
				nb++;
				float d2 = Algo::Geometry::squaredDistancePoint2Face<PFP>(myMap,d,position,P);
				if (d2<dist)
					dist = d2;

			}
			std::cout << "Dist="<< sqrt(dist) << " of "<< nb << "faces in "<< ch.elapsed()<< " ms"<< std::endl;

			break;
305 306
		}

untereiner's avatar
untereiner committed
307 308 309
		default:
			break;
	}
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
}

void Viewer::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<PFP::VEC3, VERTEX>(attrNames[0]) ;
	}

untereiner's avatar
untereiner committed
335
	//	myMap.enableQuickTraversal<VERTEX>() ;
336 337 338 339 340

	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::POINTS) ;
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::LINES) ;
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
341
	m_topoRender->updateData(myMap, position, 0.85f, 0.85f, m_drawBoundaryTopo) ;
342 343 344

	bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position) ;
	normalBaseSize = bb.diagSize() / 100.0f ;
untereiner's avatar
untereiner committed
345
	//	vertexBaseSize = normalBaseSize / 5.0f ;
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

	normal = myMap.getAttribute<VEC3, VERTEX>("normal") ;
	if(!normal.isValid())
		normal = myMap.addAttribute<VEC3, VERTEX>("normal") ;

	Algo::Surface::Geometry::computeNormalVertices<PFP>(myMap, position, normal) ;

	m_positionVBO->updateData(position) ;
	m_normalVBO->updateData(normal) ;

	setParamObject(bb.maxSize(), bb.center().data()) ;
	updateGLMatrices() ;
}

void Viewer::exportMesh(std::string& filename, bool askExportMode)
{
	size_t pos = filename.rfind(".") ;    // position of "." in filename
	std::string extension = filename.substr(pos) ;

	if (extension == std::string(".off"))
		Algo::Surface::Export::exportOFF<PFP>(myMap, position, filename.c_str()) ;
	else if (extension.compare(0, 4, std::string(".ply")) == 0)
	{
		int ascii = 0 ;
		if (askExportMode)
			Utils::QT::inputValues(Utils::QT::VarCombo("binary mode;ascii mode",ascii,"Save in")) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
373
		std::vector<VertexAttribute<VEC3, MAP_IMPL>*> attributes ;
374 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 409 410 411 412 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 438 439 440 441 442 443 444 445 446 447 448 449 450 451
		attributes.push_back(&position) ;
		Algo::Surface::Export::exportPLYnew<PFP>(myMap, attributes, filename.c_str(), !ascii) ;
	}
	else if (extension == std::string(".map"))
		myMap.saveMapBin(filename) ;
	else
		std::cerr << "Cannot save file " << filename << " : unknown or unhandled extension" << std::endl ;
}

void Viewer::slot_drawVertices(bool b)
{
	m_drawVertices = b ;
	updateGL() ;
}

void Viewer::slot_verticesSize(int i)
{
	vertexScaleFactor = i / 500.0f ;
	updateGL() ;
}

void Viewer::slot_drawEdges(bool b)
{
	m_drawEdges = b ;
	updateGL() ;
}

void Viewer::slot_drawFaces(bool b)
{
	m_drawFaces = b ;
	if (b)
	{
		Geom::Vec4f c(0.0f, 0.0f, 0.0f, 1.0f) ;
		m_simpleColorShader->setColor(c) ;
	}
	else
	{
		Geom::Vec4f c(0.9f, 0.9f, 0.1f, 1.0f) ;
		m_simpleColorShader->setColor(c) ;
	}

	updateGL() ;
}

void Viewer::slot_faceLighting(int i)
{
	m_renderStyle = i ;
	updateGL() ;
}

void Viewer::slot_drawTopo(bool b)
{
	m_drawTopo = b ;
	updateGL() ;
}

void Viewer::slot_drawNormals(bool b)
{
	m_drawNormals = b ;
	updateGL() ;
}

void Viewer::slot_normalsSize(int i)
{
	normalScaleFactor = i / 50.0f ;
	updateGL() ;
}

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

int main(int argc, char **argv)
{
	QApplication app(argc, argv) ;

	Viewer sqt ;
	sqt.setGeometry(0, 0, 1000, 800) ;
untereiner's avatar
untereiner committed
452
	sqt.show() ;
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

	if(argc >= 2)
	{
		std::string filename(argv[1]) ;
		sqt.importMesh(filename) ;
		if(argc >= 3)
		{
			std::string filenameExp(argv[2]) ;
			std::cout << "Exporting " << filename << " as " << filenameExp << " ... "<< std::flush ;
			sqt.exportMesh(filenameExp, false) ;
			std::cout << "done!" << std::endl ;

			return (0) ;
		}
	}

	sqt.initGUI() ;

	return app.exec() ;
}