dartmarker.h 9.85 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 31 32 33 34 35 36 37 38 39 40 41
* 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"

namespace CGoGN
{

// Marker Functors
/********************************************************/

template <typename MAP>
class FunctorMarker : public FunctorMap<MAP>
{
protected:
42 43
	Marker m_marker ;
	AttributeMultiVector<Mark>* m_markerTable ;
Pierre Kraemer's avatar
Pierre Kraemer committed
44
public:
45
	FunctorMarker(MAP& map, Marker m, AttributeMultiVector<Mark>* mTable) : FunctorMap<MAP>(map), m_marker(m), m_markerTable(mTable) {}
46 47
	Marker getMarker() { return m_marker ; }
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49

template <typename MAP>
50
class FunctorMark : public FunctorMarker<MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
51 52
{
public:
53
	FunctorMark(MAP& map, Marker m, AttributeMultiVector<Mark>* mTable) : FunctorMarker<MAP>(map, m, mTable) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
54 55
	bool operator()(Dart d)
	{
56 57
		this->m_markerTable->operator[](d.index).setMark(this->m_marker) ;
		return false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
58
	}
59
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
60 61

template <typename MAP>
62
class FunctorMarkStore : public FunctorMarker<MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
63
{
64 65
protected:
	std::vector<unsigned int>& m_markedDarts ;
Pierre Kraemer's avatar
Pierre Kraemer committed
66
public:
67 68 69 70
	FunctorMarkStore(MAP& map, Marker m, AttributeMultiVector<Mark>* mTable, std::vector<unsigned int>& marked) :
		FunctorMarker<MAP>(map, m, mTable),
		m_markedDarts(marked)
	{}
Pierre Kraemer's avatar
Pierre Kraemer committed
71 72
	bool operator()(Dart d)
	{
73 74 75
		this->m_markerTable->operator[](d.index).setMark(this->m_marker) ;
		m_markedDarts.push_back(d.index) ;
		return false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
76
	}
77
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
78

79 80 81 82 83 84 85 86 87 88 89
template <typename MAP>
class FunctorUnmark : public FunctorMarker<MAP>
{
public:
	FunctorUnmark(MAP& map, Marker m, AttributeMultiVector<Mark>* mTable) : FunctorMarker<MAP>(map, m, mTable) {}
	bool operator()(Dart d)
	{
		this->m_markerTable->operator[](d.index).unsetMark(this->m_marker) ;
		return false ;
	}
} ;
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91

/**
92
 * generic class that allows the marking of darts
Pierre Kraemer's avatar
Pierre Kraemer committed
93 94
 * \warning no default constructor
 */
95
class DartMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97 98 99
{
protected:
	Marker m_marker;
	GenericMap& m_map;
Sylvain Thery's avatar
Sylvain Thery committed
100
	unsigned int m_thread;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
101

Pierre Kraemer's avatar
Pierre Kraemer committed
102 103 104 105 106
public:
	/**
	 * constructor
	 * @param map the map on which we work
	 */
107
	DartMarkerGen(GenericMap& map): m_map(map), m_thread(0)
Pierre Kraemer's avatar
Pierre Kraemer committed
108
	{
109
		m_marker = map.getNewMarker(DART);
Sylvain Thery's avatar
Sylvain Thery committed
110 111
	}

112
	DartMarkerGen(GenericMap& map, unsigned int thread): m_map(map), m_thread(thread)
Sylvain Thery's avatar
Sylvain Thery committed
113
	{
114
		m_marker = map.getNewMarker(DART, thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116
	}

117
	virtual ~DartMarkerGen()
Pierre Kraemer's avatar
Pierre Kraemer committed
118
	{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
119
		m_map.releaseMarker(m_marker, m_thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121 122 123
	}

protected:
	// protected copy constructor to forbid its usage
124
	DartMarkerGen(const DartMarkerGen& dm) : m_map(dm.m_map)
Pierre Kraemer's avatar
Pierre Kraemer committed
125 126 127 128
	{}

public:
	/**
129
	 * mark the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
130 131 132
	 */
	virtual void mark(Dart d)
	{
133
		m_map.getMarkerVector(DART, m_thread)->operator[](d.index).setMark(m_marker);
Pierre Kraemer's avatar
Pierre Kraemer committed
134 135 136
	}

	/**
137
	 * unmark the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140
	 */
	virtual void unmark(Dart d)
	{
141
		m_map.getMarkerVector(DART, m_thread)->operator[](d.index).unsetMark(m_marker);
Pierre Kraemer's avatar
Pierre Kraemer committed
142 143 144
	}

	/**
145
	 * test if dart is marked
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147 148
	 */
	virtual bool isMarked(Dart d)
	{
149
		return m_map.getMarkerVector(DART, m_thread)->operator[](d.index).testMark(m_marker);
Pierre Kraemer's avatar
Pierre Kraemer committed
150 151 152
	}

	/**
153
	 * mark the darts of the given orbit of d
Pierre Kraemer's avatar
Pierre Kraemer committed
154
	 */
155
	virtual void markOrbit(unsigned int orbit, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
156
	{
157 158
		FunctorMark<GenericMap> fm(m_map, m_marker, m_map.getMarkerVector(DART, m_thread)) ;
		m_map.foreach_dart_of_orbit(orbit, d, fm, m_thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
159 160 161
	}

	/**
162
	 * unmark the darts of the given orbit of d
Pierre Kraemer's avatar
Pierre Kraemer committed
163
	 */
164
	virtual void unmarkOrbit(unsigned int orbit, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
165
	{
166 167
		FunctorUnmark<GenericMap> fm(m_map, m_marker, m_map.getMarkerVector(DART, m_thread)) ;
		m_map.foreach_dart_of_orbit(orbit, d, fm, m_thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
168 169 170
	}

	/**
171
	 * mark the darts of the given orbit of d in the parent map
Pierre Kraemer's avatar
Pierre Kraemer committed
172
	 */
173 174
	template <typename MAP>
	void markOrbitInParent(unsigned int orbit, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
175
	{
176 177
		FunctorMark<GenericMap> fm(m_map, m_marker, m_map.getMarkerVector(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
178 179
	}

180 181 182 183 184
	/**
	 * 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
185
	{
186 187
		FunctorUnmark<GenericMap> fm(m_map, m_marker, m_map.getMarkerVector(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
188 189
	}

190 191 192 193
	/**
	 * mark all darts
	 */
	virtual void markAll()
Pierre Kraemer's avatar
Pierre Kraemer committed
194
	{
195
		AttributeContainer& cont = m_map.getAttributeContainer(DART) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
196
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
197
			m_map.getMarkerVector(DART, m_thread)->operator[](i).setMark(m_marker);
Pierre Kraemer's avatar
Pierre Kraemer committed
198 199
	}

200 201 202 203 204
	/**
	 * unmark all darts
	 */
	virtual void unmarkAll() = 0 ;
};
Pierre Kraemer's avatar
Pierre Kraemer committed
205

206 207 208 209 210 211 212 213 214
/**
 * class that allows the marking of darts
 * \warning no default constructor
 */
class DartMarker : public DartMarkerGen
{
public:
	DartMarker(GenericMap& map) : DartMarkerGen(map)
	{}
215

216 217 218 219
	DartMarker(GenericMap& map, unsigned int th): DartMarkerGen(map, th)
	{}

	virtual ~DartMarker()
220
	{
221
		unmarkAll() ;
222 223
	}

224 225 226 227 228 229
protected:
	DartMarker(const DartMarker& dm) : DartMarkerGen(dm)
	{}

public:
	void unmarkAll()
230
	{
231 232 233
		AttributeContainer& cont = m_map.getAttributeContainer(DART) ;
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
			m_map.getMarkerVector(DART, m_thread)->operator[](i).unsetMark(m_marker);
234
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
235 236 237 238 239 240 241
};

/**
 * class that allows the marking of darts
 * the marked darts are stored to optimize the unmarking task at destruction
 * \warning no default constructor
 */
242
class DartMarkerStore: public DartMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
243 244 245 246 247
{
protected:
	std::vector<unsigned int> m_markedDarts ;

public:
248
	DartMarkerStore(GenericMap& map) : DartMarkerGen(map)
Pierre Kraemer's avatar
Pierre Kraemer committed
249 250
	{}

251
	DartMarkerStore(GenericMap& map, unsigned int thread): DartMarkerGen(map, thread)
Sylvain Thery's avatar
Sylvain Thery committed
252 253
	{}

254 255 256 257 258
	~DartMarkerStore()
	{
		unmarkAll() ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
259
protected:
260
	DartMarkerStore(const DartMarkerStore& dm) : DartMarkerGen(dm)
Pierre Kraemer's avatar
Pierre Kraemer committed
261 262 263 264 265
	{}

public:
	void mark(Dart d)
	{
266
		DartMarkerGen::mark(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
267 268 269
		m_markedDarts.push_back(d.index) ;
	}

270
	void markOrbit(unsigned int orbit, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
271
	{
272 273
		FunctorMarkStore<GenericMap> fm(m_map, m_marker, m_map.getMarkerVector(DART, m_thread), m_markedDarts) ;
		m_map.foreach_dart_of_orbit(orbit, d, fm, m_thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
274 275
	}

276 277 278 279 280 281 282 283 284 285 286 287 288
	template <typename MAP>
	void markOrbitInParent(unsigned int orbit, Dart d)
	{
		FunctorMarkStore<GenericMap> fm(m_map, m_marker, m_map.getMarkerVector(DART, m_thread), m_markedDarts) ;
		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
289 290 291
	void unmarkAll()
	{
		for (std::vector<unsigned int>::iterator it = m_markedDarts.begin(); it != m_markedDarts.end(); ++it)
292
			m_map.getMarkerVector(DART, m_thread)->operator[](*it).unsetMark(m_marker) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
293 294 295 296 297 298 299 300
	}
};

/**
 * class that allows the marking of darts
 * the markers are not unmarked at destruction
 * \warning no default constructor
 */
301
class DartMarkerNoUnmark: public DartMarkerGen
Pierre Kraemer's avatar
Pierre Kraemer committed
302 303
{
public:
304
	DartMarkerNoUnmark(GenericMap& map): DartMarkerGen(map)
Pierre Kraemer's avatar
Pierre Kraemer committed
305 306
	{}

307
	DartMarkerNoUnmark(GenericMap& map, unsigned int th): DartMarkerGen(map,th)
Sylvain Thery's avatar
Sylvain Thery committed
308 309
	{}

Pierre Kraemer's avatar
Pierre Kraemer committed
310
	~DartMarkerNoUnmark()
311
	{}
Pierre Kraemer's avatar
Pierre Kraemer committed
312 313

protected:
314
	DartMarkerNoUnmark(const DartMarkerNoUnmark& dm) : DartMarkerGen(dm)
Pierre Kraemer's avatar
Pierre Kraemer committed
315
	{}
316 317 318 319 320 321 322 323

public:
	void unmarkAll()
	{
		AttributeContainer& cont = m_map.getAttributeContainer(DART) ;
		for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
			m_map.getMarkerVector(DART, m_thread)->operator[](i).unsetMark(m_marker) ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
324 325 326 327 328 329 330 331
};

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

class SelectorMarked : public FunctorSelect
{
protected:
332
	DartMarkerGen& m_marker;
Pierre Kraemer's avatar
Pierre Kraemer committed
333
public:
334
	SelectorMarked(DartMarkerGen& m) : m_marker(m) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
335 336 337 338 339 340 341 342 343
	bool operator()(Dart d) const
	{
		return m_marker.isMarked(d);
	}
};

class SelectorUnmarked : public FunctorSelect
{
protected:
344
	DartMarkerGen& m_marker;
Pierre Kraemer's avatar
Pierre Kraemer committed
345
public:
346
	SelectorUnmarked(DartMarkerGen& m) : m_marker(m) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
347 348 349 350 351 352 353 354 355 356 357
	bool operator()(Dart d) const
	{
		return !m_marker.isMarked(d);
	}
};

// Functor version (needed for use with foreach_xxx)

class FunctorIsMarked : public FunctorType
{
protected:
358
	DartMarkerGen& m_marker;
Pierre Kraemer's avatar
Pierre Kraemer committed
359
public:
360
	FunctorIsMarked(DartMarkerGen& dm) : m_marker(dm) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
361 362 363 364 365 366 367 368 369
	bool operator()(Dart d)
	{
		return m_marker.isMarked(d);
	}
};

class FunctorIsUnmarked : public FunctorType
{
protected:
370
	DartMarkerGen& m_marker;
Pierre Kraemer's avatar
Pierre Kraemer committed
371
public:
372
	FunctorIsUnmarked(DartMarkerGen& dm) : m_marker(dm) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
373 374 375 376 377 378 379 380 381
	bool operator()(Dart d)
	{
		return !m_marker.isMarked(d);
	}
};

} // namespace CGoGN

#endif