mapHandler.hpp 8.06 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
{

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

17 18 19 20 21
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
22 23 24 25 26 27 28 29 30 31 32
inline QString MapHandlerGen::getAttributeTypeName(unsigned int orbit, const QString& nameAttr) const
{
	if(m_attribs[orbit].contains(nameAttr))
		return m_attribs[orbit][nameAttr];
	else
		return "";
}




33

34 35 36 37 38 39 40 41 42 43 44 45
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
46
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
47
template <typename T, unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
48
AttributeHandler<T, ORBIT, typename PFP::MAP> MapHandler<PFP>::getAttribute(const QString& nameAttr, bool onlyRegistered) const
Pierre Kraemer's avatar
Pierre Kraemer committed
49 50 51 52
{
	if(onlyRegistered)
	{
		if(m_attribs[ORBIT].contains(nameAttr))
Sylvain Thery's avatar
Sylvain Thery committed
53
			return static_cast<MAP*>(m_map)->template getAttribute<T, ORBIT, MAP>(nameAttr.toStdString());
Pierre Kraemer's avatar
Pierre Kraemer committed
54
		else
Sylvain Thery's avatar
Sylvain Thery committed
55
			return AttributeHandler<T, ORBIT, MAP>();
Pierre Kraemer's avatar
Pierre Kraemer committed
56 57
	}
	else
Sylvain Thery's avatar
Sylvain Thery committed
58
		return static_cast<MAP*>(m_map)->template getAttribute<T, ORBIT, MAP>(nameAttr.toStdString());
Pierre Kraemer's avatar
Pierre Kraemer committed
59 60
}

Pierre Kraemer's avatar
Pierre Kraemer committed
61
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
62
template <typename T, unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
63
AttributeHandler<T, ORBIT, typename PFP::MAP> MapHandler<PFP>::addAttribute(const QString& nameAttr, bool registerAttr)
Pierre Kraemer's avatar
Pierre Kraemer committed
64
{
Sylvain Thery's avatar
Sylvain Thery committed
65
	AttributeHandler<T, ORBIT, MAP> ah = static_cast<MAP*>(m_map)->template addAttribute<T, ORBIT, MAP>(nameAttr.toStdString());
Pierre Kraemer's avatar
Pierre Kraemer committed
66 67 68
	if(ah.isValid() && registerAttr)
	{
		registerAttribute(ah);
69
		DEBUG_EMIT("attributeAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
70 71 72 73 74 75 76 77 78 79 80 81
		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
82
		m_render->initPrimitives<PFP>(*(static_cast<MAP*>(m_map)), primitive) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
83 84 85 86 87 88 89

	m_render->draw(shader, primitive);
}

template <typename PFP>
void MapHandler<PFP>::drawBB()
{
90 91
	if (m_bbDrawer)
		m_bbDrawer->callList();
Pierre Kraemer's avatar
Pierre Kraemer committed
92 93 94
}

template <typename PFP>
95
void MapHandler<PFP>::updateBB()
Pierre Kraemer's avatar
Pierre Kraemer committed
96
{
97 98 99 100 101 102 103 104 105 106 107 108 109 110
	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);
		m_bbDiagSize = m_bb.diagSize();
	}
	else
	{
		m_bb.reset();
		m_bbDiagSize = 0;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
111 112
	updateBBDrawer();

113
	DEBUG_EMIT("boundingBoxModified");
114
	emit(boundingBoxModified());
115
}
Sylvain Thery's avatar
Sylvain Thery committed
116

Pierre Kraemer's avatar
Pierre Kraemer committed
117 118 119
template <typename PFP>
void MapHandler<PFP>::updateBBDrawer()
{
120
	if (!m_bbDrawer)
Pierre Kraemer's avatar
Pierre Kraemer committed
121 122
		m_bbDrawer = new Utils::Drawer();

Sylvain Thery's avatar
Sylvain Thery committed
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 153 154 155 156 157 158 159 160 161 162 163 164
	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);
		m_bbDrawer->color3f(0.0f, 1.0f, 0.0f);
		m_bbDrawer->lineWidth(1.0f);
		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
165 166
}

Pierre Kraemer's avatar
Pierre Kraemer committed
167
template <typename PFP>
168
bool MapHandler<PFP>::transformedBB(qglviewer::Vec& bbMin, qglviewer::Vec& bbMax)
Pierre Kraemer's avatar
Pierre Kraemer committed
169
{
170 171 172
	if (!m_bb.isInitialized())
		return false;

173 174
	const Geom::Vec3f& BBmin = m_bb.min();
	const Geom::Vec3f& BBmax = m_bb.max();
Pierre Kraemer's avatar
Pierre Kraemer committed
175

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

178 179 180
	qglviewer::Vec v  = qglviewer::Vec(BBmin[0], BBmin[1], BBmin[2]);
	qglviewer::Vec vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
181

182 183 184
	v  = qglviewer::Vec(BBmax[0], BBmin[1], BBmin[2]);
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
185

186 187 188
	v  = qglviewer::Vec(BBmin[0], BBmax[1], BBmin[2]);
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
189

190 191 192
	v  = qglviewer::Vec(BBmin[0], BBmin[1], BBmax[2]);
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
193

194 195 196
	v  = qglviewer::Vec(BBmax[0], BBmax[1], BBmin[2]);
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));
Pierre Kraemer's avatar
Pierre Kraemer committed
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211
	v  = qglviewer::Vec(BBmax[0], BBmin[1], BBmax[2]);
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));

	v  = qglviewer::Vec(BBmin[0], BBmax[1], BBmax[2]);
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));

	v  = qglviewer::Vec(BBmax[0], BBmax[1], BBmax[2]);
	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]);
212 213

	return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215
}

216
template <typename PFP>
217
void MapHandler<PFP>::createTopoRender(CGoGN::Utils::GLSLShader* s)
218 219 220 221 222 223 224
{
//	std::cout << "MH:createTopo"<< std::endl;
	if (m_topoRender)
		return;

	if (m_map->dimension() == 2)
	{
225
		CGoGN::Utils::ShaderSimpleColor* ssc = static_cast<CGoGN::Utils::ShaderSimpleColor*>(s);
226 227 228 229 230 231 232 233
		m_topoRender = new Algo::Render::GL2::TopoRender(ssc);
		m_topoRender->setInitialDartsColor(0.25f, 0.25f, 0.25f) ;
	}
	else
		std::cerr << "TOPO3 NOT SUPPORTED"<< std::endl;
}

template <typename PFP>
234
void MapHandler<PFP>::updateTopoRender(const QString& positionAttributeName)
235
{
236
	if (!m_topoRender)
237 238 239 240
		return;

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

241
	VertexAttribute<typename PFP::VEC3, typename PFP::MAP> position = map->template getAttribute<VEC3, VERTEX, MAP>(positionAttributeName.toStdString());
242
	if(position.isValid())
243
		m_topoRender->updateData<PFP>(*map, position, false);
244 245 246 247 248
}

template <typename PFP>
void MapHandler<PFP>::drawTopoRender(int code)
{
249
	if (!m_topoRender)
250
		return;
251

252 253 254
	m_topoRender->drawTopo(code);
}

Sylvain Thery's avatar
Sylvain Thery committed
255
template <typename PFP>
256
CellSelectorGen* MapHandler<PFP>::addCellSelector(unsigned int orbit, const QString& name)
Sylvain Thery's avatar
Sylvain Thery committed
257
{
258 259
	if(m_cellSelectors[orbit].contains(name))
		return NULL;
Sylvain Thery's avatar
Sylvain Thery committed
260

261 262
	CellSelectorGen* cs = NULL;
	MAP *m = static_cast<MAP*>(m_map);
Sylvain Thery's avatar
Sylvain Thery committed
263

264 265 266 267 268 269 270 271
	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
272

273 274
	if(!cs)
		return NULL;
Sylvain Thery's avatar
Sylvain Thery committed
275

276 277 278
	m_cellSelectors[orbit].insert(name, cs);
	DEBUG_EMIT("cellSelectorAdded");
	emit(cellSelectorAdded(orbit, name));
Sylvain Thery's avatar
Sylvain Thery committed
279

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

282 283
	return cs;
}
Sylvain Thery's avatar
Sylvain Thery committed
284

285 286 287 288 289 290 291 292
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
293
}
294

Pierre Kraemer's avatar
Pierre Kraemer committed
295 296 297
} // namespace SCHNApps

} // namespace CGoGN