Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

cellmarker.h 10.4 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-2012, 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.unistra.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"
Sylvain Thery's avatar
Sylvain Thery committed
30
#include "Topology/generic/functor.h"
31
#include "Utils/static_assert.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
32
33
34
35

namespace CGoGN
{

36
class CellMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
37
{
38
39
	friend class GenericMap ;

Pierre Kraemer's avatar
Pierre Kraemer committed
40
protected:
Sylvain Thery's avatar
Sylvain Thery committed
41
	GenericMap& m_map ;
42
	Mark m_mark ;
Pierre Kraemer's avatar
Pierre Kraemer committed
43
	unsigned int m_thread ;
44
	bool releaseOnDestruct ;
Pierre Kraemer's avatar
Pierre Kraemer committed
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public:
	CellMarkerGen(GenericMap& map, unsigned int thread = 0) : m_map(map), m_thread(thread), releaseOnDestruct(true)
	{}

	virtual ~CellMarkerGen()
	{}

	/**
	 * set if the mark has to be release on destruction or not
	 */
	void setReleaseOnDestruct(bool b) { releaseOnDestruct = b ; }

	virtual void mark(Dart d) = 0 ;
	virtual void unmark(Dart d) = 0 ;
	virtual bool isMarked(Dart d) const = 0 ;
	virtual void mark(unsigned int em) = 0 ;
	virtual void unmark(unsigned int em) = 0 ;
	virtual bool isMarked(unsigned int em) const = 0 ;
	virtual void markAll() = 0 ;
	virtual void unmarkAll() = 0 ;
	virtual bool isAllUnmarked() = 0 ;
};

/**
 * generic class that allows the marking of cells
 * \warning no default constructor
 */
template <unsigned int CELL>
class CellMarkerBase : public CellMarkerGen
{
protected:
	AttributeMultiVector<Mark>* m_markVector ;

Pierre Kraemer's avatar
Pierre Kraemer committed
79
80
81
public:
	/**
	 * constructor
82
	 * @param map the map on which we work
Pierre Kraemer's avatar
Pierre Kraemer committed
83
	 */
84
	CellMarkerBase(GenericMap& map, unsigned int thread = 0) : CellMarkerGen(map, thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
85
	{
86
87
		if(!map.isOrbitEmbedded<CELL>())
			map.addEmbedding<CELL>() ;
88
89
		m_mark = m_map.getMarkerSet<CELL>(m_thread).getNewMark() ;
		m_markVector = m_map.getMarkVector<CELL>(m_thread) ;
90
		m_map.cellMarkers.push_back(this) ;
Sylvain Thery's avatar
Sylvain Thery committed
91
92
	}

93
	virtual ~CellMarkerBase()
Pierre Kraemer's avatar
Pierre Kraemer committed
94
	{
95
96
		if(releaseOnDestruct)
		{
97
			m_map.getMarkerSet<CELL>(m_thread).releaseMark(m_mark) ;
98
99
100
101
102
103
104
105
106
107
			for(std::vector<CellMarkerGen*>::iterator it = m_map.cellMarkers.begin(); it != m_map.cellMarkers.end(); ++it)
			{
				if(*it == this)
				{
					*it = m_map.cellMarkers.back();
					m_map.cellMarkers.pop_back();
					return;
				}
			}
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
108
109
110
111
	}

protected:
	// protected copy constructor to forbid its usage
112
	CellMarkerBase(const CellMarkerGen& cm) : CellMarkerGen(cm.m_map)
Pierre Kraemer's avatar
Pierre Kraemer committed
113
114
115
116
117
118
119
120
	{}

public:
	/**
	 * mark the cell of dart
	 */
	virtual void mark(Dart d)
	{
121
122
		assert(m_map.getMarkerSet<CELL>(m_thread).testMark(m_mark));
		assert(m_map.getMarkVector<CELL>(m_thread) != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
123

124
		unsigned int a = m_map.getEmbedding<CELL>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
125
		if (a == EMBNULL)
126
			a = m_map.embedNewCell<CELL>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
127

128
		m_markVector->operator[](a).setMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
129
130
131
132
133
134
135
	}

	/**
	 * unmark the cell of dart
	 */
	virtual void unmark(Dart d)
	{
136
137
		assert(m_map.getMarkerSet<CELL>(m_thread).testMark(m_mark));
		assert(m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
138

139
		unsigned int a = m_map.getEmbedding<CELL>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
140
		if (a == EMBNULL)
141
			a = m_map.embedNewCell<CELL>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
142

143
		m_markVector->operator[](a).unsetMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
144
145
146
147
148
	}

	/**
	 * test if cell of dart is marked
	 */
149
	virtual bool isMarked(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
150
	{
151
152
		assert(m_map.getMarkerSet<CELL>(m_thread).testMark(m_mark));
		assert(m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
153

154
		unsigned int a = m_map.getEmbedding<CELL>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
155
		if (a == EMBNULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
156
			return false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
157

158
		return m_markVector->operator[](a).testMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
159
160
161
162
163
164
165
	}

	/**
	 * mark the cell
	 */
	virtual void mark(unsigned int em)
	{
166
167
		assert(m_map.getMarkerSet<CELL>(m_thread).testMark(m_mark));
		assert(m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
168

169
		m_markVector->operator[](em).setMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
170
171
172
173
174
175
176
	}

	/**
	 * unmark the cell
	 */
	virtual void unmark(unsigned int em)
	{
177
178
		assert(m_map.getMarkerSet<CELL>(m_thread).testMark(m_mark));
		assert(m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
179

180
		m_markVector->operator[](em).unsetMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
181
182
183
184
185
	}

	/**
	 * test if cell is marked
	 */
186
	virtual bool isMarked(unsigned int em) const
Pierre Kraemer's avatar
Pierre Kraemer committed
187
	{
188
189
		assert(m_map.getMarkerSet<CELL>(m_thread).testMark(m_mark));
		assert(m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
190

191
		return m_markVector->operator[](em).testMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
192
193
	}

194
195
196
	/**
	 * mark all the cells
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
197
198
	virtual void markAll()
	{
199
200
		assert(m_map.getMarkerSet<CELL>(m_thread).testMark(m_mark));
		assert(m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
201

202
		AttributeContainer& cont = m_map.getAttributeContainer<CELL>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
203
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
204
			m_markVector->operator[](i).setMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
205
206
	}

207
	virtual bool isAllUnmarked()
Pierre Kraemer's avatar
Pierre Kraemer committed
208
	{
209
210
		assert(m_map.getMarkerSet<CELL>(m_thread).testMark(m_mark));
		assert(m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
211

212
		AttributeContainer& cont = m_map.getAttributeContainer<CELL>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
213
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
214
			if(m_markVector->operator[](i).testMark(m_mark))
Pierre Kraemer's avatar
Pierre Kraemer committed
215
216
217
				return false ;
		return true ;
	}
218
219
220
221
222
223
};

/**
 * class that allows the marking of cells
 * \warning no default constructor
 */
224
225
template <unsigned int CELL>
class CellMarker : public CellMarkerBase<CELL>
226
227
{
public:
228
	CellMarker(GenericMap& map, unsigned int thread = 0) : CellMarkerBase<CELL>(map, thread)
229
230
231
232
233
234
235
236
	{}

	virtual ~CellMarker()
	{
		unmarkAll() ;
	}

protected:
237
	CellMarker(const CellMarker& cm) : CellMarkerBase<CELL>(cm)
238
239
240
	{}

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
241
242
	virtual void unmarkAll()
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
243
244
		assert(this->m_map.template getMarkerSet<CELL>(this->m_thread).testMark(this->m_mark));
		assert(this->m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
245

246
		AttributeContainer& cont = this->m_map.template getAttributeContainer<CELL>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
247
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
248
			this->m_markVector->operator[](i).unsetMark(this->m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
249
250
251
252
253
254
255
256
	}
};

/**
 * class that allows the marking of cells
 * the marked cells are stored to optimize the unmarking task at destruction
 * \warning no default constructor
 */
257
258
template <unsigned int CELL>
class CellMarkerStore: public CellMarkerBase<CELL>
Pierre Kraemer's avatar
Pierre Kraemer committed
259
260
261
262
263
{
protected:
	std::vector<unsigned int> m_markedCells ;

public:
264
	CellMarkerStore(GenericMap& map, unsigned int thread = 0) : CellMarkerBase<CELL>(map, thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
265
266
	{}

267

Pierre Kraemer's avatar
Pierre Kraemer committed
268
	virtual ~CellMarkerStore()
269
	{
270
		unmarkAll() ;
271
//		assert(isAllUnmarked);
Pierre Kraemer's avatar
Pierre Kraemer committed
272
		CGoGN_ASSERT(this->isAllUnmarked())
273
274
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
275
protected:
276
	CellMarkerStore(const CellMarkerStore& cm) : CellMarkerBase<CELL>(cm)
Pierre Kraemer's avatar
Pierre Kraemer committed
277
278
279
280
281
	{}

public:
	void mark(Dart d)
	{
282
		CellMarkerBase<CELL>::mark(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
283
		m_markedCells.push_back(this->m_map.template getEmbedding<CELL>(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
284
285
286
287
	}

	void mark(unsigned int em)
	{
288
		CellMarkerBase<CELL>::mark(em) ;
289
		m_markedCells.push_back(em) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
290
291
292
293
	}

	void unmarkAll()
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
294
295
		assert(this->m_map.template getMarkerSet<CELL>(this->m_thread).testMark(this->m_mark));
		assert(this->m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
296

Pierre Kraemer's avatar
Pierre Kraemer committed
297
		for (std::vector<unsigned int>::iterator it = m_markedCells.begin(); it != m_markedCells.end(); ++it)
298
			this->m_markVector->operator[](*it).unsetMark(this->m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
299
300
301
302
303
304
305
306
	}
};

/**
 * class that allows the marking of cells
 * the markers are not unmarked at destruction
 * \warning no default constructor
 */
307
308
template <unsigned int CELL>
class CellMarkerNoUnmark: public CellMarkerBase<CELL>
Pierre Kraemer's avatar
Pierre Kraemer committed
309
310
{
public:
311
	CellMarkerNoUnmark(GenericMap& map, unsigned int thread = 0) : CellMarkerBase<CELL>(map, thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
312
	{}
Sylvain Thery's avatar
Sylvain Thery committed
313

Pierre Kraemer's avatar
Pierre Kraemer committed
314
315
	virtual ~CellMarkerNoUnmark()
	{
316
//		assert(isAllUnmarked()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
317
		CGoGN_ASSERT(this->isAllUnmarked())
Pierre Kraemer's avatar
Pierre Kraemer committed
318
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
319
320

protected:
321
	CellMarkerNoUnmark(const CellMarkerNoUnmark& cm) : CellMarkerBase<CELL>(cm)
Pierre Kraemer's avatar
Pierre Kraemer committed
322
	{}
323
324
325
326

public:
	void unmarkAll()
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
327
328
		assert(this->m_map.template getMarkerSet<CELL>(this->m_thread).testMark(this->m_mark));
		assert(this->m_markVector != NULL);
Sylvain Thery's avatar
Sylvain Thery committed
329

330
		AttributeContainer& cont = this->m_map.template getAttributeContainer<CELL>() ;
331
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
332
			this->m_markVector->operator[](i).unsetMark(this->m_mark) ;
333
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
334
335
};

Sylvain Thery's avatar
Sylvain Thery committed
336
337

/**
338
 * selector that say if a dart has its cell marked
Sylvain Thery's avatar
Sylvain Thery committed
339
 */
340
template <unsigned int CELL>
341
class SelectorCellMarked : public FunctorSelect
Sylvain Thery's avatar
Sylvain Thery committed
342
343
{
protected:
344
	const CellMarkerBase<CELL>& m_cmarker ;
Sylvain Thery's avatar
Sylvain Thery committed
345
public:
346
	SelectorCellMarked(const CellMarkerBase<CELL>& cm) : m_cmarker(cm) {}
Sylvain Thery's avatar
Sylvain Thery committed
347
348
349
	bool operator()(Dart d) const
	{
		if (m_cmarker.isMarked(d))
350
351
			return true ;
		return false ;
Sylvain Thery's avatar
Sylvain Thery committed
352
	}
353
	FunctorSelect* copy() const { return new SelectorCellMarked(m_cmarker);}
Sylvain Thery's avatar
Sylvain Thery committed
354
355
};

356
template <unsigned int CELL>
357
class SelectorCellUnmarked : public FunctorSelect
358
359
{
protected:
360
	const CellMarkerBase<CELL>& m_cmarker ;
361
public:
362
	SelectorCellUnmarked(const CellMarkerBase<CELL>& cm) : m_cmarker(cm) {}
363
364
365
	bool operator()(Dart d) const
	{
		if (!m_cmarker.isMarked(d))
366
367
			return true ;
		return false ;
368
	}
369
	FunctorSelect* copy() const { return new SelectorCellUnmarked(m_cmarker);}
370
};
Sylvain Thery's avatar
Sylvain Thery committed
371

Thomas's avatar
Thomas committed
372
373
// Functor version (needed for use with foreach_xxx)

374
template <unsigned int CELL>
Thomas's avatar
Thomas committed
375
376
377
class FunctorCellIsMarked : public FunctorType
{
protected:
378
	CellMarkerBase<CELL>& m_marker;
Thomas's avatar
Thomas committed
379
public:
380
	FunctorCellIsMarked(CellMarkerBase<CELL>& cm) : m_marker(cm) {}
Thomas's avatar
Thomas committed
381
382
383
384
385
386
	bool operator()(Dart d)
	{
		return m_marker.isMarked(d);
	}
};

387
template <unsigned int CELL>
Thomas's avatar
Thomas committed
388
389
390
class FunctorCellIsUnmarked : public FunctorType
{
protected:
391
	CellMarkerBase<CELL>& m_marker;
Thomas's avatar
Thomas committed
392
public:
393
	FunctorCellIsUnmarked(CellMarkerBase<CELL>& cm) : m_marker(cm) {}
Thomas's avatar
Thomas committed
394
395
396
397
398
399
	bool operator()(Dart d)
	{
		return !m_marker.isMarked(d);
	}
};

Pierre Kraemer's avatar
Pierre Kraemer committed
400
401
402
} // namespace CGoGN

#endif