dartmarker.h 8.97 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

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 darts
Pierre Kraemer's avatar
Pierre Kraemer committed
39
40
 * \warning no default constructor
 */
41
class DartMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
42
{
43
44
	friend class GenericMap ;

Pierre Kraemer's avatar
Pierre Kraemer committed
45
protected:
Sylvain Thery's avatar
Sylvain Thery committed
46
	AttributeMultiVector<MarkerBool>* m_markVector;
Sylvain Thery's avatar
Sylvain Thery committed
47
	unsigned int m_thread;
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
48

49
50
51
52
public:
	/**
	 * constructor
	 */
Sylvain Thery's avatar
Sylvain Thery committed
53
54
	DartMarkerGen(unsigned int thread=0):
		m_thread(thread)
55
56
	{}

Pierre Kraemer's avatar
Pierre Kraemer committed
57
	virtual ~DartMarkerGen()
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
	{}

protected:
	// protected copy constructor to forbid its usage
	DartMarkerGen(const DartMarkerGen& /*dm*/)
	{}

} ;

template <typename MAP>
class DartMarkerTmpl : public DartMarkerGen
{
protected:
	MAP& m_map ;

Pierre Kraemer's avatar
Pierre Kraemer committed
73
74
75
76
77
public:
	/**
	 * constructor
	 * @param map the map on which we work
	 */
78
79
80
	DartMarkerTmpl(MAP& map, unsigned int thread = 0) :
		DartMarkerGen(thread),
		m_map(map)
Sylvain Thery's avatar
Sylvain Thery committed
81
	{
Sylvain Thery's avatar
Sylvain Thery committed
82
		m_markVector = m_map.template askMarkVector<DART>(m_thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
83
84
	}

85
86
87
	DartMarkerTmpl(const MAP& map, unsigned int thread = 0) :
		DartMarkerGen(thread),
		m_map(const_cast<MAP&>(map))
Sylvain Thery's avatar
Sylvain Thery committed
88
	{
Sylvain Thery's avatar
Sylvain Thery committed
89
		m_markVector = m_map.template askMarkVector<DART>(m_thread);
Sylvain Thery's avatar
Sylvain Thery committed
90
91
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
92
	virtual ~DartMarkerTmpl()
Pierre Kraemer's avatar
Pierre Kraemer committed
93
	{
94
		if (GenericMap::alive(&m_map))
Sylvain Thery's avatar
Sylvain Thery committed
95
			m_map.template releaseMarkVector<DART>(m_markVector,m_thread);
Sylvain Thery's avatar
Sylvain Thery committed
96
97

	}
98

Sylvain Thery's avatar
Sylvain Thery committed
99
100
	inline void update()
	{
Sylvain Thery's avatar
Sylvain Thery committed
101
		m_markVector = m_map.template askMarkVector<DART>(m_thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
102
103
	}

Sylvain Thery's avatar
Sylvain Thery committed
104

Pierre Kraemer's avatar
Pierre Kraemer committed
105
106
protected:
	// protected copy constructor to forbid its usage
107
	DartMarkerTmpl(const DartMarkerTmpl<MAP>& dm) :
David Cazier's avatar
David Cazier committed
108
		m_map(dm.m_map)
109
	{}
110

Pierre Kraemer's avatar
Pierre Kraemer committed
111
112
public:
	/**
113
	 * mark the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
114
	 */
Sylvain Thery's avatar
Sylvain Thery committed
115
	inline void mark(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
116
	{
Sylvain Thery's avatar
Sylvain Thery committed
117
		assert(m_markVector != NULL);
David Cazier's avatar
David Cazier committed
118
		unsigned int d_index = m_map.dartIndex(d) ;
Sylvain Thery's avatar
Sylvain Thery committed
119
		m_markVector->setTrue(d_index);
Pierre Kraemer's avatar
Pierre Kraemer committed
120
121
122
	}

	/**
123
	 * unmark the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
124
	 */
Sylvain Thery's avatar
Sylvain Thery committed
125
	inline void unmark(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
126
	{
Sylvain Thery's avatar
Sylvain Thery committed
127
		assert(m_markVector != NULL);
David Cazier's avatar
David Cazier committed
128
		unsigned int d_index = m_map.dartIndex(d) ;
Sylvain Thery's avatar
Sylvain Thery committed
129
		m_markVector->setFalse(d_index);
Pierre Kraemer's avatar
Pierre Kraemer committed
130
131
132
	}

	/**
133
	 * test if dart is marked
Pierre Kraemer's avatar
Pierre Kraemer committed
134
	 */
Sylvain Thery's avatar
Sylvain Thery committed
135
	inline bool isMarked(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
136
	{
Sylvain Thery's avatar
Sylvain Thery committed
137
		assert(m_markVector != NULL);
David Cazier's avatar
David Cazier committed
138
		unsigned int d_index = m_map.dartIndex(d) ;
Sylvain Thery's avatar
Sylvain Thery committed
139
		return (*m_markVector)[d_index];
Pierre Kraemer's avatar
Pierre Kraemer committed
140
141
142
	}

	/**
143
	 * mark the darts of the given cell
Pierre Kraemer's avatar
Pierre Kraemer committed
144
	 */
145
	template <unsigned int ORBIT>
146
	inline void markOrbit(Cell<ORBIT> c)
Pierre Kraemer's avatar
Pierre Kraemer committed
147
	{
Sylvain Thery's avatar
Sylvain Thery committed
148
		assert(m_markVector != NULL);
149
150
		m_map.foreach_dart_of_orbit(c, [&] (Dart d)
		{
Sylvain Thery's avatar
Sylvain Thery committed
151
152
			m_markVector->setTrue(m_map.dartIndex(d));
		}) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
153
154
155
	}

	/**
156
	 * unmark the darts of the given cell
Pierre Kraemer's avatar
Pierre Kraemer committed
157
	 */
158
	template <unsigned int ORBIT>
159
	inline void unmarkOrbit(Cell<ORBIT> c)
Pierre Kraemer's avatar
Pierre Kraemer committed
160
	{
Sylvain Thery's avatar
Sylvain Thery committed
161
		assert(m_markVector != NULL);
162
163
		m_map.foreach_dart_of_orbit(c, [&] (Dart d)
		{
Sylvain Thery's avatar
Sylvain Thery committed
164
165
166
			m_markVector->setFalse(m_map.dartIndex(d));
		}) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
	}

169
170
171
	/**
	 * mark all darts
	 */
Sylvain Thery's avatar
Sylvain Thery committed
172
	inline void markAll()
Pierre Kraemer's avatar
Pierre Kraemer committed
173
	{
Sylvain Thery's avatar
Sylvain Thery committed
174
		assert(m_markVector != NULL);
175
		AttributeContainer& cont = m_map.template getAttributeContainer<DART>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
176
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Sylvain Thery's avatar
Sylvain Thery committed
177
			m_markVector->setTrue(i);
Pierre Kraemer's avatar
Pierre Kraemer committed
178
179
	}

180
181
182
	/**
	 * unmark all darts
	 */
183
//	virtual void unmarkAll() = 0 ;
184

Sylvain Thery's avatar
Sylvain Thery committed
185
	inline bool isAllUnmarked()
186
	{
187
		assert(m_markVector != NULL);
188
		AttributeContainer& cont = m_map.template getAttributeContainer<DART>() ;
189
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Sylvain Thery's avatar
Sylvain Thery committed
190
			if ((*m_markVector)[i])
191
				return false ;
192
193
		return true ;
	}
194
};
Pierre Kraemer's avatar
Pierre Kraemer committed
195

196
197
198
199
/**
 * class that allows the marking of darts
 * \warning no default constructor
 */
200
201
template <typename MAP>
class DartMarker : public DartMarkerTmpl<MAP>
202
203
{
public:
Sylvain Thery's avatar
Sylvain Thery committed
204
205
	DartMarker( MAP& map, unsigned int thread=0) :
		DartMarkerTmpl<MAP>(map,thread)
206
	{}
Sylvain Thery's avatar
Sylvain Thery committed
207

Sylvain Thery's avatar
Sylvain Thery committed
208
	DartMarker(const MAP& map, unsigned int thread=0) :
209
210
		DartMarkerTmpl<MAP>(map, thread)
	{}
211

Pierre Kraemer's avatar
Pierre Kraemer committed
212
	virtual ~DartMarker()
213
	{
214
		unmarkAll() ;
215
216
	}

217
protected:
David Cazier's avatar
David Cazier committed
218
	DartMarker(const DartMarker& dm) :
219
220
		DartMarkerTmpl<MAP>(dm)
	{}
221
222

public:
Sylvain Thery's avatar
Sylvain Thery committed
223
	inline void unmarkAll()
224
	{
Sylvain Thery's avatar
Sylvain Thery committed
225
		this->m_markVector->allFalse();
226
	}
David Cazier's avatar
David Cazier committed
227
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
228
229
230
231
232
233

/**
 * class that allows the marking of darts
 * the marked darts are stored to optimize the unmarking task at destruction
 * \warning no default constructor
 */
234
235
template <typename MAP>
class DartMarkerStore : public DartMarkerTmpl<MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
236
237
238
239
240
{
protected:
	std::vector<unsigned int> m_markedDarts ;

public:
Sylvain Thery's avatar
Sylvain Thery committed
241
242
	DartMarkerStore(MAP& map, unsigned int thread=0) :
		DartMarkerTmpl<MAP>(map, thread)
243
244
245
	{
		m_markedDarts.reserve(128);
	}
Sylvain Thery's avatar
Sylvain Thery committed
246

Sylvain Thery's avatar
Sylvain Thery committed
247
	DartMarkerStore(const MAP& map, unsigned int thread=0) :
248
		DartMarkerTmpl<MAP>(map, thread)
249
250
251
	{
		m_markedDarts.reserve(128);
	}
Sylvain Thery's avatar
Sylvain Thery committed
252

Pierre Kraemer's avatar
Pierre Kraemer committed
253
	virtual ~DartMarkerStore()
254
255
	{
		unmarkAll() ;
256
//		assert(isAllUnmarked) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
257
//		CGoGN_ASSERT(isAllUnmarked())
258
259
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
260
protected:
David Cazier's avatar
David Cazier committed
261
	DartMarkerStore(const DartMarkerStore& dm) :
262
263
264
		DartMarkerTmpl<MAP>(dm),
		m_markedDarts(dm.m_markedDarts)
	{}
Pierre Kraemer's avatar
Pierre Kraemer committed
265
266

public:
Sylvain Thery's avatar
Sylvain Thery committed
267
	inline void mark(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
268
	{
269
270
		DartMarkerTmpl<MAP>::mark(d) ;
		unsigned int d_index = this->m_map.dartIndex(d) ;
271
		m_markedDarts.push_back(d_index) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
272
273
	}

274
	template <unsigned int ORBIT>
275
	inline void markOrbit(Cell<ORBIT> c)
Pierre Kraemer's avatar
Pierre Kraemer committed
276
	{
277
278
		this->m_map.foreach_dart_of_orbit(c, [&] (Dart d)
		{
279
			unsigned int d_index = this->m_map.dartIndex(d);
Sylvain Thery's avatar
Sylvain Thery committed
280
			this->m_markVector->setTrue(d_index);
281
			m_markedDarts.push_back(d_index);
Sylvain Thery's avatar
Sylvain Thery committed
282
		}) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
283
284
	}

Sylvain Thery's avatar
Sylvain Thery committed
285
	inline void unmarkAll()
Pierre Kraemer's avatar
Pierre Kraemer committed
286
	{
Sylvain Thery's avatar
Sylvain Thery committed
287
288
			for (std::vector<unsigned int>::iterator it = m_markedDarts.begin(); it != m_markedDarts.end(); ++it)
			this->m_markVector->setFalse(*it);
Pierre Kraemer's avatar
Pierre Kraemer committed
289
	}
David Cazier's avatar
David Cazier committed
290
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
291
292
293
294
295
296

/**
 * class that allows the marking of darts
 * the markers are not unmarked at destruction
 * \warning no default constructor
 */
297
298
template <typename MAP>
class DartMarkerNoUnmark : public DartMarkerTmpl<MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
299
300
{
public:
301
302
303
	DartMarkerNoUnmark(const MAP& map) :
		DartMarkerTmpl<MAP>(map)
	{}
Sylvain Thery's avatar
Sylvain Thery committed
304

305
306
307
	DartMarkerNoUnmark(const MAP& map, unsigned int thread) :
		DartMarkerTmpl<MAP>(map, thread)
	{}
Sylvain Thery's avatar
Sylvain Thery committed
308

Pierre Kraemer's avatar
Pierre Kraemer committed
309
	virtual ~DartMarkerNoUnmark()
310
311
	{
//		assert(isAllUnmarked) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
312
//		CGoGN_ASSERT(isAllUnmarked())
313
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
314
315

protected:
David Cazier's avatar
David Cazier committed
316
	DartMarkerNoUnmark(const DartMarkerNoUnmark& dm) :
317
318
		DartMarkerTmpl<MAP>(dm)
	{}
319
320

public:
Sylvain Thery's avatar
Sylvain Thery committed
321
	inline void unmarkAll()
322
	{
Sylvain Thery's avatar
Sylvain Thery committed
323
		this->m_markVector->allFalse();
324
	}
David Cazier's avatar
David Cazier committed
325
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
326
327
328
329

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

330
template <typename MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
331
332
333
class SelectorMarked : public FunctorSelect
{
protected:
334
335
	DartMarkerTmpl<MAP>& m_marker ;

Pierre Kraemer's avatar
Pierre Kraemer committed
336
public:
337
	SelectorMarked(DartMarkerTmpl<MAP>& m) :
David Cazier's avatar
David Cazier committed
338
		m_marker(m)
339
340
	{}

Sylvain Thery's avatar
Sylvain Thery committed
341
	inline bool operator()(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
342
	{
David Cazier's avatar
David Cazier committed
343
		return m_marker.isMarked(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
344
	}
345

Sylvain Thery's avatar
Sylvain Thery committed
346
	inline FunctorSelect* copy() const
David Cazier's avatar
David Cazier committed
347
348
349
350
	{
		return new SelectorMarked(m_marker) ;
	}
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
351

352
template <typename MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
353
354
355
class SelectorUnmarked : public FunctorSelect
{
protected:
356
357
	DartMarkerTmpl<MAP>& m_marker ;

Pierre Kraemer's avatar
Pierre Kraemer committed
358
public:
359
	SelectorUnmarked(DartMarkerTmpl<MAP>& m) :
David Cazier's avatar
David Cazier committed
360
		m_marker(m)
361
362
	{}

Sylvain Thery's avatar
Sylvain Thery committed
363
	inline bool operator()(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
364
	{
David Cazier's avatar
David Cazier committed
365
366
		return !m_marker.isMarked(d) ;
	}
367

Sylvain Thery's avatar
Sylvain Thery committed
368
	inline FunctorSelect* copy() const
David Cazier's avatar
David Cazier committed
369
370
	{
		return new SelectorUnmarked(m_marker) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
371
	}
David Cazier's avatar
David Cazier committed
372
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
373
374
375

// Functor version (needed for use with foreach_xxx)

376
template <typename MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
377
378
379
class FunctorIsMarked : public FunctorType
{
protected:
380
381
	DartMarkerTmpl<MAP>& m_marker ;

Pierre Kraemer's avatar
Pierre Kraemer committed
382
public:
383
	FunctorIsMarked(DartMarkerTmpl<MAP>& dm) :
David Cazier's avatar
David Cazier committed
384
		m_marker(dm)
385
386
	{}

Sylvain Thery's avatar
Sylvain Thery committed
387
	inline bool operator()(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
388
	{
David Cazier's avatar
David Cazier committed
389
		return m_marker.isMarked(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
390
	}
David Cazier's avatar
David Cazier committed
391
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
392

393
template <typename MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
394
395
396
class FunctorIsUnmarked : public FunctorType
{
protected:
397
398
	DartMarkerTmpl<MAP>& m_marker ;

Pierre Kraemer's avatar
Pierre Kraemer committed
399
public:
400
	FunctorIsUnmarked(DartMarkerTmpl<MAP>& dm) :
David Cazier's avatar
David Cazier committed
401
		m_marker(dm)
402
403
	{}

Sylvain Thery's avatar
Sylvain Thery committed
404
	inline bool operator()(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
405
	{
David Cazier's avatar
David Cazier committed
406
		return !m_marker.isMarked(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
407
	}
David Cazier's avatar
David Cazier committed
408
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
409
410
411
412

} // namespace CGoGN

#endif