surface_deformation.cpp 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "surface_deformation.h"

#include "Algo/Geometry/normal.h"
#include "Algo/Geometry/laplacian.h"

#include <QKeyEvent>
#include <QMouseEvent>

namespace CGoGN
{

namespace SCHNApps
{

MapParameters::MapParameters() :
	handleSelector(NULL),
17
	freeSelector(NULL),
18 19 20 21 22 23 24 25 26 27
	initialized(false),
	nlContext(NULL)
{}

MapParameters::~MapParameters()
{
	if(nlContext)
		nlDeleteContext(nlContext);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
28
void MapParameters::start(MapHandlerGen* mhg)
29 30 31
{
	if(!initialized)
	{
32
		if(positionAttribute.isValid() && handleSelector && freeSelector)
33
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
34 35
			MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(mhg);

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
			positionInit = mh->getAttribute<PFP2::VEC3, VERTEX>("positionInit", false);
			if(!positionInit.isValid())
				positionInit = mh->addAttribute<PFP2::VEC3, VERTEX>("positionInit", false);

			diffCoord = mh->getAttribute<PFP2::VEC3, VERTEX>("diffCoord", false);
			if(!diffCoord.isValid())
				diffCoord = mh->addAttribute<PFP2::VEC3, VERTEX>("diffCoord", false);

			vertexRotationMatrix = mh->getAttribute<Eigen_Matrix3f, VERTEX>("vertexRotationMatrix", false);
			if(!vertexRotationMatrix.isValid())
				vertexRotationMatrix = mh->addAttribute<Eigen_Matrix3f, VERTEX>("vertexRotationMatrix", false);

			rotatedDiffCoord = mh->getAttribute<PFP2::VEC3, VERTEX>("rotatedDiffCoord", false);
			if(!rotatedDiffCoord.isValid())
				rotatedDiffCoord = mh->addAttribute<PFP2::VEC3, VERTEX>("rotatedDiffCoord", false);

			vIndex = mh->getAttribute<unsigned int, VERTEX>("vIndex", false);
			if(!vIndex.isValid())
				vIndex = mh->addAttribute<unsigned int, VERTEX>("vIndex", false);

			PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(mh)->getMap();

			map->copyAttribute(positionInit, positionAttribute);

			Algo::Surface::Geometry::computeLaplacianTopoVertices<PFP2>(*map, positionAttribute, diffCoord);

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

Pierre Kraemer's avatar
Pierre Kraemer committed
65
			nb_vertices = map->computeIndexCells<VERTEX>(vIndex);
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

			if(nlContext)
				nlDeleteContext(nlContext);
			nlContext = nlNewContext();
			nlSolverParameteri(NL_NB_VARIABLES, nb_vertices);
			nlSolverParameteri(NL_LEAST_SQUARES, NL_TRUE);
			nlSolverParameteri(NL_SOLVER, NL_CHOLMOD_EXT);

			initialized = true;
		}
	}
}

void MapParameters::stop(MapHandlerGen* mh)
{
	if(initialized)
	{
//		if(positionInit.isValid())
//			mh->removeAttribute(positionInit);

//		if(diffCoord.isValid())
//			mh->removeAttribute(diffCoord);

//		if(vertexRotationMatrix.isValid())
//			mh->removeAttribute(vertexRotationMatrix);

//		if(rotatedDiffCoord.isValid())
//			mh->removeAttribute(rotatedDiffCoord);

//		if(vIndex.isValid())
//			mh->removeAttribute(vIndex);

		if(nlContext)
			nlDeleteContext(nlContext);

		initialized = false;
	}
}





bool Surface_Deformation_Plugin::enable()
{
	m_dockTab = new Surface_Deformation_DockTab(m_schnapps, this);
	m_schnapps->addPluginDockTab(this, m_dockTab, "Surface_Deformation");

	connect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)));
	connect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
	connect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));

	foreach(MapHandlerGen* map, m_schnapps->getMapSet().values())
		mapAdded(map);

	m_dockTab->updateMapParameters();

	return true;
}

void Surface_Deformation_Plugin::disable()
{
	disconnect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)));
}

131 132 133 134
void Surface_Deformation_Plugin::draw(View *view)
{
}

135 136 137 138 139 140
void Surface_Deformation_Plugin::keyPress(View* view, QKeyEvent* event)
{
	switch(event->key())
	{
		case Qt::Key_D : {
			MapHandlerGen* mh = m_schnapps->getSelectedMap();
Pierre Kraemer's avatar
Pierre Kraemer committed
141
			if(mh)
142
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144
				const MapParameters& p = h_parameterSet[mh];
				if(!m_dragging)
145
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147 148 149 150 151 152 153 154 155
					if(p.handleSelector && !p.handleSelector->getSelectedCells().empty())
					{
						m_dragging = true;
						m_draginit = false;
						view->setMouseTracking(true);
					}
				}
				else
				{
					m_dragging = false;
156
					m_draginit = false;
Pierre Kraemer's avatar
Pierre Kraemer committed
157
					view->setMouseTracking(false);
158 159
				}
			}
160 161
			break;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
162

163
		case Qt::Key_R : {
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165 166 167 168 169 170 171 172 173 174 175
			MapHandlerGen* mh = m_schnapps->getSelectedMap();
			if(mh)
			{
				const MapParameters& p = h_parameterSet[mh];
				if(p.initialized)
				{
					asRigidAsPossible(mh);
					mh->notifyAttributeModification(p.positionAttribute);
					static_cast<MapHandler<PFP2>*>(mh)->updateBB(p.positionAttribute);
					view->updateGL();
				}
			}
176 177 178 179 180 181 182 183 184 185
			break;
		}
	}
}

void Surface_Deformation_Plugin::mouseMove(View* view, QMouseEvent* event)
{
	if(m_dragging)
	{
		MapHandlerGen* mh = m_schnapps->getSelectedMap();
186

187 188 189
		MapParameters& p = h_parameterSet[mh];
		const std::vector<Dart>& handle = p.handleSelector->getSelectedCells();

190 191 192 193 194 195 196 197 198 199
		if(!m_draginit)
		{
			m_dragZ = 0;
			for(std::vector<Dart>::const_iterator it = handle.begin(); it != handle.end(); ++it)
			{
				const PFP2::VEC3& pp = p.positionAttribute[*it];
				qglviewer::Vec q = view->camera()->projectedCoordinatesOf(qglviewer::Vec(pp[0],pp[1],pp[2]));
				m_dragZ += q.z;
			}
			m_dragZ /= handle.size();
200

201 202
			qglviewer::Vec pp(event->x(), event->y(), m_dragZ);
			m_dragPrevious = view->camera()->unprojectedCoordinatesOf(pp);
203

204 205 206 207 208 209 210 211 212 213 214
			m_draginit = true;
		}
		else
		{
			qglviewer::Vec pp(event->x(), event->y(), m_dragZ);
			qglviewer::Vec qq = view->camera()->unprojectedCoordinatesOf(pp);

			qglviewer::Vec vec = qq - m_dragPrevious;
			PFP2::VEC3 t(vec.x, vec.y, vec.z);
			for(std::vector<Dart>::const_iterator it = handle.begin(); it != handle.end(); ++it)
				p.positionAttribute[*it] += t;
215

216
			m_dragPrevious = qq;
217

218 219
//			matchDiffCoord(map);
			if(p.initialized)
Pierre Kraemer's avatar
Pierre Kraemer committed
220
			{
221
				asRigidAsPossible(mh);
Pierre Kraemer's avatar
Pierre Kraemer committed
222 223 224
				mh->notifyAttributeModification(p.positionAttribute);
				static_cast<MapHandler<PFP2>*>(mh)->updateBB(p.positionAttribute);
			}
225
		}
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

		view->updateGL();
	}
}





void Surface_Deformation_Plugin::selectedMapChanged(MapHandlerGen *prev, MapHandlerGen *cur)
{
	m_dockTab->updateMapParameters();
}

void Surface_Deformation_Plugin::mapAdded(MapHandlerGen* map)
{
	connect(map, SIGNAL(attributeAdded(unsigned int, const QString&)), this, SLOT(attributeAdded(unsigned int, const QString&)));
	connect(map, SIGNAL(cellSelectorAdded(unsigned int, const QString&)), this, SLOT(cellSelectorAdded(unsigned int, const QString&)));
	connect(map, SIGNAL(cellSelectorRemoved(unsigned int, const QString&)), this, SLOT(cellSelectorRemoved(unsigned int, const QString&)));
245
	connect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(selectedCellsChanged(CellSelectorGen*)));
246 247 248 249 250 251 252
}

void Surface_Deformation_Plugin::mapRemoved(MapHandlerGen* map)
{
	disconnect(map, SIGNAL(attributeAdded(unsigned int, const QString&)), this, SLOT(attributeAdded(unsigned int, const QString&)));
	disconnect(map, SIGNAL(cellSelectorAdded(unsigned int, const QString&)), this, SLOT(cellSelectorAdded(unsigned int, const QString&)));
	disconnect(map, SIGNAL(cellSelectorRemoved(unsigned int, const QString&)), this, SLOT(cellSelectorRemoved(unsigned int, const QString&)));
253
	disconnect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(selectedCellsChanged(CellSelectorGen*)));
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
}





void Surface_Deformation_Plugin::attributeAdded(unsigned int orbit, const QString& name)
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());
	if(orbit == VERTEX && map->isSelectedMap())
		m_dockTab->addVertexAttribute(name);
}

void Surface_Deformation_Plugin::cellSelectorAdded(unsigned int orbit, const QString& name)
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());

	if(orbit == VERTEX && map->isSelectedMap())
		m_dockTab->addVertexSelector(name);
}

void Surface_Deformation_Plugin::cellSelectorRemoved(unsigned int orbit, const QString& name)
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());

	if(orbit == VERTEX && map->isSelectedMap())
		m_dockTab->removeVertexSelector(name);

	MapParameters& p = h_parameterSet[map];
283
	if(p.handleSelector->getName() == name)
284 285 286 287
	{
		p.stop(map);
		if(!p.initialized && map->isSelectedMap())
			m_dockTab->mapParametersInitialized(false);
288
		p.handleSelector = NULL;
289
	}
290
	if(p.freeSelector->getName() == name)
291 292 293 294
	{
		p.stop(map);
		if(!p.initialized && map->isSelectedMap())
			m_dockTab->mapParametersInitialized(false);
295
		p.freeSelector = NULL;
296 297 298
	}
}

299
void Surface_Deformation_Plugin::selectedCellsChanged(CellSelectorGen* cs)
300
{
301 302 303 304 305 306 307
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());
	MapParameters& p = h_parameterSet[map];
	if(p.initialized && (p.handleSelector == cs || p.freeSelector == cs))
	{
		nlMakeCurrent(p.nlContext) ;
		nlReset(NL_FALSE) ;
	}
308 309 310 311 312 313 314 315 316 317 318 319 320 321
}





void Surface_Deformation_Plugin::changePositionAttribute(const QString& map, const QString& name)
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
		MapParameters& p = h_parameterSet[m];
		if(!p.initialized)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
322 323
			MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
			p.positionAttribute = mh->getAttribute<PFP2::VEC3, VERTEX>(name);
324 325 326 327 328 329
			if(m->isSelectedMap())
				m_dockTab->updateMapParameters();
		}
	}
}

330
void Surface_Deformation_Plugin::changeHandleSelector(const QString& map, const QString& name)
331 332 333 334 335 336 337
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
		MapParameters& p = h_parameterSet[m];
		if(!p.initialized)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
338 339
			MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
			p.handleSelector = mh->getCellSelector<VERTEX>(name);
340 341 342 343 344 345
			if(m->isSelectedMap())
				m_dockTab->updateMapParameters();
		}
	}
}

346
void Surface_Deformation_Plugin::changeFreeSelector(const QString& map, const QString& name)
347 348 349 350 351 352 353
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
		MapParameters& p = h_parameterSet[m];
		if(!p.initialized)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
354 355
			MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m);
			p.freeSelector = mh->getCellSelector<VERTEX>(name);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
			if(m->isSelectedMap())
				m_dockTab->updateMapParameters();
		}
	}
}





void Surface_Deformation_Plugin::toggleMapDeformation(MapHandlerGen* map)
{
	if(map)
	{
		MapParameters& p = h_parameterSet[map];
		if(!p.initialized)
		{
			p.start(map);
			if(p.initialized && map->isSelectedMap())
				m_dockTab->mapParametersInitialized(true);
		}
		else
		{
			p.stop(map);
			if(!p.initialized && map->isSelectedMap())
				m_dockTab->mapParametersInitialized(false);
		}
	}
}

void Surface_Deformation_Plugin::matchDiffCoord(MapHandlerGen* mh)
{
	PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(mh)->getMap();
	MapParameters& p = h_parameterSet[mh];

	nlMakeCurrent(p.nlContext);
	if(nlGetCurrentState() == NL_STATE_INITIAL)
		nlBegin(NL_SYSTEM) ;
	for(int coord = 0; coord < 3; ++coord)
	{
396
		LinearSolving::setupVariables<PFP2>(*map, p.vIndex, p.freeSelector->getMarker(), p.positionAttribute, coord);
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
		nlBegin(NL_MATRIX);
		LinearSolving::addRowsRHS_Laplacian_Topo<PFP2>(*map, p.vIndex, p.diffCoord, coord);
		nlEnd(NL_MATRIX);
		nlEnd(NL_SYSTEM);
		nlSolve();
		LinearSolving::getResult<PFP2>(*map, p.vIndex, p.positionAttribute, coord);
		nlReset(NL_TRUE);
	}
}

void Surface_Deformation_Plugin::asRigidAsPossible(MapHandlerGen* mh)
{
	PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(mh)->getMap();
	MapParameters& p = h_parameterSet[mh];

	if(p.initialized)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
414
		CellMarkerNoUnmark<PFP2::MAP, VERTEX> m(*map) ;
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 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 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

		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 pp = p.positionAttribute[d] ;
				PFP2::VEC3 ppInit = p.positionInit[d] ;
	//			PFP2::REAL area = p.vertexArea[d] ;
				Dart it = d ;
				do
				{
					Dart neigh = map->phi1(it) ;
					PFP2::VEC3 v = p.positionAttribute[neigh] - pp ;
					PFP2::VEC3 vv = p.positionInit[neigh] - ppInit ;
					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 = p.positionAttribute[dboundary] - pp ;
						vv = p.positionInit[dboundary] - pp ;
						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() ;
				}

				p.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 += p.vertexRotationMatrix[map->phi1(it)] ;
					++degree ;
					Dart dboundary = map->phi_1(it) ;
					if(map->phi2(dboundary) == dboundary)
					{
						r += p.vertexRotationMatrix[dboundary] ;
						++degree ;
					}
					it = map->alpha1(it) ;
				} while(it != d) ;
				r += p.vertexRotationMatrix[d] ;
				r /= degree + 1 ;
				PFP2::VEC3& dc = p.diffCoord[d] ;
				Eigen::Vector3f rdc(dc[0], dc[1], dc[2]) ;
				rdc = r * rdc ;
				p.rotatedDiffCoord[d] = PFP2::VEC3(rdc[0], rdc[1], rdc[2]) ;

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

		nlMakeCurrent(p.nlContext);
		if(nlGetCurrentState() == NL_STATE_INITIAL)
523
			nlBegin(NL_SYSTEM);
524 525
		for(int coord = 0; coord < 3; ++coord)
		{
526
			LinearSolving::setupVariables<PFP2>(*map, p.vIndex, p.freeSelector->getMarker(), p.positionAttribute, coord);
527 528 529 530 531 532 533 534
			nlBegin(NL_MATRIX);
	//		LinearSolving::addRowsRHS_Laplacian_Cotan<PFP2>(*map, p.vIndex, p.edgeWeight, p.vertexArea, p.rotatedDiffCoord, coord);
			LinearSolving::addRowsRHS_Laplacian_Topo<PFP2>(*map, p.vIndex, p.rotatedDiffCoord, coord);
			nlEnd(NL_MATRIX);
			nlEnd(NL_SYSTEM);
			nlSolve();
			LinearSolving::getResult<PFP2>(*map, p.vIndex, p.positionAttribute, coord);
			nlReset(NL_TRUE);
535 536 537 538 539 540 541 542 543 544 545 546 547
		}
	}
}

#ifndef DEBUG
Q_EXPORT_PLUGIN2(Surface_Deformation_Plugin, Surface_Deformation_Plugin)
#else
Q_EXPORT_PLUGIN2(Surface_Deformation_PluginD, Surface_Deformation_Plugin)
#endif

} // namespace SCHNApps

} // namespace CGoGN