tetrahedralization.hpp 28.4 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
#include "Algo/Modelisation/subdivision3.h"
untereiner's avatar
untereiner committed
26
27
#include "Topology/generic/traversor3.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
28
29
30
31
32
33
34
35
36
namespace CGoGN
{

namespace Algo
{

namespace Modelisation
{

untereiner's avatar
untereiner committed
37
namespace Tetrahedralization
Pierre Kraemer's avatar
Pierre Kraemer committed
38
{
untereiner's avatar
untereiner committed
39
40
41
42

template <typename PFP>
void hexahedronToTetrahedron(typename PFP::MAP& map, Dart d)
{
43
44
45
46
	Dart d1 = d;
	Dart d2 = map.phi1(map.phi1(d));
	Dart d3 = map.phi_1(map.phi2(d));
	Dart d4 = map.phi1(map.phi1(map.phi2(map.phi_1(d3))));
untereiner's avatar
untereiner committed
47

48
49
50
51
52
	cut3Ear<PFP>(map,d1);
	cut3Ear<PFP>(map,d2);
	cut3Ear<PFP>(map,d3);
	cut3Ear<PFP>(map,d4);
}
untereiner's avatar
untereiner committed
53

54
55
56
template <typename PFP>
void hexahedronsToTetrahedrons(typename PFP::MAP& map)
{
57
58
59
60
61
62
63
64
65
66
    TraversorV<typename PFP::MAP> tv(map);

    //for each vertex
    for(Dart d = tv.begin() ; d != tv.end() ; d = tv.next())
    {
        bool vertToTet=true;
        std::vector<Dart> dov;
        dov.reserve(32);
        FunctorStore fs(dov);
        map.foreach_dart_of_vertex(d,fs);
Thomas's avatar
Thomas committed
67
        CellMarkerStore<VOLUME> cmv(map);
68
69
70
71
72
73
74
75
76
77

        //check if all vertices degree is equal to 3 (= no direct adjacent vertex has been split)
        for(std::vector<Dart>::iterator it=dov.begin();vertToTet && it!=dov.end();++it)
        {
            if(!cmv.isMarked(*it) && !map.isBoundaryMarked(*it))
            {
                cmv.mark(*it);
                vertToTet = (map.phi1(map.phi2(map.phi1(map.phi2(map.phi1(map.phi2(*it))))))==*it); //degree = 3
            }
        }
untereiner's avatar
untereiner committed
78

79
80
81
82
83
84
85
86
87
88
89
90
91
        //if ok : create tetrahedrons around the vertex
        if(vertToTet)
        {
            for(std::vector<Dart>::iterator it=dov.begin();it!=dov.end();++it)
            {
                if(cmv.isMarked(*it) && !map.isBoundaryMarked(*it))
                {
                    cmv.unmark(*it);
                    cut3Ear<PFP>(map,*it);
                }
            }
        }
    }
untereiner's avatar
untereiner committed
92
93
}

94
95
96
97
template <typename PFP>
void tetrahedrizeVolume(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
{
	//mark bad edges
98
	DartMarker mBadEdge(map);
99

100
//	unsignzed int i = 0;
101

102
	DartMarkerStore mEdge(map);
103

104
	for(Dart dit = map.begin() ; dit != map.end() ; map.next(dit))
105
106
	{
		//check if this edge is an "ear-edge"
107
		if(!mBadEdge.isMarked(dit))
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
		{
			//search three positions
			typename PFP::VEC3 tris1[3];
			tris1[0] = position[dit];
			tris1[1] = position[map.phi_1(dit)];
			tris1[2] = position[map.phi_1(map.phi2(dit))];

			//search if the triangle formed by these three points intersect the rest of the mesh (intersection triangle/triangle)
			TraversorF<typename PFP::MAP> travF(map);
			for(Dart ditF = travF.begin() ; ditF != travF.end() ; ditF = travF.next())
			{
				//get vertices position
				typename PFP::VEC3 tris2[3];
				tris2[0] = position[ditF];
				tris2[1] = position[map.phi1(ditF)];
				tris2[2] = position[map.phi_1(ditF)];

				bool intersection = false;

				for (unsigned int i = 0; i < 3 && !intersection; ++i)
				{
					typename PFP::VEC3 inter;
					intersection = Geom::intersectionSegmentTriangle(tris1[i], tris1[(i+1)%3], tris2[0], tris2[1], tris2[2], inter);
				}

				if(!intersection)
				{
					for (unsigned int i = 0; i < 3 && !intersection; ++i)
					{
						typename PFP::VEC3 inter;
						intersection = Geom::intersectionSegmentTriangle(tris2[i], tris2[(i+1)%3], tris1[0], tris1[1], tris1[2], inter);
					}
				}

				std::cout << "intersection ? " << (intersection ? "true" : "false") << std::endl;

				if(intersection)
				{
146
					mBadEdge.markOrbit<EDGE>(dit);
147
148
149
				}
				else //cut a tetrahedron
				{
150
					std::cout << "cut cut " << std::endl;
151
152
153
154
155
156
157
158
159
160
					Dart dring = map.phi_1(dit);
					std::vector<Dart> vPath;

					vPath.push_back(map.phi_1(dring));
					vPath.push_back(map.phi1(map.phi2(dring)));
					vPath.push_back(map.phi_1(map.phi2(dring)));
					vPath.push_back(map.phi1(dring));

					map.splitVolume(vPath);

161
					map.splitFace(map.phi2(map.phi1(dring)), map.phi2(map.phi1(map.phi2(dring))));
162
163

					return;
164
165
166
167
168
169
				}

//				++i;
//
//				if(i == 16)
//					return;
170
171
172
173
174
175
			}
		}
	}

}

untereiner's avatar
untereiner committed
176

177
/************************************************************************************************
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
 * 									Collapse / Split Operators
 ************************************************************************************************/
template <typename PFP>
Dart splitVertex(typename PFP::MAP& map, std::vector<Dart>& vd)
{
	//split the vertex
	Dart dres = map.splitVertex(vd);

	//split the faces incident to the new vertex
	Dart dbegin = map.phi1(map.phi2(vd.front()));
	Dart dit = dbegin;
	do
	{
		map.splitFace(map.phi1(dit),map.phi_1(dit));
		dit = map.alpha2(dit);
	}
	while(dbegin != dit);

	//split the volumes incident to the new vertex
	for(unsigned int i = 0; i < vd.size(); ++i)
	{
		Dart dit = vd[i];

		std::vector<Dart> v;
		v.push_back(map.phi1(map.phi1(map.phi2(dit))));
		std::cout << "[" << v.back();
		v.push_back(map.phi1(dit));
		std::cout << " - " << v.back();
		v.push_back(map.phi1(map.phi2(map.phi_1(dit))));
		std::cout << " - " << v.back() << "]" << std::endl;
208
		map.splitVolume(v);
209
210
211
212
213
214
215
	}

	return dres;
}

/************************************************************************************************
 * 								Tetrahedron functions															   *
216
 ************************************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
217
218

template <typename PFP>
219
bool isTetrahedron(typename PFP::MAP& the_map, Dart d, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
220
{
untereiner's avatar
untereiner committed
221
	unsigned int nbFaces = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
222
223

	//Test the number of faces end its valency
224
	Traversor3WF<typename PFP::MAP> travWF(the_map, d, false, thread);
untereiner's avatar
untereiner committed
225
	for(Dart dit = travWF.begin() ; dit != travWF.end(); dit = travWF.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
226
	{
untereiner's avatar
untereiner committed
227
228
229
230
231
232
233
234
		//increase the number of faces
		nbFaces++;
		if(nbFaces > 4)	//too much faces
			return false;

		//test the valency of this face
		if(the_map.faceDegree(dit) != 3)
			return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
235
236
	}

Thomas's avatar
Thomas committed
237
	return true;
Pierre Kraemer's avatar
Pierre Kraemer committed
238
239
}

240
241
242
243
244
245
246
247
248
249
250
251
252
template <typename PFP>
bool isTetrahedralization(typename PFP::MAP& map, const FunctorSelect& selected)
{
	TraversorV<typename PFP::MAP> travV(map, selected);
	for(Dart dit = travV.begin() ; dit != travV.end() ; dit = travV.next())
	{
		if(!isTetrahedron<PFP>(map, dit))
			return false;
	}

	return true;
}

253
/************************************************************************************************
254
 * 									Topological functions															   *
255
 ************************************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
256

257
//sew a face into the edge
Pierre Kraemer's avatar
Pierre Kraemer committed
258
template <typename PFP>
259
Dart linkIntoEdge(typename PFP::MAP& map, Dart d, Dart e)
Pierre Kraemer's avatar
Pierre Kraemer committed
260
{
261
262
263
264
265
	Dart e2 = map.phi2(e);
	Dart d3 = map.phi3(d);

	//Decoud les 2 brins
	map.unsewFaces(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
266

267
268
269
	//Coudre la nouvelle face au milieu de l'ancienne arête
	map.sewFaces(e2,d3);
	map.sewFaces(e,d);
Pierre Kraemer's avatar
Pierre Kraemer committed
270

Pierre Kraemer's avatar
Pierre Kraemer committed
271
272
	map.setDartEmbedding<VERTEX>(d, map.getEmbedding<VERTEX>(e2)) ;
	map.setDartEmbedding<VERTEX>(d3, map.getEmbedding<VERTEX>(e)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
273

274
275
	return e2;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
276

277
278
279
280
281
282
//unsew a face from the edge
template <typename PFP>
void unlinkFromEdge(typename PFP::MAP& map, Dart d)
{
	Dart d3 = map.phi3(d);

283
//	if(map.isOrbitEmbedded<VERTEX>())
284
285
286
287
//	{
//		//Si la face n'est pas libre en phi2
//		if(map.phi2(d) != d && map.phi2(d3) != d3)
//		{
288
//			unsigned int dVEmb = map.getEmbedding<VERTEX>(d) ;
289
290
//			if(dVEmb != EMBNULL)
//			{
Pierre Kraemer's avatar
Pierre Kraemer committed
291
292
//				map.embedOrbit<VERTEX>(d, dVEmb) ;
//				map.setDartEmbedding<VERTEX>(d, EMBNULL) ;
293
294
//			}
//
295
//			unsigned int d3VEmb = map.getEmbedding<VERTEX>(d3) ;
296
297
//			if(d3VEmb != EMBNULL)
//			{
Pierre Kraemer's avatar
Pierre Kraemer committed
298
299
//				map.embedOrbit<VERTEX>(d3, d3VEmb) ;
//				map.setDartEmbedding<VERTEX>(d3, EMBNULL) ;
300
301
302
303
304
305
306
307
//			}
//		}
//		//Si la face est libre en phi2
//		else
//		{
//
//		}
//	}
308
309
310
311
312
313
314
315
316
317
318

	Dart e2 = map.phi2(d3);
	Dart d2 = map.phi2(d);
	map.unsewFaces(e2);
	map.unsewFaces(d2);
	map.sewFaces(d2,e2);
}

template <typename PFP>
void unlinkFace(typename PFP::MAP& map, Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
319
320
321
	Dart e = d;
	do
	{
322
323
		unlinkFromEdge<PFP>(map, e);
		e = map.phi1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
324
325
	}
	while (e != d);
326
}
Pierre Kraemer's avatar
Pierre Kraemer committed
327

328
329
330
331
332
template <typename PFP>
void insertFace(typename PFP::MAP& map, Dart d, Dart nF)
{
	Dart dd = d;
	Dart nFd = nF;
Pierre Kraemer's avatar
Pierre Kraemer committed
333

334
335
	do {
		//TODO linkIntoEdge
Pierre Kraemer's avatar
Pierre Kraemer committed
336

337
338
339
340
		Dart d2 = map.phi2(dd);
		map.unsewFaces(dd);
		map.sewFaces(dd,nFd);
		map.sewFaces(d2,map.phi3(nFd));
Pierre Kraemer's avatar
Pierre Kraemer committed
341

Pierre Kraemer's avatar
Pierre Kraemer committed
342
343
		map.setDartEmbedding<VERTEX>(nFd, map.getEmbedding<VERTEX>(d2)) ;
		map.setDartEmbedding<VERTEX>(map.phi3(nFd), map.getEmbedding<VERTEX>(dd)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
344

345
346
		dd = map.phi_1(map.phi2(map.phi_1(dd)));
		nFd = map.phi1(nFd);
Pierre Kraemer's avatar
Pierre Kraemer committed
347

348
349
350
351
352
353
	} while (nFd != nF);
}

/***********************************************************************************************
 * 										swap functions										   *
 ***********************************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
354

355
356
357
358
359
360
361
362
363
364
365
366
367
368
//ok
template <typename PFP>
void swap2To2(typename PFP::MAP& map, Dart d)
{
	//save a dart from a non-modifed-face of one tetrahedron
	Dart r = map.phi2(d);

	//detach common face from tetrahedron from the rest of the faces
	unlinkFace<PFP>(map, d);

	//flip the middle edge
	map.flipEdge(r);
	Dart e = map.phi2(r);

369
	unsigned int dVEmb = map.getEmbedding<VERTEX>(r) ;
370
371
	if(dVEmb != EMBNULL)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
372
373
		map.setDartEmbedding<VERTEX>(map.phi_1(r), dVEmb) ;
		map.setDartEmbedding<VERTEX>(r, EMBNULL) ;
374
375
	}

376
	unsigned int eVEmb = map.getEmbedding<VERTEX>(e) ;
377
378
	if(eVEmb != EMBNULL)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
379
380
		map.setDartEmbedding<VERTEX>(map.phi_1(e), eVEmb) ;
		map.setDartEmbedding<VERTEX>(e, EMBNULL) ;
381
382
383
384
	}

	//insert the face in the flipped edge
	insertFace<PFP>(map, r, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
385
386
387

	Dart dd = d;
	do {
388
389
390
		Dart e = map.phi2(dd);
		Dart e2= map.phi2(map.phi3(dd));

Pierre Kraemer's avatar
Pierre Kraemer committed
391
392
		map.setDartEmbedding<VERTEX>(dd, map.getEmbedding<VERTEX>(e2)) ;
		map.setDartEmbedding<VERTEX>(map.phi3(dd), map.getEmbedding<VERTEX>(e)) ;
393

Pierre Kraemer's avatar
Pierre Kraemer committed
394

395
396
		dd = map.phi1(dd);
	} while( dd!=d);
Pierre Kraemer's avatar
Pierre Kraemer committed
397
398
399
400
401

}

//ok
template <typename PFP>
402
void swap4To4(typename PFP::MAP& map, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
403
404
405
{
	//!! 4 decouture inutile, seule l'intersection du centre doit etre decousu puis recousu

406
407
	Dart e = map.phi2(map.phi3(d));
	Dart dd = map.phi2(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
408
409

	//unsew middle crossing darts
410
411
	map.unsewVolumes(d);
	map.unsewVolumes(map.phi2(map.phi3(dd)));
Pierre Kraemer's avatar
Pierre Kraemer committed
412

Thomas's avatar
Thomas committed
413
414
	Algo::Modelisation::Tetrahedralization::swap2To2<PFP>(map, dd);
	Algo::Modelisation::Tetrahedralization::swap2To2<PFP>(map, e);
Pierre Kraemer's avatar
Pierre Kraemer committed
415
416

	//sew middle darts so that they do not cross
417
418
	map.sewVolumes(d,map.phi2(map.phi3(e)));
	map.sewVolumes(map.phi2(map.phi3(dd)),map.phi2(e));
Pierre Kraemer's avatar
Pierre Kraemer committed
419
420
421
}

template <typename PFP>
422
void swap3To2(typename PFP::MAP& map, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
423
{
424
425
426
	Dart en1 = map.phi_1(map.phi2(d));
	Dart en2 = map.phi1(d);
	Dart fi = map.phi2(en1);
Pierre Kraemer's avatar
Pierre Kraemer committed
427

428
429
430
	//Decouture de la premiere face
	unlinkFromEdge<PFP>(map, map.phi2(en1));
	unlinkFromEdge<PFP>(map, map.phi2(en2));
Pierre Kraemer's avatar
Pierre Kraemer committed
431

432
433
434
435
436
	//Decouture de la seconde face
	en1 = map.phi1(map.phi2(en1));
	en2 = map.phi_1(map.phi2(en2));
	unlinkFromEdge<PFP>(map, map.phi2(en1));
	unlinkFromEdge<PFP>(map, map.phi2(en2));
Pierre Kraemer's avatar
Pierre Kraemer committed
437

438
439
440
441
442
	//Decouture de la troisieme face
	en1 = map.phi1(map.phi2(en1));
	en2 = map.phi_1(map.phi2(en2));
	unlinkFromEdge<PFP>(map, map.phi2(en1));
	unlinkFromEdge<PFP>(map, map.phi2(en2));
Pierre Kraemer's avatar
Pierre Kraemer committed
443

444
445
446
	//Faces interieurs
	Dart fi2 = map.phi2(map.phi1(fi));
	Dart fi3 = map.phi2(map.phi3(map.phi1(fi)));
Pierre Kraemer's avatar
Pierre Kraemer committed
447

448
449
450
	map.deleteFace(fi);
	map.deleteFace(fi2);
	map.deleteFace(fi3);
Pierre Kraemer's avatar
Pierre Kraemer committed
451

452
453
454
455
456
	    //Couture de cette face au milieu des 2 tetraedres
	    Dart f = map.newFace(3);
	    Dart fprim = map.newFace(3);
	    map.sewVolumes(f,fprim);
	//     Dart en = linkIntoEdge(f,d);
Pierre Kraemer's avatar
Pierre Kraemer committed
457

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
	    Dart ff=f;
	    Dart en= d;
	    do {
	        Dart e2 = map.phi2(en);
	        Dart d3 = map.phi3(ff);
	        map.unsewFaces(en);
	        map.sewFaces(e2,d3);
	        map.sewFaces(en,ff);

	        en = map.phi_1(map.phi2(map.phi_1(en)));

	        ff = map.phi1(ff);
	    } while(ff!=f);


		Dart dd = d;

		do {
			Dart e = map.phi2(map.phi3(map.phi2(dd)));

478
479
			unsigned int eVEmb = map.getEmbedding<VERTEX>(e) ;
			unsigned int ddVEmb = map.getEmbedding<VERTEX>(dd) ;
480

Pierre Kraemer's avatar
Pierre Kraemer committed
481
482
			map.setDartEmbedding<VERTEX>(map.phi2(dd), eVEmb) ;
			map.setDartEmbedding<VERTEX>(map.phi2(e), ddVEmb) ;
483
484
485

			dd = map.phi1(map.phi2(map.phi1(dd)));
		} while( dd!=d);
Pierre Kraemer's avatar
Pierre Kraemer committed
486
487
488
489
490
491

}

//[precond] le brin doit venir d'une face partagé par 2 tetraèdres
// renvoie un brin de l'ancienne couture entre les 2 tetras qui est devenu une arête
template <typename PFP>
492
Dart swap2To3(typename PFP::MAP& map, Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
493
{
494
495
	Dart e = map.phi1(map.phi2(map.phi3(d)));

496
497
	unsigned int p1 = map.getEmbedding<VERTEX>(map.phi_1(map.phi2(d))) ;
	unsigned int p2 = map.getEmbedding<VERTEX>(map.phi2(map.phi1(map.phi2(map.phi3(d))))) ;
498
499
500
501
502
503
504
505
506
507

	//détachement des demi-faces du milieu
	//garde la relation volumique qui les lies
	//relie les faces de bords des 2 tetraèdres
	//renvoie le brin de base d'un des 2 tétraèdres
	unlinkFace<PFP>(map, d);

	//Couture de la premiere face
	Dart en1 = linkIntoEdge<PFP>(map,d,e);
	Dart en2 = linkIntoEdge<PFP>(map, map.phi1(d), map.phi_1(map.phi2(map.phi_1(e))));
Pierre Kraemer's avatar
Pierre Kraemer committed
508
509
	map.setDartEmbedding<VERTEX>(map.phi_1(d), p1);
	map.setDartEmbedding<VERTEX>(map.phi1(map.phi3(d)), p2);
510
511
512
513
514
515
516
517
518
519
520

	///Couture de la seconde face
	en1 = map.phi1(map.phi1(en1));
	en2 = map.phi_1(map.phi_1(en2));

	Dart f1 = map.newFace(3);
	Dart f1prim = map.newFace(3);
	map.sewVolumes(f1,f1prim);
	en1 = linkIntoEdge<PFP>(map, f1,en1);
	en2 = linkIntoEdge<PFP>(map, map.phi1(f1),en2);

Pierre Kraemer's avatar
Pierre Kraemer committed
521
522
	map.setDartEmbedding<VERTEX>(map.phi_1(f1), p1);
	map.setDartEmbedding<VERTEX>(map.phi1(map.phi3(f1)), p2);
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538

	///Couture de la troisieme face
	en1 = map.phi1(map.phi1(en1));
	en2 = map.phi_1(map.phi_1(en2));

	Dart f2 = map.newFace(3);
	Dart f2prim = map.newFace(3);
	map.sewVolumes(f2,f2prim);
	en1 = linkIntoEdge<PFP>(map, f2,en1);
	en2 = linkIntoEdge<PFP>(map, map.phi1(f2),en2);

	//couture des 3 faces du milieu
	map.sewFaces(map.phi_1(d), map.phi1(map.phi3(f2)));
	map.sewFaces(map.phi1(map.phi3(d)), map.phi_1(f1));
	map.sewFaces(map.phi1(map.phi3(f1)), map.phi_1(f2));

Pierre Kraemer's avatar
Pierre Kraemer committed
539
540
	map.setDartEmbedding<VERTEX>(map.phi_1(f2), p1);
	map.setDartEmbedding<VERTEX>(map.phi1(map.phi3(f2)), p2);
541

542
	return map.phi_1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
543
544
545
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
546
void swap5To4(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3>& positions)
Pierre Kraemer's avatar
Pierre Kraemer committed
547
548
549
{


550
551
552
}

/************************************************************************************************
553
 *							Flip Functions 																	   *
554
555
556
 ************************************************************************************************/

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
557
void flip1To4(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3>& position)
558
{
untereiner's avatar
untereiner committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//	typedef typename PFP::TVEC3 TVEC3;
//	typedef typename PFP::VEC3 VEC3;
//
//
//	//parcourir le tetra est sauvegarder un brin de chaque face + calcul du centroid
//	VEC3 volCenter;
//	unsigned count = 0 ;
//
//	DartMarkerStore mf(map);		// Lock a face marker to save one dart per face
//	DartMarkerStore mv(map);		// Lock a vertex marker to compute volume center
//
//	std::vector<Dart> visitedFaces;
//	visitedFaces.reserve(4);
//	visitedFaces.push_back(d);
//
Pierre Kraemer's avatar
Pierre Kraemer committed
574
//	mf.markOrbit<FACE>(d) ;
untereiner's avatar
untereiner committed
575
576
577
578
579
580
581
582
583
584
585
586
//
//	//TODO diminuer complexite avec boucle specifique aux tetras
//	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
//	{
//		Dart e = visitedFaces[i] ;
//		do
//		{
//			//compute volume centroid
//			if(!mv.isMarked(e))
//			{
//				volCenter += position[e];
//				++count;
Pierre Kraemer's avatar
Pierre Kraemer committed
587
//				mv.markOrbit<VERTEX>(e);
untereiner's avatar
untereiner committed
588
589
590
591
592
593
594
//			}
//
//			// add all face neighbours to the table
//			Dart ee = map.phi2(e) ;
//			if(!mf.isMarked(ee)) // not already marked
//			{
//				visitedFaces.push_back(ee) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
595
//				mf.markOrbit<FACE>(ee) ;
untereiner's avatar
untereiner committed
596
597
598
599
600
601
602
603
604
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
634
635
//			}
//
//			e = map.phi1(e) ;
//		} while(e != visitedFaces[i]) ;
//	}
//
//	volCenter /= typename PFP::REAL(count) ;
//
//	//store the new faces to 3-sew
//	std::vector<std::pair<Dart,Dart> > nFaces;
//	nFaces.reserve(6);
//
//	//triangule chaque face avec plongement au centroid
//	for (std::vector<Dart>::iterator face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
//	{
//		// on decoud et on ferme le trou
//		Dart temp = *face;
//		do
//		{
//			nFaces.push_back(std::pair<Dart,Dart>(temp, map.phi2(temp)));
//			map.unsewFaces(temp);
//			temp = map.phi1(temp);
//		}
//		while(temp != *face);
//
//		map.closeHole(*face);
//
//		Dart fi = map.phi2(*face);
//
//		Dart cd = Algo::Modelisation::trianguleFace<PFP>(map, fi);
//		position[cd] = volCenter;
//	}
//
//	//coudre les nouveaux brins entre eux par phi3
//	for (std::vector<std::pair<Dart,Dart> >::iterator face =nFaces.begin(); face != nFaces.end(); ++face)
//	{
//
//		if(map.phi3(map.phi2((*face).first)) == map.phi2((*face).first))
//			map.sewVolumes(map.phi2((*face).first), map.phi2((*face).second));
//	}
636
637
638
}

/************************************************************************************************
Pierre Kraemer's avatar
Pierre Kraemer committed
639
 *                 Bisection Functions                                                          *
640
 ************************************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
641

642
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
643
void edgeBisection(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3>& position)
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
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
{
	//coupe l'arete en 2
	Dart f = map.phi1(d);
	map.cutEdge(d);
	Dart e = map.phi1(d);
	position[e] = position[d];
	position[e] += position[f];
	position[e] *= 0.5;


	//split de la face de d
	map.splitFace(map.phi_1(d), map.phi1(d));

	//alpha2(d)
	Dart dd = map.alpha2(d);

	//si alpha2 == d
	if(dd == d)
	{
		map.splitFace(map.phi2(d), map.phi1(map.phi1(map.phi2(d))));
		std::cout << "dd == d" << std::endl;
	}
	else
	{
		Dart prev = d;

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

			std::cout << "plop" << std::endl;
			map.splitFace(map.phi_1(prev), map.phi1(prev));
		}

		if(map.phi3(map.phi2(dd)) == map.phi2(dd))
		{
			map.splitFace(map.phi2(dd), map.phi1(map.phi1(map.phi2(dd))));
		}

	}

	Dart temp = d;
	do{


		//insertion de la face
		//decouture des 2 bouts
		Dart etemp = map.phi2(map.phi1(temp));

		map.unsewFaces(map.phi1(temp));
		map.unsewFaces(map.phi_1(map.phi2(temp)));
		map.unsewFaces(map.phi1(map.phi2(map.phi_1(temp))));

		//fermture des 2 trous
		map.closeHole(map.phi1(temp));
		map.closeHole(etemp);

		//recouture par phi3
		map.sewVolumes(map.phi2(map.phi1(temp)), map.phi2(etemp));


		temp = map.alpha2(temp);
	}
	while(temp != d);

//	if(map.phi3(d) == d)
//	{
//		map.splitFace(map.phi_1(d), map.phi1(d));
//		d = map.phi2(d);
//	}
Pierre Kraemer's avatar
Pierre Kraemer committed
715
716
//
//
717
718
//	Dart prev = d;
//	Dart dd = map.alpha2(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
719
//
720
721
//	//
//	map.splitFace(d, map.phi1(map.phi1(d)));
Pierre Kraemer's avatar
Pierre Kraemer committed
722
//
723
724
//	//si phi3(d) != d
//	map.splitFace(map.phi_1(d), map.phi1(d));
Pierre Kraemer's avatar
Pierre Kraemer committed
725
//
726
//	//map.splitFace(map.phi2(d), map.phi1(map.phi1(map.phi2(d))));
Pierre Kraemer's avatar
Pierre Kraemer committed
727
//
728
729
//	//map.Map2::splitFace(map.phi_1(d), map.phi1(d));
//	//map.Map2::splitFace(map.phi2(d), map.phi1(map.phi1(map.phi2(d))));
Pierre Kraemer's avatar
Pierre Kraemer committed
730
//
731
732
733
734
//	//		if(map.phi3(d) == d)
//	//		{
//	//			map.splitFace(map.phi2(d), map.phi1(map.phi1(map.phi2(d))));
//	//		}
Pierre Kraemer's avatar
Pierre Kraemer committed
735
//
736
737
738
739
//	while (dd!=d)
//	{
//		prev = dd;
//		dd = map.alpha2(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
740
//
741
742
//		map.splitFace(map.phi_1(prev), map.phi1(prev));
//	}
Pierre Kraemer's avatar
Pierre Kraemer committed
743
744
745



746
//	DartMarkerStore mf(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
747
748
//
//
749
750
751
752
753
754
755
756
757
758
//	//tout autour de l'arete
//	Dart temp = d;
//	do{
//
//		if(!mf.isMarked(temp))
//		{
//			//split les 2 faces le long
//			map.splitFace(map.phi_1(temp), map.phi1(temp));
//			map.splitFace(map.phi2(temp), map.phi1(map.phi1(map.phi2(temp))));
//
Pierre Kraemer's avatar
Pierre Kraemer committed
759
760
//			mf.markOrbit<FACE>(temp);
//			mf.markOrbit<FACE>(map.phi2(temp));
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//		}
//			//insertion de la face
//			//decouture des 2 bouts
//			Dart etemp = map.phi2(map.phi1(temp));
//
//			map.unsewFaces(map.phi1(temp));
//			map.unsewFaces(map.phi_1(map.phi2(temp)));
//			map.unsewFaces(map.phi1(map.phi2(map.phi_1(temp))));
//
//			//fermture des 2 trous
//			map.closeHole(map.phi1(temp));
//			map.closeHole(etemp);
//
//			//recouture par phi3
//			map.sewVolumes(map.phi2(map.phi1(temp)), map.phi2(etemp));
//
//
//
//		temp = map.alpha2(temp);
//	}
//	while(temp != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
782
783
}

Pierre Kraemer's avatar
Pierre Kraemer committed
784

untereiner's avatar
untereiner committed
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
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
922
923
924
925
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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
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
1016
1017
1018
1019
1020
1021
1022
1023
///**
// * create a tetra based on the two triangles that have a common dart and phi2(dart)
// * return a new dart inside the tetra
// */
//template<typename PFP>
//Dart extractTetra(typename PFP::MAP& the_map, Dart d)
//{
//
//
//	Dart e = the_map.phi2(d);
//
//	//create the new faces
//	Dart dd = the_map.newFace(3);
//	Dart ee = the_map.newFace(3);
//
//	//update their sew
//	the_map.sewFaces(dd,ee);
//	the_map.sewFaces(the_map.phi3(dd),the_map.phi3(ee));
//
//	//add the two new faces in the mesh to obtain a tetra
//	Dart s2d = the_map.phi2(the_map.phi_1(d));
//	the_map.unsewFaces(the_map.phi_1(d));
//	the_map.sewFaces(the_map.phi_1(d),the_map.phi_1(dd));
//	the_map.sewFaces(s2d,the_map.phi3(the_map.phi_1(dd)));
//
//	Dart s2e = the_map.phi2(the_map.phi_1(e));
//	the_map.unsewFaces(the_map.phi_1(e));
//	the_map.sewFaces(the_map.phi_1(e),the_map.phi_1(ee));
//	the_map.sewFaces(s2e,the_map.phi3(the_map.phi_1(ee)));
//
//	Dart ss2d = the_map.phi2(the_map.phi1(d));
//	the_map.unsewFaces(the_map.phi1(d));
//	the_map.sewFaces(the_map.phi1(d),the_map.phi1(ee));
//	the_map.sewFaces(ss2d,the_map.phi3(the_map.phi1(ee)));
//
//	Dart ss2e = the_map.phi2(the_map.phi1(e));
//	the_map.unsewFaces(the_map.phi1(e));
//	the_map.sewFaces(the_map.phi1(e),the_map.phi1(dd));
//	the_map.sewFaces(ss2e,the_map.phi3(the_map.phi1(dd)));
//
//	//embed the coords
//	the_map.setVertexEmb(d,the_map.getVertexEmb(d));
//	the_map.setVertexEmb(e,the_map.getVertexEmb(e));
//	the_map.setVertexEmb(the_map.phi_1(d),the_map.getVertexEmb(the_map.phi_1(d)));
//	the_map.setVertexEmb(the_map.phi_1(e),the_map.getVertexEmb(the_map.phi_1(e)));
//
//	return dd;
//}
//
///**
// * tetrahedrization of the volume
// * @param the map
// * @param a dart of the volume
// * @param true if the faces are in CCW order
// * @return success of the tetrahedrization
// */
//template<typename PFP>
//bool smartVolumeTetrahedrization(typename PFP::MAP& the_map, Dart d, bool CCW=true)
//{
//
//	typedef typename PFP::EMB EMB;
//
//	bool ret=true;
//
//	if (!the_map.isTetrahedron(d))
//	{
//		//only works on a 3-map
//		assert(Dart::nbInvolutions()>=2 || "cannot be applied on this map, nbInvolutions must be at least 2");
//
//		if (Geometry::isConvex<PFP>(the_map,d,CCW))
//		{
//			the_map.tetrahedrizeVolume(d);
//		}
//		else
//		{
//
//			//get all the dart of the volume
//			std::vector<Dart> vStore;
//			FunctorStore fs(vStore);
//			the_map.foreach_dart_of_volume(d,fs);
//
//			if (vStore.size()==0)
//			{
//				if (the_map.phi1(d)==d)
//					CGoGNout << "plop" << CGoGNendl;
//				if (the_map.phi2(d)==d)
//					CGoGNout << "plip" << CGoGNendl;
//
//				CGoGNout << the_map.getVertexEmb(d)->getPosition() << CGoGNendl;
//				CGoGNout << "tiens tiens, c'est etrange" << CGoGNendl;
//			}
//			//prepare the list of embeddings of the current volume
//			std::vector<EMB *> lstEmb;
//
//			//get a marker
//			DartMarker m(the_map);
//
//			//all the darts from a vertex that can generate a tetra (3 adjacent faces)
//			std::vector<Dart> allowTetra;
//
//			//all the darts that are not in otherTetra
//			std::vector<Dart> otherTetra;
//
//			//for each dart of the volume
//			for (typename std::vector<Dart>::iterator it = vStore.begin() ; it != vStore.end() ; ++it )
//			{
//				Dart e = *it;
//				//if the vertex is not treated
//				if (!m.isMarked(e))
//				{
//					//store the embedding
//					lstEmb.push_back(reinterpret_cast<EMB*>(the_map.getVertexEmb(e)));
//					Dart ee=e;
//
//					//count the number of adjacent faces and mark the darts
//					int nbe=0;
//					do
//					{
//						nbe++;
//						m.markOrbit(DART,e);
//						ee=the_map.phi1(the_map.phi2(ee));
//					}
//					while (ee!=e);
//
//					//if 3 adjacents faces, we can create a tetra on this vertex
//					if (nbe==3)
//						allowTetra.push_back(e);
//					else
//						otherTetra.push_back(e);
//				}
//			}
//
//			//we haven't created a tetra yet
//			bool decoupe=false;
//
//			//if we have vertex that can be base
//			if (allowTetra.size()!=0)
//			{
//				//foreach possible vertex while we haven't done any cut
//				for (typename std::vector<Dart>::iterator it=allowTetra.begin();it!=allowTetra.end() && !decoupe ;++it)
//				{
//					//get the dart
//					Dart s=*it;
//					//store the emb
//					std::vector<EMB*> lstCurEmb;
//					lstCurEmb.push_back(reinterpret_cast<EMB*>(the_map.getVertexEmb(s)));
//					lstCurEmb.push_back(reinterpret_cast<EMB*>(the_map.getVertexEmb(the_map.phi1(s))));
//					lstCurEmb.push_back(reinterpret_cast<EMB*>(the_map.getVertexEmb(the_map.phi_1(s))));
//					lstCurEmb.push_back(reinterpret_cast<EMB*>(the_map.getVertexEmb(the_map.phi_1(the_map.phi2(s)))));
//
//					//store the coords of the point
//					gmtl::Vec3f points[4];
//					for (int i=0;i<4;++i)
//					{
//						points[i] = lstCurEmb[i]->getPosition();
//					}
//
//					//test if the future tetra is well oriented (concave case)
//					if (Geometry::isTetrahedronWellOriented(points,CCW))
//					{
//						//test if we haven't any point inside the future tetra
//						bool isEmpty=true;
//						for (typename std::vector<EMB *>::iterator iter = lstEmb.begin() ; iter != lstEmb.end() && isEmpty ; ++iter)
//						{
//							//we don't test the vertex that composes the new tetra
//							if (std::find(lstCurEmb.begin(),lstCurEmb.end(),*iter)==lstCurEmb.end())
//							{
//								isEmpty = !Geometry::isPointInTetrahedron(points, (*iter)->getPosition(), CCW);
//							}
//						}
//
//						//if no point inside the new tetra
//						if (isEmpty)
//						{
//							//cut the spike to make a tet
//							Dart dRes = the_map.cutSpike(*it);
//							decoupe=true;
//							//and continue with the rest of the volume
//							ret = ret && smartVolumeTetrahedrization<PFP>(the_map,the_map.phi3(dRes),CCW);
//						}
//					}
//				}
//			}
//
//			if (!decoupe)
//			{
//				//foreach other vertex while we haven't done any cut
//				for (typename std::vector<Dart>::iterator it=otherTetra.begin();it!=otherTetra.end() && !decoupe ;++it)
//				{
//					//get the dart
//					Dart s=*it;
//					//store the emb
//					std::vector<EMB*> lstCurEmb;
//					lstCurEmb.push_back(reinterpret_cast<EMB*>(the_map.getVertexEmb(s)));
//					lstCurEmb.push_back(reinterpret_cast<EMB*>(the_map.getVertexEmb(the_map.phi1(s))));
//					lstCurEmb.push_back(reinterpret_cast<EMB*>(the_map.getVertexEmb(the_map.phi_1(s))));
//					lstCurEmb.push_back(reinterpret_cast<EMB*>(the_map.getVertexEmb(the_map.phi_1(the_map.phi2(s)))));
//
//					//store the coords of the point
//					gmtl::Vec3f points[4];
//					for (int i=0;i<4;++i)
//					{
//						points[i] = lstCurEmb[i]->getPosition();
//					}
//
//					//test if the future tetra is well oriented (concave case)
//					if (Geometry::isTetrahedronWellOriented(points,CCW))
//					{
//						//test if we haven't any point inside the future tetra
//						bool isEmpty=true;
//						for (typename std::vector<EMB *>::iterator iter = lstEmb.begin() ; iter != lstEmb.end() && isEmpty ; ++iter)
//						{
//							//we don't test the vertex that composes the new tetra
//							if (std::find(lstCurEmb.begin(),lstCurEmb.end(),*iter)==lstCurEmb.end())
//							{
//								isEmpty = !Geometry::isPointInTetrahedron(points, (*iter)->getPosition(), CCW);
//							}
//						}
//
//						//if no point inside the new tetra
//						if (isEmpty)
//						{
//							//cut the spike to make a tet
//							Dart dRes = extractTetra<PFP>(the_map,*it);
//							decoupe=true;
//							//and continue with the rest of the volume
//							smartVolumeTetrahedrization<PFP>(the_map,the_map.phi3(dRes),CCW);
//						}
//					}
//				}
//			}
//
//			if (!decoupe)
//				ret=false;
//		}
//	}
//	return ret;
//}

Pierre Kraemer's avatar
Pierre Kraemer committed
1024
} // namespace Tetrahedralization
untereiner's avatar
untereiner committed
1025

Pierre Kraemer's avatar
Pierre Kraemer committed
1026
} // namespace Modelisation
Pierre Kraemer's avatar
Pierre Kraemer committed
1027

Pierre Kraemer's avatar
Pierre Kraemer committed
1028
} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
1029

Pierre Kraemer's avatar
Pierre Kraemer committed
1030
} // namespace CGoGN