ihm2_PrimalAdapt.hpp 16.7 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional 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/IHM2/ihm2_PrimalAdapt.h"

namespace CGoGN
{

namespace Algo
{

namespace MR
{

namespace Primal
{

namespace Adaptive
{

template <typename PFP>
43
IHM2<PFP>::IHM2(MAP& map) :
44 45 46 47 48 49 50 51 52
	m_map(map),
	shareVertexEmbeddings(true),
	vertexVertexFunctor(NULL),
	edgeVertexFunctor(NULL),
	faceVertexFunctor(NULL)
{

}

53 54 55 56 57 58 59 60 61 62
/***************************************************
 *               CELLS INFORMATION                 *
 ***************************************************/

template <typename PFP>
unsigned int IHM2<PFP>::edgeLevel(Dart d)
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    unsigned int ld = m_map.getDartLevel(d);
//	unsigned int ldd = m_dartLevel[phi2(d)] ;	// the level of an edge is the maximum of the
Thomas Pitiot 's avatar
Thomas Pitiot committed
63 64
    unsigned int ldd = m_map.getDartLevel(m_map.phi1(d));
    return std::max(ldd,ld) ;
65 66 67 68 69 70 71 72 73 74
}

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

    if(m_map.getCurrentLevel() == 0)
        return 0 ;

untereiner's avatar
untereiner committed
75
// Methode 1
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//	unsigned int cur = m_curLevel ;
//	Dart it = d ;
//	Dart end = d ;
//	bool resetEnd = true ;
//	bool firstEdge = true ;
//	do
//	{
//		if(!resetEnd)
//			firstEdge = false ;
//
//		unsigned int eId = m_edgeId[it] ;
//		Dart next = it ;
//		do
//		{
//			unsigned int l = edgeLevel(next) ;
//			if(l < m_curLevel)
//				m_curLevel = l ;
//			else // l == curLevel
//			{
//				if(!firstEdge)
//				{
//					--m_curLevel ;
//					next = it ;
//				}
//			}
//			next = phi1(next) ;
//		} while(m_edgeId[next] == eId) ;
//		it = next ;
//
//		if(resetEnd)
//		{
//			end = it ;
//			resetEnd = false ;
//		}
//
//	} while(!firstEdge && it != end) ;
//
//	unsigned int fLevel = m_curLevel ;
//	m_curLevel = cur ;

untereiner's avatar
untereiner committed
116
//Methode 2
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    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) ;

Thomas Pitiot 's avatar
Thomas Pitiot committed
134 135
//    unsigned int cur = m_map.getCurrentLevel() ;
//    m_map.setCurrentLevel(fLevel) ;
136

Thomas Pitiot 's avatar
Thomas Pitiot committed
137 138 139 140 141 142 143 144 145 146
//    unsigned int nbSubd = 0 ;
//    it = old ;
//    unsigned int eId = m_map.getEdgeId(old) ;			// the particular case of a face
//    CGoGNout<<"catastrophe"<<m_map.getEdgeId(it)<<CGoGNendl;
//    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 && it!=old) ;
Thomas Pitiot 's avatar
Thomas Pitiot committed
147

Thomas Pitiot 's avatar
Thomas Pitiot committed
148 149 150 151 152
//    while(nbSubd > 1)
//    {
//        nbSubd /= 2 ;
//        --fLevel ;
//    }
153

Thomas Pitiot 's avatar
Thomas Pitiot committed
154
//    m_map.setCurrentLevel(cur) ;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

    return fLevel ;
}

template <typename PFP>
Dart IHM2<PFP>::faceOrigin(Dart d)
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    unsigned int cur = m_map.getCurrentLevel() ;
    Dart p = d ;
    unsigned int pLevel = m_map.getDartLevel(p) ;
    while(pLevel > 0)
    {
        p = faceOldestDart(p) ;
        pLevel = m_map.getDartLevel(p) ;
        m_map.setCurrentLevel(pLevel) ;
    }
    m_map.setCurrentLevel(cur) ;
    return p ;
}

template <typename PFP>
Dart IHM2<PFP>::faceOldestDart(Dart d)
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    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 ;
Thomas Pitiot 's avatar
Thomas Pitiot committed
188 189

        if(l < l_old || (l == l_old && it.index < oldest.index))
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        {
            oldest = it ;
            l_old = l ;
        }
        it = m_map.phi1(it) ;
    } while(it != d) ;
    return oldest ;
}

template <typename PFP>
bool IHM2<PFP>::edgeIsSubdivided(Dart d)
{
    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 ;

//	Dart d2 = phi2(d) ;
    Dart d1 = m_map.phi1(d) ;
    m_map.incCurrentLevel() ;
//	Dart d2_l = phi2(d) ;
    Dart d1_l = m_map.phi1(d) ;
    m_map.decCurrentLevel();
    if(d1 != d1_l)
        return true ;
    else
        return false ;
}

template <typename PFP>
bool IHM2<PFP>::edgeCanBeCoarsened(Dart d)
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    bool subd = false ;
    bool subdOnce = true ;
    bool degree2 = false ;
    if(edgeIsSubdivided(d))
    {
        subd = true ;
        Dart d2 = m_map.phi2(d) ;
        m_map.incCurrentLevel() ;
        if(m_map.vertexDegree(m_map.phi1(d)) == 2)
        {
            degree2 = true ;
            if(edgeIsSubdivided(d) || edgeIsSubdivided(d2))
                subdOnce = false ;
        }
        m_map.decCurrentLevel() ;

    }
    return subd && degree2 && subdOnce ;
}

template <typename PFP>
bool IHM2<PFP>::faceIsSubdivided(Dart d)
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;

    unsigned int fLevel = faceLevel(d) ;
Thomas Pitiot 's avatar
Thomas Pitiot committed
249
    if(fLevel < m_map.getCurrentLevel())
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        return false ;

    bool subd = false ;
    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))
        subd = true ;
    m_map.decCurrentLevel() ;
    return subd ;
}

template <typename PFP>
bool IHM2<PFP>::faceIsSubdividedOnce(Dart d)
{
    assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
    unsigned int fLevel = faceLevel(d) ;
    if(fLevel < m_map.getCurrentLevel())		// a face whose level in the current level map is lower than
        return false ;			// the current level can not be subdivided to higher levels

    unsigned int degree = 0 ;
    bool subd = false ;
    bool subdOnce = true ;
    Dart fit = d ;
    do
    {
        m_map.incCurrentLevel() ;
        if(m_map.getDartLevel(m_map.phi1(fit)) == m_map.getCurrentLevel() && m_map.getEdgeId(m_map.phi1(fit)) != m_map.getEdgeId(fit))
        {
            subd = true ;
            m_map.incCurrentLevel() ;
            if(m_map.getDartLevel(m_map.phi1(fit)) == m_map.getCurrentLevel() && m_map.getEdgeId(m_map.phi1(fit)) != m_map.getEdgeId(fit))
                subdOnce = false ;
            m_map.decCurrentLevel() ;
        }
        m_map.decCurrentLevel() ;
        ++degree ;
        fit = m_map.phi1(fit) ;
    } while(subd && subdOnce && fit != d) ;

    if(degree == 3 && subd)
    {
        m_map.incCurrentLevel() ;
        Dart cf = m_map.phi2(m_map.phi1(d)) ;
        m_map.incCurrentLevel() ;
        if(m_map.getDartLevel(m_map.phi1(cf)) == m_map.getCurrentLevel() && m_map.getEdgeId(m_map.phi1(cf)) != m_map.getEdgeId(cf))
            subdOnce = false ;
        m_map.decCurrentLevel() ;
        m_map.decCurrentLevel() ;
    }

    return subd && subdOnce ;
}

/***************************************************
 *               SUBDIVISION                       *
 ***************************************************/
305 306 307 308 309

template <typename PFP>
void IHM2<PFP>::subdivideEdge(Dart d)
{
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"subdivideEdge : called with a dart inserted after current level") ;
310
    assert(!edgeIsSubdivided(d) || !"Trying to subdivide an already subdivided edge") ;
311

312
    unsigned int eLevel = edgeLevel(d) ;
313 314 315 316

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

Lionel Untereiner's avatar
Lionel Untereiner committed
317
	Dart dd = m_map.phi2(d) ;
318

319 320
	m_map.setCurrentLevel(eLevel + 1) ;

321
    m_map.cutEdge(d) ;
322
	unsigned int eId = m_map.getEdgeId(d) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
323 324
	m_map.setEdgeId(m_map.phi1(d), eId) ;
	m_map.setEdgeId(m_map.phi1(dd), eId) ;
Thomas Pitiot 's avatar
update  
Thomas Pitiot committed
325 326 327 328 329 330 331 332
    if(edgeVertexFunctor!=NULL)
    {
        (*edgeVertexFunctor)(m_map.phi1(d)) ;
    }
    else
    {
        std::cout<<"edgefunctor not initialized"<<std::endl;
    }
333 334 335 336 337 338 339 340

	m_map.setCurrentLevel(cur) ;
}

template <typename PFP>
void IHM2<PFP>::coarsenEdge(Dart d)
{
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"coarsenEdge : called with a dart inserted after current level") ;
341
    assert(edgeCanBeCoarsened(d) || !"Trying to coarsen an edge that can not be coarsened") ;
342 343 344 345 346 347 348 349 350 351 352


	unsigned int cur = m_map.getCurrentLevel() ;
//	Dart e = m_map.phi2(d) ;
	m_map.setCurrentLevel(cur + 1) ;
//	unsigned int dl = m_map.getDartLevel(e) ;
//	m_map.setDartLevel(m_map.phi1(e), dl) ;
//	m_map.collapseEdge(e) ;
	m_map.uncutEdge(d) ;
	m_map.setCurrentLevel(cur) ;
}
Thomas Pitiot 's avatar
Thomas Pitiot committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366
template <typename PFP>
bool IHM2<PFP>:: checkForSurrounded (Dart d)
{
    Dart dd=m_map.phi1(d);
    while(dd!=d)
    {
        if(!faceIsSubdivided(m_map.phi2(dd)))
        {
            return false;
        }
        dd=m_map.phi1(dd);
    }
    return true;
}
367 368 369 370 371

template <typename PFP>
unsigned int IHM2<PFP>::subdivideFace(Dart d, bool triQuad, bool OneLevelDifference)
{
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"subdivideFace : called with a dart inserted after current level") ;
372
    assert(!faceIsSubdivided(d) || !"Trying to subdivide an already subdivided face") ;
373

374
    unsigned int fLevel = faceLevel(d) ;
Thomas Pitiot 's avatar
Thomas Pitiot committed
375
//    CGoGNout<<" Subdivision : "<<fLevel<<CGoGNendl;
376
    Dart old = faceOldestDart(d) ;
377 378 379 380 381 382 383 384 385 386

	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 ;
	Dart it = old ;
	do
	{
		++degree ;						// compute the degree of the face

387 388 389
        if(OneLevelDifference)
        {
            Dart nf = m_map.phi2(it) ;
Thomas Pitiot 's avatar
ok  
Thomas Pitiot committed
390
            if(!m_map.isBoundaryMarked(2, nf))
Thomas Pitiot 's avatar
Thomas Pitiot committed
391
            {
Thomas Pitiot 's avatar
ok  
Thomas Pitiot committed
392 393
                int levelneighbor=faceLevel(nf);
                if(levelneighbor== fLevel - 1)	// check if neighboring faces have to be subdivided first to avoid 2 level differencies
Thomas Pitiot 's avatar
Thomas Pitiot committed
394
                {
Thomas Pitiot 's avatar
ok  
Thomas Pitiot committed
395 396 397 398 399 400 401
                    subdivideFace(nf,triQuad) ;
                }
                else if(!faceIsSubdivided(nf))  //check if neighboring faces have to be subdivided first to avoid one cell surrounded by subdivided ones
                {

                    if(checkForSurrounded(nf))
                    {
Thomas Pitiot 's avatar
Thomas Pitiot committed
402

Thomas Pitiot 's avatar
ok  
Thomas Pitiot committed
403
                        subdivideFace(nf,triQuad,false);
Thomas Pitiot 's avatar
Thomas Pitiot committed
404

Thomas Pitiot 's avatar
ok  
Thomas Pitiot committed
405
                    }
Thomas Pitiot 's avatar
Thomas Pitiot committed
406 407
                }
            }
408
        }
409

410
        if(!edgeIsSubdivided(it))
411 412 413 414 415 416
			subdivideEdge(it) ;			// and cut the edges (if they are not already)
		it = m_map.phi1(it) ;
	} while(it != old) ;

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

Thomas Pitiot 's avatar
Thomas Pitiot committed
417
//    CGoGNout<<" degree : "<<degree<<CGoGNendl;
untereiner's avatar
untereiner committed
418
	if((degree == 3) && triQuad)					// if subdividing a triangle
419
    {
Thomas Pitiot 's avatar
Thomas Pitiot committed
420
//        CGoGNout<<" Subdivided a triangle "<<CGoGNendl;
421 422 423 424 425 426
        Dart dd = m_map.phi1(old) ;
        Dart e = m_map.phi1(dd) ;
        (*vertexVertexFunctor)(e) ;

        e = m_map.phi1(e) ;
        m_map.splitFace(dd, e) ;					// insert a new edge
untereiner's avatar
untereiner committed
427 428 429 430
//        unsigned int id = m_map.getNewEdgeId() ;
		unsigned int id = m_map.getTriRefinementEdgeId(m_map.phi_1(dd));
		m_map.setEdgeId(m_map.phi_1(dd), id) ;		// set the edge id of the inserted
		m_map.setEdgeId(m_map.phi_1(e), id) ;		// edge to the next available id
431 432 433 434 435 436

        dd = e ;
        e = m_map.phi1(dd) ;
        (*vertexVertexFunctor)(e) ;
        e = m_map.phi1(e) ;
        m_map.splitFace(dd, e) ;
untereiner's avatar
untereiner committed
437 438
		//id = m_map.getNewEdgeId() ;
		id = m_map.getTriRefinementEdgeId(m_map.phi_1(dd));
439 440 441 442 443 444 445 446
        m_map.setEdgeId(m_map.phi_1(dd), id) ;
        m_map.setEdgeId(m_map.phi_1(e), id) ;

        dd = e ;
        e = m_map.phi1(dd) ;
        (*vertexVertexFunctor)(e) ;
        e = m_map.phi1(e) ;
        m_map.splitFace(dd, e) ;
untereiner's avatar
untereiner committed
447 448
		//id = m_map.getNewEdgeId() ;
		id = m_map.getTriRefinementEdgeId(m_map.phi_1(dd));
449 450 451 452 453
        m_map.setEdgeId(m_map.phi_1(dd), id) ;
        m_map.setEdgeId(m_map.phi_1(e), id) ;
    }
    else											// if subdividing a polygonal face
    {
Thomas Pitiot 's avatar
Thomas Pitiot committed
454
        CGoGNout<<" Subdivided a polygone "<<degree<<CGoGNendl;
455 456 457 458 459 460 461 462 463
        Dart dd = m_map.phi1(old) ;
        Dart next = m_map.phi1(dd) ;
        (*vertexVertexFunctor)(next) ;
        next = m_map.phi1(next) ;
        m_map.splitFace(dd, next) ;	// insert a first edge
        Dart ne = m_map.alpha1(dd) ;
        Dart ne2 = m_map.phi2(ne) ;

        m_map.cutEdge(ne) ;							// cut the new edge to insert the central vertex
untereiner's avatar
untereiner committed
464 465
		//unsigned int id = m_map.getNewEdgeId() ;
		unsigned int id = m_map.getQuadRefinementEdgeId(m_map.phi2(ne));
466 467
        m_map.setEdgeId(ne, id) ;
        m_map.setEdgeId(m_map.phi2(ne), id) ;			// set the edge id of the inserted
untereiner's avatar
untereiner committed
468 469
		//id = m_map.getNewEdgeId() ;
		id = m_map.getQuadRefinementEdgeId(ne2);
470 471 472 473 474 475 476 477 478 479
        m_map.setEdgeId(ne2, id) ;					// edges to the next available ids
        m_map.setEdgeId(m_map.phi2(ne2), id) ;

        dd = m_map.phi1(next) ;
        (*vertexVertexFunctor)(dd) ;
        dd = m_map.phi1(dd) ;
        while(dd != ne)								// turn around the face and insert new edges
        {											// linked to the central vertex
            m_map.splitFace(m_map.phi1(ne), dd) ;
            Dart nne = m_map.alpha1(dd) ;
untereiner's avatar
untereiner committed
480 481
			//id = m_map.getNewEdgeId() ;
			id = m_map.getQuadRefinementEdgeId(m_map.phi2(nne));
482 483 484 485 486 487 488 489 490
            m_map.setEdgeId(nne, id) ;
            m_map.setEdgeId(m_map.phi2(nne), id) ;
            dd = m_map.phi1(dd) ;
            (*vertexVertexFunctor)(dd) ;
            dd = m_map.phi1(dd) ;
        }

        (*faceVertexFunctor)(m_map.phi1(ne)) ;
    }
491 492 493 494 495 496 497 498 499 500

	m_map.setCurrentLevel(cur) ;

	return fLevel ;
}

template <typename PFP>
void IHM2<PFP>::coarsenFace(Dart d)
{
	assert(m_map.getDartLevel(d) <= m_map.getCurrentLevel() || !"coarsenFace : called with a dart inserted after current level") ;
501
    assert(faceIsSubdividedOnce(d) || !"Trying to coarsen a non-subdivided face or a more than once subdivided face") ;
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 536 537

	unsigned int cur = m_map.getCurrentLevel() ;

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

	if(degree == 3)
	{
		fit = d ;
		do
		{
			m_map.setCurrentLevel(cur + 1) ;
			Dart innerEdge = m_map.phi1(fit) ;
			m_map.setCurrentLevel(m_map.getMaxLevel()) ;
			m_map.mergeFaces(innerEdge) ;
			m_map.setCurrentLevel(cur) ;
			fit = m_map.phi1(fit) ;
		} while(fit != d) ;
	}
	else
	{
		m_map.setCurrentLevel(cur + 1) ;
		Dart centralV = m_map.phi1(m_map.phi1(d)) ;
		m_map.setCurrentLevel(m_map.getMaxLevel()) ;
		m_map.deleteVertex(centralV) ;
		m_map.setCurrentLevel(cur) ;
	}

	fit = d ;
	do
	{
538
        if(edgeCanBeCoarsened(fit))
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
			coarsenEdge(fit) ;
		fit = m_map.phi1(fit) ;
	} while(fit != d) ;
}


} // namespace Adaptive

} // namespace Primal

} // namespace MR

} // namespace Algo

} // namespace CGoGN