embeddedGMap2.cpp 13.7 KB
Newer Older
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           *
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/                                           *
21 22 23 24 25 26 27 28 29 30 31 32
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <vector>
#include <algorithm>

#include "Topology/gmap/embeddedGMap2.h"

namespace CGoGN
{

Thery Sylvain's avatar
Thery Sylvain committed
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75
Dart EmbeddedGMap2::newFace(unsigned int nbEdges, bool withBoundary)
{
	Dart d = GMap2::newFace(nbEdges, withBoundary);

	if(withBoundary)
	{
		if (isOrbitEmbedded<VERTEX>())
		{
			Traversor2FV<EmbeddedGMap2> t(*this, d);
			for(Dart it = t.begin(); it != t.end(); it = t.next())
				initOrbitEmbeddingNewCell<VERTEX>(it) ;
		}

		if(isOrbitEmbedded<EDGE>())
		{
			Traversor2FE<EmbeddedGMap2> t(*this, d);
			for(Dart it = t.begin(); it != t.end(); it = t.next())
				initOrbitEmbeddingNewCell<EDGE>(it) ;
		}

		if(isOrbitEmbedded<FACE>())
		{
			initOrbitEmbeddingNewCell<FACE>(d) ;
			initOrbitEmbeddingNewCell<FACE>(phi2(d)) ;
		}
	}
	else
	{
		if (isOrbitEmbedded<VERTEX>())
		{
			Traversor2FV<EmbeddedGMap2> t(*this, d);
			for(Dart it = t.begin(); it != t.end(); it = t.next())
				initOrbitEmbeddingNewCell<VERTEX>(it) ;
		}

		if(isOrbitEmbedded<FACE>())
			initOrbitEmbeddingNewCell<FACE>(d) ;

	}
	return d ;
}


76 77 78 79 80 81 82
void EmbeddedGMap2::splitVertex(Dart d, Dart e)
{
	Dart dd = phi2(d) ;
	Dart ee = phi2(e) ;

	GMap2::splitVertex(d, e) ;

83
	if (isOrbitEmbedded<VERTEX>())
84
	{
85 86 87 88
		unsigned int vEmb = getEmbedding<VERTEX>(d) ;
		setDartEmbedding<VERTEX>(phi1(dd), vEmb) ;
		setDartEmbedding<VERTEX>(beta2(phi1(dd)), vEmb) ;
		setDartEmbedding<VERTEX>(beta0(dd), vEmb) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
89

90
		setOrbitEmbeddingOnNewCell<VERTEX>(e) ;
91
		copyCell<VERTEX>(e, d) ;
92 93
	}

94 95 96 97 98
	if(isOrbitEmbedded<EDGE>())
	{
		initOrbitEmbeddingNewCell<EDGE>(phi1(dd)) ;
	}

99
	if(isOrbitEmbedded<FACE>())
100
	{
101 102 103 104 105 106
		unsigned int f1Emb = getEmbedding<FACE>(dd) ;
		copyDartEmbedding<FACE>(phi1(dd), f1Emb) ;
		copyDartEmbedding<FACE>(beta0(phi1(dd)), f1Emb) ;
		unsigned int f2Emb = getEmbedding<FACE>(ee) ;
		copyDartEmbedding<FACE>(phi1(ee), f2Emb) ;
		copyDartEmbedding<FACE>(beta0(phi1(ee)), f2Emb) ;
107 108 109
	}
}

110
Dart EmbeddedGMap2::deleteVertex(Dart d)
111
{
112 113
	Dart f = GMap2::deleteVertex(d) ;
	if(f != NIL)
114
	{
115
		if (isOrbitEmbedded<FACE>())
116
		{
117
			setOrbitEmbedding<FACE>(f, getEmbedding<FACE>(f)) ;
118
		}
119
	}
120
	return f ;
121
}
122

123
Dart EmbeddedGMap2::cutEdge(Dart d)
124
{
125
	Dart nd = GMap2::cutEdge(d) ;
126

127 128 129 130 131
	if(isOrbitEmbedded<VERTEX>())
	{
		initOrbitEmbeddingNewCell<VERTEX>(nd) ;
	}

132
	if (isOrbitEmbedded<EDGE>())
133
	{
134 135 136
		unsigned int eEmb = getEmbedding<EDGE>(d) ;
		setDartEmbedding<EDGE>(phi2(d), eEmb) ;
		setDartEmbedding<EDGE>(beta0(d), eEmb) ;
137
		setOrbitEmbeddingOnNewCell<EDGE>(nd) ;
138
		copyCell<EDGE>(nd, d) ;
139
	}
Thomas's avatar
Thomas committed
140

141
	if(isOrbitEmbedded<FACE>())
142
	{
143 144 145
		unsigned int f1Emb = getEmbedding<FACE>(d) ;
		setDartEmbedding<FACE>(phi1(d), f1Emb) ;
		setDartEmbedding<FACE>(beta0(d), f1Emb) ;
146
		Dart e = phi2(nd) ;
147 148 149
		unsigned int f2Emb = getEmbedding<FACE>(d) ;
		setDartEmbedding<FACE>(phi1(e), f2Emb) ;
		setDartEmbedding<FACE>(beta0(e), f2Emb) ;
150
	}
151 152

	return nd ;
153 154
}

155
bool EmbeddedGMap2::uncutEdge(Dart d)
156
{
157 158
	if(GMap2::uncutEdge(d))
	{
159
		if(isOrbitEmbedded<EDGE>())
160
		{
161 162 163
			unsigned int eEmb = getEmbedding<EDGE>(d) ;
			setDartEmbedding<EDGE>(phi2(d), eEmb) ;
			setDartEmbedding<EDGE>(beta0(d), eEmb) ;
164 165 166 167
		}
		return true ;
	}
	return false ;
168
}
169 170 171 172 173 174 175 176 177 178 179 180

bool EmbeddedGMap2::edgeCanCollapse(Dart d)
{
	if(isBoundaryVertex(d) || isBoundaryVertex(phi1(d)))
		return false ;

	unsigned int val_v1 = vertexDegree(d) ;
	unsigned int val_v2 = vertexDegree(phi1(d)) ;

	if(val_v1 + val_v2 < 8 || val_v1 + val_v2 > 14)
		return false ;

181
	if(faceDegree(d) == 3)
182 183 184 185 186 187
	{
		if(vertexDegree(phi_1(d)) < 4)
			return false ;
	}

	Dart dd = phi2(d) ;
188
	if(faceDegree(dd) == 3)
189 190 191 192 193 194 195 196 197 198 199 200
	{
		if(vertexDegree(phi_1(dd)) < 4)
			return false ;
	}

	// Check vertex sharing condition
	std::vector<unsigned int> vu1 ;
	vu1.reserve(32) ;
	Dart vit1 = alpha1(alpha1(d)) ;
	Dart end = phi1(dd) ;
	do
	{
201
		unsigned int ve = getEmbedding<VERTEX>(phi2(vit1)) ;
202 203 204 205 206 207 208
		vu1.push_back(ve) ;
		vit1 = alpha1(vit1) ;
	} while(vit1 != end) ;
	end = phi1(d) ;
	Dart vit2 = alpha1(alpha1(dd)) ;
	do
	{
209
		unsigned int ve = getEmbedding<VERTEX>(phi2(vit2)) ;
210 211 212 213 214 215 216 217 218 219 220 221
		std::vector<unsigned int>::iterator it = std::find(vu1.begin(), vu1.end(), ve) ;
		if(it != vu1.end())
			return false ;
		vit2 = alpha1(vit2) ;
	} while(vit2 != end) ;

	return true ;
}

Dart EmbeddedGMap2::collapseEdge(Dart d, bool delDegenerateFaces)
{
	unsigned int vEmb = EMBNULL ;
222
	if (isOrbitEmbedded<VERTEX>())
223
	{
224
		vEmb = getEmbedding<VERTEX>(d) ;
225 226 227 228
	}

	Dart dV = GMap2::collapseEdge(d, delDegenerateFaces);

229
	if (isOrbitEmbedded<VERTEX>())
230
	{
231
		setOrbitEmbedding<VERTEX>(dV, vEmb) ;
232 233 234 235 236 237 238 239 240 241 242
	}

	return dV ;
}

bool EmbeddedGMap2::flipEdge(Dart d)
{
	if(GMap2::flipEdge(d))
	{
		Dart e = phi2(d) ;

243
		if (isOrbitEmbedded<VERTEX>())
244
		{
245 246 247 248 249 250
			unsigned int v1Emb = getEmbedding<VERTEX>(beta1(d)) ;
			setDartEmbedding<VERTEX>(d, v1Emb) ;
			setDartEmbedding<VERTEX>(beta2(d), v1Emb) ;
			unsigned int v2Emb = getEmbedding<VERTEX>(beta1(e)) ;
			setDartEmbedding<VERTEX>(e, v2Emb) ;
			setDartEmbedding<VERTEX>(beta2(e), v2Emb) ;
251
		}
252
		if (isOrbitEmbedded<FACE>())
253
		{
254 255 256 257 258 259
			unsigned int f1Emb = getEmbedding<FACE>(d) ;
			setDartEmbedding<FACE>(phi_1(d), f1Emb) ;
			setDartEmbedding<FACE>(beta1(d), f1Emb) ;
			unsigned int f2Emb = getEmbedding<FACE>(e) ;
			setDartEmbedding<FACE>(phi_1(e), f2Emb) ;
			setDartEmbedding<FACE>(beta1(e), f2Emb) ;
260 261 262 263 264 265 266 267 268 269 270 271
		}
		return true ;
	}
	return false ;
}

bool EmbeddedGMap2::flipBackEdge(Dart d)
{
	if(GMap2::flipBackEdge(d))
	{
		Dart e = phi2(d) ;

272
		if (isOrbitEmbedded<VERTEX>())
273
		{
274 275 276 277 278 279
			unsigned int v1Emb = getEmbedding<VERTEX>(beta1(d)) ;
			setDartEmbedding<VERTEX>(d, v1Emb) ;
			setDartEmbedding<VERTEX>(beta2(d), v1Emb) ;
			unsigned int v2Emb = getEmbedding<VERTEX>(beta1(e)) ;
			setDartEmbedding<VERTEX>(e, v2Emb) ;
			setDartEmbedding<VERTEX>(beta2(e), v2Emb) ;
280
		}
Thomas's avatar
Thomas committed
281

282
		if (isOrbitEmbedded<FACE>())
283
		{
284 285 286 287 288 289
			unsigned int f1Emb = getEmbedding<FACE>(d) ;
			setDartEmbedding<FACE>(phi1(d), f1Emb) ;
			setDartEmbedding<FACE>(beta0(phi1(d)), f1Emb) ;
			unsigned int f2Emb = getEmbedding<FACE>(e) ;
			setDartEmbedding<FACE>(phi1(e), f2Emb) ;
			setDartEmbedding<FACE>(beta0(phi1(e)), f2Emb) ;
290 291 292 293 294 295
		}
		return true ;
	}
	return false ;
}

296 297 298 299
//void EmbeddedGMap2::insertEdgeInVertex(Dart d, Dart e)
//{
//	GMap2::insertEdgeInVertex(d, e);
//
300
//	if (isOrbitEmbedded<VERTEX>())
301
//	{
302 303
//		copyDartEmbedding<VERTEX>(e, d) ;
//		copyDartEmbedding<VERTEX>(beta2(e), d) ;
304 305
//	}
//
306
//	if (isOrbitEmbedded<FACE>())
307 308 309
//	{
//		if(!sameFace(d,e))
//		{
310
//			setOrbitEmbeddingOnNewCell<FACE>(e);
311
//			copyCell<FACE>(e, d) ;
312 313 314
//		}
//		else
//		{
315
//			setOrbitEmbedding<FACE>(d, getEmbedding<FACE>(d)) ;
316 317 318 319 320 321 322 323 324 325
//		}
//	}
//}
//
//void EmbeddedGMap2::removeEdgeFromVertex(Dart d)
//{
//	Dart dPrev = alpha_1(d);
//
//	GMap2::removeEdgeFromVertex(d);
//
326
//	if (isOrbitEmbedded<VERTEX>())
327
//	{
328
//		setOrbitEmbeddingOnNewCell<VERTEX>(d);
329
//		copyCell<VERTEX>(d, dPrev);
330 331
//	}
//
332
//	if (isOrbitEmbedded<FACE>())
333 334 335
//	{
//		if(!sameFace(d, dPrev))
//		{
336
//			setOrbitEmbeddingOnNewCell<FACE>(d);
337
//			copyCell<FACE>(d, dPrev) ;
338 339 340
//		}
//		else
//		{
341
//			setOrbitEmbedding<FACE>(d, getEmbedding<FACE>(d)) ;
342 343 344
//		}
//	}
//}
345

346
void EmbeddedGMap2::sewFaces(Dart d, Dart e, bool withBoundary)
347
{
348 349
	// for fixed point construction (import & primitives)
	if (!withBoundary)
350
	{
351
		GMap2::sewFaces(d, e, false) ;
Thery Sylvain's avatar
Thery Sylvain committed
352 353 354 355
		if(isOrbitEmbedded<EDGE>())
		{
			initOrbitEmbeddingNewCell<EDGE>(d) ;
		}
356
		return ;
357 358
	}

359
	GMap2::sewFaces(d, e, true) ;
360

361
	if (isOrbitEmbedded<VERTEX>())
362
	{
363 364
		setOrbitEmbedding<VERTEX>(d, getEmbedding<VERTEX>(d)) ;
		setOrbitEmbedding<VERTEX>(e, getEmbedding<VERTEX>(beta0(d))) ;
365 366
	}

367
	if (isOrbitEmbedded<EDGE>())
368
	{
369
		setOrbitEmbedding<EDGE>(e, getEmbedding<EDGE>(d)) ;
370 371 372 373 374
	}
}

void EmbeddedGMap2::unsewFaces(Dart d)
{
375 376
	Dart e = beta2(d);
	GMap2::unsewFaces(d);
377

378
	if (isOrbitEmbedded<VERTEX>())
379
	{
380
		if(!sameVertex(d,e))
381
		{
382
			setOrbitEmbeddingOnNewCell<VERTEX>(e);
383
			copyCell<VERTEX>(e, d);
384 385
		}

386 387 388 389
		d = beta0(d);
		e = beta0(e);

		if(!sameVertex(d,e))
390
		{
391
			setOrbitEmbeddingOnNewCell<VERTEX>(e);
392
			copyCell<VERTEX>(e, d);
393 394
		}
	}
395

396
	if (isOrbitEmbedded<EDGE>())
397
	{
398
		setOrbitEmbeddingOnNewCell<EDGE>(e);
399
		copyCell<EDGE>(e, d);
400 401 402 403 404
	}
}

bool EmbeddedGMap2::collapseDegeneratedFace(Dart d)
{
405 406 407 408
	Dart e = beta2(d) ;
	bool updateEdgeEmb = false ;
	if(phi1(d) != d)
		updateEdgeEmb = true ;
409 410 411

	if(GMap2::collapseDegeneratedFace(d))
	{
412
		if (isOrbitEmbedded<EDGE>() && updateEdgeEmb)
413
		{
414 415 416
			unsigned int eEmb = getEmbedding<EDGE>(e) ;
			setDartEmbedding<EDGE>(beta2(e), eEmb) ;
			setDartEmbedding<EDGE>(phi2(e), eEmb) ;
417 418 419 420 421 422 423 424 425 426
		}
		return true ;
	}
	return false ;
}

void EmbeddedGMap2::splitFace(Dart d, Dart e)
{
	GMap2::splitFace(d, e) ;

427
	if (isOrbitEmbedded<VERTEX>())
428
	{
429 430 431 432 433 434 435 436
		unsigned int v1Emb = getEmbedding<VERTEX>(d) ;
		setDartEmbedding<VERTEX>(phi_1(e), v1Emb) ;
		setDartEmbedding<VERTEX>(beta1(d), v1Emb) ;
		setDartEmbedding<VERTEX>(beta1(phi_1(e)), v1Emb) ;
		unsigned int v2Emb = getEmbedding<VERTEX>(e) ;
		setDartEmbedding<VERTEX>(phi_1(d), v2Emb) ;
		setDartEmbedding<VERTEX>(beta1(e), v2Emb) ;
		setDartEmbedding<VERTEX>(beta1(phi_1(d)), v2Emb) ;
437
	}
438 439 440 441 442 443

	if(isOrbitEmbedded<EDGE>())
	{
		initOrbitEmbeddingNewCell<EDGE>(beta1(d)) ;
	}

444
	if (isOrbitEmbedded<FACE>())
445
	{
446 447 448
		unsigned int fEmb = getEmbedding<FACE>(d) ;
		setDartEmbedding<FACE>(phi_1(d), fEmb) ;
		setDartEmbedding<FACE>(beta1(phi_1(d)), fEmb) ;
449
		setOrbitEmbeddingOnNewCell<FACE>(e) ;
450
		copyCell<FACE>(e, d) ;
451 452 453 454 455 456 457 458 459
	}
}

bool EmbeddedGMap2::mergeFaces(Dart d)
{
	Dart dNext = phi1(d) ;

	if(GMap2::mergeFaces(d))
	{
460
		if (isOrbitEmbedded<FACE>())
461
		{
462
			setOrbitEmbedding<FACE>(dNext, getEmbedding<FACE>(dNext)) ;
463 464 465 466 467 468 469 470 471 472 473
		}
		return true ;
	}
	return false ;
}

bool EmbeddedGMap2::mergeVolumes(Dart d, Dart e)
{
	std::vector<Dart> darts ;
	std::vector<unsigned int> vEmb ;
	std::vector<unsigned int> eEmb ;
474 475 476 477
	darts.reserve(32) ;
	vEmb.reserve(32) ;
	eEmb.reserve(32) ;

478 479 480 481 482
	Dart fit = d ;
	do
	{
		darts.push_back(phi2(fit)) ;

483
		if (isOrbitEmbedded<VERTEX>())
484
		{
485
			vEmb.push_back(getEmbedding<VERTEX>(phi2(fit))) ;
486 487
		}

488
		if (isOrbitEmbedded<EDGE>())
489
		{
490
			eEmb.push_back(getEmbedding<EDGE>(fit)) ;
491 492 493 494 495 496 497 498 499
		}

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

	if(GMap2::mergeVolumes(d, e))
	{
		for(unsigned int i = 0; i < darts.size(); ++i)
		{
500
			if (isOrbitEmbedded<VERTEX>())
501
			{
502
				setOrbitEmbedding<VERTEX>(darts[i], vEmb[i]) ;
503 504
			}

505
			if (isOrbitEmbedded<EDGE>())
506
			{
507
				setOrbitEmbedding<EDGE>(darts[i], eEmb[i]) ;
508 509 510 511 512 513 514
			}
		}
		return true ;
	}
	return false ;
}

515
unsigned int EmbeddedGMap2::closeHole(Dart d, bool forboundary)
516 517 518
{
	unsigned int nbE = GMap2::closeHole(d) ;
	Dart dd = phi2(d) ;
519
	Dart it = dd ;
520 521
	do
	{
522
		if (isOrbitEmbedded<VERTEX>())
523
		{
524 525
			copyDartEmbedding<VERTEX>(it, beta2(it)) ;
			copyDartEmbedding<VERTEX>(beta0(it), phi2(it)) ;
526
		}
527
		if (isOrbitEmbedded<EDGE>())
528
		{
529 530 531
			unsigned int eEmb = getEmbedding<EDGE>(beta2(it)) ;
			setDartEmbedding<EDGE>(it, eEmb) ;
			setDartEmbedding<EDGE>(beta0(it), eEmb) ;
532
		}
533 534
		it = phi1(it) ;
	} while(it != dd) ;
535 536 537 538 539 540

	if(isOrbitEmbedded<FACE>())
	{
		initOrbitEmbeddingNewCell<FACE>(dd) ;
	}

541 542 543 544 545 546 547 548 549 550 551 552
	return nbE ;
}

bool EmbeddedGMap2::check()
{
	bool topo = GMap2::check() ;
	if (!topo)
		return false ;

	CGoGNout << "Check: embedding begin" << CGoGNendl ;
	for(Dart d = begin(); d != end(); next(d))
	{
553
		if (isOrbitEmbedded<VERTEX>())
554
		{
555
			if (getEmbedding<VERTEX>(d) != getEmbedding<VERTEX>(alpha1(d)))
556 557 558 559 560 561
			{
				CGoGNout << "Check: different embeddings on vertex" << CGoGNendl ;
				return false ;
			}
		}

562
		if (isOrbitEmbedded<EDGE>())
563
		{
564
			if (getEmbedding<EDGE>(d) != getEmbedding<EDGE>(phi2(d)))
565 566 567 568 569 570
			{
				CGoGNout << "Check: different embeddings on edge" << CGoGNendl ;
				return false ;
			}
		}

571
		if (isOrbitEmbedded<FACE>())
572
		{
573
			if (getEmbedding<FACE>(d) != getEmbedding<FACE>(phi1(d)))
574 575 576 577 578 579
		{
				CGoGNout << "Check: different embeddings on face" << CGoGNendl ;
				return false ;
			}
		}
	}
580

581
	CGoGNout << "Check: embedding ok" << CGoGNendl ;
582

583
    std::cout << "nb vertex orbits : " << getNbOrbits<VERTEX>() << std::endl ;
584 585
    std::cout << "nb vertex cells : " << m_attribs[VERTEX].size() << std::endl ;

586
    std::cout << "nb edge orbits : " << getNbOrbits<EDGE>() << std::endl ;
587 588
    std::cout << "nb edge cells : " << m_attribs[EDGE].size() << std::endl ;

589
    std::cout << "nb face orbits : " << getNbOrbits<FACE>() << std::endl ;
590 591
    std::cout << "nb face cells : " << m_attribs[FACE].size() << std::endl ;

592 593 594 595
	return true ;
}

} // namespace CGoGN