genericmap.hpp 20.6 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-2012, 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.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25 26
#include "Topology/generic/dartmarker.h"
#include "Topology/generic/traversorCell.h"
27
#include "Topology/generic/traversorFactory.h"
28

Pierre Kraemer's avatar
Pierre Kraemer committed
29 30
namespace CGoGN
{
Pierre Kraemer's avatar
Pierre Kraemer committed
31

Pierre Kraemer's avatar
Pierre Kraemer committed
32 33 34 35 36 37
/****************************************
 *           DARTS MANAGEMENT           *
 ****************************************/

inline Dart GenericMap::newDart()
{
38
	unsigned int di = m_attribs[DART].insertLine();		// insert a new dart line
Pierre Kraemer's avatar
Pierre Kraemer committed
39
	for(unsigned int i = 0; i < NB_ORBITS; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
40
	{
41 42
		if (m_embeddings[i])							// set all its embeddings
			(*m_embeddings[i])[di] = EMBNULL ;			// to EMBNULL
Pierre Kraemer's avatar
Pierre Kraemer committed
43
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
44

Pierre Kraemer's avatar
Pierre Kraemer committed
45
	return Dart::create(di) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
}

Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51 52
inline void GenericMap::deleteDartLine(unsigned int index)
{
	m_attribs[DART].removeLine(index) ;	// free the dart line

	for (unsigned int t = 0; t < m_nbThreads; ++t)	// clear markers of
53
		(*m_markTables[DART][t])[index].clear() ;	// the removed dart
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
54

Pierre Kraemer's avatar
Pierre Kraemer committed
55 56
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
57
		if (m_embeddings[orbit])									// for each embedded orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
58
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
59
			unsigned int emb = (*m_embeddings[orbit])[index] ;		// get the embedding of the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
60
			if(emb != EMBNULL)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
61
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
62
				if(m_attribs[orbit].unrefLine(emb))					// unref the pointed embedding line
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
63
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
64
					for (unsigned int t = 0; t < m_nbThreads; ++t)	// and clear its markers if it was
65
						(*m_markTables[orbit][t])[emb].clear() ;	// its last unref (and was thus freed)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
66 67
				}
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
68 69
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
70
}
Pierre Kraemer's avatar
Pierre Kraemer committed
71

Pierre Kraemer's avatar
Pierre Kraemer committed
72
inline unsigned int GenericMap::copyDartLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
73
{
74 75
	unsigned int newindex = m_attribs[DART].insertLine() ;	// create a new dart line
	m_attribs[DART].copyLine(newindex, index) ;				// copy the given dart line
Pierre Kraemer's avatar
Pierre Kraemer committed
76 77
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
78 79 80 81 82 83
		if (m_embeddings[orbit])
		{
			unsigned int emb = (*m_embeddings[orbit])[newindex] ;	// add a ref to the cells pointed
			if(emb != EMBNULL)										// by the new dart line
				m_attribs[orbit].refLine(emb) ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
84 85
	}
	return newindex ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87
}

Pierre Kraemer's avatar
Pierre Kraemer committed
88 89
inline unsigned int GenericMap::getNbDarts()
{
90
	return m_attribs[DART].size() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92
}

Pierre Kraemer's avatar
Pierre Kraemer committed
93 94 95 96 97
inline bool GenericMap::isDartValid(Dart d)
{
	return !d.isNil() && m_attribs[DART].used(dartIndex(d)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
98 99 100 101
/****************************************
 *         EMBEDDING MANAGEMENT         *
 ****************************************/

102 103
template <unsigned int ORBIT>
inline bool GenericMap::isOrbitEmbedded() const
Pierre Kraemer's avatar
Pierre Kraemer committed
104
{
105
	return (ORBIT == DART) || (m_embeddings[ORBIT] != NULL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107
}

108
inline bool GenericMap::isOrbitEmbedded(unsigned int orbit) const
Pierre Kraemer's avatar
Pierre Kraemer committed
109
{
110
	return (orbit == DART) || (m_embeddings[orbit] != NULL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
111 112
}

113
template <unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
114
inline unsigned int GenericMap::getEmbedding(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
115
{
116
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
Pierre Kraemer's avatar
Pierre Kraemer committed
117

118 119
	unsigned int d_index = dartIndex(d);

120
	if (ORBIT == DART)
121
		return d_index;
Pierre Kraemer's avatar
Pierre Kraemer committed
122

123
	return (*m_embeddings[ORBIT])[d_index] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125
}

126 127 128
template <unsigned int ORBIT>
void GenericMap::setDartEmbedding(Dart d, unsigned int emb)
{
129
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

	unsigned int old = getEmbedding<ORBIT>(d);

	if (old == emb)	// if same emb
		return;		// nothing to do

	if (old != EMBNULL)	// if different
	{
		if(m_attribs[ORBIT].unrefLine(old))	// then unref the old emb
		{
			for (unsigned int t = 0; t < m_nbThreads; ++t)	// clear the markers if it was the
				(*m_markTables[ORBIT][t])[old].clear();		// last unref of the line
		}
	}

	if (emb != EMBNULL)
		m_attribs[ORBIT].refLine(emb);	// ref the new emb

Pierre Kraemer's avatar
Pierre Kraemer committed
148
	(*m_embeddings[ORBIT])[dartIndex(d)] = emb ; // finally affect the embedding to the dart
149 150
}

151 152 153 154
template <unsigned int ORBIT>
void GenericMap::initDartEmbedding(Dart d, unsigned int emb)
{
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
Pierre Kraemer's avatar
Pierre Kraemer committed
155
	assert(getEmbedding<ORBIT>(d) == EMBNULL || !"initDartEmbedding called on already embedded dart");
156 157
	if(emb != EMBNULL)
		m_attribs[ORBIT].refLine(emb);	// ref the new emb
158 159 160
	(*m_embeddings[ORBIT])[dartIndex(d)] = emb ; // affect the embedding to the dart
}

161 162
template <unsigned int ORBIT>
inline void GenericMap::copyDartEmbedding(Dart dest, Dart src)
Pierre Kraemer's avatar
Pierre Kraemer committed
163
{
164
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
165
	setDartEmbedding<ORBIT>(dest, getEmbedding<ORBIT>(src));
Pierre Kraemer's avatar
Pierre Kraemer committed
166 167
}

168 169
template <unsigned int ORBIT>
inline unsigned int GenericMap::newCell()
Pierre Kraemer's avatar
Pierre Kraemer committed
170
{
171
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
172
	return m_attribs[ORBIT].insertLine();
Pierre Kraemer's avatar
Pierre Kraemer committed
173 174
}

175
template <unsigned int ORBIT>
176
inline void GenericMap::setOrbitEmbedding(Dart d, unsigned int em)
Pierre Kraemer's avatar
Pierre Kraemer committed
177
{
178
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
179 180
	FunctorSetEmb<GenericMap, ORBIT> fsetemb(*this, em);
	foreach_dart_of_orbit<ORBIT>(d, fsetemb);
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182
}

183
template <unsigned int ORBIT>
184 185 186 187 188 189 190 191
inline void GenericMap::initOrbitEmbedding(Dart d, unsigned int em)
{
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
	FunctorInitEmb<GenericMap, ORBIT> fsetemb(*this, em);
	foreach_dart_of_orbit<ORBIT>(d, fsetemb);
}

template <unsigned int ORBIT>
192
inline unsigned int GenericMap::setOrbitEmbeddingOnNewCell(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
193
{
194
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
195
	unsigned int em = newCell<ORBIT>();
196 197 198 199 200 201 202 203 204 205
	setOrbitEmbedding<ORBIT>(d, em);
	return em;
}

template <unsigned int ORBIT>
inline unsigned int GenericMap::initOrbitEmbeddingNewCell(Dart d)
{
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
	unsigned int em = newCell<ORBIT>();
	initOrbitEmbedding<ORBIT>(d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
206 207 208
	return em;
}

209 210
template <unsigned int ORBIT>
inline void GenericMap::copyCell(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
211
{
212
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
213 214
	unsigned int dE = getEmbedding<ORBIT>(d) ;
	unsigned int eE = getEmbedding<ORBIT>(e) ;
215 216 217 218
	if(eE != EMBNULL)	// if the source is NULL, nothing to copy
	{
		if(dE == EMBNULL)	// if the dest is NULL, create a new cell
			dE = setOrbitEmbeddingOnNewCell<ORBIT>(d) ;
219
		m_attribs[ORBIT].copyLine(dE, eE) ;	// copy the data
220
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
221 222
}

223 224
template <unsigned int ORBIT>
inline void GenericMap::copyCell(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
225
{
226
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
227
	m_attribs[ORBIT].copyLine(i, j) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229
}

230 231
template <unsigned int ORBIT>
inline void GenericMap::initCell(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
232
{
233
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
234
	m_attribs[ORBIT].initLine(i) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
235 236
}

237 238 239
template <unsigned int ORBIT>
void GenericMap::initAllOrbitsEmbedding(bool realloc)
{
240 241
	if(!isOrbitEmbedded<ORBIT>())
		addEmbedding<ORBIT>() ;
242 243 244 245 246 247 248 249 250 251 252 253
	DartMarker mark(*this) ;
	for(Dart d = begin(); d != end(); next(d))
	{
		if(!mark.isMarked(d))
		{
			mark.markOrbit<ORBIT>(d) ;
			if(realloc || getEmbedding<ORBIT>(d) == EMBNULL)
				setOrbitEmbeddingOnNewCell<ORBIT>(d) ;
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
254 255 256 257 258 259 260
/****************************************
 *     QUICK TRAVERSAL MANAGEMENT       *
 ****************************************/

template <unsigned int ORBIT>
inline void GenericMap::enableQuickTraversal()
{
Sylvain Thery's avatar
Sylvain Thery committed
261

Pierre Kraemer's avatar
Pierre Kraemer committed
262 263 264 265 266 267 268 269 270 271
	if(m_quickTraversal[ORBIT] == NULL)
	{
		if(!isOrbitEmbedded<ORBIT>())
			addEmbedding<ORBIT>() ;
		m_quickTraversal[ORBIT] = m_attribs[ORBIT].addAttribute<Dart>("quick_traversal") ;
	}
	updateQuickTraversal<ORBIT>() ;
}

template <unsigned int ORBIT>
272
inline void GenericMap::updateQuickTraversal()
Pierre Kraemer's avatar
Pierre Kraemer committed
273
{
Pierre Kraemer's avatar
Pierre Kraemer committed
274 275
	assert(m_quickTraversal[ORBIT] != NULL || !"updateQuickTraversal on a disabled orbit") ;

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
//	CellMarker<ORBIT> cm(*this) ;
//	for(Dart d = begin(); d != end(); next(d))
//	{
//		if ((!cm.isMarked(d)) && (!isBoundaryMarkedCurrent(d)))
//		{
//			cm.mark(d) ;
//			(*m_quickTraversal[ORBIT])[getEmbedding<ORBIT>(d)] = d ;
//		}
//	}

	// ensure that we do not try to use quick traversal in Traversors
	AttributeMultiVector<Dart>* qt = m_quickTraversal[ORBIT];
	m_quickTraversal[ORBIT] = NULL;

	// fill the quick travsersal
	TraversorCell<GenericMap,VOLUME> trav(*this);
	for(Dart d = trav.begin(); d != trav.end(); d=trav.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
293
	{
294
		(*qt)[getEmbedding<ORBIT>(d)] = d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
295
	}
296 297 298 299

	// restore ptr
	m_quickTraversal[ORBIT] = qt;

Pierre Kraemer's avatar
Pierre Kraemer committed
300 301 302
}

template <unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
303
inline const AttributeMultiVector<Dart>* GenericMap::getQuickTraversal() const
Pierre Kraemer's avatar
Pierre Kraemer committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317
{
	return m_quickTraversal[ORBIT] ;
}

template <unsigned int ORBIT>
inline void GenericMap::disableQuickTraversal()
{
	if(m_quickTraversal[ORBIT] != NULL)
	{
		m_attribs[ORBIT].removeAttribute<Dart>(m_quickTraversal[ORBIT]->getIndex()) ;
		m_quickTraversal[ORBIT] = NULL ;
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
318 319

template <typename MAP, unsigned int ORBIT, unsigned int INCI>
Sylvain Thery's avatar
Sylvain Thery committed
320
inline void GenericMap::enableQuickIncidentTraversal()
Sylvain Thery's avatar
Sylvain Thery committed
321 322 323 324 325 326 327 328 329
{
	if(m_quickLocalIncidentTraversal[ORBIT][INCI] == NULL)
	{
		if(!isOrbitEmbedded<ORBIT>())
			addEmbedding<ORBIT>() ;
		std::stringstream ss;
		ss << "quickLocalIncidentTraversal_" << INCI;
		m_quickLocalIncidentTraversal[ORBIT][INCI] = m_attribs[ORBIT].addAttribute<NoTypeNameAttribute<std::vector<Dart> > >(ss.str()) ;
	}
Sylvain Thery's avatar
Sylvain Thery committed
330
	updateQuickIncidentTraversal<MAP,ORBIT,INCI>() ;
Sylvain Thery's avatar
Sylvain Thery committed
331 332 333
}

template <typename MAP, unsigned int ORBIT, unsigned int INCI>
Sylvain Thery's avatar
Sylvain Thery committed
334
inline void GenericMap::updateQuickIncidentTraversal()
Sylvain Thery's avatar
Sylvain Thery committed
335 336 337 338 339 340 341 342 343
{
	assert(m_quickLocalIncidentTraversal[ORBIT][INCI] != NULL || !"updateQuickTraversal on a disabled orbit") ;

	AttributeMultiVector<NoTypeNameAttribute<std::vector<Dart> > >* ptrVD = m_quickLocalIncidentTraversal[ORBIT][INCI];
	m_quickLocalIncidentTraversal[ORBIT][INCI] = NULL;

	std::vector<Dart> buffer;
	buffer.reserve(100);

Sylvain Thery's avatar
Sylvain Thery committed
344 345
	MAP& map = static_cast<MAP&>(*this);

Sylvain Thery's avatar
Sylvain Thery committed
346 347 348 349
	TraversorCell<MAP,ORBIT> tra_glob(map);
	for (Dart d = tra_glob.begin(); d != tra_glob.end(); d = tra_glob.next())
	{
		buffer.clear();
Sylvain Thery's avatar
Sylvain Thery committed
350
		Traversor* tra_loc = TraversorFactory<MAP>::createIncident(map, d, map.dimension(), ORBIT, INCI);
Sylvain Thery's avatar
Sylvain Thery committed
351
		for (Dart e = tra_loc->begin(); e != tra_loc->end(); e = tra_loc->next())
Sylvain Thery's avatar
Sylvain Thery committed
352
			buffer.push_back(e);
Sylvain Thery's avatar
Sylvain Thery committed
353
		delete tra_loc;
Sylvain Thery's avatar
Sylvain Thery committed
354 355 356 357 358 359 360 361 362
		buffer.push_back(NIL);
		std::vector<Dart>& vd = (*ptrVD)[getEmbedding<ORBIT>(d)];
		vd.reserve(buffer.size());
		vd.assign(buffer.begin(),buffer.end());
	}
	m_quickLocalIncidentTraversal[ORBIT][INCI] = ptrVD;
}

template <unsigned int ORBIT, unsigned int INCI>
Sylvain Thery's avatar
Sylvain Thery committed
363
inline const AttributeMultiVector<NoTypeNameAttribute<std::vector<Dart> > >* GenericMap::getQuickIncidentTraversal() const
Sylvain Thery's avatar
Sylvain Thery committed
364 365 366 367 368
{
	return m_quickLocalIncidentTraversal[ORBIT][INCI] ;
}

template <unsigned int ORBIT, unsigned int INCI>
Sylvain Thery's avatar
Sylvain Thery committed
369
inline void GenericMap::disableQuickIncidentTraversal()
Sylvain Thery's avatar
Sylvain Thery committed
370 371 372 373 374 375 376 377 378 379 380
{
	if(m_quickLocalIncidentTraversal[ORBIT][INCI] != NULL)
	{
		m_attribs[ORBIT].removeAttribute<Dart>(m_quickLocalIncidentTraversal[ORBIT][INCI]->getIndex()) ;
		m_quickLocalIncidentTraversal[ORBIT][INCI] = NULL ;
	}
}



template <typename MAP, unsigned int ORBIT, unsigned int ADJ>
Sylvain Thery's avatar
Sylvain Thery committed
381
inline void GenericMap::enableQuickAdjacentTraversal()
Sylvain Thery's avatar
Sylvain Thery committed
382 383 384 385 386 387
{
	if(m_quickLocalAdjacentTraversal[ORBIT][ADJ] == NULL)
	{
		if(!isOrbitEmbedded<ORBIT>())
			addEmbedding<ORBIT>() ;
		std::stringstream ss;
Sylvain Thery's avatar
Sylvain Thery committed
388
		ss << "quickLocalAdjacentTraversal" << ADJ;
Sylvain Thery's avatar
Sylvain Thery committed
389 390
		m_quickLocalAdjacentTraversal[ORBIT][ADJ] = m_attribs[ORBIT].addAttribute<NoTypeNameAttribute<std::vector<Dart> > >(ss.str()) ;
	}
Sylvain Thery's avatar
Sylvain Thery committed
391
	updateQuickAdjacentTraversal<MAP,ORBIT,ADJ>() ;
Sylvain Thery's avatar
Sylvain Thery committed
392 393 394
}

template <typename MAP, unsigned int ORBIT, unsigned int ADJ>
Sylvain Thery's avatar
Sylvain Thery committed
395
inline void GenericMap::updateQuickAdjacentTraversal()
Sylvain Thery's avatar
Sylvain Thery committed
396 397 398 399 400 401
{
	assert(m_quickLocalAdjacentTraversal[ORBIT][ADJ] != NULL || !"updateQuickTraversal on a disabled orbit") ;

	AttributeMultiVector<NoTypeNameAttribute<std::vector<Dart> > >* ptrVD = m_quickLocalAdjacentTraversal[ORBIT][ADJ];
	m_quickLocalAdjacentTraversal[ORBIT][ADJ] = NULL;

Sylvain Thery's avatar
Sylvain Thery committed
402 403
	MAP& map = static_cast<MAP&>(*this);

Sylvain Thery's avatar
Sylvain Thery committed
404 405 406 407 408 409 410
	std::vector<Dart> buffer;
	buffer.reserve(100);

	TraversorCell<MAP,ORBIT> tra_glob(map);
	for (Dart d = tra_glob.begin(); d != tra_glob.end(); d = tra_glob.next())
	{
		buffer.clear();
Sylvain Thery's avatar
Sylvain Thery committed
411
		Traversor* tra_loc = TraversorFactory<MAP>::createAdjacent(map, d, map.dimension(), ORBIT, ADJ);
Sylvain Thery's avatar
Sylvain Thery committed
412
		for (Dart e = tra_loc->begin(); e != tra_loc->end(); e = tra_loc->next())
Sylvain Thery's avatar
Sylvain Thery committed
413 414
			buffer.push_back(e);
		buffer.push_back(NIL);
Sylvain Thery's avatar
Sylvain Thery committed
415
		delete tra_loc;
Sylvain Thery's avatar
Sylvain Thery committed
416 417 418 419 420 421 422
		std::vector<Dart>& vd = (*ptrVD)[getEmbedding<ORBIT>(d)];
		vd.reserve(buffer.size());
		vd.assign(buffer.begin(),buffer.end());
	}
	m_quickLocalAdjacentTraversal[ORBIT][ADJ] = ptrVD;
}

Sylvain Thery's avatar
Sylvain Thery committed
423
template <unsigned int ORBIT, unsigned int ADJ>
Sylvain Thery's avatar
Sylvain Thery committed
424
inline const AttributeMultiVector<NoTypeNameAttribute<std::vector<Dart> > >* GenericMap::getQuickAdjacentTraversal() const
Sylvain Thery's avatar
Sylvain Thery committed
425
{
Sylvain Thery's avatar
Sylvain Thery committed
426
	return m_quickLocalAdjacentTraversal[ORBIT][ADJ] ;
Sylvain Thery's avatar
Sylvain Thery committed
427 428 429
}

template <unsigned int ORBIT, unsigned int ADJ>
Sylvain Thery's avatar
Sylvain Thery committed
430
inline void GenericMap::disableQuickAdjacentTraversal()
Sylvain Thery's avatar
Sylvain Thery committed
431 432 433 434 435 436 437 438 439 440 441
{
	if(m_quickLocalAdjacentTraversal[ORBIT][ADJ] != NULL)
	{
		m_attribs[ORBIT].removeAttribute<Dart>(m_quickLocalAdjacentTraversal[ORBIT][ADJ]->getIndex()) ;
		m_quickLocalAdjacentTraversal[ORBIT][ADJ] = NULL ;
	}
}




Pierre Kraemer's avatar
Pierre Kraemer committed
442 443 444 445
/****************************************
 *        ATTRIBUTES MANAGEMENT         *
 ****************************************/

446 447 448 449 450
inline unsigned int GenericMap::getNbCells(unsigned int orbit)
{
	return m_attribs[orbit].size() ;
}

451 452
template <unsigned int ORBIT>
inline AttributeContainer& GenericMap::getAttributeContainer()
Pierre Kraemer's avatar
Pierre Kraemer committed
453
{
454
	return m_attribs[ORBIT] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
455 456
}

Sylvain Thery's avatar
Sylvain Thery committed
457 458 459 460 461 462
template <unsigned int ORBIT>
inline const AttributeContainer& GenericMap::getAttributeContainer() const
{
	return m_attribs[ORBIT] ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
463 464 465 466 467
inline AttributeContainer& GenericMap::getAttributeContainer(unsigned int orbit)
{
	return m_attribs[orbit] ;
}

Sylvain Thery's avatar
Sylvain Thery committed
468 469 470 471 472 473
inline const AttributeContainer& GenericMap::getAttributeContainer(unsigned int orbit) const
{
	return m_attribs[orbit] ;
}


474 475
template <unsigned int ORBIT>
inline AttributeMultiVector<Mark>* GenericMap::getMarkVector(unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
476
{
477
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded") ;
478
	return m_markTables[ORBIT][thread] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
479 480
}

481 482
template <unsigned int ORBIT>
inline AttributeMultiVector<unsigned int>* GenericMap::getEmbeddingAttributeVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
483
{
484
	return m_embeddings[ORBIT] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
485 486
}

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
template <typename R>
bool GenericMap::registerAttribute(const std::string &nameType)
{
	RegisteredBaseAttribute* ra = new RegisteredAttribute<R>;
	if (ra == NULL)
	{
		CGoGNerr << "Erreur enregistrement attribut" << CGoGNendl;
		return false;
	}

	ra->setTypeName(nameType);

	m_attributes_registry_map->insert(std::pair<std::string, RegisteredBaseAttribute*>(nameType,ra));
	return true;
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
/****************************************
 *   EMBEDDING ATTRIBUTES MANAGEMENT    *
 ****************************************/

template <unsigned int ORBIT>
void GenericMap::addEmbedding()
{
	assert(!isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit already embedded") ;

	std::ostringstream oss;
	oss << "EMB_" << ORBIT;

	AttributeContainer& dartCont = m_attribs[DART] ;
	AttributeMultiVector<unsigned int>* amv = dartCont.addAttribute<unsigned int>(oss.str()) ;
	m_embeddings[ORBIT] = amv ;

519 520 521
	// set new embedding to EMBNULL for all the darts of the map
	for(unsigned int i = dartCont.begin(); i < dartCont.end(); dartCont.next(i))
		(*amv)[i] = EMBNULL ;
522 523
}

Pierre Kraemer's avatar
Pierre Kraemer committed
524 525 526 527
/****************************************
 *           DARTS TRAVERSALS           *
 ****************************************/

528
template <unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
529
bool GenericMap::foreach_dart_of_orbit(Dart d, FunctorType& f, unsigned int thread) const
530 531 532 533 534 535 536 537 538 539 540 541 542
{
	switch(ORBIT)
	{
		case DART:		return f(d);
		case VERTEX: 	return foreach_dart_of_vertex(d, f, thread);
		case EDGE: 		return foreach_dart_of_edge(d, f, thread);
		case FACE: 		return foreach_dart_of_face(d, f, thread);
		case VOLUME: 	return foreach_dart_of_volume(d, f, thread);
		case VERTEX1: 	return foreach_dart_of_vertex1(d, f, thread);
		case EDGE1: 	return foreach_dart_of_edge1(d, f, thread);
		case VERTEX2: 	return foreach_dart_of_vertex2(d, f, thread);
		case EDGE2:		return foreach_dart_of_edge2(d, f, thread);
		case FACE2:		return foreach_dart_of_face2(d, f, thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
543
		default: 		assert(!"Cells of this dimension are not handled"); break;
544 545 546 547
	}
	return false;
}

548
template <typename MAP, unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
549
bool GenericMap::foreach_orbit(FunctorType& fonct, unsigned int thread) const
550
{
551
	TraversorCell<MAP, ORBIT> trav(*this, true, thread);
552 553 554 555 556 557 558 559 560 561
	bool found = false;

	for (Dart d = trav.begin(); !found && d != trav.end(); d = trav.next())
	{
		if ((fonct)(d))
			found = true;
	}
	return found;
}

562
template <typename MAP, unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
563
unsigned int GenericMap::getNbOrbits() const
564 565
{
	FunctorCount fcount;
566
	foreach_orbit<MAP, ORBIT>(fcount);
567 568 569
	return fcount.getNb();
}

570
template <typename MAP, unsigned int ORBIT, unsigned int INCIDENT>
Sylvain Thery's avatar
Sylvain Thery committed
571
unsigned int GenericMap::degree(Dart d) const
572
{
573
	assert(ORBIT != INCIDENT || !"degree does not manage adjacency counting") ;
Sylvain Thery's avatar
Sylvain Thery committed
574
	Traversor* t = TraversorFactory<MAP>::createIncident(*(reinterpret_cast<MAP*>(this)), d, dimension(), ORBIT, INCIDENT) ;
575 576
	FunctorCount fcount ;
	t->applyFunctor(fcount) ;
577
	delete t ;
578 579 580
	return fcount.getNb() ;
}

Sylvain Thery's avatar
Sylvain Thery committed
581 582 583 584 585 586 587 588 589 590 591
/****************************************
 *  TOPOLOGICAL ATTRIBUTES MANAGEMENT   *
 ****************************************/

inline AttributeMultiVector<Dart>* GenericMap::addRelation(const std::string& name)
{
	AttributeContainer& cont = m_attribs[DART] ;
	AttributeMultiVector<Dart>* amv = cont.addAttribute<Dart>(name) ;

	// set new relation to fix point for all the darts of the map
	for(unsigned int i = cont.begin(); i < cont.end(); cont.next(i))
Pierre Kraemer's avatar
Pierre Kraemer committed
592
		(*amv)[i] = i ;
Sylvain Thery's avatar
Sylvain Thery committed
593 594 595 596

	return amv ;
}

Sylvain Thery's avatar
Sylvain Thery committed
597 598 599 600 601 602 603
inline AttributeMultiVector<Dart>* GenericMap::getRelation(const std::string& name)
{
	AttributeContainer& cont = m_attribs[DART] ;
	AttributeMultiVector<Dart>* amv = cont.getDataVector<Dart>(cont.getAttributeIndex(name)) ;
	return amv ;
}

Sylvain Thery's avatar
Sylvain Thery committed
604 605 606 607 608

/**************************
 *  BOUNDARY MANAGEMENT   *
 **************************/

Thery Sylvain's avatar
Thery Sylvain committed
609
template <unsigned int D>
Sylvain Thery's avatar
Sylvain Thery committed
610 611
inline void GenericMap::boundaryMark(Dart d)
{
Thery Sylvain's avatar
Thery Sylvain committed
612
	m_markTables[DART][0]->operator[](dartIndex(d)).setMark(m_boundaryMarkers[D-2]);
Sylvain Thery's avatar
Sylvain Thery committed
613 614
}

Thery Sylvain's avatar
Thery Sylvain committed
615
template <unsigned int D>
Sylvain Thery's avatar
Sylvain Thery committed
616 617
inline void GenericMap::boundaryUnmark(Dart d)
{
Thery Sylvain's avatar
Thery Sylvain committed
618
	m_markTables[DART][0]->operator[](dartIndex(d)).unsetMark(m_boundaryMarkers[D-2]);
Sylvain Thery's avatar
Sylvain Thery committed
619 620
}

Thery Sylvain's avatar
Thery Sylvain committed
621
template <unsigned int D>
Sylvain Thery's avatar
Sylvain Thery committed
622 623
inline bool GenericMap::isBoundaryMarked(Dart d) const
{
Thery Sylvain's avatar
Thery Sylvain committed
624 625 626 627
	return m_markTables[DART][0]->operator[](dartIndex(d)).testMark(m_boundaryMarkers[D-2]);
}


628 629 630 631 632 633
inline bool GenericMap::isBoundaryMarkedCurrent(Dart d) const
{
	return m_markTables[DART][0]->operator[](dartIndex(d)).testMark(m_boundaryMarkers[this->dimension()-2]);
}


Thery Sylvain's avatar
Thery Sylvain committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
inline void GenericMap::boundaryMark2(Dart d)
{
	boundaryMark<2>(d);
}

inline void GenericMap::boundaryUnmark2(Dart d)
{
	boundaryUnmark<2>(d);
}

inline bool GenericMap::isBoundaryMarked2(Dart d) const
{
	return isBoundaryMarked<2>(d);
}

inline void GenericMap::boundaryMark3(Dart d)
{
	boundaryMark<3>(d);
}

inline void GenericMap::boundaryUnmark3(Dart d)
{
	boundaryUnmark<3>(d);
}

inline bool GenericMap::isBoundaryMarked3(Dart d) const
{
	return isBoundaryMarked<3>(d);
}

template <unsigned int ORBIT, unsigned int  DIM>
void GenericMap::boundaryMarkOrbit(Dart d)
{
	FunctorMark<GenericMap> fm(*this, m_boundaryMarkers[DIM-2], m_markTables[DART][0]) ;
	foreach_dart_of_orbit<ORBIT>(d, fm, 0) ;
}

template <unsigned int ORBIT, unsigned int DIM>
void GenericMap::boundaryUnmarkOrbit(Dart d)
{
	FunctorUnmark<GenericMap> fm(*this, m_boundaryMarkers[DIM-2], m_markTables[DART][0]) ;
	foreach_dart_of_orbit<ORBIT>(d, fm, 0) ;
}

template <unsigned int DIM>
void GenericMap::boundaryUnmarkAll()
{
	AttributeContainer& cont = getAttributeContainer<DART>() ;
	for (unsigned int i = cont.begin(); i != cont.end(); cont.next(i))
Thery Sylvain's avatar
Thery Sylvain committed
683
		m_markTables[DART][0]->operator[](i).unsetMark(m_boundaryMarkers[DIM-2]);
Sylvain Thery's avatar
Sylvain Thery committed
684 685
}

Thery Sylvain's avatar
Thery Sylvain committed
686

Pierre Kraemer's avatar
Pierre Kraemer committed
687
} //namespace CGoGN