gmap3.cpp 21 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 28 29 30
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Topology/gmap/gmap3.h"
#include "Topology/generic/dartmarker.h"

namespace CGoGN
{

Pierre Kraemer's avatar
merges  
Pierre Kraemer committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
void GMap3::compactTopoRelations(const std::vector<unsigned int>& oldnew)
{
	for (unsigned int i = m_attribs[DART].begin(); i != m_attribs[DART].end(); m_attribs[DART].next(i))
	{
		{
			Dart& d = m_beta0->operator [](i);
			Dart e = Dart(oldnew[d.index]);
			if (d != e)
				d = e;
		}
		{
			Dart& d = m_beta1->operator [](i);
			Dart e = Dart(oldnew[d.index]);
			if (d != e)
				d = e;
		}
		{
			Dart& d = m_beta2->operator [](i);
			Dart e = Dart(oldnew[d.index]);
			if (d != e)
				d = e;
		}
		{
			Dart& d = m_beta3->operator [](i);
			Dart e = Dart(oldnew[d.index]);
			if (d != e)
				d = e;
		}
	}
}

62
/*! @name Generator and Deletor
Pierre Kraemer's avatar
Pierre Kraemer committed
63
 *  To generate or delete volumes in a 3-G-map
64 65 66
 *************************************************************************/

void GMap3::deleteVolume(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
67
{
Pierre Kraemer's avatar
Pierre Kraemer committed
68
	DartMarkerStore mark(*this);		// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
69 70

	std::vector<Dart> visitedFaces;		// Faces that are traversed
71
	visitedFaces.reserve(512);
Pierre Kraemer's avatar
Pierre Kraemer committed
72
	visitedFaces.push_back(d);			// Start with the face of d
73
	mark.markOrbit<FACE>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
74 75

	// For every face added to the list
76
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
77
	{
78
		Dart e = visitedFaces[i] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
79

Pierre Kraemer's avatar
Pierre Kraemer committed
80 81
		if(!isBoundaryFace(e))
			unsewVolumes(e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
82

Pierre Kraemer's avatar
Pierre Kraemer committed
83 84 85 86
		do	// add all face neighbours to the table
		{
			Dart ee = phi2(e) ;
			if(!mark.isMarked(ee)) // not already marked
Pierre Kraemer's avatar
Pierre Kraemer committed
87
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
88
				visitedFaces.push_back(ee) ;
89
				mark.markOrbit<FACE>(ee) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91
			}
			e = phi1(e) ;
92
		} while(e != visitedFaces[i]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
93
	}
94

Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97 98 99 100 101 102 103
	Dart dd = phi3(d) ;
	GMap2::deleteCC(d) ;
	GMap2::deleteCC(dd) ;
}

void GMap3::fillHole(Dart d)
{
	assert(isBoundaryFace(d)) ;
	Dart dd = d ;
Thery Sylvain's avatar
Thery Sylvain committed
104
	if(!isBoundaryMarked3(dd))
Pierre Kraemer's avatar
Pierre Kraemer committed
105
		dd = phi3(dd) ;
Thery Sylvain's avatar
Thery Sylvain committed
106
	boundaryUnmarkOrbit<VOLUME,3>(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
107 108
}

109
/*! @name Topological Operators
Pierre Kraemer's avatar
Pierre Kraemer committed
110
 *  Topological operations on 3-G-maps
111
 *************************************************************************/
112

Pierre Kraemer's avatar
Pierre Kraemer committed
113
Dart GMap3::deleteVertex(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
114
{
115 116 117 118 119 120 121 122 123 124 125 126 127 128
	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);
129
	for(unsigned int i = 0; i < fstoretmp.size(); ++i)
130
	{
131
		if(!mf.isMarked(fstoretmp[i]))
132
		{
133
			mf.markOrbit<FACE>(fstoretmp[i]);
134
			fstore.push_back(fstoretmp[i]);
135 136
		}
	}
137

138 139 140 141 142 143 144 145 146 147 148
	Dart res = NIL ;
	for(std::vector<Dart>::iterator it = fstore.begin() ; it != fstore.end() ; ++it)
	{
		Dart fit = *it ;
		Dart end = phi_1(fit) ;
		fit = phi1(fit) ;
		while(fit != end)
		{
			Dart d2 = phi2(fit) ;
			Dart d3 = phi3(fit) ;
			Dart d32 = phi2(d3) ;
149

150 151
			if(res == NIL)
				res = d2 ;
152

153 154
			beta2unsew(d2) ;
			beta2unsew(beta0(d2)) ;
155

156 157
			beta2unsew(d32) ;
			beta2unsew(beta0(d32)) ;
158

159 160 161 162
			beta2sew(d2, beta0(d32)) ;
			beta2sew(beta0(d2), d32) ;
			beta2sew(fit, beta0(d3)) ;
			beta2sew(beta0(fit), d3) ;
163 164

			fit = phi1(fit) ;
165 166
		}
	}
167

168
	GMap2::deleteCC(d) ;
169

170
	return res ;
Pierre Kraemer's avatar
Pierre Kraemer committed
171
}
172

173
Dart GMap3::cutEdge(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
174 175 176
{
	Dart prev = d ;
	Dart dd = alpha2(d) ;
177
	Dart nd = GMap2::cutEdge(d) ;
178

Pierre Kraemer's avatar
Pierre Kraemer committed
179
	while(dd != d)
180
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182
		prev = dd ;
		dd = alpha2(dd) ;
183

Pierre Kraemer's avatar
Pierre Kraemer committed
184
		GMap2::cutEdge(prev) ;
185

Pierre Kraemer's avatar
Pierre Kraemer committed
186 187 188
		Dart d3 = beta3(prev);
		beta3sew(beta0(prev), beta0(d3));
		beta3sew(phi1(prev), phi1(d3));
189
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192

	Dart d3 = beta3(d);
	beta3sew(beta0(d), beta0(d3));
Pierre Kraemer's avatar
Pierre Kraemer committed
193
	beta3sew(phi1(d), phi1(d3));
194 195

	return nd ;
196 197
}

Pierre Kraemer's avatar
Pierre Kraemer committed
198
bool GMap3::uncutEdge(Dart d)
Thomas's avatar
Thomas committed
199
{
200
	if(vertexDegree(phi1(d)) == 2)
Thomas's avatar
Thomas committed
201
	{
202 203 204 205 206 207 208
		Dart prev = d ;

		Dart dd = d;
		do
		{
			prev = dd;
			dd = alpha2(dd);
Thomas's avatar
Thomas committed
209

210 211
			GMap2::uncutEdge(prev);
		} while (dd != d) ;
Thomas's avatar
Thomas committed
212

213
		return true;
Thomas's avatar
Thomas committed
214
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
215
	return false;
Thomas's avatar
Thomas committed
216 217
}

218 219 220 221 222 223 224
bool GMap3::deleteEdgePreCond(Dart d)
{
	unsigned int nb1 = vertexDegree(d);
	unsigned int nb2 = vertexDegree(phi1(d));
	return (nb1!=2) && (nb2!=2);
}

225 226
Dart GMap3::deleteEdge(Dart d)
{
227 228
	assert(deleteEdgePreCond(d));

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
	if(isBoundaryEdge(d))
		return NIL ;

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

			if(res == NIL)
				res = d2 ;

			beta2unsew(d2) ;
			beta2unsew(beta0(d2)) ;
			beta2unsew(d32) ;
			beta2unsew(beta0(d32)) ;

			beta2sew(d2, beta0(d32)) ;
			beta2sew(beta0(d2), d32) ;
			beta2sew(fit, beta0(d3)) ;
			beta2sew(beta0(fit), d3) ;

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

	GMap2::deleteCC(d) ;

	return res ;
}

268 269 270 271 272
bool GMap3::splitFacePreCond(Dart d, Dart e)
{
	return (d != e && GMap2::sameFace(d, e)) ;
}

273
void GMap3::splitFace(Dart d, Dart e)
Thomas's avatar
Thomas committed
274
{
275 276 277 278
	assert(splitFacePreCond(d, e));

	if(!sameOrientedFace(d, e))
		e = beta1(e) ;
Thomas's avatar
Thomas committed
279

280 281
	Dart dd = beta1(beta3(d));
	Dart ee = beta1(beta3(e));
Thomas's avatar
Thomas committed
282

Pierre Kraemer's avatar
Pierre Kraemer committed
283 284 285 286 287 288 289 290 291 292
	Dart dprev = phi_1(d) ;
	Dart eprev = phi_1(e) ;
	Dart ddprev = phi_1(dd) ;
	Dart eeprev = phi_1(ee) ;

	beta3unsew(beta1(d)) ;
	beta3unsew(beta1(e)) ;
	beta3unsew(beta1(dd)) ;
	beta3unsew(beta1(ee)) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
293 294
	GMap2::splitFace(d, e);
	GMap2::splitFace(dd, ee);
295 296 297 298
	beta3sew(beta1(d), phi_1(ee));
	beta3sew(phi_1(d), beta1(ee));
	beta3sew(beta1(e), phi_1(dd));
	beta3sew(phi_1(e), beta1(dd));
Pierre Kraemer's avatar
Pierre Kraemer committed
299 300 301 302 303

	beta3sew(beta0(dprev), beta0(beta3(dprev))) ;
	beta3sew(beta0(eprev), beta0(beta3(eprev))) ;
	beta3sew(beta0(ddprev), beta0(beta3(ddprev))) ;
	beta3sew(beta0(eeprev), beta0(beta3(eeprev))) ;
Thomas's avatar
Thomas committed
304 305
}

Pierre Kraemer's avatar
Pierre Kraemer committed
306
void GMap3::sewVolumes(Dart d, Dart e, bool withBoundary)
307 308 309
{
	assert(faceDegree(d) == faceDegree(e));

Pierre Kraemer's avatar
Pierre Kraemer committed
310 311 312
	// if sewing with fixed points
	if (!withBoundary)
	{
313
		assert(beta3(d) == d && beta3(e) == e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
314 315 316 317
		Dart fitD = d ;
		Dart fitE = e ;
		do
		{
318 319
			beta3sew(fitD, beta0(fitE)) ;
			beta3sew(beta0(fitD), fitE) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
320 321 322 323 324 325
			fitD = phi1(fitD) ;
			fitE = phi_1(fitE) ;
		} while(fitD != d) ;
		return ;
	}

326 327
	Dart dd = beta3(d) ;
	Dart ee = beta3(e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
328 329 330

	Dart fitD = dd ;
	Dart fitE = ee ;
331 332
	do
	{
333 334
		Dart fitD2 = beta2(fitD) ;
		Dart fitE2 = beta2(fitE) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
335 336
		if(fitD2 != fitE)
		{
337 338
			beta2unsew(fitD) ;
			beta2unsew(fitE) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
339 340
			beta2unsew(beta0(fitD)) ;
			beta2unsew(beta0(fitE)) ;
341 342 343 344
			beta2sew(fitD2, beta0(fitE2)) ;
			beta2sew(beta0(fitD2), fitE2) ;
			beta2sew(fitD, beta0(fitE)) ;
			beta2sew(beta0(fitD), fitE) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
345
		}
346 347 348 349
		beta3unsew(fitD) ;
		beta3unsew(beta0(fitD)) ;
		beta3unsew(fitE) ;
		beta3unsew(beta0(fitE)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
350 351 352
		fitD = phi1(fitD) ;
		fitE = phi_1(fitE) ;
	} while(fitD != dd) ;
353
	GMap2::deleteCC(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
354 355 356

	fitD = d ;
	fitE = e ;
357 358
	do
	{
359 360
		beta3sew(fitD, beta0(fitE)) ;
		beta3sew(beta0(fitD), fitE) ;
361 362 363 364 365 366 367
		fitD = phi1(fitD) ;
		fitE = phi_1(fitE) ;
	} while(fitD != d) ;
}

void GMap3::unsewVolumes(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
368 369 370 371 372
	assert(!isBoundaryFace(d)) ;

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

373 374
	Dart b1 = newBoundaryCycle(nbE) ;
	Dart b2 = newBoundaryCycle(nbE) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
375 376 377 378 379

	Dart fit1 = d ;
	Dart fit2 = d3 ;
	Dart fitB1 = b1 ;
	Dart fitB2 = b2 ;
380 381
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
382 383 384 385
		Dart f = findBoundaryFaceOfEdge(fit1) ;
		if(f != NIL)
		{
			Dart f2 = phi2(f) ;
386 387 388 389 390 391
			beta2unsew(f) ;
			beta2unsew(beta0(f)) ;
			beta2sew(fitB1, beta0(f)) ;
			beta2sew(beta0(fitB1), f) ;
			beta2sew(fitB2, beta0(f2)) ;
			beta2sew(beta0(fitB2), f2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
392 393
		}
		else
394 395 396 397
		{
			beta2sew(fitB1, beta0(fitB2)) ;
			beta2sew(beta0(fitB1), fitB2) ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
398

399 400 401 402 403 404
		beta3unsew(fit1) ;
		beta3unsew(beta0(fit1)) ;
		beta3sew(fit1, beta0(fitB1)) ;
		beta3sew(beta0(fit1), fitB1) ;
		beta3sew(fit2, beta0(fitB2)) ;
		beta3sew(beta0(fit2), fitB2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
405 406 407 408 409 410

		fit1 = phi1(fit1) ;
		fit2 = phi_1(fit2) ;
		fitB1 = phi_1(fitB1) ;
		fitB2 = phi1(fitB2) ;
	} while(fitB1 != b1) ;
411 412 413 414
}

bool GMap3::mergeVolumes(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
415
	if(!GMap3::isBoundaryFace(d))
416
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
417
		GMap2::mergeVolumes(d, phi3(d)); // merge the two volumes along common face
418 419 420 421 422
		return true ;
	}
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
423 424 425 426 427 428 429
void GMap3::splitVolume(std::vector<Dart>& vd)
{
	assert(checkSimpleOrientedPath(vd)) ;

	Dart e = vd.front();
	Dart e2 = phi2(e);

430
	GMap2::splitSurface(vd,true,true);
Pierre Kraemer's avatar
Pierre Kraemer committed
431 432

	//sew the two connected components
433 434 435 436 437 438 439 440 441 442 443 444 445 446
	GMap3::sewVolumes(phi2(e), 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
447 448
}

449 450 451
/*! @name Topological Queries
 *  Return or set various topological information
 *************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
452

453
bool GMap3::sameOrientedVertex(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
454
{
455 456
	DartMarkerStore mv(*this);	// Lock a marker

Pierre Kraemer's avatar
Pierre Kraemer committed
457 458 459
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
460 461
	mv.mark(d);

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

Pierre Kraemer's avatar
Pierre Kraemer committed
467
		// add phi21 and phi23 successor if they are not marked yet
468
		Dart d2 = phi2(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
469 470
		Dart d21 = phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume
471

Pierre Kraemer's avatar
Pierre Kraemer committed
472 473 474 475 476 477 478 479 480
		if(!mv.isMarked(d21))
		{
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
481 482 483 484 485 486 487 488
		}
	}
	return false;
}

bool GMap3::sameVertex(Dart d, Dart e)
{
	DartMarkerStore mv(*this);	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
489

Pierre Kraemer's avatar
Pierre Kraemer committed
490 491 492
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
Pierre Kraemer's avatar
Pierre Kraemer committed
493 494
	mv.mark(d);

495
	for(unsigned int i = 0; i < darts.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
496
	{
497
		if(darts[i] == e)
498 499
			return true;

500
		Dart dx = beta1(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
501 502
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
503
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
504 505
			mv.mark(dx);
		}
506
		dx = beta2(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
507 508
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
509
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
510 511
			mv.mark(dx);
		}
512
		dx = beta3(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
513 514
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
515
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
516 517 518
			mv.mark(dx);
		}
	}
519
	return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
520 521
}

522 523
unsigned int GMap3::vertexDegree(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
524
	unsigned int count = 0;
525 526
	DartMarkerStore mv(*this);	// Lock a marker

Pierre Kraemer's avatar
Pierre Kraemer committed
527 528 529
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
530 531
	mv.mark(d);

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

		if(!mv.isMarked(d21))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
541
			darts.push_back(d21);
542 543
			mv.mark(d21);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
544
		if(!mv.isMarked(d23))
545
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
546
			darts.push_back(d23);
547 548 549 550 551
			mv.mark(d23);
		}
	}

	DartMarkerStore me(*this);
Pierre Kraemer's avatar
Pierre Kraemer committed
552
	for(std::vector<Dart>::iterator it = darts.begin(); it != darts.end() ; ++it)
553
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
554
		if(!me.isMarked(*it))
555 556
		{
			++count;
557
			me.markOrbit<EDGE>(*it);
558 559 560 561 562 563 564 565
		}
	}

	return count;
}

bool GMap3::isBoundaryVertex(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
566
	DartMarkerStore mv(*this);	// Lock a marker
567

Pierre Kraemer's avatar
Pierre Kraemer committed
568 569 570
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
571 572
	mv.mark(d);

573
	for(unsigned int i = 0; i < darts.size(); ++i)
574
	{
Thery Sylvain's avatar
Thery Sylvain committed
575
		if(isBoundaryMarked3(darts[i]))
Pierre Kraemer's avatar
Pierre Kraemer committed
576
			return true ;
577 578

		//add phi21 and phi23 successor if they are not marked yet
579
		Dart d2 = phi2(darts[i]);
580 581 582 583 584
		Dart d21 = phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume

		if(!mv.isMarked(d21))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
585
			darts.push_back(d21);
586 587
			mv.mark(d21);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
588
		if(!mv.isMarked(d23))
589
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
590
			darts.push_back(d23);
591 592 593
			mv.mark(d23);
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
594
	return false ;
595 596
}

597 598
bool GMap3::sameOrientedEdge(Dart d, Dart e)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
599
	Dart it = d;
600 601
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
602
		if(it == e || phi2(it) == e)
603
			return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
604 605
		it = alpha2(it);
	} while (it != d);
606 607
	return false;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
608

609
bool GMap3::sameEdge(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
610
{
Pierre Kraemer's avatar
Pierre Kraemer committed
611
	Dart it = d;
612 613
	do
	{
614
		if(it == e || beta0(it) == e || beta2(it) == e || phi2(it) == e)
615 616
			return true;

Pierre Kraemer's avatar
Pierre Kraemer committed
617 618
		it = alpha2(it);
	} while (it != d);
619 620 621
	return false;
}

622 623 624
unsigned int GMap3::edgeDegree(Dart d)
{
	unsigned int deg = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
625
	Dart it = d;
626 627
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
628 629 630
		++deg;
		it = alpha2(it);
	} while(it != d);
631 632 633
	return deg;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
634
bool GMap3::isBoundaryEdge(Dart d)
635
{
Pierre Kraemer's avatar
Pierre Kraemer committed
636
	Dart it = d;
637 638
	do
	{
Thery Sylvain's avatar
Thery Sylvain committed
639
		if(isBoundaryMarked3(it))
Pierre Kraemer's avatar
Pierre Kraemer committed
640 641 642
			return true ;
		it = alpha2(it);
	} while(it != d);
643 644 645
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
646
Dart GMap3::findBoundaryFaceOfEdge(Dart d)
647
{
Pierre Kraemer's avatar
Pierre Kraemer committed
648
	Dart it = d;
649 650
	do
	{
Thery Sylvain's avatar
Thery Sylvain committed
651
		if (isBoundaryMarked3(it))
Pierre Kraemer's avatar
Pierre Kraemer committed
652 653 654 655 656
			return it ;
		it = alpha2(it);
	} while(it != d);
	return NIL ;
}
657

Pierre Kraemer's avatar
Pierre Kraemer committed
658 659 660 661 662 663
bool GMap3::sameOrientedFace(Dart d, Dart e)
{
	Dart it = d;
	do
	{
		if(it == e || phi3(it) == e)
664
			return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
665 666 667 668
		it = phi1(it);
	} while (it != d);
	return false;
}
669

Pierre Kraemer's avatar
Pierre Kraemer committed
670 671 672
bool GMap3::isBoundaryVolume(Dart d)
{
	DartMarkerStore mark(*this);	// Lock a marker
673

Pierre Kraemer's avatar
Pierre Kraemer committed
674 675 676
	std::vector<Dart> visitedFaces ;
	visitedFaces.reserve(128) ;
	visitedFaces.push_back(d) ;
677
	mark.markOrbit<FACE>(d) ;
678

679
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
680
	{
Thery Sylvain's avatar
Thery Sylvain committed
681
		if (isBoundaryMarked3(beta3(visitedFaces[i])))
Pierre Kraemer's avatar
Pierre Kraemer committed
682 683
			return true ;

684
		Dart e = visitedFaces[i] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
685 686 687 688 689 690
		do	// add all face neighbours to the table
		{
			Dart ee = phi2(e) ;
			if(!mark.isMarked(ee)) // not already marked
			{
				visitedFaces.push_back(ee) ;
691
				mark.markOrbit<FACE>(ee) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
692 693
			}
			e = phi1(e) ;
694
		} while(e != visitedFaces[i]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
695
	}
696 697 698
	return false;
}

Thomas's avatar
Thomas committed
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
bool GMap3::check()
{
    CGoGNout << "Check: topology begin" << CGoGNendl;
    DartMarker m(*this);
    m.unmarkAll();
    for(Dart d = this->begin(); d != this->end(); this->next(d))
    {
        Dart d0 = beta0(d);
        if (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(beta1(d3) != beta3(beta1(d)))
        	{
        		CGoGNout << "Check: beta3 , faces are not entirely sewn" << CGoGNendl;
        		return false;
        	}
        }

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

        Dart d1 = phi1(d);
        if (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 (phi1(d1) == d)
            CGoGNout << "Check: (warning) face with only two edges" << CGoGNendl;

        if (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;
Pierre Kraemer's avatar
Pierre Kraemer committed
774

Thomas's avatar
Thomas committed
775 776 777
    return true;
}

778 779 780 781
/*! @name Cell Functors
 *  Apply functors to all darts of a cell
 *************************************************************************/

782 783
bool GMap3::foreach_dart_of_oriented_vertex(Dart d, FunctorType& f, unsigned int thread)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
784
	DartMarkerStore mv(*this, thread);	// Lock a marker
785 786
	bool found = false;					// Last functor return value

Pierre Kraemer's avatar
Pierre Kraemer committed
787 788 789
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
790 791
	mv.mark(d);

792
	for(unsigned int i = 0; !found && i < darts.size(); ++i)
793
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
794
		// add phi21 and phi23 successor if they are not marked yet
795
		Dart d2 = phi2(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
796 797
		Dart d21 = phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume
798

Pierre Kraemer's avatar
Pierre Kraemer committed
799
		if(!mv.isMarked(d21))
800
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
801 802 803 804 805 806 807
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
808 809
		}

810
		found = f(darts[i]);
811 812 813 814
	}
	return found;
}

815
bool GMap3::foreach_dart_of_vertex(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
816
{
Pierre Kraemer's avatar
Pierre Kraemer committed
817
	DartMarkerStore mv(*this, thread);	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
818
	bool found = false;					// Last functor return value
819

Pierre Kraemer's avatar
Pierre Kraemer committed
820 821 822
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
Pierre Kraemer's avatar
Pierre Kraemer committed
823 824
	mv.mark(d);

825
	for(unsigned int i = 0; !found && i < darts.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
826
	{
827
		Dart dx = beta1(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
828 829
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
830
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
831 832
			mv.mark(dx);
		}
833
		dx = beta2(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
834 835
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
836
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
837 838
			mv.mark(dx);
		}
839
		dx = beta3(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
840 841
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
842
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
843 844 845
			mv.mark(dx);
		}

846
		found = f(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
847 848
	}
	return found;
849
}
Pierre Kraemer's avatar
Pierre Kraemer committed
850

851 852 853 854 855 856 857 858 859 860 861 862
bool GMap3::foreach_dart_of_oriented_edge(Dart d, FunctorType& f, unsigned int thread)
{
	Dart it = d;
	do
	{
		if (GMap2::foreach_dart_of_oriented_edge(it, f, thread))
			return true;
		it = alpha2(it);
	} while (it != d);
	return false;
}

863 864
bool GMap3::foreach_dart_of_edge(Dart d, FunctorType& f, unsigned int thread)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
865 866 867 868
	Dart it = d;
	do
	{
		if (GMap2::foreach_dart_of_edge(it, f, thread))
869
			return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
870 871
		it = alpha2(it);
	} while (it != d);
872
	return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
873 874
}

Sylvain Thery's avatar
Sylvain Thery committed
875
bool GMap3::foreach_dart_of_cc(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
876
{
Sylvain Thery's avatar
Sylvain Thery committed
877
	DartMarkerStore mv(*this,thread);	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
878 879
	bool found = false;					// Last functor return value

Pierre Kraemer's avatar
Pierre Kraemer committed
880 881 882
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(1024);
	darts.push_back(d);			// Start with the dart d
Pierre Kraemer's avatar
Pierre Kraemer committed
883 884
	mv.mark(d);

885
	for(unsigned int i = 0; !found && i < darts.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
886
	{
887
		Dart dx = beta0(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
888 889
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
890
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
891 892
			mv.mark(dx);
		}
893
		dx = beta1(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
894 895
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
896
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
897 898
			mv.mark(dx);
		}
899
		dx = beta2(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
900 901
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
902
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
903 904
			mv.mark(dx);
		}
905
		dx = beta3(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
906 907
		if (!mv.isMarked(dx))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
908
			darts.push_back(dx);
Pierre Kraemer's avatar
Pierre Kraemer committed
909 910 911
			mv.mark(dx);
		}

912
		found =  f(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
913 914 915 916
	}
	return found;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
917 918 919 920
/*! @name Close map after import or creation
 *  These functions must be used with care, generally only by import/creation algorithms
 *************************************************************************/

921 922 923 924 925 926 927
Dart GMap3::newBoundaryCycle(unsigned int nbE)
{
	Dart d = GMap1::newCycle(nbE);
	boundaryMarkOrbit<FACE,3>(d);
	return d;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
928 929
unsigned int GMap3::closeHole(Dart d, bool forboundary)
{
930
	assert(beta3(d) == d);		// Nothing to close
Pierre Kraemer's avatar
Pierre Kraemer committed
931 932 933 934 935
	DartMarkerStore m(*this) ;

	std::vector<Dart> visitedFaces;	// Faces that are traversed
	visitedFaces.reserve(1024) ;
	visitedFaces.push_back(d);		// Start with the face of d
936
	m.markOrbit<FACE>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
937 938 939 940

	unsigned int count = 0 ;

	// For every face added to the list
941
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
942
	{
943
		Dart f = visitedFaces[i] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
944
		unsigned int degree = faceDegree(f) ;
945
		Dart b = newBoundaryCycle(degree) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
946 947 948 949 950 951 952 953 954
		++count ;

		Dart bit = b ;
		do
		{
			Dart e = alpha2(f) ;
			bool found = false ;
			do
			{
955
				if(beta3(e) == e)
Pierre Kraemer's avatar
Pierre Kraemer committed
956 957 958 959 960
				{
					found = true ;
					if(!m.isMarked(e))
					{
						visitedFaces.push_back(e) ;
961
						m.markOrbit<FACE>(e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
962 963
					}
				}
Thery Sylvain's avatar
Thery Sylvain committed
964
				else if(isBoundaryMarked3(e))
Pierre Kraemer's avatar
Pierre Kraemer committed
965 966
				{
					found = true ;
967 968
					beta2sew(e, bit) ;
					beta2sew(beta0(e), beta0(bit)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
969 970 971 972 973
				}
				else
					e = alpha2(e) ;
			} while(!found) ;

974 975 976
			beta3sew(f, bit) ;
			beta3sew(beta0(f), beta0(bit)) ;
			bit = phi1(bit) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
977
			f = phi1(f);
978
		} while(f != visitedFaces[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
979 980 981 982 983
	}

	return count ;
}

984
unsigned int GMap3::closeMap()
Pierre Kraemer's avatar
Pierre Kraemer committed
985 986
{
	// Search the map for topological holes (fix points of beta3)
987
	unsigned int nb = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
988 989 990
	for (Dart d = begin(); d != end(); next(d))
	{
		if (beta3(d) == d)
991 992
		{
			++nb ;
Pierre Kraemer's avatar
Pierre Kraemer committed
993
			closeHole(d);
994
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
995
	}
996
	return nb ;
Pierre Kraemer's avatar
Pierre Kraemer committed
997 998 999
}

} // namespace CGoGN