gmap3.hpp 27.5 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
#include "Topology/generic/traversor3.h"

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

Pierre Kraemer's avatar
Pierre Kraemer committed
30 31
template <class MAP>
inline void GMap3<MAP>::init()
32
{
Pierre Kraemer's avatar
Pierre Kraemer committed
33
	MAP::addInvolution() ;
34
}
Pierre Kraemer's avatar
Pierre Kraemer committed
35

Pierre Kraemer's avatar
Pierre Kraemer committed
36 37
template <class MAP>
inline GMap3<MAP>::GMap3() : GMap2<MAP>()
Pierre Kraemer's avatar
Pierre Kraemer committed
38
{
39
	init() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
40 41
}

Pierre Kraemer's avatar
Pierre Kraemer committed
42 43
template <class MAP>
inline std::string GMap3<MAP>::mapTypeName() const
Pierre Kraemer's avatar
Pierre Kraemer committed
44 45 46 47
{
	return "GMap3";
}

Pierre Kraemer's avatar
Pierre Kraemer committed
48 49
template <class MAP>
inline unsigned int GMap3<MAP>::dimension() const
Pierre Kraemer's avatar
Pierre Kraemer committed
50 51 52 53
{
	return 3;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
54 55
template <class MAP>
inline void GMap3<MAP>::clear(bool removeAttrib)
56
{
Pierre Kraemer's avatar
Pierre Kraemer committed
57
	ParentMap::clear(removeAttrib) ;
Sylvain Thery's avatar
Sylvain Thery committed
58 59
	if (removeAttrib)
		init() ;
60
}
Pierre Kraemer's avatar
Pierre Kraemer committed
61

Pierre Kraemer's avatar
Pierre Kraemer committed
62 63
template <class MAP>
inline void GMap3<MAP>::update_topo_shortcuts()
Sylvain Thery's avatar
Sylvain Thery committed
64
{
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66
	ParentMap::update_topo_shortcuts();
//	m_beta3 = getRelation("beta3");
Sylvain Thery's avatar
Sylvain Thery committed
67 68
}

Pierre Kraemer's avatar
Pierre Kraemer committed
69 70 71 72
/*! @name Basic Topological Operators
 * Access and Modification
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
73 74
template <class MAP>
inline Dart GMap3<MAP>::beta3(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
75
{
76
	return MAP::template getInvolution<3>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
77 78
}

Pierre Kraemer's avatar
Pierre Kraemer committed
79
template <class MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
80
template <int N>
Pierre Kraemer's avatar
Pierre Kraemer committed
81
inline Dart GMap3<MAP>::beta(const Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
82 83 84 85 86 87
{
	assert( (N > 0) || !"negative parameters not allowed in template multi-beta");
	if (N<10)
	{
		switch(N)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89 90 91 92
			case 0 : return this->beta0(d) ;
			case 1 : return this->beta1(d) ;
			case 2 : return this->beta2(d) ;
			case 3 : return beta3(d) ;
			default : assert(!"Wrong multi-beta relation value") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
93 94 95 96
		}
	}
	switch(N%10)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
97 98 99 100 101
		case 0 : return beta0(beta<N/10>(d)) ;
		case 1 : return beta1(beta<N/10>(d)) ;
		case 2 : return beta2(beta<N/10>(d)) ;
		case 3 : return beta3(beta<N/10>(d)) ;
		default : assert(!"Wrong multi-beta relation value") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103 104
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
105 106
template <class MAP>
inline Dart GMap3<MAP>::phi3(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
107
{
Pierre Kraemer's avatar
Pierre Kraemer committed
108
	return beta3(this->beta0(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
109 110
}

Pierre Kraemer's avatar
Pierre Kraemer committed
111
template <class MAP>
Pierre Kraemer's avatar
Pierre Kraemer committed
112
template <int N>
Pierre Kraemer's avatar
Pierre Kraemer committed
113
inline Dart GMap3<MAP>::phi(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
114 115 116 117 118 119
{
	assert( (N >0) || !"negative parameters not allowed in template multi-phi");
	if (N<10)
	{
		switch(N)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121 122 123
			case 1 : return this->phi1(d) ;
			case 2 : return this->phi2(d) ;
			case 3 : return phi3(d) ;
			default : assert(!"Wrong multi-phi relation value") ; return d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126 127
		}
	}
	switch(N%10)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
128 129 130 131
		case 1 : return phi1(phi<N/10>(d)) ;
		case 2 : return phi2(phi<N/10>(d)) ;
		case 3 : return phi3(phi<N/10>(d)) ;
		default : assert(!"Wrong multi-phi relation value") ; return d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
132 133 134
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
135 136
template <class MAP>
inline Dart GMap3<MAP>::alpha0(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
137
{
Pierre Kraemer's avatar
Pierre Kraemer committed
138
	return beta3(this->beta0(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
139
}
Pierre Kraemer's avatar
Pierre Kraemer committed
140

Pierre Kraemer's avatar
Pierre Kraemer committed
141 142
template <class MAP>
inline Dart GMap3<MAP>::alpha1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
143
{
Pierre Kraemer's avatar
Pierre Kraemer committed
144
	return beta3(this->beta1(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
145 146
}

Pierre Kraemer's avatar
Pierre Kraemer committed
147 148
template <class MAP>
inline Dart GMap3<MAP>::alpha2(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
149
{
Pierre Kraemer's avatar
Pierre Kraemer committed
150
	return beta3(this->beta2(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
151 152
}

Pierre Kraemer's avatar
Pierre Kraemer committed
153 154
template <class MAP>
inline Dart GMap3<MAP>::alpha_2(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
155 156 157
{
	return beta2(beta3(d)) ;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
158

Pierre Kraemer's avatar
Pierre Kraemer committed
159 160
template <class MAP>
inline void GMap3<MAP>::beta3sew(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
161
{
162
	MAP::template involutionSew<3>(d,e);
Pierre Kraemer's avatar
Pierre Kraemer committed
163 164
}

Pierre Kraemer's avatar
Pierre Kraemer committed
165 166
template <class MAP>
inline void GMap3<MAP>::beta3unsew(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
167
{
168 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 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 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 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	MAP::template involutionUnsew<3>(d);
}

/*! @name Generator and Deletor
 *  To generate or delete volumes in a 3-G-map
 *************************************************************************/

template <class MAP>
void GMap3<MAP>::deleteVolume(Dart d)
{
	DartMarkerStore mark(*this);		// Lock a marker

	std::vector<Dart> visitedFaces;		// Faces that are traversed
	visitedFaces.reserve(512);
	visitedFaces.push_back(d);			// Start with the face of d
	mark.markOrbit<FACE>(d) ;

	// For every face added to the list
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
	{
		Dart e = visitedFaces[i] ;

		if(!isBoundaryFace(e))
			unsewVolumes(e) ;

		do	// add all face neighbours to the table
		{
			Dart ee = this->phi2(e) ;
			if(!mark.isMarked(ee)) // not already marked
			{
				visitedFaces.push_back(ee) ;
				mark.markOrbit<FACE>(ee) ;
			}
			e = this->phi1(e) ;
		} while(e != visitedFaces[i]) ;
	}

	Dart dd = phi3(d) ;
	ParentMap::deleteCC(d) ;
	ParentMap::deleteCC(dd) ;
}

template <class MAP>
void GMap3<MAP>::fillHole(Dart d)
{
	assert(isBoundaryFace(d)) ;
	Dart dd = d ;
	if(!this->isBoundaryMarked3(dd))
		dd = phi3(dd) ;
	this->template boundaryUnmarkOrbit<VOLUME,3>(dd) ;
}

/*! @name Topological Operators
 *  Topological operations on 3-G-maps
 *************************************************************************/

template <class MAP>
Dart GMap3<MAP>::deleteVertex(Dart d)
{
	if(isBoundaryVertex(d))
		return NIL ;

	// Save the darts around the vertex
	// (one dart per face should be enough)
	std::vector<Dart> fstoretmp;
	fstoretmp.reserve(128);
	FunctorStore fs(fstoretmp);
	foreach_dart_of_vertex(d, fs);

	// just one dart per face
	std::vector<Dart> fstore;
	fstore.reserve(128);
	DartMarker mf(*this);
	for(unsigned int i = 0; i < fstoretmp.size(); ++i)
	{
		if(!mf.isMarked(fstoretmp[i]))
		{
			mf.markOrbit<FACE>(fstoretmp[i]);
			fstore.push_back(fstoretmp[i]);
		}
	}

	Dart res = NIL ;
	for(std::vector<Dart>::iterator it = fstore.begin() ; it != fstore.end() ; ++it)
	{
		Dart fit = *it ;
		Dart end = this->phi_1(fit) ;
		fit = this->phi1(fit) ;
		while(fit != end)
		{
			Dart d2 = this->phi2(fit) ;
			Dart d3 = phi3(fit) ;
			Dart d32 = this->phi2(d3) ;

			if(res == NIL)
				res = d2 ;

			this->beta2unsew(d2) ;
			this->beta2unsew(this->beta0(d2)) ;

			this->beta2unsew(d32) ;
			this->beta2unsew(this->beta0(d32)) ;

			this->beta2sew(d2, this->beta0(d32)) ;
			this->beta2sew(this->beta0(d2), d32) ;
			this->beta2sew(fit, this->beta0(d3)) ;
			this->beta2sew(this->beta0(fit), d3) ;

			fit = this->phi1(fit) ;
		}
	}

	ParentMap::deleteCC(d) ;

	return res ;
}

template <class MAP>
Dart GMap3<MAP>::cutEdge(Dart d)
{
	Dart prev = d ;
	Dart dd = alpha2(d) ;
	Dart nd = ParentMap::cutEdge(d) ;

	while(dd != d)
	{
		prev = dd ;
		dd = alpha2(dd) ;

		ParentMap::cutEdge(prev) ;

		Dart d3 = beta3(prev);
		beta3sew(this->beta0(prev), this->beta0(d3));
		beta3sew(this->phi1(prev), this->phi1(d3));
	}

	Dart d3 = beta3(d);
	beta3sew(this->beta0(d), this->beta0(d3));
	beta3sew(this->phi1(d), this->phi1(d3));

	return nd ;
}

template <class MAP>
bool GMap3<MAP>::uncutEdge(Dart d)
{
	if(vertexDegree(this->phi1(d)) == 2)
	{
		Dart prev = d ;

		Dart dd = d;
		do
		{
			prev = dd;
			dd = alpha2(dd);

			ParentMap::uncutEdge(prev);
		} while (dd != d) ;

		return true;
	}
	return false;
}

template <class MAP>
bool GMap3<MAP>::deleteEdgePreCond(Dart d)
{
	unsigned int nb1 = vertexDegree(d);
	unsigned int nb2 = vertexDegree(this->phi1(d));
	return (nb1!=2) && (nb2!=2);
}

template <class MAP>
Dart GMap3<MAP>::deleteEdge(Dart d)
{
	assert(deleteEdgePreCond(d));

	if(isBoundaryEdge(d))
		return NIL ;

	Dart res = NIL ;
	Dart dit = d ;
	do
	{
		Dart fit = dit ;
		Dart end = fit ;
		fit = this->phi1(fit) ;
		while(fit != end)
		{
			Dart d2 = this->phi2(fit) ;
			Dart d3 = phi3(fit) ;
			Dart d32 = this->phi2(d3) ;

			if(res == NIL)
				res = d2 ;

			this->beta2unsew(d2) ;
			this->beta2unsew(this->beta0(d2)) ;
			this->beta2unsew(d32) ;
			this->beta2unsew(this->beta0(d32)) ;

			this->beta2sew(d2, this->beta0(d32)) ;
			this->beta2sew(this->beta0(d2), d32) ;
			this->beta2sew(fit, this->beta0(d3)) ;
			this->beta2sew(this->beta0(fit), d3) ;

			fit = this->phi1(fit) ;
		}
		dit = alpha2(dit) ;
	} while(dit != d) ;

	ParentMap::deleteCC(d) ;

	return res ;
}

template <class MAP>
bool GMap3<MAP>::splitFacePreCond(Dart d, Dart e)
{
	return (d != e && ParentMap::sameFace(d, e)) ;
}

template <class MAP>
void GMap3<MAP>::splitFace(Dart d, Dart e)
{
	assert(splitFacePreCond(d, e));

	if(!sameOrientedFace(d, e))
		e = this->beta1(e) ;

	Dart dd = this->beta1(beta3(d));
	Dart ee = this->beta1(beta3(e));

	Dart dprev = this->phi_1(d) ;
	Dart eprev = this->phi_1(e) ;
	Dart ddprev = this->phi_1(dd) ;
	Dart eeprev = this->phi_1(ee) ;

	beta3unsew(this->beta1(d)) ;
	beta3unsew(this->beta1(e)) ;
	beta3unsew(this->beta1(dd)) ;
	beta3unsew(this->beta1(ee)) ;

	ParentMap::splitFace(d, e);
	ParentMap::splitFace(dd, ee);
	beta3sew(this->beta1(d), this->phi_1(ee));
	beta3sew(this->phi_1(d), this->beta1(ee));
	beta3sew(this->beta1(e), this->phi_1(dd));
	beta3sew(this->phi_1(e), this->beta1(dd));

	beta3sew(this->beta0(dprev), this->beta0(beta3(dprev))) ;
	beta3sew(this->beta0(eprev), this->beta0(beta3(eprev))) ;
	beta3sew(this->beta0(ddprev), this->beta0(beta3(ddprev))) ;
	beta3sew(this->beta0(eeprev), this->beta0(beta3(eeprev))) ;
}

template <class MAP>
void GMap3<MAP>::sewVolumes(Dart d, Dart e, bool withBoundary)
{
	assert(this->faceDegree(d) == this->faceDegree(e));

	// if sewing with fixed points
	if (!withBoundary)
	{
		assert(beta3(d) == d && beta3(e) == e) ;
		Dart fitD = d ;
		Dart fitE = e ;
		do
		{
			beta3sew(fitD, this->beta0(fitE)) ;
			beta3sew(this->beta0(fitD), fitE) ;
			fitD = this->phi1(fitD) ;
			fitE = this->phi_1(fitE) ;
		} while(fitD != d) ;
		return ;
	}

	Dart dd = beta3(d) ;
	Dart ee = beta3(e) ;

	Dart fitD = dd ;
	Dart fitE = ee ;
	do
	{
		Dart fitD2 = this->beta2(fitD) ;
		Dart fitE2 = this->beta2(fitE) ;
		if(fitD2 != fitE)
		{
			this->beta2unsew(fitD) ;
			this->beta2unsew(fitE) ;
			this->beta2unsew(this->beta0(fitD)) ;
			this->beta2unsew(this->beta0(fitE)) ;
			this->beta2sew(fitD2, this->beta0(fitE2)) ;
			this->beta2sew(this->beta0(fitD2), fitE2) ;
			this->beta2sew(fitD, this->beta0(fitE)) ;
			this->beta2sew(this->beta0(fitD), fitE) ;
		}
		beta3unsew(fitD) ;
		beta3unsew(this->beta0(fitD)) ;
		beta3unsew(fitE) ;
		beta3unsew(this->beta0(fitE)) ;
		fitD = this->phi1(fitD) ;
		fitE = this->phi_1(fitE) ;
	} while(fitD != dd) ;
	ParentMap::deleteCC(dd) ;

	fitD = d ;
	fitE = e ;
	do
	{
		beta3sew(fitD, this->beta0(fitE)) ;
		beta3sew(this->beta0(fitD), fitE) ;
		fitD = this->phi1(fitD) ;
		fitE = this->phi_1(fitE) ;
	} while(fitD != d) ;
}

template <class MAP>
void GMap3<MAP>::unsewVolumes(Dart d)
{
	assert(!isBoundaryFace(d)) ;

	unsigned int nbE = this->faceDegree(d) ;
	Dart d3 = phi3(d);

	Dart b1 = newBoundaryCycle(nbE) ;
	Dart b2 = newBoundaryCycle(nbE) ;

	Dart fit1 = d ;
	Dart fit2 = d3 ;
	Dart fitB1 = b1 ;
	Dart fitB2 = b2 ;
	do
	{
		Dart f = findBoundaryFaceOfEdge(fit1) ;
		if(f != NIL)
		{
			Dart f2 = this->phi2(f) ;
			this->beta2unsew(f) ;
			this->beta2unsew(this->beta0(f)) ;
			this->beta2sew(fitB1, this->beta0(f)) ;
			this->beta2sew(this->beta0(fitB1), f) ;
			this->beta2sew(fitB2, this->beta0(f2)) ;
			this->beta2sew(this->beta0(fitB2), f2) ;
		}
		else
		{
			this->beta2sew(fitB1, this->beta0(fitB2)) ;
			this->beta2sew(this->beta0(fitB1), fitB2) ;
		}

		beta3unsew(fit1) ;
		beta3unsew(this->beta0(fit1)) ;
		beta3sew(fit1, this->beta0(fitB1)) ;
		beta3sew(this->beta0(fit1), fitB1) ;
		beta3sew(fit2, this->beta0(fitB2)) ;
		beta3sew(this->beta0(fit2), fitB2) ;

		fit1 = this->phi1(fit1) ;
		fit2 = this->phi_1(fit2) ;
		fitB1 = this->phi_1(fitB1) ;
		fitB2 = this->phi1(fitB2) ;
	} while(fitB1 != b1) ;
}

template <class MAP>
bool GMap3<MAP>::mergeVolumes(Dart d)
{
	if(!GMap3::isBoundaryFace(d))
	{
		ParentMap::mergeVolumes(d, phi3(d)); // merge the two volumes along common face
		return true ;
	}
	return false ;
}

template <class MAP>
void GMap3<MAP>::splitVolume(std::vector<Dart>& vd)
{
	assert(ParentMap::checkSimpleOrientedPath(vd)) ;

	Dart e = vd.front();
	Dart e2 = this->phi2(e);

	ParentMap::splitSurface(vd,true,true);

	//sew the two connected components
	sewVolumes(this->phi2(e), this->phi2(e2), false);

//	Dart e = vd.front();
//	Dart e2 = phi2(e);
//
//	//unsew the edge path
//	for(std::vector<Dart>::iterator it = vd.begin() ; it != vd.end() ; ++it)
//		GMap2::unsewFaces(*it);
//
//	GMap2::fillHole(e) ;
//	GMap2::fillHole(e2) ;
//
//	//sew the two connected components
//	GMap3::sewVolumes(beta2(e), beta2(e2), false);
Pierre Kraemer's avatar
Pierre Kraemer committed
569 570
}

Pierre Kraemer's avatar
Pierre Kraemer committed
571 572 573 574
/*! @name Topological Queries
 *  Return or set various topological information
 *************************************************************************/

575 576 577 578 579 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 618 619 620 621 622 623 624 625 626 627 628 629 630 631 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 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
template <class MAP>
bool GMap3<MAP>::sameOrientedVertex(Dart d, Dart e) const
{
	DartMarkerStore mv(*this);	// Lock a marker

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; i < darts.size(); ++i)
	{
		if(darts[i] == e)
			return true;

		// add phi21 and phi23 successor if they are not marked yet
		Dart d2 = this->phi2(darts[i]);
		Dart d21 = this->phi1(d2);	// turn in volume
		Dart d23 = phi3(d2);		// change volume

		if(!mv.isMarked(d21))
		{
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
		}
	}
	return false;
}

template <class MAP>
bool GMap3<MAP>::sameVertex(Dart d, Dart e) const
{
	DartMarkerStore mv(*this);	// Lock a marker

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; i < darts.size(); ++i)
	{
		if(darts[i] == e)
			return true;

		Dart dx = this->beta1(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}
		dx = this->beta2(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}
		dx = beta3(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}
	}
	return false;
}

//template <class MAP>
//unsigned int GMap3<MAP>::vertexDegree(Dart d)
//{
//	unsigned int count = 0;
//	DartMarkerStore mv(*this);	// Lock a marker

//	std::vector<Dart> darts;	// Darts that are traversed
//	darts.reserve(256);
//	darts.push_back(d);			// Start with the dart d
//	mv.mark(d);

//	for(unsigned int i = 0; i < darts.size(); ++i)
//	{
//		//add phi21 and phi23 successor if they are not marked yet
//		Dart d2 = phi2(darts[i]);
//		Dart d21 = phi1(d2); // turn in volume
//		Dart d23 = phi3(d2); // change volume

//		if(!mv.isMarked(d21))
//		{
//			darts.push_back(d21);
//			mv.mark(d21);
//		}
//		if(!mv.isMarked(d23))
//		{
//			darts.push_back(d23);
//			mv.mark(d23);
//		}
//	}

//	DartMarkerStore me(*this);
//	for(std::vector<Dart>::iterator it = darts.begin(); it != darts.end() ; ++it)
//	{
//		if(!me.isMarked(*it))
//		{
//			++count;
//			me.markOrbit<EDGE>(*it);
//		}
//	}

//	return count;
//}

template <class MAP>
unsigned int GMap3<MAP>::vertexDegree(Dart d) const
{
	unsigned int count = 0;

	Traversor3VE<GMap3<MAP> > trav3VE(*this, d);
	for(Dart dit = trav3VE.begin() ; dit != trav3VE.end() ; dit = trav3VE.next())
	{
		++count;
	}

	return count;
}


template <class MAP>
int GMap3<MAP>::checkVertexDegree(Dart d, unsigned int vd) const
{
	unsigned int count = 0;

	Traversor3VE<GMap3<MAP> > trav3VE(*this, d);
	Dart dit = trav3VE.begin();
	for( ; (count<=vd) && (dit != trav3VE.end()) ; dit = trav3VE.next())
	{
		++count;
	}

	return count - vd;
}

template <class MAP>
bool GMap3<MAP>::isBoundaryVertex(Dart d) const
{
	DartMarkerStore mv(*this);	// Lock a marker

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; i < darts.size(); ++i)
	{
		if(this->isBoundaryMarked3(darts[i]))
			return true ;

		//add phi21 and phi23 successor if they are not marked yet
		Dart d2 = this->phi2(darts[i]);
		Dart d21 = this->phi1(d2);	// turn in volume
		Dart d23 = phi3(d2);		// change volume

		if(!mv.isMarked(d21))
		{
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
		}
	}
	return false ;
}

template <class MAP>
bool GMap3<MAP>::sameOrientedEdge(Dart d, Dart e) const
{
	Dart it = d;
	do
	{
		if(it == e || this->phi2(it) == e)
			return true;
		it = alpha2(it);
	} while (it != d);
	return false;
}

template <class MAP>
bool GMap3<MAP>::sameEdge(Dart d, Dart e) const
{
	Dart it = d;
	do
	{
		if(it == e || this->beta0(it) == e || this->beta2(it) == e || this->phi2(it) == e)
			return true;

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

template <class MAP>
unsigned int GMap3<MAP>::edgeDegree(Dart d) const
{
	unsigned int deg = 0;
	Dart it = d;
	do
	{
		++deg;
		it = alpha2(it);
	} while(it != d);
	return deg;
}

template <class MAP>
bool GMap3<MAP>::isBoundaryEdge(Dart d) const
{
	Dart it = d;
	do
	{
		if(this->isBoundaryMarked3(it))
			return true ;
		it = alpha2(it);
	} while(it != d);
	return false;
}

template <class MAP>
Dart GMap3<MAP>::findBoundaryFaceOfEdge(Dart d) const
{
	Dart it = d;
	do
	{
		if (this->isBoundaryMarked3(it))
			return it ;
		it = alpha2(it);
	} while(it != d);
	return NIL ;
}

template <class MAP>
bool GMap3<MAP>::sameOrientedFace(Dart d, Dart e) const
{
	Dart it = d;
	do
	{
		if(it == e || phi3(it) == e)
			return true;
		it = this->phi1(it);
	} while (it != d);
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
832 833
template <class MAP>
inline bool GMap3<MAP>::sameFace(Dart d, Dart e) const
Pierre Kraemer's avatar
Pierre Kraemer committed
834
{
Pierre Kraemer's avatar
Pierre Kraemer committed
835
	return ParentMap::sameFace(d, e) || ParentMap::sameFace(beta3(d), e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
836 837
}

Pierre Kraemer's avatar
Pierre Kraemer committed
838 839
template <class MAP>
inline bool GMap3<MAP>::isBoundaryFace(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
840
{
Pierre Kraemer's avatar
Pierre Kraemer committed
841
	return this->isBoundaryMarked3(d) || this->isBoundaryMarked3(beta3(d));
Pierre Kraemer's avatar
Pierre Kraemer committed
842 843
}

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
template <class MAP>
bool GMap3<MAP>::isBoundaryVolume(Dart d) const
{
	DartMarkerStore mark(*this);	// Lock a marker

	std::vector<Dart> visitedFaces ;
	visitedFaces.reserve(128) ;
	visitedFaces.push_back(d) ;
	mark.markOrbit<FACE>(d) ;

	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
	{
		if (isBoundaryMarked3(beta3(visitedFaces[i])))
			return true ;

		Dart e = visitedFaces[i] ;
		do	// add all face neighbours to the table
		{
			Dart ee = this->phi2(e) ;
			if(!mark.isMarked(ee)) // not already marked
			{
				visitedFaces.push_back(ee) ;
				mark.markOrbit<FACE>(ee) ;
			}
			e = this->phi1(e) ;
		} while(e != visitedFaces[i]) ;
	}
	return false;
}

template <class MAP>
bool GMap3<MAP>::check() const
{
	CGoGNout << "Check: topology begin" << CGoGNendl;
	DartMarker m(*this);
	m.unmarkAll();
	for(Dart d = this->begin(); d != this->end(); this->next(d))
	{
		Dart d0 = this->beta0(d);
		if (this->beta0(d0) != d) // beta0 involution ?
		{
			 CGoGNout << "Check: beta0 is not an involution" << CGoGNendl;
			return false;
		}

		Dart d3 = beta3(d);
		if (beta3(d3) != d) // beta3 involution ?
		{
			 CGoGNout << "Check: beta3 is not an involution" << CGoGNendl;
			return false;
		}

		if(d3 != d)
		{
			if(this->beta1(d3) != beta3(this->beta1(d)))
			{
				CGoGNout << "Check: beta3 , faces are not entirely sewn" << CGoGNendl;
				return false;
			}
		}

		Dart d2 = this->beta2(d);
		if (this->beta2(d2) != d) // beta2 involution ?
		{
			CGoGNout << "Check: beta2 is not an involution" << CGoGNendl;
			return false;
		}

		Dart d1 = this->phi1(d);
		if (this->phi_1(d1) != d) // phi1 a une image correcte ?
		{
			CGoGNout << "Check: unconsistent phi_1 link" << CGoGNendl;
			return false;
		}

		if (m.isMarked(d1)) // phi1 a un seul antécédent ?
		{
			CGoGNout << "Check: dart with two phi1 predecessors" << CGoGNendl;
			return false;
		}
		m.mark(d1);

		if (d1 == d)
			CGoGNout << "Check: (warning) face loop (one edge)" << CGoGNendl;

		if (this->phi1(d1) == d)
			CGoGNout << "Check: (warning) face with only two edges" << CGoGNendl;

		if (this->phi2(d1) == d)
			CGoGNout << "Check: (warning) dandling edge (phi2)" << CGoGNendl;

		if (phi3(d1) == d)
			CGoGNout << "Check: (warning) dandling edge (phi3)" << CGoGNendl;
	}

	for(Dart d = this->begin(); d != this->end(); this->next(d))
	{
		if (!m.isMarked(d)) // phi1 a au moins un antécédent ?
		{
			std::cout << "dart = " << d << std::endl;
			CGoGNout << "Check: dart with no phi1 predecessor" << CGoGNendl;
			return false;
		}
	}

	CGoGNout << "Check: topology ok" << CGoGNendl;

	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
954 955 956
/*! @name Cell Functors
 *  Apply functors to all darts of a cell
 *************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
957

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
template <class MAP>
bool GMap3<MAP>::foreach_dart_of_oriented_vertex(Dart d, FunctorType& f, unsigned int thread) const
{
	DartMarkerStore mv(*this, thread);	// Lock a marker
	bool found = false;					// Last functor return value

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; !found && i < darts.size(); ++i)
	{
		// add phi21 and phi23 successor if they are not marked yet
		Dart d2 = this->phi2(darts[i]);
		Dart d21 = this->phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume

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

		found = f(darts[i]);
	}
	return found;
}

template <class MAP>
bool GMap3<MAP>::foreach_dart_of_vertex(Dart d, FunctorType& f, unsigned int thread) const
{
	DartMarkerStore mv(*this, thread);	// Lock a marker
	bool found = false;					// Last functor return value

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; !found && i < darts.size(); ++i)
	{
		Dart dx = this->beta1(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}
		dx = this->beta2(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}
		dx = beta3(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}

		found = f(darts[i]);
	}
	return found;
}

template <class MAP>
bool GMap3<MAP>::foreach_dart_of_oriented_edge(Dart d, FunctorType& f, unsigned int thread) const
{
	Dart it = d;
	do
	{
		if (ParentMap::foreach_dart_of_oriented_edge(it, f, thread))
			return true;
		it = alpha2(it);
	} while (it != d);
	return false;
}

template <class MAP>
bool GMap3<MAP>::foreach_dart_of_edge(Dart d, FunctorType& f, unsigned int thread) const
{
	Dart it = d;
	do
	{
		if (ParentMap::foreach_dart_of_edge(it, f, thread))
			return true;
		it = alpha2(it);
	} while (it != d);
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1055 1056
template <class MAP>
inline bool GMap3<MAP>::foreach_dart_of_face(Dart d, FunctorType& f, unsigned int thread) const
Pierre Kraemer's avatar
Pierre Kraemer committed
1057
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1058
	return ParentMap::foreach_dart_of_face(d, f, thread) || ParentMap::foreach_dart_of_face(beta3(d), f, thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
1059
}
Pierre Kraemer's avatar
Pierre Kraemer committed
1060

Pierre Kraemer's avatar
Pierre Kraemer committed
1061 1062
template <class MAP>
inline bool GMap3<MAP>::foreach_dart_of_volume(Dart d, FunctorType& f, unsigned int thread) const
1063
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1064
	return ParentMap::foreach_dart_of_cc(d, f, thread);
1065 1066
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1067 1068
template <class MAP>
inline bool GMap3<MAP>::foreach_dart_of_oriented_volume(Dart d, FunctorType& f, unsigned int thread) const
1069
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1070
	return ParentMap::foreach_dart_of_oriented_cc(d, f, thread);
1071 1072
}

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
template <class MAP>
bool GMap3<MAP>::foreach_dart_of_cc(Dart d, FunctorType& f, unsigned int thread) const
{
	DartMarkerStore mv(*this,thread);	// Lock a marker
	bool found = false;					// Last functor return value

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(1024);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; !found && i < darts.size(); ++i)
	{
		Dart dx = this->beta0(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}
		dx = this->beta1(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}
		dx = this->beta2(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}
		dx = beta3(darts[i]);
		if (!mv.isMarked(dx))
		{
			darts.push_back(dx);
			mv.mark(dx);
		}

		found = f(darts[i]);
	}
	return found;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1116 1117
template <class MAP>
inline bool GMap3<MAP>::foreach_dart_of_vertex2(Dart d, FunctorType& f, unsigned int thread) const
1118
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1119
	return ParentMap::foreach_dart_of_vertex(d, f, thread);
1120 1121
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1122 1123
template <class MAP>
inline bool GMap3<MAP>::foreach_dart_of_edge2(Dart d, FunctorType& f, unsigned int thread) const
1124
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1125
	return ParentMap::foreach_dart_of_edge(d, f, thread);
1126 1127
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1128 1129
template <class MAP>
inline bool GMap3<MAP>::foreach_dart_of_face2(Dart d, FunctorType& f, unsigned int thread) const
1130
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1131
	return ParentMap::foreach_dart_of_face(d, f, thread);
1132 1133
}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
/*! @name Close map after import or creation
 *  These functions must be used with care, generally only by import/creation algorithms
 *************************************************************************/

template <class MAP>
Dart GMap3<MAP>::newBoundaryCycle(unsigned int nbE)
{
	Dart d = GMap1<MAP>::newCycle(nbE);
	this->template boundaryMarkOrbit<FACE,3>(d);
	return d;
}

template <class MAP>
unsigned int GMap3<MAP>::closeHole(Dart d, bool forboundary)
{
	assert(beta3(d) == d);		// Nothing to close
	DartMarkerStore m(*this) ;

	std::vector<Dart> visitedFaces;	// Faces that are traversed
	visitedFaces.reserve(1024) ;
	visitedFaces.push_back(d);		// Start with the face of d
	m.markOrbit<FACE>(d) ;

	unsigned int count = 0 ;

	// For every face added to the list
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
	{
		Dart f = visitedFaces[i] ;
		unsigned int degree = this->faceDegree(f) ;
		Dart b = newBoundaryCycle(degree) ;
		++count ;

		Dart bit = b ;
		do
		{
			Dart e = alpha2(f) ;
			bool found = false ;
			do
			{
				if(beta3(e) == e)
				{
					found = true ;
					if(!m.isMarked(e))
					{
						visitedFaces.push_back(e) ;
						m.markOrbit<FACE>(e) ;
					}
				}
				else if(this->isBoundaryMarked3(e))
				{
					found = true ;
					this->beta2sew(e, bit) ;
					this->beta2sew(this->beta0(e), this->beta0(bit)) ;
				}
				else
					e = alpha2(e) ;
			} while(!found) ;

			beta3sew(f, bit) ;
			beta3sew(this->beta0(f), this->beta0(bit)) ;
			bit = this->phi1(bit) ;
			f = this->phi1(f);
		} while(f != visitedFaces[i]);
	}

	return count ;
}

template <class MAP>
unsigned int GMap3<MAP>::closeMap()
{
	// Search the map for topological holes (fix points of beta3)
	unsigned int nb = 0 ;
	for (Dart d = this->begin(); d != this->end(); this->next(d))
	{
		if (beta3(d) == d)
		{
			++nb ;
			closeHole(d);
		}
	}
	return nb ;
}

/*! @name Compute dual
 * These functions compute the dual mesh
 *************************************************************************/

template <class MAP>
void GMap3<MAP>::computeDual()
{
//	DartAttribute<Dart> old_beta0 = getAttribute<Dart, DART>("beta0");
//	DartAttribute<Dart> old_beta1 = getAttribute<Dart, DART>("beta1");
//	DartAttribute<Dart> old_beta2 = getAttribute<Dart, DART>("beta2");
//	DartAttribute<Dart> old_beta3 = getAttribute<Dart, DART>("beta3") ;
//
//	swapAttributes<Dart>(old_beta0, old_beta3) ;
//	swapAttributes<Dart>(old_beta1, old_beta2) ;
//
//	swapEmbeddingContainers(VERTEX, FACE) ;
//
//	//boundary management ?
}

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