ihm3_PrimalAdapt.hpp 28.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional m_maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Multiresolution/IHM3/ihm3_PrimalAdapt.h"
26
#include "Topology/generic/traversor/traversor2.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

namespace CGoGN
{

namespace Algo
{

namespace Volume
{

namespace MR
{

namespace Primal
{

namespace Adaptive
{

template <typename PFP>
47 48
IHM3<PFP>::IHM3(MAP& map) :
	m_map(map),
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    shareVertexEmbeddings(true),
    vertexVertexFunctor(NULL),
    edgeVertexFunctor(NULL),
    faceVertexFunctor(NULL)
{

}

/***************************************************
 *               CELLS INFORMATION                 *
 ***************************************************/

template <typename PFP>
unsigned int IHM3<PFP>::edgeLevel(Dart d)
{
64
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
65

66 67
	// the level of an edge is the maximum of the
	// insertion levels of its darts
68

69 70 71
	unsigned int ld = m_map.getDartLevel(d) ;
	unsigned int ldd = m_map.getDartLevel(m_map.phi2(d)) ;
	unsigned int max = ld > ldd ? ld : ldd;
72

73
	return max;
74 75 76 77 78
}

template <typename PFP>
unsigned int IHM3<PFP>::faceLevel(Dart d)
{
79
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"faceLevel : called with a dart inserted after current level") ;
80

81
	if(m_map.getCurrentLevel() == 0)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        return 0 ;

    Dart it = d ;
    Dart old = it ;
    unsigned int l_old = m_map.getDartLevel(old) ;
    unsigned int fLevel = edgeLevel(it) ;
    do
    {
        it = m_map.phi1(it) ;
        unsigned int dl = m_map.getDartLevel(it) ;
        if(dl < l_old)							// compute the oldest dart of the face
        {										// in the same time
            old = it ;
            l_old = dl ;
        }										// in a first time, the level of a face
        unsigned int l = edgeLevel(it) ;		// is the minimum of the levels
        fLevel = l < fLevel ? l : fLevel ;		// of its edges
    } while(it != d) ;
100
/*
101 102 103 104 105 106 107 108 109 110
    unsigned int cur = m_map.getCurrentLevel() ;
    m_map.setCurrentLevel(fLevel) ;

    unsigned int nbSubd = 0 ;
    it = old ;
    unsigned int eId = m_map.getEdgeId(old) ;			// the particular case of a face
    do											// with all neighboring faces regularly subdivided
    {											// but not the face itself
        ++nbSubd ;								// is treated here
        it = m_map.phi1(it) ;
111
	} while(m_map.getEdgeId(it) == eId) ;
112 113 114 115 116 117 118 119

    while(nbSubd > 1)
    {
        nbSubd /= 2 ;
        --fLevel ;
    }

    m_map.setCurrentLevel(cur) ;
120
*/
121 122 123 124 125 126
    return fLevel ;
}

template <typename PFP>
unsigned int IHM3<PFP>::volumeLevel(Dart d)
{
127
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
128

129
	if(m_map.getCurrentLevel() == 0)
130 131
        return 0 ;

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	Dart oldest = d ;
	unsigned int vLevel = std::numeric_limits<unsigned int>::max(); //hook sioux

	//First : the level of a volume is the minimum of the levels of its faces
	Traversor3WF<typename PFP::MAP> travF(m_map, d);
	for (Dart dit = travF.begin(); dit != travF.end(); dit = travF.next())
	{
		// in a first time, the level of a face
		//the level of the volume is the minimum of the
		//levels of its faces
		unsigned int fLevel = faceLevel(dit);
		vLevel = fLevel < vLevel ? fLevel : vLevel ;
	}

	/*
147 148 149
    //First : the level of a volume is the
    //minimum of the levels of its faces

150
	DartMarkerStore<MAP> mark(m_map);		// Lock a marker
151 152 153 154 155 156 157 158 159 160 161 162

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

    Dart oldest = d ;
    unsigned int vLevel = std::numeric_limits<unsigned int>::max() ; //hook de ouf

    //parcours les faces du volume au niveau courant
    //on cherche le brin de niveau le plus bas de la hierarchie
    //on note le niveau le plus bas de la hierarchie
163
	mark.markOrbit(Face(d)) ;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    for(unsigned int i = 0; i < visitedFaces.size(); ++i)
    {
        Dart e = visitedFaces[i] ;

        // in a first time, the level of a face
        //the level of the volume is the minimum of the
        //levels of its faces

        //
        // Compute the level of this face
        // and the oldest dart
        //
        Dart it = e ;
        Dart old = it ;
        unsigned int l_old = m_map.getDartLevel(old) ;
        unsigned int fLevel = edgeLevel(it) ;
        do
        {
182
			it = m_map.phi1(it) ;
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
            unsigned int dl = m_map.getDartLevel(it) ;
            if(dl < l_old)							// compute the oldest dart of the face
            {										// in the same time
                old = it ;
                l_old = dl ;
            }										// in a first time, the level of a face
            unsigned int l = edgeLevel(it) ;		// is the minimum of the levels
            fLevel = l < fLevel ? l : fLevel ;		// of its edges
        } while(it != e) ;

        unsigned int cur = m_map.getCurrentLevel() ;
        m_map.setCurrentLevel(fLevel) ;

        unsigned int nbSubd = 0 ;
        it = old ;
        unsigned int eId =  m_map.getEdgeId(old) ;			// the particular case of a face
        do											// with all neighboring faces regularly subdivided
        {											// but not the face itself
            ++nbSubd ;								// is treated here
            it = m_map.phi1(it) ;
        } while( m_map.getEdgeId(it) == eId) ;

        while(nbSubd > 1)
        {
            nbSubd /= 2 ;
            --fLevel ;
        }

        m_map.setCurrentLevel(cur) ;

        //
        // compute the minimum level of the volume
        // if the level of this face is lower than the saved volume level
        //
        vLevel = fLevel < vLevel ? fLevel : vLevel ;

        //
        // compute the oldest dart from the volume
        // if the oldest dart from this face is oldest than the oldest saved dart
        //
223
		if(m_map.getDartLevel(old) < m_map.getDartLevel(oldest))
224 225 226 227 228 229 230 231 232 233 234
            oldest = old ;

        //
        // add all face neighbours to the table
        //
        do
        {
            Dart ee = m_map.phi2(e) ;
            if(!mark.isMarked(ee)) // not already marked
            {
                visitedFaces.push_back(ee) ;
235
				mark.markOrbit(Face(ee)) ;
236 237 238 239
            }
            e = m_map.phi1(e) ;
        } while(e != visitedFaces[i]) ;
    }
240
	*/
241 242 243 244 245 246 247 248 249 250 251 252 253 254


    //Second : the case of all faces regularly subdivided but not the volume itself
    unsigned int cur = m_map.getCurrentLevel() ;
    m_map.setCurrentLevel(vLevel) ;

    unsigned int nbSubd = 0 ;
    Dart it = oldest ;
    unsigned int eId = m_map.getEdgeId(oldest) ;

    do
    {
        ++nbSubd ;
        it = m_map.phi1(it) ;
255
	} while(m_map.getEdgeId(it) == eId) ;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271


    while(nbSubd > 1)
    {
        nbSubd /= 2 ;
        --vLevel ;
    }

    m_map.setCurrentLevel(cur) ;

    return vLevel;
}

template <typename PFP>
Dart IHM3<PFP>::faceOldestDart(Dart d)
{
272
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    Dart it = d ;
    Dart oldest = it ;
    unsigned int l_old = m_map.getDartLevel(oldest) ;
    do
    {
        unsigned int l = m_map.getDartLevel(it) ;
        if(l == 0)
            return it ;
        if(l < l_old)
        //if(l < l_old || (l == l_old && it < oldest))
        {
            oldest = it ;
            l_old = l ;
        }
        it = m_map.phi1(it) ;
    } while(it != d) ;
    return oldest ;
}

template <typename PFP>
Dart IHM3<PFP>::volumeOldestDart(Dart d)
{
295
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
296 297 298 299

    Dart oldest = d;
    unsigned int l_old = m_map.getDartLevel(oldest);

300
	Traversor3WF<typename PFP::MAP> trav3WF(m_map, oldest);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    for(Dart dit = trav3WF.begin() ; dit != trav3WF.end() ; dit = trav3WF.next())
    {
        Dart old = faceOldestDart(dit);
        unsigned int l = m_map.getDartLevel(old);
        if(l < l_old)
        {
            oldest = old;
            l_old = l;
        }
    }

    return oldest;
}

template <typename PFP>
bool IHM3<PFP>::edgeIsSubdivided(Dart d)
{
318 319 320 321 322 323 324 325
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;

	//TODO replace by phi1
	Dart d2 = m_map.phi2(d) ;
	m_map.incCurrentLevel();
	Dart d2_l = m_map.phi2(d) ;
	m_map.decCurrentLevel() ;
	if(d2 != d2_l)
326 327 328 329 330 331 332 333
        return true ;
    else
        return false ;
}

template <typename PFP>
bool IHM3<PFP>::edgeCanBeCoarsened(Dart d)
{
334
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
335 336 337 338 339 340 341 342 343 344

    bool subd = false ;
    bool subdOnce = true ;
    bool degree2 = false ;

    if(edgeIsSubdivided(d))
    {
        subd = true ;
        m_map.incCurrentLevel() ;

345
		if(m_map.vertexDegree(m_map.phi1(d)) == 2)
346 347 348 349 350 351 352 353 354 355 356 357 358
        {
            degree2 = true ;
            if(edgeIsSubdivided(d))
                subdOnce = false ;
        }
        m_map.decCurrentLevel() ;
    }
    return subd && degree2 && subdOnce ;
}

template <typename PFP>
bool IHM3<PFP>::faceIsSubdivided(Dart d)
{
359 360 361 362 363 364 365
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;

//	if(m_map.getCurrentLevel() == m_map.getMaxLevel())
//		return false ;

	unsigned int fLevel = faceLevel(d) ;
	if(fLevel < m_map.getCurrentLevel())
366 367 368
        return false ;

    bool subd = false ;
369 370
	m_map.incCurrentLevel();
	if(m_map.getDartLevel(m_map.phi1(d)) == m_map.getCurrentLevel() && m_map.getEdgeId(m_map.phi1(d)) != m_map.getEdgeId(d))
371
        subd = true ;
372
	m_map.decCurrentLevel();
373 374 375 376 377 378 379

    return subd ;
}

template <typename PFP>
bool IHM3<PFP>::faceCanBeCoarsened(Dart d)
{
380
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
381 382 383 384 385 386 387 388 389 390 391 392 393 394

    bool subd = false;
    bool subdOnce = true;
    bool subdNeighborhood = false; //deux volumes voisins de la face ne sont pas subdivise

    if(faceIsSubdivided(d))
    {
        subd = true;
        Dart d3 = m_map.phi3(d);

        //tester si le volume voisin est subdivise
        if(d3 != d && volumeIsSubdivided(d3))
            subdNeighborhood = true;

395 396 397
		unsigned int cur = m_map.getCurrentLevel();
		m_map.setCurrentLevel(cur + 1) ;
		//tester si la face subdivise a des faces subdivise
398 399 400 401 402 403 404
        Dart cf = m_map.phi1(d);

        do
        {
            if(faceIsSubdivided(cf))
                subdOnce = false;

405
			cf = m_map.phi2(m_map.phi1(cf));
406 407 408
        }
        while(subdOnce && cf != m_map.phi1(d));

409 410
		m_map.setCurrentLevel(cur) ;
	}
411 412 413 414 415 416 417

    return subd && !subdNeighborhood && subdOnce;
}

template <typename PFP>
bool IHM3<PFP>::volumeIsSubdivided(Dart d)
{
418 419 420 421
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;

	unsigned int vLevel = volumeLevel(d);
	if(vLevel < m_map.getCurrentLevel())
422 423
        return false;

424
//	bool subd = false;
425

426 427 428 429 430
//	unsigned int cur = m_map.getCurrentLevel();
//	m_map.setCurrentLevel(cur + 1) ;
//	if(m_map.getDartLevel(m_map.phi2(m_map.phi1(m_map.phi1(d)))) == m_map.getCurrentLevel() && m_map.getFaceId(m_map.phi2(m_map.phi1(m_map.phi1(d)))) != m_map.getFaceId(d))
//		subd = true;
//	m_map.setCurrentLevel(cur) ;
431

432
//	std::cout << "volume is subdivided ? " << ( subd ? "true" : "false" ) << std::endl;
433 434

//	return subd;
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

	bool facesAreSubdivided = faceIsSubdivided(d) ;
	//bool facesAreSubdivided = true ;

	Traversor3WF<MAP> trav3WF(m_map, d);
	for(Dart dit = trav3WF.begin() ; dit != trav3WF.end() ; dit = trav3WF.next())
	{
		// in a first time, the level of a face
		//the level of the volume is the minimum of the
		//levels of its faces

		facesAreSubdivided &= faceIsSubdivided(dit) ;
	}

	//but not the volume itself
	bool subd = false;
	m_map.incCurrentLevel() ;
	if(facesAreSubdivided && m_map.getDartLevel(m_map.phi2(m_map.phi1(m_map.phi1(d)))) == m_map.getCurrentLevel() && m_map.getFaceId(m_map.phi2(m_map.phi1(m_map.phi1(d)))) != m_map.getFaceId(d))
		subd = true;
	m_map.decCurrentLevel() ;

	return subd;

458 459 460 461 462
}

template <typename PFP>
bool IHM3<PFP>::volumeIsSubdividedOnce(Dart d)
{
463
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
464 465 466 467 468 469 470 471
    unsigned int vLevel = volumeLevel(d);
    if(vLevel < m_map.getCurrentLevel())
        return false;

    bool subd = false ;
    bool subdOnce = true ;

    m_map.incCurrentLevel() ;
472
	if(m_map.getDartLevel(m_map.phi2(m_map.phi1(m_map.phi1(d)))) == m_map.getCurrentLevel() && m_map.getFaceId(m_map.phi2(m_map.phi1(m_map.phi1(d)))) != m_map.getFaceId(d))
473 474 475
    {
        subd = true;
        m_map.incCurrentLevel() ;
476 477
		Dart dcenter = m_map.phi_1(m_map.phi2(m_map.phi1(d)));
		Traversor3VW<ImplicitHierarchicalMap3> trav3(m_map, dcenter);
478 479
        for(Dart dit = trav3.begin() ; subdOnce && dit != trav3.end() && subdOnce; dit = trav3.next())
        {
480
			if(m_map.getDartLevel(m_map.phi2(m_map.phi1(m_map.phi1(dit)))) == m_map.getCurrentLevel() && m_map.getFaceId(m_map.phi2(m_map.phi1(m_map.phi1(dit)))) != m_map.getFaceId(dit))
481 482 483 484 485 486 487 488 489 490
                subdOnce = false;
        }
        m_map.decCurrentLevel() ;
    }
    m_map.decCurrentLevel() ;
    return subd && subdOnce;

//	//si le volume est subdivise
//
//	//test si toutes les faces sont subdivisee
491
//	DartMarkerStore<MAP> mark(m_map);		// Lock a marker
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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
//
//	std::vector<Dart> visitedFaces;		// Faces that are traversed
//	visitedFaces.reserve(512);
//	visitedFaces.push_back(d);			// Start with the face of d
//	std::vector<Dart>::iterator face;
//
//	bool facesAreSubdivided = faceIsSubdivided(d) ;
//
//	//parcours les faces du volume au niveau courant
//	//on cherche le brin de niveau le plus bas de la hierarchie
//	//on note le niveau le plus bas de la hierarchie
//	mark.markOrbit<FACE>(d) ;
//	for(unsigned int i = 0; i < visitedFaces.size(); ++i)
//	{
//		Dart e = visitedFaces[i] ;
//
//		// in a first time, the level of a face
//		//the level of the volume is the minimum of the
//		//levels of its faces
//
//		facesAreSubdivided &= faceIsSubdivided(e) ;
//
//		do	// add all face neighbours to the table
//		{
//			Dart ee = phi2(e) ;
//			if(!mark.isMarked(ee)) // not already marked
//			{
//				visitedFaces.push_back(ee) ;
//				mark.markOrbit<FACE>(ee) ;
//			}
//			e = phi1(e) ;
//		} while(e != visitedFaces[i]) ;
//	}

}

/***************************************************
 *               SUBDIVISION                       *
 ***************************************************/

template <typename PFP>
void IHM3<PFP>::subdivideEdge(Dart d)
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
536
	assert(!edgeIsSubdivided(d) || !"Trying to subdivide an already subdivided edge") ;
537 538 539 540 541 542 543 544 545 546 547 548

    unsigned int eLevel = edgeLevel(d) ;

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

    Dart dd = m_map.phi2(d) ;

    m_map.setCurrentLevel(eLevel + 1) ;

    m_map.cutEdge(d) ;
    unsigned int eId = m_map.getEdgeId(d) ;
549 550
	m_map.setEdgeId(m_map.phi1(d), eId) ; //mise a jour de l'id d'arrete sur chaque moitie d'arete
	m_map.setEdgeId(m_map.phi1(dd), eId) ;
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

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

    (*edgeVertexFunctor)(m_map.phi1(d)) ;

    m_map.setCurrentLevel(cur) ;
}

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

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

template <typename PFP>
unsigned int IHM3<PFP>::subdivideFace(Dart d, bool triQuad)
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
576
	assert(!faceIsSubdivided(d) || !"Trying to subdivide an already subdivided face") ;
577 578 579 580 581 582 583 584

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

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

    unsigned int degree = 0 ;
585
	Traversor3FE<typename PFP::MAP>  travE(m_map, old);
586 587 588 589
    for(Dart it = travE.begin(); it != travE.end() ; it = travE.next())
    {
        ++degree ;						// compute the degree of the face

590 591
		if(!edgeIsSubdivided(it))							// first cut the edges (if they are not already)
			subdivideEdge(it) ;	// and compute the degree of the face
592 593 594 595 596 597 598 599 600
    }

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

    if((degree == 3) && triQuad)					// if subdividing a triangle
    {
        Dart dd = m_map.phi1(old) ;
        Dart e = m_map.phi1(dd) ;
        (*vertexVertexFunctor)(e) ;
601
		e = m_map.phi1(e) ;
602 603

        m_map.splitFace(dd, e) ;					// insert a new edge
604 605 606
		//unsigned int id = m_map.getNewEdgeId() ;
		unsigned int id = m_map.triRefinementEdgeId(m_map.phi_1(dd));
		m_map.setEdgeId(m_map.phi_1(dd), id) ;		// set the edge id of the inserted edge to the next available id
607 608 609 610 611 612 613 614 615 616

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

        dd = e ;
        e = m_map.phi1(dd) ;
        (*vertexVertexFunctor)(e) ;
        e = m_map.phi1(dd);
        m_map.splitFace(dd, e) ;
617 618 619
		//id = m_map.getNewEdgeId() ;
		id = m_map.triRefinementEdgeId(m_map.phi_1(dd));
		m_map.setEdgeId(m_map.phi_1(dd), id) ;
620 621 622 623 624 625 626 627 628

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

        dd = e ;
        e = m_map.phi1(dd) ;
        (*vertexVertexFunctor)(e) ;
        e = m_map.phi1(dd);
        m_map.splitFace(dd, e) ;
629 630 631
		//id = m_map.getNewEdgeId() ;
		id = m_map.triRefinementEdgeId(m_map.phi_1(dd));
		m_map.setEdgeId(m_map.phi_1(dd), id) ;
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

        m_map.setFaceId(dd, idface, FACE) ;
        m_map.setFaceId(e, idface, FACE) ;
    }
    else
    {
        Dart dd = m_map.phi1(old) ;
        Dart next = m_map.phi1(dd) ;
        (*vertexVertexFunctor)(next) ;
        next = m_map.phi1(next);
        m_map.splitFace(dd, next) ;
        Dart ne = m_map.phi2(m_map.phi_1(dd));
        Dart ne2 = m_map.phi2(ne);

        m_map.cutEdge(ne) ;
647 648 649 650 651 652 653 654
		//unsigned int id = m_map.getNewEdgeId() ;
		unsigned int id = m_map.getQuadRefinementEdgeId(m_map.phi2(ne));
		m_map.setEdgeId(ne, id) ;
		//id = m_map.getNewEdgeId() ;
		id = m_map.getQuadRefinementEdgeId(m_map.phi2(ne2));
		m_map.setEdgeId(ne2, id) ;

		dd = m_map.phi1(next) ;
655
        (*vertexVertexFunctor)(dd) ;
656
		dd = m_map.phi1(dd);
657
        while(dd != ne)
658
        {          
659 660
            m_map.splitFace(m_map.phi1(ne), dd) ;
            Dart nne = m_map.phi2(m_map.phi_1(dd)) ;
661 662 663 664 665 666
			//id = m_map.getNewEdgeId() ;
			id = m_map.getQuadRefinementEdgeId(m_map.phi2(nne));
			m_map.setEdgeId(nne, id) ;
			dd = m_map.phi1(dd) ;
			(*vertexVertexFunctor)(dd) ;
			dd = m_map.phi1(dd) ;
667 668 669 670 671 672 673 674 675 676 677 678
        }

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

679
        (*faceVertexFunctor)(m_map.phi1(ne)) ;		
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    }

    m_map.setCurrentLevel(cur) ;
}

template <typename PFP>
void IHM3<PFP>::coarsenFace(Dart d)
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    assert(faceCanBeCoarsened(d) || !"Trying to coarsen a non-subdivided face or a more than once subdivided face") ;

    unsigned int cur = m_map.getCurrentLevel() ;

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

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

    if(degree == 3)
    {

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

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

    }

    fit = d ;
    do
    {
        if(edgeCanBeCoarsened(fit))
            coarsenEdge(fit) ;
        fit = m_map.phi1(fit) ;
    } while(fit != d) ;
}

template <typename PFP>
739
Dart IHM3<PFP>::subdivideVolume(Dart d, bool triQuad, bool OneLevelDifference)
740 741 742 743 744 745 746 747 748 749
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    assert(!volumeIsSubdivided(d) || !"Trying to subdivide an already subdivided volume") ;

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

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

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
	if(OneLevelDifference)
	{
		Traversor3WF<typename PFP::MAP> traF(m_map, old);
		for(Dart dit = traF.begin(); dit != traF.end(); dit = traF.next())
		{
			Dart nv = m_map.phi3(dit);
			if(!m_map.isBoundaryMarked(3, nv))
				if(volumeLevel(nv) == vLevel - 1)
					subdivideVolume(nv,triQuad,OneLevelDifference);
		}
	}

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

	Traversor3WV<typename PFP::MAP> traV(m_map, old);
	for(Dart dit = traV.begin(); dit != traV.end(); dit = traV.next())
	{
		oldEdges.push_back(dit);
		std::cout << "rec corner : " << dit << std::endl;
	}

	std::vector<std::pair<Dart, Dart> > subdividedFaces;
	subdividedFaces.reserve(128);

	Traversor3WF<typename PFP::MAP> traF(m_map, old);
	for(Dart dit = traF.begin(); dit != traF.end(); dit = traF.next())
	{
		//if needed subdivide face
		if(!faceIsSubdivided(dit))
			subdivideFace(dit, triQuad);

		//save a dart from the subdivided face
		m_map.incCurrentLevel();

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

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

		m_map.decCurrentLevel();
	}

	Dart centralDart = NIL;
802 803 804 805

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

806 807
	m_map.setCurrentLevel(vLevel + 1) ;

808
    //Second step : deconnect each corner, close each hole, subdivide each new face into 3
809 810 811 812 813 814 815 816
//	Traversor3WV<typename PFP::MAP> traWV(m_map, old);
//	for(Dart ditWV = traWV.begin(); ditWV != traWV.end(); ditWV = traWV.next())
//	{
	for (std::vector<Dart>::iterator edge = oldEdges.begin(); edge != oldEdges.end(); ++edge)
	{
		Dart e = *edge;
//		m_map.setCurrentLevel(vLevel + 1) ; // go to the next level to perform volume subdivision
//		Dart e = ditWV;
817 818 819 820 821 822 823 824 825 826

        std::vector<Dart> v ;

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

            e = m_map.phi2(m_map.phi_1(e));
        }
827
		while(e != *edge);
828

829
		std::cout << "corner : " << *edge << std::endl;
830

831
		//m_map.splitVolume(v) ;
832

833 834 835
//		Dart old = m_map.phi2(m_map.phi1(*edge));
//        Dart dd = m_map.phi1(m_map.phi1(old)) ;
//        m_map.splitFace(old,dd) ;
836

837 838
//        unsigned int idface = m_map.getNewFaceId();
//        m_map.setFaceId(dd,idface, FACE);
839

840
//        Dart ne = m_map.phi1(m_map.phi1(old)) ;
841

842 843 844 845
//        m_map.cutEdge(ne);
//        centralDart = m_map.phi1(ne);
//        newEdges.push_back(ne);
//        newEdges.push_back(m_map.phi1(ne));
846

847 848
//        unsigned int id = m_map.getNewEdgeId() ;
//		m_map.setEdgeId(ne, id) ;
849

850 851 852 853 854
//        Dart stop = m_map.phi2(m_map.phi1(ne));
//        ne = m_map.phi2(ne);
//        do
//        {
//            dd = m_map.phi1(m_map.phi1(m_map.phi1(ne)));
855

856 857 858
//            m_map.splitFace(ne, dd) ;
//            unsigned int idface = m_map.getNewFaceId();
//            m_map.setFaceId(dd,idface, FACE);
859

860
//            newEdges.push_back(m_map.phi1(dd));
861

862 863 864 865
//            ne = m_map.phi2(m_map.phi_1(ne));
//            dd = m_map.phi1(m_map.phi1(dd));
//        }
//        while(dd != stop);
866

867 868
		//m_map.setCurrentLevel(vLevel) ;
    }
869

870
//	m_map.setCurrentLevel(vLevel + 1) ;
871

872
//	 m_map.deleteVolume(m_map.phi3(m_map.phi2(m_map.phi1(old))));
873

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
//    //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(m_map.isBoundaryFace(m_map.phi2(f1)) && m_map.isBoundaryFace(m_map.phi2(f2)))
//        {
//            //id pour toutes les faces interieures
//			m_map.sewVolumes(m_map.phi2(f1), m_map.phi2(f2),false);

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

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

    //replonger l'orbit de ditV.
898
	//Algo::Topo::setOrbitEmbedding<VERTEX>(m_map,centralDart, m_map.template getEmbedding<VERTEX>(centralDart));
899 900 901

    //LA copie de L'id est a gerer avec le sewVolumes normalement !!!!!!
    //id pour les aretes interieurs : (i.e. 6 pour un hexa)
902 903
//	DartMarkerStore<typename PFP::MAP> mne(m_map);
//    for(unsigned int i = 0; i < newEdges.size(); ++i)
904
//    {
905 906 907 908 909 910
//        if(!mne.isMarked(newEdges[i]))
//        {
//            unsigned int idedge = m_map.getNewEdgeId();
//			m_map.setEdgeId(newEdges[i], idedge);
//			mne.markOrbit(Edge(newEdges[i]));
//        }
911 912
//    }

913
//	(*volumeVertexFunctor)(centralDart) ;
914 915 916

    m_map.setCurrentLevel(cur) ;

917
	return centralDart;
918 919 920
}

template <typename PFP>
921
void IHM3<PFP>::coarsenVolume(Dart d)
922 923
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
924
	assert(volumeIsSubdividedOnce(d) || !"Trying to coarsen a non-subdivided volume or a more than once subdivided volume") ;
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

    unsigned int cur = m_map.getCurrentLevel() ;

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

    /*
     * simplifier les faces
     */
    Traversor3WF<typename PFP::m_map> trav3WF(m_map, d, true);
    for(Dart dit = trav3WF.begin() ; dit != trav3WF.end() ; dit = trav3WF.next())
    {
        if(faceCanBeCoarsened(dit))
            coarsenFace(dit);
    }
}

} // namespace Adaptive

} // namespace Primal

} // namespace MR

} // namespace Volume

} // namespace Algo

} // namespace CGoGN