ihm2.hpp 15.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 28 29 30 31 32 33 34 35 36 37 38
/*******************************************************************************
* 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
{

/***************************************************
 *             ATTRIBUTES MANAGEMENT               *
 ***************************************************/

template <typename T, unsigned int ORBIT>
AttributeHandler_IHM<T, ORBIT> ImplicitHierarchicalMap2::addAttribute(const std::string& nameAttr)
{
	bool addNextLevelCell = false ;
	if(!isOrbitEmbedded<ORBIT>())
		addNextLevelCell = true ;

39
	AttributeHandler<T, ORBIT, ImplicitHierarchicalMap2> h = TOPO_MAP::addAttribute<T, ORBIT>(nameAttr) ;
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

	if(addNextLevelCell)
	{
		AttributeContainer& cellCont = m_attribs[ORBIT] ;
		AttributeMultiVector<unsigned int>* amv = cellCont.addAttribute<unsigned int>("nextLevelCell") ;
		m_nextLevelCell[ORBIT] = amv ;
		for(unsigned int i = cellCont.begin(); i < cellCont.end(); cellCont.next(i))
			amv->operator[](i) = EMBNULL ;
	}

	return AttributeHandler_IHM<T, ORBIT>(this, h.getDataVector()) ;
}

template <typename T, unsigned int ORBIT>
AttributeHandler_IHM<T, ORBIT> ImplicitHierarchicalMap2::getAttribute(const std::string& nameAttr)
{
56
	AttributeHandler<T, ORBIT, ImplicitHierarchicalMap2> h = TOPO_MAP::getAttribute<T, ORBIT, ImplicitHierarchicalMap2>(nameAttr) ;
57 58 59 60 61 62
	return AttributeHandler_IHM<T, ORBIT>(this, h.getDataVector()) ;
}


inline void ImplicitHierarchicalMap2::update_topo_shortcuts()
{
63
//	TOPO_MAP::update_topo_shortcuts();
64 65
	m_dartLevel = TOPO_MAP::getAttribute<unsigned int, DART, ImplicitHierarchicalMap2>("dartLevel") ;
	m_edgeId = TOPO_MAP::getAttribute<unsigned int, DART, ImplicitHierarchicalMap2>("edgeId") ;
66 67 68 69 70 71 72 73 74 75 76

	//AttributeContainer& cont = m_attribs[DART] ;
	//m_nextLevelCell = cont.getDataVector<unsigned int>(cont.getAttributeIndex("nextLevelCell")) ;
}

/***************************************************
 *                 MAP TRAVERSAL                   *
 ***************************************************/

inline Dart ImplicitHierarchicalMap2::newDart()
{
77
	Dart d = TOPO_MAP::newDart() ;
78 79 80 81 82 83
	m_dartLevel[d] = m_curLevel ;
	if(m_curLevel > m_maxLevel)			// update max level
		m_maxLevel = m_curLevel ;		// if needed
	return d ;
}

Sylvain Thery's avatar
Sylvain Thery committed
84
inline Dart ImplicitHierarchicalMap2::phi1(Dart d) const
85 86
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
87
    bool finished = false ;
88 89 90 91
	unsigned int edgeId = m_edgeId[d] ;
	Dart it = d ;
	do
	{
92
		it = TOPO_MAP::phi1(it) ;
93 94 95 96 97
		if(m_dartLevel[it] <= m_curLevel)
			finished = true ;
		else
		{
			while(m_edgeId[it] != edgeId)
98
				it = TOPO_MAP::phi1(TOPO_MAP::phi2(it)) ;
99 100 101 102 103
		}
	} while(!finished) ;
	return it ;
}

Sylvain Thery's avatar
Sylvain Thery committed
104
inline Dart ImplicitHierarchicalMap2::phi_1(Dart d) const
105 106 107
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
	bool finished = false ;
108
	Dart it = TOPO_MAP::phi_1(d) ;
109 110 111 112 113 114 115
	unsigned int edgeId = m_edgeId[it] ;
	do
	{
		if(m_dartLevel[it] <= m_curLevel)
			finished = true ;
		else
		{
116
			it = TOPO_MAP::phi_1(it) ;
117
			while(m_edgeId[it] != edgeId)
118
				it = TOPO_MAP::phi_1(TOPO_MAP::phi2(it)) ;
119 120 121 122 123
		}
	} while(!finished) ;
	return it ;
}

Sylvain Thery's avatar
Sylvain Thery committed
124
inline Dart ImplicitHierarchicalMap2::phi2(Dart d) const
125 126
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
127
	if(TOPO_MAP::phi2(d) == d)
128
		return d ;
129
	return TOPO_MAP::phi2(TOPO_MAP::phi_1(phi1(d))) ;
130 131
}

Sylvain Thery's avatar
Sylvain Thery committed
132
inline Dart ImplicitHierarchicalMap2::alpha0(Dart d) const
133 134 135 136
{
	return phi2(d) ;
}

Sylvain Thery's avatar
Sylvain Thery committed
137
inline Dart ImplicitHierarchicalMap2::alpha1(Dart d) const
138
{
139
	return TOPO_MAP::alpha1(d) ;
140 141
}

Sylvain Thery's avatar
Sylvain Thery committed
142
inline Dart ImplicitHierarchicalMap2::alpha_1(Dart d) const
143
{
144
	return TOPO_MAP::alpha_1(d) ;
145 146 147 148
}

inline Dart ImplicitHierarchicalMap2::begin() const
{
149 150 151
	Dart d = TOPO_MAP::begin() ;
//	while(d != TOPO_MAP::end() && m_dartLevel[d] > m_curLevel)
//		TOPO_MAP::next(d) ;
152 153 154 155 156
	return d ;
}

inline Dart ImplicitHierarchicalMap2::end() const
{
157
	return TOPO_MAP::end() ;
158 159 160 161
}

inline void ImplicitHierarchicalMap2::next(Dart& d) const
{
162 163 164 165 166 167 168
//	do
//	{
//		TOPO_MAP::next(d) ;
//	} while(d != TOPO_MAP::end() && m_dartLevel[d] > m_curLevel) ;
	TOPO_MAP::next(d) ;
	if(m_dartLevel[d] > m_curLevel)
		d = TOPO_MAP::end() ;
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
template <unsigned int ORBIT, typename FUNC>
void ImplicitHierarchicalMap2::foreach_dart_of_orbit(Cell<ORBIT> c, FUNC f, unsigned int thread) const
{
	switch(ORBIT)
	{
		case DART:		f(c); break;
		case VERTEX: 	foreach_dart_of_vertex(c, f, thread); break;
		case EDGE: 		foreach_dart_of_edge(c, f, thread); break;
		case FACE: 		foreach_dart_of_face(c, f, thread); break;
		case VOLUME: 	foreach_dart_of_volume(c, f, thread); break;
		case VERTEX1: 	foreach_dart_of_vertex1(c, f, thread); break;
		case EDGE1: 	foreach_dart_of_edge1(c, f, thread); break;
		default: 		assert(!"Cells of this dimension are not handled"); break;
	}
}

template <unsigned int ORBIT, typename FUNC>
void ImplicitHierarchicalMap2::foreach_dart_of_orbit(Cell<ORBIT> c, FUNC& f, unsigned int thread) const
{
	switch(ORBIT)
	{
		case DART:		f(c); break;
		case VERTEX: 	foreach_dart_of_vertex(c, f, thread); break;
		case EDGE: 		foreach_dart_of_edge(c, f, thread); break;
		case FACE: 		foreach_dart_of_face(c, f, thread); break;
		case VOLUME: 	foreach_dart_of_volume(c, f, thread); break;
		case VERTEX1: 	foreach_dart_of_vertex1(c, f, thread); break;
		case EDGE1: 	foreach_dart_of_edge1(c, f, thread); break;
		default: 		assert(!"Cells of this dimension are not handled"); break;
	}
}

template <typename FUNC>
inline void ImplicitHierarchicalMap2::foreach_dart_of_vertex(Dart d, FUNC& f, unsigned int /*thread*/) const
205 206 207 208
{
	Dart dNext = d;
	do
	{
209
		f(dNext);
210
		dNext = alpha1(dNext);
211
	} while (dNext != d);
212 213
}

214 215
template <typename FUNC>
inline void ImplicitHierarchicalMap2::foreach_dart_of_edge(Dart d, FUNC& f, unsigned int /*thread*/) const
216
{
217
	f(d);
218 219
	Dart d2 = phi2(d);
	if (d2 != d)
220
		f(d2);
221 222
}

223 224
template <typename FUNC>
inline void ImplicitHierarchicalMap2::foreach_dart_of_oriented_face(Dart d, FUNC& f, unsigned int /*thread*/) const
225 226 227 228
{
	Dart dNext = d ;
	do
	{
229
		f(dNext);
230 231 232 233
		dNext = phi1(dNext) ;
	} while (dNext != d) ;
}

234 235
template <typename FUNC>
inline void ImplicitHierarchicalMap2::foreach_dart_of_face(Dart d, FUNC& f, unsigned int thread) const
236
{
237
	foreach_dart_of_oriented_face(d, f, thread) ;
238 239
}

240 241
template <typename FUNC>
inline void ImplicitHierarchicalMap2::foreach_dart_of_oriented_volume(Dart d, FUNC& f, unsigned int thread) const
242
{
Pierre Kraemer's avatar
Pierre Kraemer committed
243
	DartMarkerStore<Map2> mark(*this, thread);	// Lock a marker
244 245 246 247 248 249

	std::list<Dart> visitedFaces;	// Faces that are traversed
	visitedFaces.push_back(d);		// Start with the face of d
	std::list<Dart>::iterator face;

	// For every face added to the list
250
	for (face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
251 252 253 254
	{
		if (!mark.isMarked(*face))		// Face has not been visited yet
		{
			// Apply functor to the darts of the face
255
			foreach_dart_of_oriented_face(*face, f, thread);
256

257
			// mark visited darts (current face)
258
			// and add non visited adjacent faces to the list of face
259 260
			Dart dNext = *face ;
			do
261
			{
262 263 264 265 266 267
				mark.mark(dNext);					// Mark
				Dart adj = phi2(dNext);				// Get adjacent face
				if (adj != dNext && !mark.isMarked(adj))
					visitedFaces.push_back(adj);	// Add it
				dNext = phi1(dNext);
			} while(dNext != *face);
268 269 270 271
		}
	}
}

272 273
template <typename FUNC>
inline void ImplicitHierarchicalMap2::foreach_dart_of_volume(Dart d, FUNC& f, unsigned int thread) const
274
{
275
	foreach_dart_of_oriented_volume(d, f, thread) ;
276 277
}

278
template <typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
279
inline void ImplicitHierarchicalMap2::foreach_dart_of_vertex1(Dart d, FUNC& f, unsigned int /*thread*/) const
280 281 282 283 284
{
	f(d);
}

template <typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
285
inline void ImplicitHierarchicalMap2::foreach_dart_of_edge1(Dart d, FUNC& f, unsigned int /*thread*/) const
286 287 288 289 290 291
{
	f(d);
}

template <typename FUNC>
inline void ImplicitHierarchicalMap2::foreach_dart_of_cc(Dart d, FUNC& f, unsigned int thread) const
292
{
293
	foreach_dart_of_oriented_volume(d, f, thread) ;
294 295 296 297 298 299 300 301 302 303 304 305 306
}

/***************************************************
 *               MAP MANIPULATION                  *
 ***************************************************/

inline void ImplicitHierarchicalMap2::splitFace(Dart d, Dart e)
{
	EmbeddedMap2::splitFace(d, e) ;
	if(isOrbitEmbedded<FACE>())
	{
		unsigned int cur = m_curLevel ;
		m_curLevel = m_maxLevel ;
307 308
		Algo::Topo::setOrbitEmbedding<FACE>(*this, d, this->getEmbedding<FACE>(d)) ;
		Algo::Topo::setOrbitEmbedding<FACE>(*this, e, this->getEmbedding<FACE>(e)) ;
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
		m_curLevel = cur ;
	}
}

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

inline unsigned int ImplicitHierarchicalMap2::getCurrentLevel()
{
	return m_curLevel ;
}

inline void ImplicitHierarchicalMap2::setCurrentLevel(unsigned int l)
{
	m_curLevel = l ;
}

inline void ImplicitHierarchicalMap2::incCurrentLevel()
{
	if(m_curLevel < m_maxLevel)
		++m_curLevel ;
	else
		CGoGNout << "incCurrentLevel : already at maximum resolution level" << CGoGNendl ;
}

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

inline unsigned int ImplicitHierarchicalMap2::getMaxLevel()
{
	return m_maxLevel ;
}

inline unsigned int ImplicitHierarchicalMap2::getDartLevel(Dart d)
{
	return m_dartLevel[d] ;
}

inline void ImplicitHierarchicalMap2::setDartLevel(Dart d, unsigned int l)
{
	m_dartLevel[d] = l ;
}

358 359 360 361 362 363
inline void ImplicitHierarchicalMap2::setMaxLevel(unsigned int l)
{
	m_maxLevel = l;
}


364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
/***************************************************
 *             EDGE ID MANAGEMENT                  *
 ***************************************************/

inline unsigned int ImplicitHierarchicalMap2::getNewEdgeId()
{
	return m_idCount++ ;
}

inline unsigned int ImplicitHierarchicalMap2::getEdgeId(Dart d)
{
	return m_edgeId[d] ;
}

inline void ImplicitHierarchicalMap2::setEdgeId(Dart d, unsigned int i)
{
	m_edgeId[d] = i ;
}

383 384 385 386 387 388 389 390 391 392 393 394
inline unsigned int ImplicitHierarchicalMap2::getTriRefinementEdgeId(Dart d)
{
	unsigned int dId = getEdgeId(phi_1(d));
	unsigned int eId = getEdgeId(phi1(d));

	unsigned int id = dId + eId;

	if(id == 0)
		return 1;
	else if(id == 1)
		return 2;
	else if(id == 2)
untereiner's avatar
untereiner committed
395
	{
396 397 398 399
		if(dId == eId)
			return 0;
		else
			return 1;
untereiner's avatar
untereiner committed
400 401
	}

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
	//else if(id == 3)
	return 0;
}

inline unsigned int ImplicitHierarchicalMap2::getQuadRefinementEdgeId(Dart d)
{
	unsigned int eId = getEdgeId(phi1(d));

	if(eId == 0)
		return 1;

	//else if(eId == 1)
	return 0;
}

417 418 419 420
/***************************************************
 *               CELLS INFORMATION                 *
 ***************************************************/

421
inline unsigned int ImplicitHierarchicalMap2::faceDegree(Dart d)
422
{
423 424 425 426 427 428 429 430
    unsigned int count = 0 ;
    Dart it = d ;
    do
    {
        ++count ;
        it = phi1(it) ;
    } while (it != d) ;
    return count ;
431 432
}

433 434 435


inline unsigned int ImplicitHierarchicalMap2::vertexInsertionLevel(Dart d)
436 437
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
438
	return m_dartLevel[d] ;
439 440
}

441 442 443 444 445 446 447 448 449
//inline unsigned int ImplicitHierarchicalMap2::edgeLevel(Dart d)
//{
//	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
//	unsigned int ld = m_dartLevel[d] ;
////	unsigned int ldd = m_dartLevel[phi2(d)] ;	// the level of an edge is the maximum of the
//	unsigned int ldd = m_dartLevel[phi1(d)] ;
//	return ld < ldd ? ldd : ld ;				// insertion levels of its two darts
//}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
/***************************************************
 *               ATTRIBUTE HANDLER                 *
 ***************************************************/

template <typename T, unsigned int ORBIT>
T& AttributeHandler_IHM<T, ORBIT>::operator[](Dart d)
{
	ImplicitHierarchicalMap2* m = reinterpret_cast<ImplicitHierarchicalMap2*>(this->m_map) ;
	assert(m->m_dartLevel[d] <= m->m_curLevel || !"Access to a dart introduced after current level") ;
	assert(m->vertexInsertionLevel(d) <= m->m_curLevel || !"Access to the embedding of a vertex inserted after current level") ;

	unsigned int orbit = this->getOrbit() ;
	unsigned int nbSteps = m->m_curLevel - m->vertexInsertionLevel(d) ;
	unsigned int index = m->getEmbedding<ORBIT>(d) ;

	if(index == EMBNULL)
	{
467
		index = Algo::Topo::setOrbitEmbeddingOnNewCell<ORBIT>(m, d) ;
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
		m->m_nextLevelCell[orbit]->operator[](index) = EMBNULL ;
	}

	AttributeContainer& cont = m->getAttributeContainer<ORBIT>() ;
	unsigned int step = 0 ;
	while(step < nbSteps)
	{
		step++ ;
		unsigned int nextIdx = m->m_nextLevelCell[orbit]->operator[](index) ;
		if (nextIdx == EMBNULL)
		{
			nextIdx = m->newCell<ORBIT>() ;
			m->copyCell<ORBIT>(nextIdx, index) ;
			m->m_nextLevelCell[orbit]->operator[](index) = nextIdx ;
			m->m_nextLevelCell[orbit]->operator[](nextIdx) = EMBNULL ;
			cont.refLine(index) ;
		}
		index = nextIdx ;
	}
	return this->m_attrib->operator[](index);
}

template <typename T, unsigned int ORBIT>
const T& AttributeHandler_IHM<T, ORBIT>::operator[](Dart d) const
{
	ImplicitHierarchicalMap2* m = reinterpret_cast<ImplicitHierarchicalMap2*>(this->m_map) ;
	assert(m->m_dartLevel[d] <= m->m_curLevel || !"Access to a dart introduced after current level") ;
	assert(m->vertexInsertionLevel(d) <= m->m_curLevel || !"Access to the embedding of a vertex inserted after current level") ;

	unsigned int orbit = this->getOrbit() ;
	unsigned int nbSteps = m->m_curLevel - m->vertexInsertionLevel(d) ;
	unsigned int index = m->getEmbedding<ORBIT>(d) ;

	unsigned int step = 0 ;
	while(step < nbSteps)
	{
		step++ ;
		unsigned int next = m->m_nextLevelCell[orbit]->operator[](index) ;
		if(next != EMBNULL) index = next ;
		else break ;
	}
	return this->m_attrib->operator[](index);
}


} //namespace CGoGN