mapHandler.hpp 6.19 KB
Newer Older
1 2
#include "Utils/Shaders/shaderColorPerVertex.h"
#include "Utils/Shaders/shaderSimpleColor.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
3 4 5 6 7 8 9

namespace CGoGN
{

namespace SCHNApps
{

Pierre Kraemer's avatar
Pierre Kraemer committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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 "";
}




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

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

76 77
	glPushMatrix();
	glMultMatrixd(m_frame->matrix());
Pierre Kraemer's avatar
Pierre Kraemer committed
78
	m_render->draw(shader, primitive);
79
	glPopMatrix();
Pierre Kraemer's avatar
Pierre Kraemer committed
80 81 82 83 84 85 86 87 88 89
}

template <typename PFP>
void MapHandler<PFP>::drawBB()
{
	if(!m_bbDrawer)
	{
		m_bbDrawer = new Utils::Drawer();
		updateBBDrawer();
	}
90 91 92

	glPushMatrix();
	glMultMatrixd(m_frame->matrix());
93
//	QGLViewer::drawAxis();
Pierre Kraemer's avatar
Pierre Kraemer committed
94
	m_bbDrawer->callList();
95
	glPopMatrix();
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97 98
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
99
void MapHandler<PFP>::updateBB(const VertexAttribute<VEC3, MAP>& position)
Pierre Kraemer's avatar
Pierre Kraemer committed
100
{
Pierre Kraemer's avatar
Pierre Kraemer committed
101
	m_bb = CGoGN::Algo::Geometry::computeBoundingBox<PFP>(*(static_cast<MAP*>(m_map)), position);
Pierre Kraemer's avatar
Pierre Kraemer committed
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 140 141 142 143 144 145 146 147
	m_bbMin = qglviewer::Vec(m_bb.min()[0], m_bb.min()[1], m_bb.min()[2]);
	m_bbMax = qglviewer::Vec(m_bb.max()[0], m_bb.max()[1], m_bb.max()[2]);
	m_bbDiagSize = (m_bbMax - m_bbMin).norm();

	updateBBDrawer();
}

template <typename PFP>
void MapHandler<PFP>::updateBBDrawer()
{
	if(!m_bbDrawer)
		m_bbDrawer = new Utils::Drawer();

	const Geom::Vec3f& bbmin = m_bb.min();
	const Geom::Vec3f& bbmax = m_bb.max();

	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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
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);
170
	DEBUG_EMIT("cellSelectorAdded");
Pierre Kraemer's avatar
Pierre Kraemer committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	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;
}

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 229 230 231 232
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)
	{
		CGoGN::Utils::ShaderSimpleColor* ssc =static_cast<CGoGN::Utils::ShaderSimpleColor*>(sh1);

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

	VertexAttribute<typename PFP::VEC3, typename PFP::MAP> position = map->getAttribute<VEC3, VERTEX, MAP>(positionName.toStdString()) ;
	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);
}




Pierre Kraemer's avatar
Pierre Kraemer committed
233 234 235
} // namespace SCHNApps

} // namespace CGoGN