frameManipulator.cpp 20.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 25 26 27 28 29 30 31 32 33 34
/*******************************************************************************
 * 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                                        *
 *                                                                              *
 *******************************************************************************/

#include "Utils/frameManipulator.h"

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

#include <glm/ext.hpp>
//#include <glm/gtc/matrix_transform.hpp>
//#include "glm/gtc/type_precision.hpp"
//#include "glm/gtc/type_ptr.hpp"

35
//#include <cmath>
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

namespace CGoGN
{
namespace Utils
{


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)
52
	{
53
		m_locked_axis[i]=false;
54 55
		m_lockedPicking_axis[i]=false;
	}
56 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

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

void FrameManipulator::setSize(float radius)
{
	m_scaleRendering = radius;
}

void FrameManipulator::addSize(float radius)
{
	m_scaleRendering += radius;
}

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


void FrameManipulator::draw()
{
	glm::mat4 store = Utils::GLSLShader::currentTransfo();
	Utils::GLSLShader::currentTransfo() *= transfoRenderFrame();
	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));
		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));
		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));
		glDrawArrays(GL_TRIANGLE_STRIP, 4*nb_segments+4, 2*nb_segments+2);
	}


	if (!m_locked_axis[Xs])
	{
		if ((m_highlighted == CENTER) || (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));
		}
		glDrawArrays(GL_LINES, 6*nb_segments+6, 2);
	}

	if (!m_locked_axis[Ys])
	{
		if ((m_highlighted == CENTER) || (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));
		}
		glDrawArrays(GL_LINES, 6*nb_segments+8, 2);
	}

	if (!m_locked_axis[Zs])
	{
		if ((m_highlighted == CENTER) || (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));
		}
		glDrawArrays(GL_LINES, 6*nb_segments+10, 2);
	}

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

		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));
		}
		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));
		}
		glDrawArrays(GL_LINES, 6*nb_segments+28, 2);
		glDrawArrays(GL_TRIANGLE_FAN, 6*nb_segments+30, 6);
	}


 	m_shader->disableVertexAttribs();
 	glPopAttrib();

	Utils::GLSLShader::currentTransfo() = store;

}



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



unsigned int FrameManipulator::pick(const Geom::Vec3f& PP, const Geom::Vec3f& VV, float epsilon)
{
	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);
315 316 317 318 319 320

	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)
321 322 323 324 325
		return NONE;

	// click on center
	if (dist2 < 0.02f*0.02f)
	{
326
		if (axisPickable(CENTER))
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
			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);

346 347 348 349 350 351
	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;
352

353 354 355
		if (fabs(dist_target[3]) < ring_half_width )
			dist_cam[3] = (P-Qx)*(P-Qx);
	}
356 357 358 359 360

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

361 362 363 364 365 366
	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;
367

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

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

376 377 378 379 380 381
	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;
382

383 384 385
		if (fabs(dist_target[5]) <  ring_half_width )
			dist_cam[5] = (P-Qz)*(P-Qz);
	}
386 387 388

	// axes:

389
	if (axisPickable(Xt) || axisPickable(Xs))
390
	{
391
		Geom::Vec3f PX(3.6f*m_lengthAxes[0],0.0f,0.0f);
392 393 394
		dist_target[0] = sqrt(Geom::squaredDistanceLine2Seg(P, V, V*V, origin, PX)) ;
		if (fabs(dist_target[0]) < 0.02f)
		{
395
			if (axisPickable(Xt) && !axisPickable(Xs))
396 397
				dist_cam[0] = (P-PX)*(P-PX);
			else
398 399 400 401 402 403
			{
				if ( Qz.norm() > m_lengthAxes[0])
					dist_cam[0] = (P-PX)*(P-PX);
				else
					dist_cam[6] = P*P;
			}
404 405
		}
	}
406

407

408
	if (axisPickable(Yt) || axisPickable(Ys))
409
	{
410
		Geom::Vec3f PY(0.0f,3.6f*m_lengthAxes[1],0.0f);
411
		dist_target[1] = sqrt(Geom::squaredDistanceLine2Seg(P, V, V*V, origin, PY)) ;
412
		if (fabs(dist_target[1]) < 0.02f)
413
		{
414
			if (axisPickable(Yt) && !axisPickable(Ys))
415 416
				dist_cam[1] = (P-PY)*(P-PY);
			else
417 418 419 420 421 422
			{
				if (Qz.norm() > m_lengthAxes[1])
					dist_cam[1] = (P-PY)*(P-PY);
				else
					dist_cam[7] = P*P;
			}
423
		}
424 425
	}

426 427

	if (axisPickable(Zt) || axisPickable(Zs))
428
	{
429
		Geom::Vec3f PZ(0.0f,0.0f,3.6f*m_lengthAxes[2]);
430 431 432
		dist_target[2] = sqrt(Geom::squaredDistanceLine2Seg(P, V, V*V, origin, PZ));
		if (fabs(dist_target[2]) < 0.02f )
		{
433
			if (axisPickable(Zt) && !axisPickable(Zs))
434 435
				dist_cam[2] = (P-PZ)*(P-PZ);
			else
436 437 438 439 440 441
			{
				if (Qx.norm() > m_lengthAxes[2])
					dist_cam[2] = (P-PZ)*(P-PZ);
				else
					dist_cam[8] = P*P;
			}
442
		}
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	}

	// 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())
	{
459 460
//		if  (! m_locked_axis[Xt+min_index])
		if (axisPickable(Xt+min_index))
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
			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;

497

498 499 500 501 502 503
	positions[ind] = 0.23f*sc0;
	ind+=7;
	positions[ind] = 0.23f*sc1;
	ind+=7;
	positions[ind] = 0.23f*sc2;
	ind++;
504 505 506 507
	if (m_locked_axis[Xs])
		positions[ind] = 0.0f;
	else
		positions[ind] = 0.27f*sc0;
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	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;

525 526 527 528
	if (m_locked_axis[Ys])
		positions[ind] = 0.0f;
	else
		positions[ind] = 0.27f*sc1;
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	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;

546 547 548 549
	if (m_locked_axis[Zs])
		positions[ind] = 0.0f;
	else
		positions[ind] = 0.27f*sc2;
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 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 669 670
	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];
}

671
void FrameManipulator::lock(unsigned int axis)
672
{
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	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();
696 697 698
}


699

700
void FrameManipulator::unlock(unsigned int axis)
701
{
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
	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();
725 726
}

727

728 729
bool FrameManipulator::locked(unsigned int axis)
{
730
	assert(axis <=Zs);
731 732
	return m_locked_axis[axis];
}
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 789 790 791 792 793 794 795 796 797
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];
}


798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
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);
813 814 815 816 817 818 819 820 821
	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]);
	}
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

}


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)
{
	Geom::Vec3f dV(float(dx), float(dy), 0.0f);
	float sc = dV*m_projectedSelectedAxis;
	if (sc>0)
		sc = dV.norm()/100.0f;
	else
		sc = dV.norm()/-100.0f;

	return 1.0f + sc;
}

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
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();

890
	glm::mat4 tr = glm::rotate(glm::mat4(1.0f),float(sqrt(dx*dx+dy*dy))/2.0f,glm::vec3(axisRotation[0],axisRotation[1],axisRotation[2]));
891 892 893 894 895 896
	m_rotations = tr*m_rotations;

}



897 898
}
}