cellmarker.h 9.66 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __CELL_MARKER__
#define __CELL_MARKER__

#include "Topology/generic/marker.h"
Sylvain Thery's avatar
Sylvain Thery committed
29
#include "Topology/generic/genericmap.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30

Sylvain Thery's avatar
Sylvain Thery committed
31
#include "Topology/generic/functor.h"
32
#include "Utils/static_assert.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
33 34 35 36 37

namespace CGoGN
{

/**
38
 * generic class that allows the marking of cells
Pierre Kraemer's avatar
Pierre Kraemer committed
39 40
 * \warning no default constructor
 */
41
class CellMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
42 43
{
protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
44
	Mark m_mark ;
Sylvain Thery's avatar
Sylvain Thery committed
45
	GenericMap& m_map ;
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
	unsigned int m_cell ;
	unsigned int m_thread ;
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51

public:
	/**
	 * constructor
52
	 * @param map the map on which we work
53
	 * @param cell the type of cell we want to mark VERTEX, EDGE,...
Pierre Kraemer's avatar
Pierre Kraemer committed
54
	 */
Sylvain Thery's avatar
Sylvain Thery committed
55
	CellMarkerGen(GenericMap& map, unsigned int cell, unsigned int thread = 0) : m_map(map), m_cell(cell), m_thread(thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
56
	{
57 58
		if(!map.isOrbitEmbedded(cell))
			map.addEmbedding(cell) ;
Sylvain Thery's avatar
Sylvain Thery committed
59
		m_mark = m_map.getMarkerSet(m_cell,m_thread).getNewMark() ;
Sylvain Thery's avatar
Sylvain Thery committed
60 61
	}

62
	virtual ~CellMarkerGen()
Pierre Kraemer's avatar
Pierre Kraemer committed
63
	{
Sylvain Thery's avatar
Sylvain Thery committed
64
		m_map.getMarkerSet(m_cell,m_thread).releaseMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66 67 68
	}

protected:
	// protected copy constructor to forbid its usage
69
	CellMarkerGen(const CellMarkerGen& cm) : m_map(cm.m_map)
Pierre Kraemer's avatar
Pierre Kraemer committed
70 71 72 73 74 75 76 77
	{}

public:
	/**
	 * mark the cell of dart
	 */
	virtual void mark(Dart d)
	{
Sylvain Thery's avatar
Sylvain Thery committed
78 79 80
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		assert(m_map.getMarkVector(m_cell, m_thread) != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
81
		unsigned int a = m_map.getEmbedding(m_cell, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
82
		if (a == EMBNULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
83
			a = m_map.embedNewCell(m_cell, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
84

Pierre Kraemer's avatar
Pierre Kraemer committed
85
		m_map.getMarkVector(m_cell, m_thread)->operator[](a).setMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87 88 89 90 91 92
	}

	/**
	 * unmark the cell of dart
	 */
	virtual void unmark(Dart d)
	{
Sylvain Thery's avatar
Sylvain Thery committed
93 94 95
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		assert(m_map.getMarkVector(m_cell, m_thread) != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
96
		unsigned int a = m_map.getEmbedding(m_cell, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
97
		if (a == EMBNULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
98
			a = m_map.embedNewCell(m_cell, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
99

Pierre Kraemer's avatar
Pierre Kraemer committed
100
		m_map.getMarkVector(m_cell, m_thread)->operator[](a).unsetMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
101 102 103 104 105
	}

	/**
	 * test if cell of dart is marked
	 */
106
	virtual bool isMarked(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
107
	{
Sylvain Thery's avatar
Sylvain Thery committed
108 109 110
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		assert(m_map.getMarkVector(m_cell, m_thread) != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
111
		unsigned int a = m_map.getEmbedding(m_cell, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
112
		if (a == EMBNULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
113
			return false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
114

Pierre Kraemer's avatar
Pierre Kraemer committed
115
		return m_map.getMarkVector(m_cell, m_thread)->operator[](a).testMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
116 117 118 119 120 121 122
	}

	/**
	 * mark the cell
	 */
	virtual void mark(unsigned int em)
	{
Sylvain Thery's avatar
Sylvain Thery committed
123 124 125
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		assert(m_map.getMarkVector(m_cell, m_thread) != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
126
		m_map.getMarkVector(m_cell, m_thread)->operator[](em).setMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128 129 130 131 132 133
	}

	/**
	 * unmark the cell
	 */
	virtual void unmark(unsigned int em)
	{
Sylvain Thery's avatar
Sylvain Thery committed
134 135 136
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		assert(m_map.getMarkVector(m_cell, m_thread) != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
137
		m_map.getMarkVector(m_cell, m_thread)->operator[](em).unsetMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140 141 142
	}

	/**
	 * test if cell is marked
	 */
143
	virtual bool isMarked(unsigned int em) const
Pierre Kraemer's avatar
Pierre Kraemer committed
144
	{
Sylvain Thery's avatar
Sylvain Thery committed
145 146 147
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		assert(m_map.getMarkVector(m_cell, m_thread) != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
148
		return m_map.getMarkVector(m_cell, m_thread)->operator[](em).testMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
149 150
	}

151 152 153
	/**
	 * mark all the cells
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
154 155
	virtual void markAll()
	{
Sylvain Thery's avatar
Sylvain Thery committed
156 157 158 159
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		AttributeMultiVector<Mark>* mark_vect = m_map.getMarkVector(m_cell, m_thread);
		assert(mark_vect != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
160
		AttributeContainer& cont = m_map.getAttributeContainer(m_cell) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
161
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Sylvain Thery's avatar
Sylvain Thery committed
162
			mark_vect->operator[](i).setMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
163 164
	}

165 166 167 168
	/**
	 * unmark all the cells
	 */
	virtual void unmarkAll() = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
169 170 171

	bool isAllUnmarked()
	{
Sylvain Thery's avatar
Sylvain Thery committed
172 173 174 175 176
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));

		AttributeMultiVector<Mark>* mark_vect = m_map.getMarkVector(m_cell, m_thread);
		assert(mark_vect != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
177 178
		AttributeContainer& cont = m_map.getAttributeContainer(m_cell) ;
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Sylvain Thery's avatar
Sylvain Thery committed
179
			if(mark_vect->operator[](i).testMark(m_mark))
Pierre Kraemer's avatar
Pierre Kraemer committed
180 181 182
				return false ;
		return true ;
	}
183 184 185 186 187 188 189 190 191
};

/**
 * class that allows the marking of cells
 * \warning no default constructor
 */
class CellMarker : public CellMarkerGen
{
public:
Sylvain Thery's avatar
Sylvain Thery committed
192
	CellMarker(GenericMap& map, unsigned int cell, unsigned int thread = 0) : CellMarkerGen(map, cell, thread)
193 194 195 196 197 198 199 200 201 202 203 204
	{}

	virtual ~CellMarker()
	{
		unmarkAll() ;
	}

protected:
	CellMarker(const CellMarker& cm) : CellMarkerGen(cm)
	{}

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206
	virtual void unmarkAll()
	{
Sylvain Thery's avatar
Sylvain Thery committed
207 208 209 210
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		AttributeMultiVector<Mark>* mark_vect = m_map.getMarkVector(m_cell, m_thread);
		assert(mark_vect != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
211
		AttributeContainer& cont = m_map.getAttributeContainer(m_cell) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
212
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Sylvain Thery's avatar
Sylvain Thery committed
213
			mark_vect->operator[](i).unsetMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215 216 217 218 219 220 221
	}
};

/**
 * class that allows the marking of cells
 * the marked cells are stored to optimize the unmarking task at destruction
 * \warning no default constructor
 */
222
class CellMarkerStore: public CellMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
223 224 225 226 227
{
protected:
	std::vector<unsigned int> m_markedCells ;

public:
Sylvain Thery's avatar
Sylvain Thery committed
228
	CellMarkerStore(GenericMap& map, unsigned int cell, unsigned int thread = 0) : CellMarkerGen(map, cell, thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
229 230
	{}

231

Pierre Kraemer's avatar
Pierre Kraemer committed
232
	virtual ~CellMarkerStore()
233
	{
234
		unmarkAll() ;
235 236
//		assert(isAllUnmarked);
		CGoGN_ASSERT(isAllUnmarked())
237 238
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
239
protected:
240
	CellMarkerStore(const CellMarkerStore& cm) : CellMarkerGen(cm)
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243 244 245
	{}

public:
	void mark(Dart d)
	{
246
		CellMarkerGen::mark(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
247
		m_markedCells.push_back(m_map.getEmbedding(m_cell, d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
248 249 250 251
	}

	void mark(unsigned int em)
	{
252 253
		CellMarkerGen::mark(em) ;
		m_markedCells.push_back(em) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
254 255 256 257
	}

	void unmarkAll()
	{
Sylvain Thery's avatar
Sylvain Thery committed
258 259 260 261
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		AttributeMultiVector<Mark>* mark_vect = m_map.getMarkVector(m_cell, m_thread);
		assert(mark_vect != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
262
		for (std::vector<unsigned int>::iterator it = m_markedCells.begin(); it != m_markedCells.end(); ++it)
Sylvain Thery's avatar
Sylvain Thery committed
263
			mark_vect->operator[](*it).unsetMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
264 265 266 267 268 269 270 271
	}
};

/**
 * class that allows the marking of cells
 * the markers are not unmarked at destruction
 * \warning no default constructor
 */
272
class CellMarkerNoUnmark: public CellMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
273 274
{
public:
Sylvain Thery's avatar
Sylvain Thery committed
275
	CellMarkerNoUnmark(GenericMap& map, unsigned int cell, unsigned int thread = 0) : CellMarkerGen(map, cell, thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
276
	{}
Sylvain Thery's avatar
Sylvain Thery committed
277

Pierre Kraemer's avatar
Pierre Kraemer committed
278 279
	virtual ~CellMarkerNoUnmark()
	{
280 281
//		assert(isAllUnmarked()) ;
		CGoGN_ASSERT(isAllUnmarked())
Pierre Kraemer's avatar
Pierre Kraemer committed
282
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
283 284

protected:
285
	CellMarkerNoUnmark(const CellMarkerNoUnmark& cm) : CellMarkerGen(cm)
Pierre Kraemer's avatar
Pierre Kraemer committed
286
	{}
287 288 289 290

public:
	void unmarkAll()
	{
Sylvain Thery's avatar
Sylvain Thery committed
291 292 293 294
		assert(m_map.getMarkerSet(m_cell,m_thread).testMark(m_mark));
		AttributeMultiVector<Mark>* mark_vect = m_map.getMarkVector(m_cell, m_thread);
		assert(mark_vect != NULL);

Pierre Kraemer's avatar
Pierre Kraemer committed
295
		AttributeContainer& cont = m_map.getAttributeContainer(m_cell) ;
296
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Sylvain Thery's avatar
Sylvain Thery committed
297
			mark_vect->operator[](i).unsetMark(m_mark) ;
298
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
299 300
};

Sylvain Thery's avatar
Sylvain Thery committed
301 302

/**
303
 * selector that say if a dart has its cell marked
Sylvain Thery's avatar
Sylvain Thery committed
304
 */
305
class SelectorCellMarked : public FunctorSelect
Sylvain Thery's avatar
Sylvain Thery committed
306 307
{
protected:
308
	const CellMarkerGen& m_cmarker ;
Sylvain Thery's avatar
Sylvain Thery committed
309
public:
310
	SelectorCellMarked(const CellMarkerGen& cm) : m_cmarker(cm) {}
Sylvain Thery's avatar
Sylvain Thery committed
311 312 313
	bool operator()(Dart d) const
	{
		if (m_cmarker.isMarked(d))
314 315
			return true ;
		return false ;
Sylvain Thery's avatar
Sylvain Thery committed
316
	}
317
	FunctorSelect* copy() const { return new SelectorCellMarked(m_cmarker);}
Sylvain Thery's avatar
Sylvain Thery committed
318 319
};

320
class SelectorCellUnmarked : public FunctorSelect
321 322
{
protected:
323
	const CellMarkerGen& m_cmarker ;
324
public:
325
	SelectorCellUnmarked(const CellMarkerGen& cm) : m_cmarker(cm) {}
326 327 328
	bool operator()(Dart d) const
	{
		if (!m_cmarker.isMarked(d))
329 330
			return true ;
		return false ;
331
	}
332
	FunctorSelect* copy() const { return new SelectorCellUnmarked(m_cmarker);}
333
};
Sylvain Thery's avatar
Sylvain Thery committed
334

Thomas's avatar
Thomas committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
// Functor version (needed for use with foreach_xxx)

class FunctorCellIsMarked : public FunctorType
{
protected:
	CellMarkerGen& m_marker;
public:
	FunctorCellIsMarked(CellMarkerGen& cm) : m_marker(cm) {}
	bool operator()(Dart d)
	{
		return m_marker.isMarked(d);
	}
};

class FunctorCellIsUnmarked : public FunctorType
{
protected:
	CellMarkerGen& m_marker;
public:
	FunctorCellIsUnmarked(CellMarkerGen& cm) : m_marker(cm) {}
	bool operator()(Dart d)
	{
		return !m_marker.isMarked(d);
	}
};

Pierre Kraemer's avatar
Pierre Kraemer committed
361 362 363
} // namespace CGoGN

#endif