mapMulti.hpp 11.1 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 28 29 30 31
/*******************************************************************************
* 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
{

/****************************************
 *          DARTS MANAGEMENT            *
 ****************************************/

32
inline Dart MapMulti::newDart()
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
{
	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]++ ;

	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

	pushLevel() ;
	for(unsigned int i = m_mrCurrentLevel;  i < m_mrDarts.size(); ++i)
	{
		setCurrentLevel(i) ;

		unsigned int d_index = dartIndex(d) ;

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

	return Dart::create(mrdi) ;
}

65
inline void MapMulti::deleteDart(Dart d)
66
{
67 68
	unsigned int index = dartIndex(d);
/*
69 70 71 72 73 74 75 76 77 78 79 80 81
	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;
	}
82
*/
83 84 85
	// a MRdart can only be deleted on its insertion level
	if(getDartLevel(d) == m_mrCurrentLevel)
	{
86 87
//		if(isDartValid(d))
//		{
88 89 90
			deleteDartLine(index) ;
			m_mrattribs.removeLine(d.index);
			m_mrNbDarts[m_mrCurrentLevel]--;
91
//		}
92 93 94 95
	}
	else
	{
		unsigned int di = (*m_mrDarts[m_mrCurrentLevel - 1])[d.index];
96
		// si le brin de niveau i pointe sur un autre brin que le niveau i-1w
97 98
		if(di != index)
		{
99
//			if(isDartValid(d))//index))
100 101 102 103
				deleteDartLine(index) ;
		}

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

108
inline unsigned int MapMulti::dartIndex(Dart d) const
109 110 111 112
{
	return (*m_mrDarts[m_mrCurrentLevel])[d.index] ;
}

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

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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
inline unsigned int MapMulti::getNbInsertedDarts(unsigned int level)
{
	if(level < m_mrDarts.size())
		return m_mrNbDarts[level] ;
	else
		return 0 ;
}

inline unsigned int MapMulti::getNbDarts(unsigned int level)
{
	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 ;
}

inline unsigned int MapMulti::getNbDarts()
{
	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
183
inline AttributeContainer& MapMulti::getDartContainer()
184 185 186 187
{
	return m_mrattribs;
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/****************************************
 *        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()));
}

template <int I>
210
inline Dart MapMulti::getInvolution(Dart d) const
211 212 213 214 215
{
	return (*m_involution[I])[dartIndex(d)];
}

template <int I>
216
inline Dart MapMulti::getPermutation(Dart d) const
217 218 219 220 221
{
	return (*m_permutation[I])[dartIndex(d)];
}

template <int I>
222
inline Dart MapMulti::getPermutationInv(Dart d) const
223 224 225 226
{
	return (*m_permutation_inv[I])[dartIndex(d)];
}

Pierre Kraemer's avatar
Pierre Kraemer committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
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 ;
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
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 ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
inline void MapMulti::compactTopoRelations(const std::vector<unsigned int>& oldnew)
{
	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];
			if (d.index != oldnew[d.index])
				(*m_permutation[j])[i] = Dart(oldnew[d.index]);
		}
		for (unsigned int j = 0; j < m_permutation_inv.size(); ++j)
		{
			Dart d = (*m_permutation_inv[j])[i];
			if (d.index != oldnew[d.index])
				(*m_permutation_inv[j])[i] = Dart(oldnew[d.index]);
		}
		for (unsigned int j = 0; j < m_involution.size(); ++j)
		{
			Dart d = (*m_involution[j])[i];
			if (d.index != oldnew[d.index])
				(*m_involution[j])[i] = Dart(oldnew[d.index]);
		}
	}
}

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 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
/****************************************
 *      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           *
 ****************************************/

369
inline Dart MapMulti::begin() const
370 371 372 373 374 375 376 377 378 379
{
	unsigned int d = m_mrattribs.begin() ;
	if(d != m_mrattribs.end())
	{
		while (getDartLevel(d) > m_mrCurrentLevel)
			m_mrattribs.next(d) ;
	}
	return Dart::create(d) ;
}

380
inline Dart MapMulti::end() const
381 382 383 384
{
	return Dart::create(m_mrattribs.end()) ;
}

385
inline void MapMulti::next(Dart& d) const
386 387 388 389 390 391 392 393
{
	do
	{
		m_mrattribs.next(d.index) ;
	} while (d.index != m_mrattribs.end() && getDartLevel(d) > m_mrCurrentLevel) ;
}

} // namespace CGoGN