mapHandler.hpp 7.88 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 17 18 19 20 21 22 23 24 25 26 27
inline void MapHandlerGen::registerAttribute(const AttributeHandlerGen& ah)
{
	m_attribs[ah.getOrbit()].insert(QString::fromStdString(ah.name()), QString::fromStdString(ah.typeName()));
}

inline QString MapHandlerGen::getAttributeTypeName(unsigned int orbit, const QString& nameAttr) const
{
	if(m_attribs[orbit].contains(nameAttr))
		return m_attribs[orbit][nameAttr];
	else
		return "";
}




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

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

	m_render->draw(shader, primitive);
Sylvain Thery's avatar
Sylvain Thery committed
79

Pierre Kraemer's avatar
Pierre Kraemer committed
80 81 82 83 84
}

template <typename PFP>
void MapHandler<PFP>::drawBB()
{
85 86 87 88 89
//	if(!m_bbDrawer)
//	{
//		m_bbDrawer = new Utils::Drawer();
//		updateBBDrawer();
//	}
90

91
//	QGLViewer::drawAxis();
Pierre Kraemer's avatar
Pierre Kraemer committed
92 93 94 95
	m_bbDrawer->callList();
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
96
void MapHandler<PFP>::updateBB(const VertexAttribute<VEC3, MAP>& position)
Pierre Kraemer's avatar
Pierre Kraemer committed
97
{
Pierre Kraemer's avatar
Pierre Kraemer committed
98
	m_bb = CGoGN::Algo::Geometry::computeBoundingBox<PFP>(*(static_cast<MAP*>(m_map)), position);
Sylvain Thery's avatar
Sylvain Thery committed
99
	m_bbDiagSize = m_bb.diagSize();
100 101 102 103 104 105 106

	const typename PFP::VEC3& bmin = m_bb.min();
	m_bbMin = qglviewer::Vec(bmin[0],bmin[1],bmin[2]);

	const typename PFP::VEC3& bmax = m_bb.max();
	m_bbMax = qglviewer::Vec(bmax[0],bmax[1],bmax[2]);

Pierre Kraemer's avatar
Pierre Kraemer committed
107 108 109
	updateBBDrawer();
}

110 111 112 113 114 115
template <typename PFP>
void MapHandler<PFP>::initBBDrawer()
{
	if(!m_bbDrawer)
		m_bbDrawer = new Utils::Drawer();
}
Sylvain Thery's avatar
Sylvain Thery committed
116

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

Sylvain Thery's avatar
Sylvain Thery committed
123 124 125 126 127
	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);
Pierre Kraemer's avatar
Pierre Kraemer committed
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

	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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
template <typename PFP>
CellSelectorGen* MapHandler<PFP>::addCellSelector(unsigned int orbit, const QString& name)
{
	if(m_cellSelectors[orbit].contains(name))
		return NULL;

	CellSelectorGen* cs = NULL;
	MAP *m = static_cast<MAP*>(m_map);

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

	if(!cs)
		return NULL;

	m_cellSelectors[orbit].insert(name, cs);
181
	DEBUG_EMIT("cellSelectorAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	emit(cellSelectorAdded(orbit, name));

	connect(cs, SIGNAL(selectedCellsChanged()), this, SLOT(selectedCellsChanged()));

	return cs;
}

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

199 200 201 202 203 204 205 206 207
template <typename PFP>
void MapHandler<PFP>::createTopoRender(CGoGN::Utils::GLSLShader* sh1)
{
//	std::cout << "MH:createTopo"<< std::endl;
	if (m_topoRender)
		return;

	if (m_map->dimension() == 2)
	{
208
		CGoGN::Utils::ShaderSimpleColor* ssc = static_cast<CGoGN::Utils::ShaderSimpleColor*>(sh1);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

		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>
void MapHandler<PFP>::updateTopoRender(const QString& positionName)
{
	if (m_topoRender==NULL)
		return;

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

Sylvain Thery's avatar
Sylvain Thery committed
225
	VertexAttribute<typename PFP::VEC3, typename PFP::MAP> position = map->template getAttribute<VEC3, VERTEX, MAP>(positionName.toStdString()) ;
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	if(position.isValid())
	{
		m_topoRender->updateData<PFP>(*map,position,false);

	}
}

template <typename PFP>
void MapHandler<PFP>::drawTopoRender(int code)
{
	if (m_topoRender == NULL)
		return;
	m_topoRender->drawTopo(code);
}



Sylvain Thery's avatar
Sylvain Thery committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
template <typename PFP>
void MapHandler<PFP>::transformedBB(qglviewer::Vec& bbMin, qglviewer::Vec& bbMax)
{

	const Geom::Vec3f& BBmin = m_bb.min();
	const Geom::Vec3f& BBMax = m_bb.max();

	CGoGN::Geom::BoundingBox<typename PFP::VEC3> bb;

	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]));

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


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

	v  = qglviewer::Vec(BBmin[0], BBmin[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], BBmin[2]);
	vt = m_frame->inverseCoordinatesOf(v);
	bb.addPoint(Geom::Vec3f(vt[0], vt[1], vt[2]));

	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]);
}
289

Pierre Kraemer's avatar
Pierre Kraemer committed
290 291 292
} // namespace SCHNApps

} // namespace CGoGN