Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

map2.cpp 24.7 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
32
33
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Topology/map/map2.h"

namespace CGoGN
{

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

Sylvain Thery's avatar
Sylvain Thery committed
34
35
36
37
38
39
40
41
42
void Map2::mergeBoundaryFaces(Dart dd, Dart ee)
{
	if (ee != phi_1(dd))
		phi1sew(ee, phi_1(dd)) ;
	if (ee != phi_1(dd))
		phi1sew(ee, phi_1(dd)) ;
	Map1::deleteOrientedFace(dd) ;
}

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//void Map2::mergeFacewithBoundary(Dart d)
//{
//	Dart e = d ;
//	do									// foreach edge of face
//	{
//		Dart f = phi2(e);
//		if (isBoundaryMarked(f))		// if sewed to boundary
//		{
//			phi2unsew(e);// ?? not necessary
//			Dart ff = phi_1(f);
//			if (e != ff)
//				phi1sew(e, ff) ;		//    merge with it
//			Dart ee = phi_1(e);
//			if (f != ee)
//				phi1sew(f, ee) ;
//			Map1::deleteOrientedFace(e) ;
//		}
//		e = phi1(e) ;
//	} while (e != d) ;
//}

Sylvain Thery's avatar
Sylvain Thery committed
64
void Map2::mergeFacewithBoundary(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
65
{
66
67
68
	std::vector<Dart> storeForLinkVertex;
	std::vector<Dart> storeForLinkFace;

Pierre Kraemer's avatar
Pierre Kraemer committed
69
	Dart e = d ;
70
	do									// foreach vertex/edge of face
Pierre Kraemer's avatar
Pierre Kraemer committed
71
	{
72
73
74
75
76
77
78
79
		Dart f = findBoundaryVertex(alpha1(e));	// check if connexion by vertex
		if (f != e)
		{
			storeForLinkVertex.push_back(phi_1(e));
			storeForLinkVertex.push_back(phi_1(f));
		}

		Dart g = phi2(f);
Pierre Kraemer's avatar
Pierre Kraemer committed
80
		if (isBoundaryMarked(g))			// check if connexion by a face
Sylvain Thery's avatar
Sylvain Thery committed
81
		{
82
83
			storeForLinkFace.push_back(f);
			storeForLinkFace.push_back(g);
Sylvain Thery's avatar
Sylvain Thery committed
84
85
		}
		e = phi1(e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86
	} while (e != d) ;
87
88
89
90

	// merge by vertices
	while (!storeForLinkVertex.empty())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
91
92
93
94
		Dart a = storeForLinkVertex.back() ;
		storeForLinkVertex.pop_back() ;
		Dart b = storeForLinkVertex.back() ;
		storeForLinkVertex.pop_back() ;
95
96
97
98
99
		phi1sew(a,b);
	}
	//merge by faces
	while (!storeForLinkFace.empty())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
100
101
102
103
		Dart a = storeForLinkVertex.back() ;
		storeForLinkVertex.pop_back() ;
		Dart b = storeForLinkVertex.back() ;
		storeForLinkVertex.pop_back() ;
104
105
		mergeBoundaryFaces(a,b);
	}
Sylvain Thery's avatar
Sylvain Thery committed
106
107
108
109
110
111
112
113
}

void Map2::deleteOrientedFace(Dart d)
{
	// tag face in boundary
	boundaryMarkOrbit(FACE,d);
	mergeFacewithBoundary(d);

Pierre Kraemer's avatar
Pierre Kraemer committed
114
115
}

Sylvain Thery's avatar
Sylvain Thery committed
116
117
118
119
120
121
122
void Map2::sewOrientedFaces(Dart d, Dart e)
{
	assert(phi2(d)==d && phi2(e)==e);
	// sewing the faces
	phi2sew(d, e);
}

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Dart Map2::newFace(unsigned int nbEdges)
{
	Dart d = Map1::newFace(nbEdges);
	Dart e = Map1::newBoundaryFace(nbEdges);

	Dart x = d;
	do
	{
		phi2sew(d,e);
		d = phi1(d);
		e = phi1(e);
	} while (d != x);
	return x;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*! @name Topological Operators
 *  Topological operations on 2-maps
 *************************************************************************/

void Map2::splitVertex(Dart d, Dart e)
{
	assert(sameOrientedVertex(d, e));
	Dart dd = phi2(d) ;
	Dart ee = phi2(e) ;
	Map1::cutEdge(dd);			// Cut the edge of dd (make a new half edge)
	Map1::cutEdge(ee);			// Cut the edge of ee (make a new half edge)
	phi2sew(phi1(dd), phi1(ee));// Sew the two faces along the new edge
}

Pierre Kraemer's avatar
Pierre Kraemer committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
bool Map2::deleteVertex(Dart d)
{
	if(isBoundaryVertex(d))
		return false ;

	Dart vit = d ;
	do
	{
		Dart f = phi_1(phi2(vit)) ;
		phi1sew(vit, f) ;
		vit = alpha1(vit) ;
	} while(vit != d) ;
	Map1::deleteFace(d) ;
	return true ;
}

168
169
170
171
172
173
void Map2::linkVertices(Dart d, Dart e)
{
	Map1::linkVertices(d, e);			// Split the face
	phi2sew(phi_1(d), phi_1(e));	// Sew the two resulting faces along the new edge
}

Pierre Kraemer's avatar
Pierre Kraemer committed
174
175
void Map2::cutEdge(Dart d)
{
Sylvain Thery's avatar
Sylvain Thery committed
176
177
178
179
180
181
182
183
184
185
186
187
188
//	Map1::cutEdge(d);		// Cut the edge of d
//	Dart nd = phi1(d);
//	Dart e = phi2(d);
//	if (e != d)				// Test if an opposite edge exists
//	{
//		Map1::cutEdge(e);	// Cut the opposite edge
//		Dart ne = phi1(e);
//		phi2unsew(d);		// Correct the phi2 links
//		phi2sew(d, ne);
//		phi2sew(e, nd);
//	}

	Map1::cutEdge(d);		// Cut the 1-edge of d
Pierre Kraemer's avatar
Pierre Kraemer committed
189
	Dart e = phi2(d);
Sylvain Thery's avatar
Sylvain Thery committed
190
191
192
193
194
195
196
197
198
	Map1::cutEdge(e);		// Cut the 1-edge of phi2(d)

	phi2unsew(d);			// remove old phi2 links

	Dart nd = phi1(d);
	Dart ne = phi1(e);

	phi2sew(d, ne);			// Correct the phi2 links
	phi2sew(e, nd);
Pierre Kraemer's avatar
Pierre Kraemer committed
199
200
}

Pierre Kraemer's avatar
Pierre Kraemer committed
201
bool Map2::uncutEdge(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
202
{
Sylvain Thery's avatar
Sylvain Thery committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//	assert(vertexDegree(phi1(d)) == 2) ;
//	Dart ne = phi2(d) ;
//	if(ne == d)
//		collapseEdge(d) ;
//	else
//	{
//		Dart nd = phi1(d) ;
//		Dart e = phi_1(ne) ;
//		phi2unsew(e) ;
//		phi2unsew(d) ;
//		Map1::collapseEdge(nd) ;
//		Map1::collapseEdge(ne) ;
//		phi2sew(d, e) ;
//	}

Pierre Kraemer's avatar
Pierre Kraemer committed
218
219
220
221
222
223
224
225
226
227
228
229
230
	if(vertexDegree(phi1(d)) == 2)
	{
		Dart ne = phi2(d) ;
		Dart nd = phi1(d) ;
		Dart e = phi_1(ne) ;
		phi2unsew(e) ;
		phi2unsew(d) ;
		Map1::collapseEdge(nd) ;
		Map1::collapseEdge(ne) ;
		phi2sew(d, e) ;
		return true ;
	}
	return false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
231
232
}

233
Dart Map2::collapseEdge(Dart d, bool delDegenerateFaces)
Pierre Kraemer's avatar
Pierre Kraemer committed
234
{
Sylvain Thery's avatar
Sylvain Thery committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//	Dart resV ;
//
//	Dart e = phi2(d);
//	if (e != d)			// Test if an opposite edge exists
//	{
//		phi2unsew(d);	// Unlink the opposite edges
//		Dart f = phi1(e) ;
//		Dart g = phi_1(e) ;
//
//		if(f != d && !isFaceTriangle(e))
//			resV = f ;
//		else if(phi2(g) != g)
//			resV = phi2(g) ;
//		else if(f != d && phi2(f) != f)
//			resV = phi1(phi2(f)) ;
//
//		if (f != e && delDegenerateFaces)
//		{
//			Map1::collapseEdge(e) ;		// Collapse edge e
//			collapseDegeneratedFace(f) ;// and collapse its face if degenerated
//		}
//		else
//			Map1::collapseEdge(e) ;	// Just collapse edge e
//	}
//
//	Dart f = phi1(d) ;
//	Dart g = phi_1(d) ;
//
//	if(resV == Dart::nil())
//	{
//		if(!isFaceTriangle(d))
//			resV = f ;
//		else if(phi2(g) != g)
//			resV = phi2(g) ;
//		else if(phi2(f) != f)
//			resV = phi1(phi2(f)) ;
//	}
//
//	if (f != d && delDegenerateFaces)
//	{
//		Map1::collapseEdge(d) ;		// Collapse edge d
//		collapseDegeneratedFace(f) ;// and collapse its face if degenerated
//	}
//	else
//		Map1::collapseEdge(d) ;	// Just collapse edge d
//
//	return resV ;

Pierre Kraemer's avatar
Pierre Kraemer committed
283
	Dart resV = NIL ;
Sylvain Thery's avatar
Sylvain Thery committed
284

285
	Dart e = phi2(d);
Sylvain Thery's avatar
Sylvain Thery committed
286
287
	phi2unsew(d);	// Unlink the opposite edges

288
289
290
291
292
293
294
	Dart f = phi1(e) ;
	Dart h = alpha1(e);

	if (h != e)
		resV=h;

	if (f != e && delDegenerateFaces)
Sylvain Thery's avatar
Sylvain Thery committed
295
296
	{
		Map1::collapseEdge(e) ;		// Collapse edge e
297
		collapseDegeneratedFace(f) ;// and collapse its face if degenerated
Sylvain Thery's avatar
Sylvain Thery committed
298
299
	}
	else
300
		Map1::collapseEdge(e) ;	// Just collapse edge e
301

302
303
304
305
306
307
	f = phi1(d) ;
	if(resV == NIL)
	{
		h = alpha1(d);
		if (h != d)
			resV=h;
Pierre Kraemer's avatar
Pierre Kraemer committed
308
	}
309

310
	if (f != d && delDegenerateFaces)
Pierre Kraemer's avatar
Pierre Kraemer committed
311
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
312
		Map1::collapseEdge(d) ;		// Collapse edge d
313
		collapseDegeneratedFace(f) ;// and collapse its face if degenerated
Pierre Kraemer's avatar
Pierre Kraemer committed
314
315
	}
	else
316
		Map1::collapseEdge(d) ;	// Just collapse edge d
317
318

	return resV ;
Pierre Kraemer's avatar
Pierre Kraemer committed
319
320
321
322
}

bool Map2::flipEdge(Dart d)
{
Sylvain Thery's avatar
Sylvain Thery committed
323
	if (!isBoundaryEdge(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
324
	{
Sylvain Thery's avatar
Sylvain Thery committed
325
		Dart e = phi2(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
		Dart dNext = phi1(d);
		Dart eNext = phi1(e);
		Dart dPrev = phi_1(d);
		Dart ePrev = phi_1(e);
		phi1sew(d, ePrev);		// Detach the two
		phi1sew(e, dPrev);		// vertices of the edge
		phi1sew(d, dNext);		// Insert the edge in its
		phi1sew(e, eNext);		// new vertices after flip
		return true ;
	}
	return false ; // cannot flip a border edge
}

bool Map2::flipBackEdge(Dart d)
{
Sylvain Thery's avatar
Sylvain Thery committed
341
	if (!isBoundaryEdge(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
342
	{
Sylvain Thery's avatar
Sylvain Thery committed
343
		Dart e = phi2(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
344
345
346
347
348
349
350
351
352
353
354
		Dart dPrev = phi_1(d);
		Dart ePrev = phi_1(e);
		phi1sew(d, ePrev);			// Detach the two
		phi1sew(e, dPrev);			// vertices of the edge
		phi1sew(e, phi_1(dPrev));	// Insert the edge in its
		phi1sew(d, phi_1(ePrev));	// new vertices after flip
		return true ;
	}
	return false ; // cannot flip a border edge
}

355
356
void Map2::insertEdgeInVertex(Dart d, Dart e)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
357
	assert(!sameVertex(d,e) && phi2(e) == phi_1(e));
358
359
360
361
362
363

	phi1sew(phi_1(d),phi_1(e));
}

void Map2::removeEdgeFromVertex(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
364
	assert(phi2(d) != d);
365
366
367
368

	phi1sew(phi_1(d),phi2(d));
}

Pierre Kraemer's avatar
Pierre Kraemer committed
369
370
void Map2::sewFaces(Dart d, Dart e)
{
Sylvain Thery's avatar
Sylvain Thery committed
371
	Dart dd = phi2(d);
Sylvain Thery's avatar
Sylvain Thery committed
372
	Dart ee = phi2(e);
Sylvain Thery's avatar
Sylvain Thery committed
373
374
375
376
	// unsew from boundary
	phi2unsew(d);
	phi2unsew(e);

Sylvain Thery's avatar
Sylvain Thery committed
377
378
	// remove boundary edge (could be a Map1::mergeFaces without assert ?)
	mergeBoundaryFaces(dd,ee);
Sylvain Thery's avatar
Sylvain Thery committed
379

Sylvain Thery's avatar
Sylvain Thery committed
380
	// sewing the faces
Pierre Kraemer's avatar
Pierre Kraemer committed
381
382
383
384
385
	phi2sew(d, e);
}

void Map2::unsewFaces(Dart d)
{
Sylvain Thery's avatar
Sylvain Thery committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
	Dart dd = phi2(d);

	Dart e = newBoundaryFace(2);

	if (isBoundaryVertex(d))
	{
		Dart f = findBoundaryVertex(d);
		phi1sew(e,phi_1(f));
	}

	if (isBoundaryVertex(dd))
	{
		Dart f = findBoundaryVertex(dd);
		phi1sew(phi_1(e),phi_1(f));
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
402
	phi2unsew(d);
Sylvain Thery's avatar
Sylvain Thery committed
403
404
405
	// sew faces to the boundary
	phi2sew(d,e);
	phi2sew(dd,phi1(e));
Pierre Kraemer's avatar
Pierre Kraemer committed
406
407
408
409
}

bool Map2::collapseDegeneratedFace(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
410
	Dart e = phi1(d);				// Check if the face is degenerated
Pierre Kraemer's avatar
Pierre Kraemer committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
	if (phi1(e) == d)				// Yes: it contains one or two edge(s)
	{
		Dart d2 = phi2(d);			// Check opposite edges
		Dart e2 = phi2(e);
		if (d2 != d) phi2unsew(d);	// Update phi2-links if needed
		if (e2 != e) phi2unsew(e);
		if (d2 != d && e2 != e)
			phi2sew(d2, e2);
		Map1::deleteOrientedFace(d);// Delete the single edge or two edges of the loop
		return true ;
	}
	return false ;
}

void Map2::splitFace(Dart d, Dart e)
{
	Map1::splitFace(d, e);			// Split the face
	phi2sew(phi_1(d), phi_1(e));	// Sew the two resulting faces along the new edge
}

bool Map2::mergeFaces(Dart d)
{
Sylvain Thery's avatar
Sylvain Thery committed
433
	if (!isBoundaryEdge(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
434
	{
Sylvain Thery's avatar
Sylvain Thery committed
435
		Dart e = phi2(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
436
437
438
439
440
441
442
443
444
		phi2unsew(d);			// unsew the face of d
		Map1::mergeFaces(d, e); // merge the two faces along edges of d and e
		return true ;
	}
	return false ;
}

void Map2::extractTrianglePair(Dart d)
{
Sylvain Thery's avatar
Sylvain Thery committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//	assert(isFaceTriangle(d)) ;
//	Dart e = phi2(d) ;
//	if(e != d)
//	{
//		assert(isFaceTriangle(e)) ;
//		Dart e1 = phi2(phi1(e)) ;
//		Dart e2 = phi2(phi_1(e)) ;
//		phi2unsew(e1) ;
//		phi2unsew(e2) ;
//		phi2sew(e1, e2) ;
//	}
//	Dart d1 = phi2(phi1(d)) ;
//	Dart d2 = phi2(phi_1(d)) ;
//	phi2unsew(d1) ;
//	phi2unsew(d2) ;
//	phi2sew(d1, d2) ;

	if (!isBoundaryMarked(d))
	{
		assert(isFaceTriangle(d)) ;
		Dart d1 = phi2(phi1(d)) ;
		Dart d2 = phi2(phi_1(d)) ;
		phi2unsew(d1) ;
		phi2unsew(d2) ;
		phi2sew(d1, d2) ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
472
	Dart e = phi2(d) ;
Sylvain Thery's avatar
Sylvain Thery committed
473
	if (!isBoundaryMarked(e))
Pierre Kraemer's avatar
Pierre Kraemer committed
474
475
476
477
478
479
480
481
	{
		assert(isFaceTriangle(e)) ;
		Dart e1 = phi2(phi1(e)) ;
		Dart e2 = phi2(phi_1(e)) ;
		phi2unsew(e1) ;
		phi2unsew(e2) ;
		phi2sew(e1, e2) ;
	}
Sylvain Thery's avatar
Sylvain Thery committed
482

Pierre Kraemer's avatar
Pierre Kraemer committed
483
484
485
486
}

void Map2::insertTrianglePair(Dart d, Dart v1, Dart v2)
{
Sylvain Thery's avatar
Sylvain Thery committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//	assert(sameOrientedVertex(v1, v2)) ;
//	assert((v1 != v2 && phi2(d) != d) || (v1 == v2 && phi2(d) == d)) ;
//	assert(isFaceTriangle(d) && phi2(phi1(d)) == phi1(d) && phi2(phi_1(d)) == phi_1(d)) ;
//	Dart e = phi2(d) ;
//	if(e != d && v1 != v2)
//	{
//		assert(isFaceTriangle(e) && phi2(phi1(e)) == phi1(e) && phi2(phi_1(e)) == phi_1(e)) ;
//		Dart vv2 = phi2(v2) ;
//		phi2unsew(v2) ;
//		phi2sew(phi_1(e), v2) ;
//		phi2sew(phi1(e), vv2) ;
//	}
//	Dart vv1 = phi2(v1) ;
//	phi2unsew(v1) ;
//	phi2sew(phi_1(d), v1) ;
//	phi2sew(phi1(d), vv1) ;


Pierre Kraemer's avatar
Pierre Kraemer committed
505
	assert(sameOrientedVertex(v1, v2)) ;
Sylvain Thery's avatar
Sylvain Thery committed
506
507
	assert((v1 != v2 && !isBoundaryMarked(d)) || (v1 == v2 && isBoundaryMarked(d))) ;
	assert(isFaceTriangle(d) && isBoundaryMarked(phi1(d)) && isBoundaryMarked(phi_1(d)) ) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
508
	Dart e = phi2(d) ;
Sylvain Thery's avatar
Sylvain Thery committed
509
	if (!isBoundaryEdge(d) && v1 != v2)
Pierre Kraemer's avatar
Pierre Kraemer committed
510
	{
Sylvain Thery's avatar
Sylvain Thery committed
511
		assert( isFaceTriangle(e) && isBoundaryMarked(phi1(e)) && isBoundaryMarked(phi_1(e)) ) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
512
513
514
515
516
517
518
519
520
521
522
		Dart vv2 = phi2(v2) ;
		phi2unsew(v2) ;
		phi2sew(phi_1(e), v2) ;
		phi2sew(phi1(e), vv2) ;
	}
	Dart vv1 = phi2(v1) ;
	phi2unsew(v1) ;
	phi2sew(phi_1(d), v1) ;
	phi2sew(phi1(d), vv1) ;
}

523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
void Map2::unsewAroundVertex(Dart d)
{
	Dart e = d;

	do
	{
		Dart temp = phi1(e);
		Dart e_1 = phi_1(e);

		do
		{
			unsewFaces(temp);
			temp = phi1(temp);
		}while(temp != e_1);

		e = alpha1(e);
	}
	while(e != d);
}

Sylvain Thery's avatar
Sylvain Thery committed
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//void Map2::explodPolyhedron(Dart d)
//{
//	unsewAroundVertex(d);
////	closeHole(phi1(d));
//
//	//Recherche du (ou un des) sommet oppose
//		//tourner autour du sommet
//			//si quad  alors sommet oppose
//			//
//		//si pas de quad alors un sommet du trian
//
//	//
//}

Pierre Kraemer's avatar
Pierre Kraemer committed
557
558
bool Map2::mergeVolumes(Dart d, Dart e)
{
Sylvain Thery's avatar
Sylvain Thery committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//	// First traversal of both faces to check the face sizes
//	// and stored 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 = phi1(e) ;	// must take phi1 because of the use
//	do						// of reverse iterator for sewing loop
//	{
//		dDarts.push_back(dFit) ;
//		dFit = phi1(dFit) ;
//	} while(dFit != d) ;
//	do
//	{
//		eDarts.push_back(eFit) ;
//		eFit = phi1(eFit) ;
//	} while(eFit != 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);
//		if (d2 != *dIt) phi2unsew(d2);	// Unlink the two adjacent faces from dNext and eNext
//		if (e2 != *eIt) phi2unsew(e2);
//		if (d2 != *dIt && e2 != *eIt) phi2sew(d2,e2); // Link the two adjacent faces together if they exists
//	}
//	Map1::deleteOrientedFace(d);	// Delete the two alone faces
//	Map1::deleteOrientedFace(e);
//
//	return true ;

	if (isBoundaryMarked(d) || isBoundaryMarked(e))
		return false;

Pierre Kraemer's avatar
Pierre Kraemer committed
602
603
	// First traversal of both faces to check the face sizes
	// and stored their edges to efficiently access them further
Sylvain Thery's avatar
Sylvain Thery committed
604

Pierre Kraemer's avatar
Pierre Kraemer committed
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
	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 = phi1(e) ;	// must take phi1 because of the use
	do						// of reverse iterator for sewing loop
	{
		dDarts.push_back(dFit) ;
		dFit = phi1(dFit) ;
	} while(dFit != d) ;
	do
	{
		eDarts.push_back(eFit) ;
		eFit = phi1(eFit) ;
	} while(eFit != 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);
Sylvain Thery's avatar
Sylvain Thery committed
634
635
636
637
638
		phi2unsew(d2);	// Unlink the two adjacent faces from dNext and eNext
		phi2unsew(e2);
		phi2sew(d2,e2); // Link the two adjacent faces together
		if (isBoundaryMarked(d2) && isBoundaryMarked(e2))
			mergeBoundaryFaces(d2,e2);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	}
	Map1::deleteOrientedFace(d);	// Delete the two alone faces
	Map1::deleteOrientedFace(e);

	return true ;
}

unsigned int Map2::closeHole(Dart d)
{
	assert(phi2(d) == d);		// Nothing to close

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

	if (countEdges == 2)
	{
		countEdges = 0 ;
		collapseDegeneratedFace(first); // if the closing face is 2-sided, collapse it
	}

	return countEdges ;
}

Sylvain Thery's avatar
Sylvain Thery committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//void Map2::closeMap(DartMarker& marker)
//{
//	// Search the map for topological holes (fixed point for phi2)
//	for (Dart d = begin(); d != end(); next(d))
//	{
//		if (phi2(d) == d)
//		{
//			closeHole(d);
//			marker.markOrbit(FACE, phi2(d)) ;
//		}
//	}
//
void Map2::closeMap()
{	// Search the map for topological holes (fixed point for phi2)
Pierre Kraemer's avatar
Pierre Kraemer committed
697
698
699
700
701
	for (Dart d = begin(); d != end(); next(d))
	{
		if (phi2(d) == d)
		{
			closeHole(d);
Sylvain Thery's avatar
Sylvain Thery committed
702
			boundaryMarkOrbit(FACE,phi2(d));
Pierre Kraemer's avatar
Pierre Kraemer committed
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
		}
	}
}

/*! @name Topological Queries
 *  Return or set various topological information
 *************************************************************************/

bool Map2::sameOrientedVertex(Dart d, Dart e)
{
	Dart dNext = d;				// Foreach dart dNext in the vertex of d
	do
	{
		if (dNext == e)			// Test equality with e
			return true;
		dNext = alpha1(dNext);
	} while (dNext != d);
	return false;				// None is equal to e => vertices are distinct
}

untereiner's avatar
untereiner committed
723
724
725
bool Map2::sameOrientedFace(Dart d, Dart e)
{
	Dart dNext = d;
Pierre Kraemer's avatar
Pierre Kraemer committed
726
	do							// Foreach dart dNext in the face of d
untereiner's avatar
untereiner committed
727
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
728
		if(dNext == e)			// Test equality with e
untereiner's avatar
untereiner committed
729
730
			return true;
		dNext = phi1(dNext);
Pierre Kraemer's avatar
Pierre Kraemer committed
731
732
	} while(dNext != d);
	return false;				// None is equal to e => faces are distinct
untereiner's avatar
untereiner committed
733
734
}

Pierre Kraemer's avatar
Pierre Kraemer committed
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
unsigned int Map2::vertexDegree(Dart d)
{
	unsigned int count = 0 ;
	Dart dNext = d ;
	do
	{
		++count ;
		if(phi2(dNext) == dNext)
			++count ;
		dNext = alpha1(dNext) ;
	} while (dNext != d) ;
	return count ;
}

unsigned int Map2::volumeDegree(Dart d)
{
Sylvain Thery's avatar
Sylvain Thery committed
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//	unsigned int count = 0;
//	DartMarkerStore mark(*this);		// Lock a marker
//
//	std::vector<Dart> visitedFaces;		// Faces that are traversed
//	visitedFaces.reserve(16);
//	visitedFaces.push_back(d);			// Start with the face of d
//	std::vector<Dart>::iterator face;
//
//	// For every face added to the list
//	for (face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
//	{
//		if (!mark.isMarked(*face))		// Face has not been visited yet
//		{
//			++count;
//
//			Dart dNext = *face ;
//			do
//			{
//				mark.mark(dNext);					// Mark
//				Dart adj = phi2(dNext);				// Get adjacent face
//				if (adj != dNext && !mark.isMarked(adj))
//					visitedFaces.push_back(adj);	// Add it
//				dNext = phi1(dNext);
//			} while(dNext != *face);
//		}
//	}
//
//	return count;


Pierre Kraemer's avatar
Pierre Kraemer committed
781
782
783
784
785
786
787
788
789
	unsigned int count = 0;
	DartMarkerStore mark(*this);		// Lock a marker

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

	// For every face added to the list
Sylvain Thery's avatar
Sylvain Thery committed
790
	for (unsigned int i=0; i != visitedFaces.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
791
	{
Sylvain Thery's avatar
Sylvain Thery committed
792
793
		Dart df = visitedFaces[i];
		if (!isBoundaryMarked(df) && !mark.isMarked(df))		// Face has not been visited yet
Pierre Kraemer's avatar
Pierre Kraemer committed
794
795
796
		{
			++count;

Sylvain Thery's avatar
Sylvain Thery committed
797
			Dart dNext = df ;
Pierre Kraemer's avatar
Pierre Kraemer committed
798
799
800
801
			do
			{
				mark.mark(dNext);					// Mark
				Dart adj = phi2(dNext);				// Get adjacent face
Sylvain Thery's avatar
Sylvain Thery committed
802
				if ( !isBoundaryMarked(adj) && !mark.isMarked(adj) )
Pierre Kraemer's avatar
Pierre Kraemer committed
803
804
					visitedFaces.push_back(adj);	// Add it
				dNext = phi1(dNext);
Sylvain Thery's avatar
Sylvain Thery committed
805
			} while(dNext != df);
Pierre Kraemer's avatar
Pierre Kraemer committed
806
807
808
809
		}
	}

	return count;
Sylvain Thery's avatar
Sylvain Thery committed
810

Pierre Kraemer's avatar
Pierre Kraemer committed
811
812
813
814
815
816
817
}

bool Map2::isBoundaryVertex(Dart d)
{
	Dart dNext = d ;
	do
	{
Sylvain Thery's avatar
Sylvain Thery committed
818
		if (isBoundaryMarked(dNext))
Pierre Kraemer's avatar
Pierre Kraemer committed
819
820
821
822
			return true ;
		dNext = alpha1(dNext) ;
	} while (dNext != d) ;
	return false ;
Sylvain Thery's avatar
Sylvain Thery committed
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
}

Dart Map2::findBoundaryVertex(Dart d)
{
	Dart dNext = d ;
	do
	{
		if (isBoundaryMarked(dNext))
			return dNext ;
		dNext = alpha1(dNext) ;
	} while (dNext != d) ;
	return Dart::nil();
}

bool Map2::isBoundaryEdge(Dart d)
{
	Dart e = phi2(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
840
	return isBoundaryMarked(e) || isBoundaryMarked(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
841
842
}

Pierre Kraemer's avatar
Pierre Kraemer committed
843
844
845
846
847
848
849
850
851
852
853
bool Map2::isBoundaryFace(Dart d)
{
	Dart it = d ;
	do
	{
		if (isBoundaryMarked(phi2(it)))
			return true ;
		it = phi1(it) ;
	} while (it != d) ;
	return false ;
}
Sylvain Thery's avatar
Sylvain Thery committed
854

Pierre Kraemer's avatar
Pierre Kraemer committed
855
856
Dart Map2::nextOnBoundary(Dart d)
{
Sylvain Thery's avatar
Sylvain Thery committed
857
858
859
860
861
862
863
864
865
866
867
//	assert(phi2(d) == d);	// Only work on boundary dart
//	Dart dPhi1;
//	Dart dNext = d;
//	do
//	{						// Loop inside the vertex
//		dPhi1 = phi1(dNext);
//		dNext = phi2(dPhi1);
//	} while (dNext != dPhi1);
//	return dNext;

	assert( isBoundaryMarked(phi2(d)) );	// Only work on boundary dart
Pierre Kraemer's avatar
Pierre Kraemer committed
868
869
870
871
872
873
	Dart dPhi1;
	Dart dNext = d;
	do
	{						// Loop inside the vertex
		dPhi1 = phi1(dNext);
		dNext = phi2(dPhi1);
Sylvain Thery's avatar
Sylvain Thery committed
874
875
	} while (! isBoundaryMarked(dNext));
	return dPhi1;
Pierre Kraemer's avatar
Pierre Kraemer committed
876
877
878
879
880
881
882
883
884
}

bool Map2::isTriangular()
{
	DartMarker m(*this) ;
	for(Dart d = begin(); d != end(); next(d))
	{
		if(!m.isMarked(d))
		{
885
			m.markOrbit(FACE, d) ;
Sylvain Thery's avatar
Sylvain Thery committed
886
			if (!isBoundaryMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
887
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
888
				if(!isFaceTriangle(d))
Sylvain Thery's avatar
Sylvain Thery committed
889
					return false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
890
891
892
893
894
895
896
897
			}
		}
	}
	return true ;
}

bool Map2::check()
{
898
	CGoGNout << "Check: topology begin" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
899
	DartMarker m(*this);
Pierre Kraemer's avatar
Pierre Kraemer committed
900
	for(Dart d = Map2::begin(); d != Map2::end(); Map2::next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
901
902
903
904
	{
		Dart d2 = phi2(d);
		if (phi2(d2) != d)	// phi2 involution ?
		{
905
			CGoGNout << "Check: phi2 is not an involution" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
906
907
			return false;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
908
909
910
911
912
		if(d2 == d)
		{
			CGoGNout << "Check: phi2 fixed point" << CGoGNendl;
			return false;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
913
914
915
916

		Dart d1 = phi1(d);
		if (phi_1(d1) != d)	// phi1 a une image correcte ?
		{
917
			CGoGNout << "Check: inconsistent phi_1 link" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
918
919
920
921
922
			return false;
		}

		if (m.isMarked(d1))	// phi1 a un seul antécédent ?
		{
923
			CGoGNout << "Check: dart with two phi1 predecessors" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
924
925
926
927
928
			return false;
		}
		m.mark(d1);

		if (d1 == d)
929
			CGoGNout << "Check: (warning) face loop (one edge)" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
930
		if (phi1(d1) == d)
931
			CGoGNout << "Check: (warning) face with only two edges" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
932
		if (phi2(d1) == d)
933
			CGoGNout << "Check: (warning) dangling edge" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
934
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
935
	for(Dart d = Map2::begin(); d != Map2::end(); Map2::next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
936
937
938
	{
		if (!m.isMarked(d))	// phi1 a au moins un antécédent ?
		{
939
			CGoGNout << "Check: dart with no phi1 predecessor" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
940
941
942
			return false;
		}
	}
943
	CGoGNout << "Check: topology ok" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
944
945
946
947
948
949
950
	return true;
}

/*! @name Cell Functors
 *  Apply functors to all darts of a cell
 *************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
951
bool Map2::foreach_dart_of_vertex(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
952
953
954
955
956
957
958
959
960
{
	Dart dNext = d;
	do
	{
		if (f(dNext))
			return true;
		dNext = alpha1(dNext);
 	} while (dNext != d);
 	return false;
Sylvain Thery's avatar
Sylvain Thery committed
961

Pierre Kraemer's avatar
Pierre Kraemer committed
962
963
}

Sylvain Thery's avatar
Sylvain Thery committed
964
bool Map2::foreach_dart_of_edge(Dart d, FunctorType& fonct, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
965
{
Sylvain Thery's avatar
Sylvain Thery committed
966
967
968
969
970
971
972
973
974
//	if (fonct(d))
//		return true;
//
//	Dart d2 = phi2(d);
//	if (d2 != d)
//		return fonct(d2);
//	else
//		return false;

Pierre Kraemer's avatar
Pierre Kraemer committed
975
976
	if (fonct(d))
		return true;
Sylvain Thery's avatar
Sylvain Thery committed
977
	return fonct(phi2(d));
Pierre Kraemer's avatar
Pierre Kraemer committed
978
979
}

Sylvain Thery's avatar
Sylvain Thery committed
980
bool Map2::foreach_dart_of_oriented_volume(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
981
{
Sylvain Thery's avatar
Sylvain Thery committed
982
	DartMarkerStore mark(*this,thread);	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
	bool found = false;				// Last functor return value

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

	// For every face added to the list
	for (face = visitedFaces.begin(); !found && face != visitedFaces.end(); ++face)
	{
		if (!mark.isMarked(*face))		// Face has not been visited yet
		{
			// Apply functor to the darts of the face
			found = foreach_dart_of_oriented_face(*face, f);

			// If functor returns false then mark visited darts (current face)
			// and add non visited adjacent faces to the list of face
			if (!found)
			{
				Dart dNext = *face ;
				do
				{
					mark.mark(dNext);					// Mark
					Dart adj = phi2(dNext);				// Get adjacent face
					if (adj != dNext && !mark.isMarked(adj))
						visitedFaces.push_back(adj);	// Add it
					dNext = phi1(dNext);
				} while(dNext != *face);
			}
		}
	}
	return found;
}

Sylvain Thery's avatar
Sylvain Thery committed
1016
bool Map2::foreach_dart_of_star(Dart d, unsigned int orbit, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
1017
{
1018
	if(orbit == VERTEX)
Pierre Kraemer's avatar
Pierre Kraemer committed
1019
1020
1021
1022
1023
	{

		Dart dNext = d;
		do
		{
Sylvain Thery's avatar
Sylvain Thery committed
1024
			if(Map1::foreach_dart_of_face(dNext,f,thread))
Pierre Kraemer's avatar
Pierre Kraemer committed
1025
1026
1027
1028
1029
1030
1031
				return true;

			dNext = alpha1(dNext);
		} while (dNext != d);

		return false;
	}
1032
	else if(orbit == FACE)
Pierre Kraemer's avatar
Pierre Kraemer committed
1033
	{
Sylvain Thery's avatar
Sylvain Thery committed
1034
		if(Map1::foreach_dart_of_face(d,f,thread))
Pierre Kraemer's avatar
Pierre Kraemer committed
1035
1036
1037
			return true;

		if(phi2(d) != d)
Sylvain Thery's avatar
Sylvain Thery committed
1038
			return Map1::foreach_dart_of_face(phi2(d),f,thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
1039
1040
1041
1042
1043
1044
1045
		else
			return false;
	}

	return false;
}

Sylvain Thery's avatar
Sylvain Thery committed
1046
bool Map2::foreach_dart_of_link(Dart d, unsigned int orbit, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
1047
{
1048
	if(orbit == VERTEX)
Pierre Kraemer's avatar
Pierre Kraemer committed
1049
1050
1051
1052
	{
		Dart dNext = d;
		do
		{
Sylvain Thery's avatar
Sylvain Thery committed
1053
			if(Map2::foreach_dart_of_edge(phi1(dNext),f,thread))
Pierre Kraemer's avatar
Pierre Kraemer committed
1054
1055
1056
1057
1058
1059
1060
				return true;

			dNext = alpha1(dNext);
		} while (dNext != d);

		return false;
	}
1061
	else if(orbit == FACE)
Pierre Kraemer's avatar
Pierre Kraemer committed
1062
	{
Sylvain Thery's avatar
Sylvain Thery committed
1063
		if(Map2::foreach_dart_of_vertex(phi_1(d),f,thread))
Pierre Kraemer's avatar
Pierre Kraemer committed
1064
1065
			return true;

Sylvain Thery's avatar
Sylvain Thery committed
1066
		if(Map2::foreach_dart_of_vertex(phi_1(phi2(d)),f,thread))
Pierre Kraemer's avatar
Pierre Kraemer committed
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
			return true;

		return false;
	}

	return false;
}


} // namespace CGoGN