surface_selection.cpp 9.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "surface_selection.h"

#include "schnapps.h"
#include "view.h"
#include "mapHandler.h"

#include "Algo/Selection/raySelector.h"
#include "Algo/Selection/collector.h"

#include <QKeyEvent>
#include <QMouseEvent>

namespace CGoGN
{

namespace SCHNApps
{

Surface_Selection_Plugin::Surface_Selection_Plugin() :
	m_selecting(false)
{}

bool Surface_Selection_Plugin::enable()
{
	m_dockTab = new Surface_Selection_DockTab(m_schnapps, this);
	m_schnapps->addPluginDockTab(this, m_dockTab, "Surface_Selection");

28
	m_pointSprite = new CGoGN::Utils::PointSprite();
29
	m_drawer = new Utils::Drawer();
30 31 32

	m_selectedVerticesVBO = new Utils::VBO();

33
	m_selectionSphereVBO = new Utils::VBO();
34 35

	registerShader(m_drawer->getShader());
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	registerShader(m_pointSprite);

	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_Selection_Plugin::disable()
{
	delete m_pointSprite;
53 54
	delete m_selectedVerticesVBO;
	delete m_selectionSphereVBO;
55 56 57 58 59

	disconnect(m_schnapps, SIGNAL(selectedViewChanged(View*, View*)), this, SLOT(selectedViewChanged(View*, View*)));
	disconnect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)));
	disconnect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
	disconnect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));
60 61
}

62
void Surface_Selection_Plugin::draw(View *view)
63 64 65
{
}

66 67
void Surface_Selection_Plugin::drawMap(View* view, MapHandlerGen* map)
{
68
	if(map->isSelectedMap())
69
	{
70
		const MapParameters& p = h_parameterSet[map];
71 72 73 74 75 76 77
		if(p.positionAttribute.isValid())
		{
			unsigned int orbit = m_schnapps->getCurrentOrbit();
			CellSelectorGen* selector = m_schnapps->getSelectedSelector(orbit);
			if(selector)
			{
				const std::vector<Dart>& selectedCells = selector->getSelectedCells();
78
				switch(orbit)
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
					case VERTEX : {
						m_pointSprite->setAttributePosition(m_selectedVerticesVBO);
						m_pointSprite->setSize(map->getBBdiagSize() / 500.0f);
						m_pointSprite->setColor(CGoGN::Geom::Vec4f(1.0f, 0.0f, 0.0f, 1.0f));
						m_pointSprite->setLightPosition(CGoGN::Geom::Vec3f(0.0f, 0.0f, 1.0f));

						m_pointSprite->enableVertexAttribs();
						glEnable(GL_BLEND);
						glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
						glDrawArrays(GL_POINTS, 0, selectedCells.size());
						glDisable(GL_BLEND);
						m_pointSprite->disableVertexAttribs();

						if(m_selecting)
						{
							std::vector<PFP2::VEC3> selectionPoint;
							selectionPoint.push_back(m_selectionCenter);
							m_selectionSphereVBO->updateData(selectionPoint);

							m_pointSprite->setAttributePosition(m_selectionSphereVBO);
							m_pointSprite->setColor(CGoGN::Geom::Vec4f(0.0f, 0.0f, 1.0f, 0.5f));
							m_pointSprite->setLightPosition(CGoGN::Geom::Vec3f(0.0f, 0.0f, 1.0f));

							switch(p.selectionMethod)
							{
								case SingleCell : {
									m_pointSprite->setSize(map->getBBdiagSize() / 250.0f);
									break;
								}
								case WithinSphere : {
									m_pointSprite->setSize(m_selectionRadius);
									break;
								}
							}

							m_pointSprite->enableVertexAttribs();
							glEnable(GL_BLEND);
							glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
							glDrawArrays(GL_POINTS, 0, 1);
							glDisable(GL_BLEND);
							m_pointSprite->disableVertexAttribs();
						}
						break;
					}
					case EDGE : {
						break;
					}
					case FACE : {
						break;
					}
130 131
				}
			}
132
		}
133 134 135
	}
}

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

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

void Surface_Selection_Plugin::mousePress(View* view, QMouseEvent* event)
{
158
	if(m_selecting && (event->button() == Qt::LeftButton || event->button() == Qt::RightButton))
159 160
	{
		MapHandlerGen* mh = m_schnapps->getSelectedMap();
161
		const MapParameters& p = h_parameterSet[mh];
162 163
		if(p.positionAttribute.isValid())
		{
164 165
			unsigned int orbit = m_schnapps->getCurrentOrbit();
			CellSelectorGen* selector = m_schnapps->getSelectedSelector(orbit);
Pierre Kraemer's avatar
Pierre Kraemer committed
166

167 168 169 170 171 172
			if(selector)
			{
				QPoint pixel(event->x(), event->y());
				qglviewer::Vec orig;
				qglviewer::Vec dir;
				view->camera()->convertClickToLine(pixel, orig, dir);
173

174 175
				qglviewer::Vec orig_inv = mh->getFrame()->coordinatesOf(orig);
				qglviewer::Vec dir_inv = mh->getFrame()->transformOf(dir);
176

177 178
				PFP2::VEC3 rayA(orig_inv.x, orig_inv.y, orig_inv.z);
				PFP2::VEC3 AB(dir_inv.x, dir_inv.y, dir_inv.z);
179

180 181
				Dart d;
				PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(mh)->getMap();
182

183
				switch(orbit)
184
				{
185 186 187
					case VERTEX : {
						Algo::Selection::vertexRaySelection<PFP2>(*map, p.positionAttribute, rayA, AB, d);
						if(d != NIL)
188
						{
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
							switch(p.selectionMethod)
							{
								case SingleCell : {
									if(event->button() == Qt::LeftButton)
										selector->select(d);
									else if(event->button() == Qt::RightButton)
										selector->unselect(d);
									break;
								}
								case WithinSphere : {
									Algo::Surface::Selection::Collector_WithinSphere<PFP2> neigh(*map, p.positionAttribute, m_selectionRadius);
									neigh.collectAll(d);
									if(event->button() == Qt::LeftButton)
										selector->select(neigh.getInsideVertices());
									else if(event->button() == Qt::RightButton)
										selector->unselect(neigh.getInsideVertices());
									break;
								}
							}

							const std::vector<Dart>& selectedCells = selector->getSelectedCells();
							std::vector<PFP2::VEC3> selectedPoints;
							for(std::vector<Dart>::const_iterator it = selectedCells.begin(); it != selectedCells.end(); ++it)
								selectedPoints.push_back(p.positionAttribute[*it]);
							m_selectedVerticesVBO->updateData(selectedPoints);
214
						}
215
						break;
216
					}
217 218 219 220 221
					case EDGE : {
						break;
					}
					case FACE : {
						break;
222 223
					}
				}
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
			}
		}
	}
}

void Surface_Selection_Plugin::mouseRelease(View* view, QMouseEvent* event)
{

}

void Surface_Selection_Plugin::mouseMove(View* view, QMouseEvent* event)
{
	if(m_selecting)
	{
		MapHandlerGen* mh = m_schnapps->getSelectedMap();
239
		const MapParameters& p = h_parameterSet[mh];
240 241 242 243 244 245 246
		if(p.positionAttribute.isValid())
		{
			QPoint pixel(event->x(), event->y());
			qglviewer::Vec orig;
			qglviewer::Vec dir;
			view->camera()->convertClickToLine(pixel, orig, dir);

Pierre Kraemer's avatar
Pierre Kraemer committed
247 248 249 250 251
			qglviewer::Vec orig_inv = mh->getFrame()->coordinatesOf(orig);
			qglviewer::Vec dir_inv = mh->getFrame()->transformOf(dir);

			PFP2::VEC3 rayA(orig_inv.x, orig_inv.y, orig_inv.z);
			PFP2::VEC3 AB(dir_inv.x, dir_inv.y, dir_inv.z);
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

			Dart d;
			PFP2::MAP* map = static_cast<MapHandler<PFP2>*>(mh)->getMap();
			Algo::Selection::vertexRaySelection<PFP2>(*map, p.positionAttribute, rayA, AB, d);
			if(d != NIL)
			{
				m_selectionCenter = p.positionAttribute[d];
				view->updateGL();
			}
		}
	}
}

void Surface_Selection_Plugin::wheelEvent(View* view, QWheelEvent* event)
{
	if(m_selecting)
	{
269 270 271 272 273 274 275 276 277 278 279
		MapHandlerGen* mh = m_schnapps->getSelectedMap();
		const MapParameters& p = h_parameterSet[mh];

		if(p.selectionMethod == WithinSphere)
		{
			if(event->delta() > 0)
				m_selectionRadius *= 0.9f;
			else
				m_selectionRadius *= 1.1f;
			view->updateGL();
		}
280 281 282
	}
}

283 284 285 286 287 288 289
void Surface_Selection_Plugin::viewLinked(View *view)
{
	MapHandlerGen* map = m_schnapps->getSelectedMap();
	if(map)
		m_selectionRadius = map->getBBdiagSize() / 50.0f;
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314




void Surface_Selection_Plugin::selectedMapChanged(MapHandlerGen *prev, MapHandlerGen *cur)
{
	m_dockTab->updateMapParameters();
	if(cur)
		m_selectionRadius = cur->getBBdiagSize() / 50.0f;
}

void Surface_Selection_Plugin::mapAdded(MapHandlerGen* map)
{
	connect(map, SIGNAL(attributeAdded(unsigned int, const QString&)), this, SLOT(attributeAdded(unsigned int, const QString&)));
}

void Surface_Selection_Plugin::mapRemoved(MapHandlerGen* map)
{
	disconnect(map, SIGNAL(attributeAdded(unsigned int, const QString&)), this, SLOT(attributeAdded(unsigned int, const QString&)));
}





315
void Surface_Selection_Plugin::attributeAdded(unsigned int orbit, const QString& name)
316 317
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());
318 319 320 321 322 323 324 325 326 327 328 329 330 331
	if(orbit == VERTEX && map->isSelectedMap())
		m_dockTab->addVertexAttribute(name);
}





void Surface_Selection_Plugin::changePositionAttribute(const QString& view, const QString& map, const QString& name)
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
332 333 334 335 336 337 338 339 340 341 342 343 344 345
		h_parameterSet[m].positionAttribute = m->getAttribute<PFP2::VEC3, VERTEX>(name);
		if(m->isSelectedMap())
			m_dockTab->updateMapParameters();
	}
}

void Surface_Selection_Plugin::changeSelectionMethod(const QString& view, const QString& map, unsigned int method)
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
		h_parameterSet[m].selectionMethod = SelectionMethod(method);
		if(m->isSelectedMap())
346 347
			m_dockTab->updateMapParameters();
	}
348 349 350 351 352 353 354 355 356 357 358
}

#ifndef DEBUG
Q_EXPORT_PLUGIN2(Surface_Selection_Plugin, Surface_Selection_Plugin)
#else
Q_EXPORT_PLUGIN2(Surface_Selection_PluginD, Surface_Selection_Plugin)
#endif

} // namespace SCHNApps

} // namespace CGoGN