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

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;
Pierre Kraemer's avatar
Pierre Kraemer committed
47

48 49 50 51
public:
	/**
	 * constructor
	 */
Sylvain Thery's avatar
Sylvain Thery committed
52
	DartMarkerGen()
53 54
	{}

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

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
71 72 73 74 75
public:
	/**
	 * constructor
	 * @param map the map on which we work
	 */
Sylvain Thery's avatar
Sylvain Thery committed
76 77
	DartMarkerTmpl(MAP& map) :
		DartMarkerGen(),
78
		m_map(map)
Sylvain Thery's avatar
Sylvain Thery committed
79
	{
Sylvain Thery's avatar
Sylvain Thery committed
80
		m_markVector = m_map.template askMarkVector<DART>();
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82
	}

Sylvain Thery's avatar
Sylvain Thery committed
83 84
	DartMarkerTmpl(const MAP& map) :
		DartMarkerGen(),
85
		m_map(const_cast<MAP&>(map))
Sylvain Thery's avatar
Sylvain Thery committed
86
	{
Sylvain Thery's avatar
Sylvain Thery committed
87
		m_markVector = m_map.template askMarkVector<DART>();
Sylvain Thery's avatar
Sylvain Thery committed
88 89
	}

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

	}
96

97 98 99 100
	/**
	 * @brief update: realloc the marker in map
	 * @warning call only after map cleaning
	 */
Sylvain Thery's avatar
Sylvain Thery committed
101 102
	inline void update()
	{
Sylvain Thery's avatar
Sylvain Thery committed
103
		m_markVector = m_map.template askMarkVector<DART>();
Pierre Kraemer's avatar
Pierre Kraemer committed
104 105
	}

Sylvain Thery's avatar
Sylvain Thery committed
106

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

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

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

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
169 170
	}

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

185 186 187
	/**
	 * unmark all darts
	 */
188
	inline bool isAllUnmarked()
189
	{
190
		assert(m_markVector != NULL);
191
		AttributeContainer& cont = m_map.template getAttributeContainer<DART>() ;
192 193 194 195 196 197 198 199 200
		if (cont.hasBrowser())
		{
			for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
				if ((*m_markVector)[i])
					return false ;
			return true ;
		}
		//else
		return m_markVector->isAllFalse();
201
	}
202

203
};
Pierre Kraemer's avatar
Pierre Kraemer committed
204

205

206 207 208 209
/**
 * class that allows the marking of darts
 * \warning no default constructor
 */
210 211
template <typename MAP>
class DartMarker : public DartMarkerTmpl<MAP>
212 213
{
public:
Sylvain Thery's avatar
Sylvain Thery committed
214 215
	DartMarker( MAP& map) :
		DartMarkerTmpl<MAP>(map)
216
	{}
Sylvain Thery's avatar
Sylvain Thery committed
217

Sylvain Thery's avatar
Sylvain Thery committed
218 219
	DartMarker(const MAP& map) :
		DartMarkerTmpl<MAP>(map)
220
	{}
221

Pierre Kraemer's avatar
Pierre Kraemer committed
222
	virtual ~DartMarker()
223
	{
224
		unmarkAll() ;
225 226
	}

227
protected:
David Cazier's avatar
David Cazier committed
228
	DartMarker(const DartMarker& dm) :
229 230
		DartMarkerTmpl<MAP>(dm)
	{}
231 232

public:
233
	inline void unmarkAll()
234
	{
235 236 237 238 239 240 241 242
//		AttributeContainer& cont = this->m_map.template  getAttributeContainer<DART>();
//		if (cont.hasBrowser())
//			for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
//				this->m_markVector->setFalse(i);
//		else

		// always unmark all darts, it's to dangerous because of markOrbit that can mark dart out of Browser !
		this->m_markVector->allFalse();
243
	}
David Cazier's avatar
David Cazier committed
244
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
245 246 247 248 249 250

/**
 * class that allows the marking of darts
 * the marked darts are stored to optimize the unmarking task at destruction
 * \warning no default constructor
 */
251 252
template <typename MAP>
class DartMarkerStore : public DartMarkerTmpl<MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
253 254
{
protected:
Sylvain Thery's avatar
Sylvain Thery committed
255
	std::vector<Dart>* m_markedDarts ;
Pierre Kraemer's avatar
Pierre Kraemer committed
256
public:
Sylvain Thery's avatar
Sylvain Thery committed
257 258
	DartMarkerStore(MAP& map) :
		DartMarkerTmpl<MAP>(map)
259
	{
Sylvain Thery's avatar
Sylvain Thery committed
260
		m_markedDarts = this->m_map.askDartBuffer();
261
	}
Sylvain Thery's avatar
Sylvain Thery committed
262

Sylvain Thery's avatar
Sylvain Thery committed
263 264
	DartMarkerStore(const MAP& map) :
		DartMarkerTmpl<MAP>(map)
265
	{
Sylvain Thery's avatar
Sylvain Thery committed
266
		m_markedDarts =this->m_map.askDartBuffer();
267
	}
Sylvain Thery's avatar
Sylvain Thery committed
268

Pierre Kraemer's avatar
Pierre Kraemer committed
269
	virtual ~DartMarkerStore()
270 271
	{
		unmarkAll() ;
Sylvain Thery's avatar
Sylvain Thery committed
272
		this->m_map.releaseDartBuffer(m_markedDarts);
273 274
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
275
protected:
David Cazier's avatar
David Cazier committed
276
	DartMarkerStore(const DartMarkerStore& dm) :
277 278 279
		DartMarkerTmpl<MAP>(dm),
		m_markedDarts(dm.m_markedDarts)
	{}
Pierre Kraemer's avatar
Pierre Kraemer committed
280 281

public:
282
	inline void mark(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
283
	{
284
		DartMarkerTmpl<MAP>::mark(d) ;
Sylvain Thery's avatar
Sylvain Thery committed
285
		m_markedDarts->push_back(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
286 287
	}

288
	template <unsigned int ORBIT>
289
	inline void markOrbit(Cell<ORBIT> c)
Pierre Kraemer's avatar
Pierre Kraemer committed
290
	{
291 292
		this->m_map.foreach_dart_of_orbit(c, [&] (Dart d)
		{
Sylvain Thery's avatar
Sylvain Thery committed
293 294
			DartMarkerTmpl<MAP>::mark(d) ;
			m_markedDarts->push_back(d) ;
Sylvain Thery's avatar
Sylvain Thery committed
295
		}) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
296 297
	}

298
	inline void unmarkAll()
Pierre Kraemer's avatar
Pierre Kraemer committed
299
	{
Sylvain Thery's avatar
Sylvain Thery committed
300 301 302 303 304 305 306
		for (std::vector<Dart>::iterator it = m_markedDarts->begin(); it != m_markedDarts->end(); ++it)
			this->m_markVector->setFalse(this->m_map.dartIndex(*it));
	}

	inline const std::vector<Dart>& getDartVector() const
	{
		return *m_markedDarts;
Pierre Kraemer's avatar
Pierre Kraemer committed
307
	}
David Cazier's avatar
David Cazier committed
308
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
309 310 311 312 313 314

/**
 * class that allows the marking of darts
 * the markers are not unmarked at destruction
 * \warning no default constructor
 */
315 316
template <typename MAP>
class DartMarkerNoUnmark : public DartMarkerTmpl<MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
317
{
318 319 320
#ifndef NDEBUG
	int m_counter;
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
321
public:
Sylvain Thery's avatar
Sylvain Thery committed
322
	DartMarkerNoUnmark(MAP& map) :
323
		DartMarkerTmpl<MAP>(map)
324 325 326
#ifndef NDEBUG
	  ,m_counter(0)
#endif
327
	{}
Sylvain Thery's avatar
Sylvain Thery committed
328

Sylvain Thery's avatar
Sylvain Thery committed
329 330
	DartMarkerNoUnmark(const MAP& map) :
		DartMarkerTmpl<MAP>(map)
331 332 333
#ifndef NDEBUG
	  ,m_counter(0)
#endif
334
	{}
Sylvain Thery's avatar
Sylvain Thery committed
335

Pierre Kraemer's avatar
Pierre Kraemer committed
336
	virtual ~DartMarkerNoUnmark()
337
	{
338 339 340 341 342 343 344 345
#ifndef NDEBUG
		if (m_counter != 0)
		{
			CGoGNerr << "DartMarkerNoUnmark: Warning problem unmarking not complete"<< CGoGNendl;
			CGoGNerr << "DartMarkerNoUnmark:  -> calling unmarkAll()"<< CGoGNendl;
			unmarkAll();
		}
#endif
346
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
347 348

protected:
David Cazier's avatar
David Cazier committed
349
	DartMarkerNoUnmark(const DartMarkerNoUnmark& dm) :
350
		DartMarkerTmpl<MAP>(dm)
351 352 353
#ifndef NDEBUG
	  ,m_counter(dm.m_counter)
#endif
354
	{}
355 356

public:
357
	inline void unmarkAll()
358
	{
359 360 361 362 363 364 365 366
//		AttributeContainer& cont = this->m_map.template  getAttributeContainer<DART>();
//		if (cont.hasBrowser())
//			for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
//				this->m_markVector->setFalse(i);
//		else

		// always unmark all darts, it's to dangerous because of markOrbit that can mark dart out of Browser !
		this->m_markVector->allFalse();
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	}

#ifndef NDEBUG
	inline void mark(Dart d)
	{
		if (this->isMarked(d))
			return;
		DartMarkerTmpl<MAP>::mark(d) ;
		m_counter++;
	}

	/**
	 * unmark the dart
	 */
	inline void unmark(Dart d)
	{
		if (!this->isMarked(d))
			return;
		DartMarkerTmpl<MAP>::unmark(d) ;
		m_counter--;
387
	}
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

	template <unsigned int ORBIT>
	inline void markOrbit(Cell<ORBIT> c)
	{
		assert(this->m_markVector != NULL);
		this->m_map.foreach_dart_of_orbit(c, [&] (Dart d)
		{
			if (!this->isMarked(d))
			{
				this->m_markVector->setTrue(this->m_map.dartIndex(d));
				m_counter++;
			}
		}) ;
	}

	template <unsigned int ORBIT>
	inline void unmarkOrbit(Cell<ORBIT> c)
	{
		assert(this->m_markVector != NULL);
		this->m_map.foreach_dart_of_orbit(c, [&] (Dart d)
		{
			if (this->isMarked(d))
			{
				this->m_markVector->setFalse(this->m_map.dartIndex(d));
				m_counter--;
			}
		}) ;
	}
#endif

David Cazier's avatar
David Cazier committed
418
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
419 420 421 422

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

423
template <typename MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
424 425 426
class SelectorMarked : public FunctorSelect
{
protected:
427 428
	DartMarkerTmpl<MAP>& m_marker ;

Pierre Kraemer's avatar
Pierre Kraemer committed
429
public:
430
	SelectorMarked(DartMarkerTmpl<MAP>& m) :
David Cazier's avatar
David Cazier committed
431
		m_marker(m)
432 433
	{}

434
	inline bool operator()(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
435
	{
David Cazier's avatar
David Cazier committed
436
		return m_marker.isMarked(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
437
	}
438

439
	inline FunctorSelect* copy() const
David Cazier's avatar
David Cazier committed
440 441 442 443
	{
		return new SelectorMarked(m_marker) ;
	}
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
444

445
template <typename MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
446 447 448
class SelectorUnmarked : public FunctorSelect
{
protected:
449 450
	DartMarkerTmpl<MAP>& m_marker ;

Pierre Kraemer's avatar
Pierre Kraemer committed
451
public:
452
	SelectorUnmarked(DartMarkerTmpl<MAP>& m) :
David Cazier's avatar
David Cazier committed
453
		m_marker(m)
454 455
	{}

456
	inline bool operator()(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
457
	{
David Cazier's avatar
David Cazier committed
458 459
		return !m_marker.isMarked(d) ;
	}
460

461
	inline FunctorSelect* copy() const
David Cazier's avatar
David Cazier committed
462 463
	{
		return new SelectorUnmarked(m_marker) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
464
	}
David Cazier's avatar
David Cazier committed
465
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
466 467 468

// Functor version (needed for use with foreach_xxx)

469
template <typename MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
470 471 472
class FunctorIsMarked : public FunctorType
{
protected:
473 474
	DartMarkerTmpl<MAP>& m_marker ;

Pierre Kraemer's avatar
Pierre Kraemer committed
475
public:
476
	FunctorIsMarked(DartMarkerTmpl<MAP>& dm) :
David Cazier's avatar
David Cazier committed
477
		m_marker(dm)
478 479
	{}

480
	inline bool operator()(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
481
	{
David Cazier's avatar
David Cazier committed
482
		return m_marker.isMarked(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
483
	}
David Cazier's avatar
David Cazier committed
484
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
485

486
template <typename MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
487 488 489
class FunctorIsUnmarked : public FunctorType
{
protected:
490 491
	DartMarkerTmpl<MAP>& m_marker ;

Pierre Kraemer's avatar
Pierre Kraemer committed
492
public:
493
	FunctorIsUnmarked(DartMarkerTmpl<MAP>& dm) :
David Cazier's avatar
David Cazier committed
494
		m_marker(dm)
495 496
	{}

497
	inline bool operator()(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
498
	{
David Cazier's avatar
David Cazier committed
499
		return !m_marker.isMarked(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
500
	}
David Cazier's avatar
David Cazier committed
501
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
502 503 504 505

} // namespace CGoGN

#endif