mapHandler.hpp 8.02 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
	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
103 104 105 106
		if (m_bb.isInitialized())
			m_bbDiagSize = m_bb.diagSize();
		else
			m_bbDiagSize = 0;
107 108 109 110 111 112 113
	}
	else
	{
		m_bb.reset();
		m_bbDiagSize = 0;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
114 115
	updateBBDrawer();

116
	DEBUG_EMIT("boundingBoxModified");
117
	emit(boundingBoxModified());
118
}
Sylvain Thery's avatar
Sylvain Thery committed
119

Pierre Kraemer's avatar
Pierre Kraemer committed
120 121 122
template <typename PFP>
void MapHandler<PFP>::updateBBDrawer()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
123 124 125
	if (!m_bb.isInitialized())
		return;

126
	if (!m_bbDrawer)
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128
		m_bbDrawer = new Utils::Drawer();

Sylvain Thery's avatar
Sylvain Thery committed
129 130
	Geom::Vec3f bbmin = m_bb.min();
	Geom::Vec3f bbmax = m_bb.max();
131 132 133
	float shift = 0.005f * (bbmax - bbmin).norm();
	bbmin -= Geom::Vec3f(shift, shift, shift);
	bbmax += Geom::Vec3f(shift, shift, shift);
Pierre Kraemer's avatar
Pierre Kraemer committed
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

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

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

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

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

176 177 178
	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
179

180 181 182
	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
183

184 185 186
	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
187

188 189 190
	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
191

192 193 194
	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
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209
	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]);
210 211

	return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
212 213
}

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

	if (m_map->dimension() == 2)
	{
223
		CGoGN::Utils::ShaderSimpleColor* ssc = static_cast<CGoGN::Utils::ShaderSimpleColor*>(s);
224 225 226 227 228 229 230 231
		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>
232
void MapHandler<PFP>::updateTopoRender(const QString& positionAttributeName)
233
{
234
	if (!m_topoRender)
235 236 237 238
		return;

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

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

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

250 251 252
	m_topoRender->drawTopo(code);
}

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

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

262 263 264 265 266 267 268 269
	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
270

271 272
	if(!cs)
		return NULL;
Sylvain Thery's avatar
Sylvain Thery committed
273

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

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

280 281
	return cs;
}
Sylvain Thery's avatar
Sylvain Thery committed
282

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

Pierre Kraemer's avatar
Pierre Kraemer committed
293 294 295
} // namespace SCHNApps

} // namespace CGoGN