dartmarker.h 10.3 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#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 48
	unsigned int m_thread ;
	bool releaseOnDestruct ;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
49

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

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

protected:
	// protected copy constructor to forbid its usage
80
	DartMarkerGen(const DartMarkerGen& dm) : m_map(dm.m_map)
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82
	{}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
88 89
public:
	/**
90
	 * mark the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92 93
	 */
	virtual void mark(Dart d)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
94
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
95
		m_map.getMarkVector(DART, m_thread)->operator[](d.index).setMark(m_mark);
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97 98
	}

	/**
99
	 * unmark the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101 102
	 */
	virtual void unmark(Dart d)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
103
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
104
		m_map.getMarkVector(DART, m_thread)->operator[](d.index).unsetMark(m_mark);
Pierre Kraemer's avatar
Pierre Kraemer committed
105 106 107
	}

	/**
108
	 * test if dart is marked
Pierre Kraemer's avatar
Pierre Kraemer committed
109
	 */
110
	virtual bool isMarked(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
111
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
112
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
113
		return m_map.getMarkVector(DART, m_thread)->operator[](d.index).testMark(m_mark);
Pierre Kraemer's avatar
Pierre Kraemer committed
114 115 116
	}

	/**
117
	 * mark the darts of the given orbit of d
Pierre Kraemer's avatar
Pierre Kraemer committed
118
	 */
119
	virtual void markOrbit(unsigned int orbit, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
120
	{
121
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
122
		FunctorMark<GenericMap> fm(m_map, m_mark, m_map.getMarkVector(DART, m_thread)) ;
123
		m_map.foreach_dart_of_orbit(orbit, d, fm, m_thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126
	}

	/**
127
	 * unmark the darts of the given orbit of d
Pierre Kraemer's avatar
Pierre Kraemer committed
128
	 */
129
	virtual void unmarkOrbit(unsigned int orbit, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
130
	{
131
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
132
		FunctorUnmark<GenericMap> fm(m_map, m_mark, m_map.getMarkVector(DART, m_thread)) ;
133
		m_map.foreach_dart_of_orbit(orbit, d, fm, m_thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
134 135 136
	}

	/**
137
	 * mark the darts of the given orbit of d in the parent map
Pierre Kraemer's avatar
Pierre Kraemer committed
138
	 */
139 140
	template <typename MAP>
	void markOrbitInParent(unsigned int orbit, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
141
	{
142
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
143
		FunctorMark<GenericMap> fm(m_map, m_mark, m_map.getMarkVector(DART, m_thread)) ;
144
		foreach_dart_of_orbit_in_parent<MAP>(dynamic_cast<MAP*>(&m_map), orbit, d, fm, m_thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
145 146
	}

147 148 149 150 151
	/**
	 * unmark the darts of the given orbit of d in the parent map
	 */
	template <typename MAP>
	void unmarkOrbitInParent(unsigned int orbit, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
152
	{
153
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
154
		FunctorUnmark<GenericMap> fm(m_map, m_mark, m_map.getMarkVector(DART, m_thread)) ;
155
		foreach_dart_of_orbit_in_parent<MAP>(dynamic_cast<MAP*>(&m_map), orbit, d, fm, m_thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157
	}

158 159 160 161
	/**
	 * mark all darts
	 */
	virtual void markAll()
Pierre Kraemer's avatar
Pierre Kraemer committed
162
	{
163
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
164
		AttributeContainer& cont = m_map.getAttributeContainer(DART) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
165
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Pierre Kraemer's avatar
Pierre Kraemer committed
166
			m_map.getMarkVector(DART, m_thread)->operator[](i).setMark(m_mark);
Pierre Kraemer's avatar
Pierre Kraemer committed
167 168
	}

169 170 171 172
	/**
	 * unmark all darts
	 */
	virtual void unmarkAll() = 0 ;
173 174 175 176 177 178 179 180 181 182 183 184 185 186

	bool isAllUnmarked()
	{
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));

		AttributeMultiVector<Mark>* mark_vect = m_map.getMarkVector(DART, m_thread);
		assert(mark_vect != NULL);

		AttributeContainer& cont = m_map.getAttributeContainer(DART) ;
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
			if(mark_vect->operator[](i).testMark(m_mark))
				return false ;
		return true ;
	}
187
};
Pierre Kraemer's avatar
Pierre Kraemer committed
188

189 190 191 192 193 194 195 196 197
/**
 * class that allows the marking of darts
 * \warning no default constructor
 */
class DartMarker : public DartMarkerGen
{
public:
	DartMarker(GenericMap& map) : DartMarkerGen(map)
	{}
198

199 200 201 202
	DartMarker(GenericMap& map, unsigned int th): DartMarkerGen(map, th)
	{}

	virtual ~DartMarker()
203
	{
204
		unmarkAll() ;
205 206
	}

207 208 209 210 211 212
protected:
	DartMarker(const DartMarker& dm) : DartMarkerGen(dm)
	{}

public:
	void unmarkAll()
213
	{
214
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
215 216
		AttributeContainer& cont = m_map.getAttributeContainer(DART) ;
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Pierre Kraemer's avatar
Pierre Kraemer committed
217
			m_map.getMarkVector(DART, m_thread)->operator[](i).unsetMark(m_mark);
218
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220 221 222 223 224 225
};

/**
 * class that allows the marking of darts
 * the marked darts are stored to optimize the unmarking task at destruction
 * \warning no default constructor
 */
226
class DartMarkerStore: public DartMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
227 228 229 230 231
{
protected:
	std::vector<unsigned int> m_markedDarts ;

public:
232
	DartMarkerStore(GenericMap& map) : DartMarkerGen(map)
Pierre Kraemer's avatar
Pierre Kraemer committed
233 234
	{}

235
	DartMarkerStore(GenericMap& map, unsigned int thread): DartMarkerGen(map, thread)
Sylvain Thery's avatar
Sylvain Thery committed
236 237
	{}

238 239 240
	~DartMarkerStore()
	{
		unmarkAll() ;
241 242
//		assert(isAllUnmarked) ;
		CGoGN_ASSERT(isAllUnmarked())
243 244
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
245
protected:
246
	DartMarkerStore(const DartMarkerStore& dm) : DartMarkerGen(dm),m_markedDarts(dm.m_markedDarts)
Pierre Kraemer's avatar
Pierre Kraemer committed
247 248 249 250 251
	{}

public:
	void mark(Dart d)
	{
252
		DartMarkerGen::mark(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
253 254 255
		m_markedDarts.push_back(d.index) ;
	}

256
	void markOrbit(unsigned int orbit, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
257
	{
258
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
259
		FunctorMarkStore<GenericMap> fm(m_map, m_mark, m_map.getMarkVector(DART, m_thread), m_markedDarts) ;
260
		m_map.foreach_dart_of_orbit(orbit, d, fm, m_thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
261 262
	}

263 264 265
	template <typename MAP>
	void markOrbitInParent(unsigned int orbit, Dart d)
	{
266
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
267
		FunctorMarkStore<GenericMap> fm(m_map, m_mark, m_map.getMarkVector(DART, m_thread), m_markedDarts) ;
268 269 270 271 272 273 274 275 276
		foreach_dart_of_orbit_in_parent<MAP>(dynamic_cast<MAP*>(&m_map), orbit, d, fm, m_thread);
	}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
277 278
	void unmarkAll()
	{
279
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
Pierre Kraemer's avatar
Pierre Kraemer committed
280
		for (std::vector<unsigned int>::iterator it = m_markedDarts.begin(); it != m_markedDarts.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
281
			m_map.getMarkVector(DART, m_thread)->operator[](*it).unsetMark(m_mark) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
282 283 284 285 286 287 288 289
	}
};

/**
 * class that allows the marking of darts
 * the markers are not unmarked at destruction
 * \warning no default constructor
 */
290
class DartMarkerNoUnmark: public DartMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
291 292
{
public:
293
	DartMarkerNoUnmark(GenericMap& map): DartMarkerGen(map)
Pierre Kraemer's avatar
Pierre Kraemer committed
294 295
	{}

296
	DartMarkerNoUnmark(GenericMap& map, unsigned int th): DartMarkerGen(map,th)
Sylvain Thery's avatar
Sylvain Thery committed
297 298
	{}

Pierre Kraemer's avatar
Pierre Kraemer committed
299
	~DartMarkerNoUnmark()
300 301 302 303
	{
//		assert(isAllUnmarked) ;
		CGoGN_ASSERT(isAllUnmarked())
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
304 305

protected:
306
	DartMarkerNoUnmark(const DartMarkerNoUnmark& dm) : DartMarkerGen(dm)
Pierre Kraemer's avatar
Pierre Kraemer committed
307
	{}
308 309 310 311

public:
	void unmarkAll()
	{
312
		assert(m_map.getMarkerSet(DART, m_thread).testMark(m_mark));
313 314
		AttributeContainer& cont = m_map.getAttributeContainer(DART) ;
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Pierre Kraemer's avatar
Pierre Kraemer committed
315
			m_map.getMarkVector(DART, m_thread)->operator[](i).unsetMark(m_mark) ;
316
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
317 318 319 320 321 322 323 324
};

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

class SelectorMarked : public FunctorSelect
{
protected:
325
	DartMarkerGen& m_marker;
Pierre Kraemer's avatar
Pierre Kraemer committed
326
public:
327
	SelectorMarked(DartMarkerGen& m) : m_marker(m) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
328 329 330 331
	bool operator()(Dart d) const
	{
		return m_marker.isMarked(d);
	}
332
	FunctorSelect* copy() const { return new SelectorMarked(m_marker);}
Pierre Kraemer's avatar
Pierre Kraemer committed
333 334 335 336 337
};

class SelectorUnmarked : public FunctorSelect
{
protected:
338
	DartMarkerGen& m_marker;
Pierre Kraemer's avatar
Pierre Kraemer committed
339
public:
340
	SelectorUnmarked(DartMarkerGen& m) : m_marker(m) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
341 342 343 344
	bool operator()(Dart d) const
	{
		return !m_marker.isMarked(d);
	}
345
	FunctorSelect* copy() const { return new SelectorUnmarked(m_marker);}
Pierre Kraemer's avatar
Pierre Kraemer committed
346 347 348 349 350 351 352
};

// Functor version (needed for use with foreach_xxx)

class FunctorIsMarked : public FunctorType
{
protected:
353
	DartMarkerGen& m_marker;
Pierre Kraemer's avatar
Pierre Kraemer committed
354
public:
355
	FunctorIsMarked(DartMarkerGen& dm) : m_marker(dm) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
356 357 358 359 360 361 362 363 364
	bool operator()(Dart d)
	{
		return m_marker.isMarked(d);
	}
};

class FunctorIsUnmarked : public FunctorType
{
protected:
365
	DartMarkerGen& m_marker;
Pierre Kraemer's avatar
Pierre Kraemer committed
366
public:
367
	FunctorIsUnmarked(DartMarkerGen& dm) : m_marker(dm) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
368 369 370 371 372 373 374 375 376
	bool operator()(Dart d)
	{
		return !m_marker.isMarked(d);
	}
};

} // namespace CGoGN

#endif