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


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 151 152
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
153
Utils::Drawer* MapHandlerGen::getBBDrawer() const
154 155 156 157 158 159
{
	return m_bbDrawer;
}



160 161 162 163 164 165 166
/*********************************************************
 * MANAGE ATTRIBUTES
 *********************************************************/

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

	if (m_vbo.contains(nameAttr))
169 170
		m_vbo[nameAttr]->updateData(attr);

171
	if (m_bbVertexAttribute && m_bbVertexAttribute->getName() == attr.name())
172 173
		updateBB();

174
	DEBUG_EMIT("attributeModified");
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	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();
	}

197
	DEBUG_EMIT("connectivityModified");
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 240 241
	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);
242
				DEBUG_EMIT("attributeRemoved");
243 244 245 246 247 248
				emit(attributeRemoved(orbit, name));
			}
			else
			{
				if (orbit == VERTEX)
					updateVBO(name);
249
				DEBUG_EMIT("attributeModified");
250 251 252 253 254
				emit(attributeModified(orbit, name));
			}
		}
	}

255
	DEBUG_EMIT("connectivityModified");
256 257 258
	emit(connectivityModified());
}

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

Utils::VBO* MapHandlerGen::createVBO(const AttributeMultiVectorGen* attr)
{
	if(attr)
	{
Sylvain Thery's avatar
Sylvain Thery committed
267 268 269 270 271
		// 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
272 273 274 275 276 277 278
		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);
279
			DEBUG_EMIT("vboAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
			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
297
	return createVBO(m_map->getAttributeVectorGen(VERTEX, name.toUtf8().constData()));
Pierre Kraemer's avatar
Pierre Kraemer committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
}

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
317
	updateVBO(m_map->getAttributeVectorGen(VERTEX, name.toUtf8().constData()));
Pierre Kraemer's avatar
Pierre Kraemer committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
}

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);
334
		DEBUG_EMIT("vboRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
335 336 337 338 339
		emit(vboRemoved(vbo));
		delete vbo;
	}
}

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
355 356 357 358 359 360 361 362 363 364 365 366
		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;
}

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

374 375 376 377 378 379 380 381 382 383 384 385
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
386

Pierre Kraemer's avatar
Pierre Kraemer committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
/*********************************************************
 * 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);
}

402 403 404 405 406 407 408

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

Sylvain Thery's avatar
Sylvain Thery committed
409

410 411 412 413 414 415 416 417 418 419 420

/*********************************************************
* 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
421

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


	// 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;

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
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
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
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();
}

475 476 477 478 479





Pierre Kraemer's avatar
Pierre Kraemer committed
480 481 482
} // namespace SCHNApps

} // namespace CGoGN