mapHandler.cpp 8.63 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),
15
	m_bbVertexAttribute(NULL),
Pierre Kraemer's avatar
Pierre Kraemer committed
16
	m_bbDrawer(NULL),
17 18
	m_render(NULL),
	m_topoRender(NULL)
19 20
{
	m_frame = new qglviewer::ManipulatedFrame();
Sylvain Thery's avatar
Sylvain Thery committed
21
	connect(m_frame, SIGNAL(manipulated()), this, SLOT(frameModified()));
22
}
Pierre Kraemer's avatar
Pierre Kraemer committed
23 24 25

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

	deleteTopoRender();
Pierre Kraemer's avatar
Pierre Kraemer committed
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 129 130 131 132 133 134 135 136 137 138 139




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

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
140
Utils::Drawer* MapHandlerGen::getBBDrawer() const
141 142 143 144 145 146
{
	return m_bbDrawer;
}



147 148 149 150 151 152 153
/*********************************************************
 * MANAGE ATTRIBUTES
 *********************************************************/

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

	if (m_vbo.contains(nameAttr))
156 157
		m_vbo[nameAttr]->updateData(attr);

158
	if (m_bbVertexAttribute && m_bbVertexAttribute->getName() == attr.name())
159 160
		updateBB();

161
	DEBUG_EMIT("attributeModified");
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	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();
	}

184
	DEBUG_EMIT("connectivityModified");
185 186 187 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	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);
229
				DEBUG_EMIT("attributeRemoved");
230 231 232 233 234 235
				emit(attributeRemoved(orbit, name));
			}
			else
			{
				if (orbit == VERTEX)
					updateVBO(name);
236
				DEBUG_EMIT("attributeModified");
237 238 239 240 241
				emit(attributeModified(orbit, name));
			}
		}
	}

242
	DEBUG_EMIT("connectivityModified");
243 244 245
	emit(connectivityModified());
}

Pierre Kraemer's avatar
Pierre Kraemer committed
246 247 248 249 250 251 252 253
/*********************************************************
 * MANAGE VBOs
 *********************************************************/

Utils::VBO* MapHandlerGen::createVBO(const AttributeMultiVectorGen* attr)
{
	if(attr)
	{
Sylvain Thery's avatar
Sylvain Thery committed
254 255 256 257 258
		// 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
259 260 261 262 263 264 265
		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);
266
			DEBUG_EMIT("vboAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
			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
284
	return createVBO(m_map->getAttributeVectorGen(VERTEX, name.toUtf8().constData()));
Pierre Kraemer's avatar
Pierre Kraemer committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
}

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
304
	updateVBO(m_map->getAttributeVectorGen(VERTEX, name.toUtf8().constData()));
Pierre Kraemer's avatar
Pierre Kraemer committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
}

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);
321
		DEBUG_EMIT("vboRemoved");
Pierre Kraemer's avatar
Pierre Kraemer committed
322 323 324 325 326
		emit(vboRemoved(vbo));
		delete vbo;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
327 328 329 330 331 332 333 334 335 336
/*********************************************************
 * MANAGE CELL SELECTORS
 *********************************************************/

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

		disconnect(cs, SIGNAL(selectedCellsChanged()), this, SIGNAL(selectedCellsChanged()));

Pierre Kraemer's avatar
Pierre Kraemer committed
342 343 344 345 346 347 348 349 350 351 352 353
		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;
}

354 355 356
void MapHandlerGen::selectedCellsChanged()
{
	CellSelectorGen* cs = static_cast<CellSelectorGen*>(QObject::sender());
357
	DEBUG_EMIT("selectedCellsChanged");
358 359 360
	emit(selectedCellsChanged(cs));
}

361 362 363 364 365 366 367 368 369 370 371 372
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);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
/*********************************************************
 * 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);
}

388 389 390 391 392 393 394

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

Sylvain Thery's avatar
Sylvain Thery committed
395

396 397 398 399 400 401 402 403 404 405 406 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;
	foreach(View* view, l_views)
		view->updateGL();
}









Pierre Kraemer's avatar
Pierre Kraemer committed
419 420 421
} // namespace SCHNApps

} // namespace CGoGN