ihm3.hpp 16.3 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

untereiner's avatar
untereiner committed
31
namespace IHM
32 33
{

untereiner's avatar
untereiner committed
34
/***************************************************
untereiner's avatar
untereiner committed
35
 *  		       	    ATTRIBUTES MANAGEMENT    	           	 *
untereiner's avatar
untereiner committed
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 65
 ***************************************************/

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

	AttributeHandler<T> h = Map3::addAttribute<T>(orbit, nameAttr) ;

	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>(this, h.getDataVector()) ;
}

template <typename T>
AttributeHandler_IHM<T> ImplicitHierarchicalMap3::getAttribute(unsigned int orbit, const std::string& nameAttr)
{
	AttributeHandler<T> h = Map2::getAttribute<T>(orbit, nameAttr) ;
	return AttributeHandler_IHM<T>(this, h.getDataVector()) ;
}

66
/***************************************************
untereiner's avatar
untereiner committed
67
 *          			 	    MAP TRAVERSAL         		  		         *
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
 ***************************************************/

inline Dart ImplicitHierarchicalMap3::newDart()
{
	Dart d = Map3::newDart() ;
	m_dartLevel[d] = m_curLevel ;
	if(m_curLevel > m_maxLevel)			// update max level
		m_maxLevel = m_curLevel ;		// if needed
	return d ;
}

inline Dart ImplicitHierarchicalMap3::phi1(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
	bool finished = false ;

	unsigned int edgeId = m_edgeId[d] ;
	Dart it = d ;
untereiner's avatar
untereiner committed
86

87 88
	do
	{
untereiner's avatar
untereiner committed
89

90 91 92 93 94
		it = Map3::phi1(it) ;
		if(m_dartLevel[it] <= m_curLevel)
			finished = true ;
		else
		{
95
			while(m_edgeId[it] != edgeId)
96
				it = Map3::phi1(phi2bis(it));
untereiner's avatar
untereiner committed
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		}
	} while(!finished) ;

	return it ;
}

inline Dart ImplicitHierarchicalMap3::phi_1(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
	bool finished = false ;

	Dart it = Map3::phi_1(d) ;
	unsigned int edgeId = m_edgeId[it] ;
	do
	{
		if(m_dartLevel[it] <= m_curLevel)
			finished = true ;
		else
		{
			it = Map3::phi_1(it) ;
118
			while(m_edgeId[it] != edgeId)
untereiner's avatar
untereiner committed
119
				it = Map3::phi_1(phi2bis(it));
120 121 122 123 124 125 126 127 128 129 130 131 132
		}
	} while(!finished) ;

	return it ;
}

//TODO A verifier si besoin d'assertion
inline Dart ImplicitHierarchicalMap3::phi2bis(Dart d)
{
	unsigned int faceId = m_faceId[d];
	Dart it = d;

	it = Map3::phi2(it) ;
untereiner's avatar
untereiner committed
133

134 135 136 137
	if(m_faceId[it] == faceId)
		return it;
	else
	{
untereiner's avatar
untereiner committed
138 139
		do
		{
140
			it = Map3::phi2(Map3::phi3(it));
untereiner's avatar
untereiner committed
141 142
		}
		while(m_faceId[it] != faceId);
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

		return it;
	}
}

inline Dart ImplicitHierarchicalMap3::phi2(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;

	return Map3::phi2(Map3::phi_1(phi1(d))) ;
}

inline Dart ImplicitHierarchicalMap3::phi3(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;

	if(Map3::phi3(d) == d)
		return d;

	return Map3::phi3(Map3::phi_1(phi1(d)));
}

inline Dart ImplicitHierarchicalMap3::alpha0(Dart d)
{
	return phi3(d) ;
}

inline Dart ImplicitHierarchicalMap3::alpha1(Dart d)
{
172
	//assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
173 174 175 176 177 178

	return Map3::alpha1(d) ;
}

inline Dart ImplicitHierarchicalMap3::alpha2(Dart d)
{
untereiner's avatar
untereiner committed
179 180 181 182 183 184 185
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;

	Dart e = phi2(d);
	Dart f = phi3(e);

	if (f != e)
		return f;
186

untereiner's avatar
untereiner committed
187 188 189 190 191 192 193 194 195 196
	f = d;
	e = phi3(f);
	while (e != f)
	{
		f = phi2(e);
		e = phi3(f);
	}
	return f;

	//return Map3::alpha2(d);
197 198 199 200
}

inline Dart ImplicitHierarchicalMap3::alpha_2(Dart d)
{
untereiner's avatar
untereiner committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;

	Dart e = phi3(d);

	if (e != d)
		return phi2(e);

	e = d;
	Dart f = phi2(d);
	while (phi3(f) != f)
	{
		e = phi3(f);
		f = phi2(e);
	}
	return e;
216

untereiner's avatar
untereiner committed
217
	//return Map3::alpha_2(d);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
}

inline Dart ImplicitHierarchicalMap3::begin()
{
	Dart d = Map3::begin() ;
	while(m_dartLevel[d] > m_curLevel)
		Map3::next(d) ;
	return d ;
}

inline Dart ImplicitHierarchicalMap3::end()
{
	return Map3::end() ;
}

inline void ImplicitHierarchicalMap3::next(Dart& d)
{
	do
	{
		Map3::next(d) ;
238
	} while(d != Map3::end() && m_dartLevel[d] > m_curLevel) ;
239 240
}

241
inline bool ImplicitHierarchicalMap3::foreach_dart_of_vertex(Dart d, FunctorType& f, unsigned int thread)
242 243 244 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 271 272 273 274 275 276 277 278
{
	DartMarkerStore mv(*this);			// Lock a marker
	bool found = false;					// Last functor return value

	std::list<Dart> darts_list;			//Darts that are traversed
	darts_list.push_back(d);			//Start with the dart d
	std::list<Dart>::iterator darts;

	mv.mark(d);

	for(darts = darts_list.begin(); !found && darts != darts_list.end() ; ++darts)
	{
		Dart dc = *darts;

		//add phi21 and phi23 successor if they are not marked yet
		Dart d2 = phi2(dc);
		Dart d21 = phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume

		if(!mv.isMarked(d21))
		{
			darts_list.push_back(d21);
			mv.mark(d21);
		}

		if((d23!=d2) && !mv.isMarked(d23))
		{
			darts_list.push_back(d23);
			mv.mark(d23);
		}

		found = f(dc);
	}

	return found;
}

279
inline bool ImplicitHierarchicalMap3::foreach_dart_of_edge(Dart d, FunctorType& f, unsigned int thread)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
{

	Dart dNext = d;
	do {

		if (f(dNext))
			return true;

		Dart d2 = phi2(dNext);
		if (d2 != dNext)
			return f(d2);
		else
			return false;

		dNext = alpha2(dNext);
	} while (dNext != d);
	return false;
}

299
inline bool ImplicitHierarchicalMap3::foreach_dart_of_oriented_face(Dart d, FunctorType& f, unsigned int thread)
300 301 302 303 304 305 306 307 308 309 310
{
	Dart dNext = d ;
	do
	{
		if (f(dNext))
			return true ;
		dNext = phi1(dNext) ;
	} while (dNext != d) ;
	return false ;
}

311
inline bool ImplicitHierarchicalMap3::foreach_dart_of_face(Dart d, FunctorType& f, unsigned int thread)
312 313 314 315 316 317 318 319
{
	if (foreach_dart_of_oriented_face(d,f)) return true;

	Dart d3 = phi3(d);
	if (d3 != d) return foreach_dart_of_oriented_face(d3,f);
	return false;
}

320
inline bool ImplicitHierarchicalMap3::foreach_dart_of_oriented_volume(Dart d, FunctorType& f, unsigned int thread)
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
{
	DartMarkerStore mark(*this);	// Lock a marker
	bool found = false;				// Last functor return value

	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
	for (face = visitedFaces.begin(); !found && face != visitedFaces.end(); ++face)
	{
		if (!mark.isMarked(*face))		// Face has not been visited yet
		{
			// Apply functor to the darts of the face
			found = foreach_dart_of_oriented_face(*face, f);

			// If functor returns false then mark visited darts (current face)
			// and add non visited adjacent faces to the list of face
			if (!found)
			{
				Dart dNext = *face ;
				do
				{
					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);
			}
		}
	}
	return found;
}

356
inline bool ImplicitHierarchicalMap3::foreach_dart_of_volume(Dart d, FunctorType& f, unsigned int thread)
357 358 359 360
{
	return foreach_dart_of_oriented_volume(d, f) ;
}

361
inline bool ImplicitHierarchicalMap3::foreach_dart_of_cc(Dart d, FunctorType& f, unsigned int thread)
362 363 364 365
{
	return foreach_dart_of_oriented_volume(d, f) ;
}

untereiner's avatar
untereiner committed
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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
/**************************************************
 *       		       EMBEDDED FUNCTIONS      		           *
 **************************************************/
//void ImplicitHierarchicalMap3::cutEdge(Dart d)
//{
//	Dart dd = phi2(d) ;
//	EmbeddedMap3<Map3>::cutEdge(d);
//
//	//
//	//mise a jour de l'id d'arrete sur chaque moitie d'arete
//	//
//	unsigned int eId;
//
//	//Test si id existe
//	if(m_edgeId[d] == EMBNULL)
//	{
//		eId = getNewEdgeId();
//	}
//	else
//	{
//		eId = getEdgeId(d) ;
//	}
//
//	//tour de la 1ere moitie de l'arete avec modification
//	Dart e = d;
//	do
//	{
//		m_edgeId[e] = eId; //cas ou l'arete n'est pas du tout plonge
//		m_edgeId[Map3::phi2(e)] = eId;
//
//		e = Map3::alpha2(e);
//	} while(e != d);
//
//
//	 e = dd;
//	do
//	{
//		m_edgeId[e] = eId;
//		m_edgeId[Map3::phi2(e)] = eId;
//
//		e = Map3::alpha2(e);
//	} while(e != dd);
//
//
//	//
//	//mise a jour de l'id de face sur chaque brin de chaque moitie d'arete
//	//
//
//}
//
//void ImplicitHierarchicalMap3::splitFace(Dart d, Dart e)
//{
//	//
//	//Verification que les brins precedents ceux que l'on split n'ont pas le meme id
//	//
//	if(m_edgeId[d] == m_edgeId[Map3::phi_1(d)])
//	{
//		unsigned int eId = getNewEdgeId() ;
//
//		Dart e = d;
//		do
//		{
//			m_edgeId[e] = eId; //cas ou l'arete n'est pas du tout plonge
//			m_edgeId[Map3::phi2(e)] = eId;
//
//			e = Map3::alpha2(e);
//		} while(e != d);
//	}
//
//	EmbeddedMap3<Map3>::splitFace(d,e);
//
//	//
//	//creation d'un id d'arete sur la nouvelle arete
//	//
//
//	unsigned int eId = getNewEdgeId() ;
//	Dart a =Map3:: phi_1(d);
//
//	do
//	{
//		m_edgeId[a] = eId;
//		m_edgeId[Map3::phi2(a)] = eId;
//
//		a = Map3::alpha2(a);
//	} while(a != Map3::phi_1(d));
//
//}
//
//void ImplicitHierarchicalMap3::sewVolumes(Dart d, Dart e)
//{
//
//}

459 460 461 462 463 464 465 466 467 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
/***************************************************
 *              LEVELS MANAGEMENT                  *
 ***************************************************/

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

inline void ImplicitHierarchicalMap3::setCurrentLevel(unsigned int l)
{
	assert(l >= 0 || !"Trying to set current level to a negative value") ;
	m_curLevel = l ;
}

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

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

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

/***************************************************
 *             EDGE ID MANAGEMENT                  *
 ***************************************************/

inline unsigned int ImplicitHierarchicalMap3::getNewEdgeId()
{
	return m_edgeIdCount++ ;
}


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

inline void ImplicitHierarchicalMap3::setEdgeId(Dart d, unsigned int i, unsigned int orbit)
{
506
	if(orbit == EDGE)
507 508 509 510 511 512 513 514 515 516 517
	{
		Dart e = d;

		do
		{
			m_edgeId[e] = i;
			m_edgeId[Map3::phi2(e)] = i;

			e = Map3::alpha2(e);
		} while(e != d);
	}
518
	else if(orbit == DART)
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
	{
		m_edgeId[d] = i;
	}
}

/***************************************************
 *             FACE ID MANAGEMENT                  *
 ***************************************************/

inline unsigned int ImplicitHierarchicalMap3::getNewFaceId()
{
	return m_faceIdCount++;
}

inline unsigned int ImplicitHierarchicalMap3::getFaceId(Dart d)
{
	return m_faceId[d] ;
}

538
inline void ImplicitHierarchicalMap3::setFaceId(unsigned int orbit, Dart d)
539 540
{
	//Mise a jour de l'id de face pour les brins autour d'une arete
541
	if(orbit == EDGE)
542 543 544 545 546
	{
		Dart e = d;

		do
		{
547
			m_faceId[Map3::phi1(e)] = m_faceId[e];
548 549 550 551 552

			e = Map3::alpha2(e);
		}while(e != d);

	}
553 554 555 556 557
}

inline void ImplicitHierarchicalMap3::setFaceId(Dart d, unsigned int i, unsigned int orbit)
{

558
	//Mise a jour de l'id de face pour les brins autour de la face
559
	if(orbit == FACE)
560 561 562 563 564 565 566 567 568 569 570 571 572 573
	{
		Dart e = d;

		do
		{
			m_faceId[e] = i;

			Dart e3 = Map3::phi3(e);
			if(e3 != e)
				m_faceId[e3] = i;

			e = Map3::phi1(e);
		} while(e != d);
	}
574
	else if(orbit == DART)
575 576
	{
		m_faceId[d] = i;
577 578 579

		if(Map3::phi3(d) != d)
			m_faceId[Map3::phi3(d)] = i;
580 581 582 583 584 585 586 587 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
	}
}

/***************************************************
 *               CELLS INFORMATION                 *
 ***************************************************/

//TODO
inline unsigned int ImplicitHierarchicalMap3::vertexInsertionLevel(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;
	return m_dartLevel[d] ;
}

inline unsigned int ImplicitHierarchicalMap3::edgeLevel(Dart d)
{
	assert(m_dartLevel[d] <= m_curLevel || !"Access to a dart introduced after current level") ;

	// the level of an edge is the maximum of the
	// insertion levels of its darts

	unsigned int r = 0;

	Dart e = d;
	do
	{
		unsigned int ld = m_dartLevel[e] ;
		unsigned int ldd = m_dartLevel[phi2(e)] ;
		unsigned int temp = ld < ldd ? ldd : ld;

		r =  r < temp ? temp : r ;

		e = alpha2(e);
	} while(e != d);

	return r;
}

untereiner's avatar
untereiner committed
618 619 620 621 622 623 624 625 626 627 628 629 630
/***************************************************
 *               ATTRIBUTE HANDLER                 *
 ***************************************************/

template <typename T>
T& AttributeHandler_IHM<T>::operator[](Dart d)
{
	ImplicitHierarchicalMap3* m = reinterpret_cast<ImplicitHierarchicalMap3*>(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) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
631
	unsigned int index = m->getEmbedding(orbit, d) ;
untereiner's avatar
untereiner committed
632 633 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

	if(index == EMBNULL)
	{
		index = m->embedNewCell(orbit, d) ;
		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>
const T& AttributeHandler_IHM<T>::operator[](Dart d) const
{
	ImplicitHierarchicalMap3* m = reinterpret_cast<ImplicitHierarchicalMap3*>(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) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
667
	unsigned int index = m->getEmbedding(orbit, d) ;
untereiner's avatar
untereiner committed
668 669 670 671 672 673 674 675 676 677 678

	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);
}
679 680 681 682 683 684

} //namespace IHM

} //namespace Algo

} //namespace CGoGN