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

25
26
#include "Topology/generic/traversor3.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
27
28
29
namespace CGoGN
{

Pierre Kraemer's avatar
Pierre Kraemer committed
30
31
template <typename MAP_IMPL>
inline void Map3<MAP_IMPL>::init()
Pierre Kraemer's avatar
Pierre Kraemer committed
32
{
Pierre Kraemer's avatar
Pierre Kraemer committed
33
	MAP_IMPL::addInvolution() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
34
35
}

Pierre Kraemer's avatar
Pierre Kraemer committed
36
37
template <typename MAP_IMPL>
inline Map3<MAP_IMPL>::Map3() : Map2<MAP_IMPL>()
38
39
40
41
{
	init() ;
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
48
49
template <typename MAP_IMPL>
inline unsigned int Map3<MAP_IMPL>::dimension() const
Pierre Kraemer's avatar
Pierre Kraemer committed
50
51
52
53
{
	return 3;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
54
55
template <typename MAP_IMPL>
inline void Map3<MAP_IMPL>::clear(bool removeAttrib)
56
{
Pierre Kraemer's avatar
Pierre Kraemer committed
57
	ParentMap::clear(removeAttrib) ;
Sylvain Thery's avatar
Sylvain Thery committed
58
59
	if (removeAttrib)
		init() ;
60
61
}

Pierre Kraemer's avatar
Pierre Kraemer committed
62
template <typename MAP_IMPL>
Pierre Kraemer's avatar
Pierre Kraemer committed
63
inline unsigned int Map3<MAP_IMPL>::getNbInvolutions() const
Sylvain Thery's avatar
Sylvain Thery committed
64
{
Pierre Kraemer's avatar
Pierre Kraemer committed
65
66
67
68
69
70
71
	return 1 + ParentMap::getNbInvolutions();
}

template <typename MAP_IMPL>
inline unsigned int Map3<MAP_IMPL>::getNbPermutations() const
{
	return ParentMap::getNbPermutations();
Sylvain Thery's avatar
Sylvain Thery committed
72
73
}

Pierre Kraemer's avatar
Pierre Kraemer committed
74
75
76
77
/*! @name Basic Topological Operators
 * Access and Modification
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
78
79
template <typename MAP_IMPL>
inline Dart Map3<MAP_IMPL>::phi3(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
80
{
Pierre Kraemer's avatar
Pierre Kraemer committed
81
	return MAP_IMPL::template getInvolution<1>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
82
83
}

Pierre Kraemer's avatar
Pierre Kraemer committed
84
template <typename MAP_IMPL>
Pierre Kraemer's avatar
Pierre Kraemer committed
85
template <int N>
Pierre Kraemer's avatar
Pierre Kraemer committed
86
inline Dart Map3<MAP_IMPL>::phi(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
87
88
89
90
91
92
{
	assert( (N >0) || !"negative parameters not allowed in template multi-phi");
	if (N<10)
	{
		switch(N)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
93
94
95
96
			case 1 : return this->phi1(d) ;
			case 2 : return this->phi2(d) ;
			case 3 : return phi3(d) ;
			default : assert(!"Wrong multi-phi relation value") ; return d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
97
98
99
100
		}
	}
	switch(N%10)
	{
101
102
		case 1 : return this->phi1(phi<N/10>(d)) ;
		case 2 : return this->phi2(phi<N/10>(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
103
104
		case 3 : return phi3(phi<N/10>(d)) ;
		default : assert(!"Wrong multi-phi relation value") ; return d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
105
106
107
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
108
109
template <typename MAP_IMPL>
inline Dart Map3<MAP_IMPL>::alpha0(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
110
111
112
113
{
	return phi3(d) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
114
115
template <typename MAP_IMPL>
inline Dart Map3<MAP_IMPL>::alpha1(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
116
{
Pierre Kraemer's avatar
Pierre Kraemer committed
117
	return phi3(this->phi_1(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
118
119
}

Pierre Kraemer's avatar
Pierre Kraemer committed
120
121
template <typename MAP_IMPL>
inline Dart Map3<MAP_IMPL>::alpha2(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
122
{
Pierre Kraemer's avatar
Pierre Kraemer committed
123
	return phi3(this->phi2(d));
Pierre Kraemer's avatar
Pierre Kraemer committed
124
125
}

Pierre Kraemer's avatar
Pierre Kraemer committed
126
127
template <typename MAP_IMPL>
inline Dart Map3<MAP_IMPL>::alpha_2(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
128
{
129
	return this->phi2(phi3(d));
Pierre Kraemer's avatar
Pierre Kraemer committed
130
131
}

Pierre Kraemer's avatar
Pierre Kraemer committed
132
133
template <typename MAP_IMPL>
inline void Map3<MAP_IMPL>::phi3sew(Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
134
{
Pierre Kraemer's avatar
Pierre Kraemer committed
135
	MAP_IMPL::template involutionSew<1>(d,e);
Pierre Kraemer's avatar
Pierre Kraemer committed
136
137
}

Pierre Kraemer's avatar
Pierre Kraemer committed
138
139
template <typename MAP_IMPL>
inline void Map3<MAP_IMPL>::phi3unsew(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
140
{
Pierre Kraemer's avatar
Pierre Kraemer committed
141
	MAP_IMPL::template involutionUnsew<1>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
142
143
}

144
145
/*! @name Generator and Deletor
 *  To generate or delete volumes in a 3-map
untereiner's avatar
untereiner committed
146
 *************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
147

Pierre Kraemer's avatar
Pierre Kraemer committed
148
149
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::deleteVolume(Dart d, bool withBoundary)
untereiner's avatar
untereiner committed
150
{
151
152
	if(withBoundary)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
153
		DartMarkerStore<MAP_IMPL> mark(*this);		// Lock a marker
154
155
156
157
158

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

Pierre Kraemer's avatar
Pierre Kraemer committed
159
		mark.template markOrbit<FACE2>(d) ;
160
161
162
163
164
165
166
167
168
169
170
171
172
173

		for(unsigned int i = 0; i < visitedFaces.size(); ++i)
		{
			Dart e = visitedFaces[i] ;

			if(!isBoundaryFace(e))
				unsewVolumes(e) ;

			do	// add all face neighbours to the table
			{
				Dart ee = this->phi2(e) ;
				if(!mark.isMarked(ee)) // not already marked
				{
					visitedFaces.push_back(ee) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
174
					mark.template markOrbit<FACE2>(ee) ;
175
176
177
178
179
180
181
182
183
184
185
186
187
				}
				e = this->phi1(e) ;
			} while(e != visitedFaces[i]) ;
		}

		Dart dd = phi3(d) ;
		ParentMap::deleteCC(d) ; //deleting the volume
		ParentMap::deleteCC(dd) ; //deleting its border (created from the unsew operation)

		return;
	}

	//else remove the CC and create fixed points
Pierre Kraemer's avatar
Pierre Kraemer committed
188
	DartMarkerStore<MAP_IMPL> mark(*this);		// Lock a marker
189
190
191
192
193

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

Pierre Kraemer's avatar
Pierre Kraemer committed
194
	mark.template markOrbit<FACE2>(d) ;
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
	{
		Dart e = visitedFaces[i] ;

		Dart it = e ;
		do
		{
			phi3unsew(it);
			it = this->phi1(it) ;
		} while(it != e) ;

		do	// add all face neighbours to the table
		{
			Dart ee = this->phi2(e) ;
			if(!mark.isMarked(ee)) // not already marked
			{
				visitedFaces.push_back(ee) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
213
				mark.template markOrbit<FACE2>(ee) ;
214
215
216
217
218
219
			}
			e = this->phi1(e) ;
		} while(e != visitedFaces[i]) ;
	}

	ParentMap::deleteCC(d) ; //deleting the volume
untereiner's avatar
untereiner committed
220
221
}

Pierre Kraemer's avatar
Pierre Kraemer committed
222
223
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::fillHole(Dart d)
untereiner's avatar
untereiner committed
224
{
225
226
	assert(isBoundaryFace(d)) ;
	Dart dd = d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
227
	if(!this->template isBoundaryMarked<3>(dd))
228
229
		dd = phi3(dd) ;
	this->template boundaryUnmarkOrbit<VOLUME,3>(dd) ;
untereiner's avatar
untereiner committed
230
}
Pierre Kraemer's avatar
Pierre Kraemer committed
231

Pierre Kraemer's avatar
Pierre Kraemer committed
232
233
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::createHole(Dart d)
untereiner's avatar
untereiner committed
234
{
235
236
	assert(!isBoundaryFace(d)) ;
	this->template boundaryMarkOrbit<VOLUME,3>(d) ;
untereiner's avatar
untereiner committed
237
238
}

239
240
/*! @name Topological Operators
 *  Topological operations on 3-maps
untereiner's avatar
untereiner committed
241
242
 *************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
243
244
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::splitVertex(std::vector<Dart>& vd)
untereiner's avatar
untereiner committed
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
283
284
285
286
287
288
289
290
291
	//assert(checkPathAroundVertex(vd)) ;

	//bool boundE = false;

	Dart prev = vd.front();	//elt 0

	Dart db1 = NIL;
	if(isBoundaryFace(this->phi2(prev)))
		db1 = this->phi2(phi3(this->phi1(this->phi2(prev))));

	Dart fs = this->phi_1(this->phi2(this->phi_1(prev)));	//first side

	ParentMap::splitVertex(prev, this->phi2(fs));

	for(unsigned int i = 1; i < vd.size(); ++i)
	{
		prev = vd[i];

		Dart fs = this->phi_1(this->phi2(this->phi_1(prev)));	//first side

		ParentMap::splitVertex(prev, this->phi2(fs));

		Dart d1 = this->phi_1(this->phi2(this->phi_1(vd[i-1])));
		Dart d2 = this->phi1(this->phi2(vd[i]));

		phi3sew(d1, d2);
	}

	Dart db2 = NIL;
	if(isBoundaryFace(this->phi2(this->phi_1(prev))))
		db2 = this->phi2(phi3(this->phi2(this->phi_1(prev))));

	if(db1 != NIL && db2 != NIL)
	{
		ParentMap::splitVertex(db1, db2);
		phi3sew(this->phi1(this->phi2(db2)), this->phi_1(phi3(this->phi2(db2))));
		phi3sew(this->phi1(this->phi2(db1)), this->phi_1(phi3(this->phi2(db1))));
	}
	else
	{
		Dart dbegin = this->phi1(this->phi2(vd.front()));
		Dart dend = this->phi_1(this->phi2(this->phi_1(vd.back())));
		phi3sew(dbegin, dend);
	}

	return this->phi_1(this->phi2(this->phi_1(prev)));
292
293
}

Pierre Kraemer's avatar
Pierre Kraemer committed
294
295
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::deleteVertex(Dart d)
296
{
297
298
299
300
301
302
303
304
305
306
307
308
309
	//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);
Pierre Kraemer's avatar
Pierre Kraemer committed
310
	DartMarker<MAP_IMPL> mf(*this);
311
312
313
314
	for(unsigned int i = 0; i < fstoretmp.size(); ++i)
	{
		if(!mf.isMarked(fstoretmp[i]))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
315
			mf.template markOrbit<FACE>(fstoretmp[i]);
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
			fstore.push_back(fstoretmp[i]);
		}
	}

	std::cout << "nb faces " << fstore.size() << std::endl;

	Dart res = NIL ;
	for(std::vector<Dart>::iterator it = fstore.begin() ; it != fstore.end() ; ++it)
	{
		Dart fit = *it ;
		Dart end = this->phi_1(fit) ;
		fit = this->phi1(fit) ;

		if(fit == end)
		{
			std::cout << " mmmmmmmmmmmmmmmmmmmmmerrrrrrrrrrrrrrrrrde !!!!!!!!!!!! " << std::endl;

//			Dart d2 = phi2(fit) ;
//			Dart d23 = phi3(d2) ;
//			Dart d3 = phi3(fit) ;
//			Dart d32 = phi2(d3) ;
//
//			//phi3unsew()
//			phi3sew(d3,23);
//
//			fit = phi_1(fit);
//
//			d2 = phi2(fit) ;
//			d23 = phi3(d2) ;
//			d3 = phi3(fit) ;
//			d32 = phi2(d3) ;
//			phi3sew(d3,23);

//			Map2::deleteCC(fit);
		}
		else
		{
			while(fit != end)
			{
				Dart d2 = this->phi2(fit) ;
				Dart d3 = phi3(fit) ;
				Dart d32 = this->phi2(d3) ;

				if(res == NIL)
					res = d2 ;

				this->phi2unsew(d2) ;
				this->phi2unsew(d32) ;
				this->phi2sew(d2, d32) ;
				this->phi2sew(fit, d3) ;

				fit = this->phi1(fit) ;
			}
		}
	}

	ParentMap::deleteCC(d) ;

	return res ;
375
376
}

Pierre Kraemer's avatar
Pierre Kraemer committed
377
378
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::cutEdge(Dart d)
379
{
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
	Dart prev = d;
	Dart dd = this->alpha2(d);
	Dart nd = ParentMap::cutEdge(d);

	while (dd != d)
	{
		prev = dd;
		dd = alpha2(dd);

		ParentMap::cutEdge(prev);

		Dart d3 = phi3(prev);
		phi3unsew(prev);
		phi3sew(prev, this->phi1(d3));
		phi3sew(d3, this->phi1(prev));
	}

	Dart d3 = phi3(d);
	phi3unsew(d);
	phi3sew(d, this->phi1(d3));
	phi3sew(d3, this->phi1(d));

	return nd;
403
404
}

Pierre Kraemer's avatar
Pierre Kraemer committed
405
406
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::uncutEdge(Dart d)
407
{
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
	if(vertexDegree(this->phi1(d)) == 2)
	{
		Dart prev = d ;
		phi3unsew(this->phi1(prev)) ;

		Dart dd = d;
		do
		{
			prev = dd;
			dd = this->alpha2(dd);

			phi3unsew(this->phi2(prev)) ;
			phi3unsew(this->phi2(this->phi1(prev))) ;
			ParentMap::uncutEdge(prev);
			phi3sew(dd, this->phi2(prev));
		} while (dd != d) ;

		return true;
	}
	return false;
428
429
}

Pierre Kraemer's avatar
Pierre Kraemer committed
430
431
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::deleteEdgePreCond(Dart d)
432
{
433
434
435
436
437
	unsigned int nb1 = vertexDegree(d);
	unsigned int nb2 = vertexDegree(this->phi1(d));
	return (nb1!=2) && (nb2!=2);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
438
439
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::deleteEdge(Dart d)
440
441
442
443
444
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
472
473
474
475
476
{
	assert(deleteEdgePreCond(d));

	if(isBoundaryEdge(d))
		return NIL ;

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

			if(res == NIL)
				res = d2 ;

			this->phi2unsew(d2) ;
			this->phi2unsew(d32) ;
			this->phi2sew(d2, d32) ;
			this->phi2sew(fit, d3) ;

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

	ParentMap::deleteCC(d) ;

	return res ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
477
478
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::collapseEdge(Dart d, bool delDegenerateVolumes)
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
{
	Dart resV = NIL;
	Dart dit = d;

	std::vector<Dart> darts;
	do
	{
		darts.push_back(dit);
		dit = this->alpha2(dit);
	} while(dit != d);

	for (std::vector<Dart>::iterator it = darts.begin(); it != darts.end(); ++it)
	{
		Dart x = this->phi2(this->phi_1(*it));

		Dart resCV = NIL;

		if(!isBoundaryFace(this->phi2(this->phi1(*it))))
			resCV = phi3(this->phi2(this->phi1(*it)));
		else if(!isBoundaryFace(this->phi2(this->phi_1(*it))))
			resCV = phi3(this->phi2(this->phi_1(*it)));

		resV = ParentMap::collapseEdge(*it, true);
		if (delDegenerateVolumes)
			if(collapseDegeneretedVolume(x) && resCV != NIL)
				resV = resCV;
	}

	return resV;
}


Pierre Kraemer's avatar
Pierre Kraemer committed
511
512
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::splitFacePreCond(Dart d, Dart e)
513
514
515
516
{
	return (d != e && this->sameOrientedFace(d, e)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
517
518
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::splitFace(Dart d, Dart e)
519
520
521
522
523
524
525
526
527
528
529
530
531
532
{
//	assert(d != e && sameOrientedFace(d, e)) ;
	assert(splitFacePreCond(d,e));

	Dart dd = this->phi1(phi3(d));
	Dart ee = this->phi1(phi3(e));

	ParentMap::splitFace(d, e);
	ParentMap::splitFace(dd, ee);

	phi3sew(this->phi_1(d), this->phi_1(ee));
	phi3sew(this->phi_1(e), this->phi_1(dd));
}

Pierre Kraemer's avatar
Pierre Kraemer committed
533
534
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::mergeFaces(Dart d)
535
536
537
538
539
540
541
542
543
544
545
546
547
{
	assert(edgeDegree(d)==2);

	Dart dd = phi3(d);

	phi3unsew(d);
	phi3unsew(dd);

	//use code of mergesFaces to override the if(isBoundaryEdge)
	//we have to merge the faces if the face is linked to a border also
//	Map2::mergeFaces(d);
	Dart e = this->phi2(d) ;
	this->phi2unsew(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
548
549
550
	Map1<MAP_IMPL>::mergeCycles(d, this->phi1(e)) ;
	Map1<MAP_IMPL>::splitCycle(e, this->phi1(d)) ;
	Map1<MAP_IMPL>::deleteCycle(d) ;
551
552
553
//	ParentMap::mergeFaces(dd);
	e = this->phi2(dd) ;
	this->phi2unsew(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
554
555
556
	Map1<MAP_IMPL>::mergeCycles(dd, this->phi1(e)) ;
	Map1<MAP_IMPL>::splitCycle(e, this->phi1(dd)) ;
	Map1<MAP_IMPL>::deleteCycle(dd);
557
558
559
560

	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
561
562
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::collapseFace(Dart d, bool delDegenerateVolumes)
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
{
	Dart resV = NIL;
	Dart stop = this->phi_1(d);
	Dart dit = d;
	std::vector<Dart> vd;
	vd.reserve(32);

	do
	{
		vd.push_back(this->alpha2(dit));
		dit = this->phi1(dit);
	}
	while(dit != stop);

	for(std::vector<Dart>::iterator it = vd.begin() ; it != vd.end() ; ++it)
		resV = Map3::collapseEdge(*it, delDegenerateVolumes);

	return resV;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
583
584
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::collapseDegeneretedVolume(Dart d)
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
{
	Dart e1 = d;
	Dart e2 = this->phi2(d);

	do
	{
		if (e1 != this->phi2(e2))
			return false;
		e1 = this->phi1(e1);
		e2 = this->phi_1(e2);
	}while (e1 != d);

	if (e2 != this->phi2(d))
		return false;

	// degenerated:
	do
	{
		Dart f1 = phi3(e1);
		Dart f2 = phi3(e2);
		phi3unsew(e1);
		phi3unsew(e2);
		phi3sew(f1,f2);
		e1 = this->phi1(e1);
		e2 = this->phi_1(e2);
	}while (e1 != d);

	ParentMap::deleteCC(d) ;
	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
616
617
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::sewVolumesPreCond(Dart d, Dart e)
618
619
620
621
{
	return (this->faceDegree(d) == this->faceDegree(e));
}

Pierre Kraemer's avatar
Pierre Kraemer committed
622
623
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::sewVolumes(Dart d, Dart e, bool withBoundary)
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
{
	assert(sewVolumesPreCond(d,e));

	// 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 = this->phi1(fitD) ;
			fitE = this->phi_1(fitE) ;
		} while(fitD != d) ;
		return ;
	}

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

	Dart fitD = dd ;
	Dart fitE = ee ;
	do
	{
		Dart fitD2 = this->phi2(fitD) ;
		Dart fitE2 = this->phi2(fitE) ;
		if(fitD2 != fitE)
		{
			this->phi2unsew(fitD) ;
			this->phi2unsew(fitE) ;
			this->phi2sew(fitD2, fitE2) ;
			this->phi2sew(fitD, fitE) ;
		}
		phi3unsew(fitD) ;
		phi3unsew(fitE) ;
		fitD = this->phi1(fitD) ;
		fitE = this->phi_1(fitE) ;
	} while(fitD != dd) ;
	ParentMap::deleteCC(dd) ;

	fitD = d ;
	fitE = e ;
	do
	{
		phi3sew(fitD, fitE) ;
		fitD = this->phi1(fitD) ;
		fitE = this->phi_1(fitE) ;
	} while(fitD != d) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
675
676
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::unsewVolumesPreCond(Dart d)
677
678
679
680
{
	return (!this->isBoundaryFace(d)) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
681
682
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::unsewVolumes(Dart d, bool withBoundary)
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
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
{
	assert(unsewVolumesPreCond(d)) ;

	if (!withBoundary)
	{
		Dart fitD = d ;
		do
		{
			phi3unsew(fitD) ;
			fitD = this->phi1(fitD) ;
		} while(fitD != d) ;
		return ;
	}

	unsigned int nbE = this->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 ;
	do
	{
		Dart f = findBoundaryFaceOfEdge(fit1) ;
		if(f != NIL)
		{
			Dart f2 = this->phi2(f) ;
			this->phi2unsew(f) ;
			this->phi2sew(fitB1, f) ;
			this->phi2sew(fitB2, f2) ;
		}
		else
			this->phi2sew(fitB1, fitB2) ;

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

		fit1 = this->phi1(fit1) ;
		fit2 = this->phi_1(fit2) ;
		fitB1 = this->phi_1(fitB1) ;
		fitB2 = this->phi1(fitB2) ;
	} while(fitB1 != b1) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
731
732
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::mergeVolumes(Dart d, bool deleteFace)
733
{
Pierre Kraemer's avatar
Pierre Kraemer committed
734
	if(!Map3<MAP_IMPL>::isBoundaryFace(d))
735
736
737
738
739
740
741
	{
		ParentMap::mergeVolumes(d, phi3(d), deleteFace); // merge the two volumes along common face
		return true ;
	}
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
742
743
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::splitVolume(std::vector<Dart>& vd)
744
745
746
747
748
749
750
751
752
{
	//assert(checkSimpleOrientedPath(vd)) ;

	Dart e = vd.front();
	Dart e2 = this->phi2(e);

	ParentMap::splitSurface(vd, true, true);

	//sew the two connected components
Pierre Kraemer's avatar
Pierre Kraemer committed
753
	Map3<MAP_IMPL>::sewVolumes(this->phi2(e), this->phi2(e2), false);
754
755
}

Pierre Kraemer's avatar
Pierre Kraemer committed
756
757
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::splitVolumeWithFace(std::vector<Dart>& vd, Dart d)
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
{
	assert(vd.size() == this->faceDegree(d));

	// deconnect edges around the path
	// sew the given face into the paths
	Dart dit = d;
	for(std::vector<Dart>::iterator it = vd.begin() ; it != vd.end() ; ++it)
	{
		Dart it2 = this->phi2(*it);
		ParentMap::unsewFaces(*it, false) ;

		ParentMap::sewFaces(*it, dit, false);
		ParentMap::sewFaces(it2, phi3(dit), false);

		dit = this->phi_1(dit);
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
776
777
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::collapseVolume(Dart d, bool delDegenerateVolumes)
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
{
	Dart resV = NIL;
	std::vector<Dart> vd;
	vd.reserve(32);

	vd.push_back(d);
	vd.push_back(this->alpha2(this->phi1(d)));
	vd.push_back(this->alpha2(this->phi_1(this->phi2(this->phi1(d)))));

//	Traversor3WF<Map3> tra(*this, phi1(d));
//	for(Dart dit = tra.begin() ; dit != tra.end() ; dit = tra.next())
//	{
//		vd.push_back(alpha2(dit));
//	}
//	vd.pop_back();

	for(std::vector<Dart>::iterator it = vd.begin() ; it != vd.end() ; ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
795
		resV = Map3<MAP_IMPL>::collapseEdge(*it, delDegenerateVolumes);
796
797
798
799

	return resV;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
800
801
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::faceToEdge(Dart d)
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
{
	Dart dc = this->phi2(this->phi1(d));
	Dart dc1 = this->phi_1(d);
	Dart dc2 = this->phi1(this->phi2(dc));

	ParentMap::unsewFaces(dc, false);
	ParentMap::unsewFaces(dc1, false);
	ParentMap::unsewFaces(dc2, false);

	unsewFaces(phi3(dc), false);
	unsewFaces(phi3(dc1), false);
	unsewFaces(phi3(dc2), false);

	return dc;
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
822
823
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::sameVertex(Dart d, Dart e) const
824
{
Pierre Kraemer's avatar
Pierre Kraemer committed
825
	DartMarkerStore<MAP_IMPL> mv(*this);	// Lock a marker
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

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

		// add phi21 and phi23 successor if they are not marked yet
		Dart d2 = this->phi2(darts[i]);
		Dart d21 = this->phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume

		if(!mv.isMarked(d21))
		{
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
		}
	}
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
856
857
template <typename MAP_IMPL>
unsigned int Map3<MAP_IMPL>::vertexDegree(Dart d) const
858
859
860
{
	unsigned int count = 0;

Pierre Kraemer's avatar
Pierre Kraemer committed
861
	Traversor3VE<Map3<MAP_IMPL> > trav3VE(*this, d);
862
863
864
865
866
867
	for(Dart dit = trav3VE.begin() ; dit != trav3VE.end() ; dit = trav3VE.next())
		++count;

	return count;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
868
869
template <typename MAP_IMPL>
int Map3<MAP_IMPL>::checkVertexDegree(Dart d, unsigned int vd) const
870
871
872
{
	unsigned int count = 0;

Pierre Kraemer's avatar
Pierre Kraemer committed
873
	Traversor3VE<Map3<MAP_IMPL> > trav3VE(*this, d);
874
875
876
877
878
879
880
	Dart dit = trav3VE.begin();
	for( ; (count <= vd) && (dit != trav3VE.end()) ; dit = trav3VE.next())
		++count;

	return count - vd;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
881
882
template <typename MAP_IMPL>
unsigned int Map3<MAP_IMPL>::vertexDegreeOnBoundary(Dart d) const
883
884
885
886
887
{
	assert(Map3::isBoundaryVertex(d));
	return ParentMap::vertexDegree(d);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
888
889
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::isBoundaryVertex(Dart d) const
890
{
Pierre Kraemer's avatar
Pierre Kraemer committed
891
	DartMarkerStore<MAP_IMPL> mv(*this);	// Lock a marker
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; i < darts.size(); ++i)
	{
		if(this->isBoundaryMarked3(darts[i]))
			return true ;

		//add phi21 and phi23 successor if they are not marked yet
		Dart d2 = this->phi2(darts[i]);
		Dart d21 = this->phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume

		if(!mv.isMarked(d21))
		{
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
		}
	}
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
922
923
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::findBoundaryFaceOfVertex(Dart d) const
924
{
Pierre Kraemer's avatar
Pierre Kraemer committed
925
	DartMarkerStore<MAP_IMPL> mv(*this);	// Lock a marker
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; i < darts.size(); ++i)
	{
		if(this->isBoundaryMarked3(darts[i]))
			return darts[i];

		//add phi21 and phi23 successor if they are not marked yet
		Dart d2 = this->phi2(darts[i]);
		Dart d21 = this->phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume

		if(!mv.isMarked(d21))
		{
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
		}
	}
	return NIL ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
956
957
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::sameOrientedEdge(Dart d, Dart e) const
958
959
960
961
962
963
964
965
966
967
968
{
	Dart it = d;
	do
	{
		if(it == e)
			return true;
		it = alpha2(it);
	} while (it != d);
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
969
970
template <typename MAP_IMPL>
inline bool Map3<MAP_IMPL>::sameEdge(Dart d, Dart e) const
971
972
973
974
{
	return sameOrientedEdge(d, e) || sameOrientedEdge(this->phi2(d), e) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
975
976
template <typename MAP_IMPL>
unsigned int Map3<MAP_IMPL>::edgeDegree(Dart d) const
977
978
979
980
981
{
	unsigned int deg = 0;
	Dart it = d;
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
982
		if(!this->template isBoundaryMarked<3>(it))
983
984
985
986
987
988
			++deg;
		it = alpha2(it);
	} while(it != d);
	return deg;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
989
990
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::isBoundaryEdge(Dart d) const
991
992
993
994
{
	Dart it = d;
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
995
		if(this->template isBoundaryMarked<3>(it))
996
997
998
999
1000
1001
			return true ;
		it = alpha2(it);
	} while(it != d);
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1002
1003
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::findBoundaryFaceOfEdge(Dart d) const
1004
1005
1006
1007
{
	Dart it = d;
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
1008
		if (this->template isBoundaryMarked<3>(it))
1009
1010
1011
1012
1013
1014
			return it ;
		it = alpha2(it);
	} while(it != d);
	return NIL ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1015
1016
template <typename MAP_IMPL>
inline bool Map3<MAP_IMPL>::sameFace(Dart d, Dart e) const
1017
1018
1019
1020
{
	return ParentMap::sameOrientedFace(d, e) || ParentMap::sameOrientedFace(phi3(d), e) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1021
1022
template <typename MAP_IMPL>
inline bool Map3<MAP_IMPL>::isBoundaryFace(Dart d) const
1023
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1024
	return this->template isBoundaryMarked<3>(d) || this->template isBoundaryMarked<3>(phi3(d));
1025
1026
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1027
1028
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::isBoundaryVolume(Dart d) const
1029
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1030
	Traversor3WF<Map3<MAP_IMPL> > tra(*this, d);
1031
1032
1033
1034
1035
1036
1037
1038
	for(Dart dit = tra.begin() ; dit != tra.end() ; dit = tra.next())
	{
		if(isBoundaryMarked3(phi3(dit)))
			return true ;
	}
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1039
1040
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::hasBoundaryEdge(Dart d) const
1041
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1042
	Traversor3WE<Map3<MAP_IMPL> > tra(*this, d);
1043
1044
1045
1046
1047
1048
1049
1050
1051
	for(Dart dit = tra.begin() ; dit != tra.end() ; dit = tra.next())
	{
		if(isBoundaryEdge(dit))
			return true;
	}

	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1052
1053
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::check() const
1054
1055
{
	std::cout << "Check: topology begin" << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
1056
	DartMarkerStore<MAP_IMPL> m(*this);
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
	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;
		}

		if(this->phi1(d3) != phi3(this->phi_1(d)))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1068
			if(this->template isBoundaryMarked<3>(d))
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
				std::cout << "Boundary case - Check: phi3 , faces are not entirely sewn" << std::endl;
			else
				std::cout << "Check: phi3 , faces are not entirely sewn" << std::endl;
			std::cout << "face : " << this->phi1(d3) << " and face = " << phi3(this->phi_1(d)) << std::endl;
			return false;
		}

		Dart d2 = this->phi2(d);
		if (this->phi2(d2) != d) // phi2 involution ?
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1079
			if(this->template isBoundaryMarked<3>(d))
1080
1081
1082
1083
1084
1085
1086
1087
				std::cout << "Boundary case - ";
			std::cout << "Check: phi2 is not an involution" << std::endl;
			return false;
		}

		Dart d1 = this->phi1(d);
		if (this->phi_1(d1) != d) // phi1 a une image correcte ?
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1088
			if(this->template isBoundaryMarked<3>(d))
1089
1090
1091
1092
1093
1094
1095
				std::cout << "Boundary case - ";
			std::cout << "Check: unconsistent phi_1 link" << std::endl;
			return false;
		}

		if (m.isMarked(d1)) // phi1 a un seul antécédent ?
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1096
			if(this->template isBoundaryMarked<3>(d))
1097
1098
1099
1100
1101
1102
1103
1104
				std::cout << "Boundary case - ";
			std::cout << "Check: dart with two phi1 predecessors" << std::endl;
			return false;
		}
		m.mark(d1);

		if (d1 == d)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1105
			if(this->template isBoundaryMarked<3>(d))
1106
1107
1108
1109
1110
1111
				std::cout << "Boundary case - ";
			std::cout << "Check: (warning) face loop (one edge)" << std::endl;
		}

		if (this->phi1(d1) == d)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1112
			if(this->template isBoundaryMarked<3>(d))
1113
1114
1115
1116
1117
1118
				std::cout << "Boundary case - ";
			std::cout << "Check: (warning) face with only two edges" << std::endl;
		}

		if (this->phi2(d1) == d)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1119
			if(this->template isBoundaryMarked<3>(d))
1120
1121
1122
1123
1124
1125
				std::cout << "Boundary case - ";
			std::cout << "Check: (warning) dandling edge (phi2)" << std::endl;
		}

		if (phi3(d1) == d)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1126
			if(this->template isBoundaryMarked<3>(d))
1127
1128
1129
1130
1131
1132
1133
1134
1135
				std::cout << "Boundary case - ";
			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 ?
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1136
			if(this->template isBoundaryMarked<3>(d))
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
				std::cout << "Boundary case - ";
			std::cout << "Check: dart with no phi1 predecessor" << std::endl;
			return false;
		}
	}

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

	return true;
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
1152
1153
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::foreach_dart_of_vertex(Dart d, FunctorType& f, unsigned int thread) const
1154
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1155
	DartMarkerStore<MAP_IMPL> mv(*this, thread);	// Lock a marker
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
	bool found = false;					// Last functor return value

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(256);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; !found && i < darts.size(); ++i)
	{
		// add phi21 and phi23 successor if they are not marked yet
		Dart d2 = this->phi2(darts[i]);
		Dart d21 = this->phi1(d2); // turn in volume
		Dart d23 = phi3(d2); // change volume

		if(!mv.isMarked(d21))
		{
			darts.push_back(d21);
			mv.mark(d21);
		}
		if(!mv.isMarked(d23))
		{
			darts.push_back(d23);
			mv.mark(d23);
		}

		found = f(darts[i]);
	}
	return found;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1186
1187
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::foreach_dart_of_edge(Dart d, FunctorType& f, unsigned int thread) const
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
{
	Dart it = d;
	do
	{
		if (ParentMap::foreach_dart_of_edge(it, f, thread))
			return true;
		it = alpha2(it);
	} while (it != d);
	return false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1199
1200
template <typename MAP_IMPL>
inline bool Map3<MAP_IMPL>::foreach_dart_of_face(Dart d, FunctorType& f, unsigned int thread) const
1201
1202
1203
1204
{
	return ParentMap::foreach_dart_of_face(d, f, thread) || ParentMap::foreach_dart_of_face(phi3(d), f, thread);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1205
1206
template <typename MAP_IMPL>
inline bool Map3<MAP_IMPL>::foreach_dart_of_volume(Dart d, FunctorType& f, unsigned int thread) const
1207
1208
1209
1210
{
	return ParentMap::foreach_dart_of_cc(d, f, thread);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1211
1212
template <typename MAP_IMPL>
bool Map3<MAP_IMPL>::foreach_dart_of_cc(Dart d, FunctorType& f, unsigned int thread) const
1213
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1214
	DartMarkerStore<MAP_IMPL> mv(*this,thread);	// Lock a marker
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
	bool found = false;					// Last functor return value

	std::vector<Dart> darts;	// Darts that are traversed
	darts.reserve(1024);
	darts.push_back(d);			// Start with the dart d
	mv.mark(d);

	for(unsigned int i = 0; !found && i < darts.size(); ++i)
	{
		// add all successors if they are not marked yet
		Dart d2 = this->phi1(darts[i]); // turn in face
		Dart d3 = this->phi2(darts[i]); // change face
		Dart d4 = phi3(darts[i]); // change volume

		if (!mv.isMarked(d2))
		{
			darts.push_back(d2);
			mv.mark(d2);
		}
		if (!mv.isMarked(d3))
		{
			darts.push_back(d2);
			mv.mark(d2);
		}
		if (!mv.isMarked(d4))
		{
			darts.push_back(d4);
			mv.mark(d4);
		}

		found = f(darts[i]);
	}
	return found;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1250
1251
template <typename MAP_IMPL>
inline bool Map3<MAP_IMPL>::foreach_dart_of_vertex2(Dart d, FunctorType& f, unsigned int thread) const
1252
1253
1254
1255
{
	return ParentMap::foreach_dart_of_vertex(d, f, thread);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1256
1257
template <typename MAP_IMPL>
inline bool Map3<MAP_IMPL>::foreach_dart_of_edge2(Dart d, FunctorType& f, unsigned int thread) const
1258
1259
1260
1261
{
	return ParentMap::foreach_dart_of_edge(d, f, thread);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1262
1263
template <typename MAP_IMPL>
inline bool Map3<MAP_IMPL>::foreach_dart_of_face2(Dart d, FunctorType& f, unsigned int thread) const
1264
1265
1266
1267
1268
1269
1270
1271
{
	return ParentMap::foreach_dart_of_face(d, f, thread);
}

/*! @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
1272
1273
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::newBoundaryCycle(unsigned int nbE)
1274
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1275
	Dart d = Map1<MAP_IMPL>::newCycle(nbE);
1276
1277
1278
1279
	this->template boundaryMarkOrbit<FACE,3>(d);
	return d;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1280
1281
template <typename MAP_IMPL>
unsigned int Map3<MAP_IMPL>::closeHole(Dart d, bool forboundary)
1282
1283
{
	assert(phi3(d) == d);		// Nothing to close
Pierre Kraemer's avatar
Pierre Kraemer committed
1284
	DartMarkerStore<MAP_IMPL> m(*this) ;
1285
1286
1287
1288

	std::vector<Dart> visitedFaces;	// Faces that are traversed
	visitedFaces.reserve(1024) ;
	visitedFaces.push_back(d);		// Start with the face of d
Pierre Kraemer's avatar
Pierre Kraemer committed
1289
	m.template markOrbit<FACE2>(d) ;
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315

	unsigned int count = 0 ;

	// For every face added to the list
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
	{
		Dart it = visitedFaces[i] ;
		Dart f = it ;

		unsigned int degree = this->faceDegree(f) ;
		Dart b = newBoundaryCycle(degree) ;
		++count ;

		Dart bit = b ;
		do
		{
			Dart e = alpha2(f) ;
			bool found = false ;
			do
			{
				if(phi3(e) == e)
				{
					found = true ;
					if(!m.isMarked(e))
					{
						visitedFaces.push_back(e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
1316
						m.template markOrbit<FACE2>(e) ;
1317
1318
					}
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
1319
				else if(this->template isBoundaryMarked<3>(e))
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
				{
					found = true ;
					this->phi2sew(e, bit) ;
				}
				else
					e = alpha2(e) ;
			} while(!found) ;

			phi3sew(f, bit) ;
			bit = this->phi_1(bit) ;
			f = this->phi1(f);
		} while(f != it) ;
	}

	return count ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1337
1338
template <typename MAP_IMPL>
unsigned int Map3<MAP_IMPL>::closeMap()
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
{
	// Search the map for topological holes (fix points of phi3)
	unsigned int nb = 0 ;
	for (Dart d = this->begin(); d != this->end(); this->next(d))
	{
		if (phi3(d) == d)
		{
			++nb ;
			closeHole(d);
		}
	}
	return nb ;
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
1357
1358
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::reverseOrientation()
1359
1360
1361
1362
{

}

Pierre Kraemer's avatar
Pierre Kraemer committed
1363
1364
template <typename MAP_IMPL>
void Map3<MAP_IMPL>::computeDual()
1365
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1366
1367
1368
1369
	DartAttribute<Dart, MAP_IMPL> old_phi1 = this->template getAttribute<Dart, DART>("phi1") ;
	DartAttribute<Dart, MAP_IMPL> old_phi_1 = this->template getAttribute<Dart, DART>("phi_1") ;
	DartAttribute<Dart, MAP_IMPL> new_phi1 = this->template addAttribute<Dart, DART>("new_phi1") ;
	DartAttribute<Dart, MAP_IMPL> new_phi_1 = this->template addAttribute<Dart, DART>("new_phi_1") ;
1370

Pierre Kraemer's avatar
Pierre Kraemer committed
1371
1372
	DartAttribute<Dart, MAP_IMPL> old_phi2 = this->template getAttribute<Dart, DART>("phi2") ;
	DartAttribute<Dart, MAP_IMPL> new_phi2 = this->template addAttribute<Dart, DART>("new_phi2") ;
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535

	for(Dart d = this->begin(); d != this->end(); this->next(d))
	{
		Dart dd = this->phi2(phi3(d)) ;
		new_phi1[d] = dd ;
		new_phi_1[dd] = d ;

		Dart ddd = this->phi1(phi3(d));
		new_phi2[d] = ddd;
		new_phi2[ddd] = d;
	}

	this->template swapAttributes<Dart>(old_phi1, new_phi1) ;
	this->template swapAttributes<Dart>(old_phi_1, new_phi_1) ;
	this->template swapAttributes<Dart>(old_phi2, new_phi2) ;

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

	this->swapEmbeddingContainers(VERTEX, VOLUME) ;

//	unsigned int count = 0;

//	std::vector<Dart> vbound;

//	//std::cout << "nb faces : " << closeMap() << std::endl;

//	for(Dart d = begin(); d != end(); next(d))
//	{
//		if(isBoundaryMarked3(d) && !isBoundaryMarked3(phi3(d)))
//		{
//			vbound.push_back(d);
//		}
//	}
//
//	std::cout << "vbound size = " << vbound.size() << std::endl;
//
//	for(std::vector<Dart>::iterator it = vbound.begin() ; it != vbound.end() ; ++it)
//	{
//		Dart d = *it;
//		//Dart d3 = phi3(d);
//		phi3unsew(d);
//		//phi3unsew(d3);
//	}
//
//	//std::cout << "nb faces : " << closeMap() << std::endl;
//
//			if(d == 14208)
//			{
//				std::cout << "yeahhhhhhhh" << std::endl;
//				std::cout << "isBoundaryMarked ? " << isBoundaryMarked3(phi3(phi2(14208))) << std::endl;
//
//			}
//
//			//boundaryUnmark<3>(d);
//
//		}
//			if(d == 1569)
//			{
//				std::cout << "d " << std::endl;
//
//				Traversor3WE<Map3> t(*this,d);
//				for(Dart dit = t.begin() ; dit != t.end() ; dit = t.next())
//				{
//					Dart temp = dit;
//					do
//					{
//						if(isBoundaryMarked3(d))
//							std::cout << "d boundary " << std::endl;
//
//						temp = alpha2(temp);
//					}while(temp != dit);
//				}
//
//				if(isBoundaryMarked3(d))
//					std::cout << "d boundary " << std::endl;
//
//				if(isBoundaryMarked3(phi1(d)))
//					std::cout << "phi1(d) boundary " << std::endl;
//
//				if(isBoundaryMarked3(phi_1(d)))
//					std::cout << "phi_1(d) boundary " << std::endl;
//
//				if(isBoundaryMarked3(phi2(d)))
//					std::cout << "phi2(d) boundary " << std::endl;
//
//				if(isBoundaryMarked3(phi3(d)))
//					std::cout << "phi3(d) boundary " << std::endl;
//
//				if(isBoundaryMarked3(phi2(phi3(d))))
//					std::cout << "phi2(phi3(d)) boundary " << std::endl;
//
//				if(isBoundaryMarked3(phi3(phi2(d))))
//					std::cout << "phi3(phi2(d)) boundary " << std::endl;
//
//				if(isBoundaryMarked3(phi1(phi3(d))))
//					std::cout << "phi1(phi3(d)) boundary " << std::endl;
//
//				if(isBoundaryMarked3(phi3(phi1(d))))
//					std::cout << "phi3(phi1(d)) boundary " << std::endl;
//			}
//
//			if(isBoundaryMarked3(d))
//			{
//
//			if(isBoundaryMarked3(d))
//				std::cout << "d = " << d << std::endl;
//
//			if(isBoundaryMarked3(phi3(d)))
//				std::cout << "phi3(d) = " << phi3(d) << std::endl;
//
//			if(isBoundaryMarked3(phi2(d)))
//				std::cout << "phi2(d) = " << phi2(d) << std::endl;
//
//			Dart dit = deleteVertex(phi3(d));
//
//			if(dit == NIL)
//				std::cout << "NILLLLLLLLLLLLLLL" << std::endl;
//
//			++count;
//
//			if(count == 5)
//				return;
//		}
//	}


//	TraversorW<Map3> tW(*this);
//	for(Dart d = tW.begin(); d != tW.end(); d = tW.next())
//	{
//		if(isBoundaryMarked3(d))
//		{
//			boundaryMarkOrbit<VOLUME,3>(d);
//		}
//	}

//	unsigned int count = 0;
//	for(Dart d = begin(); d != end(); next(d))
//	{
//		if(isBoundaryMarked3(d))
//		{
//			++count;
//		}
//	}
//	std::cout << "nb boundar marked = " << count << std::endl;
//
//	count = 0;
//	for(Dart d = begin(); d != end(); next(d))
//	{
//		if(isBoundaryMarked3(d))
//		{
//			++count;
//			std::cout << count << std::endl;
//			//Map3::deleteVolume(d,false);
//			//deleteVolume(d,false);
//		}
//	}


	//std::cout << "Map closed (" << closeMap() <<" boundary faces)" << std::endl;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
1536
1537
template <typename MAP_IMPL>
Dart Map3<MAP_IMPL>::explodBorderTopo(Dart d)
1538
1539
1540
1541
1542
{
	std::vector<std::pair<Dart,Dart> > ve;
	ve.reserve(1024);

	//stocke un brin par face du bord
Pierre Kraemer's avatar
Pierre Kraemer committed
1543
	DartMarker<MAP_IMPL> me(*this);
1544
1545
	for(Dart dit = this->begin() ; dit != this->end() ; this->next(dit))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
1546
		if(this->template isBoundaryMarked<3>(dit) && !me.isMarked(dit))
1547
1548
		{
			ve.push_back(std::make_pair(dit, this->phi2(dit)));
Pierre Kraemer's avatar
Pierre Kraemer committed
1549
			me.template markOrbit<EDGE>(dit);
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
		}
	}

	//decoud chaque face
	for(std::vector<std::pair<Dart,Dart> >::iterator it = ve.begin() ; it != ve.end() ; ++it)
	{
		ParentMap::unsewFaces((*it).first, false);
	}

	//triangule chaque face
Pierre Kraemer's avatar
Pierre Kraemer committed
1560
	DartMarker<MAP_IMPL> mf(*this);
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
	for(std::vector<std::pair<Dart,Dart> >::iterator it = ve.begin() ; it != ve.end() ; ++it)
	{
		Dart first = (*it).first;
		Dart second = (*it).second;

		if(!mf.isMarked(first))
		{
			unsigned int degf = ParentMap::faceDegree(first);

			Dart dnf = ParentMap::newFace(degf, false);
			Dart dit = first;
			do
			{
				ParentMap::sewFaces(dit, dnf, false);
				this->template copyDartEmbedding<VERTEX>(dnf, this->phi1(dit)) ;
				dit = this->phi1(dit);
				dnf = this->phi_1(dnf);
			} while(dit != first);

Pierre Kraemer's avatar
Pierre Kraemer committed
1580
			mf.template markOrbit<FACE>(first);
1581
1582
1583
1584
1585
1586

			Dart db = dnf;
			Dart d1 = this->phi1(db);
			ParentMap::splitFace(db, d1) ;
			ParentMap::cutEdge(this->phi_1(db)) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
1587
			Dart x = this->phi2(this->phi_1(db)) ;
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
			Dart dd = this->phi1(this->phi1(this->phi1(x)));
			while(dd != x)
			{
				Dart next = this->phi1(dd) ;
				ParentMap::splitFace(dd, this->phi1(x)) ;
				dd = next ;
			}

			Dart cd = this->phi_1(db);
			do
			{
				this->template setDartEmbedding<VERTEX>(this->phi2(cd), this->template getEmbedding<VERTEX>(this->phi1(cd))) ;
				cd = this->phi2(this->phi_1(cd));
			} while(cd != this->phi_1(db));
		}

		if(!mf.isMarked(second))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
1606
			mf.template markOrbit<FACE>(second);
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
			unsigned int degf = ParentMap::faceDegree(second);

			Dart dnf = ParentMap::newFace(degf, false);
			Dart dit = second;
			do
			{
				ParentMap::sewFaces(dit, dnf, false);
				this->template copyDartEmbedding<VERTEX>(dnf, this->phi1(dit));
				dit = this->phi1(dit);
				dnf = this->phi_1(dnf);
			} while(dit != second);

Pierre Kraemer's avatar
Pierre Kraemer committed
1619
			mf.template markOrbit<FACE>(second);
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648

			Dart db = dnf;
			Dart d1 = this->phi1(db);
			ParentMap::splitFace(db, d1);
			ParentMap::cutEdge(this->phi_1(db));

			Dart x = this->phi2(this->phi_1(db)) ;
			Dart dd = this->phi1(this->phi1(this->phi1(x)));
			while(dd != x)
			{
				Dart next = this->phi1(dd) ;
				ParentMap::splitFace(dd, this->phi1(x)) ;
				dd = next ;
			}

			Dart cd = this->phi_1(db);
			do
			{
				this->template setDartEmbedding<VERTEX>(this->phi2(cd), this->template getEmbedding<VERTEX>(this->phi1(cd))) ;
				cd = this->phi2(this->phi_1(cd));
			} while(cd != this->phi_1(db));
		}
	}

	//close de chaque nouveau volume
	for(std::vector<std::pair<Dart,Dart> >::iterator it = ve.begin() ; it != ve.end() ; ++it)
	{
		Dart dit1 = this->phi2((*it).first);
		Dart dit2 = this->phi2((*it).second);
Pierre Kraemer's avatar
Pierre Kraemer committed
1649
		Map3<MAP_IMPL>::sewVolumes(dit1, dit2, false);
1650
1651
1652
1653
1654
1655
1656
	}

	this->template setOrbitEmbeddingOnNewCell<VERTEX>(this->phi_1(this->phi2(ve.front().first)));

	return this->phi_1(this->phi2(ve.front().first));
}