ihm3_PrimalAdapt.hpp 27.8 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
//	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 ;
//	}
144 145


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

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

    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
162
	mark.markOrbit(Face(d)) ;
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    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
        {
181
			it = m_map.phi1(it) ;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            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
        //
222
		if(m_map.getDartLevel(old) < m_map.getDartLevel(oldest))
223 224 225 226 227 228 229 230 231 232 233
            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) ;
234
				mark.markOrbit(Face(ee)) ;
235 236 237 238
            }
            e = m_map.phi1(e) ;
        } while(e != visitedFaces[i]) ;
    }
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253


    //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) ;
254
	} while(m_map.getEdgeId(it) == eId) ;
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270


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

    m_map.setCurrentLevel(cur) ;

    return vLevel;
}

template <typename PFP>
Dart IHM3<PFP>::faceOldestDart(Dart d)
{
271
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    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)
{
294
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
295 296 297 298

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

299
	Traversor3WF<typename PFP::MAP> trav3WF(m_map, oldest);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    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)
{
317 318 319 320 321 322 323 324
	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)
325 326 327 328 329 330 331 332
        return true ;
    else
        return false ;
}

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

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

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

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

template <typename PFP>
bool IHM3<PFP>::faceIsSubdivided(Dart d)
{
358 359 360 361 362 363 364
	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())
365 366 367
        return false ;

    bool subd = false ;
368 369
	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))
370
        subd = true ;
371
	m_map.decCurrentLevel();
372 373 374 375 376 377 378

    return subd ;
}

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

    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;

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

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

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

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

    return subd && !subdNeighborhood && subdOnce;
}

template <typename PFP>
bool IHM3<PFP>::volumeIsSubdivided(Dart d)
{
417 418 419 420
	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())
421 422
        return false;

423
//	bool subd = false;
424

425 426 427 428 429
//	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) ;
430

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

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

	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;

457 458 459 460 461
}

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

    bool subd = false ;
    bool subdOnce = true ;

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

//	//si le volume est subdivise
//
//	//test si toutes les faces sont subdivisee
490
//	DartMarkerStore<MAP> mark(m_map);		// Lock a marker
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
//
//	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") ;
535
	assert(!edgeIsSubdivided(d) || !"Trying to subdivide an already subdivided edge") ;
536 537 538 539 540 541 542 543 544 545 546 547

    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) ;
548 549
	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) ;
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

    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") ;
575
	assert(!faceIsSubdivided(d) || !"Trying to subdivide an already subdivided face") ;
576 577 578 579 580 581 582 583

    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 ;
584
	Traversor3FE<typename PFP::MAP>  travE(m_map, old);
585 586 587 588
    for(Dart it = travE.begin(); it != travE.end() ; it = travE.next())
    {
        ++degree ;						// compute the degree of the face

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

    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) ;
600
		e = m_map.phi1(e) ;
601 602

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

        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) ;
616 617
		id = m_map.getNewEdgeId() ;
//		id = m_map.triRefinementEdgeId(m_map.phi_1(dd));
618
		m_map.setEdgeId(m_map.phi_1(dd), id) ;
619 620 621 622 623 624 625 626 627

        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) ;
628 629
		id = m_map.getNewEdgeId() ;
//		id = m_map.triRefinementEdgeId(m_map.phi_1(dd));
630
		m_map.setEdgeId(m_map.phi_1(dd), id) ;
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

        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) ;
646 647
		unsigned int id = m_map.getNewEdgeId() ;
//		unsigned int id = m_map.getQuadRefinementEdgeId(m_map.phi2(ne));
648
		m_map.setEdgeId(ne, id) ;
649 650
		id = m_map.getNewEdgeId() ;
//		id = m_map.getQuadRefinementEdgeId(m_map.phi2(ne2));
651 652 653
		m_map.setEdgeId(ne2, id) ;

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

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

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

    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>
738
Dart IHM3<PFP>::subdivideVolume(Dart d, bool triQuad, bool OneLevelDifference)
739 740 741 742 743 744 745 746 747 748
{
    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);

749 750 751 752 753 754 755 756 757 758 759 760 761 762
	std::cout << "current Level = " << m_map.getCurrentLevel() << std::endl;

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

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

	//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::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
785 786
//		m_map.incCurrentLevel();
		unsigned int cur = m_map.getCurrentLevel();
787

788 789
		unsigned int fLevel = faceLevel(dit); //puisque dans tous les cas, la face est subdivisee
		m_map.setCurrentLevel(fLevel + 1) ;
790 791 792 793 794 795 796 797 798 799

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

800 801
//		m_map.decCurrentLevel();
		m_map.setCurrentLevel(cur);
802 803 804
	}

	Dart centralDart = NIL;
805 806 807 808

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

809 810
	m_map.setCurrentLevel(vLevel + 1) ;

811
    //Second step : deconnect each corner, close each hole, subdivide each new face into 3
812 813 814 815 816 817
//	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;
818 819 820 821 822

        std::vector<Dart> v ;

        do
        {
823
			v.push_back(m_map.phi1(m_map.phi1(e)));
824 825 826
            v.push_back(m_map.phi1(e));
            e = m_map.phi2(m_map.phi_1(e));
        }
827
		while(e != *edge);
828

829
		m_map.splitVolume(v) ;
830

831 832 833
		Dart old = m_map.phi2(m_map.phi1(*edge));
		Dart dd = m_map.phi1(m_map.phi1(old)) ;
		m_map.splitFace(old,dd) ;
834

835 836
		unsigned int idface = m_map.getNewFaceId();
		m_map.setFaceId(dd,idface, FACE);
837

838
		Dart ne = m_map.phi1(m_map.phi1(old)) ;
839

840 841 842 843
		m_map.cutEdge(ne);
		centralDart = m_map.phi1(ne);
		newEdges.push_back(ne);
		newEdges.push_back(m_map.phi1(ne));
844

845 846
		unsigned int id = m_map.getNewEdgeId() ;
		m_map.setEdgeId(ne, id) ;
847

848 849 850 851 852
		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)));
853

854 855 856
			m_map.splitFace(ne, dd) ;
			unsigned int idface = m_map.getNewFaceId();
			m_map.setFaceId(dd,idface, FACE);
857

858
			newEdges.push_back(m_map.phi1(dd));
859

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

866 867 868
	if(vLevel < 1)
	{
	 m_map.deleteVolume(m_map.phi3(m_map.phi2(m_map.phi1(oldEdges.front()))));
869

870 871 872 873 874
	//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;
875

876 877 878 879
		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));
880

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

886 887 888 889 890 891
		//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);
	}
892 893

    //replonger l'orbit de ditV.
894
//	Algo::Topo::setOrbitEmbedding<VERTEX>(m_map,centralDart, m_map.template getEmbedding<VERTEX>(centralDart));
895 896 897

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

909 910 911
	(*volumeVertexFunctor)(centralDart) ;

//    m_map.setCurrentLevel(cur) ;
912

913
	return centralDart;
914 915 916
	}
	else
		return NIL;
917 918 919
}

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

    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