mapHandler.hpp 9.61 KB
Newer Older
1 2
#include "Utils/Shaders/shaderColorPerVertex.h"
#include "Utils/Shaders/shaderSimpleColor.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
3

Sylvain Thery's avatar
Sylvain Thery committed
4 5
#include "Geometry/bounding_box.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
6 7 8 9 10 11
namespace CGoGN
{

namespace SCHNApps
{

Sylvain Thery's avatar
Sylvain Thery committed
12 13


Pierre Kraemer's avatar
Pierre Kraemer committed
14 15 16 17 18
inline void MapHandlerGen::registerAttribute(const AttributeHandlerGen& ah)
{
	m_attribs[ah.getOrbit()].insert(QString::fromStdString(ah.name()), QString::fromStdString(ah.typeName()));
}

19 20 21 22 23
inline void MapHandlerGen::registerAttribute(unsigned int orbit, const QString& name, const QString& typeName)
{
	m_attribs[orbit].insert(name, typeName);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
24 25 26 27 28 29 30 31 32 33 34
inline QString MapHandlerGen::getAttributeTypeName(unsigned int orbit, const QString& nameAttr) const
{
	if(m_attribs[orbit].contains(nameAttr))
		return m_attribs[orbit][nameAttr];
	else
		return "";
}




35

Sylvain Thery's avatar
Sylvain Thery committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
template <typename PFP>
MapHandler<PFP>::~MapHandler()
{
	// clean  the cell selector
	for (unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
		foreach(CellSelectorGen* cs, m_cellSelectors[orbit])
		{
			if (cs)
			{
				emit(cellSelectorRemoved(orbit, cs->getName()));
				disconnect(cs, SIGNAL(selectedCellsChanged()), this, SLOT(selectedCellsChanged()));
				delete cs;
			}
		}
		m_cellSelectors[orbit].clear();
	}

	if (m_map)
		delete m_map;
}


59 60 61 62 63 64 65 66 67 68 69 70
template <typename PFP>
unsigned int MapHandler<PFP>::getNbDarts()
{
	return static_cast<MAP*>(m_map)->getNbDarts();
}

template <typename PFP>
unsigned int MapHandler<PFP>::getNbOrbits(unsigned int orbit)
{
	return Algo::Topo::getNbOrbits(*static_cast<MAP*>(m_map), orbit);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
71
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
72
template <typename T, unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
73
AttributeHandler<T, ORBIT, typename PFP::MAP> MapHandler<PFP>::getAttribute(const QString& nameAttr, bool onlyRegistered) const
Pierre Kraemer's avatar
Pierre Kraemer committed
74 75 76 77
{
	if(onlyRegistered)
	{
		if(m_attribs[ORBIT].contains(nameAttr))
Sylvain Thery's avatar
Sylvain Thery committed
78
			return static_cast<MAP*>(m_map)->template getAttribute<T, ORBIT, MAP>(nameAttr.toStdString());
Pierre Kraemer's avatar
Pierre Kraemer committed
79
		else
Sylvain Thery's avatar
Sylvain Thery committed
80
			return AttributeHandler<T, ORBIT, MAP>();
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82
	}
	else
Sylvain Thery's avatar
Sylvain Thery committed
83
		return static_cast<MAP*>(m_map)->template getAttribute<T, ORBIT, MAP>(nameAttr.toStdString());
Pierre Kraemer's avatar
Pierre Kraemer committed
84 85
}

Pierre Kraemer's avatar
Pierre Kraemer committed
86
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
87
template <typename T, unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
88
AttributeHandler<T, ORBIT, typename PFP::MAP> MapHandler<PFP>::addAttribute(const QString& nameAttr, bool registerAttr)
Pierre Kraemer's avatar
Pierre Kraemer committed
89
{
Sylvain Thery's avatar
Sylvain Thery committed
90
	AttributeHandler<T, ORBIT, MAP> ah = static_cast<MAP*>(m_map)->template addAttribute<T, ORBIT, MAP>(nameAttr.toStdString());
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92 93
	if(ah.isValid() && registerAttr)
	{
		registerAttribute(ah);
94
		DEBUG_EMIT("attributeAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97 98 99 100 101 102 103 104 105 106
		emit(attributeAdded(ORBIT, nameAttr));
	}
	return ah;
}

template <typename PFP>
void MapHandler<PFP>::draw(Utils::GLSLShader* shader, int primitive)
{
	if(!m_render)
		m_render = new Algo::Render::GL2::MapRender();

	if(!m_render->isPrimitiveUpToDate(primitive))
Pierre Kraemer's avatar
Pierre Kraemer committed
107
		m_render->initPrimitives<PFP>(*(static_cast<MAP*>(m_map)), primitive) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
108 109 110 111 112 113 114

	m_render->draw(shader, primitive);
}

template <typename PFP>
void MapHandler<PFP>::drawBB()
{
115
	if (m_showBB && m_bbDrawer)
116
		m_bbDrawer->callList();
Pierre Kraemer's avatar
Pierre Kraemer committed
117 118 119
}

template <typename PFP>
120
void MapHandler<PFP>::updateBB()
Pierre Kraemer's avatar
Pierre Kraemer committed
121
{
122 123 124 125 126 127
	if (m_bbVertexAttribute)
	{
		MAP* map = static_cast<MAP*>(m_map);
		VertexAttribute<VEC3, MAP> bbVertexAttribute(map, dynamic_cast<AttributeMultiVector<VEC3>*>(m_bbVertexAttribute));

		m_bb = CGoGN::Algo::Geometry::computeBoundingBox<PFP>(*map, bbVertexAttribute);
Pierre Kraemer's avatar
Pierre Kraemer committed
128 129 130 131
		if (m_bb.isInitialized())
			m_bbDiagSize = m_bb.diagSize();
		else
			m_bbDiagSize = 0;
132 133 134 135 136 137 138
	}
	else
	{
		m_bb.reset();
		m_bbDiagSize = 0;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
139 140
	updateBBDrawer();

141
	DEBUG_EMIT("boundingBoxModified");
142
	emit(boundingBoxModified());
143
}
Sylvain Thery's avatar
Sylvain Thery committed
144

Pierre Kraemer's avatar
Pierre Kraemer committed
145 146 147
template <typename PFP>
void MapHandler<PFP>::updateBBDrawer()
{
148
	if (!m_bbDrawer)
149
		m_bbDrawer = new Utils::Drawer(1);
Pierre Kraemer's avatar
Pierre Kraemer committed
150

Sylvain Thery's avatar
Sylvain Thery committed
151 152 153 154 155 156 157 158 159
	if (m_bb.isInitialized())
	{
		Geom::Vec3f bbmin = m_bb.min();
		Geom::Vec3f bbmax = m_bb.max();
		float shift = 0.005f * (bbmax - bbmin).norm();
		bbmin -= Geom::Vec3f(shift, shift, shift);
		bbmax += Geom::Vec3f(shift, shift, shift);

		m_bbDrawer->newList(GL_COMPILE);
160
		m_bbDrawer->color3f(m_bbColor[0], m_bbColor[1], m_bbColor[2]);
161 162
		m_bbDrawer->lineWidth(2.0f);
//		m_bbDrawer->lineWidth(shift);
Sylvain Thery's avatar
Sylvain Thery committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		m_bbDrawer->begin(GL_LINE_LOOP);
		m_bbDrawer->vertex(bbmin);
		m_bbDrawer->vertex3f(bbmin[0], bbmax[1], bbmin[2]);
		m_bbDrawer->vertex3f(bbmax[0], bbmax[1], bbmin[2]);
		m_bbDrawer->vertex3f(bbmax[0], bbmin[1], bbmin[2]);
		m_bbDrawer->vertex(bbmin);
		m_bbDrawer->end();
		m_bbDrawer->begin(GL_LINE_LOOP);
		m_bbDrawer->vertex(bbmax);
		m_bbDrawer->vertex3f(bbmax[0], bbmin[1], bbmax[2]);
		m_bbDrawer->vertex3f(bbmin[0], bbmin[1], bbmax[2]);
		m_bbDrawer->vertex3f(bbmin[0], bbmax[1], bbmax[2]);
		m_bbDrawer->vertex(bbmax);
		m_bbDrawer->end();
		m_bbDrawer->begin(GL_LINES);
		m_bbDrawer->vertex(bbmin);
		m_bbDrawer->vertex3f(bbmin[0], bbmin[1], bbmax[2]);
		m_bbDrawer->vertex3f(bbmin[0], bbmax[1], bbmin[2]);
		m_bbDrawer->vertex3f(bbmin[0], bbmax[1], bbmax[2]);
		m_bbDrawer->vertex3f(bbmax[0], bbmax[1], bbmin[2]);
		m_bbDrawer->vertex(bbmax);
		m_bbDrawer->vertex3f(bbmax[0], bbmin[1], bbmin[2]);
		m_bbDrawer->vertex3f(bbmax[0], bbmin[1], bbmax[2]);
		m_bbDrawer->end();
		m_bbDrawer->endList();
	}
	else
	{
		m_bbDrawer->newList();
		m_bbDrawer->endList();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
194 195
}

Pierre Kraemer's avatar
Pierre Kraemer committed
196
template <typename PFP>
197
bool MapHandler<PFP>::transformedBB(qglviewer::Vec& bbMin, qglviewer::Vec& bbMax)
Pierre Kraemer's avatar
Pierre Kraemer committed
198
{
199 200 201
	if (!m_bb.isInitialized())
		return false;

202 203
	const Geom::Vec3f& BBmin = m_bb.min();
	const Geom::Vec3f& BBmax = m_bb.max();
Pierre Kraemer's avatar
Pierre Kraemer committed
204

205
	CGoGN::Geom::BoundingBox<typename PFP::VEC3> bb;
Pierre Kraemer's avatar
Pierre Kraemer committed
206

207 208 209
//	qglviewer::Vec v  = qglviewer::Vec(BBmin[0], BBmin[1], BBmin[2]);
	glm::vec4 v4 = this->m_transfoMatrix* glm::vec4(BBmin[0], BBmin[1], BBmin[2], 1.0f);
	qglviewer::Vec v = qglviewer::Vec(v4[0], v4[1], v4[2]);
210 211
	qglviewer::Vec vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
212

213 214 215
//	v  = qglviewer::Vec(BBmax[0], BBmin[1], BBmin[2]);
	v4 = this->m_transfoMatrix* glm::vec4(BBmax[0], BBmin[1], BBmin[2], 1.0f);
	v = qglviewer::Vec(v4[0], v4[1], v4[2]);
216 217
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
218

219 220 221
//	v  = qglviewer::Vec(BBmin[0], BBmax[1], BBmin[2]);
	v4 = this->m_transfoMatrix* glm::vec4(BBmin[0], BBmax[1], BBmin[2], 1.0f);
	v = qglviewer::Vec(v4[0], v4[1], v4[2]);
222 223
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
224

225 226 227
//	v  = qglviewer::Vec(BBmin[0], BBmin[1], BBmax[2]);
	v4 = this->m_transfoMatrix* glm::vec4(BBmin[0], BBmin[1], BBmax[2], 1.0f);
	v = qglviewer::Vec(v4[0], v4[1], v4[2]);
228 229
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
230

231 232 233
//	v  = qglviewer::Vec(BBmax[0], BBmax[1], BBmin[2]);
	v4 = this->m_transfoMatrix* glm::vec4(BBmax[0], BBmax[1], BBmin[2], 1.0f);
	v = qglviewer::Vec(v4[0], v4[1], v4[2]);
234 235
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
236

237 238 239
//	v  = qglviewer::Vec(BBmax[0], BBmin[1], BBmax[2]);
	v4 = this->m_transfoMatrix* glm::vec4(BBmax[0], BBmin[1], BBmax[2], 1.0f);
	v = qglviewer::Vec(v4[0], v4[1], v4[2]);
240 241 242
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));

243 244 245
//	v  = qglviewer::Vec(BBmin[0], BBmax[1], BBmax[2]);
	v4 = this->m_transfoMatrix* glm::vec4(BBmin[0], BBmax[1], BBmax[2], 1.0f);
	v = qglviewer::Vec(v4[0], v4[1], v4[2]);
246 247 248
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));

249 250 251
//	v  = qglviewer::Vec(BBmax[0], BBmax[1], BBmax[2]);
	v4 = this->m_transfoMatrix* glm::vec4(BBmax[0], BBmax[1], BBmax[2], 1.0f);
	v = qglviewer::Vec(v4[0], v4[1], v4[2]);
252 253 254 255 256
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));

	bbMin = qglviewer::Vec(bb.min()[0], bb.min()[1], bb.min()[2]);
	bbMax = qglviewer::Vec(bb.max()[0], bb.max()[1], bb.max()[2]);
257 258

	return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
259 260
}

261
template <typename PFP>
262
void MapHandler<PFP>::createTopoRender(std::vector<CGoGN::Utils::GLSLShader*> s)
263 264 265 266 267 268 269
{
//	std::cout << "MH:createTopo"<< std::endl;
	if (m_topoRender)
		return;

	if (m_map->dimension() == 2)
	{
270 271
//		CGoGN::Utils::ShaderSimpleColor* ssc = static_cast<CGoGN::Utils::ShaderSimpleColor*>(s);
		m_topoRender = new Algo::Render::GL2::TopoRender(s);
272 273 274 275 276 277 278
		m_topoRender->setInitialDartsColor(0.25f, 0.25f, 0.25f) ;
	}
	else
		std::cerr << "TOPO3 NOT SUPPORTED"<< std::endl;
}

template <typename PFP>
279
void MapHandler<PFP>::updateTopoRender(const QString& positionAttributeName)
280
{
281
	if (!m_topoRender)
282 283 284 285
		return;

	typename PFP::MAP* map = this->getMap();

286
	VertexAttribute<typename PFP::VEC3, typename PFP::MAP> position = map->template getAttribute<VEC3, VERTEX, MAP>(positionAttributeName.toStdString());
287
	if(position.isValid())
288
		m_topoRender->updateData<PFP>(*map, position, false);
289 290 291 292 293
}

template <typename PFP>
void MapHandler<PFP>::drawTopoRender(int code)
{
294
	if (!m_topoRender)
295
		return;
296

297 298 299
	m_topoRender->drawTopo(code);
}

Sylvain Thery's avatar
Sylvain Thery committed
300
template <typename PFP>
301
CellSelectorGen* MapHandler<PFP>::addCellSelector(unsigned int orbit, const QString& name)
Sylvain Thery's avatar
Sylvain Thery committed
302
{
303 304
	if(m_cellSelectors[orbit].contains(name))
		return NULL;
Sylvain Thery's avatar
Sylvain Thery committed
305

306 307
	CellSelectorGen* cs = NULL;
	MAP *m = static_cast<MAP*>(m_map);
Sylvain Thery's avatar
Sylvain Thery committed
308

309 310 311 312 313 314 315 316
	switch(orbit)
	{
		case DART: cs = new CellSelector<MAP, DART>(*m, name); break;
		case VERTEX: cs = new CellSelector<MAP, VERTEX>(*m, name); break;
		case EDGE: cs = new CellSelector<MAP, EDGE>(*m, name); break;
		case FACE: cs = new CellSelector<MAP, FACE>(*m, name); break;
		case VOLUME: cs = new CellSelector<MAP, VOLUME>(*m, name); break;
	}
Sylvain Thery's avatar
Sylvain Thery committed
317

318 319
	if(!cs)
		return NULL;
Sylvain Thery's avatar
Sylvain Thery committed
320

321 322 323
	m_cellSelectors[orbit].insert(name, cs);
	DEBUG_EMIT("cellSelectorAdded");
	emit(cellSelectorAdded(orbit, name));
Sylvain Thery's avatar
Sylvain Thery committed
324

325
	connect(cs, SIGNAL(selectedCellsChanged()), this, SLOT(selectedCellsChanged()));
Sylvain Thery's avatar
Sylvain Thery committed
326

327 328
	return cs;
}
Sylvain Thery's avatar
Sylvain Thery committed
329

330 331 332 333 334 335 336 337
template <typename PFP>
template <unsigned int ORBIT>
CellSelector<typename PFP::MAP, ORBIT>* MapHandler<PFP>::getCellSelector(const QString& name) const
{
	if (m_cellSelectors[ORBIT].contains(name))
		return static_cast<CellSelector<MAP, ORBIT>*>(m_cellSelectors[ORBIT][name]);
	else
		return NULL;
Sylvain Thery's avatar
Sylvain Thery committed
338
}
339

Pierre Kraemer's avatar
Pierre Kraemer committed
340 341 342
} // namespace SCHNApps

} // namespace CGoGN