subdivision3.hpp 82.7 KB
Newer Older
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
21
22
23
24
25
26
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Geometry/centroid.h"
#include "Algo/Modelisation/subdivision.h"
27
#include "Algo/Modelisation/extrusion.h"
28
29
30
31
32
33
34

namespace CGoGN
{

namespace Algo
{

35
36
37
namespace Volume
{

untereiner's avatar
untereiner committed
38
namespace IHM
39
40
{

untereiner's avatar
untereiner committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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


template <typename PFP>
void newLevelHexa(typename PFP::MAP& map, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
{
//    DartMarkerStore m(map);

//    unsigned int max = map.getMaxLevel() ;
//    map.setCurrentLevel(max) ;

//    for(Dart d = map.begin() ; d != map.end() ; map.next(d))
//    {
//        if(!map.isBoundaryMarked3(d))
//        {
//            if(!m.isMarked(d))
//            {
//                if(map.isDartValid(d) && map.getDartLevel(d) <= max && !map.volumeIsSubdivided(d))
//                {
//                    Dart dres = Algo::Volume::IHM::subdivideVolumeClassic<PFP>(map, d, position);
//                    //Algo::IHM::subdivideLoop<PFP>(map, d, position);
//                    Traversor3VW<typename PFP::MAP> tew(map, dres);
//                    for(Dart dit2 = tew.begin(); dit2 != tew.end(); dit2 = tew.next())
//                    {
//                        m.markOrbit<VOLUME>(dit2);
//                    }

//                }
//            }
//        }
//    }





    unsigned int cur = map.getCurrentLevel();
    map.setCurrentLevel(map.getMaxLevel()) ;

    TraversorE<typename PFP::MAP> travE(map);
    for (Dart d = travE.begin(); d != travE.end(); d = travE.next())
    {
        Dart dd = map.phi2(d) ;
        typename PFP::VEC3 p1 = position[d] ;
        typename PFP::VEC3 p2 = position[map.phi1(d)] ;

        map.cutEdge(d) ;
        travE.skip(d) ;
        travE.skip(map.phi1(d)) ;

        unsigned int eId = map.getEdgeId(d) ;
        map.setEdgeId(map.phi1(d), eId, EDGE) ; //mise a jour de l'id d'arrete sur chaque moitie d'arete
        map.setEdgeId(map.phi1(dd), eId, EDGE) ;

        map.setFaceId(EDGE, d) ; //mise a jour de l'id de face sur chaque brin de chaque moitie d'arete
        map.setFaceId(EDGE, dd) ;

        position[map.phi1(d)] = (p1 + p2) * typename PFP::REAL(0.5) ;
    }

    TraversorF<typename PFP::MAP> travF(map) ;
    for (Dart d = travF.begin(); d != travF.end(); d = travF.next())
    {
        Dart old = d;

        if(map.getDartLevel(old) < map.getMaxLevel())
            old = map.phi1(old) ;

        typename PFP::VEC3 p = position[old] ;
        unsigned int count = 1;

        Dart dd = map.phi1(old) ;
        map.splitFace(dd, map.phi1(map.phi1(dd))) ;

        Dart ne = map.phi2(map.phi_1(dd));
        Dart ne2 = map.phi2(ne);

        map.cutEdge(ne) ;
        travF.skip(dd) ;
        unsigned int id = map.getNewEdgeId() ;
        map.setEdgeId(ne, id, EDGE) ;
        id = map.getNewEdgeId() ;
        map.setEdgeId(ne2, id, EDGE) ;

        dd = map.phi1(map.phi1(map.phi1(map.phi1(ne)))) ;
        while(dd != ne)
        {
            p += position[map.phi_1(dd)];
            count++;

            Dart tmp = map.phi1(ne) ;
            Dart next = map.phi1(map.phi1(dd)) ;

            map.splitFace(map.phi1(ne), dd) ;
            travF.skip(tmp) ;
            Dart nne = map.phi2(map.phi_1(dd)) ;

            id = map.getNewEdgeId() ;
            map.setEdgeId(nne, id, EDGE) ;

            dd = next ;
        }
        travF.skip(ne) ;

        p += position[map.phi_1(ne)];
        count++;
        std::cout << "count  = " << count << std::endl;

        unsigned int idface = map.getFaceId(old);
        //Dart e = dd;
        do
        {
            map.setFaceId(dd, idface, DART) ;
            map.setFaceId(map.phi2(dd), idface, DART) ;
            dd = map.phi2(map.phi1(dd));
        }
        while(dd != ne);

        p /= count;

        position[map.phi1(ne)] = p ;
    }

    TraversorW<typename PFP::MAP> traW(map);
    for(Dart dit = traW.begin(); dit != traW.end(); dit = traW.next())
    {
//        DartMarkerStore mf(map);		// Lock a face marker to save one dart per face
//        DartMarkerStore mv(map);

//        typename PFP::VEC3 volCenter;
//        unsigned count = 0 ;

//        //Store faces that are traversed and start with the face of d
//        std::vector<Dart> visitedFaces;
//        visitedFaces.reserve(200);
//        visitedFaces.push_back(old);

//        //Store the edges before the cutEdge
//        std::vector<Dart> oldEdges;
//        oldEdges.reserve(20);

//        mf.markOrbit<FACE>(old) ;

//        for(unsigned int i = 0; i < visitedFaces.size(); ++i)
//        {
//            Dart e = visitedFaces[i] ;
//            do
//            {
//                //add one old edge per vertex to the old edge list
//                //compute volume centroid
//                if(!mv.isMarked(e))
//                {
//                    mv.markOrbit<VERTEX>(e);
//                    volCenter += position[e];
//                    ++count;
//                    oldEdges.push_back(e);
//                }

//                // add all face neighbours to the table
//                Dart ee = map.phi2(e) ;
//                if(!mf.isMarked(ee)) // not already marked
//                {
//                    visitedFaces.push_back(ee) ;
//                    mf.markOrbit<FACE>(ee) ;
//                }

//                e = map.phi1(e) ;
//            } while(e != visitedFaces[i]) ;
//        }

//        volCenter /= typename PFP::REAL(count) ;

//        /*
//         * Subdivision
//         */

//        //Store the darts from quadrangulated faces
//        std::vector<std::pair<Dart,Dart> > subdividedfaces;
//        subdividedfaces.reserve(25);

//        //First step : subdivide edges and faces
//        //creates a i+1 edge level and i+1 face level
//        for (std::vector<Dart>::iterator face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
//        {
//            Dart d = *face;

//            //if needed subdivide face
//            if(!map.faceIsSubdivided(d))
//                IHM::subdivideFace<PFP>(map, d, position, IHM::S_QUAD);

//            //save a dart from the subdivided face
//            unsigned int cur = map.getCurrentLevel() ;

//            unsigned int fLevel = map.faceLevel(d) + 1; //puisque dans tous les cas, la face est subdivisee
//            map.setCurrentLevel(fLevel) ;

//            //le brin est forcement du niveau cur
//            Dart cf = map.phi1(d);
//            Dart e = cf;
//            do
//            {
//                subdividedfaces.push_back(std::pair<Dart,Dart>(e,map.phi2(e)));
//                e = map.phi2(map.phi1(e));
//            }while (e != cf);

//            map.setCurrentLevel(cur);
//        }

//        map.setCurrentLevel(vLevel + 1) ; // go to the next level to perform volume subdivision

//        std::vector<Dart> newEdges;	//save darts from inner edges
//        newEdges.reserve(50);

//        Dart centralDart = NIL;
//        //Second step : deconnect each corner, close each hole, subdivide each new face into 3
//        for (std::vector<Dart>::iterator edge = oldEdges.begin(); edge != oldEdges.end(); ++edge)
//        {
//            Dart e = *edge;

//            std::vector<Dart> v ;

//            do
//            {
//                v.push_back(map.phi1(e));
//                v.push_back(map.phi1(map.phi1(e)));

//                e = map.phi2(map.phi_1(e));
//            }
//            while(e != *edge);

//            map.splitVolume(v) ;

//            Dart old = map.phi2(map.phi1(*edge));
//            Dart dd = map.phi1(map.phi1(old)) ;
//            map.splitFace(old,dd) ;

//            unsigned int idface = map.getNewFaceId();
//            map.setFaceId(dd,idface, FACE);

//            Dart ne = map.phi1(map.phi1(old)) ;

//            map.cutEdge(ne);
//            position[map.phi1(ne)] = volCenter; //plonger a la fin de la boucle ????
//            centralDart = map.phi1(ne);
//            newEdges.push_back(ne);
//            newEdges.push_back(map.phi1(ne));

//            unsigned int id = map.getNewEdgeId() ;
//            map.setEdgeId(ne, id, EDGE) ;

//            Dart stop = map.phi2(map.phi1(ne));
//            ne = map.phi2(ne);
//            do
//            {
//                dd = map.phi1(map.phi1(map.phi1(ne)));

//                map.splitFace(ne, dd) ;
//                unsigned int idface = map.getNewFaceId();
//                map.setFaceId(dd,idface, FACE);

//                newEdges.push_back(map.phi1(dd));

//                ne = map.phi2(map.phi_1(ne));
//                dd = map.phi1(map.phi1(dd));
//            }
//            while(dd != stop);

//            //map.computeVertexVertexFunctor(e);
//        }


//         map.deleteVolume(map.phi3(map.phi2(map.phi1(oldEdges.front()))));

//        //Third step : 3-sew internal faces
//        for (std::vector<std::pair<Dart,Dart> >::iterator it = subdividedfaces.begin(); it != subdividedfaces.end(); ++it)
//        {
//            Dart f1 = (*it).first;
//            Dart f2 = (*it).second;

//            if(map.isBoundaryFace(map.phi2(f1)) && map.isBoundaryFace(map.phi2(f2)))
//            {
//                //id pour toutes les faces interieures
//                map.sewVolumes(map.phi2(f1), map.phi2(f2));

//                //Fais a la couture !!!!!
//                unsigned int idface = map.getNewFaceId();
//                map.setFaceId(map.phi2(f1),idface, FACE);
//            }

//            //FAIS a la couture !!!!!!!
//            //id pour toutes les aretes exterieurs des faces quadrangulees
//            unsigned int idedge = map.getEdgeId(f1);
//            map.setEdgeId(map.phi2(f1), idedge, DART);
//            map.setEdgeId( map.phi2(f2), idedge, DART);
//        }

//        //LA copie de L'id est a gerer avec le sewVolumes normalement !!!!!!
//        //id pour les aretes interieurs : (i.e. 6 pour un hexa)
//        DartMarkerStore mne(map);
//        for(unsigned int i = 0; i < newEdges.size(); ++i)
//        {
//            if(!mne.isMarked(newEdges[i]))
//            {
//                unsigned int idedge = map.getNewEdgeId();
//                map.setEdgeId(newEdges[i], idedge, EDGE);
//                mne.markOrbit<EDGE>(newEdges[i]);
//            }
//        }

//    //	std::cout << map.getCurrentLevel() << std::endl;
//    //	//Second step : deconnect each corner, close each hole, subdivide each new face into 3
//        for (std::vector<Dart>::iterator edge = oldEdges.begin(); edge != oldEdges.end(); ++edge)
//        {
//            Dart e = *edge;
//            if(e == 309)
//                std::cout << "plop" << std::endl;
//            //std::cout << "emb ? " << map.template getEmbedding<VERTEX>(e) << std::endl;
//            //if(map.template getEmbedding<VERTEX>(e) == EMBNULL)
//            map.computeVertexVertexFunctor(e);
//            //std::cout << "emb = " << map.template getEmbedding<VERTEX>(e) << " / dartlevel = " <<  map.getDartLevel(e) << std::endl;
//        }
//        std::cout << std::endl;

//        //map.computerVolumeVertexFunctor(centralDart);

//        map.setCurrentLevel(cur) ;

    }


    map.setCurrentLevel(cur);
}


375
template <typename PFP>
376
void subdivideEdge(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
377
378
379
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(!map.edgeIsSubdivided(d) || !"Trying to subdivide an already subdivided edge") ;
Thery Sylvain's avatar
Thery Sylvain committed
380
	assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");
381
382
383
384
385
386
387
388
389
390
391
392
393
394

	unsigned int eLevel = map.edgeLevel(d) ;

	unsigned int cur = map.getCurrentLevel() ;
	map.setCurrentLevel(eLevel) ;

	Dart dd = map.phi2(d) ;
	typename PFP::VEC3 p1 = position[d] ;
	typename PFP::VEC3 p2 = position[map.phi1(d)] ;

	map.setCurrentLevel(eLevel + 1) ;

	map.cutEdge(d) ;
	unsigned int eId = map.getEdgeId(d) ;
395
396
	map.setEdgeId(map.phi1(d), eId, EDGE) ; //mise a jour de l'id d'arrete sur chaque moitie d'arete
	map.setEdgeId(map.phi1(dd), eId, EDGE) ;
397

398
399
	map.setFaceId(EDGE, d) ; //mise a jour de l'id de face sur chaque brin de chaque moitie d'arete
	map.setFaceId(EDGE, dd) ;
400

401
402
    position[map.phi1(d)] = (p1 + p2) * typename PFP::REAL(0.5) ;
    //map.computeEdgeVertexFunctor(map.phi1(d));
403

404
405
406
407
	map.setCurrentLevel(cur) ;
}

template <typename PFP>
408
void subdivideFace(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position, SubdivideType sType)
409
410
411
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(!map.faceIsSubdivided(d) || !"Trying to subdivide an already subdivided face") ;
Thery Sylvain's avatar
Thery Sylvain committed
412
	assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");
413
414
415
416
417
418
419

	unsigned int fLevel = map.faceLevel(d) ;
	Dart old = map.faceOldestDart(d) ;

	unsigned int cur = map.getCurrentLevel() ;
	map.setCurrentLevel(fLevel) ;		// go to the level of the face to subdivide its edges

untereiner's avatar
untereiner committed
420
421
	unsigned int vLevel = map.volumeLevel(old);
	//one level of subdivision in the neighbordhood
422
423
424
425
//	Traversor3VW<typename PFP::MAP> trav3EW(map, old);
//	for(Dart dit = trav3EW.begin() ; dit != trav3EW.end() ; dit = trav3EW.next())
//	{
//		Dart oldit = map.volumeOldestDart(dit);
untereiner's avatar
untereiner committed
426
//
untereiner's avatar
untereiner committed
427
428
429
//		//std::cout << "vLevel courant = " << map.volumeLevel(oldit) << std::endl;
//
//		if(((vLevel+1) - map.volumeLevel(oldit)) > 1)
430
//				IHM::subdivideVolumeClassic<PFP>(map, oldit, position);
431
//	}
untereiner's avatar
untereiner committed
432

433
434
	unsigned int degree = 0 ;
	typename PFP::VEC3 p ;
untereiner's avatar
untereiner committed
435
436
	Traversor2FE<typename PFP::MAP>  travE(map, old);
	for(Dart it = travE.begin(); it != travE.end() ; it = travE.next())
437
438
439
	{
		++degree;
		p += position[it] ;
untereiner's avatar
untereiner committed
440

441
		if(!map.edgeIsSubdivided(it))							// first cut the edges (if they are not already)
442
			IHM::subdivideEdge<PFP>(map, it, position) ;	// and compute the degree of the face
untereiner's avatar
untereiner committed
443
	}
444
445
	p /= typename PFP::REAL(degree) ;

untereiner's avatar
untereiner committed
446

447
448
	map.setCurrentLevel(fLevel + 1) ;			// go to the next level to perform face subdivision

449
	Dart res;
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
477
478
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
511
512
513
514
515
516
517
518
519
520
//	if(degree == 3 && sType == IHM::S_TRI)	//subdiviser une face triangulaire
//	{
//		Dart dd = map.phi1(old) ;
//		Dart e = map.phi1(map.phi1(dd)) ;
//		map.splitFace(dd, e) ;					// insert a new edge
//		unsigned int id = map.getNewEdgeId() ;
//		map.setEdgeId(map.phi_1(dd), id, EDGE) ;		// set the edge id of the inserted edge to the next available id

//		unsigned int idface = map.getFaceId(old);
//		map.setFaceId(dd, idface, FACE) ;
//		map.setFaceId(e, idface, FACE) ;

//		dd = e ;
//		e = map.phi1(map.phi1(dd)) ;
//		map.splitFace(dd, e) ;
//		id = map.getNewEdgeId() ;
//		map.setEdgeId(map.phi_1(dd), id, EDGE) ;

//		map.setFaceId(dd, idface, FACE) ;
//		map.setFaceId(e, idface, FACE) ;

//		dd = e ;
//		e = map.phi1(map.phi1(dd)) ;
//		map.splitFace(dd, e) ;
//		id = map.getNewEdgeId() ;
//		map.setEdgeId(map.phi_1(dd), id, EDGE) ;

//		map.setFaceId(dd, idface, FACE) ;
//		map.setFaceId(e, idface, FACE) ;

//		Dart stop = map.phi2(map.phi1(old));
//		Dart dit = stop;
//		do
//		{
//			unsigned int dId = map.getEdgeId(map.phi_1(map.phi2(dit)));
//			unsigned int eId = map.getEdgeId(map.phi1(map.phi2(dit)));

//			unsigned int t = dId + eId;

//			if(t == 0)
//			{
//				map.setEdgeId(dit, 1, EDGE) ;
//				map.setEdgeId(map.phi2(dit), 1, EDGE) ;
//			}
//			else if(t == 1)
//			{
//				map.setEdgeId(dit, 2, EDGE) ;
//				map.setEdgeId(map.phi2(dit), 2, EDGE) ;
//			}
//			else if(t == 2)
//			{
//				if(dId == eId)
//				{
//					map.setEdgeId(dit, 0, EDGE) ;
//					map.setEdgeId(map.phi2(dit), 0, EDGE) ;
//				}
//				else
//				{
//					map.setEdgeId(dit, 1, EDGE) ;
//					map.setEdgeId(map.phi2(dit), 1, EDGE) ;
//				}
//			}
//			else if(t == 3)
//			{
//				map.setEdgeId(dit, 0, EDGE) ;
//				map.setEdgeId(map.phi2(dit), 0, EDGE) ;
//			}

//			dit = map.phi1(dit);
//		}while(dit != stop);
untereiner's avatar
untereiner committed
521

522
523
524
//	}
//	else
//	{
525
526
527
528
529
		Dart dd = map.phi1(old) ;
		map.splitFace(dd, map.phi1(map.phi1(dd))) ;

		Dart ne = map.phi2(map.phi_1(dd));
		Dart ne2 = map.phi2(ne);
530

531
532
		map.cutEdge(ne) ;
		unsigned int id = map.getNewEdgeId() ;
533
		map.setEdgeId(ne, id, EDGE) ;
534
		id = map.getNewEdgeId() ;
535
		map.setEdgeId(ne2, id, EDGE) ;
536

537
        position[map.phi1(ne)] = p ;
538

539
540
541
542
543
544
		dd = map.phi1(map.phi1(map.phi1(map.phi1(ne)))) ;
		while(dd != ne)
		{
			Dart next = map.phi1(map.phi1(dd)) ;
			map.splitFace(map.phi1(ne), dd) ;
			Dart nne = map.phi2(map.phi_1(dd)) ;
545

546
			id = map.getNewEdgeId() ;
547
			map.setEdgeId(nne, id, EDGE) ;
548
549
550
551
552

			dd = next ;
		}

		unsigned int idface = map.getFaceId(old);
553
		//Dart e = dd;
554
555
		do
		{
556
557
			map.setFaceId(dd, idface, DART) ;
			map.setFaceId(map.phi2(dd), idface, DART) ;
558
559
560
			dd = map.phi2(map.phi1(dd));
		}
		while(dd != ne);
561

562
563
564
        //map.computeFaceVertexFunctor(map.phi1(ne));

//	}
565
566

	map.setCurrentLevel(cur) ;
567
568
}

untereiner's avatar
untereiner committed
569
template <typename PFP>
570
571
Dart subdivideVolumeClassic(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
//Dart subdivideVolumeClassic(typename PFP::MAP& map, Dart d, Algo::Volume::IHM::AttributeHandler_IHM<typename PFP::VEC3, VERTEX>& position)
untereiner's avatar
untereiner committed
572
573
574
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(!map.volumeIsSubdivided(d) || !"Trying to subdivide an already subdivided volume") ;
Thery Sylvain's avatar
Thery Sylvain committed
575
	assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");
untereiner's avatar
untereiner committed
576
577
578
579
580
581
582

	unsigned int vLevel = map.volumeLevel(d);
	Dart old = map.volumeOldestDart(d);

	unsigned int cur = map.getCurrentLevel();
	map.setCurrentLevel(vLevel);

untereiner's avatar
untereiner committed
583
584
585
586
587
588
	//	//one level of subdivision in the neighbordhood
	//	Traversor3VW<typename PFP::MAP> trav3EW(map, old);
	//	for(Dart dit = trav3EW.begin() ; dit != trav3EW.end() ; dit = trav3EW.next())
	//	{
	//		Dart oldit = map.volumeOldestDart(dit);
	//		if(((vLevel+1) - map.volumeLevel(oldit)) > 1)
589
	//			IHM::subdivideVolumeClassic<PFP>(map, oldit, position);
untereiner's avatar
untereiner committed
590
	//	}
untereiner's avatar
untereiner committed
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612

	/*
	 * au niveau du volume courant i
	 * stockage d'un brin de chaque face de celui-ci
	 * avec calcul du centroid
	 */

	DartMarkerStore mf(map);		// Lock a face marker to save one dart per face
	DartMarkerStore mv(map);

	typename PFP::VEC3 volCenter;
	unsigned count = 0 ;

	//Store faces that are traversed and start with the face of d
	std::vector<Dart> visitedFaces;
	visitedFaces.reserve(200);
	visitedFaces.push_back(old);

	//Store the edges before the cutEdge
	std::vector<Dart> oldEdges;
	oldEdges.reserve(20);

Pierre Kraemer's avatar
Pierre Kraemer committed
613
	mf.markOrbit<FACE>(old) ;
untereiner's avatar
untereiner committed
614

615
	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
untereiner's avatar
untereiner committed
616
	{
617
		Dart e = visitedFaces[i] ;
untereiner's avatar
untereiner committed
618
619
620
621
622
623
		do
		{
			//add one old edge per vertex to the old edge list
			//compute volume centroid
			if(!mv.isMarked(e))
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
624
				mv.markOrbit<VERTEX>(e);
untereiner's avatar
untereiner committed
625
626
627
628
629
630
631
632
633
634
				volCenter += position[e];
				++count;
				oldEdges.push_back(e);
			}

			// 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
635
				mf.markOrbit<FACE>(ee) ;
untereiner's avatar
untereiner committed
636
637
638
			}

			e = map.phi1(e) ;
639
		} while(e != visitedFaces[i]) ;
untereiner's avatar
untereiner committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
	}

	volCenter /= typename PFP::REAL(count) ;

	/*
	 * Subdivision
	 */

	//Store the darts from quadrangulated faces
	std::vector<std::pair<Dart,Dart> > subdividedfaces;
	subdividedfaces.reserve(25);

	//First step : subdivide edges and faces
	//creates a i+1 edge level and i+1 face level
	for (std::vector<Dart>::iterator face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
	{
		Dart d = *face;

		//if needed subdivide face
		if(!map.faceIsSubdivided(d))
660
			IHM::subdivideFace<PFP>(map, d, position, IHM::S_QUAD);
untereiner's avatar
untereiner committed
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684

		//save a dart from the subdivided face
		unsigned int cur = map.getCurrentLevel() ;

		unsigned int fLevel = map.faceLevel(d) + 1; //puisque dans tous les cas, la face est subdivisee
		map.setCurrentLevel(fLevel) ;

		//le brin est forcement du niveau cur
		Dart cf = map.phi1(d);
		Dart e = cf;
		do
		{
			subdividedfaces.push_back(std::pair<Dart,Dart>(e,map.phi2(e)));
			e = map.phi2(map.phi1(e));
		}while (e != cf);

		map.setCurrentLevel(cur);
	}

	map.setCurrentLevel(vLevel + 1) ; // go to the next level to perform volume subdivision

	std::vector<Dart> newEdges;	//save darts from inner edges
	newEdges.reserve(50);

685
	Dart centralDart = NIL;
untereiner's avatar
untereiner committed
686
687
688
	//Second step : deconnect each corner, close each hole, subdivide each new face into 3
	for (std::vector<Dart>::iterator edge = oldEdges.begin(); edge != oldEdges.end(); ++edge)
	{
untereiner's avatar
untereiner committed
689
		Dart e = *edge;
690

untereiner's avatar
untereiner committed
691
692
693
		std::vector<Dart> v ;

		do
untereiner's avatar
untereiner committed
694
		{
untereiner's avatar
untereiner committed
695
			v.push_back(map.phi1(e));
696
			v.push_back(map.phi1(map.phi1(e)));
untereiner's avatar
untereiner committed
697

untereiner's avatar
untereiner committed
698
699
700
			e = map.phi2(map.phi_1(e));
		}
		while(e != *edge);
untereiner's avatar
untereiner committed
701

untereiner's avatar
untereiner committed
702
		map.splitVolume(v) ;
untereiner's avatar
untereiner committed
703

untereiner's avatar
untereiner committed
704
		Dart old = map.phi2(map.phi1(*edge));
untereiner's avatar
untereiner committed
705
706
707
		Dart dd = map.phi1(map.phi1(old)) ;
		map.splitFace(old,dd) ;

708
709
710
		unsigned int idface = map.getNewFaceId();
		map.setFaceId(dd,idface, FACE);

untereiner's avatar
untereiner committed
711
712
713
714
		Dart ne = map.phi1(map.phi1(old)) ;

		map.cutEdge(ne);
		position[map.phi1(ne)] = volCenter; //plonger a la fin de la boucle ????
715
		centralDart = map.phi1(ne);
untereiner's avatar
untereiner committed
716
717
718
		newEdges.push_back(ne);
		newEdges.push_back(map.phi1(ne));

719
720
721
		unsigned int id = map.getNewEdgeId() ;
		map.setEdgeId(ne, id, EDGE) ;

untereiner's avatar
untereiner committed
722
723
724
725
726
727
728
		Dart stop = map.phi2(map.phi1(ne));
		ne = map.phi2(ne);
		do
		{
			dd = map.phi1(map.phi1(map.phi1(ne)));

			map.splitFace(ne, dd) ;
729
730
			unsigned int idface = map.getNewFaceId();
			map.setFaceId(dd,idface, FACE);
untereiner's avatar
untereiner committed
731
732
733
734
735
736
737

			newEdges.push_back(map.phi1(dd));

			ne = map.phi2(map.phi_1(ne));
			dd = map.phi1(map.phi1(dd));
		}
		while(dd != stop);
untereiner's avatar
untereiner committed
738

739
		//map.computeVertexVertexFunctor(e);
740
741
	}

untereiner's avatar
untereiner committed
742

743
	 map.deleteVolume(map.phi3(map.phi2(map.phi1(oldEdges.front()))));
untereiner's avatar
untereiner committed
744

untereiner's avatar
untereiner committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
	//Third step : 3-sew internal faces
	for (std::vector<std::pair<Dart,Dart> >::iterator it = subdividedfaces.begin(); it != subdividedfaces.end(); ++it)
	{
		Dart f1 = (*it).first;
		Dart f2 = (*it).second;

		if(map.isBoundaryFace(map.phi2(f1)) && map.isBoundaryFace(map.phi2(f2)))
		{
			//id pour toutes les faces interieures
			map.sewVolumes(map.phi2(f1), map.phi2(f2));

			//Fais a la couture !!!!!
			unsigned int idface = map.getNewFaceId();
			map.setFaceId(map.phi2(f1),idface, FACE);
		}

		//FAIS a la couture !!!!!!!
		//id pour toutes les aretes exterieurs des faces quadrangulees
		unsigned int idedge = map.getEdgeId(f1);
		map.setEdgeId(map.phi2(f1), idedge, DART);
		map.setEdgeId( map.phi2(f2), idedge, DART);
	}

	//LA copie de L'id est a gerer avec le sewVolumes normalement !!!!!!
	//id pour les aretes interieurs : (i.e. 6 pour un hexa)
untereiner's avatar
untereiner committed
770
	DartMarkerStore mne(map);
untereiner's avatar
untereiner committed
771
772
773
774
775
776
	for(unsigned int i = 0; i < newEdges.size(); ++i)
	{
		if(!mne.isMarked(newEdges[i]))
		{
			unsigned int idedge = map.getNewEdgeId();
			map.setEdgeId(newEdges[i], idedge, EDGE);
Pierre Kraemer's avatar
Pierre Kraemer committed
777
			mne.markOrbit<EDGE>(newEdges[i]);
untereiner's avatar
untereiner committed
778
779
		}
	}
untereiner's avatar
untereiner committed
780

781
782
783
784
785
786
787
788
789
790
791
792
793
////	std::cout << map.getCurrentLevel() << std::endl;
////	//Second step : deconnect each corner, close each hole, subdivide each new face into 3
//	for (std::vector<Dart>::iterator edge = oldEdges.begin(); edge != oldEdges.end(); ++edge)
//	{
//		Dart e = *edge;
//		if(e == 309)
//			std::cout << "plop" << std::endl;
//		//std::cout << "emb ? " << map.template getEmbedding<VERTEX>(e) << std::endl;
//		//if(map.template getEmbedding<VERTEX>(e) == EMBNULL)
//		map.computeVertexVertexFunctor(e);
//		//std::cout << "emb = " << map.template getEmbedding<VERTEX>(e) << " / dartlevel = " <<  map.getDartLevel(e) << std::endl;
//	}
//	std::cout << std::endl;
794

795
	//map.computerVolumeVertexFunctor(centralDart);
796

untereiner's avatar
untereiner committed
797
798
	map.setCurrentLevel(cur) ;

799
	return centralDart; //subdividedfaces.begin()->first;
untereiner's avatar
untereiner committed
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
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
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
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
template <typename PFP>
void subdivideEdgeWrong(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
{
    assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    assert(!map.edgeIsSubdivided(d) || !"Trying to subdivide an already subdivided edge") ;
    assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");

    unsigned int eLevel = map.edgeLevel(d) ;

    unsigned int cur = map.getCurrentLevel() ;
    map.setCurrentLevel(eLevel) ;

    Dart dd = map.phi2(d) ;
    typename PFP::VEC3 p1 = position[d] ;
    typename PFP::VEC3 p2 = position[map.phi1(d)] ;

    map.setCurrentLevel(eLevel + 1) ;

    map.cutEdge(d) ;
    unsigned int eId = map.getEdgeId(d) ;
    map.setEdgeId(map.phi1(d), eId, EDGE) ; //mise a jour de l'id d'arrete sur chaque moitie d'arete
    map.setEdgeId(map.phi1(dd), eId, EDGE) ;

    map.setFaceId(EDGE, d) ; //mise a jour de l'id de face sur chaque brin de chaque moitie d'arete
    map.setFaceId(EDGE, dd) ;

    position[map.phi1(d)] = (p1 + p2) * typename PFP::REAL(0.5) ;

    map.setCurrentLevel(cur) ;
}

template <typename PFP>
void subdivideFaceWrong(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position, SubdivideType sType)
{
    assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    assert(!map.faceIsSubdivided(d) || !"Trying to subdivide an already subdivided face") ;
    assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");

    unsigned int fLevel = map.faceLevel(d) ;
    Dart old = map.faceOldestDart(d) ;

    unsigned int cur = map.getCurrentLevel() ;
    map.setCurrentLevel(fLevel) ;		// go to the level of the face to subdivide its edges

    unsigned int vLevel = map.volumeLevel(old);
    //one level of subdivision in the neighbordhood
//	Traversor3VW<typename PFP::MAP> trav3EW(map, old);
//	for(Dart dit = trav3EW.begin() ; dit != trav3EW.end() ; dit = trav3EW.next())
//	{
//		Dart oldit = map.volumeOldestDart(dit);
//
//		//std::cout << "vLevel courant = " << map.volumeLevel(oldit) << std::endl;
//
//		if(((vLevel+1) - map.volumeLevel(oldit)) > 1)
//				IHM::subdivideVolumeClassic<PFP>(map, oldit, position);
//	}

    unsigned int degree = 0 ;
    typename PFP::VEC3 p ;
    Traversor2FE<typename PFP::MAP>  travE(map, old);
    for(Dart it = travE.begin(); it != travE.end() ; it = travE.next())
    {
        ++degree;
        p += position[it] ;

        if(!map.edgeIsSubdivided(it))							// first cut the edges (if they are not already)
            IHM::subdivideEdgeWrong<PFP>(map, it, position) ;	// and compute the degree of the face
    }
    p /= typename PFP::REAL(degree) ;


    map.setCurrentLevel(fLevel + 1) ;			// go to the next level to perform face subdivision

    Dart res;

    if(degree == 3 && sType == IHM::S_TRI)	//subdiviser une face triangulaire
    {
        Dart dd = map.phi1(old) ;
        Dart e = map.phi1(map.phi1(dd)) ;
        map.splitFace(dd, e) ;					// insert a new edge
        unsigned int id = map.getNewEdgeId() ;
        map.setEdgeId(map.phi_1(dd), id, EDGE) ;		// set the edge id of the inserted edge to the next available id

        unsigned int idface = map.getFaceId(old);
        map.setFaceId(dd, idface, FACE) ;
        map.setFaceId(e, idface, FACE) ;

        dd = e ;
        e = map.phi1(map.phi1(dd)) ;
        map.splitFace(dd, e) ;
        id = map.getNewEdgeId() ;
        map.setEdgeId(map.phi_1(dd), id, EDGE) ;

        map.setFaceId(dd, idface, FACE) ;
        map.setFaceId(e, idface, FACE) ;

        dd = e ;
        e = map.phi1(map.phi1(dd)) ;
        map.splitFace(dd, e) ;
        id = map.getNewEdgeId() ;
        map.setEdgeId(map.phi_1(dd), id, EDGE) ;

        map.setFaceId(dd, idface, FACE) ;
        map.setFaceId(e, idface, FACE) ;

        Dart stop = map.phi2(map.phi1(old));
        Dart dit = stop;
        do
        {
            unsigned int dId = map.getEdgeId(map.phi_1(map.phi2(dit)));
            unsigned int eId = map.getEdgeId(map.phi1(map.phi2(dit)));

            unsigned int t = dId + eId;

            if(t == 0)
            {
                map.setEdgeId(dit, 1, EDGE) ;
                map.setEdgeId(map.phi2(dit), 1, EDGE) ;
            }
            else if(t == 1)
            {
                map.setEdgeId(dit, 2, EDGE) ;
                map.setEdgeId(map.phi2(dit), 2, EDGE) ;
            }
            else if(t == 2)
            {
                if(dId == eId)
                {
                    map.setEdgeId(dit, 0, EDGE) ;
                    map.setEdgeId(map.phi2(dit), 0, EDGE) ;
                }
                else
                {
                    map.setEdgeId(dit, 1, EDGE) ;
                    map.setEdgeId(map.phi2(dit), 1, EDGE) ;
                }
            }
            else if(t == 3)
            {
                map.setEdgeId(dit, 0, EDGE) ;
                map.setEdgeId(map.phi2(dit), 0, EDGE) ;
            }

            dit = map.phi1(dit);
        }while(dit != stop);

    }
    else
    {
        Dart dd = map.phi1(old) ;
        map.splitFace(dd, map.phi1(map.phi1(dd))) ;

        Dart ne = map.phi2(map.phi_1(dd));
        Dart ne2 = map.phi2(ne);

        map.cutEdge(ne) ;
        unsigned int id = map.getNewEdgeId() ;
        map.setEdgeId(ne, id, EDGE) ;
        id = map.getNewEdgeId() ;
        map.setEdgeId(ne2, id, EDGE) ;

        position[map.phi1(ne)] = p ;

        dd = map.phi1(map.phi1(map.phi1(map.phi1(ne)))) ;
        while(dd != ne)
        {
            Dart next = map.phi1(map.phi1(dd)) ;
            map.splitFace(map.phi1(ne), dd) ;
            Dart nne = map.phi2(map.phi_1(dd)) ;

            id = map.getNewEdgeId() ;
            map.setEdgeId(nne, id, EDGE) ;

            dd = next ;
        }

        unsigned int idface = map.getFaceId(old);
        //Dart e = dd;
        do
        {
            map.setFaceId(dd, idface, DART) ;
            map.setFaceId(map.phi2(dd), idface, DART) ;
            dd = map.phi2(map.phi1(dd));
        }
        while(dd != ne);

        //position[map.phi1(ne)] = p ;
    }

    map.setCurrentLevel(cur) ;
}


template <typename PFP>
//Dart subdivideVolumeClassic(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
Dart subdivideVolumeClassicWrong(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
{
    assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    assert(!map.volumeIsSubdivided(d) || !"Trying to subdivide an already subdivided volume") ;
    assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");

    unsigned int vLevel = map.volumeLevel(d);
    Dart old = map.volumeOldestDart(d);

    unsigned int cur = map.getCurrentLevel();
    map.setCurrentLevel(vLevel);

    //	//one level of subdivision in the neighbordhood
    //	Traversor3VW<typename PFP::MAP> trav3EW(map, old);
    //	for(Dart dit = trav3EW.begin() ; dit != trav3EW.end() ; dit = trav3EW.next())
    //	{
    //		Dart oldit = map.volumeOldestDart(dit);
    //		if(((vLevel+1) - map.volumeLevel(oldit)) > 1)
    //			IHM::subdivideVolumeClassic<PFP>(map, oldit, position);
    //	}

    /*
     * au niveau du volume courant i
     * stockage d'un brin de chaque face de celui-ci
     * avec calcul du centroid
     */

    DartMarkerStore mf(map);		// Lock a face marker to save one dart per face
    DartMarkerStore mv(map);

    typename PFP::VEC3 volCenter;
    unsigned count = 0 ;

    //Store faces that are traversed and start with the face of d
    std::vector<Dart> visitedFaces;
    visitedFaces.reserve(200);
    visitedFaces.push_back(old);

    //Store the edges before the cutEdge
    std::vector<Dart> oldEdges;
    oldEdges.reserve(20);

    mf.markOrbit<FACE>(old) ;

    for(unsigned int i = 0; i < visitedFaces.size(); ++i)
    {
        Dart e = visitedFaces[i] ;
        do
        {
            //add one old edge per vertex to the old edge list
            //compute volume centroid
            if(!mv.isMarked(e))
            {
                mv.markOrbit<VERTEX>(e);
                volCenter += position[e];
                ++count;
                oldEdges.push_back(e);
            }

            // add all face neighbours to the table
            Dart ee = map.phi2(e) ;
            if(!mf.isMarked(ee)) // not already marked
            {
                visitedFaces.push_back(ee) ;
                mf.markOrbit<FACE>(ee) ;
            }

            e = map.phi1(e) ;
        } while(e != visitedFaces[i]) ;
    }

    volCenter /= typename PFP::REAL(count) ;

    /*
     * Subdivision
     */

    //Store the darts from quadrangulated faces
    std::vector<std::pair<Dart,Dart> > subdividedfaces;
    subdividedfaces.reserve(25);

    //First step : subdivide edges and faces
    //creates a i+1 edge level and i+1 face level
    for (std::vector<Dart>::iterator face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
    {
        Dart d = *face;

        //if needed subdivide face
        if(!map.faceIsSubdivided(d))
            IHM::subdivideFaceWrong<PFP>(map, d, position, IHM::S_QUAD);

        //save a dart from the subdivided face
        unsigned int cur = map.getCurrentLevel() ;

        unsigned int fLevel = map.faceLevel(d) + 1; //puisque dans tous les cas, la face est subdivisee
        map.setCurrentLevel(fLevel) ;

        //le brin est forcement du niveau cur
        Dart cf = map.phi1(d);
        Dart e = cf;
        do
        {
            subdividedfaces.push_back(std::pair<Dart,Dart>(e,map.phi2(e)));
            e = map.phi2(map.phi1(e));
        }while (e != cf);

        map.setCurrentLevel(cur);
    }

    map.setCurrentLevel(vLevel + 1) ; // go to the next level to perform volume subdivision

    std::vector<Dart> newEdges;	//save darts from inner edges
    newEdges.reserve(50);

    Dart centralDart = NIL;
    //Second step : deconnect each corner, close each hole, subdivide each new face into 3
    for (std::vector<Dart>::iterator edge = oldEdges.begin(); edge != oldEdges.end(); ++edge)
    {
        Dart e = *edge;

        std::vector<Dart> v ;

        do
        {
            v.push_back(map.phi1(e));
            v.push_back(map.phi1(map.phi1(e)));

            e = map.phi2(map.phi_1(e));
        }
        while(e != *edge);

        map.splitVolume(v) ;

        Dart old = map.phi2(map.phi1(*edge));
        Dart dd = map.phi1(map.phi1(old)) ;
        map.splitFace(old,dd) ;

        unsigned int idface = map.getNewFaceId();
        map.setFaceId(dd,idface, FACE);

        Dart ne = map.phi1(map.phi1(old)) ;

        map.cutEdge(ne);
        position[map.phi1(ne)] = volCenter; //plonger a la fin de la boucle ????
        centralDart = map.phi1(ne);
        newEdges.push_back(ne);
        newEdges.push_back(map.phi1(ne));

        unsigned int id = map.getNewEdgeId() ;
        map.setEdgeId(ne, id, EDGE) ;

        Dart stop = map.phi2(map.phi1(ne));
        ne = map.phi2(ne);
        do
        {
            dd = map.phi1(map.phi1(map.phi1(ne)));

            map.splitFace(ne, dd) ;
            unsigned int idface = map.getNewFaceId();
            map.setFaceId(dd,idface, FACE);

            newEdges.push_back(map.phi1(dd));

            ne = map.phi2(map.phi_1(ne));
            dd = map.phi1(map.phi1(dd));
        }
        while(dd != stop);
    }


     map.deleteVolume(map.phi3(map.phi2(map.phi1(oldEdges.front()))));

    //Third step : 3-sew internal faces
    for (std::vector<std::pair<Dart,Dart> >::iterator it = subdividedfaces.begin(); it != subdividedfaces.end(); ++it)
    {
        Dart f1 = (*it).first;
        Dart f2 = (*it).second;

        if(map.isBoundaryFace(map.phi2(f1)) && map.isBoundaryFace(map.phi2(f2)))
        {
            //id pour toutes les faces interieures
            map.sewVolumes(map.phi2(f1), map.phi2(f2));

            //Fais a la couture !!!!!
            unsigned int idface = map.getNewFaceId();
            map.setFaceId(map.phi2(f1),idface, FACE);
        }

        //FAIS a la couture !!!!!!!
        //id pour toutes les aretes exterieurs des faces quadrangulees
        unsigned int idedge = map.getEdgeId(f1);
        map.setEdgeId(map.phi2(f1), idedge, DART);
        map.setEdgeId( map.phi2(f2), idedge, DART);
    }

    //LA copie de L'id est a gerer avec le sewVolumes normalement !!!!!!
    //id pour les aretes interieurs : (i.e. 6 pour un hexa)
    DartMarkerStore mne(map);
    for(unsigned int i = 0; i < newEdges.size(); ++i)
    {
        if(!mne.isMarked(newEdges[i]))
        {
            unsigned int idedge = map.getNewEdgeId();
            map.setEdgeId(newEdges[i], idedge, EDGE);
            mne.markOrbit<EDGE>(newEdges[i]);
        }
    }

    map.setCurrentLevel(cur) ;

    return centralDart; //subdividedfaces.begin()->first;
}


1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
/************************************************************************************************
 * 									Simplification												*
 ************************************************************************************************/


template <typename PFP>
void coarsenEdge(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(map.edgeCanBeCoarsened(d) || !"Trying to coarsen an edge that can not be coarsened") ;

	unsigned int cur = map.getCurrentLevel() ;
	map.setCurrentLevel(cur + 1) ;
	map.uncutEdge(d) ;
	map.setCurrentLevel(cur) ;
}

template <typename PFP>
void coarsenFace(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position, SubdivideType sType)
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
1233
	assert(map.faceCanBeCoarsened(d) || !"Trying to coarsen a non-subdivided face or a more than once subdivided face") ;
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276

	unsigned int cur = map.getCurrentLevel() ;

	unsigned int degree = 0 ;
	Dart fit = d ;
	do
	{
		++degree ;
		fit = map.phi1(fit) ;
	} while(fit != d) ;

//	Dart d3 = map.phi3(d);

	if(degree == 3)
	{

	}
	else
	{
		map.setCurrentLevel(cur + 1) ;
		map.deleteVertexSubdividedFace(d);
		map.setCurrentLevel(cur) ;

//		Dart centralV = map.phi1(map.phi1(d));
//		map.Map2::deleteVertex(centralV);
//
//		//demarking faces from border to delete .... fucking shit
//		Dart it = d ;
//		do
//		{
//			if (map.boundaryUnmark(it))
//				return true ;
//			it = map.phi2(map.phi_1(it)) ;
//		} while (it != d) ;
//
//		map.Map2::deleteVertex(map.phi1(map.phi1(d3)));

	}

	fit = d ;
	do
	{
		if(map.edgeCanBeCoarsened(fit))
1277
			IHM::coarsenEdge<PFP>(map, fit, position) ;
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
		fit = map.phi1(fit) ;
	} while(fit != d) ;
}

template <typename PFP>
void coarsenVolume(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(map.volumeIsSubdividedOnce(d) || !"Trying to coarsen a non-subdivided volume or a more than once subdivided volume") ;

	unsigned int cur = map.getCurrentLevel() ;

	/*
	 * Deconnecter toutes les faces interieurs
	 */
	map.setCurrentLevel(cur + 1) ;
	Dart nf = map.phi_1(map.phi2(map.phi1(d)));
	map.deleteVertex(nf);
	map.setCurrentLevel(cur) ;

	/*
	 * simplifier les faces
	 */
untereiner's avatar
untereiner committed
1301
	Traversor3WF<typename PFP::MAP> trav3WF(map, d, true);
1302
1303
1304
	for(Dart dit = trav3WF.begin() ; dit != trav3WF.end() ; dit = trav3WF.next())
	{
		if(map.faceCanBeCoarsened(dit))
1305
			IHM::coarsenFace<PFP>(map, dit, position, IHM::S_QUAD);
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
	}
}




/* **************************************************************************************
 *    							USE WITH CAUTION										*
 ****************************************************************************************/










untereiner's avatar
untereiner committed
1325
template <typename PFP>
1326
Dart subdivideVolumeClassic2(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
untereiner's avatar
untereiner committed
1327
1328
1329
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(!map.volumeIsSubdivided(d) || !"Trying to subdivide an already subdivided volume") ;
Thery Sylvain's avatar
Thery Sylvain committed
1330
	assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");
untereiner's avatar
untereiner committed
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340

	unsigned int vLevel = map.volumeLevel(d);
	Dart old = map.volumeOldestDart(d);

	unsigned int cur = map.getCurrentLevel();
	map.setCurrentLevel(vLevel);

	/*
	 * Compute volume centroid
	 */
1341
	typename PFP::VEC3 volCenter =  Algo::Surface::Geometry::volumeCentroid<PFP>(map, old, position);
untereiner's avatar
untereiner committed
1342

untereiner's avatar
untereiner committed
1343
1344
	Traversor3WV<typename PFP::MAP> traV(map, old);

untereiner's avatar
untereiner committed
1345
1346
1347
	/*
	 * Subdivide Faces
	 */
1348
1349
	std::vector<std::pair<Dart,Dart> > subdividedFaces;
	subdividedFaces.reserve(128);
untereiner's avatar
untereiner committed
1350
1351
1352
1353
1354
1355

	Traversor3WF<typename PFP::MAP> traF(map, old);
	for(Dart dit = traF.begin(); dit != traF.end(); dit = traF.next())
	{
		//if needed subdivide face
		if(!map.faceIsSubdivided(dit))
1356
			IHM::subdivideFace<PFP>(map, dit, position, IHM::S_QUAD);
untereiner's avatar
untereiner committed
1357

1358
		//save darts from the central vertex of each subdivided face
untereiner's avatar
untereiner committed
1359
		unsigned int cur = map.getCurrentLevel() ;
1360
1361
1362
		unsigned int fLevel = map.faceLevel(dit);
		map.setCurrentLevel(fLevel + 1) ;
		map.saveRelationsAroundVertex(map.phi2(map.phi1(dit)), subdividedFaces);
untereiner's avatar
untereiner committed
1363
1364
1365
		map.setCurrentLevel(cur);
	}

1366
1367
1368
1369
	cur = map.getCurrentLevel() ;
	unsigned int fLevel = map.faceLevel(old);
	map.setCurrentLevel(fLevel + 1) ;
	map.unsewAroundVertex(subdividedFaces);
untereiner's avatar
untereiner committed
1370
1371
	map.setCurrentLevel(cur);

untereiner's avatar
untereiner committed
1372
1373
1374
1375
1376
1377
1378
	/*
	 * Create inside volumes
	 */
	std::vector<Dart> newEdges;	//save darts from inner edges
	newEdges.reserve(50);
	Dart centralDart = NIL;

untereiner's avatar
untereiner committed
1379
1380
	//unsigned int degree = 0 ;
	//typename PFP::VEC3 volCenter ;
untereiner's avatar
untereiner committed
1381

untereiner's avatar
untereiner committed
1382
	map.setCurrentLevel(vLevel + 1) ; // go to the next level to perform volume subdivision
untereiner's avatar
untereiner committed
1383

untereiner's avatar
untereiner committed
1384
1385
1386
1387
	for(Dart dit = traV.begin(); dit != traV.end(); dit = traV.next())
	{
		//volCenter += position[d];
		//++degree;
untereiner's avatar
untereiner committed
1388

1389
		centralDart = map.quadranguleFace(dit);
untereiner's avatar
untereiner committed
1390
		position[centralDart] = volCenter;
untereiner's avatar
untereiner committed
1391
	}
1392
	//volCenter /= double(degree);
untereiner's avatar
untereiner committed
1393

1394
	for (std::vector<std::pair<Dart,Dart> >::iterator it = subdividedFaces.begin(); it != subdividedFaces.end(); ++it)
untereiner's avatar
untereiner committed
1395
	{
1396
1397
		Dart f1 = map.phi2((*it).first);
		Dart f2 = map.phi2((*it).second);
untereiner's avatar
untereiner committed
1398

1399
		if(map.isBoundaryFace(f1) && map.isBoundaryFace(f2))
untereiner's avatar
untereiner committed
1400
		{
1401
1402
			std::cout << "plop" << std::endl;
			map.sewVolumes(f1, f2, false);
untereiner's avatar
untereiner committed
1403
1404
1405
		}
	}

untereiner's avatar
untereiner committed
1406
	position[centralDart] = volCenter;
1407
1408


untereiner's avatar
untereiner committed
1409
1410


1411
1412
1413
1414
1415
//	//Third step : 3-sew internal faces
//	for (std::vector<std::pair<Dart,Dart> >::iterator it = subdividedfaces.begin(); it != subdividedfaces.end(); ++it)
//	{
//		Dart f1 = (*it).first;
//		Dart f2 = (*it).second;
untereiner's avatar
untereiner committed
1416
//
untereiner's avatar
untereiner committed
1417
//
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
//		if(map.isBoundaryFace(map.phi2(f1)) && map.isBoundaryFace(map.phi2(f2)))
//		{
//			std::cout << "boundary" << std::endl;
//			//id pour toutes les faces interieures
//			map.sewVolumes(map.phi2(f1), map.phi2(f2));
//
////
////			//Fais a la couture !!!!!
////			unsigned int idface = map.getNewFaceId();
////			map.setFaceId(map.phi2(f1),idface, FACE);
//		}
////
////		//FAIS a la couture !!!!!!!
////		//id pour toutes les aretes exterieurs des faces quadrangulees
////		unsigned int idedge = map.getEdgeId(f1);
////		map.setEdgeId(map.phi2(f1), idedge, DART);
////		map.setEdgeId( map.phi2(f2), idedge, DART);
//	}
untereiner's avatar
untereiner committed
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445

	//LA copie de L'id est a gerer avec le sewVolumes normalement !!!!!!
	//id pour les aretes interieurs : (i.e. 6 pour un hexa)
	DartMarker mne(map);
	for(unsigned int i = 0; i < newEdges.size(); ++i)
	{
		if(!mne.isMarked(newEdges[i]))
		{
			unsigned int idedge = map.getNewEdgeId();
			map.setEdgeId(newEdges[i], idedge, EDGE);
Pierre Kraemer's avatar
Pierre Kraemer committed
1446
			mne.markOrbit<EDGE>(newEdges[i]);
untereiner's avatar
untereiner committed
1447
1448
		}
	}
untereiner's avatar
untereiner committed
1449
1450
1451


	//plonger a la fin de la boucle ????
untereiner's avatar
untereiner committed
1452
1453
1454

	map.setCurrentLevel(cur) ;

1455
	return subdividedFaces.front().first;
untereiner's avatar
untereiner committed
1456

untereiner's avatar
untereiner committed
1457
1458
}

untereiner's avatar
untereiner committed
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
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
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
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666









template <typename PFP>
void subdivideEdgeLoop(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
{
    assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    assert(!map.edgeIsSubdivided(d) || !"Trying to subdivide an already subdivided edge") ;
    assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");

    unsigned int eLevel = map.edgeLevel(d) ;

    unsigned int cur = map.getCurrentLevel() ;
    map.setCurrentLevel(eLevel) ;

    Dart dd = map.phi2(d) ;
    typename PFP::VEC3 p1 = position[d] ;
    typename PFP::VEC3 p2 = position[map.phi1(d)] ;

    map.setCurrentLevel(eLevel + 1) ;

    map.cutEdge(d) ;
    unsigned int eId = map.getEdgeId(d) ;
    map.setEdgeId(map.phi1(d), eId, EDGE) ; //mise a jour de l'id d'arrete sur chaque moitie d'arete
    map.setEdgeId(map.phi1(dd), eId, EDGE) ;

    map.setFaceId(EDGE, d) ; //mise a jour de l'id de face sur chaque brin de chaque moitie d'arete
    map.setFaceId(EDGE, dd) ;

    position[map.phi1(d)] = (p1 + p2) * typename PFP::REAL(0.5) ;
    map.setCurrentLevel(cur) ;
}

template <typename PFP>
void subdivideFaceLoop(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position, SubdivideType sType)
{
    assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    assert(!map.faceIsSubdivided(d) || !"Trying to subdivide an already subdivided face") ;
    assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");

    unsigned int fLevel = map.faceLevel(d) ;
    Dart old = map.faceOldestDart(d) ;

    unsigned int cur = map.getCurrentLevel() ;
    map.setCurrentLevel(fLevel) ;		// go to the level of the face to subdivide its edges

    unsigned int vLevel = map.volumeLevel(old);
    //one level of subdivision in the neighbordhood
//	Traversor3VW<typename PFP::MAP> trav3EW(map, old);
//	for(Dart dit = trav3EW.begin() ; dit != trav3EW.end() ; dit = trav3EW.next())
//	{
//		Dart oldit = map.volumeOldestDart(dit);
//
//		//std::cout << "vLevel courant = " << map.volumeLevel(oldit) << std::endl;
//
//		if(((vLevel+1) - map.volumeLevel(oldit)) > 1)
//				IHM::subdivideVolumeClassic<PFP>(map, oldit, position);
//	}

    unsigned int degree = 0 ;
    typename PFP::VEC3 p ;
    Traversor2FE<typename PFP::MAP>  travE(map, old);
    for(Dart it = travE.begin(); it != travE.end() ; it = travE.next())
    {
        ++degree;
        p += position[it] ;

        if(!map.edgeIsSubdivided(it))							// first cut the edges (if they are not already)
            IHM::subdivideEdgeLoop<PFP>(map, it, position) ;	// and compute the degree of the face
    }
    p /= typename PFP::REAL(degree) ;


    map.setCurrentLevel(fLevel + 1) ;			// go to the next level to perform face subdivision

    Dart res;

    if(degree == 3 && sType == IHM::S_TRI)	//subdiviser une face triangulaire
    {
        Dart dd = map.phi1(old) ;
        Dart e = map.phi1(map.phi1(dd)) ;
        map.splitFace(dd, e) ;					// insert a new edge
        unsigned int id = map.getNewEdgeId() ;
        map.setEdgeId(map.phi_1(dd), id, EDGE) ;		// set the edge id of the inserted edge to the next available id

        unsigned int idface = map.getFaceId(old);
        map.setFaceId(dd, idface, FACE) ;
        map.setFaceId(e, idface, FACE) ;

        dd = e ;
        e = map.phi1(map.phi1(dd)) ;
        map.splitFace(dd, e) ;
        id = map.getNewEdgeId() ;
        map.setEdgeId(map.phi_1(dd), id, EDGE) ;

        map.setFaceId(dd, idface, FACE) ;
        map.setFaceId(e, idface, FACE) ;

        dd = e ;
        e = map.phi1(map.phi1(dd)) ;
        map.splitFace(dd, e) ;
        id = map.getNewEdgeId() ;
        map.setEdgeId(map.phi_1(dd), id, EDGE) ;

        map.setFaceId(dd, idface, FACE) ;
        map.setFaceId(e, idface, FACE) ;

        Dart stop = map.phi2(map.phi1(old));
        Dart dit = stop;
        do
        {
            unsigned int dId = map.getEdgeId(map.phi_1(map.phi2(dit)));
            unsigned int eId = map.getEdgeId(map.phi1(map.phi2(dit)));

            unsigned int t = dId + eId;

            if(t == 0)
            {
                map.setEdgeId(dit, 1, EDGE) ;
                map.setEdgeId(map.phi2(dit), 1, EDGE) ;
            }
            else if(t == 1)
            {
                map.setEdgeId(dit, 2, EDGE) ;
                map.setEdgeId(map.phi2(dit), 2, EDGE) ;
            }
            else if(t == 2)
            {
                if(dId == eId)
                {
                    map.setEdgeId(dit, 0, EDGE) ;
                    map.setEdgeId(map.phi2(dit), 0, EDGE) ;
                }
                else
                {
                    map.setEdgeId(dit, 1, EDGE) ;
                    map.setEdgeId(map.phi2(dit), 1, EDGE) ;
                }
            }
            else if(t == 3)
            {
                map.setEdgeId(dit, 0, EDGE) ;
                map.setEdgeId(map.phi2(dit), 0, EDGE) ;
            }

            dit = map.phi1(dit);
        }while(dit != stop);

    }
    else
    {
        Dart dd = map.phi1(old) ;
        map.splitFace(dd, map.phi1(map.phi1(dd))) ;

        Dart ne = map.phi2(map.phi_1(dd));
        Dart ne2 = map.phi2(ne);

        map.cutEdge(ne) ;
        unsigned int id = map.getNewEdgeId() ;
        map.setEdgeId(ne, id, EDGE) ;
        id = map.getNewEdgeId() ;
        map.setEdgeId(ne2, id, EDGE) ;

        position[map.phi1(ne)] = p ;

        dd = map.phi1(map.phi1(map.phi1(map.phi1(ne)))) ;
        while(dd != ne)
        {
            Dart next = map.phi1(map.phi1(dd)) ;
            map.splitFace(map.phi1(ne), dd) ;
            Dart nne = map.phi2(map.phi_1(dd)) ;

            id = map.getNewEdgeId() ;
            map.setEdgeId(nne, id, EDGE) ;

            dd = next ;
        }

        unsigned int idface = map.getFaceId(old);
        //Dart e = dd;
        do
        {
            map.setFaceId(dd, idface, DART) ;
            map.setFaceId(map.phi2(dd), idface, DART) ;
            dd = map.phi2(map.phi1(dd));
        }
        while(dd != ne);

        position[map.phi1(ne)] = p ;
    }

    map.setCurrentLevel(cur) ;
}









untereiner's avatar
untereiner committed
1667
template <typename PFP>
1668
void subdivideLoop(typename PFP::MAP& map, Dart d, AttributeHandler<typename PFP::VEC3, VERTEX>& position)
untereiner's avatar
untereiner committed
1669
1670
1671
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(!map.volumeIsSubdivided(d) || !"Trying to subdivide an already subdivided volume") ;
Thery Sylvain's avatar
Thery Sylvain committed
1672
	assert(!map.isBoundaryMarked3(d) || !"Trying to subdivide a dart marked boundary");
untereiner's avatar
untereiner committed
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682

	unsigned int vLevel = map.volumeLevel(d);
	Dart old = map.volumeOldestDart(d);

	unsigned int cur = map.getCurrentLevel();
	map.setCurrentLevel(vLevel);

	/*
	 * Compute volume centroid
	 */
1683
	typename PFP::VEC3 volCenter =  Algo::Surface::Geometry::volumeCentroid<PFP>(map, old, position);
untereiner's avatar
untereiner committed
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695

	/*
	 * Subdivide Faces
	 */
	std::vector<std::pair<Dart,Dart> > subdividedfaces;
	subdividedfaces.reserve(25);

	Traversor3WF<typename PFP::MAP> traF(map, old);
	for(Dart dit = traF.begin(); dit != traF.end(); dit = traF.next())
	{
		//if needed subdivide face
		if(!map.faceIsSubdivided(dit))
untereiner's avatar
untereiner committed
1696
            IHM::subdivideFaceLoop<PFP>(map, dit, position, IHM::S_TRI);
untereiner's avatar
untereiner committed
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734

		//save a dart from the subdivided face
		unsigned int cur = map.getCurrentLevel() ;

		unsigned int fLevel = map.faceLevel(d) + 1; //puisque dans tous les cas, la face est subdivisee
		map.setCurrentLevel(fLevel) ;

		//le brin est forcement du niveau cur
		Dart cf = map.phi1(d);
		Dart e = cf;
		do
		{
			subdividedfaces.push_back(std::pair<Dart,Dart>(e,map.phi2(e)));
			e = map.phi2(map.phi1(e));
		}while (e != cf);

		map.setCurrentLevel(cur);
	}

	/*
	 * Create inside volumes
	 */
	std::vector<Dart> newEdges;	//save darts from inner edges
	newEdges.reserve(50);
	Dart centralDart = NIL;

	//unsigned int degree = 0 ;
	//typename PFP::VEC3 volCenter ;
	Traversor3WV<typename PFP::MAP> traV(map, old);

	bool isNotTet = false;

	map.setCurrentLevel(vLevel + 1) ; // go to the next level to perform volume subdivision

	for(Dart dit = traV.begin(); dit != traV.end(); dit = traV.next())
	{
		//volCenter += position[d];
		//++degree;
1735
		Dart f1 = map.phi1(dit);
untereiner's avatar
untereiner committed
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
		Dart e = dit;
		std::vector<Dart> v ;

		do
		{
			v.push_back(map.phi1(e));
			e = map.phi2(map.phi_1(e));
		}
		while(e != dit);

		map.splitVolume(v) ;

1748
1749
1750
1751
1752
		for(std::vector<Dart>::iterator it = v.begin() ; it != v.end() ; ++it)
		{
			map.setEdgeId(*it,map.getEdgeId(*it), EDGE);
		}

untereiner's avatar
untereiner committed
1753
		//if is not a tetrahedron
1754
1755
		unsigned int fdeg = map.faceDegree(map.phi2(f1));
		if(fdeg > 3)
untereiner's avatar
untereiner committed
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
		{
			isNotTet = true;
			Dart old = map.phi2(map.phi1(dit));
			Dart dd = map.phi1(old) ;
			map.splitFace(old,dd) ;

			Dart ne = map.phi1(old);

			map.cutEdge(ne);
			position[map.phi1(ne)] = volCenter; //plonger a la fin de la boucle ????
			centralDart = map.phi1(ne);
1767
1768
1769
1770

			unsigned int idFace = map.getNewFaceId();
			map.setFaceId(ne, idFace, FACE);

untereiner's avatar
untereiner committed
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
			newEdges.push_back(ne);
			newEdges.push_back(map.phi1(ne));

			Dart stop = map.phi2(map.phi1(ne));
			ne = map.phi2(ne);
			do
			{
				dd = map.phi1(map.phi1(ne));

				map.splitFace(ne, dd) ;

1782
1783
1784
				unsigned int idFace = map.getNewFaceId();
				map.setFaceId(ne, idFace, FACE);

untereiner's avatar
untereiner committed
1785
1786
1787
1788
1789
1790
				newEdges.push_back(map.phi1(dd));

				ne = map.phi2(map.phi_1(ne));
				dd = map.phi1(dd);
			}
			while(dd != stop);
1791
1792
1793
1794

			idFace = map.getNewFaceId();
			map.setFaceId(stop, idFace, FACE);

untereiner's avatar
untereiner committed
1795
		}
1796
1797
1798
1799
1800
		else
		{
			unsigned int idface = map.getNewFaceId();
			map.setFaceId(map.phi2(v.front()),idface, FACE);
		}
untereiner's avatar
untereiner committed
1801
1802
	}

untereiner's avatar
untereiner committed
1803
1804
1805
1806
    //switch inner faces
    if(isNotTet)
    {
        DartMarker me(map);
untereiner's avatar
untereiner committed
1807

untereiner's avatar
untereiner committed
1808
1809
1810
1811
        for(Dart dit = traV.begin(); dit != traV.end(); dit = traV.next())
        {
            Dart x = map.phi_1(map.phi2(map.phi1(dit)));
            Dart f = x;
untereiner's avatar
untereiner committed
1812

untereiner's avatar
untereiner committed
1813
1814
1815
            do
            {
                Dart f3 = map.phi3(f);
untereiner's avatar
untereiner committed
1816

untereiner's avatar
untereiner committed
1817
1818
1819
                if(!me.isMarked(f3))
                {
                    Dart tmp =  map.phi_1(map.phi2(map.phi_1(map.phi2(map.phi_1(f3))))); //future voisin par phi2
1820

untereiner's avatar
untereiner committed
1821
1822
                    Dart f32 = map.phi2(f3);
                    map.swapEdges(f3, tmp);
untereiner's avatar
untereiner committed
1823

untereiner's avatar
untereiner committed
1824
1825
1826
1827
                    unsigned int idface = map.getNewFaceId();
                    map.setFaceId(f3,idface, FACE);
                    idface = map.getNewFaceId();
                    map.setFaceId(f32,idface, FACE);