mapHandler.h 4.25 KB
Newer Older
1 2
#ifndef _MAPHANDLER_H_
#define _MAPHANDLER_H_
3

4
#include <QString>
5

6
#include "types.h"
7
#include "view.h"
8
#include "plugin.h"
9

Pierre Kraemer's avatar
Pierre Kraemer committed
10
#include "Topology/generic/genericmap.h"
11
#include "Topology/generic/attribmap.h"
12
#include "Topology/generic/functor.h"
13
#include "Topology/generic/attributeHandler.h"
14 15
#include "Utils/vbo.h"
#include "Algo/Render/GL2/mapRender.h"
16
#include "Algo/Geometry/boundingbox.h"
17

Pierre Kraemer's avatar
Pierre Kraemer committed
18 19 20 21 22 23
namespace CGoGN
{

namespace SCHNApps
{

24
class MapHandlerGen : public QObject
25
{
26 27
	Q_OBJECT

28
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
29
	MapHandlerGen(const QString& name, Window* window, GenericMap* map);
30 31
	virtual ~MapHandlerGen();

32
	const QString& getName() const { return m_name; }
33 34 35

public slots:
	QString getName() { return m_name; }
36
	void setName(const QString& name) { m_name = name; }
37

38
	Window* getWindow() const { return m_window; }
39 40
	void setWindow(Window* w) { m_window = w; }

Pierre Kraemer's avatar
Pierre Kraemer committed
41
	GenericMap* getGenericMap() { return m_map; }
Pierre Kraemer's avatar
Pierre Kraemer committed
42

43 44
	const qglviewer::Vec& getBBmin() const { return m_bbMin; }
	const qglviewer::Vec& getBBmax() const { return m_bbMax; }
45
	float getBBdiagSize() { return m_bbDiagSize; }
46

Pierre Kraemer's avatar
Pierre Kraemer committed
47 48
	bool isUsed() const { return !l_views.empty(); }

49
public:
50
	void draw(Utils::GLSLShader* shader, int primitive) { m_render->draw(shader, primitive); }
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	/*********************************************************
	 * MANAGE ATTRIBUTES
	 *********************************************************/

	template <typename T, unsigned int ORBIT>
	AttributeHandler<T, ORBIT> getAttribute(const QString& nameAttr)
	{
		AttributeHandler<T,ORBIT> ah = static_cast<AttribMap*>(m_map)->getAttribute<T,ORBIT>(nameAttr.toUtf8().constData());
		return ah;
	}

	template <typename T, unsigned int ORBIT>
	AttributeHandler<T, ORBIT> addAttribute(const QString& nameAttr)
	{
		AttributeHandler<T,ORBIT> ah = static_cast<AttribMap*>(m_map)->addAttribute<T,ORBIT>(nameAttr.toUtf8().constData());
		emit(attributeAdded());
		return ah;
	}

71 72 73 74
	/*********************************************************
	 * MANAGE VBOs
	 *********************************************************/

75 76 77
	template <typename ATTR_HANDLER>
	Utils::VBO* createVBO(const ATTR_HANDLER& attr)
	{
78 79 80 81 82 83 84
		QString name = QString::fromStdString(attr.name());
		Utils::VBO* vbo = getVBO(name);
		if(!vbo)
		{
			vbo = new Utils::VBO();
			h_vbo.insert(name, vbo);
		}
85
		vbo->updateData(attr);
86
		emit(vboAdded(vbo));
87 88 89
		return vbo;
	}

90 91 92 93
	template <typename ATTR_HANDLER>
	void updateVBO(const ATTR_HANDLER& attr)
	{
		Utils::VBO* vbo = getVBO(QString::fromStdString(attr.name()));
94 95
		if(vbo)
			vbo->updateData(attr);
96 97
	}

98 99
	Utils::VBO* getVBO(const QString& name);
	QList<Utils::VBO*> getVBOList() const { return h_vbo.values(); }
100
	QList<Utils::VBO*> getVBOList(const std::string& typeName);
101
	void deleteVBO(const QString& name);
102

103 104 105 106
	/*********************************************************
	 * MANAGE LINKED VIEWS
	 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
107
	void linkView(View* view);
108
	void unlinkView(View* view);
109 110
	const QList<View*>& getLinkedViews() const { return l_views; }
	bool isLinkedToView(View* view) const { return l_views.contains(view); }
111

112
protected:
113
	QString m_name;
114
	Window* m_window;
Pierre Kraemer's avatar
Pierre Kraemer committed
115
	GenericMap* m_map;
116

Pierre Kraemer's avatar
Pierre Kraemer committed
117
	Algo::Render::GL2::MapRender* m_render;
118

119 120
	qglviewer::Vec m_bbMin;
	qglviewer::Vec m_bbMax;
121
	float m_bbDiagSize;
122

123 124
	QList<View*> l_views;

125
	VBOHash h_vbo;
126 127

signals:
128
	void attributeAdded();
129 130
	void vboAdded(Utils::VBO* vbo);
	void vboRemoved(Utils::VBO* vbo);
131
};
132

133 134 135 136 137
template <typename PFP>
class MapHandler : public MapHandlerGen
{
public:
	MapHandler(const QString& name, Window* window, typename PFP::MAP* map) :
Pierre Kraemer's avatar
Pierre Kraemer committed
138
		MapHandlerGen(name, window, map)
139
	{}
140 141 142 143 144 145 146

	~MapHandler()
	{
		if (m_map)
			delete m_map;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
147
	typename PFP::MAP* getMap() { return static_cast<typename PFP::MAP*>(m_map); }
148

149 150
	void updateBB(const VertexAttribute<typename PFP::VEC3>& position)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
151
		CGoGN::Geom::BoundingBox<typename PFP::VEC3> bb = CGoGN::Algo::Geometry::computeBoundingBox<PFP>(*(static_cast<typename PFP::MAP*>(m_map)), position);
152 153 154 155 156
		m_bbMin = qglviewer::Vec(bb.min()[0], bb.min()[1], bb.min()[2]);
		m_bbMax = qglviewer::Vec(bb.max()[0], bb.max()[1], bb.max()[2]);
		m_bbDiagSize = (m_bbMax - m_bbMin).norm();
	}

157
	void updatePrimitives(int primitive, const FunctorSelect& good = allDarts)
158
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
159
		m_render->initPrimitives<PFP>(*(static_cast<typename PFP::MAP*>(m_map)), good, primitive) ;
160
	}
161 162
};

Pierre Kraemer's avatar
Pierre Kraemer committed
163 164 165 166
} // namespace SCHNApps

} // namespace CGoGN

167
#endif