cellSelector.h 2.32 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9
#ifndef _CELL_SELECTOR_H_
#define _CELL_SELECTOR_H_

#include <QString>

#include "Topology/generic/dart.h"
#include "Topology/generic/genericmap.h"
#include "Topology/generic/cellmarker.h"

10 11
#include <QObject>

Pierre Kraemer's avatar
Pierre Kraemer committed
12 13 14 15 16 17
namespace CGoGN
{

namespace SCHNApps
{

18
class CellSelectorGen : public QObject
Pierre Kraemer's avatar
Pierre Kraemer committed
19
{
20 21
	Q_OBJECT

Pierre Kraemer's avatar
Pierre Kraemer committed
22 23 24 25 26 27 28 29 30 31 32 33 34
public:
	static unsigned int selectorCount;

	CellSelectorGen(const QString& name);
	virtual ~CellSelectorGen()
	{}

	inline const QString& getName() { return m_name; }

	inline const std::vector<Dart>& getSelectedCells() { return m_cells; }

	virtual unsigned int getOrbit() = 0;

35 36
	virtual void select(Dart d, bool emitSignal) = 0;
	virtual void unselect(Dart d, bool emitSignal) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
37

38 39
	virtual void select(const std::vector<Dart>& d) = 0;
	virtual void unselect(const std::vector<Dart>& d) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
40

41
	virtual bool isSelected(Dart d) = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
42

43 44 45 46
signals:
	void selectedCellsChanged();

protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	QString m_name;
	std::vector<Dart> m_cells;
};

template <unsigned int ORBIT>
class CellSelector : public CellSelectorGen
{
public:
	CellSelector(GenericMap& m, const QString& name, unsigned int thread = 0) :
		CellSelectorGen(name),
		m_map(m),
		m_cm(m, thread)
	{}

	~CellSelector()
	{}

	inline unsigned int getOrbit() { return ORBIT; }

66
	inline void select(Dart d, bool emitSignal = true)
Pierre Kraemer's avatar
Pierre Kraemer committed
67 68 69 70 71 72
	{
		unsigned int v = m_map.getEmbedding<ORBIT>(d);
		if(!m_cm.isMarked(v))
		{
			m_cells.push_back(d);
			m_cm.mark(v);
73 74
			if(emitSignal)
				emit(selectedCellsChanged());
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76 77
		}
	}

78
	inline void unselect(Dart d, bool emitSignal = true)
Pierre Kraemer's avatar
Pierre Kraemer committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	{
		unsigned int v = m_map.getEmbedding<ORBIT>(d);
		if(m_cm.isMarked(v))
		{
			bool found = false;
			unsigned int i;
			for(i = 0; i < m_cells.size() && !found; ++i)
			{
				if(m_map.getEmbedding<ORBIT>(m_cells[i]) == v)
					found = true ;
			}
			if(found)
			{
				m_cm.unmark(m_cells[i-1]);
				m_cells[i-1] = m_cells.back();
				m_cells.pop_back();
95 96
				if(emitSignal)
					emit(selectedCellsChanged());
Pierre Kraemer's avatar
Pierre Kraemer committed
97 98 99 100 101 102 103
			}
		}
	}

	inline void select(const std::vector<Dart>& d)
	{
		for(unsigned int i = 0; i < d.size(); ++i)
104
			select(d[i], false);
Pierre Kraemer's avatar
Pierre Kraemer committed
105 106 107 108 109
	}

	inline void unselect(const std::vector<Dart>& d)
	{
		for(unsigned int i = 0; i < d.size(); ++i)
110
			unselect(d[i], false);
Pierre Kraemer's avatar
Pierre Kraemer committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	}

	inline bool isSelected(Dart d)
	{
		return m_cm.isMarked(d);
	}

private:
	GenericMap& m_map;
	CellMarker<ORBIT> m_cm;
};

} // namespace SCHNApps

} // namespace CGoGN

#endif