mapHandler.cpp 9.66 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9
#include "mapHandler.h"

namespace CGoGN
{

namespace SCHNApps
{

MapHandlerGen::MapHandlerGen(const QString& name, SCHNApps* s, GenericMap* map) :
10
	m_showBB(true),
Pierre Kraemer's avatar
Pierre Kraemer committed
11 12 13
	m_name(name),
	m_schnapps(s),
	m_map(map),
14
	m_frame(NULL),
Sylvain Thery's avatar
Sylvain Thery committed
15 16
	m_transfoMatrix(1.0f),
	m_transfoMatrixInv(1.0f),
17
	m_bbVertexAttribute(NULL),
Pierre Kraemer's avatar
Pierre Kraemer committed
18
	m_bbDrawer(NULL),
19
	m_bbColor(0,1,0),
20 21
	m_render(NULL),
	m_topoRender(NULL)
22 23
{
	m_frame = new qglviewer::ManipulatedFrame();
Sylvain Thery's avatar
Sylvain Thery committed
24
	connect(m_frame, SIGNAL(manipulated()), this, SLOT(frameModified()));
25
}
Pierre Kraemer's avatar
Pierre Kraemer committed
26 27 28

MapHandlerGen::~MapHandlerGen()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
29 30
	if(m_frame)
		delete m_frame;
Pierre Kraemer's avatar
Pierre Kraemer committed
31 32 33 34 35 36
	if(m_bbDrawer)
		delete m_bbDrawer;
	if(m_render)
		delete m_render;
	foreach(CGoGN::Utils::VBO* vbo, m_vbo)
		delete vbo;
37 38

	deleteTopoRender();
Pierre Kraemer's avatar
Pierre Kraemer committed
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




QString MapHandlerGen::getName()
{
	return m_name;
}

SCHNApps* MapHandlerGen::getSCHNApps() const
{
	return m_schnapps;
}

bool MapHandlerGen::isSelectedMap() const
{
	return m_schnapps->getSelectedMap() == this;
}

GenericMap* MapHandlerGen::getGenericMap() const
{
	return m_map;
}

/*********************************************************
* MANAGE FRAME
*********************************************************/

qglviewer::ManipulatedFrame* MapHandlerGen::getFrame() const
{
	return m_frame;
}

glm::mat4 MapHandlerGen::getFrameMatrix() const
{
	GLdouble m[16];
	m_frame->getMatrix(m);
	glm::mat4 matrix;
	for (unsigned int i = 0; i < 4; ++i)
	{
		for (unsigned int j = 0; j < 4; ++j)
			matrix[i][j] = (float)m[i * 4 + j];
	}
	return matrix;
}

void MapHandlerGen::frameModified()
{
	DEBUG_EMIT("frameModified");
	emit(boundingBoxModified());
}




/*********************************************************
* MANAGE BOUNDING BOX
*********************************************************/

void MapHandlerGen::showBB(bool b)
{
	m_showBB = b;
	foreach(View* view, l_views)
		view->updateGL();
}

bool MapHandlerGen::isBBshown() const
{
	return m_showBB;
}

112 113 114 115 116 117 118 119 120
void  MapHandlerGen::setBBColor(const QString& color)
{
	QColor col(color);
	m_bbColor[0] = col.redF();
	m_bbColor[1] = col.greenF();
	m_bbColor[2] = col.blueF();
	updateBB();
}

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
void MapHandlerGen::setBBVertexAttribute(const QString& name)
{
	m_bbVertexAttribute = m_map->getAttributeVectorGen(VERTEX, name.toStdString());
	updateBB();
	// for update of interface
	if (m_schnapps->getSelectedMap() == this)
	{
		m_schnapps->setSelectedMap("NONE");
		m_schnapps->setSelectedMap(this->getName());
	}
}

AttributeMultiVectorGen* MapHandlerGen::getBBVertexAttribute() const
{
	return m_bbVertexAttribute;
}

QString MapHandlerGen::getBBVertexAttributeName() const
{
	if (m_bbVertexAttribute)
		return QString::fromStdString(m_bbVertexAttribute->getName());
	else
		return QString();
}

float MapHandlerGen::getBBdiagSize() const
{
	return m_bbDiagSize;
}

Sylvain Thery's avatar
Sylvain Thery committed
151
Utils::Drawer* MapHandlerGen::getBBDrawer() const
152 153 154 155 156 157
{
	return m_bbDrawer;
}



158 159 160 161 162 163 164
/*********************************************************
 * MANAGE ATTRIBUTES
 *********************************************************/

void MapHandlerGen::notifyAttributeModification(const AttributeHandlerGen& attr)
{
	QString nameAttr = QString::fromStdString(attr.name());
165 166

	if (m_vbo.contains(nameAttr))
167 168
		m_vbo[nameAttr]->updateData(attr);

169
	if (m_bbVertexAttribute && m_bbVertexAttribute->getName() == attr.name())
170 171
		updateBB();

172
	DEBUG_EMIT("attributeModified");
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	emit(attributeModified(attr.getOrbit(), nameAttr));

	foreach(View* view, l_views)
		view->updateGL();
}

void MapHandlerGen::notifyConnectivityModification()
{
	if (m_render)
	{
		m_render->setPrimitiveDirty(Algo::Render::GL2::POINTS);
		m_render->setPrimitiveDirty(Algo::Render::GL2::LINES);
		m_render->setPrimitiveDirty(Algo::Render::GL2::TRIANGLES);
		m_render->setPrimitiveDirty(Algo::Render::GL2::BOUNDARY);
	}

	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
		foreach (CellSelectorGen* cs, m_cellSelectors[orbit])
			cs->rebuild();
	}

195
	DEBUG_EMIT("connectivityModified");
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	emit(connectivityModified());

	foreach(View* view, l_views)
		view->updateGL();
}

void MapHandlerGen::clear(bool removeAttrib)
{
	if (m_render)
	{
		m_render->setPrimitiveDirty(Algo::Render::GL2::POINTS);
		m_render->setPrimitiveDirty(Algo::Render::GL2::LINES);
		m_render->setPrimitiveDirty(Algo::Render::GL2::TRIANGLES);
		m_render->setPrimitiveDirty(Algo::Render::GL2::BOUNDARY);
	}

	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
		foreach (CellSelectorGen* cs, m_cellSelectors[orbit])
			cs->rebuild();
	}

	std::vector<std::string> attrs[NB_ORBITS];

	for (unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
		if(m_map->isOrbitEmbedded(orbit))
		{
			AttributeContainer& cont = m_map->getAttributeContainer(orbit);
			cont.getAttributesNames(attrs[orbit]);
		}
	}

	m_map->clear(removeAttrib);

	for (unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
		for (unsigned int i = 0; i < attrs[orbit].size(); ++i)
		{
			QString name = QString::fromStdString(attrs[orbit][i]);
			if (removeAttrib)
			{
				if (orbit == VERTEX)
					deleteVBO(name);
240
				DEBUG_EMIT("attributeRemoved");
241 242 243 244 245 246
				emit(attributeRemoved(orbit, name));
			}
			else
			{
				if (orbit == VERTEX)
					updateVBO(name);
247
				DEBUG_EMIT("attributeModified");
248 249 250 251 252
				emit(attributeModified(orbit, name));
			}
		}
	}

253
	DEBUG_EMIT("connectivityModified");
254 255 256
	emit(connectivityModified());
}

Pierre Kraemer's avatar
Pierre Kraemer committed
257 258 259 260 261 262 263 264
/*********************************************************
 * MANAGE VBOs
 *********************************************************/

Utils::VBO* MapHandlerGen::createVBO(const AttributeMultiVectorGen* attr)
{
	if(attr)
	{
Sylvain Thery's avatar
Sylvain Thery committed
265 266 267 268 269
		// RECORDING
		QTextStream* rec = m_schnapps->pythonStreamRecorder();
		if (rec)
			*rec << this->getName() << ".createVBO(\"" << QString(attr->getName().c_str()) << "\");" << endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
270 271 272 273 274 275 276
		QString name = QString::fromStdString(attr->getName());
		Utils::VBO* vbo = getVBO(name);
		if(!vbo)
		{
			vbo = new Utils::VBO(attr->getName());
			vbo->updateData(attr);
			m_vbo.insert(name, vbo);
277
			DEBUG_EMIT("vboAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
			emit(vboAdded(vbo));
		}
		else
			vbo->updateData(attr);
		return vbo;
	}
	else
		return NULL;
}

Utils::VBO* MapHandlerGen::createVBO(const AttributeHandlerGen& attr)
{
	return createVBO(attr.getDataVectorGen());
}

Utils::VBO* MapHandlerGen::createVBO(const QString& name)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
295
	return createVBO(m_map->getAttributeVectorGen(VERTEX, name.toUtf8().constData()));
Pierre Kraemer's avatar
Pierre Kraemer committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
}

void MapHandlerGen::updateVBO(const AttributeMultiVectorGen* attr)
{
	if(attr)
	{
		Utils::VBO* vbo = getVBO(QString::fromStdString(attr->getName()));
		if(vbo)
			vbo->updateData(attr);
	}
}

void MapHandlerGen::updateVBO(const AttributeHandlerGen& attr)
{
	return updateVBO(attr.getDataVectorGen());
}

void MapHandlerGen::updateVBO(const QString& name)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
315
	updateVBO(m_map->getAttributeVectorGen(VERTEX, name.toUtf8().constData()));
Pierre Kraemer's avatar
Pierre Kraemer committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
}

Utils::VBO* MapHandlerGen::getVBO(const QString& name) const
{
	if (m_vbo.contains(name))
		return m_vbo[name];
	else
		return NULL;
}

void MapHandlerGen::deleteVBO(const QString& name)
{
	if (m_vbo.contains(name))
	{
		Utils::VBO* vbo = m_vbo[name];
		m_vbo.remove(name);
332
		DEBUG_EMIT("vboRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
333 334 335 336 337
		emit(vboRemoved(vbo));
		delete vbo;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
338 339 340 341 342 343 344 345 346 347
/*********************************************************
 * MANAGE CELL SELECTORS
 *********************************************************/

void MapHandlerGen::removeCellSelector(unsigned int orbit, const QString& name)
{
	CellSelectorGen* cs = getCellSelector(orbit, name);
	if (cs)
	{
		m_cellSelectors[orbit].remove(name);
348
		DEBUG_EMIT("cellSelectorRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
349
		emit(cellSelectorRemoved(orbit, name));
350

Sylvain Thery's avatar
Sylvain Thery committed
351
		disconnect(cs, SIGNAL(selectedCellsChanged()), this, SLOT(selectedCellsChanged()));
352

Pierre Kraemer's avatar
Pierre Kraemer committed
353 354 355 356 357 358 359 360 361 362 363 364
		delete cs;
	}
}

CellSelectorGen* MapHandlerGen::getCellSelector(unsigned int orbit, const QString& name) const
{
	if (m_cellSelectors[orbit].contains(name))
		return m_cellSelectors[orbit][name];
	else
		return NULL;
}

365 366 367
void MapHandlerGen::selectedCellsChanged()
{
	CellSelectorGen* cs = static_cast<CellSelectorGen*>(QObject::sender());
368
	DEBUG_EMIT("selectedCellsChanged");
369 370 371
	emit(selectedCellsChanged(cs));
}

372 373 374 375 376 377 378 379 380 381 382 383
void MapHandlerGen::updateMutuallyExclusiveSelectors(unsigned int orbit)
{
	QList<CellSelectorGen*> mex;
	foreach(CellSelectorGen* cs, m_cellSelectors[orbit])
	{
		if(cs->isMutuallyExclusive())
			mex.append(cs);
	}
	foreach(CellSelectorGen* cs, m_cellSelectors[orbit])
		cs->setMutuallyExclusiveSet(mex);
}

Sylvain Thery's avatar
Sylvain Thery committed
384

Pierre Kraemer's avatar
Pierre Kraemer committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
/*********************************************************
 * MANAGE LINKED VIEWS
 *********************************************************/

void MapHandlerGen::linkView(View* view)
{
	if(view && !l_views.contains(view))
		l_views.push_back(view);
}

void MapHandlerGen::unlinkView(View* view)
{
	l_views.removeOne(view);
}

400 401 402 403 404 405 406

void MapHandlerGen::deleteTopoRender()
{
	if (m_topoRender)
		delete m_topoRender;
}

Sylvain Thery's avatar
Sylvain Thery committed
407

408 409 410 411 412 413 414 415 416 417 418

/*********************************************************
* MANAGE TRANSFO
*********************************************************/


void MapHandlerGen::setScaling(float sx, float sy, float sz)
{
	m_transfoMatrix[0][0] = sx;
	m_transfoMatrix[1][1] = sy;
	m_transfoMatrix[2][2] = sz;
Sylvain Thery's avatar
Sylvain Thery committed
419

420 421
	foreach(View* view, l_views)
		view->updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
422 423 424 425 426 427 428


	// replace by inverse compute if other transfo than scale
	m_transfoMatrixInv[0][0] = 1.0f/sx;
	m_transfoMatrixInv[1][1] = 1.0f/sy;
	m_transfoMatrixInv[2][2] = 1.0f/sz;

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
QString MapHandlerGen::frameToString()
{
	QString res;
	QTextStream str(&res);
	const GLdouble* mat = m_frame->matrix();
	for (int i = 0; i < 16; ++i)
		str << mat[i] << " ";
	return res;
}

void MapHandlerGen::frameFromString(QString frame)
{
	QTextStream str(&frame);

	GLdouble mat[16];
	for (int i = 0; i < 16; ++i)
		str >> mat[i];

	m_frame->setFromMatrix(mat);

	frameModified();
}

Sylvain Thery's avatar
Sylvain Thery committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
void MapHandlerGen::frameReset()
{
	GLdouble mat[16];
	// Identity
	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < 4; ++j)
			if (i==j)
				mat[i*4+j] = 1;
			else
				mat[i*4+j] = 0;

	m_frame->setFromMatrix(mat);

	frameModified();
}

473 474 475 476 477





Pierre Kraemer's avatar
Pierre Kraemer committed
478 479 480
} // namespace SCHNApps

} // namespace CGoGN