genericmap.hpp 18.8 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 27
#include "Topology/generic/dartmarker.h"
#include "Topology/generic/traversorCell.h"

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

31 32 33 34 35 36 37 38 39 40 41 42 43 44
/****************************************
 *           MULTIRES                   *
 ****************************************/

inline unsigned int GenericMap::getCurrentLevel()
{
	return m_mrCurrentLevel ;
}

inline void GenericMap::setCurrentLevel(unsigned int l)
{
	if(l < m_mrDarts.size())
		m_mrCurrentLevel = l ;
	else
Pierre Kraemer's avatar
Pierre Kraemer committed
45
		CGoGNout << "setCurrentLevel : try to access nonexistent resolution level" << CGoGNendl ;
46 47
}

Pierre Kraemer's avatar
Pierre Kraemer committed
48 49
inline void GenericMap::incCurrentLevel()
{
50
	if(m_mrCurrentLevel < m_mrDarts.size() - 1)
Pierre Kraemer's avatar
Pierre Kraemer committed
51 52 53 54 55 56 57 58 59 60 61 62 63
		++m_mrCurrentLevel ;
	else
		CGoGNout << "incCurrentLevel : already at maximum resolution level" << CGoGNendl ;
}

inline void GenericMap::decCurrentLevel()
{
	if(m_mrCurrentLevel > 0)
		--m_mrCurrentLevel ;
	else
		CGoGNout << "decCurrentLevel : already at minimum resolution level" << CGoGNendl ;
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
inline void GenericMap::pushLevel()
{
	m_mrLevelStack.push_back(m_mrCurrentLevel) ;
}

inline void GenericMap::popLevel()
{
	m_mrCurrentLevel = m_mrLevelStack.back() ;
	m_mrLevelStack.pop_back() ;
}

inline unsigned int GenericMap::getMaxLevel()
{
	return m_mrDarts.size() - 1 ;
}

80

Pierre Kraemer's avatar
Pierre Kraemer committed
81 82 83 84 85 86
/****************************************
 *           DARTS MANAGEMENT           *
 ****************************************/

inline Dart GenericMap::newDart()
{
87
	unsigned int di = m_attribs[DART].insertLine();		// insert a new dart line
Pierre Kraemer's avatar
Pierre Kraemer committed
88
	for(unsigned int i = 0; i < NB_ORBITS; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
89
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91
		if (m_embeddings[i])								// set all its embeddings
			(*m_embeddings[i])[di] = EMBNULL ;				// to EMBNULL
Pierre Kraemer's avatar
Pierre Kraemer committed
92
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
93

Pierre Kraemer's avatar
Pierre Kraemer committed
94 95
	if (m_isMultiRes)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97
		unsigned int mrdi = m_mrattribs.insertLine() ;		// insert a new MRdart line
		(*m_mrLevels)[mrdi] = m_mrCurrentLevel ;			// set the introduction level of the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
98
		m_mrNbDarts[m_mrCurrentLevel]++ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
99

Pierre Kraemer's avatar
Pierre Kraemer committed
100
		for(unsigned int i = 0; i < m_mrCurrentLevel; ++i)	// for all previous levels
101
			(*m_mrDarts[i])[mrdi] = MRNULL ;					// this MRdart does not exist
Pierre Kraemer's avatar
Pierre Kraemer committed
102

untereiner's avatar
untereiner committed
103 104
		for(unsigned int i = m_mrCurrentLevel; i < m_mrDarts.size(); ++i)	// for all levels from current to max
  			(*m_mrDarts[i])[mrdi] = di ;									// make this MRdart point to the new dart line
105

Pierre Kraemer's avatar
Pierre Kraemer committed
106 107
		return Dart::create(mrdi) ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
108

Pierre Kraemer's avatar
Pierre Kraemer committed
109
	return Dart::create(di) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111 112 113
}

inline void GenericMap::deleteDart(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
114 115
	if(m_isMultiRes)
	{
untereiner's avatar
untereiner committed
116
		unsigned int index = (*m_mrDarts[m_mrCurrentLevel])[d.index] ;
117

118
		if(getDartLevel(d) > m_mrCurrentLevel)
119
		{
120 121 122 123 124 125 126 127
			unsigned int di = (*m_mrDarts[m_mrCurrentLevel + 1])[d.index];
			// si le brin de niveau i pointe sur le meme brin que le niveau i-1
			if(di != index)
			{
				if(isDartValid(d))//index))
					deleteDartLine(index) ;
			}

untereiner's avatar
untereiner committed
128 129
			(*m_mrDarts[m_mrCurrentLevel])[d.index] = MRNULL ;
			return;
130
		}
untereiner's avatar
untereiner committed
131

132 133 134
		// a MRdart can only be deleted on its insertion level
		if(getDartLevel(d) == m_mrCurrentLevel)
		{
135
			if(isDartValid(d))//index))
136
				deleteDartLine(index) ;
untereiner's avatar
untereiner committed
137 138 139

			m_mrattribs.removeLine(d.index);
			m_mrNbDarts[m_mrCurrentLevel]--;
140
		}
untereiner's avatar
untereiner committed
141 142
		else
		{
143
			unsigned int di = (*m_mrDarts[m_mrCurrentLevel - 1])[d.index];
144
			// si le brin de niveau i pointe sur le meme brin que le niveau i-1
untereiner's avatar
untereiner committed
145 146
			if(di != index)
			{
147
				if(isDartValid(d))//index))
untereiner's avatar
untereiner committed
148 149 150
					deleteDartLine(index) ;
			}

151 152 153 154 155
			for(unsigned int i = m_mrCurrentLevel; i <= getMaxLevel(); ++i) // for all levels from current to max
			{
				(*m_mrDarts[i])[d.index] = di ; //copy the index from previous level
			}
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157 158 159 160 161 162 163 164 165
	}
	else
		deleteDartLine(dartIndex(d)) ;
}

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
166
		(*m_markTables[DART][t])[index].clear() ;		// the removed dart
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
167

Pierre Kraemer's avatar
Pierre Kraemer committed
168 169
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
170
		if (m_embeddings[orbit])									// for each embedded orbit
Pierre Kraemer's avatar
Pierre Kraemer committed
171
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
172
			unsigned int emb = (*m_embeddings[orbit])[index] ;		// get the embedding of the dart
Pierre Kraemer's avatar
Pierre Kraemer committed
173
			if(emb != EMBNULL)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
174
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
175
				if(m_attribs[orbit].unrefLine(emb))					// unref the pointed embedding line
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
176
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
177
					for (unsigned int t = 0; t < m_nbThreads; ++t)	// and clear its markers if it was
178
						(*m_markTables[orbit][t])[emb].clear() ;		// its last unref (and was thus freed)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
179 180
				}
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
183
}
Pierre Kraemer's avatar
Pierre Kraemer committed
184

Pierre Kraemer's avatar
Pierre Kraemer committed
185
inline unsigned int GenericMap::copyDartLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
186
{
187 188
	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
189 190
	for(unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
191 192 193 194 195 196
		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
197 198
	}
	return newindex ;
Pierre Kraemer's avatar
Pierre Kraemer committed
199 200
}

Pierre Kraemer's avatar
Pierre Kraemer committed
201 202
inline void GenericMap::duplicateDart(Dart d)
{
203
	assert(getDartLevel(d) <= m_mrCurrentLevel || !"duplicateDart : called with a dart inserted after current level") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
204

205
	if(getDartLevel(d) == m_mrCurrentLevel)	// no need to duplicate
Pierre Kraemer's avatar
Pierre Kraemer committed
206 207 208 209
		return ;								// a dart from its insertion level

	unsigned int oldindex = dartIndex(d) ;

210 211 212 213 214
	if(m_mrCurrentLevel > 0)
	{
		if((*m_mrDarts[m_mrCurrentLevel - 1])[d.index] != oldindex)	// no need to duplicate if the dart is already
			return ;												// duplicated with respect to previous level
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
215 216 217

	unsigned int newindex = copyDartLine(oldindex) ;

218
	for(unsigned int i = m_mrCurrentLevel; i <= getMaxLevel(); ++i) // for all levels from current to max
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220 221 222 223 224
	{
		assert((*m_mrDarts[i])[d.index] == oldindex || !"duplicateDart : dart was already duplicated on a greater level") ;
		(*m_mrDarts[i])[d.index] = newindex ;						// make this MRdart points to the new dart line
	}
}

untereiner's avatar
untereiner committed
225
inline void GenericMap::duplicateDartAtOneLevel(Dart d, unsigned int level)
226
{
untereiner's avatar
untereiner committed
227
	(*m_mrDarts[level])[d.index] = copyDartLine(dartIndex(d)) ;
228 229
}

Sylvain Thery's avatar
Sylvain Thery committed
230
inline unsigned int GenericMap::dartIndex(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
231
{
Pierre Kraemer's avatar
Pierre Kraemer committed
232 233 234 235 236
	if (m_isMultiRes)
		return (*m_mrDarts[m_mrCurrentLevel])[d.index] ;
	return d.index;
}

237
inline unsigned int GenericMap::getDartLevel(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
238 239 240 241
{
	return (*m_mrLevels)[d.index] ;
}

242 243
inline void GenericMap::incDartLevel(Dart d) const
{
244
	++((*m_mrLevels)[d.index]) ;
245 246 247
}


Pierre Kraemer's avatar
Pierre Kraemer committed
248 249 250 251 252 253 254 255 256 257 258
inline unsigned int GenericMap::getNbInsertedDarts(unsigned int level)
{
	if(level < m_mrDarts.size())
		return m_mrNbDarts[level] ;
	else
		return 0 ;
}

inline unsigned int GenericMap::getNbDarts(unsigned int level)
{
	if(level < m_mrDarts.size())
Pierre Kraemer's avatar
Pierre Kraemer committed
259
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
260 261 262 263
		unsigned int nb = 0 ;
		for(unsigned int i = 0; i <= level; ++i)
			nb += m_mrNbDarts[i] ;
		return nb ;
Pierre Kraemer's avatar
Pierre Kraemer committed
264
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
265 266 267 268 269 270 271 272 273
	else
		return 0 ;
}

inline unsigned int GenericMap::getNbDarts()
{
	if(m_isMultiRes)
		return getNbDarts(m_mrCurrentLevel) ;

274
	return m_attribs[DART].size() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
275 276
}

Pierre Kraemer's avatar
Pierre Kraemer committed
277 278 279 280 281
inline bool GenericMap::isDartValid(Dart d)
{
	return !d.isNil() && m_attribs[DART].used(dartIndex(d)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
282 283 284 285
/****************************************
 *         EMBEDDING MANAGEMENT         *
 ****************************************/

286 287
template <unsigned int ORBIT>
inline bool GenericMap::isOrbitEmbedded() const
Pierre Kraemer's avatar
Pierre Kraemer committed
288
{
289
	return (ORBIT == DART) || (m_embeddings[ORBIT] != NULL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
290 291
}

292
inline bool GenericMap::isOrbitEmbedded(unsigned int orbit) const
Pierre Kraemer's avatar
Pierre Kraemer committed
293
{
294
	return (orbit == DART) || (m_embeddings[orbit] != NULL) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
295 296
}

297 298
template <unsigned int ORBIT>
inline unsigned int GenericMap::getEmbedding(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
299
{
300
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
Pierre Kraemer's avatar
Pierre Kraemer committed
301

302 303
	unsigned int d_index = dartIndex(d);

304
	if (ORBIT == DART)
305
		return d_index;
Pierre Kraemer's avatar
Pierre Kraemer committed
306

307
	return (*m_embeddings[ORBIT])[d_index] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
308 309
}

310 311 312
template <unsigned int ORBIT>
void GenericMap::setDartEmbedding(Dart d, unsigned int emb)
{
313
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

	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
332
	(*m_embeddings[ORBIT])[dartIndex(d)] = emb ; // finally affect the embedding to the dart
333 334
}

335 336
template <unsigned int ORBIT>
inline void GenericMap::copyDartEmbedding(Dart dest, Dart src)
Pierre Kraemer's avatar
Pierre Kraemer committed
337
{
338
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
339
	setDartEmbedding<ORBIT>(dest, getEmbedding<ORBIT>(src));
Pierre Kraemer's avatar
Pierre Kraemer committed
340 341
}

342 343
template <unsigned int ORBIT>
inline unsigned int GenericMap::newCell()
Pierre Kraemer's avatar
Pierre Kraemer committed
344
{
345
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
346
	return m_attribs[ORBIT].insertLine();
Pierre Kraemer's avatar
Pierre Kraemer committed
347 348
}

349 350
template <unsigned int ORBIT>
inline void GenericMap::embedOrbit(Dart d, unsigned int em)
Pierre Kraemer's avatar
Pierre Kraemer committed
351
{
352
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
353 354
	FunctorSetEmb<GenericMap, ORBIT> fsetemb(*this, em);
	foreach_dart_of_orbit<ORBIT>(d, fsetemb);
Pierre Kraemer's avatar
Pierre Kraemer committed
355 356
}

357 358
template <unsigned int ORBIT>
inline unsigned int GenericMap::embedNewCell(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
359
{
360
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
361 362
	unsigned int em = newCell<ORBIT>();
	embedOrbit<ORBIT>(d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
363 364 365
	return em;
}

366 367
template <unsigned int ORBIT>
inline void GenericMap::copyCell(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
368
{
369
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
370 371
	unsigned int dE = getEmbedding<ORBIT>(d) ;
	unsigned int eE = getEmbedding<ORBIT>(e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
372 373 374
	if(eE != EMBNULL)	// if the source is NULL, nothing to copy
	{
		if(dE == EMBNULL)	// if the dest is NULL, create a new cell
375 376
			dE = embedNewCell<ORBIT>(d) ;
		m_attribs[ORBIT].copyLine(dE, eE) ;	// copy the data
Pierre Kraemer's avatar
Pierre Kraemer committed
377 378 379
	}
}

380 381
template <unsigned int ORBIT>
inline void GenericMap::copyCell(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
382
{
383
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
384
	m_attribs[ORBIT].copyLine(i, j) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
385 386
}

387 388
template <unsigned int ORBIT>
inline void GenericMap::initCell(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
389
{
390
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded");
391
	m_attribs[ORBIT].initLine(i) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
392 393
}

Pierre Kraemer's avatar
Pierre Kraemer committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/****************************************
 *     QUICK TRAVERSAL MANAGEMENT       *
 ****************************************/

template <unsigned int ORBIT>
inline void GenericMap::enableQuickTraversal()
{
	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>
inline void GenericMap::updateQuickTraversal()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
413 414 415
	assert(m_quickTraversal[ORBIT] != NULL || !"updateQuickTraversal on a disabled orbit") ;

	CellMarker<ORBIT> cm(*this) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
416 417 418 419 420
	for(Dart d = begin(); d != end(); next(d))
	{
		if(!cm.isMarked(d))
		{
			cm.mark(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
421
			(*m_quickTraversal[ORBIT])[getEmbedding<ORBIT>(d)] = d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
		}
	}
}

template <unsigned int ORBIT>
inline AttributeMultiVector<Dart>* GenericMap::getQuickTraversal()
{
	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 ;
	}
}

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

446 447
template <unsigned int ORBIT>
inline AttributeContainer& GenericMap::getAttributeContainer()
Pierre Kraemer's avatar
Pierre Kraemer committed
448
{
449
	return m_attribs[ORBIT] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
450 451
}

452 453
template <unsigned int ORBIT>
inline AttributeMultiVector<Mark>* GenericMap::getMarkVector(unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
454
{
455
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded") ;
456
	return m_markTables[ORBIT][thread] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
457 458
}

459 460
template <unsigned int ORBIT>
inline AttributeMultiVector<unsigned int>* GenericMap::getEmbeddingAttributeVector()
Pierre Kraemer's avatar
Pierre Kraemer committed
461
{
462
	return m_embeddings[ORBIT] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
463 464
}

465 466 467 468 469 470 471 472 473 474 475
inline AttributeContainer& GenericMap::getMRAttributeContainer()
{
	return m_mrattribs ;
}

inline AttributeMultiVector<unsigned int>* GenericMap::getMRDartAttributeVector(unsigned int level)
{
	assert(level <= getMaxLevel() || !"Invalid parameter: level does not exist");
	return m_mrDarts[level] ;
}

untereiner's avatar
untereiner committed
476 477 478 479 480
inline AttributeMultiVector<unsigned int>* GenericMap::getMRLevelAttributeVector()
{
	return m_mrLevels ;
}

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
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;
}

template <unsigned int ORBIT>
void GenericMap::initOrbitEmbedding(bool realloc)
{
500
	assert(isOrbitEmbedded<ORBIT>() || !"Invalid parameter: orbit not embedded") ;
501 502 503 504 505 506 507 508 509 510 511 512
	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)
				embedNewCell<ORBIT>(d) ;
		}
	}
}

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
/****************************************
 *   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 ;

	// 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 ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
534 535 536 537
/****************************************
 *           DARTS TRAVERSALS           *
 ****************************************/

538
inline Dart GenericMap::begin() const
Pierre Kraemer's avatar
Pierre Kraemer committed
539
{
540 541 542 543 544 545 546 547 548
	if (m_isMultiRes)
	{
		unsigned int d = m_mrattribs.begin() ;
		while (getDartLevel(d) > m_mrCurrentLevel)
			m_mrattribs.next(d) ;
		return Dart::create(d) ;
	}

	return Dart::create(m_attribs[DART].begin()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
549 550
}

551
inline Dart GenericMap::end() const
Pierre Kraemer's avatar
Pierre Kraemer committed
552
{
553 554 555
	if (m_isMultiRes)
		return Dart::create(m_mrattribs.end()) ;

556
	return Dart::create(m_attribs[DART].end()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
557 558
}

559
inline void GenericMap::next(Dart& d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
560
{
561 562
	if (m_isMultiRes)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
563 564
		do
		{
565
			m_mrattribs.next(d.index) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
566
		} while (d.index != m_mrattribs.end() && getDartLevel(d) > m_mrCurrentLevel) ;
567 568 569
	}
	else
		m_attribs[DART].next(d.index) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
570 571
}

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
template <unsigned int ORBIT>
bool GenericMap::foreach_dart_of_orbit(Dart d, FunctorType& f, unsigned int thread)
{
	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
587
		default: 		assert(!"Cells of this dimension are not handled"); break;
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
	}
	return false;
}

template <unsigned int ORBIT>
bool GenericMap::foreach_orbit(FunctorType& fonct, const FunctorSelect& good, unsigned int thread)
{
	TraversorCell<GenericMap, ORBIT> trav(*this, good, true, thread);
	bool found = false;

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

template <unsigned int ORBIT>
unsigned int GenericMap::getNbOrbits(const FunctorSelect& good)
{
	FunctorCount fcount;
	foreach_orbit<ORBIT>(fcount, good);
	return fcount.getNb();
}

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

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

Sylvain Thery's avatar
Sylvain Thery committed
628 629 630 631 632 633 634 635 636 637 638
/****************************************
 *  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
639
		(*amv)[i] = i ;
Sylvain Thery's avatar
Sylvain Thery committed
640 641 642 643

	return amv ;
}

Sylvain Thery's avatar
Sylvain Thery committed
644 645 646 647 648 649 650
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
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

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

inline void GenericMap::boundaryMark(Dart d)
{
	m_markTables[DART][0]->operator[](dartIndex(d)).setMark(m_boundaryMarker);
}

inline void GenericMap::boundaryUnmark(Dart d)
{
	m_markTables[DART][0]->operator[](dartIndex(d)).unsetMark(m_boundaryMarker);
}

inline bool GenericMap::isBoundaryMarked(Dart d) const
{
	return m_markTables[DART][0]->operator[](dartIndex(d)).testMark(m_boundaryMarker);
}

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