mapMulti.hpp 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

Pierre Kraemer's avatar
Pierre Kraemer committed
28 29 30 31 32 33 34 35 36 37 38 39
inline void MapMulti::clear(bool removeAttrib)
{
	GenericMap::clear(removeAttrib);
	if (removeAttrib)
	{
		m_permutation.clear();
		m_permutation_inv.clear();
		m_involution.clear();
	}
	initMR();
}

40 41 42 43
/****************************************
 *          DARTS MANAGEMENT            *
 ****************************************/

44
inline Dart MapMulti::newDart()
45 46 47 48 49 50 51
{
	Dart d = GenericMap::newDart() ;

	unsigned int mrdi = m_mrattribs.insertLine() ;	// insert a new MRdart line
	(*m_mrLevels)[mrdi] = m_mrCurrentLevel ;		// set the introduction level of the dart
	m_mrNbDarts[m_mrCurrentLevel]++ ;

Pierre Kraemer's avatar
Pierre Kraemer committed
52 53 54 55 56 57 58 59 60
	Dart mrd = Dart::create(mrdi);

	for (unsigned int i = 0; i < m_permutation.size(); ++i)
		(*m_permutation[i])[d.index] = mrd ;
	for (unsigned int i = 0; i < m_permutation_inv.size(); ++i)
		(*m_permutation_inv[i])[d.index] = mrd ;
	for (unsigned int i = 0; i < m_involution.size(); ++i)
		(*m_involution[i])[d.index] = mrd ;

61 62 63 64 65 66
	for(unsigned int i = 0; i < m_mrCurrentLevel; ++i)	// for all previous levels
		(*m_mrDarts[i])[mrdi] = MRNULL ;				// this MRdart does not exist

	for(unsigned int i = m_mrCurrentLevel; i < m_mrDarts.size(); ++i)	// for all levels from current to max
		(*m_mrDarts[i])[mrdi] = d.index ;								// make this MRdart point to the new dart line

Pierre Kraemer's avatar
Pierre Kraemer committed
67
	return mrd ;
68 69
}

70
inline void MapMulti::deleteDart(Dart d)
71
{
72 73
	unsigned int index = dartIndex(d);
/*
74 75 76 77 78 79 80 81 82 83 84 85 86
	if(getDartLevel(d) > m_mrCurrentLevel)
	{
		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) ;
		}

		(*m_mrDarts[m_mrCurrentLevel])[d.index] = MRNULL ;
		return;
	}
87
*/
88 89 90
	// a MRdart can only be deleted on its insertion level
	if(getDartLevel(d) == m_mrCurrentLevel)
	{
91 92
//		if(isDartValid(d))
//		{
93 94 95
			deleteDartLine(index) ;
			m_mrattribs.removeLine(d.index);
			m_mrNbDarts[m_mrCurrentLevel]--;
96
//		}
97 98 99 100
	}
	else
	{
		unsigned int di = (*m_mrDarts[m_mrCurrentLevel - 1])[d.index];
101
		// si le brin de niveau i pointe sur un autre brin que le niveau i-1w
102 103
		if(di != index)
		{
104
//			if(isDartValid(d))//index))
105 106 107 108
				deleteDartLine(index) ;
		}

		for(unsigned int i = m_mrCurrentLevel; i <= getMaxLevel(); ++i) // for all levels from current to max
109
			(*m_mrDarts[i])[d.index] = di ; // copy the index from previous level
110 111 112
	}
}

113
inline unsigned int MapMulti::dartIndex(Dart d) const
114 115 116 117
{
	return (*m_mrDarts[m_mrCurrentLevel])[d.index] ;
}

118 119 120 121 122
inline Dart MapMulti::indexDart(unsigned int index) const
{
	return Dart( (*m_mrDarts[m_mrCurrentLevel])[index] ) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
123
inline unsigned int MapMulti::getNbInsertedDarts(unsigned int level) const
124 125 126 127 128 129 130
{
	if(level < m_mrDarts.size())
		return m_mrNbDarts[level] ;
	else
		return 0 ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
131
inline unsigned int MapMulti::getNbDarts(unsigned int level) const
132 133 134 135 136 137 138 139 140 141 142 143
{
	if(level < m_mrDarts.size())
	{
		unsigned int nb = 0 ;
		for(unsigned int i = 0; i <= level; ++i)
			nb += m_mrNbDarts[i] ;
		return nb ;
	}
	else
		return 0 ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
144
inline unsigned int MapMulti::getNbDarts() const
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
	return getNbDarts(m_mrCurrentLevel) ;
}

inline unsigned int MapMulti::getDartLevel(Dart d) const
{
	return (*m_mrLevels)[d.index] ;
}

inline void MapMulti::incDartLevel(Dart d) const
{
	++((*m_mrLevels)[d.index]) ;
}

inline void MapMulti::duplicateDart(Dart d)
{
	assert(getDartLevel(d) <= m_mrCurrentLevel || !"duplicateDart : called with a dart inserted after current level") ;

	if(getDartLevel(d) == m_mrCurrentLevel)	// no need to duplicate
		return ;							// a dart from its insertion level

	unsigned int oldindex = dartIndex(d) ;

	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
	}

	unsigned int newindex = copyDartLine(oldindex) ;

	for(unsigned int i = m_mrCurrentLevel; i <= getMaxLevel(); ++i) // for all levels from current to max
	{
		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
	}
}

inline void MapMulti::duplicateDartAtOneLevel(Dart d, unsigned int level)
{
	(*m_mrDarts[level])[d.index] = copyDartLine(dartIndex(d)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
188
inline AttributeContainer& MapMulti::getDartContainer()
189 190 191 192
{
	return m_mrattribs;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
/****************************************
 *        RELATIONS MANAGEMENT          *
 ****************************************/

inline void MapMulti::addInvolution()
{
	std::stringstream sstm;
	sstm << "involution_" << m_involution.size();
	m_involution.push_back(addRelation(sstm.str()));
}

inline void MapMulti::addPermutation()
{
	std::stringstream sstm;
	sstm << "permutation_" << m_permutation.size();
	m_permutation.push_back(addRelation(sstm.str()));
	std::stringstream sstm2;
	sstm2 << "permutation_inv_" << m_permutation_inv.size();
	m_permutation_inv.push_back(addRelation(sstm2.str()));
}

Pierre Kraemer's avatar
Pierre Kraemer committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
inline AttributeMultiVector<Dart>* MapMulti::getInvolutionAttribute(unsigned int i)
{
	if (i < m_involution.size())
		return m_involution[i];
	else
		return NULL;
}

inline AttributeMultiVector<Dart>* MapMulti::getPermutationAttribute(unsigned int i)
{
	if (i < m_permutation.size())
		return m_permutation[i];
	else
		return NULL;
}

inline AttributeMultiVector<Dart>* MapMulti::getPermutationInvAttribute(unsigned int i)
{
	if (i < m_permutation_inv.size())
		return m_permutation_inv[i];
	else
		return NULL;
}

238
template <int I>
239
inline Dart MapMulti::getInvolution(Dart d) const
240 241 242 243 244
{
	return (*m_involution[I])[dartIndex(d)];
}

template <int I>
245
inline Dart MapMulti::getPermutation(Dart d) const
246 247 248 249 250
{
	return (*m_permutation[I])[dartIndex(d)];
}

template <int I>
251
inline Dart MapMulti::getPermutationInv(Dart d) const
252 253 254 255
{
	return (*m_permutation_inv[I])[dartIndex(d)];
}

Pierre Kraemer's avatar
Pierre Kraemer committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
template <int I>
inline void MapMulti::involutionSew(Dart d, Dart e)
{
	assert((*m_involution[I])[dartIndex(d)] == d) ;
	assert((*m_involution[I])[dartIndex(e)] == e) ;
	(*m_involution[I])[dartIndex(d)] = e ;
	(*m_involution[I])[dartIndex(e)] = d ;
}

template <int I>
inline void MapMulti::involutionUnsew(Dart d)
{
	unsigned int d_index = dartIndex(d);
	Dart e = (*m_involution[I])[d_index] ;
	(*m_involution[I])[d_index] = d ;
	(*m_involution[I])[dartIndex(e)] = e ;
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
template <int I>
inline void MapMulti::permutationSew(Dart d, Dart e)
{
	unsigned int d_index = dartIndex(d);
	unsigned int e_index = dartIndex(e);
	Dart f = (*m_permutation[I])[d_index] ;
	Dart g = (*m_permutation[I])[e_index] ;
	(*m_permutation[I])[d_index] = g ;
	(*m_permutation[I])[e_index] = f ;
	(*m_permutation_inv[I])[dartIndex(g)] = d ;
	(*m_permutation_inv[I])[dartIndex(f)] = e ;
}

template <int I>
inline void MapMulti::permutationUnsew(Dart d)
{
	unsigned int d_index = dartIndex(d);
	Dart e = (*m_permutation[I])[d_index] ;
	unsigned int e_index = dartIndex(e);
	Dart f = (*m_permutation[I])[e_index] ;
	(*m_permutation[I])[d_index] = f ;
	(*m_permutation[I])[e_index] = e ;
	(*m_permutation_inv[I])[dartIndex(f)] = d ;
	(*m_permutation_inv[I])[e_index] = e ;
}

300
inline void MapMulti::compactTopo()
Pierre Kraemer's avatar
Pierre Kraemer committed
301
{
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	std::vector<unsigned int> oldnewMR;
	m_mrattribs.compact(oldnewMR);

	std::vector<unsigned int> oldnew;
	m_attribs[DART].compact(oldnew);

	unsigned int nbl = m_mrDarts.size();
	for (unsigned int i = m_mrattribs.begin(); i != m_mrattribs.end(); m_mrattribs.next(i))
	{
		for (unsigned int level = 0; level < nbl; ++level)
		{
			unsigned int& d = m_mrDarts[level]->operator[](i);
			if (d != oldnew[d])
				d = oldnew[d];
		}
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
319 320 321 322 323
	for (unsigned int i = m_attribs[DART].begin(); i != m_attribs[DART].end(); m_attribs[DART].next(i))
	{
		for (unsigned int j = 0; j < m_permutation.size(); ++j)
		{
			Dart d = (*m_permutation[j])[i];
324 325
			if (d.index != oldnewMR[d.index])
				(*m_permutation[j])[i] = Dart(oldnewMR[d.index]);
Pierre Kraemer's avatar
Pierre Kraemer committed
326 327 328 329
		}
		for (unsigned int j = 0; j < m_permutation_inv.size(); ++j)
		{
			Dart d = (*m_permutation_inv[j])[i];
330 331
			if (d.index != oldnewMR[d.index])
				(*m_permutation_inv[j])[i] = Dart(oldnewMR[d.index]);
Pierre Kraemer's avatar
Pierre Kraemer committed
332 333 334 335
		}
		for (unsigned int j = 0; j < m_involution.size(); ++j)
		{
			Dart d = (*m_involution[j])[i];
336 337
			if (d.index != oldnewMR[d.index])
				(*m_involution[j])[i] = Dart(oldnewMR[d.index]);
Pierre Kraemer's avatar
Pierre Kraemer committed
338 339 340 341
		}
	}
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 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
/****************************************
 *      MR CONTAINER MANAGEMENT         *
 ****************************************/

inline AttributeContainer& MapMulti::getMRAttributeContainer()
{
	return m_mrattribs ;
}

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

inline AttributeMultiVector<unsigned int>* MapMulti::getMRLevelAttributeVector()
{
	return m_mrLevels ;
}

/****************************************
 *     RESOLUTION LEVELS MANAGEMENT     *
 ****************************************/

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

inline void MapMulti::setCurrentLevel(unsigned int l)
{
	if(l < m_mrDarts.size())
		m_mrCurrentLevel = l ;
	else
		CGoGNout << "setCurrentLevel : try to access nonexistent resolution level" << CGoGNendl ;
}

inline void MapMulti::incCurrentLevel()
{
	if(m_mrCurrentLevel < m_mrDarts.size() - 1)
		++m_mrCurrentLevel ;
	else
		CGoGNout << "incCurrentLevel : already at maximum resolution level" << CGoGNendl ;
}

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

inline void MapMulti::pushLevel()
{
	m_mrLevelStack.push_back(m_mrCurrentLevel) ;
}

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

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

/****************************************
 *           DARTS TRAVERSALS           *
 ****************************************/

415
inline Dart MapMulti::begin() const
416 417 418 419 420 421 422 423 424 425
{
	unsigned int d = m_mrattribs.begin() ;
	if(d != m_mrattribs.end())
	{
		while (getDartLevel(d) > m_mrCurrentLevel)
			m_mrattribs.next(d) ;
	}
	return Dart::create(d) ;
}

426
inline Dart MapMulti::end() const
427 428 429 430
{
	return Dart::create(m_mrattribs.end()) ;
}

431
inline void MapMulti::next(Dart& d) const
432 433 434 435 436 437 438
{
	do
	{
		m_mrattribs.next(d.index) ;
	} while (d.index != m_mrattribs.end() && getDartLevel(d) > m_mrCurrentLevel) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
439 440 441 442 443 444 445 446 447 448
inline bool MapMulti::foreach_dart(FunctorType& f)
{
	for (Dart d = begin(); d != end(); next(d))
	{
		if (f(d))
			return true;
	}
	return false;
}

449
} // namespace CGoGN