map3.cpp 18.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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Topology/map/map3.h"

namespace CGoGN
{

Pierre Kraemer's avatar
merges    
Pierre Kraemer committed
30
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
void Map3::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_phi1->operator [](i);
			Dart e = Dart(oldnew[d.index]);
			if (d != e)
				d = e;
		}
		{
			Dart& d = m_phi_1->operator [](i);
			Dart e = Dart(oldnew[d.index]);
			if (d != e)
				d = e;
		}
		{
			Dart& d = m_phi2->operator [](i);
			Dart e = Dart(oldnew[d.index]);
			if (d != e)
				d = e;
		}
		{
			Dart& d = m_phi3->operator [](i);
			Dart e = Dart(oldnew[d.index]);
			if (d != e)
				d = e;
		}
	}
}

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

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

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

73
	mark.markOrbit(ORIENTED_FACE, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
74

75
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
76
	{
77
		Dart e = visitedFaces[i] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
78

79
80
		if(!isBoundaryFace(e))
			unsewVolumes(e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
81
82
83
84
85
86
87

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

untereiner's avatar
untereiner committed
94
95
96
	Dart dd = phi3(d) ;
	Map2::deleteCC(d) ;
	Map2::deleteCC(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
97
98
}

99
100
void Map3::fillHole(Dart d)
{
101
102
103
104
105
	assert(isBoundaryFace(d)) ;
	Dart dd = d ;
	if(!isBoundaryMarked(dd))
		dd = phi3(dd) ;
	boundaryUnmarkOrbit(VOLUME, dd) ;
106
107
}

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

112
Dart Map3::deleteVertex(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
113
{
114
115
116
	if(isBoundaryVertex(d))
		return NIL ;

Pierre Kraemer's avatar
Pierre Kraemer committed
117
118
	// Save the darts around the vertex
	// (one dart per face should be enough)
untereiner's avatar
untereiner committed
119
	std::vector<Dart> fstoretmp;
120
	fstoretmp.reserve(128);
untereiner's avatar
untereiner committed
121
122
123
	FunctorStore fs(fstoretmp);
	foreach_dart_of_vertex(d, fs);

Pierre Kraemer's avatar
Pierre Kraemer committed
124
	// just one dart per face
untereiner's avatar
untereiner committed
125
	std::vector<Dart> fstore;
126
	fstore.reserve(128);
untereiner's avatar
untereiner committed
127
	DartMarker mf(*this);
128
	for(unsigned int i = 0; i < fstoretmp.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
129
	{
130
		if(!mf.isMarked(fstoretmp[i]))
131
		{
132
133
			mf.markOrbit(FACE, fstoretmp[i]);
			fstore.push_back(fstoretmp[i]);
untereiner's avatar
untereiner committed
134
		}
135
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
136

137
	Dart res = NIL ;
untereiner's avatar
untereiner committed
138
139
	for(std::vector<Dart>::iterator it = fstore.begin() ; it != fstore.end() ; ++it)
	{
140
141
142
143
144
145
146
147
		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) ;
148

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

152
153
154
155
			phi2unsew(d2) ;
			phi2unsew(d32) ;
			phi2sew(d2, d32) ;
			phi2sew(fit, d3) ;
156
157

			fit = phi1(fit) ;
158
		}
untereiner's avatar
untereiner committed
159
	}
160

161
	Map2::deleteCC(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
162

163
	return res ;
Pierre Kraemer's avatar
Pierre Kraemer committed
164
165
}

David Cazier's avatar
David Cazier committed
166
Dart Map3::cutEdge(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
169
{
	Dart prev = d;
	Dart dd = alpha2(d);
David Cazier's avatar
David Cazier committed
170
	Dart nd = Map2::cutEdge(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
171

172
	while (dd != d)
Pierre Kraemer's avatar
Pierre Kraemer committed
173
174
175
176
177
178
	{
		prev = dd;
		dd = alpha2(dd);

		Map2::cutEdge(prev);

179
180
181
182
		Dart d3 = phi3(prev);
		phi3unsew(prev);
		phi3sew(prev, phi1(d3));
		phi3sew(d3, phi1(prev));
Pierre Kraemer's avatar
Pierre Kraemer committed
183
184
	}

185
186
187
188
	Dart d3 = phi3(d);
	phi3unsew(d);
	phi3sew(d, phi1(d3));
	phi3sew(d3, phi1(d));
Pierre Kraemer's avatar
Pierre Kraemer committed
189

David Cazier's avatar
David Cazier committed
190
	return nd;
Pierre Kraemer's avatar
Pierre Kraemer committed
191
192
}

Pierre Kraemer's avatar
Pierre Kraemer committed
193
bool Map3::uncutEdge(Dart d)
untereiner's avatar
untereiner committed
194
{
Pierre Kraemer's avatar
Pierre Kraemer committed
195
196
	if(vertexDegree(phi1(d)) == 2)
	{
197
198
		Dart prev = d ;
		phi3unsew(phi1(prev)) ;
untereiner's avatar
untereiner committed
199

200
201
		Dart dd = d;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
202
203
204
		{
			prev = dd;
			dd = alpha2(dd);
untereiner's avatar
untereiner committed
205

206
207
			phi3unsew(phi2(prev)) ;
			phi3unsew(phi2(phi1(prev))) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
208
209
			Map2::uncutEdge(prev);
			phi3sew(dd, phi2(prev));
210
211
		} while (dd != d) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
212
		return true;
untereiner's avatar
untereiner committed
213
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
214
	return false;
untereiner's avatar
untereiner committed
215
216
}

untereiner's avatar
untereiner committed
217
218
219
220
221
222
Dart Map3::deleteEdge(Dart d)
{
	if(isBoundaryEdge(d))
		return NIL ;

	Dart res = NIL ;
223
224
	Dart dit = d ;
	do
untereiner's avatar
untereiner committed
225
	{
226
227
		Dart fit = dit ;
		Dart end = fit ;
untereiner's avatar
untereiner committed
228
229
230
231
232
233
		fit = phi1(fit) ;
		while(fit != end)
		{
			Dart d2 = phi2(fit) ;
			Dart d3 = phi3(fit) ;
			Dart d32 = phi2(d3) ;
234

untereiner's avatar
untereiner committed
235
236
			if(res == NIL)
				res = d2 ;
237

untereiner's avatar
untereiner committed
238
239
240
241
			phi2unsew(d2) ;
			phi2unsew(d32) ;
			phi2sew(d2, d32) ;
			phi2sew(fit, d3) ;
242
243

			fit = phi1(fit) ;
untereiner's avatar
untereiner committed
244
		}
245
246
247
		dit = alpha2(dit) ;
	} while(dit != d) ;

untereiner's avatar
untereiner committed
248
249
250
251
252
253
254
255
256
	Map2::deleteCC(d) ;

	return res ;
}

Dart Map3::collapseEdge(Dart d, bool delDegenerateVolumes)
{
	Dart resV = NIL;

257
	Dart dit = d;
untereiner's avatar
untereiner committed
258
259
260

	do
	{
261
262
		Dart e = dit;
		dit = alpha2(dit);
untereiner's avatar
untereiner committed
263

264
265
266
		//test si un seul polyedre autour de l'arete
		if(e == dit)
			resV == phi3(phi2(phi1(e)));
untereiner's avatar
untereiner committed
267

268
		if(delDegenerateVolumes)
untereiner's avatar
untereiner committed
269
		{
270
271
272
273
274
			Map2::collapseEdge(e, true);
			//collapseDegeneretedVolume(e);
		}
		else
			Map2::collapseEdge(e, false);
untereiner's avatar
untereiner committed
275

276
277
		if(resV == NIL)
		{
untereiner's avatar
untereiner committed
278

279
		}
untereiner's avatar
untereiner committed
280
281


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
	}while(d != dit);





//	Dart e = d;
//
//	// stocke un brin par volume autour de l'arete
//	std::vector<Dart> tmp;
//	tmp.reserve(32);
//	do
//	{
//		tmp.push_back(e);
//		e = alpha2(e);
//	} while (e != d);
//
//	// contraction de la 2 carte de chaque 2-arete
//	for (std::vector<Dart>::iterator it = tmp.begin(); it != tmp.end(); ++it)
//	{
//		// un brin d'une face adjacente a l'arrete contracte
//		Dart d = phi2(phi_1(*it));
//		Map2::collapseEdge(*it, true);
//
//		// test de la degeneresence
//		// impossible d'avoir un volume de moins de 4 faces sans avoir de phi2 en points fixe donc on les vire
//		if(delDegenerateVolumes && Map2::volumeDegree(d) < 4)
//		{
//			Dart e = d;
//			// pour tous les brins de la face adjacente
//
//			do
//			{
//				Dart ee = phi3(e);
//				Dart ff = phi3(phi2(e));
//
//				// si les brins ont un voisin par phi3
//				if(ee != e)
//
//					phi3unsew(ee);
//				if(ff != phi2(e))
//					phi3unsew(ff);
//
//				// si les deux en ont un, il faut les coudres ensemble
//				if(ee != e && ff != phi2(e))
//					phi3sew(ee, ff);
//
//				// on peut supprimer les brins de cette arete
//				deleteDart(e);
//				deleteDart(phi2(e));
//				e = phi1(e);
//
//			} while (e != d);
//		}
//	}
untereiner's avatar
untereiner committed
337
338
339
340

	return resV;
}

341
void Map3::splitFace(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
342
{
343
	assert(d != e && Map2::sameOrientedFace(d, e)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
344

345
346
	Dart dd = phi1(phi3(d));
	Dart ee = phi1(phi3(e));
Pierre Kraemer's avatar
Pierre Kraemer committed
347

untereiner's avatar
untereiner committed
348
	Map2::splitFace(d, e);
349
	Map2::splitFace(dd, ee);
Pierre Kraemer's avatar
Pierre Kraemer committed
350

351
352
	phi3sew(phi_1(d), phi_1(ee));
	phi3sew(phi_1(e), phi_1(dd));
Pierre Kraemer's avatar
Pierre Kraemer committed
353
354
}

355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
bool Map3::collapseDegeneretedVolume(Dart d)
{
	Dart e1 = phi2(d);
	Dart e2 = phi2(phi1(d));

	//Si l'une des faces est du bord
	if(isBoundaryFace(e1) || isBoundaryFace(e2))
	{
		//alors simple suppression du volume degenere
	}
	else
	{
		Dart e13 = e1;
		Dart e23 = e2;
		if(!isBoundaryFace(e1))
			e13 = phi3(e1);

		if(!isBoundaryFace(e2))
			e23 = phi3(e2);

		//if(faceDegree(e1) < faceDegree)
	}
377
378

	return false;
379
380
}

381
void Map3::sewVolumes(Dart d, Dart e, bool withBoundary)
382
383
384
{
	assert(faceDegree(d) == faceDegree(e));

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
413
414
415
	// if sewing with fixed points
	if (!withBoundary)
	{
		assert(phi3(d) == d && phi3(e) == e) ;
		Dart fitD = d ;
		Dart fitE = e ;
		do
		{
			phi3sew(fitD, fitE) ;
			fitD = phi1(fitD) ;
			fitE = phi_1(fitE) ;
		} while(fitD != d) ;
		return ;
	}

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

	Dart fitD = dd ;
	Dart fitE = ee ;
	do
	{
		Dart fitD2 = phi2(fitD) ;
		Dart fitE2 = phi2(fitE) ;
		if(fitD2 != fitE)
		{
			phi2unsew(fitD) ;
			phi2unsew(fitE) ;
			phi2sew(fitD2, fitE2) ;
			phi2sew(fitD, fitE) ;
		}
416
417
		phi3unsew(fitD) ;
		phi3unsew(fitE) ;
418
419
		fitD = phi1(fitD) ;
		fitE = phi_1(fitE) ;
420
	} while(fitD != dd) ;
421
422
	Map2::deleteCC(dd) ;

423
424
	fitD = d ;
	fitE = e ;
425
426
	do
	{
427
		phi3sew(fitD, fitE) ;
428
429
430
431
432
433
434
		fitD = phi1(fitD) ;
		fitE = phi_1(fitE) ;
	} while(fitD != d) ;
}

void Map3::unsewVolumes(Dart d)
{
untereiner's avatar
untereiner committed
435
436
437
438
439
440
441
442
443
444
445
446
	assert(!isBoundaryFace(d)) ;

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

	Dart b1 = newBoundaryCycle(nbE) ;
	Dart b2 = newBoundaryCycle(nbE) ;

	Dart fit1 = d ;
	Dart fit2 = d3 ;
	Dart fitB1 = b1 ;
	Dart fitB2 = b2 ;
447
448
	do
	{
untereiner's avatar
untereiner committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
		Dart f = findBoundaryFaceOfEdge(fit1) ;
		if(f != NIL)
		{
			Dart f2 = phi2(f) ;
			phi2unsew(f) ;
			phi2sew(fitB1, f) ;
			phi2sew(fitB2, f2) ;
		}
		else
			phi2sew(fitB1, fitB2) ;

		phi3unsew(fit1) ;
		phi3sew(fit1, fitB1) ;
		phi3sew(fit2, fitB2) ;

		fit1 = phi1(fit1) ;
		fit2 = phi_1(fit2) ;
		fitB1 = phi_1(fitB1) ;
		fitB2 = phi1(fitB2) ;
	} while(fitB1 != b1) ;
469
470
471
472
}

bool Map3::mergeVolumes(Dart d)
{
untereiner's avatar
untereiner committed
473
	if(!Map3::isBoundaryFace(d))
474
	{
untereiner's avatar
untereiner committed
475
		Map2::mergeVolumes(d, phi3(d)); // merge the two volumes along common face
476
477
478
479
480
		return true ;
	}
	return false ;
}

untereiner's avatar
untereiner committed
481
482
void Map3::splitVolume(std::vector<Dart>& vd)
{
untereiner's avatar
untereiner committed
483
484
	assert(checkSimpleOrientedPath(vd)) ;

untereiner's avatar
untereiner committed
485
486
487
488
489
490
491
	Dart e = vd.front();
	Dart e2 = phi2(e);

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

untereiner's avatar
untereiner committed
492
493
	Map2::fillHole(e) ;
	Map2::fillHole(e2) ;
untereiner's avatar
untereiner committed
494

untereiner's avatar
untereiner committed
495
496
	//sew the two connected components
	Map3::sewVolumes(phi2(e), phi2(e2), false);
untereiner's avatar
untereiner committed
497
498
}

499
500
501
502
/*! @name Topological Queries
 *  Return or set various topological information
 *************************************************************************/

503
bool Map3::sameVertex(Dart d, Dart e)
504
505
506
{
	DartMarkerStore mv(*this);	// Lock a marker

untereiner's avatar
untereiner committed
507
508
509
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
510
511
	mv.mark(d);

512
	for(unsigned int i = 0; i < darts.size(); ++i)
513
	{
514
		if(darts[i] == e)
515
516
			return true;

Pierre Kraemer's avatar
Pierre Kraemer committed
517
		// add phi21 and phi23 successor if they are not marked yet
518
		Dart d2 = phi2(darts[i]);
untereiner's avatar
untereiner committed
519
520
		Dart d21 = phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume
521

untereiner's avatar
untereiner committed
522
523
524
525
526
527
528
529
530
		if(!mv.isMarked(d21))
		{
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
531
532
533
534
535
		}
	}
	return false;
}

untereiner's avatar
untereiner committed
536
537
unsigned int Map3::vertexDegree(Dart d)
{
untereiner's avatar
untereiner committed
538
	unsigned int count = 0;
untereiner's avatar
untereiner committed
539
540
	DartMarkerStore mv(*this);	// Lock a marker

untereiner's avatar
untereiner committed
541
542
543
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
untereiner's avatar
untereiner committed
544
545
	mv.mark(d);

546
	for(unsigned int i = 0; i < darts.size(); ++i)
untereiner's avatar
untereiner committed
547
548
	{
		//add phi21 and phi23 successor if they are not marked yet
549
		Dart d2 = phi2(darts[i]);
untereiner's avatar
untereiner committed
550
551
552
553
554
		Dart d21 = phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume

		if(!mv.isMarked(d21))
		{
untereiner's avatar
untereiner committed
555
			darts.push_back(d21);
untereiner's avatar
untereiner committed
556
557
			mv.mark(d21);
		}
untereiner's avatar
untereiner committed
558
		if(!mv.isMarked(d23))
untereiner's avatar
untereiner committed
559
		{
untereiner's avatar
untereiner committed
560
			darts.push_back(d23);
untereiner's avatar
untereiner committed
561
562
563
564
565
			mv.mark(d23);
		}
	}

	DartMarkerStore me(*this);
untereiner's avatar
untereiner committed
566
	for(std::vector<Dart>::iterator it = darts.begin(); it != darts.end() ; ++it)
untereiner's avatar
untereiner committed
567
	{
untereiner's avatar
untereiner committed
568
		if(!me.isMarked(*it))
untereiner's avatar
untereiner committed
569
570
		{
			++count;
untereiner's avatar
untereiner committed
571
			me.markOrbit(EDGE, *it);
untereiner's avatar
untereiner committed
572
573
574
575
576
577
		}
	}

	return count;
}

578
579
bool Map3::isBoundaryVertex(Dart d)
{
untereiner's avatar
untereiner committed
580
	DartMarkerStore mv(*this);	// Lock a marker
581

untereiner's avatar
untereiner committed
582
583
584
	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
585
586
	mv.mark(d);

587
	for(unsigned int i = 0; i < darts.size(); ++i)
588
	{
589
		if(isBoundaryMarked(darts[i]))
untereiner's avatar
untereiner committed
590
			return true ;
591
592

		//add phi21 and phi23 successor if they are not marked yet
593
		Dart d2 = phi2(darts[i]);
594
595
596
597
598
		Dart d21 = phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume

		if(!mv.isMarked(d21))
		{
untereiner's avatar
untereiner committed
599
			darts.push_back(d21);
600
601
			mv.mark(d21);
		}
untereiner's avatar
untereiner committed
602
		if(!mv.isMarked(d23))
603
		{
untereiner's avatar
untereiner committed
604
			darts.push_back(d23);
605
606
607
			mv.mark(d23);
		}
	}
untereiner's avatar
untereiner committed
608
	return false ;
609
610
611
612
}

bool Map3::sameOrientedEdge(Dart d, Dart e)
{
untereiner's avatar
untereiner committed
613
	Dart it = d;
614
615
	do
	{
untereiner's avatar
untereiner committed
616
		if(it == e)
617
			return true;
untereiner's avatar
untereiner committed
618
619
		it = alpha2(it);
	} while (it != d);
620
621
622
	return false;
}

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

untereiner's avatar
untereiner committed
635
bool Map3::isBoundaryEdge(Dart d)
636
{
untereiner's avatar
untereiner committed
637
638
639
640
641
642
643
644
	Dart it = d;
	do
	{
		if(isBoundaryMarked(it))
			return true ;
		it = alpha2(it);
	} while(it != d);
	return false;
645
646
}

untereiner's avatar
untereiner committed
647
Dart Map3::findBoundaryFaceOfEdge(Dart d)
648
{
untereiner's avatar
untereiner committed
649
650
651
652
653
654
655
656
	Dart it = d;
	do
	{
		if (isBoundaryMarked(it))
			return it ;
		it = alpha2(it);
	} while(it != d);
	return NIL ;
657
658
}

Pierre Kraemer's avatar
Pierre Kraemer committed
659
660
bool Map3::isBoundaryVolume(Dart d)
{
untereiner's avatar
untereiner committed
661
	DartMarkerStore mark(*this);	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
662
663

	std::vector<Dart> visitedFaces ;
untereiner's avatar
untereiner committed
664
	visitedFaces.reserve(128) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
665
	visitedFaces.push_back(d) ;
untereiner's avatar
untereiner committed
666
	mark.markOrbit(ORIENTED_FACE, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
667

668
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
669
	{
670
		if (isBoundaryMarked(phi3(visitedFaces[i])))
untereiner's avatar
untereiner committed
671
			return true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
672

673
		Dart e = visitedFaces[i] ;
untereiner's avatar
untereiner committed
674
		do	// add all face neighbours to the table
Pierre Kraemer's avatar
Pierre Kraemer committed
675
		{
untereiner's avatar
untereiner committed
676
677
			Dart ee = phi2(e) ;
			if(!mark.isMarked(ee)) // not already marked
Pierre Kraemer's avatar
Pierre Kraemer committed
678
			{
untereiner's avatar
untereiner committed
679
680
681
682
				visitedFaces.push_back(ee) ;
				mark.markOrbit(ORIENTED_FACE, ee) ;
			}
			e = phi1(e) ;
683
		} while(e != visitedFaces[i]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
684
	}
untereiner's avatar
untereiner committed
685
	return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
686
687
}

688
bool Map3::check()
689
{
690
691
692
693
694
695
696
697
698
699
    std::cout << "Check: topology begin" << std::endl;
    DartMarker m(*this);
    for(Dart d = Map3::begin(); d != Map3::end(); Map3::next(d))
    {
        Dart d3 = phi3(d);
        if (phi3(d3) != d) // phi3 involution ?
		{
            std::cout << "Check: phi3 is not an involution" << std::endl;
            return false;
        }
700

untereiner's avatar
untereiner committed
701
702
703
704
705
		if(phi1(d3) != phi3(phi_1(d)))
		{
			std::cout << "Check: phi3 , faces are not entirely sewn" << std::endl;
			return false;
		}
706

707
708
709
710
711
712
        Dart d2 = phi2(d);
        if (phi2(d2) != d) // phi2 involution ?
		{
            std::cout << "Check: phi2 is not an involution" << std::endl;
            return false;
        }
713

714
715
716
717
718
719
        Dart d1 = phi1(d);
        if (phi_1(d1) != d) // phi1 a une image correcte ?
		{
            std::cout << "Check: unconsistent phi_1 link" << std::endl;
            return false;
        }
720

721
        if (m.isMarked(d1)) // phi1 a un seul antécédent ?
722
		{
723
724
725
726
            std::cout << "Check: dart with two phi1 predecessors" << std::endl;
            return false;
        }
        m.mark(d1);
727

728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
        if (d1 == d)
            std::cout << "Check: (warning) face loop (one edge)" << std::endl;

        if (phi1(d1) == d)
            std::cout << "Check: (warning) face with only two edges" << std::endl;

        if (phi2(d1) == d)
            std::cout << "Check: (warning) dandling edge (phi2)" << std::endl;

        if (phi3(d1) == d)
            std::cout << "Check: (warning) dandling edge (phi3)" << std::endl;
    }

    for(Dart d = this->begin(); d != this->end(); this->next(d))
    {
        if (!m.isMarked(d)) // phi1 a au moins un antécédent ?
744
		{
745
746
747
748
            std::cout << "Check: dart with no phi1 predecessor" << std::endl;
            return false;
        }
    }
749

750
    std::cout << "Check: topology ok" << std::endl;
751

752
    return true;
753
754
}

Pierre Kraemer's avatar
Pierre Kraemer committed
755
756
757
758
/*! @name Cell Functors
 *  Apply functors to all darts of a cell
 *************************************************************************/

untereiner's avatar
untereiner committed
759
bool Map3::foreach_dart_of_vertex(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
760
{
untereiner's avatar
untereiner committed
761
	DartMarkerStore mv(*this, thread);	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
762
763
	bool found = false;					// Last functor return value

untereiner's avatar
untereiner committed
764
765
766
	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
767
768
	mv.mark(d);

769
	for(unsigned int i = 0; !found && i < darts.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
770
	{
771
		// add phi21 and phi23 successor if they are not marked yet
772
		Dart d2 = phi2(darts[i]);
untereiner's avatar
untereiner committed
773
774
		Dart d21 = phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume
untereiner's avatar
untereiner committed
775

untereiner's avatar
untereiner committed
776
777
778
779
780
781
782
783
784
		if(!mv.isMarked(d21))
		{
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
Pierre Kraemer's avatar
Pierre Kraemer committed
785
786
		}

787
		found = f(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
788
789
790
791
	}
	return found;
}

Sylvain Thery's avatar
Sylvain Thery committed
792
bool Map3::foreach_dart_of_edge(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
793
{
untereiner's avatar
untereiner committed
794
	Dart it = d;
Pierre Kraemer's avatar
Pierre Kraemer committed
795
796
	do
	{
untereiner's avatar
untereiner committed
797
		if (Map2::foreach_dart_of_edge(it, f, thread))
Pierre Kraemer's avatar
Pierre Kraemer committed
798
			return true;
untereiner's avatar
untereiner committed
799
800
		it = alpha2(it);
	} while (it != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
801
802
803
	return false;
}

untereiner's avatar
untereiner committed
804
bool Map3::foreach_dart_of_cc(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
805
{
Sylvain Thery's avatar
Sylvain Thery committed
806
	DartMarkerStore mv(*this,thread);	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
807
808
	bool found = false;					// Last functor return value

untereiner's avatar
untereiner committed
809
810
811
	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
812
813
	mv.mark(d);

814
	for(unsigned int i = 0; !found && i < darts.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
815
	{
untereiner's avatar
untereiner committed
816
		// add all successors if they are not marked yet
817
818
819
		Dart d2 = phi1(darts[i]); // turn in face
		Dart d3 = phi2(darts[i]); // change face
		Dart d4 = phi3(darts[i]); // change volume
Pierre Kraemer's avatar
Pierre Kraemer committed
820

untereiner's avatar
untereiner committed
821
822
823
824
825
		if (!mv.isMarked(d2))
		{
			darts.push_back(d2);
			mv.mark(d2);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
826
827
		if (!mv.isMarked(d3))
		{
untereiner's avatar
untereiner committed
828
829
			darts.push_back(d2);
			mv.mark(d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
830
831
832
		}
		if (!mv.isMarked(d4))
		{
untereiner's avatar
untereiner committed
833
			darts.push_back(d4);
Pierre Kraemer's avatar
Pierre Kraemer committed
834
835
836
			mv.mark(d4);
		}

837
		found = f(darts[i]);
Pierre Kraemer's avatar
Pierre Kraemer committed
838
839
840
841
	}
	return found;
}

untereiner's avatar
untereiner committed
842
843
844
/*! @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
845

untereiner's avatar
untereiner committed
846
unsigned int Map3::closeHole(Dart d, bool forboundary)
Pierre Kraemer's avatar
Pierre Kraemer committed
847
{
untereiner's avatar
untereiner committed
848
	assert(phi3(d) == d);		// Nothing to close
849
	DartMarkerStore m(*this) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
850

untereiner's avatar
untereiner committed
851
852
853
	std::vector<Dart> visitedFaces;	// Faces that are traversed
	visitedFaces.reserve(1024) ;
	visitedFaces.push_back(d);		// Start with the face of d
854
	m.markOrbit(ORIENTED_FACE, d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
855

untereiner's avatar
untereiner committed
856
	unsigned int count = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
857

untereiner's avatar
untereiner committed
858
	// For every face added to the list
859
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
860
	{
861
862
863
		Dart it = visitedFaces[i] ;
		Dart f = it ;

untereiner's avatar
untereiner committed
864
865
866
		unsigned int degree = faceDegree(f) ;
		Dart b = newBoundaryCycle(degree) ;
		++count ;
Pierre Kraemer's avatar
Pierre Kraemer committed
867

untereiner's avatar
untereiner committed
868
869
		Dart bit = b ;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
870
		{
untereiner's avatar
untereiner committed
871
872
873
874
875
876
877
			Dart e = alpha2(f) ;
			bool found = false ;
			do
			{
				if(phi3(e) == e)
				{
					found = true ;
878
879
880
881
882
					if(!m.isMarked(e))
					{
						visitedFaces.push_back(e) ;
						m.markOrbit(ORIENTED_FACE, e) ;
					}
untereiner's avatar
untereiner committed
883
				}
884
				else if(isBoundaryMarked(e))
untereiner's avatar
untereiner committed
885
886
				{
					found = true ;
887
					phi2sew(e, bit) ;
untereiner's avatar
untereiner committed
888
889
890
891
892
893
894
895
				}
				else
					e = alpha2(e) ;
			} while(!found) ;

			phi3sew(f, bit) ;
			bit = phi_1(bit) ;
			f = phi1(f);
896
		} while(f != it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
897
898
	}

untereiner's avatar
untereiner committed
899
900
901
902
903
904
905
906
907
908
909
	return count ;
}

void Map3::closeMap()
{
	// Search the map for topological holes (fix points of phi3)
	for (Dart d = begin(); d != end(); next(d))
	{
		if (phi3(d) == d)
			closeHole(d);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
910
911
912
}

} // namespace CGoGN