mapHandler.h 3.48 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/functor.h"
12
#include "Topology/generic/attributeHandler.h"
13 14
#include "Utils/vbo.h"
#include "Algo/Render/GL2/mapRender.h"
15
#include "Algo/Geometry/boundingbox.h"
16

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

namespace SCHNApps
{

23
class MapHandlerGen : public QObject
24
{
25 26
	Q_OBJECT

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

31
	const QString& getName() const { return m_name; }
32
	void setName(const QString& name) { m_name = name; }
33

34
	Window* getWindow() const { return m_window; }
35 36
	void setWindow(Window* w) { m_window = w; }

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

39 40
	const qglviewer::Vec& getBBmin() const { return m_bbMin; }
	const qglviewer::Vec& getBBmax() const { return m_bbMax; }
41
	float getBBdiagSize() { return m_bbDiagSize; }
42

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

45
	void draw(Utils::GLSLShader* shader, int primitive) { m_render->draw(shader, primitive); }
46

47 48 49 50
	/*********************************************************
	 * MANAGE VBOs
	 *********************************************************/

51 52 53
	template <typename ATTR_HANDLER>
	Utils::VBO* createVBO(const ATTR_HANDLER& attr)
	{
54 55 56 57 58 59 60
		QString name = QString::fromStdString(attr.name());
		Utils::VBO* vbo = getVBO(name);
		if(!vbo)
		{
			vbo = new Utils::VBO();
			h_vbo.insert(name, vbo);
		}
61
		vbo->updateData(attr);
62
		emit(vboAdded(vbo));
63 64 65
		return vbo;
	}

66 67 68 69
	template <typename ATTR_HANDLER>
	void updateVBO(const ATTR_HANDLER& attr)
	{
		Utils::VBO* vbo = getVBO(QString::fromStdString(attr.name()));
70 71
		if(vbo)
			vbo->updateData(attr);
72 73
	}

74 75
	Utils::VBO* getVBO(const QString& name);
	QList<Utils::VBO*> getVBOList() const { return h_vbo.values(); }
76
	QList<Utils::VBO*> getVBOList(const std::string& typeName);
77
	void deleteVBO(const QString& name);
78

79 80 81 82
	/*********************************************************
	 * MANAGE LINKED VIEWS
	 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
83
	void linkView(View* view);
84
	void unlinkView(View* view);
85 86
	const QList<View*>& getLinkedViews() const { return l_views; }
	bool isLinkedToView(View* view) const { return l_views.contains(view); }
87

88
protected:
89
	QString m_name;
90
	Window* m_window;
Pierre Kraemer's avatar
Pierre Kraemer committed
91
	GenericMap* m_map;
92

Pierre Kraemer's avatar
Pierre Kraemer committed
93
	Algo::Render::GL2::MapRender* m_render;
94

95 96
	qglviewer::Vec m_bbMin;
	qglviewer::Vec m_bbMax;
97
	float m_bbDiagSize;
98

99 100
	QList<View*> l_views;

101
	VBOHash h_vbo;
102 103 104 105

signals:
	void vboAdded(Utils::VBO* vbo);
	void vboRemoved(Utils::VBO* vbo);
106
};
107

108 109 110 111 112
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
113
		MapHandlerGen(name, window, map)
114
	{}
115 116 117 118 119 120 121

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

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

124 125
	void updateBB(const VertexAttribute<typename PFP::VEC3>& position)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
126
		CGoGN::Geom::BoundingBox<typename PFP::VEC3> bb = CGoGN::Algo::Geometry::computeBoundingBox<PFP>(*(static_cast<typename PFP::MAP*>(m_map)), position);
127 128 129 130 131
		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();
	}

132
	void updatePrimitives(int primitive, const FunctorSelect& good = allDarts)
133
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
134
		m_render->initPrimitives<PFP>(*(static_cast<typename PFP::MAP*>(m_map)), good, primitive) ;
135
	}
136 137
};

Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140 141
} // namespace SCHNApps

} // namespace CGoGN

142
#endif