frameManipulator.cpp 21.3 KB
Newer Older
1 2 3
/*******************************************************************************
 * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
 * version 0.1                                                                  *
4
 * Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *                                                                              *
 * 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.           *
 *                                                                              *
20
 * Web site: http://cgogn.unistra.fr/                                           *
21 22 23 24 25 26 27 28 29 30 31
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#include "Utils/frameManipulator.h"

#include "Geometry/distances.h"
#include "Geometry/intersection.h"

#include <glm/ext.hpp>

thery's avatar
thery committed
32 33 34
#define _USE_MATH_DEFINES
#include <math.h>

35 36 37

namespace CGoGN
{
Pierre Kraemer's avatar
Pierre Kraemer committed
38

39 40 41
namespace Utils
{

thery's avatar
thery committed
42
const float FrameManipulator::ring_half_width = 0.08f;
43 44 45 46 47 48 49 50 51 52

FrameManipulator::FrameManipulator():
		m_highlighted(NONE),
		m_rotations(1.0f),
		m_scaleRendering(1.0f),
		m_trans(0.0f,0.0f,0.0f),
		m_scale(1.0f,1.0f,1.0f)
{

	for (unsigned int i=0; i<11; ++i)
53
	{
54
		m_locked_axis[i]=false;
55 56
		m_lockedPicking_axis[i]=false;
	}
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

	m_vboPos = new VBO();
	m_vboPos->setDataSize(3);

	m_vboCol = new VBO();
	m_vboCol->setDataSize(3);

	m_shader = new ShaderSimpleColor();

	m_shader->setAttributePosition(m_vboPos);

	GLSLShader::registerShader(NULL, m_shader);

	std::vector<Geom::Vec3f> points;
	points.reserve(6*nb_segments+30);
	points.resize(6*nb_segments+6);

	unsigned int second = 2*(nb_segments+1);
	unsigned int third = 4*(nb_segments+1);

	for (unsigned int i=0; i<=nb_segments; ++i)
	{
		float alpha = float(i)*M_PI/float(nb_segments/2);
		float x = (1.0f+ring_half_width) * cos(alpha);
		float y = (1.0f+ring_half_width) * sin(alpha);
		float xx = (1.0f-ring_half_width) * cos(alpha);
		float yy = (1.0f-ring_half_width) * sin(alpha);

		points[2*i] = Geom::Vec3f(0.0f,x,y);
		points[2*i+1] = Geom::Vec3f(0.0f,xx,yy);
		points[second + 2*i] = Geom::Vec3f(x,0.0f,y);
		points[second + 2*i+1] = Geom::Vec3f(xx,0.0f,yy);
		points[third + 2*i] = Geom::Vec3f(x,y,0.0f);
		points[third + 2*i+1] = Geom::Vec3f(xx,yy,0.0f);
	}

	points.push_back(Geom::Vec3f(0.0f,0.0f,0.0f));
	points.push_back(Geom::Vec3f(0.23f,0.0f,0.0f));

	points.push_back(Geom::Vec3f(0.0f,0.0f,0.0f));
	points.push_back(Geom::Vec3f(0.0f,0.23f,0.0f));

	points.push_back(Geom::Vec3f(0.0f,0.0f,0.0f));
	points.push_back(Geom::Vec3f(0.0f,0.0f,0.23f));

	points.push_back(Geom::Vec3f(0.27f,0.0f,0.0f));
	points.push_back(Geom::Vec3f(0.75f,0.0f,0.0f));
	points.push_back(Geom::Vec3f(0.9f,0.0f,0.0f));
	points.push_back(Geom::Vec3f(0.7f,-0.03f,0.0f));
	points.push_back(Geom::Vec3f(0.7f,0.0f,-0.03f));
	points.push_back(Geom::Vec3f(0.7f,0.03f,0.0f));
	points.push_back(Geom::Vec3f(0.7f,0.0f,0.03f));
	points.push_back(Geom::Vec3f(0.7f,-0.03f,0.0f));

	points.push_back(Geom::Vec3f(0.0f,  0.27f,0.0f));
	points.push_back(Geom::Vec3f(0.0f,  0.75f, 0.0f));
	points.push_back(Geom::Vec3f(0.0f,  0.9f, 0.0f));
	points.push_back(Geom::Vec3f(0.0f,  0.7f, 0.03f));
	points.push_back(Geom::Vec3f(0.03f, 0.7f, 0.0f));
	points.push_back(Geom::Vec3f(0.0f,  0.7f,-0.03f));
	points.push_back(Geom::Vec3f(-0.03f,0.7f, 0.0f));
	points.push_back(Geom::Vec3f(0.0f,  0.7f, 0.03f));

	points.push_back(Geom::Vec3f(0.0f,0.0f,  0.27f));
	points.push_back(Geom::Vec3f(0.0f,0.0f,  0.75f));
	points.push_back(Geom::Vec3f(0.0f,0.0f,  0.9f));
	points.push_back(Geom::Vec3f(0.03f,0.0f, 0.7f));
	points.push_back(Geom::Vec3f(0.0f,0.03f, 0.7f));
	points.push_back(Geom::Vec3f(-0.03f,0.0f,0.7f));
	points.push_back(Geom::Vec3f(0.0f,-0.03f,0.7f));
	points.push_back(Geom::Vec3f(0.03f,0.0f, 0.7f));

	m_vboPos->bind();
	glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(Geom::Vec3f), &(points[0]), GL_STREAM_DRAW);
Sylvain Thery's avatar
Sylvain Thery committed
131 132

	setLengthAxes();
133 134 135 136
}

void FrameManipulator::setSize(float radius)
{
137 138
	if (m_scaleRendering >0.0f)
		m_scaleRendering = radius;
139 140 141 142 143
}

void FrameManipulator::addSize(float radius)
{
	m_scaleRendering += radius;
144 145
	if (m_scaleRendering <= 0.0f)
		m_scaleRendering -= radius;
146 147 148 149 150 151 152 153 154
}

float FrameManipulator::getSize()
{
	return m_scaleRendering;
}

void FrameManipulator::draw()
{
Sylvain Thery's avatar
Sylvain Thery committed
155 156
	Utils::GLSLShader::pushTransfo();
	Utils::GLSLShader::applyTransfo(transfoRenderFrame());
157 158 159 160 161 162 163 164 165 166 167
	Utils::GLSLShader::updateCurrentMatrices();

 	glPushAttrib(GL_LINE_BIT);
	m_shader->enableVertexAttribs();

	if (!m_locked_axis[Xr])
	{
		if (m_highlighted == Xr)
			m_shader->setColor(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
		else
			m_shader->setColor(Geom::Vec4f(1.0f,0.0f,0.0f,0.0f));
Sylvain Thery's avatar
Sylvain Thery committed
168
		m_shader->bind();
169 170 171 172 173 174 175 176 177
		glDrawArrays(GL_TRIANGLE_STRIP, 0, 2*nb_segments+2);
	}

	if (!m_locked_axis[Yr])
	{
		if (m_highlighted == Yr)
			m_shader->setColor(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
		else
			m_shader->setColor(Geom::Vec4f(0.0f,1.0f,0.0f,0.0f));
Sylvain Thery's avatar
Sylvain Thery committed
178
		m_shader->bind();
179 180 181 182 183 184 185 186 187
		glDrawArrays(GL_TRIANGLE_STRIP, 2*nb_segments+2, 2*nb_segments+2);
	}

	if (!m_locked_axis[Zr])
	{
		if (m_highlighted == Zr)
			m_shader->setColor(Geom::Vec4f(1.0,1.0,0.0f,0.0f));
		else
			m_shader->setColor(Geom::Vec4f(0.0f,0.0f,1.0f,0.0f));
Sylvain Thery's avatar
Sylvain Thery committed
188
		m_shader->bind();
189 190 191
		glDrawArrays(GL_TRIANGLE_STRIP, 4*nb_segments+4, 2*nb_segments+2);
	}

192 193 194 195
	if ((!m_locked_axis[CENTER]) && (m_highlighted == CENTER))
	{
		glLineWidth(6.0f);
		m_shader->setColor(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
Sylvain Thery's avatar
Sylvain Thery committed
196
		m_shader->bind();
197 198 199 200
		glDrawArrays(GL_LINES, 6*nb_segments+6, 6);
	}
	else
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
201
		if (!m_locked_axis[Xs])
202
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
203 204 205 206 207 208 209 210 211 212 213 214
			if (m_highlighted == Xs)
			{
				glLineWidth(6.0f);
				m_shader->setColor(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
			}
			else
			{
				glLineWidth(3.0f);
				m_shader->setColor(Geom::Vec4f(1.0f,0.0f,0.0f,0.0f));
			}
			m_shader->bind();
			glDrawArrays(GL_LINES, 6*nb_segments+6, 2);
215 216
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
217
		if (!m_locked_axis[Ys])
218
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220 221 222 223 224 225 226 227 228 229 230
			if (m_highlighted == Ys)
			{
				glLineWidth(6.0f);
				m_shader->setColor(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
			}
			else
			{
				glLineWidth(3.0f);
				m_shader->setColor(Geom::Vec4f(0.0f,0.7f,0.0f,0.0f));
			}
			m_shader->bind();
			glDrawArrays(GL_LINES, 6*nb_segments+8, 2);
231 232
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
233
		if (!m_locked_axis[Zs])
234
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
235 236 237 238 239 240 241 242 243 244 245 246
			if (m_highlighted == Zs)
			{
				glLineWidth(6.0f);
				m_shader->setColor(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
			}
			else
			{
				glLineWidth(3.0f);
				m_shader->setColor(Geom::Vec4f(0.0f,0.0f,0.7f,0.0f));
			}
			m_shader->bind();
			glDrawArrays(GL_LINES, 6*nb_segments+10, 2);
247
		}
248 249
	}

250 251 252 253 254 255 256 257 258 259 260 261 262 263

	if (!m_locked_axis[Xt])
	{
		if (m_highlighted == Xt)
		{
			m_shader->setColor(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
			glLineWidth(6.0f);
		}
		else
		{
			glLineWidth(3.0f);
			m_shader->setColor(Geom::Vec4f(1.0f,0.0f,0.0f,0.0f));
		}

Sylvain Thery's avatar
Sylvain Thery committed
264
		m_shader->bind();
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		glDrawArrays(GL_LINES, 6*nb_segments+12, 2);
		glDrawArrays(GL_TRIANGLE_FAN, 6*nb_segments+14, 6);
	}

	if (!m_locked_axis[Yt])
	{
		if (m_highlighted == Yt)
		{
			glLineWidth(6.0f);
			m_shader->setColor(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
		}
		else
		{
			glLineWidth(3.0f);
			m_shader->setColor(Geom::Vec4f(0.0f,1.0f,0.0f,0.0f));
		}
Sylvain Thery's avatar
Sylvain Thery committed
281
		m_shader->bind();
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		glDrawArrays(GL_LINES, 6*nb_segments+20, 2);
		glDrawArrays(GL_TRIANGLE_FAN, 6*nb_segments+22, 6);
	}

	if (!m_locked_axis[Zt])
	{
		if (m_highlighted == Zt)
		{
			glLineWidth(6.0f);
			m_shader->setColor(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
		}
		else
		{
			glLineWidth(3.0f);
			m_shader->setColor(Geom::Vec4f(0.0f,0.0f,1.0f,0.0f));
		}
Sylvain Thery's avatar
Sylvain Thery committed
298
		m_shader->bind();
299 300 301 302 303 304 305
		glDrawArrays(GL_LINES, 6*nb_segments+28, 2);
		glDrawArrays(GL_TRIANGLE_FAN, 6*nb_segments+30, 6);
	}

 	m_shader->disableVertexAttribs();
 	glPopAttrib();

Sylvain Thery's avatar
Sylvain Thery committed
306
 	Utils::GLSLShader::popTransfo();
307
	Utils::GLSLShader::updateCurrentMatrices();
Sylvain Thery's avatar
Sylvain Thery committed
308
	m_shader->unbind();
309 310 311 312 313 314 315 316 317 318
}

void FrameManipulator::highlight(unsigned int axis)
{
	if (m_highlighted == axis)
		m_highlighted = NONE;
	else
		m_highlighted = axis;
}

319
unsigned int FrameManipulator::pick(const Geom::Vec3f& PP, const Geom::Vec3f& VV, float /*epsilon*/)
320 321 322 323 324 325 326 327 328 329
{
	Geom::Vec3f P,V;

	Pickable::invertPV(PP,VV, transfoRenderFrame(),P,V );

	// origin of frame
	Geom::Vec3f origin(0.0f,0.0f,0.0f);

	// intersection possible between line and frame (10% margin)?
	float dist2 = Geom::squaredDistanceLine2Point<Geom::Vec3f>(P,V,V*V,origin);
330 331 332 333 334 335

	float distMax= std::max(m_lengthAxes[0],std::max(m_lengthAxes[1],m_lengthAxes[2]));
	distMax *=3.6f;
	distMax= std::max(distMax,1.0f+ring_half_width);

	if (dist2 > distMax*distMax)
336 337 338 339 340
		return NONE;

	// click on center
	if (dist2 < 0.02f*0.02f)
	{
341
		if (axisPickable(CENTER))
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
			return CENTER;
		else
			return NONE;
	}

	float dist_target[9];
	float dist_cam[9];

	for (unsigned int i=0; i<9; ++i)
	{
		dist_target[i] = 2.0f;
		dist_cam[i] = std::numeric_limits<float>::max();
	}

	// Circles:
	// plane X=0
	Geom::Vec3f Qx;
	Geom::Intersection inter = Geom::intersectionLinePlane<Geom::Vec3f>(P,V,origin, Geom::Vec3f(1.0f,0.0f,0.0f), Qx);

361 362 363 364 365 366
	if (axisPickable(Xr))
	{
		if (inter == Geom::FACE_INTERSECTION)
			dist_target[3] = Qx.norm() - 1.0f;
		else if (inter == Geom::EDGE_INTERSECTION)
			dist_target[3] = sqrt(dist2) - 1.0f;
367

368 369 370
		if (fabs(dist_target[3]) < ring_half_width )
			dist_cam[3] = (P-Qx)*(P-Qx);
	}
371 372 373 374 375

	// plane Y=0
	Geom::Vec3f Qy;
	inter = Geom::intersectionLinePlane<Geom::Vec3f>(P,V,origin, Geom::Vec3f(0.0f,1.0f,0.0f), Qy);

376 377 378 379 380 381
	if (axisPickable(Yr))
	{
		if (inter == Geom::FACE_INTERSECTION)
			dist_target[4] = Qy.norm() - 1.0f;
		else if (inter == Geom::EDGE_INTERSECTION)
			dist_target[4] = sqrt(dist2) - 1.0f;
382

383 384 385
		if (fabs(dist_target[4]) < ring_half_width )
			dist_cam[4] = (P-Qy)*(P-Qy);
	}
386 387 388 389 390

	// plane Z=0
	Geom::Vec3f Qz;
	inter = Geom::intersectionLinePlane<Geom::Vec3f>(P,V,origin, Geom::Vec3f(0.0f,0.0f,1.0f), Qz);

391 392 393 394 395 396
	if (axisPickable(Zr))
	{
		if (inter == Geom::FACE_INTERSECTION)
			dist_target[5] = Qz.norm() - 1.0f;
		else if (inter == Geom::EDGE_INTERSECTION)
			dist_target[5] = sqrt(dist2) - 1.0f;
397

398 399 400
		if (fabs(dist_target[5]) <  ring_half_width )
			dist_cam[5] = (P-Qz)*(P-Qz);
	}
401 402 403

	// axes:

404
	if (axisPickable(Xt) || axisPickable(Xs))
405
	{
406
		Geom::Vec3f PX(3.6f*m_lengthAxes[0],0.0f,0.0f);
407 408 409
		dist_target[0] = sqrt(Geom::squaredDistanceLine2Seg(P, V, V*V, origin, PX)) ;
		if (fabs(dist_target[0]) < 0.02f)
		{
410
			if (axisPickable(Xt) && !axisPickable(Xs))
411 412
				dist_cam[0] = (P-PX)*(P-PX);
			else
413 414 415 416 417 418
			{
				if ( Qz.norm() > m_lengthAxes[0])
					dist_cam[0] = (P-PX)*(P-PX);
				else
					dist_cam[6] = P*P;
			}
419 420
		}
	}
421

422
	if (axisPickable(Yt) || axisPickable(Ys))
423
	{
424
		Geom::Vec3f PY(0.0f,3.6f*m_lengthAxes[1],0.0f);
425
		dist_target[1] = sqrt(Geom::squaredDistanceLine2Seg(P, V, V*V, origin, PY)) ;
426
		if (fabs(dist_target[1]) < 0.02f)
427
		{
428
			if (axisPickable(Yt) && !axisPickable(Ys))
429 430
				dist_cam[1] = (P-PY)*(P-PY);
			else
431 432 433 434 435 436
			{
				if (Qz.norm() > m_lengthAxes[1])
					dist_cam[1] = (P-PY)*(P-PY);
				else
					dist_cam[7] = P*P;
			}
437
		}
438 439
	}

440
	if (axisPickable(Zt) || axisPickable(Zs))
441
	{
442
		Geom::Vec3f PZ(0.0f,0.0f,3.6f*m_lengthAxes[2]);
443 444 445
		dist_target[2] = sqrt(Geom::squaredDistanceLine2Seg(P, V, V*V, origin, PZ));
		if (fabs(dist_target[2]) < 0.02f )
		{
446
			if (axisPickable(Zt) && !axisPickable(Zs))
447 448
				dist_cam[2] = (P-PZ)*(P-PZ);
			else
449 450 451 452 453 454
			{
				if (Qx.norm() > m_lengthAxes[2])
					dist_cam[2] = (P-PZ)*(P-PZ);
				else
					dist_cam[8] = P*P;
			}
455
		}
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	}

	// find min dist_cam value;
	unsigned int min_index=0;
	float min_val = dist_cam[0];
	for (unsigned int i=1; i<9; ++i)
	{
		if  (dist_cam[i] < min_val)
		{
			min_val = dist_cam[i];
			min_index = i;
		}
	}

	if (min_val < std::numeric_limits<float>::max())
	{
472 473
//		if  (! m_locked_axis[Xt+min_index])
		if (axisPickable(Xt+min_index))
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
			return Xt+min_index;
	}

	return NONE;
}

void FrameManipulator::rotate(unsigned int axis, float angle)
{
	// create axis
	glm::vec3 ax(0.0f,0.0f,0.0f);
	ax[axis-Xr]=1.0f;

	glm::mat4 tr = glm::rotate(glm::mat4(1.0f),angle,ax);
	m_rotations = m_rotations*tr;
}

void FrameManipulator::translate(unsigned int axis, float x)
{
	m_trans += x*m_scaleRendering * glm::vec3(m_rotations[axis-Xt][0],m_rotations[axis-Xt][1],m_rotations[axis-Xt][2]);
}

void FrameManipulator::setLengthAxes()
{
	float avgScale =(m_scale[0]+m_scale[1]+m_scale[2])/3.0f;

	float* positions = reinterpret_cast<float*>(m_vboPos->lockPtr());
	unsigned int ind=3*(6*nb_segments+6+1);

	float sc0 = m_scale[0]/avgScale;
	float sc1 = m_scale[1]/avgScale;
	float sc2 = m_scale[2]/avgScale;

	positions[ind] = 0.23f*sc0;
	ind+=7;
	positions[ind] = 0.23f*sc1;
	ind+=7;
	positions[ind] = 0.23f*sc2;
	ind++;
512
	if ((m_locked_axis[Xs])&&(m_highlighted!=CENTER))
513 514 515
		positions[ind] = 0.0f;
	else
		positions[ind] = 0.27f*sc0;
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
	ind+=3;
	positions[ind] = 0.75f*sc0;
	ind+=3;
	positions[ind] = 0.9f*sc0;
	ind+=3;
	float le = 0.7f*sc0;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=4;

533
	if ((m_locked_axis[Ys])&&(m_highlighted!=CENTER))
534 535 536
		positions[ind] = 0.0f;
	else
		positions[ind] = 0.27f*sc1;
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
	ind+=3;
	positions[ind] = 0.75f*sc1;
	ind+=3;
	positions[ind] = 0.9f*sc1;
	ind+=3;
	le = 0.7f*sc1;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=4;

554
	if ((m_locked_axis[Zs])&&(m_highlighted!=CENTER))
555 556 557
		positions[ind] = 0.0f;
	else
		positions[ind] = 0.27f*sc2;
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 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 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 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 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	ind+=3;
	positions[ind] = 0.75f*sc2;
	ind+=3;
	positions[ind] = 0.9f*sc2;
	ind+=3;
	le = 0.7f*sc2;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;
	ind+=3;
	positions[ind] = le;

	m_vboPos->releasePtr();

	m_lengthAxes = Geom::Vec3f(0.25f*sc0, 0.25f*sc1, 0.25f*sc2);
}

void FrameManipulator::scale(unsigned int axis, float sc)
{
	if (axis==CENTER)
	{
		m_scale[0] *= sc;
		m_scale[1] *= sc;
		m_scale[2] *= sc;
	}
	else
		m_scale[axis-Xs] *= sc;

	setLengthAxes();
}

glm::mat4 FrameManipulator::transfoRenderFrame()
{
	glm::mat4 tr = glm::translate(glm::mat4(1.0f), m_trans);
	tr *= m_rotations;

	float avgScale =(m_scale[0]+m_scale[1]+m_scale[2])/3.0f;
	avgScale *= m_scaleRendering;
	return glm::scale(tr,glm::vec3(avgScale,avgScale,avgScale));
}

glm::mat4 FrameManipulator::transfo()
{
	glm::mat4 tr = glm::translate(glm::mat4(1.0f), m_trans);
	tr *= m_rotations;
	return glm::scale(tr,glm::vec3(m_scale[0],m_scale[1],m_scale[2]));
}

void FrameManipulator::setTranslation(const Geom::Vec3f& P)
{
	m_trans[0] = P[0];
	m_trans[1] = P[1];
	m_trans[2] = P[2];
}

void FrameManipulator::setScale(const Geom::Vec3f& S)
{
	m_scale[0] = S[0];
	m_scale[1] = S[1];
	m_scale[2] = S[2];

	setLengthAxes();
}

bool FrameManipulator::setOrientation(const Geom::Vec3f& X, const Geom::Vec3f& Y)
{
	Geom::Vec3f Z = X^Y;

	if ((X.norm() != 1.0f) || (Y.norm() != 1.0f) || (Z.norm() != 1.0f))
		return false;

	m_rotations[0][0] = X[0];
	m_rotations[0][1] = X[1];
	m_rotations[0][2] = X[2];

	m_rotations[1][0] = Y[0];
	m_rotations[1][1] = Y[1];
	m_rotations[1][2] = Y[2];

	m_rotations[2][0] = Z[0];
	m_rotations[2][1] = Z[1];
	m_rotations[2][2] = Z[2];

	return true;
}

void FrameManipulator::setTransformation( const glm::mat4& transfo)
{
	setTranslation(Geom::Vec3f(transfo[3][0],transfo[3][1],transfo[3][2]));

	Geom::Vec3f Rx(	transfo[0][0], transfo[0][1], transfo[0][2]);
	Geom::Vec3f Ry(	transfo[1][0], transfo[1][1], transfo[1][2]);
	Geom::Vec3f Rz(	transfo[2][0], transfo[2][1], transfo[2][2]);

	setScale(Geom::Vec3f(Rx.normalize(), Ry.normalize(), Rz.normalize()));

	m_rotations[0][0] = Rx[0];
	m_rotations[0][1] = Rx[1];
	m_rotations[0][2] = Rx[2];
	m_rotations[1][0] = Ry[0];
	m_rotations[1][1] = Ry[1];
	m_rotations[1][2] = Ry[2];
	m_rotations[2][0] = Rz[0];
	m_rotations[2][1] = Rz[1];
	m_rotations[2][2] = Rz[2];
}

Sylvain Thery's avatar
Sylvain Thery committed
669
void FrameManipulator::lock(unsigned int axis)
670
{
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
	assert(axis <=Scales);
	switch (axis)
	{
	case Translations:
		m_locked_axis[Xt] = true;
		m_locked_axis[Yt] = true;
		m_locked_axis[Zt] = true;
		break;
	case Rotations:
		m_locked_axis[Xr] = true;
		m_locked_axis[Yr] = true;
		m_locked_axis[Zr] = true;
		break;
	case Scales:
		m_locked_axis[Xs] = true;
		m_locked_axis[Ys] = true;
		m_locked_axis[Zs] = true;
		break;
	default:
		m_locked_axis[axis] = true;
		break;
	}
	setLengthAxes();
694 695
}

Sylvain Thery's avatar
Sylvain Thery committed
696
void FrameManipulator::unlock(unsigned int axis)
697
{
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	assert(axis <=Scales);
	switch (axis)
	{
	case Translations:
		m_locked_axis[Xt] = false;
		m_locked_axis[Yt] = false;
		m_locked_axis[Zt] = false;
		break;
	case Rotations:
		m_locked_axis[Xr] = false;
		m_locked_axis[Yr] = false;
		m_locked_axis[Zr] = false;
		break;
	case Scales:
		m_locked_axis[Xs] = false;
		m_locked_axis[Ys] = false;
		m_locked_axis[Zs] = false;
		break;
	default:
		m_locked_axis[axis] = false;
		break;
	}
	setLengthAxes();
721 722
}

Sylvain Thery's avatar
Sylvain Thery committed
723 724
bool FrameManipulator::locked(unsigned int axis)
{
725
	assert(axis <=Zs);
Sylvain Thery's avatar
Sylvain Thery committed
726 727
	return m_locked_axis[axis];
}
728

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
void FrameManipulator::lockPicking(unsigned int axis)
{
	assert(axis <=Scales);
	switch (axis)
	{
	case Translations:
		m_lockedPicking_axis[Xt] = true;
		m_lockedPicking_axis[Yt] = true;
		m_lockedPicking_axis[Zt] = true;
		break;
	case Rotations:
		m_lockedPicking_axis[Xr] = true;
		m_lockedPicking_axis[Yr] = true;
		m_lockedPicking_axis[Zr] = true;
		break;
	case Scales:
		m_lockedPicking_axis[Xs] = true;
		m_lockedPicking_axis[Ys] = true;
		m_lockedPicking_axis[Zs] = true;
		break;
	default:
		m_lockedPicking_axis[axis] = true;
		break;
	}
	setLengthAxes();
}

void FrameManipulator::unlockPicking(unsigned int axis)
{
	assert(axis <=Scales);
	switch (axis)
	{
	case Translations:
		m_lockedPicking_axis[Xt] = false;
		m_lockedPicking_axis[Yt] = false;
		m_lockedPicking_axis[Zt] = false;
		break;
	case Rotations:
		m_lockedPicking_axis[Xr] = false;
		m_lockedPicking_axis[Yr] = false;
		m_lockedPicking_axis[Zr] = false;
		break;
	case Scales:
		m_lockedPicking_axis[Xs] = false;
		m_lockedPicking_axis[Ys] = false;
		m_lockedPicking_axis[Zs] = false;
		break;
	default:
		m_lockedPicking_axis[axis] = false;
		break;
	}
	setLengthAxes();
}

bool FrameManipulator::lockedPicking(unsigned int axis)
{
	return m_lockedPicking_axis[axis];
}


789 790 791 792 793 794 795 796 797 798 799 800 801 802
Geom::Vec3f  FrameManipulator::getAxis(unsigned int ax)
{
	unsigned int i = (ax-Xt)%3;

	return Geom::Vec3f(m_rotations[i][0],m_rotations[i][1],m_rotations[i][2]);
}

void FrameManipulator::storeProjection(unsigned int ax)
{
	Geom::Vec3f O = getPosition();

	glm::i32vec4 viewport;
	glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
	glm::vec3 winO = glm::project(glm::vec3(O[0],O[1],O[2]), GLSLShader::currentModelView(), GLSLShader::currentProjection(), viewport);
Sylvain Thery's avatar
Sylvain Thery committed
803 804 805 806 807 808 809 810 811
	m_projectedOrigin = Geom::Vec3f(winO[0], winO[1], winO[2]);

	if (ax>CENTER)
	{
		Geom::Vec3f A = getAxis(ax);
		A += O;
		glm::vec3 winA = glm::project(glm::vec3(A[0],A[1],A[2]), GLSLShader::currentModelView(), GLSLShader::currentProjection(), viewport);
		m_projectedSelectedAxis = Geom::Vec3f(winA[0]-winO[0], winA[1]-winO[1],winA[2]-winO[2]);
	}
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
}

float FrameManipulator::angleFromMouse(int x, int y, int dx, int dy)
{
	Geom::Vec3f V(float(x) - m_projectedOrigin[0], float(y) - m_projectedOrigin[1],0.0f);
	Geom::Vec3f dV(float(dx), float(dy), 0.0f);
	Geom::Vec3f W = V^dV;

	float alpha=dV.norm()/4.0f;
	// which direction ?
	if (W*m_projectedSelectedAxis > 0.0f)
		alpha *= -1.0f;
	return alpha;
}

float FrameManipulator::distanceFromMouse(int dx, int dy)
{
	Geom::Vec3f dV(float(dx), float(dy), 0.0f);
	float tr = dV*m_projectedSelectedAxis;
	if (tr>0)
		tr = dV.norm()/100.0f;
	else
		tr = dV.norm()/-100.0f;
	return tr;
}

float FrameManipulator::scaleFromMouse(int dx, int dy)
{
Thery Sylvain's avatar
Thery Sylvain committed
840
	if (abs(dx) > abs(dy))
Sylvain Thery's avatar
Sylvain Thery committed
841 842
	{
		if (dx>0)
Sylvain Thery's avatar
Sylvain Thery committed
843 844
			return 1.01f;
		return 0.99f;
Sylvain Thery's avatar
Sylvain Thery committed
845 846 847 848
	}
	else
	{
		if (dy>0)
Sylvain Thery's avatar
Sylvain Thery committed
849 850
			return 1.01f;
		return 0.99f;
Sylvain Thery's avatar
Sylvain Thery committed
851
	}
852 853
}

Sylvain Thery's avatar
Sylvain Thery committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
void FrameManipulator::translateInScreen(int dx, int dy)
{
	glm::i32vec4 viewport;
	glGetIntegerv(GL_VIEWPORT, &(viewport[0]));

	Geom::Vec3f NO = m_projectedOrigin+Geom::Vec3f(float(dx), float(dy), 0.0f);

	glm::vec3 P = glm::unProject(glm::vec3(NO[0],NO[1],NO[2]), GLSLShader::currentModelView(), GLSLShader::currentProjection(), viewport);

	m_trans[0] = P[0];
	m_trans[1] = P[1];
	m_trans[2] = P[2];
	storeProjection(NONE);
}

void FrameManipulator::rotateInScreen(int dx, int dy)
{
	glm::i32vec4 viewport;
	glGetIntegerv(GL_VIEWPORT, &(viewport[0]));

	Geom::Vec3f NO = m_projectedOrigin+Geom::Vec3f(float(-dy), float(dx), 0.0f);

	glm::vec3 P = glm::unProject(glm::vec3(NO[0],NO[1],NO[2]), GLSLShader::currentModelView(), GLSLShader::currentProjection(), viewport);

	Geom::Vec3f axisRotation(P[0]-m_trans[0], P[1]-m_trans[1], P[2]-m_trans[2]);
	axisRotation.normalize();

881
	glm::mat4 tr = glm::rotate(glm::mat4(1.0f),sqrtf(float(dx*dx+dy*dy))/2.0f,glm::vec3(axisRotation[0],axisRotation[1],axisRotation[2]));
Sylvain Thery's avatar
Sylvain Thery committed
882 883 884
	m_rotations = tr*m_rotations;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
885
} // namespace Utils
Sylvain Thery's avatar
Sylvain Thery committed
886

Pierre Kraemer's avatar
Pierre Kraemer committed
887
} // namespace CGoGN