surfaceDeformation.cpp 21.7 KB
Newer Older
1 2
#include "surfaceDeformation.h"

3 4 5
#include "Algo/Selection/raySelector.h"
#include "Algo/Selection/collector.h"

6 7 8
#include "Algo/Geometry/normal.h"
#include "Algo/Geometry/laplacian.h"

9 10
#include "Utils/drawer.h"

11 12 13
#include <QKeyEvent>
#include <QMouseEvent>

14 15

PerMapParameterSet::PerMapParameterSet(MapHandlerGen* mh) :
16
	verticesSelectionMode(LOCKED)
17
{
18
	PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(mh)->getMap();
19

20 21 22 23 24 25 26 27 28 29 30 31
	AttributeContainer& cont = map->getAttributeContainer<VERTEX>();

	std::vector<std::string> names;
	std::vector<std::string> types;
	cont.getAttributesNames(names);
	cont.getAttributesTypes(types);
	std::string vec3TypeName = nameOfType(PFP2::VEC3());
	for(unsigned int i = 0; i < names.size(); ++i)
	{
		if(types[i] == vec3TypeName)
		{
			if(names[i] == "position") // try to select a position attribute named "position"
32
				positionAttribute = mh->getAttribute<PFP2::VEC3, VERTEX>(QString::fromStdString(names[i]));
33 34 35 36 37 38 39 40 41
		}
	}

	if(!positionAttribute.isValid())
	{
		for(unsigned int i = 0; i < names.size(); ++i)
		{
			if(types[i] == vec3TypeName)
			{
42
				positionAttribute = mh->getAttribute<PFP2::VEC3, VERTEX>(QString::fromStdString(names[i]));
43 44 45 46
				break;
			}
		}
	}
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

	lockingMarker = new CellMarker<VERTEX>(*map);
	handleMarker = new CellMarker<VERTEX>(*map);

	positionInit = mh->addAttribute<PFP2::VEC3, VERTEX>("positionInit") ;
	map->copyAttribute(positionInit, positionAttribute) ;

	vertexNormal = mh->addAttribute<PFP2::VEC3, VERTEX>("vertexNormal");
	edgeAngle = mh->addAttribute<PFP2::REAL, EDGE>("edgeAngle");
	edgeWeight = mh->addAttribute<PFP2::REAL, EDGE>("edgeWeight");
	vertexArea = mh->addAttribute<PFP2::REAL, VERTEX>("vertexArea");
	diffCoord = mh->addAttribute<PFP2::VEC3, VERTEX>("diffCoord");
	vertexRotationMatrix = mh->addAttribute<Eigen_Matrix3f, VERTEX>("vertexRotationMatrix") ;
	rotatedDiffCoord = mh->addAttribute<PFP2::VEC3, VERTEX>("rotatedDiffCoord") ;

	Algo::Surface::Geometry::computeNormalVertices<PFP2>(*map, positionAttribute, vertexNormal) ;
	Algo::Surface::Geometry::computeAnglesBetweenNormalsOnEdges<PFP2>(*map, positionAttribute, edgeAngle) ;
	Algo::Surface::Geometry::computeCotanWeightEdges<PFP2>(*map, positionAttribute, edgeWeight) ;
	Algo::Surface::Geometry::computeVoronoiAreaVertices<PFP2>(*map, positionAttribute, vertexArea) ;
	Algo::Surface::Geometry::computeLaplacianCotanVertices<PFP2>(*map, edgeWeight, vertexArea, positionAttribute, diffCoord) ;

	for(unsigned int i = vertexRotationMatrix.begin(); i != vertexRotationMatrix.end() ; vertexRotationMatrix.next(i))
		vertexRotationMatrix[i] = Eigen::Matrix3f::Identity();

	vIndex = mh->addAttribute<unsigned int, VERTEX>("vIndex");
	nb_vertices = static_cast<AttribMap*>(map)->computeIndexCells<VERTEX>(vIndex);

	solver = new LinearSolver<PFP2::REAL>(nb_vertices);
75 76
}

77 78 79 80 81 82
PerMapParameterSet::~PerMapParameterSet()
{
	delete lockingMarker;
	delete handleMarker;
}

83

84 85
bool SurfaceDeformationPlugin::enable()
{
86 87 88 89 90
	m_dockTab = new SurfaceDeformationDockTab(this);
	addTabInDock(m_dockTab, "Surface Deformation");

	connect(m_dockTab->mapList, SIGNAL(itemSelectionChanged()), this, SLOT(cb_selectedMapChanged()));
	connect(m_dockTab->combo_positionAttribute, SIGNAL(currentIndexChanged(int)), this, SLOT(cb_positionAttributeChanged(int)));
91 92
	connect(m_dockTab->radio_locked, SIGNAL(toggled(bool)), this, SLOT(cb_selectLockedVertices(bool)));
	connect(m_dockTab->radio_handle, SIGNAL(toggled(bool)), this, SLOT(cb_selectHandleVertices(bool)));
93 94 95 96 97

	connect(m_window, SIGNAL(viewAndPluginLinked(View*, Plugin*)), this, SLOT(viewLinked(View*, Plugin*)));
	connect(m_window, SIGNAL(viewAndPluginUnlinked(View*, Plugin*)), this, SLOT(viewUnlinked(View*, Plugin*)));
	connect(m_window, SIGNAL(currentViewChanged(View*)), this, SLOT(currentViewChanged(View*)));

98 99 100 101 102 103
	Utils::ShaderColorPerVertex* s = new Utils::ShaderColorPerVertex();
	registerShader(s);

	m_drawer = new Utils::Drawer();
	registerShader(m_drawer->getShader());

104 105 106
	return true;
}

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
void SurfaceDeformationPlugin::disable()
{
	delete m_drawer;
}

void SurfaceDeformationPlugin::redraw(View* view)
{
	if(selecting)
	{
		glDisable(GL_LIGHTING) ;
		m_drawer->newList(GL_COMPILE_AND_EXECUTE) ;
		m_drawer->lineWidth(2.0f) ;
		m_drawer->begin(GL_LINES) ;
		m_drawer->color3f(0.0f, 0.0f, 1.0f) ;
		m_drawer->vertex(selectionCenter) ;
		m_drawer->vertex(selectionCenter + selectionRadius * PFP2::VEC3(1,0,0)) ;
		m_drawer->vertex(selectionCenter) ;
		m_drawer->vertex(selectionCenter + selectionRadius * PFP2::VEC3(-1,0,0)) ;
		m_drawer->vertex(selectionCenter) ;
		m_drawer->vertex(selectionCenter + selectionRadius * PFP2::VEC3(0,1,0)) ;
		m_drawer->vertex(selectionCenter) ;
		m_drawer->vertex(selectionCenter + selectionRadius * PFP2::VEC3(0,-1,0)) ;
		m_drawer->vertex(selectionCenter) ;
		m_drawer->vertex(selectionCenter + selectionRadius * PFP2::VEC3(0,0,1)) ;
		m_drawer->vertex(selectionCenter) ;
		m_drawer->vertex(selectionCenter + selectionRadius * PFP2::VEC3(0,0,-1)) ;
		m_drawer->end() ;
		m_drawer->endList() ;
	}

	ParameterSet* params = h_viewParams[view];
	MapHandlerGen* mh = params->selectedMap;
	if(mh)
	{
		PerMapParameterSet* perMap = params->perMap[mh->getName()];

		if(!perMap->locked_vertices.empty() || !perMap->handle_vertices.empty())
		{
			glDisable(GL_LIGHTING) ;
			m_drawer->newList(GL_COMPILE_AND_EXECUTE) ;
			m_drawer->pointSize(4.0f) ;
			m_drawer->begin(GL_POINTS) ;
			m_drawer->color3f(1.0f, 0.0f, 0.0f) ;
			for(unsigned int i = 0; i < perMap->locked_vertices.size(); ++i)
			{
				if (!perMap->handleMarker->isMarked(perMap->locked_vertices[i]))
					m_drawer->vertex(perMap->positionAttribute[perMap->locked_vertices[i]]) ;
			}
			m_drawer->color3f(0.0f, 0.0f, 1.0f) ;
			for(unsigned int i = 0; i < perMap->handle_vertices.size(); ++i)
				m_drawer->vertex(perMap->positionAttribute[perMap->handle_vertices[i]]) ;
			m_drawer->end() ;
			m_drawer->endList() ;
		}
	}
}

164 165 166 167 168
void SurfaceDeformationPlugin::keyPress(View* view, QKeyEvent* event)
{
	if(event->key() == Qt::Key_Shift)
	{
		view->setMouseTracking(true);
169
		selecting = true;
170 171 172 173 174 175 176 177 178
		view->updateGL();
	}
}

void SurfaceDeformationPlugin::keyRelease(View* view, QKeyEvent* event)
{
	if(event->key() == Qt::Key_Shift)
	{
		view->setMouseTracking(false);
179
		selecting = false;
180 181 182 183 184 185
		view->updateGL();
	}
}

void SurfaceDeformationPlugin::mousePress(View* view, QMouseEvent* event)
{
186
	if(event->button() == Qt::LeftButton && selecting)
187 188
	{
		ParameterSet* params = h_viewParams[view];
189
		PerMapParameterSet* perMap = params->getCurrentMapParameterSet();
190 191 192 193

		QPoint pixel(event->x(), event->y());
		qglviewer::Vec orig;
		qglviewer::Vec dir;
194
		view->camera()->convertClickToLine(pixel, orig, dir);
195

196 197
		PFP2::VEC3 rayA(orig.x, orig.y, orig.z);
		PFP2::VEC3 AB(dir.x, dir.y, dir.z);
198 199

		Dart d ;
200 201
		PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(params->selectedMap)->getMap();
		Algo::Selection::vertexRaySelection<PFP2>(*map, perMap->positionAttribute, rayA, AB, d) ;
202 203 204

		if(d != NIL)
		{
205
			Algo::Surface::Selection::Collector_WithinSphere<PFP2> neigh(*map, perMap->positionAttribute, selectionRadius) ;
206 207 208
			neigh.collectAll(d) ;
			const std::vector<Dart>& insideV = neigh.getInsideVertices() ;

209
			for(unsigned int i = 0; i < insideV.size(); ++i)
210
			{
211 212
				unsigned int v = map->getEmbedding<VERTEX>(insideV[i]) ;
				if (!perMap->lockingMarker->isMarked(v))
213
				{
214 215
					perMap->locked_vertices.push_back(v) ;
					perMap->lockingMarker->mark(v);
216
				}
217
				if(perMap->verticesSelectionMode == HANDLE)
218
				{
219
					if(!perMap->handleMarker->isMarked(v))
220
					{
221 222
						perMap->handle_vertices.push_back(v) ;
						perMap->handleMarker->mark(v);
223 224 225
					}
				}
			}
226 227 228

			LinearSolving::resetSolver(perMap->solver, false) ;

229 230 231
			view->updateGL() ;
		}
	}
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	else if(event->button() == Qt::RightButton && event->modifiers() & Qt::ShiftModifier)
	{
		view->setMouseTracking(false) ;

		ParameterSet* params = h_viewParams[view];
		PerMapParameterSet* perMap = params->getCurrentMapParameterSet();

		selecting = false ;
		dragging = true ;

		dragZ = 0;
		for(unsigned int i = 0; i < perMap->handle_vertices.size(); ++i)
		{
			const PFP2::VEC3& p = perMap->positionAttribute[perMap->handle_vertices[i]] ;
			qglviewer::Vec q = view->camera()->projectedCoordinatesOf(qglviewer::Vec(p[0],p[1],p[2]));
			dragZ += q.z ;
		}
		dragZ /= perMap->handle_vertices.size() ;

		qglviewer::Vec p(event->x(), event->y(), dragZ);
		dragPrevious = view->camera()->unprojectedCoordinatesOf(p);
	}
254 255 256 257 258 259
}

void SurfaceDeformationPlugin::mouseRelease(View* view, QMouseEvent* event)
{
	if(event->button() == Qt::RightButton)
	{
260
		dragging = false ;
261 262 263 264 265 266
	}
}

void SurfaceDeformationPlugin::mouseMove(View* view, QMouseEvent* event)
{
	ParameterSet* params = h_viewParams[view];
267 268
	MapHandlerGen* map = params->selectedMap;
	PerMapParameterSet* perMap = params->perMap[map->getName()];
269 270

	if(dragging)
271
	{
272 273
		qglviewer::Vec p(event->x(), event->y(), dragZ);
		qglviewer::Vec q = view->camera()->unprojectedCoordinatesOf(p);
274

275 276 277 278
		qglviewer::Vec vec = q - dragPrevious;
		PFP2::VEC3 t(vec.x, vec.y, vec.z);
		for(unsigned int i = 0; i < perMap->handle_vertices.size(); ++i)
			perMap->positionAttribute[perMap->handle_vertices[i]] += t ;
279

280
		dragPrevious = q ;
281

282
//		matchDiffCoord(view, map);
283
//		for(unsigned int i = 0; i < 2; ++i)
284
			asRigidAsPossible(view, map);
285

286
		params->selectedMap->updateVBO(perMap->positionAttribute);
287 288 289 290 291 292 293 294

		view->updateGL();
	}
	else if(selecting)
	{
		QPoint pixel(event->x(), event->y());
		qglviewer::Vec orig;
		qglviewer::Vec dir;
295
		view->camera()->convertClickToLine(pixel, orig, dir);
296

297 298
		PFP2::VEC3 rayA(orig.x, orig.y, orig.z);
		PFP2::VEC3 AB(dir.x, dir.y, dir.z);
299 300

		Dart d ;
301 302
		PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(params->selectedMap)->getMap();
		Algo::Selection::vertexRaySelection<PFP2>(*map, perMap->positionAttribute, rayA, AB, d) ;
303 304
		if(d != NIL)
		{
305
			selectionCenter = perMap->positionAttribute[d] ;
306 307 308 309 310
			view->updateGL() ;
		}
	}
}

311 312 313 314 315 316 317 318 319 320 321 322
void SurfaceDeformationPlugin::wheelEvent(View* view, QWheelEvent* event)
{
	if(selecting)
	{
		if(event->delta() > 0)
			selectionRadius *= 0.9f ;
		else
			selectionRadius *= 1.1f ;
		view->updateGL() ;
	}
}

323 324 325 326 327 328 329 330 331
void SurfaceDeformationPlugin::viewLinked(View* view, Plugin* plugin)
{
	if(plugin == this)
	{
		ParameterSet* params = new ParameterSet();
		h_viewParams.insert(view, params);
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
		foreach(MapHandlerGen* map, maps)
		{
332
			PerMapParameterSet* p = new PerMapParameterSet(map);
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
			params->perMap.insert(map->getName(), p);
		}
		if (!maps.empty())
			changeSelectedMap(view, maps[0]);

		connect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		connect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));

		if(view->isCurrentView())
			m_dockTab->refreshUI(params);
	}
}

void SurfaceDeformationPlugin::viewUnlinked(View* view, Plugin* plugin)
{
	if(plugin == this)
	{
350 351 352 353 354 355 356
		ParameterSet* params = h_viewParams[view];
		QHash<QString, PerMapParameterSet*>::const_iterator i = params->perMap.constBegin();
		while (i != params->perMap.constEnd())
		{
			delete i.value();
			++i;
		}
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
		h_viewParams.remove(view);

		disconnect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		disconnect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));
	}
}

void SurfaceDeformationPlugin::currentViewChanged(View* view)
{
	if(isLinkedToView(view))
		m_dockTab->refreshUI(h_viewParams[view]);
}

void SurfaceDeformationPlugin::mapLinked(MapHandlerGen* m)
{
	View* view = static_cast<View*>(QObject::sender());
	assert(isLinkedToView(view));

	ParameterSet* params = h_viewParams[view];
376
	PerMapParameterSet* p = new PerMapParameterSet(m);
377 378 379 380 381 382 383 384 385 386 387 388 389
	params->perMap.insert(m->getName(), p);
	if(params->selectedMap == NULL || params->perMap.count() == 1)
		changeSelectedMap(view, m);
	else
		m_dockTab->refreshUI(params);
}

void SurfaceDeformationPlugin::mapUnlinked(MapHandlerGen* m)
{
	View* view = static_cast<View*>(QObject::sender());
	assert(isLinkedToView(view));

	ParameterSet* params = h_viewParams[view];
390
	delete params->perMap[m->getName()];
391 392 393 394 395 396 397 398 399 400 401 402 403
	params->perMap.remove(m->getName());

	if(params->selectedMap == m)
	{
		if(!params->perMap.empty())
			changeSelectedMap(view, m_window->getMap(params->perMap.begin().key()));
		else
			changeSelectedMap(view, NULL);
	}
	else
		m_dockTab->refreshUI(params);
}

404 405 406 407 408 409
void SurfaceDeformationPlugin::attributeAdded()
{
	ParameterSet* params = h_viewParams[m_window->getCurrentView()];
	m_dockTab->refreshUI(params);
}

410 411 412 413 414 415 416 417 418 419 420 421 422 423
void SurfaceDeformationPlugin::changeSelectedMap(View* view, MapHandlerGen* map)
{
	ParameterSet* params = h_viewParams[view];

	MapHandlerGen* prev = params->selectedMap;
	params->selectedMap = map;

	if(view->isCurrentView())
	{
		if(prev)
			disconnect(map, SIGNAL(attributeAdded()), this, SLOT(attributeAdded()));
		if(map)
			connect(map, SIGNAL(attributeAdded()), this, SLOT(attributeAdded()));

424 425
		selectionRadius = map->getBBdiagSize() / 50.0;

426 427 428 429 430 431 432 433
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void SurfaceDeformationPlugin::changePositionAttribute(View* view, MapHandlerGen* map, VertexAttribute<PFP2::VEC3> attribute)
{
	ParameterSet* params = h_viewParams[view];
434
	params->perMap[map->getName()]->positionAttribute = attribute;
435 436 437 438

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
439 440 441 442 443 444 445 446 447 448 449 450 451
//		view->updateGL();
	}
}

void SurfaceDeformationPlugin::changeVerticesSelectionMode(View* view, MapHandlerGen* map, SelectionMode m)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()]->verticesSelectionMode = m;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
//		view->updateGL();
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	}
}

void SurfaceDeformationPlugin::cb_selectedMapChanged()
{
	if(!b_refreshingUI)
	{
		QList<QListWidgetItem*> currentItems = m_dockTab->mapList->selectedItems();
		if(!currentItems.empty())
			changeSelectedMap(m_window->getCurrentView(), m_window->getMap(currentItems[0]->text()));
	}
}

void SurfaceDeformationPlugin::cb_positionAttributeChanged(int index)
{
	if(!b_refreshingUI)
	{
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		changePositionAttribute(view, map, map->getAttribute<PFP2::VEC3, VERTEX>(m_dockTab->combo_positionAttribute->currentText()));
	}
}

void SurfaceDeformationPlugin::cb_selectLockedVertices(bool b)
{
	if(!b_refreshingUI)
	{
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changeVerticesSelectionMode(view, map, LOCKED);
	}
}

void SurfaceDeformationPlugin::cb_selectHandleVertices(bool b)
{
	if(!b_refreshingUI)
	{
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changeVerticesSelectionMode(view, map, HANDLE);
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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 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
void SurfaceDeformationPlugin::matchDiffCoord(View* view, MapHandlerGen* mh)
{
	PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(mh)->getMap();
	PerMapParameterSet* perMap = h_viewParams[view]->perMap[mh->getName()];

	LinearSolving::initSolver(perMap->solver, perMap->nb_vertices, true, true) ;
	for(int coord = 0; coord < 3; ++coord)
	{
		LinearSolving::setupVariables<PFP2>(*map, perMap->solver, perMap->vIndex, *perMap->lockingMarker, perMap->positionAttribute, coord) ;
		LinearSolving::startMatrix(perMap->solver) ;
		LinearSolving::addRowsRHS_Laplacian_Cotan<PFP2>(*map, perMap->solver, perMap->vIndex, perMap->edgeWeight, perMap->vertexArea, perMap->diffCoord, coord) ;
//		LinearSolving::addRowsRHS_Laplacian_Topo<PFP2>(*map, perMap->solver, perMap->vIndex, perMap->diffCoord, coord);
		LinearSolving::endMatrix(perMap->solver) ;
		LinearSolving::solve(perMap->solver) ;
		LinearSolving::getResult<PFP2>(*map, perMap->solver, perMap->vIndex, perMap->positionAttribute, coord) ;
		LinearSolving::resetSolver(perMap->solver, true) ;
	}
}

void SurfaceDeformationPlugin::asRigidAsPossible(View* view, MapHandlerGen* mh)
{
	PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(mh)->getMap();
	PerMapParameterSet* perMap = h_viewParams[view]->perMap[mh->getName()];

	CellMarkerNoUnmark<VERTEX> m(*map) ;

	for(Dart d = map->begin(); d != map->end(); map->next(d))
	{
		if(!m.isMarked(d))
		{
			m.mark(d) ;

			Eigen::Matrix3f cov = Eigen::Matrix3f::Zero() ;
			PFP2::VEC3 p = perMap->positionAttribute[d] ;
			PFP2::VEC3 pInit = perMap->positionInit[d] ;
			PFP2::REAL area = perMap->vertexArea[d] ;
			Dart it = d ;
			do
			{
				Dart neigh = map->phi1(it) ;
				PFP2::VEC3 v = perMap->positionAttribute[neigh] - p ;
				PFP2::VEC3 vv = perMap->positionInit[neigh] - pInit ;
				for(unsigned int i = 0; i < 3; ++i)
					for(unsigned int j = 0; j < 3; ++j)
						cov(i,j) += v[i] * vv[j] * perMap->edgeWeight[it] / area ;
				Dart dboundary = map->phi_1(it) ;
				if(map->phi2(dboundary) == dboundary)
				{
					v = perMap->positionAttribute[dboundary] - p ;
					vv = perMap->positionInit[dboundary] - p ;
					for(unsigned int i = 0; i < 3; ++i)
						for(unsigned int j = 0; j < 3; ++j)
							cov(i,j) += v[i] * vv[j] * perMap->edgeWeight[dboundary] / area ;
				}
				it = map->alpha1(it) ;
			} while(it != d) ;

			Eigen::JacobiSVD<Eigen::Matrix3f> svd(cov, Eigen::ComputeFullU | Eigen::ComputeFullV) ;
			Eigen::Matrix3f R = svd.matrixU() * svd.matrixV().transpose() ;

			if(R.determinant() < 0)
			{
				Eigen::Matrix3f U = svd.matrixU() ;
				for(unsigned int i = 0; i < 3; ++i)
					U(i,2) *= -1 ;
				R = U * svd.matrixV().transpose() ;
			}

			perMap->vertexRotationMatrix[d] = R ;
		}
	}

	for(Dart d = map->begin(); d != map->end(); map->next(d))
	{
		if(m.isMarked(d))
		{
			m.unmark(d) ;

			unsigned int degree = 0 ;
			Eigen::Matrix3f r = Eigen::Matrix3f::Zero() ;
			Dart it = d ;
			do
			{
				r += perMap->vertexRotationMatrix[map->phi1(it)] ;
				++degree ;
				Dart dboundary = map->phi_1(it) ;
				if(map->phi2(dboundary) == dboundary)
				{
					r += perMap->vertexRotationMatrix[dboundary] ;
					++degree ;
				}
				it = map->alpha1(it) ;
			} while(it != d) ;
			r += perMap->vertexRotationMatrix[d] ;
			r /= degree + 1 ;
			PFP2::VEC3& dc = perMap->diffCoord[d] ;
			Eigen::Vector3f rdc(dc[0], dc[1], dc[2]) ;
			rdc = r * rdc ;
			perMap->rotatedDiffCoord[d] = PFP2::VEC3(rdc[0], rdc[1], rdc[2]) ;

//			Eigen::Vector3f rdc = Eigen::Vector3f::Zero() ;
//			Dart it = d ;
//			PFP::REAL vArea = perMap->vertexArea[d] ;
//			Eigen::Matrix3f& rotM = perMap->vertexRotationMatrix[d] ;
//			PFP::REAL val = 0 ;
//			do
//			{
//				Dart ddn = map->phi1(it) ;
//				PFP::REAL w = perMap->edgeWeight[it] / vArea ;
//				PFP::VEC3 vv = (perMap->positionInit[ddn] - perMap->positionInit[it]) * w ;
//				val += w ;
//				Eigen::Matrix3f r = 0.5f * (perMap->vertexRotationMatrix[ddn] + rotM) ;
//				Eigen::Vector3f vvr = r * Eigen::Vector3f(vv[0], vv[1], vv[2]) ;
//				rdc += vvr ;
//				Dart dboundary = map->phi_1(it) ;
//				if(map->phi2(dboundary) == dboundary)
//				{
//					w = perMap->edgeWeight[dboundary] / vArea ;
//					vv = (perMap->positionInit[dboundary] - perMap->positionInit[it]) * w ;
//					val += w ;
//					r = 0.5f * (perMap->vertexRotationMatrix[dboundary] + rotM) ;
//					vvr = r * Eigen::Vector3f(vv[0], vv[1], vv[2]) ;
//					rdc += vvr ;
//				}
//				it = map->alpha1(it) ;
//			} while(it != d) ;
//			rdc /= val ;
//			perMap->rotatedDiffCoord[d] = PFP::VEC3(rdc[0], rdc[1], rdc[2]) ;
		}
	}

	LinearSolving::initSolver(perMap->solver, perMap->nb_vertices, true, true) ;
	for(int coord = 0; coord < 3; ++coord)
	{
		LinearSolving::setupVariables<PFP2>(*map, perMap->solver, perMap->vIndex, *perMap->lockingMarker, perMap->positionAttribute, coord) ;
		LinearSolving::startMatrix(perMap->solver) ;
		LinearSolving::addRowsRHS_Laplacian_Cotan<PFP2>(*map, perMap->solver, perMap->vIndex, perMap->edgeWeight, perMap->vertexArea, perMap->rotatedDiffCoord, coord) ;
		LinearSolving::endMatrix(perMap->solver) ;
		LinearSolving::solve(perMap->solver) ;
		LinearSolving::getResult<PFP2>(*map, perMap->solver, perMap->vIndex, perMap->positionAttribute, coord) ;
		LinearSolving::resetSolver(perMap->solver, true) ;
	}
}

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 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713


void SurfaceDeformationDockTab::refreshUI(ParameterSet* params)
{
	plugin->setRefreshingUI(true);

	mapList->clear();
	combo_positionAttribute->clear();

	MapHandlerGen* mh = params->selectedMap;

	QHash<QString, PerMapParameterSet*>::const_iterator i = params->perMap.constBegin();
	while (i != params->perMap.constEnd())
	{
		mapList->addItem(i.key());
		if(mh != NULL && i.key() == mh->getName())
		{
			QList<QListWidgetItem*> item = mapList->findItems(mh->getName(), Qt::MatchExactly);
			item[0]->setSelected(true);

			PerMapParameterSet* p = params->perMap[mh->getName()];

			GenericMap* map = mh->getGenericMap();

			AttributeContainer& cont = map->getAttributeContainer<VERTEX>();

			std::vector<std::string> names;
			std::vector<std::string> types;
			cont.getAttributesNames(names);
			cont.getAttributesTypes(types);
			std::string vec3TypeName = nameOfType(PFP2::VEC3());
			unsigned int j = 0;
			for(unsigned int i = 0; i < names.size(); ++i)
			{
				if(types[i] == vec3TypeName)
				{
					combo_positionAttribute->addItem(QString::fromStdString(names[i]));
					if(!p->positionAttribute.isValid())
					{								// if nothing is specified in the parameter set
						if(names[i] == "position")	// try to select a position attribute named "position"
						{
							p->positionAttribute = mh->getAttribute<PFP2::VEC3, VERTEX>(QString::fromStdString(names[i]));
							combo_positionAttribute->setCurrentIndex(j);
						}
					}
					++j;
				}
			}

			j = 0;
			if(!p->positionAttribute.isValid())
			{
				for(unsigned int i = 0; i < names.size(); ++i)
				{
					if(types[i] == vec3TypeName)
					{
						p->positionAttribute = mh->getAttribute<PFP2::VEC3, VERTEX>(QString::fromStdString(names[i]));
						combo_positionAttribute->setCurrentIndex(j);
						++j;
						break;
					}
				}
			}

			radio_locked->setChecked(p->verticesSelectionMode == LOCKED);
			radio_handle->setChecked(p->verticesSelectionMode == HANDLE);
		}
		++i;
	}

	plugin->setRefreshingUI(false);
}



714 715 716 717 718
#ifndef DEBUG
Q_EXPORT_PLUGIN2(SurfaceDeformationPlugin, SurfaceDeformationPlugin)
#else
Q_EXPORT_PLUGIN2(SurfaceDeformationPluginD, SurfaceDeformationPlugin)
#endif