gmap2.hpp 31.9 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 25 26 27
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

Pierre Kraemer's avatar
Pierre Kraemer committed
28 29
template <typename MAP_IMPL>
inline void GMap2<MAP_IMPL>::init()
Pierre Kraemer's avatar
Pierre Kraemer committed
30
{
Pierre Kraemer's avatar
Pierre Kraemer committed
31
	MAP_IMPL::addInvolution() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
32 33
}

Pierre Kraemer's avatar
Pierre Kraemer committed
34 35
template <typename MAP_IMPL>
inline GMap2<MAP_IMPL>::GMap2() : GMap1<MAP_IMPL>()
36 37 38 39
{
	init() ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
40 41
template <typename MAP_IMPL>
inline std::string GMap2<MAP_IMPL>::mapTypeName() const
Pierre Kraemer's avatar
Pierre Kraemer committed
42 43 44 45
{
	return "GMap2";
}

Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
template <typename MAP_IMPL>
inline unsigned int GMap2<MAP_IMPL>::dimension() const
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51
{
	return 2;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
52 53
template <typename MAP_IMPL>
inline void GMap2<MAP_IMPL>::clear(bool removeAttrib)
54
{
Pierre Kraemer's avatar
Pierre Kraemer committed
55
	ParentMap::clear(removeAttrib) ;
Sylvain Thery's avatar
Sylvain Thery committed
56 57
	if (removeAttrib)
		init() ;
58 59
}

Pierre Kraemer's avatar
Pierre Kraemer committed
60
template <typename MAP_IMPL>
Pierre Kraemer's avatar
Pierre Kraemer committed
61
inline unsigned int GMap2<MAP_IMPL>::getNbInvolutions() const
Sylvain Thery's avatar
Sylvain Thery committed
62
{
Pierre Kraemer's avatar
Pierre Kraemer committed
63 64 65 66 67 68 69
	return 1 + ParentMap::getNbInvolutions();
}

template <typename MAP_IMPL>
inline unsigned int GMap2<MAP_IMPL>::getNbPermutations() const
{
	return ParentMap::getNbPermutations();
Sylvain Thery's avatar
Sylvain Thery committed
70 71
}

Pierre Kraemer's avatar
Pierre Kraemer committed
72 73 74 75
/*! @name Basic Topological Operators
 * Access and Modification
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
76 77
template <typename MAP_IMPL>
inline Dart GMap2<MAP_IMPL>::beta2(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
78
{
Pierre Kraemer's avatar
Pierre Kraemer committed
79
	return MAP_IMPL::template getInvolution<2>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
80 81
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
106 107
template <typename MAP_IMPL>
inline Dart GMap2<MAP_IMPL>::phi2(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
108
{
Pierre Kraemer's avatar
Pierre Kraemer committed
109
	return beta2(this->beta0(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111
}

Pierre Kraemer's avatar
Pierre Kraemer committed
112
template <typename MAP_IMPL>
Pierre Kraemer's avatar
Pierre Kraemer committed
113
template <int N>
Pierre Kraemer's avatar
Pierre Kraemer committed
114
inline Dart GMap2<MAP_IMPL>::phi(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116 117 118 119 120
{
	assert( (N >0) || !"negative parameters not allowed in template multi-phi");
	if (N<10)
	{
		switch(N)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
121 122 123
			case 1 : return this->phi1(d) ;
			case 2 : return phi2(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
		case 1 : return phi1(phi<N/10>(d)) ;
		case 2 : return phi2(phi<N/10>(d)) ;
		default : assert(!"Wrong multi-phi relation value") ; return d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
131 132 133
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
134 135
template <typename MAP_IMPL>
inline Dart GMap2<MAP_IMPL>::alpha0(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
136
{
Pierre Kraemer's avatar
Pierre Kraemer committed
137
	return beta2(this->beta0(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139
}

Pierre Kraemer's avatar
Pierre Kraemer committed
140 141
template <typename MAP_IMPL>
inline Dart GMap2<MAP_IMPL>::alpha1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
142
{
Pierre Kraemer's avatar
Pierre Kraemer committed
143
	return beta2(this->beta1(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145
}

Pierre Kraemer's avatar
Pierre Kraemer committed
146 147
template <typename MAP_IMPL>
inline Dart GMap2<MAP_IMPL>::alpha_1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
148
{
149
	return beta1(beta2(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
150 151
}

Pierre Kraemer's avatar
Pierre Kraemer committed
152 153
template <typename MAP_IMPL>
inline void GMap2<MAP_IMPL>::beta2sew(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
154
{
Pierre Kraemer's avatar
Pierre Kraemer committed
155
	MAP_IMPL::template involutionSew<2>(d,e);
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157
}

Pierre Kraemer's avatar
Pierre Kraemer committed
158 159
template <typename MAP_IMPL>
inline void GMap2<MAP_IMPL>::beta2unsew(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
160
{
Pierre Kraemer's avatar
Pierre Kraemer committed
161
	MAP_IMPL::template involutionUnsew<2>(d);
162 163 164 165 166 167
}

/*! @name Constructors and Destructors
 *  To generate or delete faces in a 2-G-map
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
168 169
template <typename MAP_IMPL>
Dart GMap2<MAP_IMPL>::newFace(unsigned int nbEdges, bool withBoundary)
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
	Dart d = ParentMap::newCycle(nbEdges);
	if (withBoundary)
	{
		Dart e = newBoundaryCycle(nbEdges);

		Dart it = d;
		do
		{
			beta2sew(it, this->beta0(e));
			beta2sew(this->beta0(it), e);
			it = this->phi1(it);
			e = this->phi_1(e);
		} while (it != d);
	}
	return d;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
188 189
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::deleteFace(Dart d)
190
{
Pierre Kraemer's avatar
Pierre Kraemer committed
191
	assert(!this->template isBoundaryMarked<2>(d)) ;
192 193 194 195 196 197 198 199 200 201 202 203
	Dart it = d ;
	do
	{
		if(!isBoundaryEdge(it))
			unsewFaces(it) ;
		it = this->phi1(it) ;
	} while(it != d) ;
	Dart dd = phi2(d) ;
	ParentMap::deleteCycle(d) ;
	ParentMap::deleteCycle(dd) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
204 205
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::deleteCC(Dart d)
206
{
Sylvain Thery's avatar
Sylvain Thery committed
207
	DartMarkerNoUnmark<MAP_IMPL> mark(*this);
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

	std::vector<Dart> visited;
	visited.reserve(1024) ;
	visited.push_back(d);
	mark.mark(d) ;

	for(unsigned int i = 0; i < visited.size(); ++i)
	{
		Dart d0 = this->beta0(visited[i]) ;
		if(!mark.isMarked(d0))
		{
			visited.push_back(d0) ;
			mark.mark(d0);
		}
		Dart d1 = this->beta1(visited[i]) ;
		if(!mark.isMarked(d1))
		{
			visited.push_back(d1) ;
			mark.mark(d1);
		}
		Dart d2 = beta2(visited[i]) ;
		if(!mark.isMarked(d2))
		{
			visited.push_back(d2) ;
			mark.mark(d2);
		}
	}

	for(std::vector<Dart>::iterator it = visited.begin(); it != visited.end(); ++it)
Sylvain Thery's avatar
Sylvain Thery committed
237 238
	{
		mark.unmark(*it);
239
		this->deleteDart(*it) ;
Sylvain Thery's avatar
Sylvain Thery committed
240
	}
241 242
}

Pierre Kraemer's avatar
Pierre Kraemer committed
243 244
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::fillHole(Dart d)
245 246 247
{
	assert(isBoundaryEdge(d)) ;
	Dart dd = d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
248
	if(!this->template isBoundaryMarked<2>(dd))
249
		dd = phi2(dd) ;
250
	Algo::Topo::boundaryUnmarkOrbit<2,FACE>(*this, dd) ;
251 252 253 254 255 256
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
257 258
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::splitVertex(Dart d, Dart e)
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
{
	assert(sameVertex(d, e));

	if(!sameOrientedVertex(d, e))
		e = beta2(e) ;

	Dart dd = phi2(d) ;
	Dart ee = phi2(e) ;
	beta2unsew(d) ;
	beta2unsew(e) ;

	ParentMap::cutEdge(dd);	// Cut the edge of dd (make a new edge)
	ParentMap::cutEdge(ee);	// Cut the edge of ee (make a new edge)

	beta2sew(this->phi1(dd), this->beta0(this->phi1(ee)));	// Sew the two faces along the new edge
	beta2sew(this->phi1(ee), this->beta0(this->phi1(dd)));
	beta2sew(d, this->beta0(dd)) ;
	beta2sew(e, this->beta0(ee)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
279 280
template <typename MAP_IMPL>
Dart GMap2<MAP_IMPL>::deleteVertex(Dart d)
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
{
	if(isBoundaryVertex(d))
		return NIL ;

	Dart res = NIL ;
	Dart vit = d ;
	do
	{
		if(res == NIL && this->phi1(this->phi1(d)) != d)
			res = this->phi1(d) ;

		Dart d0 = this->beta0(vit) ;
		Dart d02 = beta2(d0) ;
		Dart d01 = this->beta1(d0) ;
		Dart d021 = this->beta1(d02) ;
		this->beta1unsew(d0) ;
		this->beta1unsew(d02) ;
		this->beta1sew(d0, d02) ;
		this->beta1sew(d01, d021) ;

		vit = alpha1(vit) ;
	} while(vit != d) ;
	ParentMap::deleteCycle(d) ;
	return res ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
307 308
template <typename MAP_IMPL>
Dart GMap2<MAP_IMPL>::cutEdge(Dart d)
309 310 311 312 313 314 315 316 317 318 319
{
	Dart e = phi2(d) ;
	Dart nd = ParentMap::cutEdge(d) ;
	Dart ne = ParentMap::cutEdge(e) ;

	beta2sew(nd, this->beta1(ne)) ;
	beta2sew(ne, this->beta1(nd)) ;

	return nd ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
320 321
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::uncutEdge(Dart d)
322 323 324 325 326 327 328 329 330 331
{
	if(vertexDegree(this->phi1(d)) == 2)
	{
		ParentMap::uncutEdge(d) ;
		ParentMap::uncutEdge(beta2(d)) ;
		return true ;
	}
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
332 333
template <typename MAP_IMPL>
Dart GMap2<MAP_IMPL>::collapseEdge(Dart d, bool delDegenerateFaces)
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
{
	Dart resV = NIL ;

	Dart e = phi2(d);
	beta2unsew(d);	// Unlink the opposite edges
	beta2unsew(e);

	Dart f = this->phi1(e) ;
	Dart h = alpha1(e);

	if (h != e)
		resV = h;

	if (f != e && delDegenerateFaces)
	{
		ParentMap::collapseEdge(e) ;	// Collapse edge e
		collapseDegeneratedFace(f) ;	// and collapse its face if degenerated
	}
	else
		ParentMap::collapseEdge(e) ;	// Just collapse edge e

	f = this->phi1(d) ;
	if(resV == NIL)
	{
		h = alpha1(d);
		if (h != d)
			resV = h;
	}

	if (f != d && delDegenerateFaces)
	{
		ParentMap::collapseEdge(d) ;	// Collapse edge d
		collapseDegeneratedFace(f) ;	// and collapse its face if degenerated
	}
	else
		ParentMap::collapseEdge(d) ;	// Just collapse edge d

	return resV ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
374 375
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::flipEdge(Dart d)
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
{
	if (!isBoundaryEdge(d))
	{
		Dart e = phi2(d);
		Dart dNext = this->phi1(d);
		Dart eNext = this->phi1(e);
		Dart dPrev = this->phi_1(d);
		Dart ePrev = this->phi_1(e);
		Dart dNext2 = this->phi1(dNext);
		Dart eNext2 = this->phi1(eNext);

		this->beta1unsew(d) ;
		this->beta1unsew(eNext) ;
		this->beta1unsew(e) ;
		this->beta1unsew(dNext) ;
		this->beta1unsew(dNext2) ;
		this->beta1unsew(eNext2) ;

		this->beta1sew(this->beta0(e), eNext2) ;
		this->beta1sew(d, this->beta0(eNext)) ;
		this->beta1sew(this->beta0(d), dNext2) ;
		this->beta1sew(e, this->beta0(dNext)) ;
		this->beta1sew(eNext, this->beta0(dPrev)) ;
		this->beta1sew(dNext, this->beta0(ePrev)) ;

		return true ;
	}
	return false ; // cannot flip a border edge
}

Pierre Kraemer's avatar
Pierre Kraemer committed
406 407
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::flipBackEdge(Dart d)
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
{
	if (!isBoundaryEdge(d))
	{
		Dart e = phi2(d);
		Dart dNext = this->phi1(d);
		Dart eNext = this->phi1(e);
		Dart dPrev = this->phi_1(d);
		Dart ePrev = this->phi_1(e);
		Dart dPrev1 = this->beta1(dPrev) ;
		Dart ePrev1 = this->beta1(ePrev) ;

		this->beta1unsew(d) ;
		this->beta1unsew(eNext) ;
		this->beta1unsew(e) ;
		this->beta1unsew(dNext) ;
		this->beta1unsew(dPrev) ;
		this->beta1unsew(ePrev) ;

		this->beta1sew(this->beta0(e), dPrev) ;
		this->beta1sew(d, dPrev1) ;
		this->beta1sew(this->beta0(d), ePrev) ;
		this->beta1sew(e, ePrev1) ;
		this->beta1sew(eNext, this->beta0(dPrev)) ;
		this->beta1sew(dNext, this->beta0(ePrev)) ;

		return true ;
	}
	return false ; // cannot flip a border edge
}

Pierre Kraemer's avatar
Pierre Kraemer committed
438 439
//template <typename MAP_IMPL>
//void GMap2<MAP_IMPL>::insertEdgeInVertex(Dart d, Dart e)
440 441 442 443 444 445
//{
//	assert(!sameVertex(d,e) && phi2(e)==phi_1(e));
//
//	phi1sew(phi_1(d),phi_1(e));
//}
//
Pierre Kraemer's avatar
Pierre Kraemer committed
446 447
//template <typename MAP_IMPL>
//void GMap2<MAP_IMPL>::removeEdgeFromVertex(Dart d)
448 449 450 451 452 453
//{
//	assert(phi2(d)!=d);
//
//	phi1sew(phi_1(d),phi2(d));
//}

Pierre Kraemer's avatar
Pierre Kraemer committed
454 455
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::sewFaces(Dart d, Dart e, bool withBoundary)
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
{
	// if sewing with fixed points
	if (!withBoundary)
	{
		assert(beta2(d) == d && beta2(this->beta0(d)) == this->beta0(d) && beta2(e) == e && beta2(this->beta0(e)) == this->beta0(e)) ;
		beta2sew(d, this->beta0(e)) ;
		beta2sew(e, this->beta0(d)) ;
		return ;
	}

	assert(isBoundaryEdge(d) && isBoundaryEdge(e)) ;

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

	beta2unsew(d) ;	// unsew faces from boundary
	beta2unsew(this->beta0(d)) ;
	beta2unsew(e) ;
	beta2unsew(this->beta0(e)) ;

	if (ee != this->phi_1(dd))
	{
		Dart eeN = this->phi1(ee) ;		// remove the boundary edge
		Dart dd1 = this->beta1(dd) ;
		this->beta1unsew(eeN) ;
		this->beta1unsew(dd1) ;
		this->beta1sew(this->beta0(ee), dd) ;
		this->beta1sew(eeN, dd1) ;
	}
	if (dd != this->phi_1(ee))
	{
		Dart ddN = this->phi1(dd) ;		// and properly close incident boundaries
		Dart ee1 = this->beta1(ee) ;
		this->beta1unsew(ddN) ;
		this->beta1unsew(ee1) ;
		this->beta1sew(this->beta0(dd), ee) ;
		this->beta1sew(ddN, ee1) ;
	}
	ParentMap::deleteCycle(dd) ;

	beta2sew(d, this->beta0(e)) ; // sew the faces
	beta2sew(e, this->beta0(d)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
500 501
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::unsewFaces(Dart d)
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
{
	assert(!isBoundaryEdge(d)) ;

	Dart dd = phi2(d);

	Dart e = newBoundaryCycle(2);
	Dart ee = this->phi1(e) ;

	Dart f = findBoundaryEdgeOfVertex(d) ;
	if (f != NIL)
	{
		Dart f1 = this->beta1(f) ;
		this->beta1unsew(ee) ;
		this->beta1unsew(f) ;
		this->beta1sew(f, this->beta0(e)) ;
		this->beta1sew(f1, ee) ;
	}

	f = findBoundaryEdgeOfVertex(dd) ;
	if (f != NIL)
	{
		Dart f1 = this->beta1(f) ;
		this->beta1unsew(e) ;
		this->beta1unsew(f) ;
		this->beta1sew(f, this->beta0(ee)) ;
		this->beta1sew(f1, e) ;
	}

	beta2unsew(d) ;
	beta2unsew(this->beta0(d)) ;
	beta2unsew(dd) ;
	beta2unsew(this->beta0(dd)) ;

	beta2sew(d, this->beta0(e)) ;	// sew faces
	beta2sew(e, this->beta0(d)) ;
	beta2sew(dd, this->beta0(ee)) ;	// to the boundary
	beta2sew(ee, this->beta0(dd)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
541 542
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::collapseDegeneratedFace(Dart d)
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 569 570 571 572 573
{
	Dart e = this->phi1(d);		// Check if the face is a loop
	if (this->phi1(e) == d)		// Yes: it contains one or two edge(s)
	{
		Dart d2 = phi2(d) ;		// Check opposite edges
		Dart e2 = phi2(e) ;
		beta2unsew(d) ;
		beta2unsew(this->beta0(d)) ;
		if(d != e)
		{
			beta2unsew(e) ;
			beta2unsew(this->beta0(e)) ;
			beta2sew(d2, this->beta0(e2)) ;
			beta2sew(e2, this->beta0(d2)) ;
		}
		else
		{
			Dart d21 = this->beta1(d2) ;
			Dart d2N = this->phi1(d2) ;
			this->beta1unsew(d2) ;
			this->beta1unsew(d2N) ;
			this->beta1sew(d21, d2N) ;
			this->beta1sew(d2, this->beta0(d2)) ;
			ParentMap::deleteCycle(d2) ;
		}
		ParentMap::deleteCycle(d) ;
		return true ;
	}
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
574 575
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::splitFace(Dart d, Dart e)
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
{
	assert(d != e && sameFace(d, e)) ;

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

	Dart dprev = this->phi_1(d) ;
	Dart eprev = this->phi_1(e) ;

	// required to unsew and resew because we use GMap1 cutEdge
	// which insert new darts within the cut edge
	beta2unsew(this->beta1(d)) ;
	beta2unsew(this->beta1(e)) ;

	Dart dd = ParentMap::cutEdge(this->phi_1(d)) ;
	Dart ee = ParentMap::cutEdge(this->phi_1(e)) ;
	ParentMap::splitCycle(dd, ee) ;
	beta2sew(dd, this->beta1(e)) ;
	beta2sew(ee, this->beta1(d)) ;

	beta2sew(this->beta0(dprev), this->beta0(beta2(dprev))) ;
	beta2sew(this->beta0(eprev), this->beta0(beta2(eprev))) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
600 601
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::mergeFaces(Dart d)
602 603 604 605 606 607 608 609 610 611 612 613 614 615
{
	if (!isBoundaryEdge(d))
	{
		Dart e = phi2(d) ;
		beta2unsew(d) ;
		beta2unsew(e) ;
		ParentMap::mergeCycles(d, this->phi1(e)) ;
		ParentMap::splitCycle(e, this->phi1(d)) ;
		ParentMap::deleteCycle(d) ;
		return true ;
	}
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
616 617
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::extractTrianglePair(Dart d)
618 619 620
{
	Dart e = phi2(d) ;

Sylvain Thery's avatar
Tutos  
Sylvain Thery committed
621
	assert(!isBoundaryAdjacentFace(d) && !isBoundaryAdjacentFace(e)) ;
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	assert(faceDegree(d) == 3 && faceDegree(e) == 3) ;

	Dart d1 = phi2(this->phi1(d)) ;
	Dart d2 = phi2(this->phi_1(d)) ;
	beta2unsew(d1) ;
	beta2unsew(this->beta0(d1)) ;
	beta2unsew(d2) ;
	beta2unsew(this->beta0(d2)) ;
	beta2sew(d1, this->beta0(d2)) ;
	beta2sew(d2, this->beta0(d1)) ;

	Dart e1 = phi2(this->phi1(e)) ;
	Dart e2 = phi2(this->phi_1(e)) ;
	beta2unsew(e1) ;
	beta2unsew(this->beta0(e1)) ;
	beta2unsew(e2) ;
	beta2unsew(this->beta0(e2)) ;
	beta2sew(e1, this->beta0(e2)) ;
	beta2sew(e2, this->beta0(e1)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
643 644
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::insertTrianglePair(Dart d, Dart v1, Dart v2)
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
{
	Dart e = phi2(d) ;

	assert(v1 != v2 && sameOrientedVertex(v1, v2)) ;
	assert(faceDegree(d) == 3 && faceDegree(phi2(d)) == 3) ;

	Dart vv1 = phi2(v1) ;
	beta2unsew(v1) ;
	beta2unsew(vv1) ;
	beta2sew(this->phi_1(d), this->beta0(v1)) ;
	beta2sew(this->beta1(d), v1) ;
	beta2sew(this->phi1(d), this->beta0(vv1)) ;
	beta2sew(this->beta0(this->phi1(d)), vv1) ;

	Dart vv2 = phi2(v2) ;
	beta2unsew(v2) ;
	beta2unsew(vv2) ;
	beta2sew(this->phi_1(e), this->beta0(v2)) ;
	beta2sew(this->beta1(e), v2) ;
	beta2sew(this->phi1(e), this->beta0(vv2)) ;
	beta2sew(this->beta0(this->phi1(e)), vv2) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
668 669
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::mergeVolumes(Dart d, Dart e)
670
{
Pierre Kraemer's avatar
Pierre Kraemer committed
671
	assert(!this->template isBoundaryMarked<2>(d) && !this->template isBoundaryMarked<2>(e)) ;
672

Sylvain Thery's avatar
Tutos  
Sylvain Thery committed
673
	if (GMap2::isBoundaryAdjacentFace(d) || GMap2::isBoundaryAdjacentFace(e))
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
		return false;

	// First traversal of both faces to check the face sizes
	// and store their edges to efficiently access them further

	std::vector<Dart> dDarts;
	std::vector<Dart> eDarts;
	dDarts.reserve(16);		// usual faces have less than 16 edges
	eDarts.reserve(16);

	Dart dFit = d ;
	Dart eFit = this->phi1(e) ;	// must take phi1 because of the use
	do							// of reverse iterator for sewing loop
	{
		dDarts.push_back(dFit) ;
		dFit = this->phi1(dFit) ;
	} while(dFit != d) ;
	do
	{
		eDarts.push_back(eFit) ;
		eFit = this->phi1(eFit) ;
	} while(eFit != this->phi1(e)) ;

	if(dDarts.size() != eDarts.size())
		return false ;

	// Make the sewing: take darts in initial order (clockwise) in first face
	// and take darts in reverse order (counter-clockwise) in the second face
	std::vector<Dart>::iterator dIt;
	std::vector<Dart>::reverse_iterator eIt;
	for (dIt = dDarts.begin(), eIt = eDarts.rbegin(); dIt != dDarts.end(); ++dIt, ++eIt)
	{
		Dart d2 = phi2(*dIt);	// Search the faces adjacent to dNext and eNext
		Dart e2 = phi2(*eIt);
		beta2unsew(d2);		// Unlink the two adjacent faces from dNext and eNext
		beta2unsew(this->beta0(d2));
		beta2unsew(e2);
		beta2unsew(this->beta0(e2));
		beta2sew(d2, this->beta0(e2));	// Link the two adjacent faces together
		beta2sew(e2, this->beta0(d2));
	}
	ParentMap::deleteCycle(d);		// Delete the two alone faces
	ParentMap::deleteCycle(e);

	return true ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
721 722
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::splitSurface(std::vector<Dart>& vd, bool firstSideClosed, bool secondSideClosed)
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
{
	//assert(checkSimpleOrientedPath(vd)) ;
	Dart e = vd.front() ;
	Dart e2 = phi2(e) ;

	//unsew the edge path
	for(std::vector<Dart>::iterator it = vd.begin() ; it != vd.end() ; ++it)
	{
		if(!GMap2::isBoundaryEdge(*it))
			unsewFaces(*it) ;
	}

	if(firstSideClosed)
		GMap2::fillHole(e) ;

	if(secondSideClosed)
		GMap2::fillHole(e2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
740 741
}

742 743 744 745
/*! @name Topological Queries
 *  Return or set various topological information
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
746 747
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::sameOrientedVertex(Dart d, Dart e) const
748 749 750 751 752 753 754 755 756 757 758
{
	Dart it = d;
	do
	{
		if (it == e)	// Test equality with e
			return true;
		it = alpha1(it);
	} while (it != d);
	return false;		// None is equal to e => vertices are distinct
}

Pierre Kraemer's avatar
Pierre Kraemer committed
759 760
template <typename MAP_IMPL>
inline bool GMap2<MAP_IMPL>::sameVertex(Dart d, Dart e) const
761 762 763 764
{
	return sameOrientedVertex(d, e) || sameOrientedVertex(beta2(d), e) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
765 766
template <typename MAP_IMPL>
unsigned int GMap2<MAP_IMPL>::vertexDegree(Dart d) const
767 768 769 770 771 772 773 774 775 776 777
{
	unsigned int count = 0 ;
	Dart it = d ;
	do
	{
		++count ;
		it = alpha1(it) ;
	} while (it != d) ;
	return count ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
778 779
template <typename MAP_IMPL>
int GMap2<MAP_IMPL>::checkVertexDegree(Dart d, unsigned int vd) const
780 781 782 783 784 785 786 787 788 789 790 791
{
	unsigned int count = 0 ;
	Dart it = d ;
	do
	{
		++count ;
		it = alpha1(it) ;
	} while ((count<=vd) && (it != d)) ;

	return count-vd;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
792 793
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::isBoundaryVertex(Dart d) const
794 795 796 797
{
	Dart it = d ;
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
798
		if (this->template isBoundaryMarked<2>(it))
799 800 801 802 803 804
			return true ;
		it = alpha1(it) ;
	} while (it != d) ;
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
805 806
template <typename MAP_IMPL>
Dart GMap2<MAP_IMPL>::findBoundaryEdgeOfVertex(Dart d) const
807 808 809 810
{
	Dart it = d ;
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
811
		if (this->template isBoundaryMarked<2>(it))
812 813 814 815 816 817
			return it ;
		it = alpha1(it) ;
	} while (it != d) ;
	return NIL ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
818 819
template <typename MAP_IMPL>
inline bool GMap2<MAP_IMPL>::sameEdge(Dart d, Dart e) const
Pierre Kraemer's avatar
Pierre Kraemer committed
820
{
Pierre Kraemer's avatar
Pierre Kraemer committed
821
	return d == e || beta2(d) == e || this->beta0(d) == e || beta2(this->beta0(d)) == e ;
Pierre Kraemer's avatar
Pierre Kraemer committed
822 823
}

Pierre Kraemer's avatar
Pierre Kraemer committed
824 825
template <typename MAP_IMPL>
inline bool GMap2<MAP_IMPL>::isBoundaryEdge(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
826
{
Pierre Kraemer's avatar
Pierre Kraemer committed
827
	return this->template isBoundaryMarked<2>(d) || this->template isBoundaryMarked<2>(beta2(d));
Pierre Kraemer's avatar
Pierre Kraemer committed
828 829
}

Pierre Kraemer's avatar
Pierre Kraemer committed
830 831
template <typename MAP_IMPL>
inline bool GMap2<MAP_IMPL>::sameOrientedFace(Dart d, Dart e) const
Pierre Kraemer's avatar
Pierre Kraemer committed
832
{
Pierre Kraemer's avatar
Pierre Kraemer committed
833
	return ParentMap::sameOrientedCycle(d, e) ;
834 835
}

Pierre Kraemer's avatar
Pierre Kraemer committed
836 837
template <typename MAP_IMPL>
inline bool GMap2<MAP_IMPL>::sameFace(Dart d, Dart e) const
838
{
Pierre Kraemer's avatar
Pierre Kraemer committed
839
	return ParentMap::sameCycle(d, e) ;
840 841
}

Pierre Kraemer's avatar
Pierre Kraemer committed
842 843
template <typename MAP_IMPL>
inline unsigned int GMap2<MAP_IMPL>::faceDegree(Dart d) const
844
{
Pierre Kraemer's avatar
Pierre Kraemer committed
845
	return ParentMap::cycleDegree(d) ;
846 847
}

Pierre Kraemer's avatar
Pierre Kraemer committed
848 849
template <typename MAP_IMPL>
inline int GMap2<MAP_IMPL>::checkFaceDegree(Dart d, unsigned int le) const
850
{
Pierre Kraemer's avatar
Pierre Kraemer committed
851
	return ParentMap::checkCycleDegree(d,le) ;
852 853
}

Pierre Kraemer's avatar
Pierre Kraemer committed
854
template <typename MAP_IMPL>
Sylvain Thery's avatar
Tutos  
Sylvain Thery committed
855
bool GMap2<MAP_IMPL>::isBoundaryAdjacentFace(Dart d) const
856 857 858 859
{
	Dart it = d ;
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
860
		if (this->template isBoundaryMarked<2>(beta2(it)))
861 862 863 864 865 866
			return true ;
		it = this->phi1(it) ;
	} while (it != d) ;
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
867 868
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::sameOrientedVolume(Dart d, Dart e) const
869
{
870
	DartMarkerStore<GMap2<MAP_IMPL> > mark(*this);	// Lock a marker
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

	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(); face != visitedFaces.end(); ++face)
	{
		if (!this->isBoundaryMarked2(*face) && !mark.isMarked(*face))	// Face has not been visited yet
		{
			Dart it = *face ;
			do
			{
				if(it == e)
					return true;

				mark.mark(it);						// Mark
				Dart adj = phi2(it);				// Get adjacent face
				if (!this->isBoundaryMarked2(adj) && !mark.isMarked(adj))
					visitedFaces.push_back(adj);	// Add it
				it = this->phi1(it);
			} while(it != *face);
		}
	}
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
898 899
template <typename MAP_IMPL>
inline bool GMap2<MAP_IMPL>::sameVolume(Dart d, Dart e) const
900 901
{
	return sameOrientedVolume(d, e) || sameOrientedVolume(beta2(d), e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
902 903
}

Pierre Kraemer's avatar
Pierre Kraemer committed
904 905
template <typename MAP_IMPL>
unsigned int GMap2<MAP_IMPL>::volumeDegree(Dart d) const
906 907
{
	unsigned int count = 0;
908
	DartMarkerStore<GMap2<MAP_IMPL> > mark(*this);		// Lock a marker
909 910 911 912 913 914 915 916 917 918

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

	// For every face added to the list
	for (unsigned int i = 0; i != visitedFaces.size(); ++i)
	{
		Dart df = visitedFaces[i];
Pierre Kraemer's avatar
Pierre Kraemer committed
919
		if (!this->template isBoundaryMarked<2>(df) && !mark.isMarked(df))	// Face has not been visited yet
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
		{
			++count;
			Dart it = df ;
			do
			{
				mark.mark(it);					// Mark
				Dart adj = phi2(it);			// Get adjacent face
				if ( !this->isBoundaryMarked2(adj) && !mark.isMarked(adj) )
					visitedFaces.push_back(adj);// Add it
				it = this->phi1(it);
			} while(it != df);
		}
	}

	return count;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
937 938
template <typename MAP_IMPL>
int GMap2<MAP_IMPL>::checkVolumeDegree(Dart d, unsigned int volDeg) const
939 940
{
	unsigned int count = 0;
941
	DartMarkerStore<GMap2<MAP_IMPL> > mark(*this);		// Lock a marker
942 943 944 945 946 947 948 949 950 951

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

	// For every face added to the list
	for (unsigned int i = 0; i != visitedFaces.size(); ++i)
	{
		Dart df = visitedFaces[i];
Pierre Kraemer's avatar
Pierre Kraemer committed
952
		if (!this->template isBoundaryMarked<2>(df) && !mark.isMarked(df))	// Face has not been visited yet
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
		{
			++count;
			Dart it = df ;
			do
			{
				mark.mark(it);					// Mark
				Dart adj = phi2(it);			// Get adjacent face
				if ( !this->isBoundaryMarked2(adj) && !mark.isMarked(adj) )
					visitedFaces.push_back(adj);// Add it
				it = this->phi1(it);
			} while(it != df);
		}
		if (count > volDeg)
			break;
	}

	return count - volDeg;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
972 973
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::isTriangular() const
974 975 976 977 978 979 980 981 982 983
{
	TraversorF<GMap2> t(*this) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
	{
		if(faceDegree(d) != 3)
			return false ;
	}
	return true ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
984 985
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::check() const
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
{
	CGoGNout << "Check: topology begin" << CGoGNendl;
	for(Dart d = this->begin(); d != this->end(); this->next(d))
	{
		Dart dd = this->beta0(d);
		if (this->beta0(dd) != d)	// beta0 involution ?
		{
			CGoGNout << "Check: beta0 is not an involution" << CGoGNendl;
			return false;
		}

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

		dd = beta2(d);
		if (beta2(dd) != d)	// beta2 involution ?
		{
			CGoGNout << "Check: beta2 is not an involution" << CGoGNendl;
			return false;
		}
		if(dd == d)
			CGoGNout << "Check (warning): beta2 has fix points" << CGoGNendl;
	}

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

	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1019 1020
template <typename MAP_IMPL>
bool GMap2<MAP_IMPL>::checkSimpleOrientedPath(std::vector<Dart>& vd)
1021
{
1022
	DartMarkerStore<GMap2<MAP_IMPL> > dm(*this) ;
1023 1024 1025 1026 1027
	for(std::vector<Dart>::iterator it = vd.begin() ; it != vd.end() ; ++it)
	{
		if(dm.isMarked(*it))
			return false ;

Pierre Kraemer's avatar
Pierre Kraemer committed
1028
		dm.template markOrbit<VERTEX>(*it) ;
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

		std::vector<Dart>::iterator prev ;
		if(it == vd.begin())
			prev = vd.end() - 1 ;
		else
			prev = it - 1 ;

		if(!sameVertex(*it, this->phi1(*prev)))
			return false ;
	}
	return true ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1042 1043 1044 1045
/*! @name Cell Functors
 *  Apply functors to all darts of a cell
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
1046
template <typename MAP_IMPL>
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
template <unsigned int ORBIT, typename FUNC>
void GMap2<MAP_IMPL>::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 MAP_IMPL>
template <unsigned int ORBIT, typename FUNC>
void GMap2<MAP_IMPL>::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 MAP_IMPL>
template <typename FUNC>
void GMap2<MAP_IMPL>::foreach_dart_of_oriented_vertex(Dart d, FUNC& f, unsigned int /*thread*/) const
1083 1084 1085 1086
{
	Dart it = d;
	do
	{
1087
		f(it);
1088 1089 1090 1091
		it = alpha1(it);
	} while (it != d);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1092
template <typename MAP_IMPL>
1093 1094
template <typename FUNC>
inline void GMap2<MAP_IMPL>::foreach_dart_of_vertex(Dart d, FUNC& f, unsigned int thread) const
1095
{
1096 1097
	GMap2<MAP_IMPL>::foreach_dart_of_oriented_vertex(d, f, thread);
	GMap2<MAP_IMPL>::foreach_dart_of_oriented_vertex(this->beta1(d), f, thread);
1098 1099
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1100
template <typename MAP_IMPL>
1101 1102
template <typename FUNC>
inline void GMap2<MAP_IMPL>::foreach_dart_of_oriented_edge(Dart d, FUNC& f, unsigned int /*thread*/) const
1103
{
1104 1105
	f(d);
	f(beta2(this->beta0(d)));
1106 1107
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1108
template <typename MAP_IMPL>
1109 1110
template <typename FUNC>
inline void GMap2<MAP_IMPL>::foreach_dart_of_edge(Dart d, FUNC& f, unsigned int /*thread*/) const
1111
{
1112
	f(d);
1113
	Dart e = this->beta0(d) ;
1114
	f(e);
1115
	e = beta2(d) ;
1116
	f(e);
1117
	e = this->beta0(e) ;
1118
	f(e);
1119 1120
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1121
template <typename MAP_IMPL>
1122 1123
template <typename FUNC>
inline void GMap2<MAP_IMPL>::foreach_dart_of_oriented_face(Dart d, FUNC& f, unsigned int thread) const
1124
{
1125
	ParentMap::foreach_dart_of_oriented_cc(d, f, thread);
1126 1127
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1128
template <typename MAP_IMPL>
1129 1130
template <typename FUNC>
inline void GMap2<MAP_IMPL>::foreach_dart_of_face(Dart d, FUNC& f, unsigned int thread) const
1131
{
1132
	ParentMap::foreach_dart_of_cc(d, f, thread);
1133 1134
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1135
template <typename MAP_IMPL>
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
template <typename FUNC>
inline void GMap2<MAP_IMPL>::foreach_dart_of_volume(Dart d, FUNC& f, unsigned int thread) const
{
	foreach_dart_of_cc(d, f, thread);
}

template <typename MAP_IMPL>
template <typename FUNC>
inline void GMap2<MAP_IMPL>::foreach_dart_of_vertex1(Dart d, FUNC& f, unsigned int thread) const
{
	ParentMap::foreach_dart_of_vertex(d, f, thread);
}

template <typename MAP_IMPL>
template <typename FUNC>
inline void GMap2<MAP_IMPL>::foreach_dart_of_edge1(Dart d, FUNC& f, unsigned int thread) const
1152
{
1153 1154 1155 1156 1157 1158 1159 1160
	ParentMap::foreach_dart_of_edge(d, f, thread);
}

template <typename MAP_IMPL>
template <typename FUNC>
void GMap2<MAP_IMPL>::foreach_dart_of_oriented_cc(Dart d, FUNC& f, unsigned int thread) const
{
	DartMarkerStore<GMap2<MAP_IMPL> > mark(*this, thread);	// Lock a marker
1161 1162 1163 1164 1165 1166

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

	// For every face added to the list
1167
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
1168 1169 1170 1171
	{
		if (!mark.isMarked(visitedFaces[i]))		// Face has not been visited yet
		{
			// Apply functor to the darts of the face
1172
			foreach_dart_of_oriented_face(visitedFaces[i], f);
1173

1174
			// mark visited darts (current face)
1175
			// and add non visited adjacent faces to the list of face
1176 1177
			Dart e = visitedFaces[i] ;
			do
1178
			{
1179 1180 1181 1182 1183 1184
				mark.mark(e);					// Mark
				Dart adj = phi2(e);				// Get adjacent face
				if (!mark.isMarked(adj))
					visitedFaces.push_back(adj);	// Add it
				e = this->phi1(e);
			} while(e != visitedFaces[i]);
1185 1186 1187 1188
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1189
template <typename MAP_IMPL>
1190 1191
template <typename FUNC>
inline void GMap2<MAP_IMPL>::foreach_dart_of_cc(Dart d, FUNC& f, unsigned int thread) const
1192
{
1193 1194
	GMap2<MAP_IMPL>::foreach_dart_of_oriented_cc(d, f, thread);
	GMap2<MAP_IMPL>::foreach_dart_of_oriented_cc(this->beta0(d), f, thread);
1195 1196 1197 1198 1199 1200
}

/*! @name Close map after import or creation
 *  These functions must be used with care, generally only by import/creation algorithms
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
1201 1202
template <typename MAP_IMPL>
Dart GMap2<MAP_IMPL>::newBoundaryCycle(unsigned int nbE)
1203 1204
{
	Dart d = ParentMap::newCycle(nbE);
1205
	Algo::Topo::boundaryMarkOrbit<2,FACE>(*this, d);
1206 1207 1208
	return d;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1209 1210
template <typename MAP_IMPL>
unsigned int GMap2<MAP_IMPL>::closeHole(Dart d, bool forboundary)
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 1239 1240 1241 1242 1243 1244 1245 1246
{
	assert(beta2(d) == d);		// Nothing to close

	Dart first = ParentMap::newEdge();	// First edge of the face that will fill the hole
	unsigned int countEdges = 1;

	beta2sew(d, this->beta0(first));	// sew the new edge to the hole
	beta2sew(first, this->beta0(d));

	Dart prev = first ;
	Dart dNext = d;	// Turn around the hole
	Dart dPhi1;		// to complete the face
	do
	{
		dPhi1 = this->phi1(dNext) ;
		dNext = beta2(dPhi1) ;
		while(dNext != dPhi1 && dPhi1 != d)
		{
			dPhi1 = this->beta1(dNext) ;	// Search and put in dNext
			dNext = beta2(dPhi1) ;			// the next dart of the hole
		}

		if (dPhi1 != d)
		{
			Dart next = ParentMap::newEdge();		// Add a new edge there and link it to the face
			++countEdges;
			this->beta1sew(this->beta0(next), prev);// the edge is linked to the face
			prev = next ;
			beta2sew(dNext, this->beta0(next));		// the face is linked to the hole
			beta2sew(next, this->beta0(dNext));
		}
	} while (dPhi1 != d);

	this->beta1sew(prev, this->beta0(first)) ;

	if(forboundary)
1247
		Algo::Topo::boundaryMarkOrbit<2,FACE>(*this, phi2(d));
1248 1249 1250 1251

	return countEdges ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1252 1253
template <typename MAP_IMPL>
unsigned int GMap2<MAP_IMPL>::closeMap()
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
{
	// Search the map for topological holes (fix points of phi2)
	unsigned int nb = 0 ;
	for (Dart d = this->begin(); d != this->end(); this->next(d))
	{
		if (beta2(d) == d)
		{
			++nb ;
			closeHole(d);
		}
	}
	return nb ;
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
1272 1273
template <typename MAP_IMPL>
void GMap2<MAP_IMPL>::computeDual()
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
{
//	DartAttribute<Dart> old_beta0 = this->getAttribute<Dart, DART>("beta0");
//	DartAttribute<Dart> old_beta2 = this->getAttribute<Dart, DART>("beta2") ;
//
//	swapAttributes<Dart>(old_beta0, old_beta2) ;
//
//	swapEmbeddingContainers(VERTEX, FACE) ;
//
//	//boundary management ?
}

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