dartmarker.h 9.14 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
{
protected:
Sylvain Thery's avatar
Sylvain Thery committed
238
	std::vector<unsigned int>* m_markedDarts ;
Pierre Kraemer's avatar
Pierre Kraemer committed
239
public:
Sylvain Thery's avatar
Sylvain Thery committed
240 241
	DartMarkerStore(MAP& map, unsigned int thread=0) :
		DartMarkerTmpl<MAP>(map, thread)
242
	{
Sylvain Thery's avatar
Sylvain Thery committed
243 244
//		m_markedDarts.reserve(128);
		m_markedDarts = GenericMap::askUIntBuffer(thread);
245
	}
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
	{
Sylvain Thery's avatar
Sylvain Thery committed
250 251
//		m_markedDarts.reserve(128);
		m_markedDarts = GenericMap::askUIntBuffer(thread);
252
	}
Sylvain Thery's avatar
Sylvain Thery committed
253

Pierre Kraemer's avatar
Pierre Kraemer committed
254
	virtual ~DartMarkerStore()
255 256
	{
		unmarkAll() ;
Sylvain Thery's avatar
Sylvain Thery committed
257
		GenericMap::releaseUIntBuffer(m_markedDarts, this->m_thread);
258
//		assert(isAllUnmarked) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
259
//		CGoGN_ASSERT(isAllUnmarked())
260 261
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// Functor version (needed for use with foreach_xxx)

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

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

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

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

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

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

} // namespace CGoGN

#endif