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

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

6 7
#include "Utils/drawer.h"

8 9 10
#include <QKeyEvent>
#include <QMouseEvent>

11 12

PerMapParameterSet::PerMapParameterSet(MapHandlerGen* mh) :
13
	verticesSelectionMode(LOCKED)
14 15
{
	GenericMap* map = mh->getGenericMap();
16 17 18 19

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

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 47 48
				break;
			}
		}
	}
}

49 50 51 52 53 54
PerMapParameterSet::~PerMapParameterSet()
{
	delete lockingMarker;
	delete handleMarker;
}

55

56 57
bool SurfaceDeformationPlugin::enable()
{
58 59 60 61 62
	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)));
63 64
	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)));
65 66 67 68 69

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

70 71 72 73 74 75
	Utils::ShaderColorPerVertex* s = new Utils::ShaderColorPerVertex();
	registerShader(s);

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

76 77 78
	return true;
}

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

136 137 138 139 140
void SurfaceDeformationPlugin::keyPress(View* view, QKeyEvent* event)
{
	if(event->key() == Qt::Key_Shift)
	{
		view->setMouseTracking(true);
141
		selecting = true;
142 143 144 145 146 147 148 149 150
		view->updateGL();
	}
}

void SurfaceDeformationPlugin::keyRelease(View* view, QKeyEvent* event)
{
	if(event->key() == Qt::Key_Shift)
	{
		view->setMouseTracking(false);
151
		selecting = false;
152 153 154 155 156 157
		view->updateGL();
	}
}

void SurfaceDeformationPlugin::mousePress(View* view, QMouseEvent* event)
{
158
	if(event->button() == Qt::LeftButton && selecting)
159 160
	{
		ParameterSet* params = h_viewParams[view];
161
		PerMapParameterSet* perMap = params->getCurrentMapParameterSet();
162 163 164 165

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

168 169
		PFP2::VEC3 rayA(orig.x, orig.y, orig.z);
		PFP2::VEC3 AB(dir.x, dir.y, dir.z);
170 171

		Dart d ;
172 173
		PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(params->selectedMap)->getMap();
		Algo::Selection::vertexRaySelection<PFP2>(*map, perMap->positionAttribute, rayA, AB, d) ;
174 175 176

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

181
			if(perMap->verticesSelectionMode == LOCKED)
182 183 184 185
			{
				for(unsigned int i = 0; i < insideV.size(); ++i)
				{
					unsigned int v = map->getEmbedding<VERTEX>(insideV[i]) ;
186
					if (!perMap->lockingMarker->isMarked(v))
187
					{
188 189
						perMap->locked_vertices.push_back(v) ;
						perMap->lockingMarker->mark(v);
190 191 192 193 194 195 196
					}
				}
			}
			else
			{
				for(unsigned int i = 0; i < insideV.size(); ++i)
				{
197 198
					unsigned int v = map->getEmbedding<VERTEX>(insideV[i]) ;
					if(!perMap->handleMarker->isMarked(v))
199
					{
200 201
						perMap->handle_vertices.push_back(v) ;
						perMap->handleMarker->mark(v);
202
					}
203
					if(!perMap->lockingMarker->isMarked(v))
204
					{
205 206
						perMap->locked_vertices.push_back(v) ;
						perMap->lockingMarker->mark(v) ;
207 208 209 210 211 212
					}
				}
			}
			view->updateGL() ;
		}
	}
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	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);
	}
235 236 237 238 239 240
}

void SurfaceDeformationPlugin::mouseRelease(View* view, QMouseEvent* event)
{
	if(event->button() == Qt::RightButton)
	{
241
		dragging = false ;
242 243 244 245 246 247
	}
}

void SurfaceDeformationPlugin::mouseMove(View* view, QMouseEvent* event)
{
	ParameterSet* params = h_viewParams[view];
248 249 250
	PerMapParameterSet* perMap = params->getCurrentMapParameterSet();

	if(dragging)
251
	{
252 253
		qglviewer::Vec p(event->x(), event->y(), dragZ);
		qglviewer::Vec q = view->camera()->unprojectedCoordinatesOf(p);
254

255 256 257 258
		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 ;
259

260
		dragPrevious = q ;
261 262 263 264 265

//		matchDiffCoord() ;
//		for(unsigned int i = 0; i < 2; ++i)
//			asRigidAsPossible();

266
		params->selectedMap->updateVBO(perMap->positionAttribute);
267 268 269 270 271 272 273 274

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

277 278
		PFP2::VEC3 rayA(orig.x, orig.y, orig.z);
		PFP2::VEC3 AB(dir.x, dir.y, dir.z);
279 280

		Dart d ;
281 282
		PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(params->selectedMap)->getMap();
		Algo::Selection::vertexRaySelection<PFP2>(*map, perMap->positionAttribute, rayA, AB, d) ;
283 284
		if(d != NIL)
		{
285
			selectionCenter = perMap->positionAttribute[d] ;
286 287 288 289 290
			view->updateGL() ;
		}
	}
}

291 292 293 294 295 296 297 298 299 300 301 302
void SurfaceDeformationPlugin::wheelEvent(View* view, QWheelEvent* event)
{
	if(selecting)
	{
		if(event->delta() > 0)
			selectionRadius *= 0.9f ;
		else
			selectionRadius *= 1.1f ;
		view->updateGL() ;
	}
}

303 304 305 306 307 308 309 310 311
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)
		{
312
			PerMapParameterSet* p = new PerMapParameterSet(map);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
			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)
	{
330 331 332 333 334 335 336
		ParameterSet* params = h_viewParams[view];
		QHash<QString, PerMapParameterSet*>::const_iterator i = params->perMap.constBegin();
		while (i != params->perMap.constEnd())
		{
			delete i.value();
			++i;
		}
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
		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];
356
	PerMapParameterSet* p = new PerMapParameterSet(m);
357 358 359 360 361 362 363 364 365 366 367 368 369
	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];
370
	delete params->perMap[m->getName()];
371 372 373 374 375 376 377 378 379 380 381 382 383
	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);
}

384 385 386 387 388 389
void SurfaceDeformationPlugin::attributeAdded()
{
	ParameterSet* params = h_viewParams[m_window->getCurrentView()];
	m_dockTab->refreshUI(params);
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403
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()));

404 405
		selectionRadius = map->getBBdiagSize() / 50.0;

406 407 408 409 410 411 412 413
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void SurfaceDeformationPlugin::changePositionAttribute(View* view, MapHandlerGen* map, VertexAttribute<PFP2::VEC3> attribute)
{
	ParameterSet* params = h_viewParams[view];
414
	params->perMap[map->getName()]->positionAttribute = attribute;
415 416 417 418

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
419 420 421 422 423 424 425 426 427 428 429 430 431
//		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();
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	}
}

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;
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
		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);
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 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


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



550 551 552 553 554
#ifndef DEBUG
Q_EXPORT_PLUGIN2(SurfaceDeformationPlugin, SurfaceDeformationPlugin)
#else
Q_EXPORT_PLUGIN2(SurfaceDeformationPluginD, SurfaceDeformationPlugin)
#endif