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 38 39 40

namespace CGoGN
{
namespace Utils
{

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

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

	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
134 135

	setLengthAxes();
136 137 138 139
}

void FrameManipulator::setSize(float radius)
{
140 141
	if (m_scaleRendering >0.0f)
		m_scaleRendering = radius;
142 143 144 145 146
}

void FrameManipulator::addSize(float radius)
{
	m_scaleRendering += radius;
147 148
	if (m_scaleRendering <= 0.0f)
		m_scaleRendering -= radius;
149 150 151 152 153 154 155 156 157 158
}

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


void FrameManipulator::draw()
{
159 160
	Utils::GLSLShader::pushTransfo();
	Utils::GLSLShader::applyTransfo(transfoRenderFrame());
161 162 163 164 165 166 167 168 169 170 171 172
	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
173
		m_shader->bind();
174 175 176 177 178 179 180 181 182
		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
183
		m_shader->bind();
184 185 186 187 188 189 190 191 192
		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
193
		m_shader->bind();
194 195 196
		glDrawArrays(GL_TRIANGLE_STRIP, 4*nb_segments+4, 2*nb_segments+2);
	}

197 198 199 200
	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
201
		m_shader->bind();
202 203 204 205
		glDrawArrays(GL_LINES, 6*nb_segments+6, 6);
	}
	else
	{
206 207
	if (!m_locked_axis[Xs])
	{
208
		if (m_highlighted == Xs)
209 210 211 212 213 214 215 216 217
		{
			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));
		}
Sylvain Thery's avatar
Sylvain Thery committed
218
		m_shader->bind();
219 220 221 222 223
		glDrawArrays(GL_LINES, 6*nb_segments+6, 2);
	}

	if (!m_locked_axis[Ys])
	{
224
		if (m_highlighted == Ys)
225 226 227 228 229 230 231 232 233
		{
			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));
		}
Sylvain Thery's avatar
Sylvain Thery committed
234
		m_shader->bind();
235 236 237 238 239
		glDrawArrays(GL_LINES, 6*nb_segments+8, 2);
	}

	if (!m_locked_axis[Zs])
	{
240
		if (m_highlighted == Zs)
241 242 243 244 245 246 247 248 249
		{
			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));
		}
Sylvain Thery's avatar
Sylvain Thery committed
250
		m_shader->bind();
251 252
		glDrawArrays(GL_LINES, 6*nb_segments+10, 2);
	}
253 254
	}

255 256 257 258 259 260 261 262 263 264 265 266 267 268

	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
269
		m_shader->bind();
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
		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
286
		m_shader->bind();
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
		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
303
		m_shader->bind();
304 305 306 307 308 309 310 311
		glDrawArrays(GL_LINES, 6*nb_segments+28, 2);
		glDrawArrays(GL_TRIANGLE_FAN, 6*nb_segments+30, 6);
	}


 	m_shader->disableVertexAttribs();
 	glPopAttrib();

312
 	Utils::GLSLShader::popTransfo();
313
	Utils::GLSLShader::updateCurrentMatrices();
Sylvain Thery's avatar
Sylvain Thery committed
314
	m_shader->unbind();
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
}



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);
340 341 342 343 344 345

	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)
346 347 348 349 350
		return NONE;

	// click on center
	if (dist2 < 0.02f*0.02f)
	{
351
		if (axisPickable(CENTER))
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
			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);

371 372 373 374 375 376
	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;
377

378 379 380
		if (fabs(dist_target[3]) < ring_half_width )
			dist_cam[3] = (P-Qx)*(P-Qx);
	}
381 382 383 384 385

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

386 387 388 389 390 391
	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;
392

393 394 395
		if (fabs(dist_target[4]) < ring_half_width )
			dist_cam[4] = (P-Qy)*(P-Qy);
	}
396 397 398 399 400

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

401 402 403 404 405 406
	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;
407

408 409 410
		if (fabs(dist_target[5]) <  ring_half_width )
			dist_cam[5] = (P-Qz)*(P-Qz);
	}
411 412 413

	// axes:

414
	if (axisPickable(Xt) || axisPickable(Xs))
415
	{
416
		Geom::Vec3f PX(3.6f*m_lengthAxes[0],0.0f,0.0f);
417 418 419
		dist_target[0] = sqrt(Geom::squaredDistanceLine2Seg(P, V, V*V, origin, PX)) ;
		if (fabs(dist_target[0]) < 0.02f)
		{
420
			if (axisPickable(Xt) && !axisPickable(Xs))
421 422
				dist_cam[0] = (P-PX)*(P-PX);
			else
423 424 425 426 427 428
			{
				if ( Qz.norm() > m_lengthAxes[0])
					dist_cam[0] = (P-PX)*(P-PX);
				else
					dist_cam[6] = P*P;
			}
429 430
		}
	}
431

432

433
	if (axisPickable(Yt) || axisPickable(Ys))
434
	{
435
		Geom::Vec3f PY(0.0f,3.6f*m_lengthAxes[1],0.0f);
436
		dist_target[1] = sqrt(Geom::squaredDistanceLine2Seg(P, V, V*V, origin, PY)) ;
437
		if (fabs(dist_target[1]) < 0.02f)
438
		{
439
			if (axisPickable(Yt) && !axisPickable(Ys))
440 441
				dist_cam[1] = (P-PY)*(P-PY);
			else
442 443 444 445 446 447
			{
				if (Qz.norm() > m_lengthAxes[1])
					dist_cam[1] = (P-PY)*(P-PY);
				else
					dist_cam[7] = P*P;
			}
448
		}
449 450
	}

451 452

	if (axisPickable(Zt) || axisPickable(Zs))
453
	{
454
		Geom::Vec3f PZ(0.0f,0.0f,3.6f*m_lengthAxes[2]);
455 456 457
		dist_target[2] = sqrt(Geom::squaredDistanceLine2Seg(P, V, V*V, origin, PZ));
		if (fabs(dist_target[2]) < 0.02f )
		{
458
			if (axisPickable(Zt) && !axisPickable(Zs))
459 460
				dist_cam[2] = (P-PZ)*(P-PZ);
			else
461 462 463 464 465 466
			{
				if (Qx.norm() > m_lengthAxes[2])
					dist_cam[2] = (P-PZ)*(P-PZ);
				else
					dist_cam[8] = P*P;
			}
467
		}
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	}

	// 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())
	{
484 485
//		if  (! m_locked_axis[Xt+min_index])
		if (axisPickable(Xt+min_index))
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 512 513 514 515 516 517 518 519 520 521
			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;

522

523 524 525 526 527 528
	positions[ind] = 0.23f*sc0;
	ind+=7;
	positions[ind] = 0.23f*sc1;
	ind+=7;
	positions[ind] = 0.23f*sc2;
	ind++;
529
	if ((m_locked_axis[Xs])&&(m_highlighted!=CENTER))
530 531 532
		positions[ind] = 0.0f;
	else
		positions[ind] = 0.27f*sc0;
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	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;

550
	if ((m_locked_axis[Ys])&&(m_highlighted!=CENTER))
551 552 553
		positions[ind] = 0.0f;
	else
		positions[ind] = 0.27f*sc1;
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	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;

571
	if ((m_locked_axis[Zs])&&(m_highlighted!=CENTER))
572 573 574
		positions[ind] = 0.0f;
	else
		positions[ind] = 0.27f*sc2;
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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	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];
}

696
void FrameManipulator::lock(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] = 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();
721 722 723
}


724

725
void FrameManipulator::unlock(unsigned int axis)
726
{
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
	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();
750 751
}

752

753 754
bool FrameManipulator::locked(unsigned int axis)
{
755
	assert(axis <=Zs);
756 757
	return m_locked_axis[axis];
}
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 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
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];
}


823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
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);
838 839 840 841 842 843 844 845 846
	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]);
	}
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

}


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
878
	if (abs(dx) > abs(dy))
Sylvain Thery's avatar
Sylvain Thery committed
879 880 881 882 883 884 885 886 887 888 889
	{
		if (dx>0)
			return 1.01;
		return 0.99;
	}
	else
	{
		if (dy>0)
			return 1.01;
		return 0.99;
	}
890 891
}

892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
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();

920
	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]));
921 922 923 924 925 926
	m_rotations = tr*m_rotations;

}



927 928
}
}