subdivision3.hpp 87.4 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
#include "Topology/generic/dartmarker.h"
29

30
//#define DEBUG_affichage
31 32 33 34 35 36
namespace CGoGN
{

namespace Algo
{

37 38 39
namespace Volume
{

40
namespace IHM
41 42
{

untereiner's avatar
untereiner committed
43

44

45
template <typename PFP>
46
void subdivideEdge(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
47
{
48 49
    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") ;
50
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
51

52
    unsigned int eLevel = map.edgeLevel(d) ;
53

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

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

61
    map.setCurrentLevel(eLevel + 1) ;
62

63 64 65 66
    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) ;
67

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

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

74
    map.setCurrentLevel(cur) ;
75 76 77
}

template <typename PFP>
78
void subdivideFace(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, SubdivideType sType)
79
{
80 81
    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") ;
82
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
83

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

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

90
    //one level of subdivision in the neighbordhood
untereiner's avatar
untereiner committed
91
//	unsigned int vLevel = map.volumeLevel(old);
92 93 94 95
//	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
96
//
untereiner's avatar
untereiner committed
97 98 99
//		//std::cout << "vLevel courant = " << map.volumeLevel(oldit) << std::endl;
//
//		if(((vLevel+1) - map.volumeLevel(oldit)) > 1)
100
//				IHM::subdivideVolumeClassic<PFP>(map, oldit, position);
101
//	}
untereiner's avatar
untereiner committed
102

103 104 105 106 107 108 109
    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] ;
untereiner's avatar
untereiner committed
110

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

untereiner's avatar
untereiner committed
116

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


120 121 122 123 124 125 126
    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
untereiner's avatar
untereiner committed
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
        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) ;

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

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

159 160 161 162 163
        map.cutEdge(ne) ;
        unsigned int id = map.getNewEdgeId() ;
        map.setEdgeId(ne, id, EDGE) ;
        id = map.getNewEdgeId() ;
        map.setEdgeId(ne2, id, EDGE) ;
164

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

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        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);
189

190 191
        //map.computeFaceVertexFunctor(map.phi1(ne));

192
    }
193

194
    map.setCurrentLevel(cur) ;
195 196
}

untereiner's avatar
untereiner committed
197
template <typename PFP>
Lionel Untereiner's avatar
Lionel Untereiner committed
198
Dart subdivideVolumeClassic(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, bool OneLevelDifference)
199
//Dart subdivideVolumeClassic(typename PFP::MAP& map, Dart d, Algo::Volume::IHM::AttributeHandler_IHM<typename PFP::VEC3, VERTEX>& position)
untereiner's avatar
untereiner committed
200
{
201 202
    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") ;
203
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
pitiot's avatar
pitiot committed
204 205 206
#ifdef DEBUG_affichage
    CGoGNout<<"debut subdivision --"<< CGoGNendl;
#endif
207 208
    unsigned int vLevel = map.volumeLevel(d);
    Dart old = map.volumeOldestDart(d);
pitiot's avatar
pitiot committed
209 210 211
#ifdef DEBUG_affichage
    CGoGNout<<"debut subdivision -"<< CGoGNendl;
#endif
212 213 214
    unsigned int cur = map.getCurrentLevel();
    map.setCurrentLevel(vLevel);

Lionel Untereiner's avatar
Lionel Untereiner committed
215 216
	if(OneLevelDifference)
	{
217

Lionel Untereiner's avatar
Lionel Untereiner committed
218 219 220 221 222 223
		Traversor3WF<typename PFP::MAP> traF(map, old);
		for(Dart dit = traF.begin(); dit != traF.end(); dit = traF.next())
		{
			Dart nv = map.phi3(dit);
			if(!map.isBoundaryMarked(3, nv))
				if(map.volumeLevel(nv) == vLevel - 1)
224 225

                    subdivideVolumeClassic<PFP>(map,nv,position,OneLevelDifference);
Lionel Untereiner's avatar
Lionel Untereiner committed
226 227
		}
	}
228 229 230 231 232 233 234

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

235 236
	DartMarkerStore<typename PFP::MAP> mf(map);		// Lock a face marker to save one dart per face
	DartMarkerStore<typename PFP::MAP> mv(map);
237 238 239 240 241 242 243 244 245 246 247 248 249

    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);

250
	mf.markOrbit(Face(old)) ;
pitiot's avatar
pitiot committed
251 252 253
#ifdef DEBUG_affichage
    CGoGNout<<"debut subdivision ++"<< CGoGNendl;
#endif
254 255 256 257 258 259 260 261 262
    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))
            {
263
				mv.markOrbit(Vertex(e));
264 265 266 267 268 269 270 271 272 273
                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) ;
274
				mf.markOrbit(Face(ee)) ;
275 276 277 278 279
            }

            e = map.phi1(e) ;
        } while(e != visitedFaces[i]) ;
    }
pitiot's avatar
pitiot committed
280 281 282
#ifdef DEBUG_affichage
    CGoGNout<<"debut subdivision +++"<< CGoGNendl;
#endif
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
    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
pitiot's avatar
pitiot committed
322 323 324
#ifdef DEBUG_affichage
    CGoGNout<<"debut subdivision ++++"<< CGoGNendl;
#endif
325 326 327 328 329 330 331
    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)
    {
Lionel Untereiner's avatar
Lionel Untereiner committed
332
        Dart e = *edge;		
333
        std::vector<Dart> v ;
Lionel Untereiner's avatar
Lionel Untereiner committed
334 335
//		unsigned int cur = map.getCurrentLevel();
//		map.setCurrentLevel(map.getMaxLevel());
336 337

        do
Lionel Untereiner's avatar
Lionel Untereiner committed
338 339 340 341 342 343 344 345 346 347 348
		{
			if(map.faceLevel(map.phi1(e)) > map.getCurrentLevel())
			{
				std::cout << "plop" << std::endl;
				return NIL;
			}
			else
			{
				v.push_back(map.phi1(e));
				v.push_back(map.phi1(map.phi1(e)));
			}
untereiner's avatar
untereiner committed
349

Lionel Untereiner's avatar
Lionel Untereiner committed
350 351 352 353 354 355 356 357 358
//			Dart dit = map.phi1(e);
//			do
//			{
//				v.push_back(dit);
//				dit = map.phi1(dit);
//			}while(dit != map.phi_1(e));

//            v.push_back(map.phi1(e));
//            v.push_back(map.phi1(map.phi1(e)));
359
//			std::cout << "v size = " << v.size() << std::endl;
360 361 362
            e = map.phi2(map.phi_1(e));
        }
        while(e != *edge);
untereiner's avatar
untereiner committed
363

364
        map.splitVolume(v) ;
untereiner's avatar
untereiner committed
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        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);
400

Lionel Untereiner's avatar
Lionel Untereiner committed
401 402
//		map.setCurrentLevel(cur);

403
        //map.computeVertexVertexFunctor(e);
404 405
    }

pitiot's avatar
pitiot committed
406 407 408
#ifdef DEBUG_affichage
    CGoGNout<<"debut subdivision +++++"<< CGoGNendl;
#endif
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
     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);
    }

pitiot's avatar
pitiot committed
434 435 436 437

#ifdef DEBUG_affichage
    CGoGNout<<"debut subdivision ++++++"<< CGoGNendl;
#endif
438 439
    //LA copie de L'id est a gerer avec le sewVolumes normalement !!!!!!
    //id pour les aretes interieurs : (i.e. 6 pour un hexa)
440
	DartMarkerStore<typename PFP::MAP> mne(map);
441 442 443 444 445 446
    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);
447
			mne.markOrbit(Edge(newEdges[i]));
448 449
        }
    }
untereiner's avatar
untereiner committed
450

451 452 453 454 455 456 457 458 459 460 461 462 463
//	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;
464

465
//    map.computerVolumeVertexFunctor(centralDart);
466

467
    map.setCurrentLevel(cur) ;
untereiner's avatar
untereiner committed
468

469
    return centralDart; //subdividedfaces.begin()->first;
untereiner's avatar
untereiner committed
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
template <typename PFP>
501
void subdivideEdgeGeom(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
502 503 504
{
    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") ;
505
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525

    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) ;

526 527
    //position[map.phi1(d)] = (p1 + p2) * typename PFP::REAL(0.5) ;
    map.computeEdgeVertexFunctor(map.phi1(d));
528 529 530 531 532

    map.setCurrentLevel(cur) ;
}

template <typename PFP>
533
void subdivideFaceGeom(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, SubdivideType sType)
534 535 536
{
    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") ;
537
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

    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] ;

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 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 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
        if(!map.edgeIsSubdivided(it))							// first cut the edges (if they are not already)
            IHM::subdivideEdgeGeom<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) ;

    }
    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);

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

    map.setCurrentLevel(cur) ;
}

template <typename PFP>
654
Dart subdivideVolumeClassicGeom(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
655 656 657 658
//Dart subdivideVolumeClassic(typename PFP::MAP& map, Dart d, Algo::Volume::IHM::AttributeHandler_IHM<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") ;
659
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

    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
     */

682 683
	DartMarkerStore<typename PFP::MAP> mf(map);		// Lock a face marker to save one dart per face
	DartMarkerStore<typename PFP::MAP> mv(map);
684 685 686 687 688 689 690 691 692 693 694 695 696

    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);

697
	mf.markOrbit(Face(old)) ;
698 699 700 701 702 703 704 705 706 707

    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))
            {
708
				mv.markOrbit(Vertex(e));
709 710 711 712 713 714 715 716 717 718
                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) ;
719
				mf.markOrbit(Face(ee)) ;
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 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 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 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
            }

            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::subdivideFaceGeom<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)
854
	DartMarkerStore<typename PFP::MAP> mne(map);
855 856 857 858 859 860
    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);
861
			mne.markOrbit(Edge(newEdges[i]));
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
        }
    }

    //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) ;

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













































































template <typename PFP>
963
void subdivideEdgeWrong(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
964 965 966
{
    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") ;
967
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
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

    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>
994
void subdivideFaceWrong(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, SubdivideType sType)
995 996 997
{
    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") ;
998
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
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

    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);
//
//		//out << "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] ;

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
        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)
1157
Dart subdivideVolumeClassicWrong(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
1158 1159 1160
{
    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") ;
1161
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

    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
     */

1184 1185
	DartMarkerStore<typename PFP::MAP> mf(map);		// Lock a face marker to save one dart per face
	DartMarkerStore<typename PFP::MAP> mv(map);
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 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 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 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353

    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)
1354
	DartMarkerStore<typename PFP::MAP> mne(map);
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    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;
}


1371 1372 1373 1374 1375 1376
/************************************************************************************************
 * 									Simplification												*
 ************************************************************************************************/


template <typename PFP>
1377
void coarsenEdge(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
{
	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>
1389
void coarsenFace(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, SubdivideType sType)
1390 1391
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
1392
	assert(map.faceCanBeCoarsened(d) || !"Trying to coarsen a non-subdivided face or a more than once subdivided face") ;
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

	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))
1436
			IHM::coarsenEdge<PFP>(map, fit, position) ;
1437 1438 1439 1440 1441
		fit = map.phi1(fit) ;
	} while(fit != d) ;
}

template <typename PFP>
1442
void coarsenVolume(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
{
	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
1460
	Traversor3WF<typename PFP::MAP> trav3WF(map, d, true);
1461 1462 1463
	for(Dart dit = trav3WF.begin() ; dit != trav3WF.end() ; dit = trav3WF.next())
	{
		if(map.faceCanBeCoarsened(dit))
1464
			IHM::coarsenFace<PFP>(map, dit, position, IHM::S_QUAD);
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
	}
}




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










untereiner's avatar
untereiner committed
1484
template <typename PFP>
1485
Dart subdivideVolumeClassic2(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
untereiner's avatar
untereiner committed
1486 1487 1488
{
	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") ;
1489
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
untereiner's avatar
untereiner committed
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499

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

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

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

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

untereiner's avatar
untereiner committed
1504 1505 1506
	/*
	 * Subdivide Faces
	 */
1507 1508
	std::vector<std::pair<Dart,Dart> > subdividedFaces;
	subdividedFaces.reserve(128);
untereiner's avatar
untereiner committed
1509 1510 1511 1512 1513 1514

	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))
1515
			IHM::subdivideFace<PFP>(map, dit, position, IHM::S_QUAD);
untereiner's avatar
untereiner committed
1516

1517
		//save darts from the central vertex of each subdivided face
untereiner's avatar
untereiner committed
1518
		unsigned int cur = map.getCurrentLevel() ;
1519 1520 1521
		unsigned int fLevel = map.faceLevel(dit);
		map.setCurrentLevel(fLevel + 1) ;
		map.saveRelationsAroundVertex(map.phi2(map.phi1(dit)), subdividedFaces);
untereiner's avatar
untereiner committed
1522 1523 1524
		map.setCurrentLevel(cur);
	}

1525 1526 1527 1528
	cur = map.getCurrentLevel() ;
	unsigned int fLevel = map.faceLevel(old);
	map.setCurrentLevel(fLevel + 1) ;
	map.unsewAroundVertex(subdividedFaces);
untereiner's avatar
untereiner committed
1529 1530
	map.setCurrentLevel(cur);

untereiner's avatar
untereiner committed
1531 1532 1533 1534 1535 1536 1537
	/*
	 * Create inside volumes
	 */
	std::vector<Dart> newEdges;	//save darts from inner edges
	newEdges.reserve(50);
	Dart centralDart = NIL;

untereiner's avatar
untereiner committed
1538 1539
	//unsigned int degree = 0 ;
	//typename PFP::VEC3 volCenter ;
untereiner's avatar
untereiner committed
1540

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

untereiner's avatar
untereiner committed
1543 1544 1545 1546
	for(Dart dit = traV.begin(); dit != traV.end(); dit = traV.next())
	{
		//volCenter += position[d];
		//++degree;
untereiner's avatar
untereiner committed
1547

1548
		centralDart = map.quadranguleFace(dit);
untereiner's avatar
untereiner committed
1549
		position[centralDart] = volCenter;
untereiner's avatar
untereiner committed
1550
	}
1551
	//volCenter /= double(degree);
untereiner's avatar
untereiner committed
1552

1553
	for (std::vector<std::pair<Dart,Dart> >::iterator it = subdividedFaces.begin(); it != subdividedFaces.end(); ++it)
untereiner's avatar
untereiner committed
1554
	{
1555 1556
		Dart f1 = map.phi2((*it).first);
		Dart f2 = map.phi2((*it).second);
untereiner's avatar
untereiner committed
1557

1558
		if(map.isBoundaryFace(f1) && map.isBoundaryFace(f2))
untereiner's avatar
untereiner committed
1559
		{
1560
            //std::cout << "plop" << std::endl;
1561
			map.sewVolumes(f1, f2, false);
untereiner's avatar
untereiner committed
1562 1563 1564
		}
	}

untereiner's avatar
untereiner committed
1565
	position[centralDart] = volCenter;
1566 1567


untereiner's avatar
untereiner committed
1568 1569


1570 1571 1572 1573 1574
//	//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
1575
//
1576
//
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
//		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
1595 1596 1597

	//LA copie de L'id est a gerer avec le sewVolumes normalement !!!!!!
	//id pour les aretes interieurs : (i.e. 6 pour un hexa)
1598
	DartMarker<typename PFP::MAP> mne(map);
untereiner's avatar
untereiner committed
1599 1600 1601 1602 1603 1604
	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
1605
			mne.markOrbit<EDGE>(newEdges[i]);
untereiner's avatar
untereiner committed
1606 1607
		}
	}
1608 1609 1610


	//plonger a la fin de la boucle ????
untereiner's avatar
untereiner committed
1611 1612 1613

	map.setCurrentLevel(cur) ;

1614
	return subdividedFaces.front().first;
untereiner's avatar
untereiner committed
1615

untereiner's avatar
untereiner committed
1616 1617
}

untereiner's avatar
untereiner committed
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627









template <typename PFP>
1628
void subdivideEdgeLoop(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
untereiner's avatar
untereiner committed
1629 1630 1631
{
    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") ;
1632
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
untereiner's avatar
untereiner committed
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

    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>
1658
void subdivideFaceLoop(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, SubdivideType sType)
untereiner's avatar
untereiner committed
1659 1660 1661
{
    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") ;
1662
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
untereiner's avatar
untereiner committed
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 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 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825

    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
1826
template <typename PFP>
1827
void subdivideLoop(typename PFP::MAP& map, Dart d, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
untereiner's avatar
untereiner committed
1828 1829 1830
{
	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") ;
1831
	assert(!map.isBoundaryMarked(3,d) || !"Trying to subdivide a dart marked boundary");
untereiner's avatar
untereiner committed
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841

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

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

	/*
	 * Compute volume centroid
	 */
1842
	typename PFP::VEC3 volCenter =  Algo::Surface::Geometry::volumeCentroid<PFP>(map, old, position);
untereiner's avatar
untereiner committed
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854

	/*
	 * 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
1855
            IHM::subdivideFaceLoop<PFP>(map, dit, position, IHM::S_TRI);
untereiner's avatar
untereiner committed
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893

		//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;
1894
		Dart f1 = map.phi1(dit);
untereiner's avatar
untereiner committed
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
		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) ;

1907 1908 1909 1910 1911
		for(std::vector<Dart>::iterator it = v.begin() ; it != v.end() ; ++it)
		{
			map.setEdgeId(*it,map.getEdgeId(*it), EDGE);
		}

untereiner's avatar
untereiner committed
1912
		//if is not a tetrahedron
1913 1914
		unsigned int fdeg = map.faceDegree(map.phi2(f1));
		if(fdeg > 3)
untereiner's avatar
untereiner committed
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
		{
			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);
1926 1927 1928 1929

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

untereiner's avatar
untereiner committed
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
			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) ;

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

untereiner's avatar
untereiner committed
1944 1945 1946 1947 1948 1949
				newEdges.push_back(map.phi1(dd));

				ne = map.phi2(map.phi_1(ne));
				dd = map.phi1(dd);
			}
			while(dd != stop);
1950 1951 1952 1953

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

untereiner's avatar
untereiner committed
1954
		}
1955 1956 1957 1958 1959
		else
		{
			unsigned int idface = map.getNewFaceId();
			map.setFaceId(map.phi2(v.front()),idface, FACE);
		}