Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

clipping.cpp 27.1 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2011, 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.u-strasbg.fr/                                         *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25
#include "clipping.h"
26
27
#include "Utils/static_assert.h"

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
/*******************************************************************************
 *														MISCELLANOUS
 *******************************************************************************/

void computeBasisFromVector(const Geom::Vec3f& vec1, Geom::Vec3f& vec2, Geom::Vec3f& vec3)
{
	const float epsilon = 0.0001f;

	// Check if the given vector length is acceptable
	if (vec1.norm() < epsilon)
		return;

	// First take a non colinear second vector to cross product with vec1
	// (by default (0.0, 0.0, 1.0)
	Geom::Vec3f tempVec (0.0, 0.0, 1.0);

	// Construct second vector, check other vectors non colinearity at the same time
	vec2 = vec1 ^ tempVec;
	float sinAngle = vec2.norm() / (vec1.norm() + tempVec.norm());
	if (sinAngle < epsilon) // f:x->sin(x) ~ f:x->x when x ~ 0
	{
		tempVec = Geom::Vec3f (1.0, 0.0, 0.0);
		vec2 = vec1 ^ tempVec;
	}

	// Get third vector
	vec3 = vec1 ^ vec2;

}

58
59
60
61
/*******************************************************************************
 *														SLOTS
 *******************************************************************************/

62
void Clipping::slot_drawTopo(bool b)
63
64
65
66
67
{
	m_drawTopo = b;
	updateGL();
}

68
void Clipping::slot_drawVertices(bool b)
69
70
71
72
73
{
	m_drawVertices = b;
	updateGL();
}

74
void Clipping::slot_drawLines(bool b)
75
76
77
78
79
{
	m_drawLines = b;
	updateGL();
}

80
void Clipping::slot_drawFaces(bool b)
81
82
83
84
85
{
	m_drawFaces = b;
	updateGL();
}

86
void Clipping::slot_explodTopoPhi1(double c)
87
88
89
90
91
92
{
	m_coeffTopoExplod[0] = (float)c;
	m_render_topo->updateData<PFP>(myMap, allDarts, position, m_coeffTopoExplod[0], m_coeffTopoExplod[1], m_coeffTopoExplod[2]);
	updateGL();
}

93
void Clipping::slot_explodTopoPhi2(double c)
94
95
96
97
98
99
{
	m_coeffTopoExplod[1] = (float)c;
	m_render_topo->updateData<PFP>(myMap, allDarts, position, m_coeffTopoExplod[0], m_coeffTopoExplod[1], m_coeffTopoExplod[2]);
	updateGL();
}

100
void Clipping::slot_explodTopoPhi3(double c)
101
102
103
104
105
106
{
	m_coeffTopoExplod[2] = (float)c;
	m_render_topo->updateData<PFP>(myMap, allDarts, position, m_coeffTopoExplod[0], m_coeffTopoExplod[1], m_coeffTopoExplod[2]);
	updateGL();
}

107
void Clipping::slot_pushButton_addPlane()
108
{
109
	// Create clipping and pickable objects
110
	unsigned int newPlaneId = m_shader->addClipPlane();
111
	Utils::Pickable* pickable = new Utils::Pickable(m_planeDrawable, newPlaneId);
Maire Nicolas's avatar
Maire Nicolas committed
112
	m_pickablePlanes.push_back(pickable);
113

114
115
	// Set params
	m_shader->setClipPlaneParamsOrigin(newPlaneId, m_bb.center());
116
	pickable->translate(m_bb.center());
117
	pickable->scale(Geom::Vec3f(m_bb.maxSize()*0.5));
118

119
	// Update shader sources edits
120
121
122
123
124
125
	dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc()));
	dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc()));

	updateGLMatrices();
}

126
void Clipping::slot_spinBox_GridResolution(int i)
127
{
128
	m_planeDrawable->updatePrecisionDrawing(i);
129
130
131
	updateGL();
}

132
void Clipping::slot_pushButton_changePlanesColor()
133
{
134
135
	QColor NewColor;
	QColorDialog ColDialog;
136

137
138
139
140
141
142
	NewColor = ColDialog.getColor();

	int r, g, b;
	NewColor.getRgb(&r, &g, &b);

	m_planeDrawable->setColor(Geom::Vec4f((float)r/255.0, (float)g/255.0, (float)b/255.0, 1.0));
143
144
}

145
void Clipping::slot_pushButton_addSphere()
146
{
147
	// Create clipping and pickable objects
148
	unsigned int newSphereId = m_shader->addClipSphere();
149
	Utils::Pickable* pickable = new Utils::Pickable(m_sphereDrawable, newSphereId);
Maire Nicolas's avatar
Maire Nicolas committed
150
	m_pickableSpheres.push_back(pickable);
151

152
153
	// Set params
	m_shader->setClipSphereParamsCenter(newSphereId, m_bb.center());
154
	m_shader->setClipSphereParamsRadius(newSphereId, (m_bb.maxSize())*0.5);
155
	pickable->translate(m_bb.center());
156
	pickable->scale(Geom::Vec3f(m_bb.maxSize()*0.5));
157

158
	// Update shader sources edits
159
160
161
162
163
164
	dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc()));
	dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc()));

	updateGLMatrices();
}

165
void Clipping::slot_spinBox_SphereResolution(int i)
166
{
167
	m_sphereDrawable->updatePrecisionDrawing(i);
168
169
170
	updateGL();
}

171
void Clipping::slot_pushButton_changeSpheresColor()
172
{
173
174
	QColor NewColor;
	QColorDialog ColDialog;
175

176
177
178
179
180
181
	NewColor = ColDialog.getColor();

	int r, g, b;
	NewColor.getRgb(&r, &g, &b);

	m_sphereDrawable->setColor(Geom::Vec4f((float)r/255.0, (float)g/255.0, (float)b/255.0, 1.0));
182
183
}

184
void Clipping::slot_doubleSpinBox_ColorAttenuationFactor(double c)
185
{
186
	m_shader->setClipColorAttenuationFactorAbsolute((float)c);
187
188
189
	updateGL();
}

190
void Clipping::slot_radioButton_ClippingMode(bool b)
191
{
192
	if (b)
193
		m_shader->setClipMode(Utils::ClippingShader::CLIPPING_MODE_AND);
194
	else
195
196
197
198
199
200
201
202
		m_shader->setClipMode(Utils::ClippingShader::CLIPPING_MODE_OR);

	dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc()));
	dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc()));

	updateGLMatrices();
}

203
204
205
206
207
208
209
210
211
212
213
214
215
void Clipping::slot_radioButton_ColorAttenuationMode(bool b)
{
	if (b)
		m_shader->setClipColorAttenuationMode(Utils::ClippingShader::COLOR_ATTENUATION_MODE_LINEAR);
	else
		m_shader->setClipColorAttenuationMode(Utils::ClippingShader::COLOR_ATTENUATION_MODE_QUADRATIC);

	dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc()));
	dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc()));

	updateGLMatrices();
}

216
void Clipping::slot_pushButton_deleteSelectedObject()
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
{
	if (m_lastPickedObject)
	{
		// Delete clipping object
		if (m_lastPickedObject->checkType<Utils::Grid>())
			m_shader->deleteClipPlane(m_lastPickedObject->id());
		else if (m_lastPickedObject->checkType<Utils::Sphere>())
			m_shader->deleteClipSphere(m_lastPickedObject->id());

		// Delete pickable
		if (m_lastPickedObject->checkType<Utils::Grid>())
		{
			for (size_t i = 0; i < m_pickablePlanes.size(); i++)
			{
				if (m_pickablePlanes[i] == m_lastPickedObject)
				{
					delete m_pickablePlanes[i];
					m_pickablePlanes.erase(m_pickablePlanes.begin() + i);
				}
			}
		}
		else if (m_lastPickedObject->checkType<Utils::Sphere>())
		{
			for (size_t i = 0; i < m_pickableSpheres.size(); i++)
			{
				if (m_pickableSpheres[i] == m_lastPickedObject)
				{
					delete m_pickableSpheres[i];
					m_pickableSpheres.erase(m_pickableSpheres.begin() + i);
				}
			}
		}
		m_lastPickedObject = NULL;

		// Update shader sources edits
		dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc()));
		dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc()));

		updateGLMatrices();

	}
}

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
void Clipping::slot_pushButton_applyClippingPreset()
{
	// Create and apply preset
	Utils::ClippingPreset *preset = NULL;
	switch (dock.comboBox_ClippingPresets->currentIndex())
	{
		case 0 : // Dual planes
		 	{
		 	using namespace CGoGN::Utils::QT;

		 	double centerX = (double)m_bb.center()[0];
		 	double centerY = (double)m_bb.center()[1];
		 	double centerZ = (double)m_bb.center()[2];
		 	double size = (double)m_bb.maxSize()*0.75;
		 	int axis = 0;
		 	bool facing = false;
		 	if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X",
		 					VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y",
		 					VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z",
		 					VarDbl(size - 100.0, size + 100.0, size, "Size",
		 					VarSlider(0, 2, axis, "Axis",
		 					VarBool(facing, "Facing"
		 				)))))), "Preset Setup"))
		 		preset = Utils::ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, axis, facing);
		 	}
			break;

		case 1 : // Cube
		 	{
		 	using namespace CGoGN::Utils::QT;

		 	double centerX = (double)m_bb.center()[0];
		 	double centerY = (double)m_bb.center()[1];
		 	double centerZ = (double)m_bb.center()[2];
		 	double size = (double)m_bb.maxSize()*0.75;
		 	bool facing = false;
		 	if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X",
		 					VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y",
		 					VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z",
		 					VarDbl(size - 100.0, size + 100.0, size, "Size",
		 					VarBool(facing, "Facing"
		 				))))), "Preset Setup"))
		 		preset = Utils::ClippingPreset::CreateCubePreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, facing);
		 	}
			break;
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

		case 2 : // Tube
		 	{
		 	using namespace CGoGN::Utils::QT;

		 	double centerX = (double)m_bb.center()[0];
		 	double centerY = (double)m_bb.center()[1];
		 	double centerZ = (double)m_bb.center()[2];
		 	double size = (double)m_bb.maxSize()*0.75;
		 	int axis = 0;
		 	int precision = 1;
		 	bool facing = false;
		 	if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X",
		 					VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y",
		 					VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z",
		 					VarDbl(size - 100.0, size + 100.0, size, "Size",
		 					VarSlider(0, 2, axis, "Axis",
		 					VarSlider(1, 30, precision, "Precision",
		 					VarBool(facing, "Facing"
		 				))))))), "Preset Setup"))
		 		preset = Utils::ClippingPreset::CreateTubePreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, axis, precision, facing);
		 	}
			break;

		case 3 : // Molecule
		 	{
		 	using namespace CGoGN::Utils::QT;

		 	double centerX = (double)m_bb.center()[0];
		 	double centerY = (double)m_bb.center()[1];
		 	double centerZ = (double)m_bb.center()[2];
		 	double size = (double)m_bb.maxSize()*0.75;
		 	double atomsRadiuses = (double)m_bb.maxSize()*0.25;
		 	bool orClipping = true;
		 	if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X",
		 					VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y",
		 					VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z",
		 					VarDbl(size - 100.0, size + 100.0, size, "Size",
		 					VarDbl(atomsRadiuses - 100.0, atomsRadiuses + 100.0, atomsRadiuses, "Atoms radiuses",
		 					VarBool(orClipping, "OR clipping mode"
		 				)))))), "Preset Setup"))
		 		preset = Utils::ClippingPreset::CreateMoleculePreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, atomsRadiuses, orClipping);
		 	}
			break;
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
386
	}
	std::vector<unsigned int> planesIds;
	std::vector<unsigned int> spheresIds;
	preset->apply(m_shader, &planesIds, &spheresIds);
	delete preset;

	// Cleanup of pickables before adding new ones
	m_lastPickedObject = NULL;
	for (size_t i = 0; i < m_pickablePlanes.size(); i++)
		delete m_pickablePlanes[i];
	m_pickablePlanes.resize(0);
	for (size_t i = 0; i < m_pickableSpheres.size(); i++)
		delete m_pickableSpheres[i];
	m_pickableSpheres.resize(0);

	// Add new pickable objects
	for (size_t i = 0; i < planesIds.size(); i++)
	{
		Utils::Pickable* pickable = new Utils::Pickable(m_planeDrawable, planesIds[i]);
		pickable->translate(m_shader->getClipPlaneParamsOrigin(planesIds[i]));
		Geom::Vec3f vec1, vec2, vec3;
		vec1 = m_shader->getClipPlaneParamsNormal(planesIds[i]);
		computeBasisFromVector(vec1, vec2, vec3);
		glm::mat4& transfoMat = pickable->transfo();
		for (int i = 0; i < 3; i++)
		{
			transfoMat[0][i] = vec2[i];
			transfoMat[1][i] = vec3[i];
			transfoMat[2][i] = vec1[i];
		}

		m_pickablePlanes.push_back(pickable);
	}
	for (size_t i = 0; i < spheresIds.size(); i++)
	{
		Utils::Pickable* pickable = new Utils::Pickable(m_sphereDrawable, spheresIds[i]);
		pickable->translate(m_shader->getClipSphereParamsCenter(spheresIds[i]));
		pickable->scale(m_shader->getClipSphereParamsRadius(spheresIds[i]));
387
388

		m_pickableSpheres.push_back(pickable);
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
	}

	// Update shader sources edits
	dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc()));
	dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc()));

	// Update clipping parameters in interface
	Utils::ClippingShader::clippingMode clipMode = m_shader->getClipMode();
	if (clipMode == Utils::ClippingShader::CLIPPING_MODE_AND)
		dock.radioButton_ClippingModeAnd->setChecked(true);
	else if (clipMode == Utils::ClippingShader::CLIPPING_MODE_OR)
		dock.radioButton_ClippingModeOr->setChecked(true);


	updateGLMatrices();
}

406
void Clipping::button_compile()
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
{
	QString st1 = dynamic_cast<Utils::QT::uiDockInterface*>(dockWidget())->vertexEdit->toPlainText();
	QString st2 = dynamic_cast<Utils::QT::uiDockInterface*>(dockWidget())->fragmentEdit->toPlainText();

	m_shader->reloadVertexShaderFromMemory(st1.toStdString().c_str());
	m_shader->reloadFragmentShaderFromMemory(st2.toStdString().c_str());

	m_shader->recompile();
	updateGLMatrices();
}

/*******************************************************************************
 *														Init Functions
 *******************************************************************************/

422
Clipping::Clipping():
423
424
425
426
427
428
429
430
431
432
433
434
	m_drawVertices(false),
	m_drawLines(true),
	m_drawFaces(true),
	m_drawTopo(false),
	m_render(NULL),
	m_render_topo(NULL),
	m_positionVBO(NULL),
	m_shader(NULL)
{
	m_coeffTopoExplod = Geom::Vec3f(0.9,0.9,0.9);
}

435
void Clipping::initGUI()
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
{
	CGoGNStream::allToConsole(this) ;
	CGoGNStream::allToStd(true) ;

	setDock(&dock);

	dock.setWindowFlags(Qt::WindowMinimizeButtonHint);

	setCallBack(dock.checkBox_vertices, SIGNAL(toggled(bool)), SLOT(slot_drawVertices(bool)));
	setCallBack(dock.checkBox_lines, SIGNAL(toggled(bool)), SLOT(slot_drawLines(bool)));
	setCallBack(dock.checkBox_faces, SIGNAL(toggled(bool)), SLOT(slot_drawFaces(bool)));
	setCallBack(dock.groupBox_topo, SIGNAL(toggled(bool)), SLOT(slot_drawTopo(bool)));

	setCallBack(dock.explod_phi1, SIGNAL(valueChanged(double)), SLOT(slot_explodTopoPhi1(double)));
	setCallBack(dock.explod_phi2, SIGNAL(valueChanged(double)), SLOT(slot_explodTopoPhi2(double)));
	setCallBack(dock.explod_phi3, SIGNAL(valueChanged(double)), SLOT(slot_explodTopoPhi3(double)));

	setCallBack(dock.pushButton_addPlane, SIGNAL(clicked()), SLOT(slot_pushButton_addPlane()));

455
	setCallBack(dock.spinBox_GridResolution, SIGNAL(valueChanged(int)), SLOT(slot_spinBox_GridResolution(int)));
456
	setCallBack(dock.pushButton_changePlanesColor, SIGNAL(clicked()), SLOT(slot_pushButton_changePlanesColor()));
457

458
	setCallBack(dock.compileButton, SIGNAL(clicked()), SLOT(button_compile()));
459
460
461
462

	dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc()));
	dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc()));

463
464
465
	unsigned int planesPrecision1, planesPrecision2;
	m_planeDrawable->getPrecisionDrawing(planesPrecision1, planesPrecision2);
	dock.spinBox_GridResolution->setValue(planesPrecision1);
466
467
468
469
470



	setCallBack(dock.pushButton_addSphere, SIGNAL(clicked()), SLOT(slot_pushButton_addSphere()));

471
	setCallBack(dock.spinBox_SphereResolution, SIGNAL(valueChanged(int)), SLOT(slot_spinBox_SphereResolution(int)));
472
	setCallBack(dock.pushButton_changeSpheresColor, SIGNAL(clicked()), SLOT(slot_pushButton_changeSpheresColor()));
473

474
475
	unsigned int spheresPrecision1, spheresPrecision2;
	m_sphereDrawable->getPrecisionDrawing(spheresPrecision1, spheresPrecision2);
476
	dock.spinBox_SphereResolution->setValue(spheresPrecision1);
477
478
479
480



	setCallBack(dock.doubleSpinBox_ColorAttenuationFactor, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_ColorAttenuationFactor(double)));
481
482
	setCallBack(dock.radioButton_ClippingModeAnd, SIGNAL(toggled(bool)), SLOT(slot_radioButton_ClippingMode(bool)));
	setCallBack(dock.radioButton_ColorAttenuationModeLinear, SIGNAL(toggled(bool)), SLOT(slot_radioButton_ColorAttenuationMode(bool)));
483
484
485
486

	dock.doubleSpinBox_ColorAttenuationFactor->setValue(m_shader->getClipColorAttenuationFactor());
	Utils::ClippingShader::clippingMode clipMode = m_shader->getClipMode();
	if (clipMode == Utils::ClippingShader::CLIPPING_MODE_AND)
487
		dock.radioButton_ClippingModeAnd->setChecked(true);
488
	else if (clipMode == Utils::ClippingShader::CLIPPING_MODE_OR)
489
490
491
492
493
494
		dock.radioButton_ClippingModeOr->setChecked(true);
	Utils::ClippingShader::colorAttenuationMode colorAttMode = m_shader->getClipColorAttenuationMode();
	if (colorAttMode == Utils::ClippingShader::COLOR_ATTENUATION_MODE_LINEAR)
		dock.radioButton_ColorAttenuationModeLinear->setChecked(true);
	else if (colorAttMode == Utils::ClippingShader::COLOR_ATTENUATION_MODE_QUADRATIC)
		dock.radioButton_ColorAttenuationModeQuadratic->setChecked(true);
495

496
497
498
499
500

	setCallBack(dock.PushButton_ApplyClippingPreset, SIGNAL(clicked()), SLOT(slot_pushButton_applyClippingPreset()));

	dock.comboBox_ClippingPresets->addItem("Dual Planes");
	dock.comboBox_ClippingPresets->addItem("Cube");
501
502
	dock.comboBox_ClippingPresets->addItem("Tube");
	dock.comboBox_ClippingPresets->addItem("Molecule");
503

504
505
}

506
void Clipping::cb_Open()
507
508
509
510
511
512
513
514
{
	std::string filters("all (*.*)") ;
	std::string filename = selectFile("Open Mesh", "", filters) ;

	importMesh(filename) ;
	updateGL() ;
}

515
void Clipping::importMesh(std::string& filename)
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
{
	std::vector<std::string> attrNames ;

	std::string extension;
	size_t pos;

	pos = filename.rfind(".");    // position of "." in filename
	 extension = filename.substr(pos);

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

	updateVBOprimitives(Algo::Render::GL2::TRIANGLES | Algo::Render::GL2::LINES | Algo::Render::GL2::POINTS) ;

	m_render_topo->updateData<PFP>(myMap, allDarts, position, m_coeffTopoExplod[0], m_coeffTopoExplod[1], m_coeffTopoExplod[2]);

	m_bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position) ;
	gPosObj = m_bb.center() ;
	float tailleX = m_bb.size(0) ;
	float tailleY = m_bb.size(1) ;
	float tailleZ = m_bb.size(2) ;
	gWidthObj = std::max<float>(std::max<float>(tailleX, tailleY), tailleZ) ;

	m_positionVBO->updateData(position) ;

	setParamObject(gWidthObj, gPosObj.data()) ;

561
	updateGLMatrices();
562
563

	m_shader->setClipColorAttenuationFactorRelative(m_bb.maxSize(), 5.0);
564
565
566
567
568
569
}

/*******************************************************************************
 *														GL FUNCTIONS
 *******************************************************************************/

570
void Clipping::cb_initGL()
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
{
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);

	// create the render
	m_render = new Algo::Render::GL2::MapRender();
	m_render_topo = new Algo::Render::GL2::Topo3RenderMapD();

	// create VBO for position
	m_positionVBO = new Utils::VBO();

	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
	m_shader->setAttributePosition(m_positionVBO);
	m_shader->setColor(Geom::Vec4f(0.,1.,0.,0.));

	registerShader(m_shader);

	// setup clipping
	m_shader->insertClippingCode();

	// setup clipping shapes
	m_planeDrawable = new Utils::Grid;
	m_planeDrawable->setColor(Geom::Vec4f(1.0, 0.0, 0.0, 1.0));
595
596
	m_planeDrawable->updatePrecisionDrawing(5);
	m_sphereDrawable = new Utils::IcoSphere;
597
	m_sphereDrawable->setColor(Geom::Vec4f(0.0, 0.4, 1.0, 1.0));
598
	m_sphereDrawable->updatePrecisionDrawing(1);
599
600
601

	// setup clipping picking frame
	m_frameManipulator = new Utils::FrameManipulator();
602
	m_lastPickedObject = NULL;
603
604
}

605
void Clipping::updateVBOprimitives(int upType)
606
607
608
609
610
611
612
613
614
615
616
{
	if(upType & Algo::Render::GL2::TRIANGLES)
		m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES) ;

	if(upType & Algo::Render::GL2::LINES)
		m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES,false) ;

	if(upType & Algo::Render::GL2::POINTS)
		m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::POINTS) ;
}

617
void Clipping::cb_redraw()
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
{
	if(m_drawVertices)
	{
		m_shader->setColor(Geom::Vec4f(0.,0.,1.,0.));
		m_render->draw(m_shader, Algo::Render::GL2::POINTS);
	}

	if(m_drawLines)
	{
		glLineWidth(2.0f);
		m_shader->setColor(Geom::Vec4f(1.,1.,0.,0.));
		m_render->draw(m_shader, 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);
		m_shader->setColor(Geom::Vec4f(0.,1.,0.,0.));
		m_render->draw(m_shader, Algo::Render::GL2::TRIANGLES);
		glDisable(GL_POLYGON_OFFSET_FILL);
	}

	if (m_drawTopo)
		m_render_topo->drawTopo();

	// Display clipping shapes
Maire Nicolas's avatar
Maire Nicolas committed
647
	for (size_t i = 0; i < m_pickablePlanes.size(); i++)
648
649
650
651
652
	{
		if (m_pickablePlanes[i] == m_lastPickedObject)
			glLineWidth(5.0);
		else
			glLineWidth(2.0);
Maire Nicolas's avatar
Maire Nicolas committed
653
		m_pickablePlanes[i]->draw();
654
	}
Maire Nicolas's avatar
Maire Nicolas committed
655
	for (size_t i = 0; i < m_pickableSpheres.size(); i++)
656
657
658
659
660
	{
		if (m_pickableSpheres[i] == m_lastPickedObject)
			glLineWidth(5.0);
		else
			glLineWidth(2.0);
Maire Nicolas's avatar
Maire Nicolas committed
661
		m_pickableSpheres[i]->draw();
662
	}
663

664
665
666
667
	// Display picking frame
	if (m_lastPickedObject)
		m_frameManipulator->draw();

668
669
}

670
void Clipping::cb_keyPress(int code)
671
{
672
673
674
	switch (code)
	{
		case 16777223 : // Suppr
675
			slot_pushButton_deleteSelectedObject();
676
677
			break;
	}
678
679
}

680
void Clipping::cb_mousePress(int button, int x, int y)
681
{
Maire Nicolas's avatar
Maire Nicolas committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
	if (!Shift())
		return;

	m_lastClickedX = x;
	m_lastClickedY = y;

	// get ray of selection
	Geom::Vec3f rayA, rayB;
	float dist = getOrthoScreenRay(x, y, rayA, rayB);
	Geom::Vec3f AB = rayB - rayA;


	unsigned int newPickedFrameAxis = 0;
	unsigned int lastPickedFrameAxis = m_frameManipulatorPickedAxis;
	if (m_lastPickedObject)	// an object is already picked ? => frame is drawn
	{
		// picking the frame -> axis
		newPickedFrameAxis = m_frameManipulator->pick(rayA, AB, dist);
		m_frameManipulatorPickedAxis = newPickedFrameAxis;

702
703
704
705
		// highlight new axis
		if (lastPickedFrameAxis != newPickedFrameAxis)
			m_frameManipulator->highlight(m_frameManipulatorPickedAxis);
	}
Maire Nicolas's avatar
Maire Nicolas committed
706
707
708

	if (newPickedFrameAxis == 0)	// frame not picked -> pick the pickable objects
	{
709
710
711
712
		std::vector<Utils::Pickable*> tempo = m_pickablePlanes;
		tempo.insert(tempo.end(), m_pickableSpheres.begin(), m_pickableSpheres.end());
		m_lastPickedObject = Utils::Pickable::pick(tempo, rayA, AB);

Maire Nicolas's avatar
Maire Nicolas committed
713
714
		// set FrameManipulator on picked object
		if (m_lastPickedObject)
715
		{
Maire Nicolas's avatar
Maire Nicolas committed
716
			m_frameManipulator->setTransformation(m_lastPickedObject->transfo());
717
718
719
720
721
722

			// Lock individual scale axes for the sphere
			if (m_lastPickedObject->checkType<Utils::Grid>())
				m_frameManipulator->unlock(Utils::FrameManipulator::Scales);
			else if (m_lastPickedObject->checkType<Utils::Sphere>())
				m_frameManipulator->lock(Utils::FrameManipulator::Scales);
723
		}
Maire Nicolas's avatar
Maire Nicolas committed
724
725
726
727
728
729
730
731
	}

	// store origin & selected axis on screen projection for easy manipulation.
	m_frameManipulator->storeProjection(m_frameManipulatorPickedAxis);

	updateGL();
}

732
void Clipping::cb_mouseMove(int buttons, int x, int y)
Maire Nicolas's avatar
Maire Nicolas committed
733
734
735
736
{
	if (!Shift())
		return;

737
738
	bool clippingUpdateNeeded = false;

Maire Nicolas's avatar
Maire Nicolas committed
739
740
741
742
743
744
745
746
747
748
749
750
	// rotation selected ?
	if (Utils::FrameManipulator::rotationAxis(m_frameManipulatorPickedAxis))
	{
		if (buttons & 1)
		{
			float angle = m_frameManipulator->angleFromMouse(x, y, x - m_lastClickedX, y - m_lastClickedY);
			m_frameManipulator->rotate(m_frameManipulatorPickedAxis, angle);
		}
		else if (buttons & 2)
			m_frameManipulator->rotateInScreen(x - m_lastClickedX, y - m_lastClickedY);

		m_lastPickedObject->transfo() = m_frameManipulator->transfo();
751
752

		clippingUpdateNeeded = true;
Maire Nicolas's avatar
Maire Nicolas committed
753
754
755
756
757
758
759
760
761
762
763
764
765
	}
	// translation selected
	else if (Utils::FrameManipulator::translationAxis(m_frameManipulatorPickedAxis))
	{
		if (buttons & 1)
		{
			float dist =  m_frameManipulator->distanceFromMouse(x - m_lastClickedX, y - m_lastClickedY);
			m_frameManipulator->translate(m_frameManipulatorPickedAxis, dist);
		}
		else if (buttons & 2)
			m_frameManipulator->translateInScreen(x - m_lastClickedX, y - m_lastClickedY);

		m_lastPickedObject->transfo() = m_frameManipulator->transfo();
766
767

		clippingUpdateNeeded = true;
Maire Nicolas's avatar
Maire Nicolas committed
768
769
770
771
772
773
774
	}
	// scale selected
	else if (Utils::FrameManipulator::scaleAxis(m_frameManipulatorPickedAxis) )
	{
		float scale = m_frameManipulator->scaleFromMouse(x - m_lastClickedX, y - m_lastClickedY);
		m_frameManipulator->scale(m_frameManipulatorPickedAxis, scale );
		m_lastPickedObject->transfo() = m_frameManipulator->transfo();
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

		clippingUpdateNeeded = true;
	}

	// Update clipping shape if needed
	if (clippingUpdateNeeded && m_lastPickedObject)
	{
		if (m_lastPickedObject->checkType<Utils::Grid>())
		{
			// Get plane position and normal
			Geom::Vec3f planeNewPos = m_lastPickedObject->getPosition();
			float planeNewAxisScale; // Not used
			Geom::Vec3f planeNewNormal = m_lastPickedObject->getAxisScale(2, planeNewAxisScale); // 2 = Z axis = plane normal

			// Update clipping shape
			m_shader->setClipPlaneParamsAll(m_lastPickedObject->id(), planeNewNormal, planeNewPos);
		}
		else if (m_lastPickedObject->checkType<Utils::Sphere>())
		{
			// Get sphere position and size
			Geom::Vec3f sphereNewPos = m_lastPickedObject->getPosition();
			float sphereNewSize;
			m_lastPickedObject->getAxisScale(0, sphereNewSize); // On the sphere, every axis has the same size, so take 0 as default

			// Update clipping shape
			m_shader->setClipSphereParamsAll(m_lastPickedObject->id(), sphereNewPos, sphereNewSize);
		}
Maire Nicolas's avatar
Maire Nicolas committed
802
803
804
805
806
807
	}

	m_lastClickedX = x;
	m_lastClickedY = y;

	updateGL();
808
809
810
811
812
813
814
815
816

}

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

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

819
    Clipping sqt;
820
821
    sqt.setGeometry(0, 0, 1000, 800);
    sqt.show();
822

823
	if(argc == 2)
824
    {
825
826
            std::string filename(argv[1]);
            sqt.importMesh(filename);
827
828
829
830
831
832
833
834
835
    }
	else
	{
		sqt.position = sqt.myMap.addAttribute<PFP::VEC3>(VERTEX, "position");
		Algo::Modelisation::Primitive3D<PFP> prim(sqt.myMap, sqt.position);
		prim.hexaGrid_topo(10,10,10);
		prim.embedHexaGrid(1.0f,1.0f,1.0f);
	}

836
    sqt.initGUI();
837

838
    return app.exec();
839
}