dartmarker.h 10.7 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
/*******************************************************************************
David Cazier's avatar
David Cazier committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
 * version 0.1                                                                  *
 * Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
 *                                                                              *
 * 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.           *
 *                                                                              *
 * Web site: http://cgogn.unistra.fr/                                           *
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
24
25
26
27
28
29
30

#ifndef __DART_MARKER__
#define __DART_MARKER__

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

namespace CGoGN
{

/**
37
 * generic class that allows the marking of darts
Pierre Kraemer's avatar
Pierre Kraemer committed
38
39
 * \warning no default constructor
 */
40
class DartMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
41
{
42
43
	friend class GenericMap ;

Pierre Kraemer's avatar
Pierre Kraemer committed
44
protected:
45
46
	GenericMap& m_map ;
	Mark m_mark ;
47
	AttributeMultiVector<Mark>* m_markVector ;
48
49
	unsigned int m_thread ;
	bool releaseOnDestruct ;
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
50

Pierre Kraemer's avatar
Pierre Kraemer committed
51
52
53
54
55
public:
	/**
	 * constructor
	 * @param map the map on which we work
	 */
David Cazier's avatar
David Cazier committed
56
57
	DartMarkerGen(GenericMap& map, unsigned int thread = 0) :
		m_map(map), m_thread(thread), releaseOnDestruct(true)
Sylvain Thery's avatar
Sylvain Thery committed
58
	{
59
60
		m_mark = m_map.getMarkerSet<DART>(m_thread).getNewMark() ;
		m_markVector = m_map.getMarkVector<DART>(m_thread) ;
61
		m_map.dartMarkers[m_thread].push_back(this) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
62
63
	}

64
	virtual ~DartMarkerGen()
Pierre Kraemer's avatar
Pierre Kraemer committed
65
	{
David Cazier's avatar
David Cazier committed
66
		if (releaseOnDestruct)
67
		{
68
			m_map.getMarkerSet<DART>(m_thread).releaseMark(m_mark) ;
David Cazier's avatar
David Cazier committed
69
70
			std::vector<DartMarkerGen*>& dmg = m_map.dartMarkers[m_thread] ;
			for (std::vector<DartMarkerGen*>::iterator it = dmg.begin(); it != dmg.end(); ++it)
71
			{
David Cazier's avatar
David Cazier committed
72
				if (*it == this)
73
				{
David Cazier's avatar
David Cazier committed
74
75
76
					*it = dmg.back() ;
					dmg.pop_back() ;
					return ;
77
78
79
				}
			}
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
80
81
	}

David Cazier's avatar
David Cazier committed
82
83
84
85
	unsigned int getThread()
	{
		return m_thread ;
	}
86

David Cazier's avatar
David Cazier committed
87
88
89
90
	void updateMarkVector(AttributeMultiVector<Mark>* amv)
	{
		m_markVector = amv ;
	}
91

Pierre Kraemer's avatar
Pierre Kraemer committed
92
93
protected:
	// protected copy constructor to forbid its usage
David Cazier's avatar
David Cazier committed
94
95
96
97
	DartMarkerGen(const DartMarkerGen& dm) :
		m_map(dm.m_map)
	{
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
98

99
100
101
	/**
	 * set if the mark has to be release on destruction or not
	 */
David Cazier's avatar
David Cazier committed
102
103
104
105
	void setReleaseOnDestruct(bool b)
	{
		releaseOnDestruct = b ;
	}
106

Pierre Kraemer's avatar
Pierre Kraemer committed
107
108
public:
	/**
109
	 * mark the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
110
111
112
	 */
	virtual void mark(Dart d)
	{
113
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
David Cazier's avatar
David Cazier committed
114
115
		unsigned int d_index = m_map.dartIndex(d) ;
		m_markVector->operator[](d_index).setMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
116
117
118
	}

	/**
119
	 * unmark the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
120
121
122
	 */
	virtual void unmark(Dart d)
	{
123
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
David Cazier's avatar
David Cazier committed
124
125
		unsigned int d_index = m_map.dartIndex(d) ;
		m_markVector->operator[](d_index).unsetMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
126
127
128
	}

	/**
129
	 * test if dart is marked
Pierre Kraemer's avatar
Pierre Kraemer committed
130
	 */
131
	virtual bool isMarked(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
132
	{
133
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
David Cazier's avatar
David Cazier committed
134
135
		unsigned int d_index = m_map.dartIndex(d) ;
		return m_markVector->operator[](d_index).testMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
136
137
138
	}

	/**
139
	 * mark the darts of the given orbit of d
Pierre Kraemer's avatar
Pierre Kraemer committed
140
	 */
141
142
	template <unsigned int ORBIT>
	void markOrbit(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
143
	{
144
145
146
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
		FunctorMark<GenericMap> fm(m_map, m_mark, m_markVector) ;
		m_map.foreach_dart_of_orbit<ORBIT>(d, fm, m_thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
147
148
149
	}

	/**
150
	 * unmark the darts of the given orbit of d
Pierre Kraemer's avatar
Pierre Kraemer committed
151
	 */
152
	template <unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
153
	void unmarkOrbit(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
154
	{
155
156
157
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
		FunctorUnmark<GenericMap> fm(m_map, m_mark, m_markVector) ;
		m_map.foreach_dart_of_orbit<ORBIT>(d, fm, m_thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
158
159
	}

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//	/**
//	 * mark the darts of the given orbit of d in the parent map
//	 */
//	template <typename MAP>
//	void markOrbitInParent(unsigned int orbit, Dart d)
//	{
//		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
//		FunctorMark<GenericMap> fm(m_map, m_mark, m_map.getMarkVector(DART, m_thread)) ;
//		foreach_dart_of_orbit_in_parent<MAP>(dynamic_cast<MAP*>(&m_map), orbit, d, fm, m_thread);
//	}
//
//	/**
//	 * unmark the darts of the given orbit of d in the parent map
//	 */
//	template <typename MAP>
//	void unmarkOrbitInParent(unsigned int orbit, Dart d)
//	{
//		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
//		FunctorUnmark<GenericMap> fm(m_map, m_mark, m_map.getMarkVector(DART, m_thread)) ;
//		foreach_dart_of_orbit_in_parent<MAP>(dynamic_cast<MAP*>(&m_map), orbit, d, fm, m_thread);
//	}
Pierre Kraemer's avatar
Pierre Kraemer committed
181

182
183
184
185
	/**
	 * mark all darts
	 */
	virtual void markAll()
Pierre Kraemer's avatar
Pierre Kraemer committed
186
	{
187
188
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
		AttributeContainer& cont = m_map.getAttributeContainer<DART>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
189
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
David Cazier's avatar
David Cazier committed
190
			m_markVector->operator[](i).setMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
191
192
	}

193
194
195
196
	/**
	 * unmark all darts
	 */
	virtual void unmarkAll() = 0 ;
197
198
199

	bool isAllUnmarked()
	{
200
201
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
		assert(m_markVector != NULL);
202

203
		AttributeContainer& cont = m_map.getAttributeContainer<DART>() ;
204
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
David Cazier's avatar
David Cazier committed
205
			if (m_markVector->operator[](i).testMark(m_mark)) return false ;
206
207
		return true ;
	}
David Cazier's avatar
David Cazier committed
208
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
209

210
211
212
213
214
215
216
/**
 * class that allows the marking of darts
 * \warning no default constructor
 */
class DartMarker : public DartMarkerGen
{
public:
David Cazier's avatar
David Cazier committed
217
218
219
220
	DartMarker(GenericMap& map) :
		DartMarkerGen(map)
	{
	}
221

David Cazier's avatar
David Cazier committed
222
223
224
225
	DartMarker(GenericMap& map, unsigned int thread) :
		DartMarkerGen(map, thread)
	{
	}
226
227

	virtual ~DartMarker()
228
	{
229
		unmarkAll() ;
230
231
	}

232
protected:
David Cazier's avatar
David Cazier committed
233
234
235
236
	DartMarker(const DartMarker& dm) :
		DartMarkerGen(dm)
	{
	}
237
238
239

public:
	void unmarkAll()
240
	{
241
242
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
		AttributeContainer& cont = m_map.getAttributeContainer<DART>() ;
243
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
David Cazier's avatar
David Cazier committed
244
			m_markVector->operator[](i).unsetMark(m_mark) ;
245
	}
David Cazier's avatar
David Cazier committed
246
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
247
248
249
250
251
252

/**
 * class that allows the marking of darts
 * the marked darts are stored to optimize the unmarking task at destruction
 * \warning no default constructor
 */
David Cazier's avatar
David Cazier committed
253
class DartMarkerStore : public DartMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
254
255
256
257
258
{
protected:
	std::vector<unsigned int> m_markedDarts ;

public:
David Cazier's avatar
David Cazier committed
259
260
261
262
	DartMarkerStore(GenericMap& map) :
		DartMarkerGen(map)
	{
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
263

David Cazier's avatar
David Cazier committed
264
265
266
267
	DartMarkerStore(GenericMap& map, unsigned int thread) :
		DartMarkerGen(map, thread)
	{
	}
Sylvain Thery's avatar
Sylvain Thery committed
268

269
270
271
	~DartMarkerStore()
	{
		unmarkAll() ;
272
273
//		assert(isAllUnmarked) ;
		CGoGN_ASSERT(isAllUnmarked())
274
275
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
276
protected:
David Cazier's avatar
David Cazier committed
277
278
279
280
	DartMarkerStore(const DartMarkerStore& dm) :
		DartMarkerGen(dm), m_markedDarts(dm.m_markedDarts)
	{
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
281
282
283
284

public:
	void mark(Dart d)
	{
285
		DartMarkerGen::mark(d) ;
David Cazier's avatar
David Cazier committed
286
		unsigned int d_index = m_map.dartIndex(d) ;
287
		m_markedDarts.push_back(d_index) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
288
289
	}

290
291
	template <unsigned int ORBIT>
	void markOrbit(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
292
	{
293
294
295
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
		FunctorMarkStore<GenericMap> fm(m_map, m_mark, m_markVector, m_markedDarts) ;
		m_map.foreach_dart_of_orbit<ORBIT>(d, fm, m_thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
296
297
	}

298
299
300
301
302
303
304
//	template <typename MAP>
//	void markOrbitInParent(unsigned int orbit, Dart d)
//	{
//		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
//		FunctorMarkStore<GenericMap> fm(m_map, m_mark, m_map.getMarkVector(DART, m_thread), m_markedDarts) ;
//		foreach_dart_of_orbit_in_parent<MAP>(dynamic_cast<MAP*>(&m_map), orbit, d, fm, m_thread);
//	}
305
306
307
308
309
310
311

//	void mark(unsigned int d)
//	{
//		DartMarkerGen::mark(d) ;
//		m_markedDarts.push_back(d) ;
//	}

Pierre Kraemer's avatar
Pierre Kraemer committed
312
313
	void unmarkAll()
	{
314
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
315
		for (std::vector<unsigned int>::iterator it = m_markedDarts.begin(); it != m_markedDarts.end(); ++it)
316
			m_markVector->operator[](*it).unsetMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
317
	}
David Cazier's avatar
David Cazier committed
318
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
319
320
321
322
323
324

/**
 * class that allows the marking of darts
 * the markers are not unmarked at destruction
 * \warning no default constructor
 */
David Cazier's avatar
David Cazier committed
325
class DartMarkerNoUnmark : public DartMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
326
327
{
public:
David Cazier's avatar
David Cazier committed
328
329
330
331
	DartMarkerNoUnmark(GenericMap& map) :
		DartMarkerGen(map)
	{
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
332

David Cazier's avatar
David Cazier committed
333
334
335
336
	DartMarkerNoUnmark(GenericMap& map, unsigned int th) :
		DartMarkerGen(map, th)
	{
	}
Sylvain Thery's avatar
Sylvain Thery committed
337

Pierre Kraemer's avatar
Pierre Kraemer committed
338
	~DartMarkerNoUnmark()
339
340
341
342
	{
//		assert(isAllUnmarked) ;
		CGoGN_ASSERT(isAllUnmarked())
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
343
344

protected:
David Cazier's avatar
David Cazier committed
345
346
347
348
	DartMarkerNoUnmark(const DartMarkerNoUnmark& dm) :
		DartMarkerGen(dm)
	{
	}
349
350
351
352

public:
	void unmarkAll()
	{
353
354
		assert(m_map.getMarkerSet<DART>(m_thread).testMark(m_mark));
		AttributeContainer& cont = m_map.getAttributeContainer<DART>() ;
355
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
356
			m_markVector->operator[](i).unsetMark(m_mark) ;
357
	}
David Cazier's avatar
David Cazier committed
358
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
359
360
361
362
363
364
365

// Selector and count functors testing for marker existence
/********************************************************/

class SelectorMarked : public FunctorSelect
{
protected:
David Cazier's avatar
David Cazier committed
366
	DartMarkerGen& m_marker ;
Pierre Kraemer's avatar
Pierre Kraemer committed
367
public:
David Cazier's avatar
David Cazier committed
368
369
370
371
	SelectorMarked(DartMarkerGen& m) :
		m_marker(m)
	{
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
372
373
	bool operator()(Dart d) const
	{
David Cazier's avatar
David Cazier committed
374
		return m_marker.isMarked(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
375
	}
David Cazier's avatar
David Cazier committed
376
377
378
379
380
	FunctorSelect* copy() const
	{
		return new SelectorMarked(m_marker) ;
	}
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
381
382
383
384

class SelectorUnmarked : public FunctorSelect
{
protected:
David Cazier's avatar
David Cazier committed
385
	DartMarkerGen& m_marker ;
Pierre Kraemer's avatar
Pierre Kraemer committed
386
public:
David Cazier's avatar
David Cazier committed
387
388
389
390
	SelectorUnmarked(DartMarkerGen& m) :
		m_marker(m)
	{
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
391
392
	bool operator()(Dart d) const
	{
David Cazier's avatar
David Cazier committed
393
394
395
396
397
		return !m_marker.isMarked(d) ;
	}
	FunctorSelect* copy() const
	{
		return new SelectorUnmarked(m_marker) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
398
	}
David Cazier's avatar
David Cazier committed
399
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
400
401
402
403
404
405

// Functor version (needed for use with foreach_xxx)

class FunctorIsMarked : public FunctorType
{
protected:
David Cazier's avatar
David Cazier committed
406
	DartMarkerGen& m_marker ;
Pierre Kraemer's avatar
Pierre Kraemer committed
407
public:
David Cazier's avatar
David Cazier committed
408
409
410
411
	FunctorIsMarked(DartMarkerGen& dm) :
		m_marker(dm)
	{
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
412
413
	bool operator()(Dart d)
	{
David Cazier's avatar
David Cazier committed
414
		return m_marker.isMarked(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
415
	}
David Cazier's avatar
David Cazier committed
416
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
417
418
419
420

class FunctorIsUnmarked : public FunctorType
{
protected:
David Cazier's avatar
David Cazier committed
421
	DartMarkerGen& m_marker ;
Pierre Kraemer's avatar
Pierre Kraemer committed
422
public:
David Cazier's avatar
David Cazier committed
423
424
425
426
	FunctorIsUnmarked(DartMarkerGen& dm) :
		m_marker(dm)
	{
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
427
428
	bool operator()(Dart d)
	{
David Cazier's avatar
David Cazier committed
429
		return !m_marker.isMarked(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
430
	}
David Cazier's avatar
David Cazier committed
431
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
432
433
434
435

} // namespace CGoGN

#endif