Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

surface_deformation.cpp 14.9 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
28
29
30
31
	initialized(false),
	nlContext(NULL)
{}

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

void MapParameters::start(MapHandlerGen* mh)
{
	if(!initialized)
	{
32
		if(positionAttribute.isValid() && handleSelector && freeSelector)
33
34
35
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
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
		{
			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();

			nb_vertices = static_cast<AttribMap*>(map)->computeIndexCells<VERTEX>(vIndex);

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

129
130
131
132
void Surface_Deformation_Plugin::draw(View *view)
{
}

133
134
135
136
137
138
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
139
			if(mh)
140
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
141
142
				const MapParameters& p = h_parameterSet[mh];
				if(!m_dragging)
143
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
144
145
146
147
148
149
150
151
152
153
					if(p.handleSelector && !p.handleSelector->getSelectedCells().empty())
					{
						m_dragging = true;
						m_draginit = false;
						view->setMouseTracking(true);
					}
				}
				else
				{
					m_dragging = false;
154
					m_draginit = false;
Pierre Kraemer's avatar
Pierre Kraemer committed
155
					view->setMouseTracking(false);
156
157
				}
			}
158
159
			break;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
160

161
		case Qt::Key_R : {
Pierre Kraemer's avatar
Pierre Kraemer committed
162
163
164
165
166
167
168
169
170
171
172
173
			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();
				}
			}
174
175
176
177
178
179
180
181
182
183
			break;
		}
	}
}

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

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

188
189
190
191
192
193
194
195
196
197
		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();
198

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

202
203
204
205
206
207
208
209
210
211
212
			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;
213

214
			m_dragPrevious = qq;
215

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

		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&)));
243
	connect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(selectedCellsChanged(CellSelectorGen*)));
244
245
246
247
248
249
250
}

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&)));
251
	disconnect(map, SIGNAL(selectedCellsChanged(CellSelectorGen*)), this, SLOT(selectedCellsChanged(CellSelectorGen*)));
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
}





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];
281
	if(p.handleSelector->getName() == name)
282
283
284
285
	{
		p.stop(map);
		if(!p.initialized && map->isSelectedMap())
			m_dockTab->mapParametersInitialized(false);
286
		p.handleSelector = NULL;
287
	}
288
	if(p.freeSelector->getName() == name)
289
290
291
292
	{
		p.stop(map);
		if(!p.initialized && map->isSelectedMap())
			m_dockTab->mapParametersInitialized(false);
293
		p.freeSelector = NULL;
294
295
296
	}
}

297
void Surface_Deformation_Plugin::selectedCellsChanged(CellSelectorGen* cs)
298
{
299
300
301
302
303
304
305
	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) ;
	}
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
}





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)
		{
			p.positionAttribute = m->getAttribute<PFP2::VEC3, VERTEX>(name);
			if(m->isSelectedMap())
				m_dockTab->updateMapParameters();
		}
	}
}

327
void Surface_Deformation_Plugin::changeHandleSelector(const QString& map, const QString& name)
328
329
330
331
332
333
334
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
		MapParameters& p = h_parameterSet[m];
		if(!p.initialized)
		{
335
			p.handleSelector = m->getCellSelector<VERTEX>(name);
336
337
338
339
340
341
			if(m->isSelectedMap())
				m_dockTab->updateMapParameters();
		}
	}
}

342
void Surface_Deformation_Plugin::changeFreeSelector(const QString& map, const QString& name)
343
344
345
346
347
348
349
{
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(m)
	{
		MapParameters& p = h_parameterSet[m];
		if(!p.initialized)
		{
350
			p.freeSelector = m->getCellSelector<VERTEX>(name);
351
352
353
354
355
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
			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)
	{
391
		LinearSolving::setupVariables<PFP2>(*map, p.vIndex, p.freeSelector->getMarker(), p.positionAttribute, coord);
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
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
		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)
	{
		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 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)
518
			nlBegin(NL_SYSTEM);
519
520
		for(int coord = 0; coord < 3; ++coord)
		{
521
			LinearSolving::setupVariables<PFP2>(*map, p.vIndex, p.freeSelector->getMarker(), p.positionAttribute, coord);
522
523
524
525
526
527
528
529
			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);
530
531
532
533
534
535
536
537
538
539
540
541
542
		}
	}
}

#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