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

32
void computeBasisFromVector(const Geom::Vec3f& inVec1, Geom::Vec3f& vec2, Geom::Vec3f& vec3)
33
{
34
	const float epsilon = 0.000001f;
35
36

	// Check if the given vector length is acceptable
37
	if (inVec1.norm() < epsilon)
38
39
40
41
42
43
44
		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
45
46
	vec2 = inVec1 ^ tempVec;
	float sinAngle = vec2.norm() / (inVec1.norm()*tempVec.norm());
47
48
49
	if (sinAngle < epsilon) // f:x->sin(x) ~ f:x->x when x ~ 0
	{
		tempVec = Geom::Vec3f (1.0, 0.0, 0.0);
50
		vec2 = inVec1 ^ tempVec;
51
52
	}

53
54
55
	// Normalize second vector
	vec2.normalize();

56
	// Get third vector
57
58
59
60
61
62
	vec3 = inVec1 ^ vec2;

	// Normalize third vector
	vec3.normalize();

}
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
void Clipping::updatePickables()
{
	for (size_t i = 0; i < m_pickablePlanes.size(); i++)
	{
		unsigned int id = m_pickablePlanes[i]->id();
		Geom::Vec3f pos = m_shader->getClipPlaneParamsOrigin(id);
		Geom::Vec3f vec1, vec2, vec3;
		vec1 = m_shader->getClipPlaneParamsNormal(id);
		computeBasisFromVector(vec1, vec2, vec3);
		glm::mat4& transfoMat = m_pickablePlanes[i]->transfo();
		for (int j = 0; j < 3; j++)
		{
			transfoMat[0][j] = vec2[j];
			transfoMat[1][j] = vec3[j];
			transfoMat[2][j] = vec1[j];
			transfoMat[3][j] = pos[j];
		}
81
82
83
84
		transfoMat[0][3] = 0.0;
		transfoMat[1][3] = 0.0;
		transfoMat[2][3] = 0.0;
		transfoMat[3][3] = 1.0;
85
86
87
88
	}
	for (size_t i = 0; i < m_pickableSpheres.size(); i++)
	{
		unsigned int id = m_pickableSpheres[i]->id();
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
		Geom::Vec3f pos = m_shader->getClipSphereParamsCenter(id);
		float radius = m_shader->getClipSphereParamsRadius(id);
		Geom::Vec3f vec1 (radius, 0.0, 0.0);
		Geom::Vec3f vec2 (0.0, radius, 0.0);
		Geom::Vec3f vec3 (0.0, 0.0, radius);
		glm::mat4& transfoMat = m_pickableSpheres[i]->transfo();
		for (int j = 0; j < 3; j++)
		{
			transfoMat[0][j] = vec2[j];
			transfoMat[1][j] = vec3[j];
			transfoMat[2][j] = vec1[j];
			transfoMat[3][j] = pos[j];
		}
		transfoMat[0][3] = 0.0;
		transfoMat[1][3] = 0.0;
		transfoMat[2][3] = 0.0;
		transfoMat[3][3] = 1.0;
106
	}
107
108
}

109
110
111
112
/*******************************************************************************
 *														SLOTS
 *******************************************************************************/

113
void Clipping::slot_drawTopo(bool b)
114
115
116
117
118
{
	m_drawTopo = b;
	updateGL();
}

119
void Clipping::slot_drawVertices(bool b)
120
121
122
123
124
{
	m_drawVertices = b;
	updateGL();
}

125
void Clipping::slot_drawLines(bool b)
126
127
128
129
130
{
	m_drawLines = b;
	updateGL();
}

131
void Clipping::slot_drawFaces(bool b)
132
133
134
135
136
{
	m_drawFaces = b;
	updateGL();
}

137
void Clipping::slot_explodTopoPhi1(double c)
138
139
140
141
142
143
{
	m_coeffTopoExplod[0] = (float)c;
	m_render_topo->updateData<PFP>(myMap, allDarts, position, m_coeffTopoExplod[0], m_coeffTopoExplod[1], m_coeffTopoExplod[2]);
	updateGL();
}

144
void Clipping::slot_explodTopoPhi2(double c)
145
146
147
148
149
150
{
	m_coeffTopoExplod[1] = (float)c;
	m_render_topo->updateData<PFP>(myMap, allDarts, position, m_coeffTopoExplod[0], m_coeffTopoExplod[1], m_coeffTopoExplod[2]);
	updateGL();
}

151
void Clipping::slot_explodTopoPhi3(double c)
152
153
154
155
156
157
{
	m_coeffTopoExplod[2] = (float)c;
	m_render_topo->updateData<PFP>(myMap, allDarts, position, m_coeffTopoExplod[0], m_coeffTopoExplod[1], m_coeffTopoExplod[2]);
	updateGL();
}

158
void Clipping::slot_pushButton_addPlane()
159
{
Maire Nicolas's avatar
Maire Nicolas committed
160
161
162
163
	// Check if no animation is active
	if (m_lastAnimatedClippingPreset != NULL)
		return;

164
	// Create clipping and pickable objects
165
	unsigned int newPlaneId = m_shader->addClipPlane();
166
	Utils::Pickable* pickable = new Utils::Pickable(m_planeDrawable, newPlaneId);
Maire Nicolas's avatar
Maire Nicolas committed
167
	m_pickablePlanes.push_back(pickable);
168

169
170
	// Set params
	m_shader->setClipPlaneParamsOrigin(newPlaneId, m_bb.center());
171
	pickable->translate(m_bb.center());
172
	pickable->scale(Geom::Vec3f(m_bb.maxSize()*0.5));
173

174
	// Update shader sources edits
175
176
177
178
179
180
	dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc()));
	dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc()));

	updateGLMatrices();
}

181
void Clipping::slot_spinBox_GridResolution(int i)
182
{
183
	m_planeDrawable->updatePrecisionDrawing(i);
184
185
186
	updateGL();
}

187
void Clipping::slot_pushButton_changePlanesColor()
188
{
Sylvain Thery's avatar
Sylvain Thery committed
189
	QColor NewColor = QColorDialog::getColor();
190
191
192
193
194

	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));
195
196
}

197
void Clipping::slot_pushButton_addSphere()
198
{
Maire Nicolas's avatar
Maire Nicolas committed
199
200
201
202
	// Check if no animation is active
	if (m_lastAnimatedClippingPreset != NULL)
		return;

203
	// Create clipping and pickable objects
204
	unsigned int newSphereId = m_shader->addClipSphere();
205
	Utils::Pickable* pickable = new Utils::Pickable(m_sphereDrawable, newSphereId);
Maire Nicolas's avatar
Maire Nicolas committed
206
	m_pickableSpheres.push_back(pickable);
207

208
209
	// Set params
	m_shader->setClipSphereParamsCenter(newSphereId, m_bb.center());
210
	m_shader->setClipSphereParamsRadius(newSphereId, (m_bb.maxSize())*0.5);
211
	pickable->translate(m_bb.center());
212
	pickable->scale(Geom::Vec3f(m_bb.maxSize()*0.5));
213

214
	// Update shader sources edits
215
216
217
218
219
220
	dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc()));
	dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc()));

	updateGLMatrices();
}

221
void Clipping::slot_spinBox_SphereResolution(int i)
222
{
223
	m_sphereDrawable->updatePrecisionDrawing(i);
224
225
226
	updateGL();
}

227
void Clipping::slot_pushButton_changeSpheresColor()
228
{
229
	QColor NewColor = QColorDialog::getColor();
230

231
232
233
234
235

	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));
236
237
}

238
void Clipping::slot_doubleSpinBox_ColorAttenuationFactor(double c)
239
{
240
	m_shader->setClipColorAttenuationFactorAbsolute((float)c);
241
242
243
	updateGL();
}

244
void Clipping::slot_radioButton_ClippingMode(bool b)
245
{
246
	if (b)
247
		m_shader->setClipMode(Utils::ClippingShader::CLIPPING_MODE_AND);
248
	else
249
250
251
252
253
254
255
256
		m_shader->setClipMode(Utils::ClippingShader::CLIPPING_MODE_OR);

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

	updateGLMatrices();
}

257
258
259
260
261
262
263
264
265
266
267
268
269
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();
}

270
void Clipping::slot_pushButton_deleteSelectedObject()
271
{
Maire Nicolas's avatar
Maire Nicolas committed
272
273
274
275
	// Check if no animation is active
	if (m_lastAnimatedClippingPreset != NULL)
		return;

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
	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;
308
309
		m_frameManipulator->highlight(m_frameManipulatorPickedAxis);
		m_frameManipulatorPickedAxis = 0;
310
311
312
313
314
315
316
317
318
319

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

		updateGLMatrices();

	}
}

320
void Clipping::slot_pushButton_applyStaticClippingPreset()
321
{
Maire Nicolas's avatar
Maire Nicolas committed
322
323
324
325
	// Check if no animation is active
	if (m_lastAnimatedClippingPreset != NULL)
		return;

326
327
	// Create and apply preset
	Utils::ClippingPreset *preset = NULL;
328
	switch (dock.comboBox_StaticClippingPresets->currentIndex())
329
330
	{
		case 0 : // Dual planes
Maire Nicolas's avatar
Maire Nicolas committed
331
	 	{
332
333
		 	using namespace CGoGN::Utils::QT;

334
335
336
337
		 	float centerX = m_bb.center()[0];
		 	float centerY = m_bb.center()[1];
		 	float centerZ = m_bb.center()[2];
		 	float size = m_bb.maxSize()*0.75f;
338
339
		 	int axis = 0;
		 	bool facing = false;
340
341
342
343
		 	if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X",
		 					VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y",
		 					VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z",
		 					VarFloat(size - 100.0f, size + 100.0f, size, "Size",
344
345
346
		 					VarSlider(0, 2, axis, "Axis",
		 					VarBool(facing, "Facing"
		 				)))))), "Preset Setup"))
347
		 		preset = Utils::ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f(centerX, centerY, centerZ), size, axis, facing);
Maire Nicolas's avatar
Maire Nicolas committed
348
349
	 	}
		break;
350
351

		case 1 : // Cube
Maire Nicolas's avatar
Maire Nicolas committed
352
	 	{
353
354
		 	using namespace CGoGN::Utils::QT;

355
356
357
358
		 	float centerX = m_bb.center()[0];
		 	float centerY = m_bb.center()[1];
		 	float centerZ = m_bb.center()[2];
		 	float size = m_bb.maxSize()*0.75f;
359
		 	bool facing = false;
360
361
362
363
		 	if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X",
		 					VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y",
		 					VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z",
		 					VarFloat(size - 100.0f, size + 100.0f, size, "Size",
364
365
		 					VarBool(facing, "Facing"
		 				))))), "Preset Setup"))
366
		 		preset = Utils::ClippingPreset::CreateCubePreset(Geom::Vec3f(centerX, centerY, centerZ), size, facing);
Maire Nicolas's avatar
Maire Nicolas committed
367
368
	 	}
		break;
369
370

		case 2 : // Tube
Maire Nicolas's avatar
Maire Nicolas committed
371
	 	{
372
373
		 	using namespace CGoGN::Utils::QT;

374
375
376
377
		 	float centerX = m_bb.center()[0];
		 	float centerY = m_bb.center()[1];
		 	float centerZ = m_bb.center()[2];
		 	float size = m_bb.maxSize()*0.75f;
378
379
380
		 	int axis = 0;
		 	int precision = 1;
		 	bool facing = false;
381
382
383
384
		 	if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X",
		 					VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y",
		 					VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z",
		 					VarFloat(size - 100.0f, size + 100.0f, size, "Size",
385
386
387
388
		 					VarSlider(0, 2, axis, "Axis",
		 					VarSlider(1, 30, precision, "Precision",
		 					VarBool(facing, "Facing"
		 				))))))), "Preset Setup"))
389
		 		preset = Utils::ClippingPreset::CreateTubePreset(Geom::Vec3f(centerX, centerY, centerZ), size, axis, precision, facing);
Maire Nicolas's avatar
Maire Nicolas committed
390
391
	 	}
		break;
392
393

		case 3 : // Molecule
Maire Nicolas's avatar
Maire Nicolas committed
394
	 	{
395
396
		 	using namespace CGoGN::Utils::QT;

397
398
399
400
401
		 	float centerX = m_bb.center()[0];
		 	float centerY = m_bb.center()[1];
		 	float centerZ = m_bb.center()[2];
		 	float size = m_bb.maxSize()*0.75f;
		 	float atomsRadiuses = m_bb.maxSize()*0.25f;
402
		 	bool orClipping = true;
403
404
405
406
407
		 	if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X",
		 					VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y",
		 					VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z",
		 					VarFloat(size - 100.0f, size + 100.0f, size, "Size",
		 					VarFloat(atomsRadiuses - 100.0f, atomsRadiuses + 100.0f, atomsRadiuses, "Atoms radiuses",
408
409
		 					VarBool(orClipping, "OR clipping mode"
		 				)))))), "Preset Setup"))
410
		 		preset = Utils::ClippingPreset::CreateMoleculePreset(Geom::Vec3f(centerX, centerY, centerZ), size, atomsRadiuses, orClipping);
Maire Nicolas's avatar
Maire Nicolas committed
411
412
	 	}
		break;
413
	}
414
415
416
417

	if (preset == NULL)
		return;

418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
	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]);
		m_pickablePlanes.push_back(pickable);
	}
	for (size_t i = 0; i < spheresIds.size(); i++)
	{
		Utils::Pickable* pickable = new Utils::Pickable(m_sphereDrawable, spheresIds[i]);
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
		m_pickableSpheres.push_back(pickable);
	}
	updatePickables();

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

void Clipping::slot_pushButton_applyAnimatedClippingPreset()
{
Maire Nicolas's avatar
Maire Nicolas committed
462
463
464
465
	// Check if no animation is active
	if (m_lastAnimatedClippingPreset != NULL)
		return;

466
467
468
469
470
	// Create and apply preset
	Utils::ClippingPresetAnimated *animatedPreset = NULL;
	switch (dock.comboBox_AnimatedClippingPresets->currentIndex())
	{
		case 0 : // Moving Dual planes
Maire Nicolas's avatar
Maire Nicolas committed
471
	 	{
472
473
474
		 	Utils::ClippingPresetAnimatedDualPlanes *preset = NULL;
		 	using namespace CGoGN::Utils::QT;

Maire Nicolas's avatar
Maire Nicolas committed
475
		 	float centerStartX = m_bb.center()[0] - m_bb.size(0)*0.75f;
476
477
		 	float centerStartY = m_bb.center()[1];
		 	float centerStartZ = m_bb.center()[2];
Maire Nicolas's avatar
Maire Nicolas committed
478
		 	float centerEndX = m_bb.center()[0] + m_bb.size(0)*0.75f;
479
480
481
		 	float centerEndY = m_bb.center()[1];
		 	float centerEndZ = m_bb.center()[2];
		 	float size = m_bb.maxSize()*0.1f;
482
483
		 	int axis = 0;
		 	bool facing = false;
484
		 	bool zigzag = false;
485
486
487
488
489
490
491
		 	if (inputValues(VarFloat(centerStartX - 100.0f, centerStartX + 100.0f, centerStartX, "Center Start X",
		 					VarFloat(centerStartY - 100.0f, centerStartY + 100.0f, centerStartY, "Center Start Y",
		 					VarFloat(centerStartZ - 100.0f, centerStartZ + 100.0f, centerStartZ, "Center Start Z",
		 					VarFloat(centerEndX - 100.0f, centerEndX + 100.0f, centerEndX, "Center End X",
		 					VarFloat(centerEndY - 100.0f, centerEndY + 100.0f, centerEndY, "Center End Y",
		 					VarFloat(centerEndZ - 100.0f, centerEndZ + 100.0f, centerEndZ, "Center End Z",
		 					VarFloat(size - 100.0f, size + 100.0f, size, "Size",
492
		 					VarSlider(0, 2, axis, "Axis",
493
494
495
		 					VarBool(facing, "Facing",
		 					VarBool(zigzag, "Zigzag"
		 				)))))))))), "Preset Setup"))
496
		 		preset = new Utils::ClippingPresetAnimatedDualPlanes(
497
498
		 				Geom::Vec3f(centerStartX, centerStartY, centerStartZ), Geom::Vec3f(centerEndX, centerEndY, centerEndZ),
		 				size, axis, facing, zigzag);
499

500
		 	animatedPreset = preset;
Maire Nicolas's avatar
Maire Nicolas committed
501
502
	 	}
		break;
503

Maire Nicolas's avatar
Maire Nicolas committed
504
505
506
507
508
		case 1 : // Rotating Plane
	 	{
		 	Utils::ClippingPresetAnimatedRotatingPlane *preset = NULL;
		 	using namespace CGoGN::Utils::QT;

509
510
511
		 	float centerX = m_bb.center()[0];
		 	float centerY = m_bb.center()[1];
		 	float centerZ = m_bb.center()[2];
Maire Nicolas's avatar
Maire Nicolas committed
512
		 	int axis = 0;
513
514
515
		 	if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X",
		 					VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y",
		 					VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z",
Maire Nicolas's avatar
Maire Nicolas committed
516
517
518
		 					VarSlider(0, 2, axis, "Axis"
		 				)))), "Preset Setup"))
		 		preset = new Utils::ClippingPresetAnimatedRotatingPlane(
519
		 				Geom::Vec3f(centerX, centerY, centerZ), axis);
Maire Nicolas's avatar
Maire Nicolas committed
520
521
522
523

		 	animatedPreset = preset;
	 	}
		break;
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
		case 2 : // Scaled Sphere
	 	{
		 	Utils::ClippingPresetAnimatedScaledSphere *preset = NULL;
		 	using namespace CGoGN::Utils::QT;

		 	float centerX = m_bb.center()[0];
		 	float centerY = m_bb.center()[1];
		 	float centerZ = m_bb.center()[2];
		 	float radiusStart = m_bb.maxSize()*0.1f;
		 	float radiusEnd = m_bb.maxSize()*1.0f;
		 	bool zigzag = false;
		 	if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X",
		 					VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y",
		 					VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z",
		 					VarFloat(-radiusEnd*4.0f, radiusEnd*4.0f, radiusStart, "Radius Start",
		 					VarFloat(-radiusEnd*4.0f, radiusEnd*4.0f, radiusEnd, "Radius End",
		 					VarBool(zigzag, "Zigzag"
		 				)))))), "Preset Setup"))
		 		preset = new Utils::ClippingPresetAnimatedScaledSphere(
		 				Geom::Vec3f(centerX, centerY, centerZ), radiusStart, radiusEnd, zigzag);

		 	animatedPreset = preset;
	 	}
		break;

		case 3 : // Spheres Cube Collision
	 	{
		 	Utils::ClippingPresetAnimatedSpheresCubeCollision *preset = NULL;
		 	using namespace CGoGN::Utils::QT;

		 	float centerX = m_bb.center()[0];
		 	float centerY = m_bb.center()[1];
		 	float centerZ = m_bb.center()[2];
		 	float size = m_bb.maxSize()*1.0f;
		 	int spheresCount = 5;
		 	float radius = m_bb.maxSize()*0.2f;
		 	if (inputValues(VarFloat(centerX - 100.0f, centerX + 100.0f, centerX, "Center X",
		 					VarFloat(centerY - 100.0f, centerY + 100.0f, centerY, "Center Y",
		 					VarFloat(centerZ - 100.0f, centerZ + 100.0f, centerZ, "Center Z",
		 					VarFloat(-size*4.0f, size*4.0f, size, "Cube Size",
		 					VarSlider(1, 20, spheresCount, "Spheres Count",
		 					VarFloat(-radius*4.0f, radius*4.0f, radius, "Spheres Radius"
		 				)))))), "Preset Setup"))
		 	{
		 		preset = new Utils::ClippingPresetAnimatedSpheresCubeCollision(
		 				Geom::Vec3f(centerX, centerY, centerZ), size, spheresCount, radius);
		 	}

		 	animatedPreset = preset;
	 	}
		break;
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
	}

	if (animatedPreset == NULL)
		return;

	std::vector<unsigned int> planesIds;
	std::vector<unsigned int> spheresIds;
	animatedPreset->apply(m_shader, &planesIds, &spheresIds);

	// 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]);
		m_pickablePlanes.push_back(pickable);
	}
	for (size_t i = 0; i < spheresIds.size(); i++)
	{
		Utils::Pickable* pickable = new Utils::Pickable(m_sphereDrawable, spheresIds[i]);
603
		m_pickableSpheres.push_back(pickable);
604
	}
605
	updatePickables();
606
607
608
609
610
611
612
613
614
615
616
617

	// 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);

618
619
620
	// Set on animated mode
	m_lastAnimatedClippingPreset = animatedPreset;
	m_timer->start(1000.0f/60.0f);
621
622
	m_lastAnimatedClippingPreset->setAnimationSpeedFactor((float)dock.doubleSpinBox_AnimatedClippingPresetSpeed->value());
	slot_setAnimationState(true);
623

624
625
626
627

	updateGLMatrices();
}

628
629
630
631
632
633
634
void Clipping::slot_pushButton_StopAnimation()
{
	if (m_lastAnimatedClippingPreset != NULL)
	{
		delete m_lastAnimatedClippingPreset;
		m_lastAnimatedClippingPreset = NULL;
	}
635
	slot_setAnimationState(false);
636
637
638
639
640
641
642
643
}

void Clipping::slot_doubleSpinBox_AnimatedClippingPresetSpeed(double c)
{
	if (m_lastAnimatedClippingPreset != NULL)
		m_lastAnimatedClippingPreset->setAnimationSpeedFactor((float)c);
}

644
645
646
647
648
649
650
651
void Clipping::slot_setAnimationState(bool state)
{
	dock.pushButton_addPlane->setEnabled(!state);
	dock.pushButton_addSphere->setEnabled(!state);
	dock.PushButton_ApplyStaticClippingPreset->setEnabled(!state);
	dock.PushButton_ApplyAnimatedClippingPreset->setEnabled(!state);
}

652
653
654
655
656
657
658
659
660
661
662
663
664
665
void Clipping::slot_animationTimer()
{
	if (m_lastAnimatedClippingPreset == NULL)
	{
		m_timer->stop();
		return;
	}

	m_lastAnimatedClippingPreset->step(1);
	updatePickables();

	updateGL();
}

666
void Clipping::button_compile()
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
{
	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
 *******************************************************************************/

682
Clipping::Clipping():
683
684
685
686
687
688
689
	m_drawVertices(false),
	m_drawLines(true),
	m_drawFaces(true),
	m_drawTopo(false),
	m_render(NULL),
	m_render_topo(NULL),
	m_positionVBO(NULL),
690
691
692
	m_shader(NULL),
	m_timer(NULL),
	m_lastAnimatedClippingPreset(NULL)
693
694
695
696
{
	m_coeffTopoExplod = Geom::Vec3f(0.9,0.9,0.9);
}

697
void Clipping::initGUI()
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
{
	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()));

717
	setCallBack(dock.spinBox_GridResolution, SIGNAL(valueChanged(int)), SLOT(slot_spinBox_GridResolution(int)));
718
	setCallBack(dock.pushButton_changePlanesColor, SIGNAL(clicked()), SLOT(slot_pushButton_changePlanesColor()));
719

720
	setCallBack(dock.compileButton, SIGNAL(clicked()), SLOT(button_compile()));
721
722
723
724

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

725
726
727
	unsigned int planesPrecision1, planesPrecision2;
	m_planeDrawable->getPrecisionDrawing(planesPrecision1, planesPrecision2);
	dock.spinBox_GridResolution->setValue(planesPrecision1);
728
729
730
731
732



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

733
	setCallBack(dock.spinBox_SphereResolution, SIGNAL(valueChanged(int)), SLOT(slot_spinBox_SphereResolution(int)));
734
	setCallBack(dock.pushButton_changeSpheresColor, SIGNAL(clicked()), SLOT(slot_pushButton_changeSpheresColor()));
735

736
737
	unsigned int spheresPrecision1, spheresPrecision2;
	m_sphereDrawable->getPrecisionDrawing(spheresPrecision1, spheresPrecision2);
738
	dock.spinBox_SphereResolution->setValue(spheresPrecision1);
739
740
741
742



	setCallBack(dock.doubleSpinBox_ColorAttenuationFactor, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_ColorAttenuationFactor(double)));
743
744
	setCallBack(dock.radioButton_ClippingModeAnd, SIGNAL(toggled(bool)), SLOT(slot_radioButton_ClippingMode(bool)));
	setCallBack(dock.radioButton_ColorAttenuationModeLinear, SIGNAL(toggled(bool)), SLOT(slot_radioButton_ColorAttenuationMode(bool)));
745
746
747
748

	dock.doubleSpinBox_ColorAttenuationFactor->setValue(m_shader->getClipColorAttenuationFactor());
	Utils::ClippingShader::clippingMode clipMode = m_shader->getClipMode();
	if (clipMode == Utils::ClippingShader::CLIPPING_MODE_AND)
749
		dock.radioButton_ClippingModeAnd->setChecked(true);
750
	else if (clipMode == Utils::ClippingShader::CLIPPING_MODE_OR)
751
752
753
754
755
756
		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);
757

758

759
760
761
762
763
764
765
766
767
768
769
	setCallBack(dock.PushButton_ApplyStaticClippingPreset, SIGNAL(clicked()), SLOT(slot_pushButton_applyStaticClippingPreset()));

	dock.comboBox_StaticClippingPresets->addItem("Dual Planes");
	dock.comboBox_StaticClippingPresets->addItem("Cube");
	dock.comboBox_StaticClippingPresets->addItem("Tube");
	dock.comboBox_StaticClippingPresets->addItem("Molecule");


	setCallBack(dock.PushButton_ApplyAnimatedClippingPreset, SIGNAL(clicked()), SLOT(slot_pushButton_applyAnimatedClippingPreset()));

	dock.comboBox_AnimatedClippingPresets->addItem("Moving Dual Planes");
Maire Nicolas's avatar
Maire Nicolas committed
770
	dock.comboBox_AnimatedClippingPresets->addItem("Rotating Plane");
771
772
	dock.comboBox_AnimatedClippingPresets->addItem("Scaled Sphere");
	dock.comboBox_AnimatedClippingPresets->addItem("Spheres Cube Collision");
773
774
775

	setCallBack(dock.pushButton_StopAnimation, SIGNAL(clicked()), SLOT(slot_pushButton_StopAnimation()));
	setCallBack(dock.doubleSpinBox_AnimatedClippingPresetSpeed, SIGNAL(valueChanged(double)), SLOT(slot_doubleSpinBox_AnimatedClippingPresetSpeed(double)));
776

777
778
779
	// timer used for animation
	m_timer = new QTimer( this );
	setCallBack( m_timer, SIGNAL(timeout()), SLOT(slot_animationTimer()) );
780

781
782
}

783
void Clipping::cb_Open()
784
785
786
787
788
789
790
791
{
	std::string filters("all (*.*)") ;
	std::string filename = selectFile("Open Mesh", "", filters) ;

	importMesh(filename) ;
	updateGL() ;
}

792
void Clipping::importMesh(std::string& filename)
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
{
	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]) ;
	}
Sylvain Thery's avatar
Sylvain Thery committed
822
823
824
825
826
827
828
829
830
831
832
	if(extension == std::string(".map"))
	{
		if(!myMap.loadMapBin(filename))
		{
			CGoGNerr << "could not import " << filename << CGoGNendl ;
			return;
		}
		else
			position = myMap.getAttribute<PFP::VEC3>(VERTEX , "position") ;
	}

833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848

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

849
	updateGLMatrices();
850
851

	m_shader->setClipColorAttenuationFactorRelative(m_bb.maxSize(), 5.0);
852
853
854
855
856
857
}

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

858
void Clipping::cb_initGL()
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
{
	// 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));
883
884
	m_planeDrawable->updatePrecisionDrawing(5);
	m_sphereDrawable = new Utils::IcoSphere;
885
	m_sphereDrawable->setColor(Geom::Vec4f(0.0, 0.4, 1.0, 1.0));
886
	m_sphereDrawable->updatePrecisionDrawing(1);
887
888
889

	// setup clipping picking frame
	m_frameManipulator = new Utils::FrameManipulator();
890
	m_lastPickedObject = NULL;
891
892
}

893
void Clipping::updateVBOprimitives(int upType)
894
895
896
897
898
899
900
901
902
903
904
{
	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) ;
}

905
void Clipping::cb_redraw()
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
{
	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
935
	for (size_t i = 0; i < m_pickablePlanes.size(); i++)
936
937
938
939
940
	{
		if (m_pickablePlanes[i] == m_lastPickedObject)
			glLineWidth(5.0);
		else
			glLineWidth(2.0);
Maire Nicolas's avatar
Maire Nicolas committed
941
		m_pickablePlanes[i]->draw();
942
	}
Maire Nicolas's avatar
Maire Nicolas committed
943
	for (size_t i = 0; i < m_pickableSpheres.size(); i++)
944
945
946
947
948
	{
		if (m_pickableSpheres[i] == m_lastPickedObject)
			glLineWidth(5.0);
		else
			glLineWidth(2.0);
Maire Nicolas's avatar
Maire Nicolas committed
949
		m_pickableSpheres[i]->draw();
950
	}
951

952
953
954
955
	// Display picking frame
	if (m_lastPickedObject)
		m_frameManipulator->draw();

956
957
}

958
void Clipping::cb_keyPress(int code)
959
{
960
961
962
	switch (code)
	{
		case 16777223 : // Suppr
963
			slot_pushButton_deleteSelectedObject();
964
			break;
Maire Nicolas's avatar
Maire Nicolas committed
965
966
967
968
969
970
971
972
973
974

		case '+' :
			dock.doubleSpinBox_AnimatedClippingPresetSpeed->setValue(
					dock.doubleSpinBox_AnimatedClippingPresetSpeed->value() + 0.1);
			break;

		case '-' :
			dock.doubleSpinBox_AnimatedClippingPresetSpeed->setValue(
					dock.doubleSpinBox_AnimatedClippingPresetSpeed->value() - 0.1);
			break;
975
	}
976
977
}

978
void Clipping::cb_mousePress(int button, int x, int y)
979
{
Maire Nicolas's avatar
Maire Nicolas committed
980
981
982
983
	// Check if no animation is active
	if (m_lastAnimatedClippingPreset != NULL)
		return;

Maire Nicolas's avatar
Maire Nicolas committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
	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;

1004
1005
1006
1007
		// highlight new axis
		if (lastPickedFrameAxis != newPickedFrameAxis)
			m_frameManipulator->highlight(m_frameManipulatorPickedAxis);
	}
Maire Nicolas's avatar
Maire Nicolas committed
1008
1009
1010

	if (newPickedFrameAxis == 0)	// frame not picked -> pick the pickable objects
	{
1011
1012
1013
1014
		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
1015
1016
		// set FrameManipulator on picked object
		if (m_lastPickedObject)
1017
		{
Maire Nicolas's avatar
Maire Nicolas committed
1018
			m_frameManipulator->setTransformation(m_lastPickedObject->transfo());
1019
1020
1021
1022
1023
1024

			// 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);
1025
		}
Maire Nicolas's avatar
Maire Nicolas committed
1026
1027
1028
1029
1030
1031
1032
1033
	}

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

	updateGL();
}

1034
void Clipping::cb_mouseMove(int buttons, int x, int y)
Maire Nicolas's avatar
Maire Nicolas committed
1035
{
Maire Nicolas's avatar
Maire Nicolas committed
1036
1037
1038
1039
	// Check if no animation is active
	if (m_lastAnimatedClippingPreset != NULL)
		return;

Maire Nicolas's avatar
Maire Nicolas committed
1040
1041
1042
	if (!Shift())
		return;

1043
1044
	bool clippingUpdateNeeded = false;

Maire Nicolas's avatar
Maire Nicolas committed
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
	// 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();
1057
1058

		clippingUpdateNeeded = true;
Maire Nicolas's avatar
Maire Nicolas committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
	}
	// 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();
1072
1073

		clippingUpdateNeeded = true;
Maire Nicolas's avatar
Maire Nicolas committed
1074
1075
1076
1077
1078
1079
1080
	}
	// 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();
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107

		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
1108
1109
1110
1111
1112
1113
	}

	m_lastClickedX = x;
	m_lastClickedY = y;

	updateGL();
1114
1115
1116
1117
1118
1119
1120
1121
1122

}

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

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

1125
    Clipping sqt;
1126
1127
    sqt.setGeometry(0, 0, 1000, 800);
    sqt.show();
1128

1129
	if(argc == 2)
1130
    {
1131
1132
            std::string filename(argv[1]);
            sqt.importMesh(filename);
1133
1134
1135
1136
1137
1138
1139
1140
1141
    }
	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);
	}

1142
    sqt.initGUI();
1143

1144
    return app.exec();
1145
}