map2.hpp 31.3 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 Map2<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 Map2<MAP_IMPL>::Map2() : Map1<MAP_IMPL>()
36 37 38 39
{
	init() ;
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
template <typename MAP_IMPL>
inline unsigned int Map2<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 Map2<MAP_IMPL>::clear(bool removeAttrib)
54
{
55
	ParentMap::clear(removeAttrib) ;
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 Map2<MAP_IMPL>::getNbInvolutions() const
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 Map2<MAP_IMPL>::getNbPermutations() const
{
	return ParentMap::getNbPermutations();
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 Map2<MAP_IMPL>::phi2(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
78
{
Pierre Kraemer's avatar
Pierre Kraemer committed
79
	return MAP_IMPL::template getInvolution<0>(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 Map2<MAP_IMPL>::phi(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-phi");
	if (N < 10)
	{
		switch(N)
		{
91
			case 1 : return this->phi1(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
92 93 94 95 96 97
			case 2 : return phi2(d) ;
			default : assert(!"Wrong multi-phi relation value") ; return d ;
		}
	}
	switch(N%10)
	{
98
		case 1 : return this->phi1(phi<N/10>(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
99 100 101 102 103
		case 2 : return phi2(phi<N/10>(d)) ;
		default : assert(!"Wrong multi-phi relation value") ; return d ;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
104 105
template <typename MAP_IMPL>
inline Dart Map2<MAP_IMPL>::alpha0(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107 108 109
{
	return phi2(d) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
110 111
template <typename MAP_IMPL>
inline Dart Map2<MAP_IMPL>::alpha1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
112
{
113
	return phi2(this->phi_1(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
114 115
}

Pierre Kraemer's avatar
Pierre Kraemer committed
116 117
template <typename MAP_IMPL>
inline Dart Map2<MAP_IMPL>::alpha_1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
118
{
119
	return this->phi1(phi2(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121
}

Pierre Kraemer's avatar
Pierre Kraemer committed
122 123
template <typename MAP_IMPL>
inline Dart Map2<MAP_IMPL>::phi2_1(Dart d) const
124
{
125
	return phi2(this->phi_1(d)) ;
126 127
}

Pierre Kraemer's avatar
Pierre Kraemer committed
128 129
template <typename MAP_IMPL>
inline Dart Map2<MAP_IMPL>::phi12(Dart d) const
130
{
131
	return this->phi1(phi2(d)) ;
132 133
}

Pierre Kraemer's avatar
Pierre Kraemer committed
134 135
template <typename MAP_IMPL>
inline void Map2<MAP_IMPL>::phi2sew(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
136
{
Pierre Kraemer's avatar
Pierre Kraemer committed
137
	MAP_IMPL::template involutionSew<0>(d,e);
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139
}

Pierre Kraemer's avatar
Pierre Kraemer committed
140 141
template <typename MAP_IMPL>
inline void Map2<MAP_IMPL>::phi2unsew(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
142
{
Pierre Kraemer's avatar
Pierre Kraemer committed
143
	MAP_IMPL::template involutionUnsew<0>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145
}

146 147 148 149
/*! @name Generator and Deletor
 *  To generate or delete faces in a 2-map
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
150 151
template <typename MAP_IMPL>
Dart Map2<MAP_IMPL>::newPolyLine(unsigned int nbEdges)
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
{
	Dart d = ParentMap::newCycle(2*nbEdges);
	{
		Dart it1 = d;
		Dart it2 = this->phi_1(d);
		for(unsigned int i = 0; i < nbEdges ; ++i)
		{
			phi2sew(it1, it2);
			it1 = this->phi1(it1);
			it2 = this->phi_1(it2);
		}
	}
	return d;
}

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
186
template <typename MAP_IMPL>
187
void Map2<MAP_IMPL>::deleteFace(Dart d)
188
{
Pierre Kraemer's avatar
Pierre Kraemer committed
189
	assert(!this->template isBoundaryMarked<2>(d)) ;
190

191 192 193
	Dart it = d ;
	do
	{
194 195
		if(!isBoundaryEdge(it))
			unsewFaces(it) ;
196 197
		it = this->phi1(it) ;
	} while(it != d) ;
198 199 200
	Dart dd = phi2(d) ;
	ParentMap::deleteCycle(d) ;
	ParentMap::deleteCycle(dd) ;
201 202
}

Sylvain Thery's avatar
Sylvain Thery committed
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
//template <typename MAP_IMPL>
//void Map2<MAP_IMPL>::deleteCC(Dart d)
//{
//	DartMarkerNoUnmark<MAP_IMPL> mark(*this);

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

//	for(unsigned int i = 0; i < visited.size(); ++i)
//	{
//		Dart d1 = this->phi1(visited[i]) ;
//		if(!mark.isMarked(d1))
//		{
//			visited.push_back(d1) ;
//			mark.mark(d1);
//		}
//		Dart d2 = phi2(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)
//	{
//		mark.unmark(*it);
//		this->deleteDart(*it) ;
//	}
//}

Pierre Kraemer's avatar
Pierre Kraemer committed
236 237
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::deleteCC(Dart d)
238
{
Sylvain Thery's avatar
Sylvain Thery committed
239
//TODO Try to remove the use of vector<Dart>
240
	DartMarkerNoUnmark<MAP_IMPL> mark(*this);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

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

	for(unsigned int i = 0; i < visited.size(); ++i)
	{
		Dart d1 = this->phi1(visited[i]) ;
		if(!mark.isMarked(d1))
		{
			visited.push_back(d1) ;
			mark.mark(d1);
		}
		Dart d2 = phi2(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)
264 265
	{
		mark.unmark(*it);
266
		this->deleteDart(*it) ;
267
	}
268 269
}

Pierre Kraemer's avatar
Pierre Kraemer committed
270 271
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::fillHole(Dart d)
272 273
{
	assert(isBoundaryEdge(d)) ;
274 275
	if(!this->template isBoundaryMarked<2>(d))
		d = phi2(d) ;
276
	Algo::Topo::boundaryUnmarkOrbit<2,FACE>(*this, d) ;
277 278
}

Pierre Kraemer's avatar
Pierre Kraemer committed
279 280
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::createHole(Dart d)
281 282
{
	assert(!isBoundaryEdge(d)) ;
283
	Algo::Topo::boundaryMarkOrbit<2,FACE>(*this, d) ;
284 285 286 287 288 289
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
290 291
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::splitVertex(Dart d, Dart e)
292 293 294 295 296 297 298 299 300
{
	assert(sameVertex(d, e)) ;
	Dart d2 = phi2(d) ; assert(d != d2) ;
	Dart e2 = phi2(e) ; assert(e != e2) ;
	Dart nd = ParentMap::cutEdge(d2) ;	// Cut the edge of dd (make a new half edge)
	Dart ne = ParentMap::cutEdge(e2) ;	// Cut the edge of ee (make a new half edge)
	phi2sew(nd, ne) ;					// Sew the two faces along the new edge
}

Pierre Kraemer's avatar
Pierre Kraemer committed
301 302
template <typename MAP_IMPL>
Dart Map2<MAP_IMPL>::deleteVertex(Dart d)
303 304 305 306 307 308 309 310 311 312 313 314
{
	//TODO utile ?
	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) ;

315
		Dart f = this->phi_1(phi2(vit)) ;
316 317 318 319 320 321 322 323
		this->phi1sew(vit, f) ;

		vit = phi2(this->phi_1(vit)) ;
	} while(vit != d) ;
	ParentMap::deleteCycle(d) ;
	return res ;
}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::mergeOppositeVertices(Dart d)
{
	Dart d2 = phi2(d);
	Dart d_12 = phi2(this->phi_1(d));
	Dart d_112 = phi2(this->phi_1(this->phi_1(d)));
	Dart d12 = phi2(this->phi1(d));

	phi2unsew(d2);
	phi2unsew(d_12);
	phi2unsew(d_112);
	phi2unsew(d12);

	phi2sew(d2,d12);
	phi2sew(d_12,d_112);

	ParentMap::deleteCycle(d);
}


Pierre Kraemer's avatar
Pierre Kraemer committed
344 345
template <typename MAP_IMPL>
Dart Map2<MAP_IMPL>::cutEdge(Dart d)
346 347 348 349 350 351 352 353 354 355
{
	Dart e = phi2(d);
	phi2unsew(d);						// remove old phi2 links
	Dart nd = ParentMap::cutEdge(d);	// Cut the 1-edge of d
	Dart ne = ParentMap::cutEdge(e);	// Cut the 1-edge of phi2(d)
	phi2sew(d, ne);						// Correct the phi2 links
	phi2sew(e, nd);
	return nd;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
356 357
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::uncutEdge(Dart d)
358 359 360 361 362 363 364 365 366 367 368 369 370 371
{
	if(vertexDegree(this->phi1(d)) == 2)
	{
		Dart e = phi2(this->phi1(d)) ;
		phi2unsew(e) ;
		phi2unsew(d) ;
		ParentMap::uncutEdge(d) ;
		ParentMap::uncutEdge(e) ;
		phi2sew(d, e) ;
		return true ;
	}
	return false ;
}

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

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

	Dart f = this->phi1(e) ;
	Dart h = phi2(this->phi_1(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 = phi2(this->phi_1(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
413 414
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::flipEdge(Dart d)
415
{
416
	if (!isBoundaryEdge(d)) // cannot flip a boundary edge
417 418 419 420 421 422 423 424 425 426 427 428
	{
		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);
		this->phi1sew(d, ePrev);	// Detach the two
		this->phi1sew(e, dPrev);	// vertices of the edge
		this->phi1sew(d, dNext);	// Insert the edge in its
		this->phi1sew(e, eNext);	// new vertices after flip
		return true ;
	}
429
	return false ;
430 431
}

Pierre Kraemer's avatar
Pierre Kraemer committed
432 433
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::flipBackEdge(Dart d)
434
{
435
	if (!isBoundaryEdge(d)) // cannot flip a boundary edge
436 437 438 439
	{
		Dart e = phi2(d);
		Dart dPrev = this->phi_1(d);
		Dart ePrev = this->phi_1(e);
440 441
		this->phi1sew(d, ePrev);				// Detach the two
		this->phi1sew(e, dPrev);				// vertices of the edge
442 443 444 445
		this->phi1sew(e, this->phi_1(dPrev));	// Insert the edge in its
		this->phi1sew(d, this->phi_1(ePrev));	// new vertices after flip
		return true ;
	}
446
	return false ;
447 448
}

Pierre Kraemer's avatar
Pierre Kraemer committed
449 450
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::swapEdges(Dart d, Dart e)
451
{
452
	assert(!isBoundaryEdge(d) && !isBoundaryEdge(e));
453 454 455 456 457 458 459 460 461 462 463

	Dart d2 = phi2(d);
	Dart e2 = phi2(e);

	phi2unsew(d);
	phi2unsew(e) ;

	phi2sew(d, e);
	phi2sew(d2, e2);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
464 465
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::insertEdgeInVertex(Dart d, Dart e)
466 467 468
{
	assert(!sameVertex(d,e));
	assert(phi2(e) == this->phi_1(e));
469
	this->phi1sew(this->phi_1(d), this->phi_1(e));
470 471
}

Pierre Kraemer's avatar
Pierre Kraemer committed
472 473
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::removeEdgeFromVertex(Dart d)
474 475 476 477 478 479 480 481 482
{
	if (!isBoundaryEdge(d))
	{
		this->phi1sew(this->phi_1(d), phi2(d)) ;
		return true ;
	}
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
483 484
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::sewFaces(Dart d, Dart e, bool withBoundary)
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
{
	// if sewing with fixed points
	if (!withBoundary)
	{
		assert(phi2(d) == d && phi2(e) == e) ;
		phi2sew(d, e) ;
		return ;
	}

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

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

	phi2unsew(d) ;	// unsew faces from boundary
	phi2unsew(e) ;

	if (ee != this->phi_1(dd))
		this->phi1sew(ee, this->phi_1(dd)) ;	// remove the boundary edge
	if (dd != this->phi_1(ee))
		this->phi1sew(dd, this->phi_1(ee)) ;	// and properly close incident boundaries
	ParentMap::deleteCycle(dd) ;

	phi2sew(d, e) ; // sew the faces
}

Pierre Kraemer's avatar
Pierre Kraemer committed
511 512
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::unsewFaces(Dart d, bool withBoundary)
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
{
	if (!withBoundary)
	{
		phi2unsew(d) ;
		return ;
	}

	assert(!Map2::isBoundaryEdge(d)) ;

	Dart dd = phi2(d) ;

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

	Dart f = findBoundaryEdgeOfVertex(d) ;
	Dart ff = findBoundaryEdgeOfVertex(dd) ;

	if(f != NIL)
		this->phi1sew(e, this->phi_1(f)) ;

	if(ff != NIL)
		this->phi1sew(ee, this->phi_1(ff)) ;

	phi2unsew(d) ;

	phi2sew(d, e) ;		// sew faces
	phi2sew(dd, ee) ;	// to the boundary
}

Pierre Kraemer's avatar
Pierre Kraemer committed
542 543
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::collapseDegeneratedFace(Dart d)
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
{
	Dart e = this->phi1(d) ;		// Check if the face is degenerated
	if (this->phi1(e) == d)			// Yes: it contains one or two edge(s)
	{
		Dart d2 = phi2(d) ;			// Check opposite edges
		Dart e2 = phi2(e) ;
		phi2unsew(d) ;
		if(d != e)
		{
			phi2unsew(e) ;
			phi2sew(d2, e2) ;
		}
		else
		{
			this->phi1sew(d2, this->phi_1(d2)) ;
			ParentMap::deleteCycle(d2) ;
		}
		ParentMap::deleteCycle(d) ;
		return true ;
	}
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
567 568
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::splitFace(Dart d, Dart e)
569
{
Pierre Kraemer's avatar
Pierre Kraemer committed
570
	assert(d != e && Map2<MAP_IMPL>::sameFace(d, e)) ;
571 572 573 574 575 576
	Dart dd = ParentMap::cutEdge(this->phi_1(d)) ;
	Dart ee = ParentMap::cutEdge(this->phi_1(e)) ;
	ParentMap::splitCycle(dd, ee) ;
	phi2sew(dd, ee);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
577 578
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::mergeFaces(Dart d)
579 580 581 582 583 584 585 586 587 588 589 590 591
{
	if (!isBoundaryEdge(d))
	{
		Dart e = phi2(d) ;
		phi2unsew(d) ;
		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
592 593
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::extractTrianglePair(Dart d)
594 595 596
{
	Dart e = phi2(d) ;

597
	assert(!isFaceIncidentToBoundary(d) && !isFaceIncidentToBoundary(e)) ;
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
	assert(faceDegree(d) == 3 && faceDegree(e) == 3) ;

	Dart d1 = phi2(this->phi1(d)) ;
	Dart d2 = phi2(this->phi_1(d)) ;
	phi2unsew(d1) ;
	phi2unsew(d2) ;
	phi2sew(d1, d2) ;

	Dart e1 = phi2(this->phi1(e)) ;
	Dart e2 = phi2(this->phi_1(e)) ;
	phi2unsew(e1) ;
	phi2unsew(e2) ;
	phi2sew(e1, e2) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
613 614
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::insertTrianglePair(Dart d, Dart v1, Dart v2)
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
{
	Dart e = phi2(d) ;

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

	Dart vv1 = phi2(v1) ;
	phi2unsew(v1) ;
	phi2sew(this->phi_1(d), v1) ;
	phi2sew(this->phi1(d), vv1) ;

	Dart vv2 = phi2(v2) ;
	phi2unsew(v2) ;
	phi2sew(this->phi_1(e), v2) ;
	phi2sew(this->phi1(e), vv2) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
632 633
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::mergeVolumes(Dart d, Dart e, bool deleteFace)
634
{
Pierre Kraemer's avatar
Pierre Kraemer committed
635
	assert(!this->template isBoundaryMarked<2>(d) && !this->template isBoundaryMarked<2>(e)) ;
636

637
	if (isFaceIncidentToBoundary(d) || isFaceIncidentToBoundary(e))
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
		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);
		phi2unsew(d2);		// Unlink the two adjacent faces from dNext and eNext
		phi2unsew(e2);
		phi2sew(d2, e2);	// Link the two adjacent faces together
	}

	if(deleteFace)
	{
		ParentMap::deleteCycle(d);		// Delete the two alone faces
		ParentMap::deleteCycle(e);
	}

	return true ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
686 687
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::splitSurface(std::vector<Dart>& vd, bool firstSideClosed, bool secondSideClosed)
688 689 690 691 692 693 694 695 696
{
//	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)
	{
Lionel Untereiner's avatar
Lionel Untereiner committed
697
		if(!Map2<MAP_IMPL>::isBoundaryEdge(*it))
698 699 700 701
			unsewFaces(*it) ;
	}

	if(firstSideClosed)
702
		fillHole(e) ;
703 704

	if(secondSideClosed)
705
		fillHole(e2) ;
706 707
}

Pierre Kraemer's avatar
Pierre Kraemer committed
708 709 710 711
/*! @name Topological Queries
 *  Return or set various topological information
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
712
template <typename MAP_IMPL>
713
bool Map2<MAP_IMPL>::sameOrientedVertex(Vertex v1, Vertex v2) const
714
{
715
	Dart it = v1.dart;			// Foreach dart in vertex v1
716 717
	do
	{
718
		if (it == v2.dart)		// Test equality with v2
719 720
			return true;
		it = phi2(this->phi_1(it));
721
	} while (it != v1.dart);
722 723 724
	return false;				// None is equal to e => vertices are distinct
}

Pierre Kraemer's avatar
Pierre Kraemer committed
725
template <typename MAP_IMPL>
726
inline bool Map2<MAP_IMPL>::sameVertex(Vertex v1, Vertex v2) const
Pierre Kraemer's avatar
Pierre Kraemer committed
727
{
728
	return sameOrientedVertex(v1, v2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
729 730
}

Pierre Kraemer's avatar
Pierre Kraemer committed
731
template <typename MAP_IMPL>
732
unsigned int Map2<MAP_IMPL>::vertexDegree(Vertex v) const
733 734
{
	unsigned int count = 0 ;
735
	Dart it = v.dart ;
736 737 738 739
	do
	{
		++count ;
		it = phi2(this->phi_1(it)) ;
740
	} while (it != v.dart) ;
741 742 743
	return count ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
744
template <typename MAP_IMPL>
745
int Map2<MAP_IMPL>::checkVertexDegree(Vertex v, unsigned int vd) const
746 747
{
	unsigned int count = 0 ;
748
	Dart it = v.dart ;
749 750 751 752
	do
	{
		++count ;
		it = phi2(this->phi_1(it)) ;
753
	} while ((count <= vd) && (it != v.dart)) ;
754 755 756 757

	return count - vd;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
758
template <typename MAP_IMPL>
759
bool Map2<MAP_IMPL>::isBoundaryVertex(Vertex v) const
760
{
761
	Dart it = v.dart ;
762 763
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
764
		if (this->template isBoundaryMarked<2>(it))
765 766
			return true ;
		it = phi2(this->phi_1(it)) ;
767
	} while (it != v.dart) ;
768 769 770
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
771
template <typename MAP_IMPL>
772
Dart Map2<MAP_IMPL>::findBoundaryEdgeOfVertex(Vertex v) const
773
{
774
	Dart it = v.dart ;
775 776
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
777
		if (this->template isBoundaryMarked<2>(it))
778 779
			return it ;
		it = phi2(this->phi_1(it)) ;
780
	} while (it != v.dart) ;
781 782 783
	return NIL ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
784
template <typename MAP_IMPL>
785
inline bool Map2<MAP_IMPL>::sameEdge(Edge e1, Edge e2) const
786
{
787
	return e1.dart == e2.dart || phi2(e1.dart) == e2.dart ;
788 789
}

Pierre Kraemer's avatar
Pierre Kraemer committed
790
template <typename MAP_IMPL>
791
inline bool Map2<MAP_IMPL>::isBoundaryEdge(Edge e) const
untereiner's avatar
untereiner committed
792
{
793
	return this->template isBoundaryMarked<2>(e.dart) || this->template isBoundaryMarked<2>(phi2(e.dart));
untereiner's avatar
untereiner committed
794 795
}

Pierre Kraemer's avatar
Pierre Kraemer committed
796
template <typename MAP_IMPL>
797
inline bool Map2<MAP_IMPL>::sameOrientedFace(Face f1, Face f2) const
798
{
799
	return ParentMap::sameCycle(f1, f2) ;
800 801
}

Pierre Kraemer's avatar
Pierre Kraemer committed
802
template <typename MAP_IMPL>
803
inline bool Map2<MAP_IMPL>::sameFace(Face f1, Face f2) const
804
{
805
	return sameOrientedFace(f1, f2) ;
806 807
}

Pierre Kraemer's avatar
Pierre Kraemer committed
808
template <typename MAP_IMPL>
809
inline unsigned int Map2<MAP_IMPL>::faceDegree(Face f) const
810
{
811
	return ParentMap::cycleDegree(f) ;
812 813
}

Pierre Kraemer's avatar
Pierre Kraemer committed
814
template <typename MAP_IMPL>
815
inline int Map2<MAP_IMPL>::checkFaceDegree(Face f, unsigned int fd) const
816
{
817
	return ParentMap::checkCycleDegree(f, fd) ;
818 819
}

Pierre Kraemer's avatar
Pierre Kraemer committed
820
template <typename MAP_IMPL>
821
bool Map2<MAP_IMPL>::isFaceIncidentToBoundary(Face f) const
822
{
823
	Dart it = f.dart ;
824 825
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
826
		if (this->template isBoundaryMarked<2>(phi2(it)))
827 828
			return true ;
		it = this->phi1(it) ;
829
	} while (it != f.dart) ;
830 831 832
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
833
template <typename MAP_IMPL>
834
Dart Map2<MAP_IMPL>::findBoundaryEdgeOfFace(Face f) const
835
{
836
	Dart it = f.dart ;
837 838
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
839
		if (this->template isBoundaryMarked<2>(phi2(it)))
840 841
			return phi2(it) ;
		it = this->phi1(it) ;
842
	} while (it != f.dart) ;
843 844 845
	return NIL ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
846
template <typename MAP_IMPL>
847
bool Map2<MAP_IMPL>::sameOrientedVolume(Vol v1, Vol v2) const
848
{
849
	DartMarkerStore<Map2<MAP_IMPL> > mark(*this);	// Lock a marker
850

851 852
	std::list<Dart> visitedFaces;		// Faces that are traversed
	visitedFaces.push_back(v1.dart);	// Start with a face of v1
853 854 855 856 857
	std::list<Dart>::iterator face;

	// For every face added to the list
	for (face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
	{
858 859
		// Face has not been visited yet
		if (!this->template isBoundaryMarked<2>(*face) && !mark.isMarked(*face))
860 861 862 863
		{
			Dart it = *face ;
			do
			{
864
				if(it == v2.dart)
865 866 867 868
					return true;

				mark.mark(it);						// Mark
				Dart adj = phi2(it);				// Get adjacent face
Sylvain Thery's avatar
Sylvain Thery committed
869
				if (!this->template isBoundaryMarked<2>(adj) && !mark.isMarked(adj))
870 871 872 873 874 875 876 877
					visitedFaces.push_back(adj);	// Add it
				it = this->phi1(it);
			} while(it != *face);
		}
	}
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
878
template <typename MAP_IMPL>
879
inline bool Map2<MAP_IMPL>::sameVolume(Vol v1, Vol v2) const
880
{
881
	return sameOrientedVolume(v1, v2) ;
882 883
}

Pierre Kraemer's avatar
Pierre Kraemer committed
884
template <typename MAP_IMPL>
885
unsigned int Map2<MAP_IMPL>::volumeDegree(Vol v) const
886 887
{
	unsigned int count = 0;
888
	DartMarkerStore<Map2<MAP_IMPL> > mark(*this);		// Lock a marker
889 890 891

	std::vector<Dart> visitedFaces;		// Faces that are traversed
	visitedFaces.reserve(16);
892
	visitedFaces.push_back(v.dart);		// Start with a face of v
893 894 895 896 897

	// 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
898
		if (!this->template isBoundaryMarked<2>(df) && !mark.isMarked(df))		// Face has not been visited yet
899 900 901 902 903 904 905
		{
			++count;
			Dart it = df ;
			do
			{
				mark.mark(it);					// Mark
				Dart adj = phi2(it);			// Get adjacent face
906
				if ( !this->template isBoundaryMarked<2>(adj) && !mark.isMarked(adj) )
907 908 909 910 911 912 913 914 915
					visitedFaces.push_back(adj);// Add it
				it = this->phi1(it);
			} while(it != df);
		}
	}

	return count;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
916
template <typename MAP_IMPL>
917
int Map2<MAP_IMPL>::checkVolumeDegree(Vol v, unsigned int vd) const
918 919
{
	unsigned int count = 0;
920
	DartMarkerStore<Map2<MAP_IMPL> > mark(*this);		// Lock a marker
921 922 923

	std::vector<Dart> visitedFaces;		// Faces that are traversed
	visitedFaces.reserve(16);
924
	visitedFaces.push_back(v.dart);		// Start with a face of v
925 926 927 928 929

	// 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
930
		if (!this->template isBoundaryMarked<2>(df) && !mark.isMarked(df))		// Face has not been visited yet
931 932 933 934 935 936 937
		{
			++count;
			Dart it = df ;
			do
			{
				mark.mark(it);					// Mark
				Dart adj = phi2(it);			// Get adjacent face
Sylvain Thery's avatar
Sylvain Thery committed
938
				if ( !this->isBoundaryMarked<2>(adj) && !mark.isMarked(adj) )
939 940 941 942
					visitedFaces.push_back(adj);// Add it
				it = this->phi1(it);
			} while(it != df);
		}
943
		if (count > vd)
944 945 946
			break;
	}

947
	return count - vd;
948 949
}

Pierre Kraemer's avatar
Pierre Kraemer committed
950 951
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::isTriangular() const
952
{
953
	bool tri = true;
Sylvain Thery's avatar
Sylvain Thery committed
954
	foreach_cell_until<FACE>(*this, [&] (Face f)
955
	{
956 957
		if (this->faceDegree(f) != 3)
			tri = false;
958
		return tri;
959 960
	});
	return tri;
961 962
}

963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::isOpen() const
{
	bool open = false;
	for(Dart d = Map2::begin(); d != Map2::end(); Map2::next(d))
	{
		if(this->template isBoundaryMarked<2>(d))
		{
			open = true;
			return open;
		}

	}

	return open;
}


Pierre Kraemer's avatar
Pierre Kraemer committed
981 982
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::check() const
983 984
{
	CGoGNout << "Check: topology begin" << CGoGNendl;
985
	DartMarker<Map2<MAP_IMPL> > m(*this);
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
	for(Dart d = Map2::begin(); d != Map2::end(); Map2::next(d))
	{
		Dart d2 = phi2(d);
		if (phi2(d2) != d)	// phi2 involution ?
		{
			CGoGNout << "Check: phi2 is not an involution" << CGoGNendl;
			return false;
		}
		if(d2 == d)
		{
			CGoGNout << "Check: phi2 fixed point" << CGoGNendl;
			return false;
		}

		Dart d1 = this->phi1(d);
		if (this->phi_1(d1) != d)	// phi1 a une image correcte ?
		{
			CGoGNout << "Check: inconsistent 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 (phi2(d1) == d)
			CGoGNout << "Check: (warning) dangling edge" << CGoGNendl;
	}

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

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

	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1036 1037
template <typename MAP_IMPL>
bool Map2<MAP_IMPL>::checkSimpleOrientedPath(std::vector<Dart>& vd)
1038
{
1039
	DartMarkerStore<Map2<MAP_IMPL> > dm(*this) ;
1040 1041 1042 1043
	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
1044
		dm.template markOrbit<VERTEX>(*it) ;
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

		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
1058 1059 1060 1061
/*! @name Cell Functors
 *  Apply functors to all darts of a cell
 *************************************************************************/

1062 1063
//template <typename MAP_IMPL>
//template <unsigned int ORBIT, typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
1064
//void Map2<MAP_IMPL>::foreach_dart_of_orbit(Cell<ORBIT> c, FUNC f) const
1065 1066 1067 1068
//{
//	switch(ORBIT)
//	{
//		case DART:		f(c); break;
Sylvain Thery's avatar
Sylvain Thery committed
1069 1070 1071 1072 1073 1074
//		case VERTEX: 	foreach_dart_of_vertex(c, f); break;
//		case EDGE: 		foreach_dart_of_edge(c, f); break;
//		case FACE: 		foreach_dart_of_face(c, f); break;
//		case VOLUME: 	foreach_dart_of_volume(c, f); break;
//		case VERTEX1: 	foreach_dart_of_vertex1(c, f); break;
//		case EDGE1: 	foreach_dart_of_edge1(c, f); break;
1075 1076 1077
//		default: 		assert(!"Cells of this dimension are not handled"); break;
//	}
//}
1078 1079 1080

template <typename MAP_IMPL>
template <unsigned int ORBIT, typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
1081
void Map2<MAP_IMPL>::foreach_dart_of_orbit(Cell<ORBIT> c,const FUNC& f) const
1082 1083 1084 1085
{
	switch(ORBIT)
	{
		case DART:		f(c); break;
Sylvain Thery's avatar
Sylvain Thery committed
1086 1087 1088 1089 1090 1091
		case VERTEX: 	foreach_dart_of_vertex(c, f); break;
		case EDGE: 		foreach_dart_of_edge(c, f); break;
		case FACE: 		foreach_dart_of_face(c, f); break;
		case VOLUME: 	foreach_dart_of_volume(c, f); break;
		case VERTEX1: 	foreach_dart_of_vertex1(c, f); break;
		case EDGE1: 	foreach_dart_of_edge1(c, f); break;
1092 1093 1094 1095 1096 1097
		default: 		assert(!"Cells of this dimension are not handled"); break;
	}
}

template <typename MAP_IMPL>
template <typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
1098
inline void Map2<MAP_IMPL>::foreach_dart_of_vertex(Dart d, const FUNC& f) const
1099 1100 1101 1102
{
	Dart dNext = d;
	do
	{
1103
		f(dNext);
1104 1105 1106 1107
		dNext = phi2(this->phi_1(dNext));
	} while (dNext != d);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1108
template <typename MAP_IMPL>
1109
template <typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
1110
inline void Map2<MAP_IMPL>::foreach_dart_of_edge(Dart d, const FUNC& f) const
1111
{
1112 1113
	f(d);
	f(phi2(d));
1114 1115
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1116
template <typename MAP_IMPL>
1117
template <typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
1118
inline void Map2<MAP_IMPL>::foreach_dart_of_face(Dart d, const FUNC& f) const
Pierre Kraemer's avatar
Pierre Kraemer committed
1119
{
Sylvain Thery's avatar
Sylvain Thery committed
1120
	ParentMap::foreach_dart_of_cc(d, f);
Pierre Kraemer's avatar
Pierre Kraemer committed
1121 1122
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1123
template <typename MAP_IMPL>
1124
template <typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
1125
inline void Map2<MAP_IMPL>::foreach_dart_of_volume(Dart d, const FUNC& f) const
Pierre Kraemer's avatar
Pierre Kraemer committed
1126
{
Sylvain Thery's avatar
Sylvain Thery committed
1127
	foreach_dart_of_cc(d, f);
Pierre Kraemer's avatar
Pierre Kraemer committed
1128
}
1129

Pierre Kraemer's avatar
Pierre Kraemer committed
1130
template <typename MAP_IMPL>
1131
template <typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
1132
inline void Map2<MAP_IMPL>::foreach_dart_of_vertex1(Dart d, const FUNC& f) const
1133
{
Sylvain Thery's avatar
Sylvain Thery committed
1134
	return ParentMap::foreach_dart_of_vertex(d, f);
1135 1136 1137 1138
}

template <typename MAP_IMPL>
template <typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
1139
inline void Map2<MAP_IMPL>::foreach_dart_of_edge1(Dart d, const FUNC& f) const
1140
{
Sylvain Thery's avatar
Sylvain Thery committed
1141
	return ParentMap::foreach_dart_of_edge(d, f);
1142 1143 1144 1145
}

template <typename MAP_IMPL>
template <typename FUNC>
Sylvain Thery's avatar
Sylvain Thery committed
1146
void Map2<MAP_IMPL>::foreach_dart_of_cc(Dart d, const FUNC& f) const
1147
{
Sylvain Thery's avatar
Sylvain Thery committed
1148
	DartMarkerStore<Map2<MAP_IMPL> > mark(*this);	// Lock a marker
1149

Sylvain Thery's avatar
Sylvain Thery committed
1150 1151 1152
	std::vector<Dart>* visitedFaces = this->askDartBuffer();

	visitedFaces->push_back(d);		// Start with the face of d
1153 1154

	// For every face added to the list
Sylvain Thery's avatar
Sylvain Thery committed
1155
	for(unsigned int i = 0; i < visitedFaces->size(); ++i)
1156
	{
Sylvain Thery's avatar
Sylvain Thery committed
1157
		if (!mark.isMarked((*visitedFaces)[i]))	// Face has not been visited yet
1158 1159
		{
			// Apply functor to the darts of the face
Sylvain Thery's avatar
Sylvain Thery committed
1160
			Map2::foreach_dart_of_face((*visitedFaces)[i], f);
1161

1162
			// mark visited darts (current face)
1163
			// and add non visited adjacent faces to the list of face
Sylvain Thery's avatar
Sylvain Thery committed
1164
			Dart e = (*visitedFaces)[i] ;
1165
			do
1166
			{
1167 1168 1169
				mark.mark(e);				// Mark
				Dart adj = phi2(e);			// Get adjacent face
				if (!mark.isMarked(adj))
Sylvain Thery's avatar
Sylvain Thery committed
1170
					visitedFaces->push_back(adj);	// Add it
1171
				e = this->phi1(e);
Sylvain Thery's avatar
Sylvain Thery committed
1172
			} while(e != (*visitedFaces)[i]);
1173 1174
		}
	}
Sylvain Thery's avatar
Sylvain Thery committed
1175 1176

	this->releaseDartBuffer(visitedFaces);
1177 1178 1179 1180 1181 1182
}

/*! @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
1183 1184
template <typename MAP_IMPL>
Dart Map2<MAP_IMPL>::newBoundaryCycle(unsigned int nbE)
1185 1186
{
	Dart d = ParentMap::newCycle(nbE);
1187
	Algo::Topo::boundaryMarkOrbit<2,FACE>(*this, d);
1188 1189 1190
	return d;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1191 1192
template <typename MAP_IMPL>
unsigned int Map2<MAP_IMPL>::closeHole(Dart d, bool forboundary)
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
{
	assert(phi2(d) == d);		// Nothing to close

	Dart first = this->newDart();	// First edge of the face that will fill the hole
	unsigned int countEdges = 1;

	phi2sew(d, first);	// phi2-link the new edge to the hole

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

		if (dPhi1 != d)
		{
			Dart next = this->newDart();	// Add a new edge there and link it to the face
			++countEdges;
			this->phi1sew(first, next);	// the edge is linked to the face
			phi2sew(dNext, next);		// the face is linked to the hole
		}
	} while (dPhi1 != d);

	if (forboundary)
1221
		Algo::Topo::boundaryMarkOrbit<2,FACE>(*this, phi2(d));
1222 1223 1224 1225

	return countEdges ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1226 1227
template <typename MAP_IMPL>
unsigned int Map2<MAP_IMPL>::closeMap(bool forboundary)
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
{
	// 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 (phi2(d) == d)
		{
			++nb ;
			closeHole(d, forboundary);
		}
	}
	return nb ;
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
1246 1247
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::reverseOrientation()
1248
{
1249
	DartAttribute<unsigned int, Map2<MAP_IMPL> > emb0(this, this->template getEmbeddingAttributeVector<VERTEX>()) ;
1250 1251
	if(emb0.isValid())
	{
1252
		DartAttribute<unsigned int, Map2<MAP_IMPL> > new_emb0 = this->template addAttribute<unsigned int, DART, Map2<MAP_IMPL> >("new_EMB_0") ;
1253 1254 1255
		for(Dart d = this->begin(); d != this->end(); this->next(d))
			new_emb0[d] = emb0[this->phi1(d)] ;

1256
		this->swapAttributes(emb0, new_emb0) ;
1257 1258 1259
		this->removeAttribute(new_emb0) ;
	}

1260 1261
	DartAttribute<Dart, Map2<MAP_IMPL> > n_phi1(this, this->getPermutationAttribute(0)) ;
	DartAttribute<Dart, Map2<MAP_IMPL> > n_phi_1(this, this->getPermutationInvAttribute(0)) ;
1262 1263

	this->swapAttributes(n_phi1, n_phi_1) ;
1264 1265
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1266 1267
template <typename MAP_IMPL>
void Map2<MAP_IMPL>::computeDual()
1268
{
1269 1270
	DartAttribute<Dart, Map2<MAP_IMPL> > old_phi1(this, this->getPermutationAttribute(0)) ;
	DartAttribute<Dart, Map2<MAP_IMPL> > old_phi_1(this, this->getPermutationInvAttribute(0)) ;
1271

1272 1273
	DartAttribute<Dart, Map2<MAP_IMPL> > new_phi1 = this->template addAttribute<Dart, DART, Map2<MAP_IMPL> >("new_phi1") ;
	DartAttribute<Dart, Map2<MAP_IMPL> > new_phi_1 = this->template addAttribute<Dart, DART, Map2<MAP_IMPL> >("new_phi_1") ;
1274 1275 1276 1277 1278 1279 1280 1281 1282

	for(Dart d = this->begin(); d != this->end(); this->next(d))
	{
		Dart dd = this->phi1(phi2(d));

		new_phi1[d] = dd ;
		new_phi_1[dd] = d ;
	}

1283 1284
	this->swapAttributes(old_phi1, new_phi1) ;
	this->swapAttributes(old_phi_1, new_phi_1) ;
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295

	this->removeAttribute(new_phi1) ;
	this->removeAttribute(new_phi_1) ;

	this->swapEmbeddingContainers(VERTEX, FACE) ;

	reverseOrientation() ;

	// boundary management
	for(Dart d = this->begin(); d != this->end(); this->next(d))
	{
1296
		if(this->template isBoundaryMarked<2>(d))
1297
			Algo::Topo::boundaryMarkOrbit<2,FACE>(*this, deleteVertex(phi2(d)));
1298 1299 1300
	}
}

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