ihm2_PrimalAdapt.hpp 15.2 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 63 64 65 66 67 68 69 70 71 72 73 74
/***************************************************
 *               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
    unsigned int ldd = m_map.getDartLevel(m_map.phi1(d));
    return ld < ldd ? ldd : ld ;
}

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    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) ;

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

    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 ;
        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>
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) ;
    if(fLevel <= m_map.getCurrentLevel())
        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                       *
 ***************************************************/
304 305 306 307 308

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") ;
309
    assert(!edgeIsSubdivided(d) || !"Trying to subdivide an already subdivided edge") ;
310

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

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

316 317
    Dart dd = m_map.phi2(d) ;

318 319
	m_map.setCurrentLevel(eLevel + 1) ;

320
    m_map.cutEdge(d) ;
321
	unsigned int eId = m_map.getEdgeId(d) ;
322 323 324
    m_map.setEdgeId(m_map.phi1(d), eId) ;
    m_map.setEdgeId(m_map.phi1(dd), eId) ;
    (*edgeVertexFunctor)(m_map.phi1(d)) ;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

	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") ;
	assert(m_map.edgeCanBeCoarsened(d) || !"Trying to coarsen an edge that can not be coarsened") ;


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

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") ;
350
    assert(!faceIsSubdivided(d) || !"Trying to subdivide an already subdivided face") ;
351

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

untereiner's avatar
untereiner committed
355 356
	std::cout << "faceLevel = " << fLevel << std::endl;

357 358 359 360 361 362 363 364 365
	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

366 367 368 369
        if(OneLevelDifference)
        {
            Dart nf = m_map.phi2(it) ;
            if(faceLevel(nf) == fLevel - 1)	// check if neighboring faces have to be subdivided first
untereiner's avatar
untereiner committed
370
				subdivideFace(nf,triQuad) ;
371
        }
372

373
        if(!edgeIsSubdivided(it))
374 375 376 377 378 379 380
			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


untereiner's avatar
untereiner committed
381
	if(triQuad && (degree == 3))					// if subdividing a triangle
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 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 434 435 436 437 438 439 440 441 442 443 444 445
    {
        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
        unsigned int id = m_map.getNewEdgeId() ;
        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

        dd = e ;
        e = m_map.phi1(dd) ;
        (*vertexVertexFunctor)(e) ;
        e = m_map.phi1(e) ;
        m_map.splitFace(dd, e) ;
        id = m_map.getNewEdgeId() ;
        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) ;
        id = m_map.getNewEdgeId() ;
        m_map.setEdgeId(m_map.phi_1(dd), id) ;
        m_map.setEdgeId(m_map.phi_1(e), id) ;
    }
    else											// if subdividing a polygonal face
    {
        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
        unsigned int id = m_map.getNewEdgeId() ;
        m_map.setEdgeId(ne, id) ;
        m_map.setEdgeId(m_map.phi2(ne), id) ;			// set the edge id of the inserted
        id = m_map.getNewEdgeId() ;
        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) ;
            id = m_map.getNewEdgeId() ;
            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)) ;
    }
446 447 448 449 450 451 452 453 454 455

	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") ;
456
    assert(faceIsSubdividedOnce(d) || !"Trying to coarsen a non-subdivided face or a more than once subdivided face") ;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

	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
	{
		if(m_map.edgeCanBeCoarsened(fit))
			coarsenEdge(fit) ;
		fit = m_map.phi1(fit) ;
	} while(fit != d) ;
}


} // namespace Adaptive

} // namespace Primal

} // namespace MR

} // namespace Algo

} // namespace CGoGN