map2MR_Primal.cpp 11.4 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Topology/map/map2MR/map2MR_Primal.h"

namespace CGoGN
{

30
Map2MR_Primal::Map2MR_Primal() : shareVertexEmbeddings(true)
Pierre Kraemer's avatar
Pierre Kraemer committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
	initMR() ;
}

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

unsigned int Map2MR_Primal::vertexInsertionLevel(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"vertexInsertionLevel : called with a dart introduced after current level") ;
	return getDartLevel(d) ;
}

unsigned int Map2MR_Primal::edgeLevel(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"edgeLevel : called with a dart introduced after current level") ;

	unsigned int ld = getDartLevel(d) ;
	unsigned int ldd = getDartLevel(phi2(d)) ;	// the level of an edge is the maximum of the
	return ld > ldd ? ld : ldd ;				// insertion levels of its two darts
}

unsigned int Map2MR_Primal::faceLevel(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"faceLevel : called with a dart introduced after current level") ;

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

	Dart it = d ;
	unsigned int min1 = getDartLevel(it) ;		// the level of a face is the second minimum of the
	it = phi1(it) ;
	unsigned int min2 = getDartLevel(it) ;		// insertion levels of its darts

	if(min2 < min1)
	{
		unsigned int tmp = min1 ;
		min1 = min2 ;
		min2 = tmp ;
	}

	it = phi1(it) ;
	while(it != d)
	{
		unsigned int dl = getDartLevel(it) ;
		if(dl < min2)
		{
			if(dl < min1)
			{
				min2 = min1 ;
				min1 = dl ;
			}
			else
				min2 = dl ;
		}
		it = phi1(it) ;
	}

	return min2 ;
}

Dart Map2MR_Primal::faceOrigin(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"faceOrigin : called with a dart introduced after current level") ;

	pushLevel() ;
	Dart p = d ;
	unsigned int pLevel = getDartLevel(p) ;
	while(pLevel > 0)
	{
		p = faceOldestDart(p) ;
		pLevel = getDartLevel(p) ;
		setCurrentLevel(pLevel) ;
	}
	popLevel() ;
	return p ;
}

Dart Map2MR_Primal::faceOldestDart(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"faceOldestDart : called with a dart introduced after current level") ;

	Dart it = d ;
	Dart oldest = it ;
	unsigned int l_old = getDartLevel(oldest) ;
	do
	{
		unsigned int l = getDartLevel(it) ;
		if(l == 0)
			return it ;
		if(l < l_old)
		{
			oldest = it ;
			l_old = l ;
		}
		it = phi1(it) ;
	} while(it != d) ;
	return oldest ;
}

bool Map2MR_Primal::edgeIsSubdivided(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"edgeIsSubdivided : called with a dart introduced after current level") ;

	if(getCurrentLevel() == getMaxLevel())
		return false ;

	Dart d2 = phi2(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
140
	incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
141
	Dart d2_l = phi2(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
142
	decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	if(d2 != d2_l)
		return true ;
	else
		return false ;
}

bool Map2MR_Primal::edgeCanBeCoarsened(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"edgeCanBeCoarsened : called with a dart introduced after current level") ;

	if(edgeIsSubdivided(d))
	{
		bool subdOnce = true ;
		bool degree2 = false ;

		Dart d2 = phi2(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
159
		incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
160 161 162 163 164 165
		if(vertexDegree(phi1(d)) == 2)
		{
			degree2 = true ;
			if(edgeIsSubdivided(d) || edgeIsSubdivided(d2))
				subdOnce = false ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
166
		decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

		return degree2 && subdOnce ;
	}

	return false ;
}

bool Map2MR_Primal::faceIsSubdivided(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"faceIsSubdivided : called with a dart introduced after current level") ;

	if(getCurrentLevel() == getMaxLevel())
		return false ;

	unsigned int fLevel = faceLevel(d) ;
	if(fLevel < 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

	bool subd = false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
186
	incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
187 188
	if(getDartLevel(phi1(phi1(d))) == getCurrentLevel())
		subd = true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
189
	decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	return subd ;
}

bool Map2MR_Primal::faceIsSubdividedOnce(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"faceIsSubdividedOnce : called with a dart introduced after current level") ;

	if(getCurrentLevel() == getMaxLevel())
		return false ;

	unsigned int fLevel = faceLevel(d) ;
	if(fLevel < 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 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
208
	incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
209 210
	if(getDartLevel(phi1(phi1(d))) == getCurrentLevel())
		subd = true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
211
	decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
212 213 214

	if(subd)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
215
		incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
216 217 218

		if(getCurrentLevel() == getMaxLevel())
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
219
			decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
220 221 222 223 224 225
			return true ;
		}

		Dart fit = d ;
		do
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
226
			incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
227 228
			if(getDartLevel(phi1(phi1(fit))) == getCurrentLevel())
				subdOnce = false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
229
			decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231 232 233 234 235 236
			++degree ;
			fit = phi1(fit) ;
		} while(subdOnce && fit != d) ;

		if(degree == 3 && subdOnce)
		{
			Dart cf = phi2(phi1(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
237
			incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
238 239
			if(getDartLevel(phi1(phi1(cf))) == getCurrentLevel())
				subdOnce = false ;
Pierre Kraemer's avatar
Pierre Kraemer committed
240
			decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
243
		decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
244 245 246 247 248 249 250

		return subdOnce ;
	}

	return false ;
}

251
/***************************************************
252
 *               SUBDIVISION                       *
253 254
 ***************************************************/

255
void Map2MR_Primal::duplicateDart(Dart d)
256
{
257
	if(getDartLevel(d) < getCurrentLevel())
258
	{
259 260 261 262 263 264 265 266 267 268 269 270 271
		unsigned int oldindex = dartIndex(d) ;
		unsigned int newindex = newCopyOfDartLine(oldindex) ;
		for(unsigned int i = getCurrentLevel(); i <= getMaxLevel(); ++i)
		{
			(*m_mrDarts[i])[d.index] = newindex ;
			if(shareVertexEmbeddings)
			{
				unsigned int emb = (*m_embeddings[VERTEX])[oldindex] ;
				(*m_embeddings[VERTEX])[newindex] = emb ;
				if(emb != EMBNULL)
					m_attribs[VERTEX].refLine(emb);
			}
		}
272 273 274 275 276 277 278
	}
}

void Map2MR_Primal::addNewLevel()
{
	addLevel() ;
	if(shareVertexEmbeddings)
279 280 281 282 283 284 285 286 287 288 289 290
	{
		unsigned int maxL = getMaxLevel() ;
		for(unsigned int i = m_mrattribs.begin(); i != m_mrattribs.end(); m_mrattribs.next(i))
		{
			unsigned int previdx = (*m_mrDarts[maxL - 1])[i] ;
			unsigned int newidx = (*m_mrDarts[maxL])[i] ;
			unsigned int emb = (*m_embeddings[VERTEX])[previdx] ;
			(*m_embeddings[VERTEX])[newidx] = emb ;
			if(emb != EMBNULL)
				m_attribs[VERTEX].refLine(emb);
		}
	}
291 292
}

293
unsigned int Map2MR_Primal::subdivideEdge(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
294 295 296 297 298 299 300 301 302
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"subdivideEdge : called with a dart introduced after current level") ;
	assert(!edgeIsSubdivided(d) || !"Trying to subdivide an already subdivided edge") ;

	unsigned int eLevel = edgeLevel(d) ;

	pushLevel() ;

	if(eLevel == getMaxLevel())
303
		addNewLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
304 305

	setCurrentLevel(eLevel + 1) ;
306 307 308 309 310
	Dart dd = phi2(d) ;
	duplicateDart(d) ;
	duplicateDart(phi1(d)) ;
	duplicateDart(dd) ;
	duplicateDart(phi1(dd)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
311 312 313
	cutEdge(d) ;

	popLevel() ;
314 315

	return eLevel ;
Pierre Kraemer's avatar
Pierre Kraemer committed
316 317
}

318
unsigned int Map2MR_Primal::subdivideFace(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
319 320 321 322 323 324 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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"subdivideFace : called with a dart introduced after current level") ;
	assert(!faceIsSubdivided(d) || !"Trying to subdivide an already subdivided face") ;

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

	pushLevel() ;
	setCurrentLevel(fLevel) ;		// go to the level of the face to subdivide its edges

	unsigned int degree = 0 ;
	Dart it = old ;
	do
	{
		++degree ;
		if(!edgeIsSubdivided(it))	// first cut the edges (if they are not already)
			subdivideEdge(it) ;		// and compute the degree of the face
		it = phi1(it) ;
	} while(it != old) ;

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

	if(degree == 3)					// if subdividing a triangle
	{
		Dart dd = phi1(old) ;
		Dart e = phi1(phi1(dd)) ;
		splitFace(dd, e) ;

		dd = e ;
		e = phi1(phi1(dd)) ;
		splitFace(dd, e) ;

		dd = e ;
		e = phi1(phi1(dd)) ;
		splitFace(dd, e) ;
	}
	else							// if subdividing a polygonal face
	{
		Dart dd = phi1(old) ;
		splitFace(dd, phi1(phi1(dd))) ;	// insert a first edge
		Dart ne = alpha1(dd) ;

		cutEdge(ne) ;					// cut the new edge to insert the central vertex

		dd = phi1(phi1(phi1(phi1(ne)))) ;
		while(dd != ne)								// turn around the face and insert new edges
		{											// linked to the central vertex
			Dart next = phi1(phi1(dd)) ;
			splitFace(phi1(ne), dd) ;
			dd = next ;
		}
	}

	popLevel() ;
373 374

	return fLevel ;
Pierre Kraemer's avatar
Pierre Kraemer committed
375 376 377 378 379 380 381
}

void Map2MR_Primal::coarsenEdge(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"coarsenEdge : called with a dart introduced after current level") ;
	assert(edgeCanBeCoarsened(d) || !"Trying to coarsen an edge that can not be coarsened") ;

Pierre Kraemer's avatar
Pierre Kraemer committed
382
	incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
383
	uncutEdge(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
384 385 386 387 388
	decCurrentLevel() ;

	unsigned int maxL = getMaxLevel() ;
	if(getCurrentLevel() == maxL - 1 && getNbInsertedDarts(maxL) == 0)
		removeLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
}

void Map2MR_Primal::coarsenFace(Dart d)
{
	assert(getDartLevel(d) <= getCurrentLevel() || !"coarsenFace : called with a dart introduced after current level") ;
	assert(faceIsSubdividedOnce(d) || !"Trying to coarsen a non-subdivided face or a more than once subdivided face") ;

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

	if(degree == 3)
	{
		fit = d ;
		do
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
409
			incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
410 411 412
			Dart innerEdge = phi1(fit) ;
			setCurrentLevel(getMaxLevel()) ;
			mergeFaces(innerEdge) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
413
			decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
414 415 416 417 418
			fit = phi1(fit) ;
		} while(fit != d) ;
	}
	else
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
419
		incCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
420 421 422
		Dart centralV = phi1(phi1(d)) ;
		setCurrentLevel(getMaxLevel()) ;
		deleteVertex(centralV) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
423
		decCurrentLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
424 425 426 427 428 429 430 431 432
	}

	fit = d ;
	do
	{
		if(edgeCanBeCoarsened(fit))
			coarsenEdge(fit) ;
		fit = phi1(fit) ;
	} while(fit != d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
433 434 435 436

	unsigned int maxL = getMaxLevel() ;
	if(getCurrentLevel() == maxL - 1 && getNbInsertedDarts(maxL) == 0)
		removeLevel() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
437 438 439
}

} // namespace CGoGN