embeddedMap2.cpp 9.83 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 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2011, 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.u-strasbg.fr/                                         *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <vector>
#include <algorithm>

#include "Topology/map/embeddedMap2.h"

namespace CGoGN
{

void EmbeddedMap2::splitVertex(Dart d, Dart e)
{
	Dart dd = phi2(d) ;
	Dart ee = phi2(e) ;

	Map2::splitVertex(d, e) ;

	if (isOrbitEmbedded(VERTEX))
	{
		copyDartEmbedding(VERTEX, phi1(dd), d) ;
		copyDartEmbedding(VERTEX, phi1(ee), e) ;
		embedNewCell(VERTEX, e) ;
		copyCell(VERTEX, e, d) ;
	}

	if(isOrbitEmbedded(FACE))
	{
		copyDartEmbedding(FACE, phi1(dd), dd) ;
		copyDartEmbedding(FACE, phi1(ee), ee) ;
	}
}

bool EmbeddedMap2::deleteVertex(Dart d)
{
	Dart f = phi1(d) ;
	if(Map2::deleteVertex(d))
	{
		if (isOrbitEmbedded(FACE))
		{
			embedOrbit(FACE, f, getEmbedding(FACE, f)) ;
		}
		return true ;
	}
	return false ;
}

void EmbeddedMap2::linkVertices(Dart d, Dart e)
{
	Dart dNext = phi1(d) ;

	Map2::linkVertices(d,e);

	if (isOrbitEmbedded(VERTEX))
	{
		copyDartEmbedding(VERTEX, phi_1(e), d) ;
		copyDartEmbedding(VERTEX, phi_1(d), e) ;
	}

	if (isOrbitEmbedded(FACE))
	{
		embedOrbit(FACE, dNext, getEmbedding(FACE, dNext)) ;
	}
}

void EmbeddedMap2::cutEdge(Dart d)
{
	Map2::cutEdge(d) ;

	Dart nd = phi1(d) ;

	if (isOrbitEmbedded(EDGE))
	{
		embedNewCell(EDGE, nd) ;
		copyDartEmbedding(EDGE, phi2(d), d) ;
		copyCell(EDGE, nd, d) ;
	}
	if(isOrbitEmbedded(FACE))
	{
		copyDartEmbedding(FACE, phi1(d), d) ;
		Dart e = phi2(nd) ;
		if(e != nd)
			copyDartEmbedding(FACE, phi1(e), e) ;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
108
bool EmbeddedMap2::uncutEdge(Dart d)
109
{
Pierre Kraemer's avatar
Pierre Kraemer committed
110
	if(Map2::uncutEdge(d))
111 112 113
	{
		if(isOrbitEmbedded(EDGE))
			copyDartEmbedding(EDGE, phi2(d), d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
114
		return true ;
115
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
116
	return false ;
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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 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 373 374 375 376 377 378 379 380 381 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 446 447 448 449 450 451 452 453 454 455 456 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
}

bool EmbeddedMap2::edgeCanCollapse(Dart d)
{
	if(phi2(d) == d)
		return false ;

	if(isBoundaryVertex(d) || isBoundaryVertex(phi1(d)))
		return false ;

	unsigned int val_v1 = vertexDegree(d) ;
	unsigned int val_v2 = vertexDegree(phi1(d)) ;

	if(val_v1 + val_v2 < 8 || val_v1 + val_v2 > 14)
		return false ;

	if(isFaceTriangle(d))
	{
		if(vertexDegree(phi_1(d)) < 4)
			return false ;
	}

	Dart dd = phi2(d) ;
	if(isFaceTriangle(dd))
	{
		if(vertexDegree(phi_1(dd)) < 4)
			return false ;
	}

	// Check vertex sharing condition
	std::vector<unsigned int> vu1 ;
	vu1.reserve(32) ;
	Dart vit1 = alpha1(alpha1(d)) ;
	Dart end = phi1(dd) ;
	do
	{
		unsigned int ve = getEmbedding(VERTEX, phi2(vit1)) ;
		vu1.push_back(ve) ;
		vit1 = alpha1(vit1) ;
	} while(vit1 != end) ;
	end = phi1(d) ;
	Dart vit2 = alpha1(alpha1(dd)) ;
	do
	{
		unsigned int ve = getEmbedding(VERTEX, phi2(vit2)) ;
		std::vector<unsigned int>::iterator it = std::find(vu1.begin(), vu1.end(), ve) ;
		if(it != vu1.end())
			return false ;
		vit2 = alpha1(vit2) ;
	} while(vit2 != end) ;

	return true ;
}

Dart EmbeddedMap2::collapseEdge(Dart d, bool delDegenerateFaces)
{
	unsigned int vEmb = EMBNULL ;
	if (isOrbitEmbedded(VERTEX))
	{
		vEmb = getEmbedding(VERTEX, d) ;
	}

	Dart dV = Map2::collapseEdge(d, delDegenerateFaces);

	if (isOrbitEmbedded(VERTEX))
	{
		embedOrbit(VERTEX, dV, vEmb) ;
	}

	return dV ;
}

bool EmbeddedMap2::flipEdge(Dart d)
{
	if(Map2::flipEdge(d))
	{
		Dart e = phi2(d) ;

		if (isOrbitEmbedded(VERTEX))
		{
			copyDartEmbedding(VERTEX, d, phi1(e)) ;
			copyDartEmbedding(VERTEX, e, phi1(d)) ;
		}
		if (isOrbitEmbedded(FACE))
		{
			copyDartEmbedding(FACE, phi_1(d), d) ;
			copyDartEmbedding(FACE, phi_1(e), e) ;
		}
		return true ;
	}
	return false ;
}

bool EmbeddedMap2::flipBackEdge(Dart d)
{
	if(Map2::flipBackEdge(d))
	{
		Dart e = phi2(d) ;

		if (isOrbitEmbedded(VERTEX))
		{
			copyDartEmbedding(VERTEX, d, phi1(e)) ;
			copyDartEmbedding(VERTEX, e, phi1(d)) ;
		}
		if (isOrbitEmbedded(FACE))
		{
			copyDartEmbedding(FACE, phi1(d), d) ;
			copyDartEmbedding(FACE, phi1(e), e) ;
		}
		return true ;
	}
	return false ;
}

void EmbeddedMap2::insertEdgeInVertex(Dart d, Dart e)
{
	Map2::insertEdgeInVertex(d, e);

	if (isOrbitEmbedded(VERTEX))
	{
		copyDartEmbedding(VERTEX, e, d) ;
	}

	if (isOrbitEmbedded(FACE))
	{
		if(!sameFace(d,e))
		{
			embedNewCell(FACE, e);
			copyCell(FACE, e, d) ;
		}
		else
		{
			embedOrbit(FACE, d, getEmbedding(FACE, d)) ;
		}
	}
}

void EmbeddedMap2::removeEdgeFromVertex(Dart d)
{
	Dart dPrev = alpha_1(d);

	Map2::removeEdgeFromVertex(d);

	if (isOrbitEmbedded(VERTEX))
	{
		embedNewCell(VERTEX, d);
		copyCell(VERTEX, d, dPrev);
	}

	if (isOrbitEmbedded(FACE))
	{
		if(!sameFace(d, dPrev))
		{
			embedNewCell(FACE, d);
			copyCell(FACE, d, dPrev) ;
		}
		else
		{
			embedOrbit(FACE, d, getEmbedding(FACE, d)) ;
		}
	}
}

void EmbeddedMap2::sewFaces(Dart d, Dart e)
{
	unsigned int vEmb1 = EMBNULL ;
	unsigned int vEmb2 = EMBNULL ;
	if (isOrbitEmbedded(VERTEX))
	{
		vEmb1 = getEmbedding(VERTEX, d) ;
		vEmb2 = getEmbedding(VERTEX, phi1(d)) ;
	}

	Map2::sewFaces(d, e) ;

	if (isOrbitEmbedded(VERTEX))
	{
		embedOrbit(VERTEX, d, vEmb1) ;
		embedOrbit(VERTEX, e, vEmb2) ;
	}

	if (isOrbitEmbedded(EDGE))
	{
		copyDartEmbedding(EDGE, e, d) ;
	}
}

void EmbeddedMap2::unsewFaces(Dart d)
{
	bool boundaryD = false ;
	bool boundaryE = false ;
	if (isOrbitEmbedded(VERTEX))
	{
		if(isBoundaryVertex(d))
			boundaryD = true ;
		if(isBoundaryVertex(phi1(d)))
			boundaryE = true ;
	}

	Dart e = phi2(d) ;
	Map2::unsewFaces(d) ;

	if (isOrbitEmbedded(VERTEX))
	{
		if(boundaryD)
		{
			if(e != d)
			{
				Dart ee = phi1(e) ;
				embedNewCell(VERTEX, ee) ;
				copyCell(VERTEX, ee, d) ;
			}
		}

		if(boundaryE)
		{
			if(e != d)
			{
				embedNewCell(VERTEX, e) ;
				copyCell(VERTEX, e, phi1(d)) ;
			}
		}
	}
	if (isOrbitEmbedded(EDGE))
	{
		embedNewCell(EDGE, e) ;
		copyCell(EDGE, e, d) ;
	}
}

bool EmbeddedMap2::collapseDegeneratedFace(Dart d)
{
	Dart e = phi2(d) ;

	if(Map2::collapseDegeneratedFace(d))
	{
		if (isOrbitEmbedded(EDGE))
		{
			copyDartEmbedding(EDGE, phi2(e), e) ;
		}
		return true ;
	}
	return false ;
}

void EmbeddedMap2::splitFace(Dart d, Dart e)
{
	Map2::splitFace(d, e) ;

	if (isOrbitEmbedded(VERTEX))
	{
		copyDartEmbedding(VERTEX, phi_1(e), d) ;
		copyDartEmbedding(VERTEX, phi_1(d), e) ;
	}
	if (isOrbitEmbedded(FACE))
	{
		embedNewCell(FACE, e) ;
		copyCell(FACE, e, d) ;
	}
}

bool EmbeddedMap2::mergeFaces(Dart d)
{
	Dart dNext = phi1(d) ;

	if(Map2::mergeFaces(d))
	{
		if (isOrbitEmbedded(FACE))
		{
			embedOrbit(FACE, dNext, getEmbedding(FACE, dNext)) ;
		}
		return true ;
	}
	return false ;
}

bool EmbeddedMap2::mergeVolumes(Dart d, Dart e)
{
	std::vector<Dart> darts ;
	std::vector<unsigned int> vEmb ;
	std::vector<unsigned int> eEmb ;
	Dart fit = d ;
	do
	{
		darts.push_back(phi2(fit)) ;

		if (isOrbitEmbedded(VERTEX))
		{
			vEmb.push_back(getEmbedding(VERTEX, phi2(fit))) ;
		}

		if (isOrbitEmbedded(EDGE))
		{
			eEmb.push_back(getEmbedding(EDGE, fit)) ;
		}

		fit = phi1(fit) ;
	} while (fit != d) ;

	if(Map2::mergeVolumes(d, e))
	{
		for(unsigned int i = 0; i < darts.size(); ++i)
		{
			if (isOrbitEmbedded(VERTEX))
			{
				embedOrbit(VERTEX, darts[i], vEmb[i]) ;
			}

			if (isOrbitEmbedded(EDGE))
			{
				embedOrbit(EDGE, darts[i], eEmb[i]) ;
			}
		}
		return true ;
	}
	return false ;
}

unsigned int EmbeddedMap2::closeHole(Dart d)
{
	unsigned int nbE = Map2::closeHole(d) ;
	Dart dd = phi2(d) ;
	Dart f = dd ;
	do
	{
		if (isOrbitEmbedded(VERTEX))
		{
			copyDartEmbedding(VERTEX, f, alpha1(f)) ;
		}
		if (isOrbitEmbedded(EDGE))
		{
			copyDartEmbedding(EDGE, f, phi2(f)) ;
		}
		f = phi1(f) ;
	} while(dd != f) ;
	return nbE ;
}

bool EmbeddedMap2::check()
{
	bool topo = Map2::check() ;
	if (!topo)
		return false ;

	CGoGNout << "Check: embedding begin" << CGoGNendl ;
	for(Dart d = begin(); d != end(); next(d))
	{
		if (isOrbitEmbedded(VERTEX))
		{
			if (getEmbedding(VERTEX, d) != getEmbedding(VERTEX, alpha1(d)))
			{
				CGoGNout << "Check: different embeddings on vertex" << CGoGNendl ;
				return false ;
			}
		}

		if (isOrbitEmbedded(EDGE))
		{
			if (getEmbedding(EDGE, d) != getEmbedding(EDGE, phi2(d)))
			{
				CGoGNout << "Check: different embeddings on edge" << CGoGNendl ;
				return false ;
			}
		}

		if (isOrbitEmbedded(FACE))
		{
			if (getEmbedding(FACE, d) != getEmbedding(FACE, phi1(d)))
		{
				CGoGNout << "Check: different embeddings on face" << CGoGNendl ;
				return false ;
			}
		}
	}
	CGoGNout << "Check: embedding ok" << CGoGNendl ;
	return true ;
}

} // namespace CGoGN