gmap3.cpp 10 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Topology/gmap/gmap3.h"
#include "Topology/generic/dartmarker.h"

namespace CGoGN
{

void GMap3::deleteOrientedVolume(Dart d)
{
	DartMarkerStore mark(*this);				// Lock a marker
	bool found = false;					// Last functor return value

	std::vector<Dart> visitedFaces;		// Faces that are traversed
37
	visitedFaces.reserve(512);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	visitedFaces.push_back(d);			// Start with the face of d
	std::vector<Dart>::iterator face;

	// For every face added to the list
	for (face = visitedFaces.begin(); !found && face != visitedFaces.end(); ++face)
	{
		if (!mark.isMarked(*face))		// Face has not been visited yet
		{
			unsewVolumes(*face);

			Dart dNext = *face ;

			do
			{
				mark.mark(dNext);					// Mark
				Dart adj = beta2(dNext);				// Get adjacent face
				if (adj != dNext && !mark.isMarked(adj))
					visitedFaces.push_back(adj);	// Add it

				dNext = beta0(dNext);				// same with beta0, in most case nothing happend
				mark.mark(dNext);					// except marking the dart
				adj = beta2(dNext);
				if (adj != dNext && !mark.isMarked(adj))
					visitedFaces.push_back(adj);

				dNext = beta1(dNext);
			} while(dNext != *face);		// assume here that faces are closed

		}
	}
68

Pierre Kraemer's avatar
Pierre Kraemer committed
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
	// delete every visited face
	for (face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
		GMap1::deleteFace(*face);
}





void GMap3::sewVolumes(Dart d, Dart e)
{
	assert(faceDegree(d) == faceDegree(e));

	Dart fitD = d ;
	Dart fitE = e ;
	do
	{
		phi3sew(fitD,fitE);
		fitD = phi1(fitD) ;
		fitE = phi_1(fitE) ;
	} while(fitD != d) ;
}

void GMap3::unsewVolumes(Dart d)
{
	Dart fitD = d;
	do
	{
		phi3unsew(fitD);
		fitD = phi1(fitD);
	} while(fitD != d);
}

bool GMap3::mergeVolumes(Dart d)
{
	Dart e = phi3(d) ;
	if(e != d)
	{
		unsewVolumes(d);
		GMap2::mergeVolumes(d, e); // merge the two volumes along common face
		return true ;
	}
	return false ;
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
void GMap3::splitFace(Dart d, Dart e)
{
	GMap2::splitFace(d,e);

	if (phi3(d) != d)
	{
		Dart dd = phi1(phi3(d));
		Dart ee = phi1(phi3(e));

		GMap2::splitFace(dd,ee);

		phi3sew(phi_1(d), phi_1(ee));
		phi3sew(phi_1(e), phi_1(dd));
	}
}

Thomas's avatar
Thomas committed
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
int GMap3::collapseEdge(Dart d, bool delDegenerateFaces,
		bool delDegenerateVolumes)
{
	Dart e = d;
	int i = 0;

	//stocke un brin par volume autour de l'arete
	std::list<Dart> tmp;
	do
	{
		tmp.push_back(e);
		e = alpha2(e);
		i++;
	} while (e != d);

	for (std::list<Dart>::iterator it = tmp.begin(); it != tmp.end(); ++it)
	{
		Dart e = phi2(*it);

		cutSpike(e);

		Dart t1=e,t2=e;
		//si les faces opposées ont un tetraedre cousu
		if(phi3(phi2(phi1(e))) != phi2(phi1(e))) {
			t1 = phi3(phi2(phi1(e)));
			unsewVolumes(t1);
		}

		if(phi3(phi2(phi_1(e))) != phi2(phi_1(e))) {
			t2 = phi3(phi2(phi_1(e)));
			unsewVolumes(t2);
		}

		if(t1 != e && t2 != e) {
			sewVolumes(t1,t2);
		}

		//unsewVolumes(e);
		//unsewVolumes(*it);

		deleteOrientedVolume(*it);
	}

	return i;
}

//TODO
void GMap3::collapseFace(Dart d, bool delDegenerateFaces,
		bool delDegenerateVolumes)
{
	Dart e = d;
	std::list<Dart> tmp;

	//save a dart from the edge for all neighbors
	do
	{
		//if(phi3(phi2(e)) != phi2(e))
		//		tmp.push_back(phi3(phi2(e)));
		tmp.push_back(phi3(phi2(e)));
		e = phi1(e);
	}while(e != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
191

Thomas's avatar
Thomas committed
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
	//del the last one (n-1 edge collapse)
	tmp.pop_back();

	//CGoGNout << "#voisin=" << tmp.size() << CGoGNendl;

	//collapse all the edges in the list
	for(std::list<Dart>::iterator it = tmp.begin() ; it != tmp.end() ; ++it)
	{
		Dart d = *it;
		//CGoGNout << "collapseEdge" << CGoGNendl;
		//collapseEdge(*it, delDegenerateFaces, delDegenerateVolumes);
		//stocke un brin par volume autour de l'arete

		Dart e = d;

		//stocke un brin par volume autour de l'arete
		std::list<Dart> tmpedge;
		do
		{
			tmpedge.push_back(e);
			e = alpha2(e);
		} while (e != d);

		for (std::list<Dart>::iterator it = tmpedge.begin(); it != tmpedge.end(); ++it)
		{
			Dart e = phi2(*it);

			cutSpike(e);

			Dart t1=e,t2=e;
			//si les faces opposées ont un tetraedre cousu
			if(phi3(phi2(phi1(e))) != phi2(phi1(e))) {
				t1 = phi3(phi2(phi1(e)));
				unsewVolumes(t1);
			}

			if(phi3(phi2(phi_1(e))) != phi2(phi_1(e))) {
				t2 = phi3(phi2(phi_1(e)));
				unsewVolumes(t2);
			}

			if(t1 != e && t2 != e) {
				sewVolumes(t1,t2);
			}

			//deleteOrientedVolume(*it);
		}
	}
//	for(std::list<Dart>::iterator it = tmp.begin() ; it != tmp.end() ; ++it)
//	{
//		deleteOrientedVolume(*it);
//	}
}

Dart GMap3::cutSpike(Dart d)
{
  Dart e=d;
  int nb=0;
  Dart dNew;
  int tet=0;

  //CGoGNout << "cut" << CGoGNendl;

  //count the valence of the vertex
  do {
    nb++;
    e=phi1(phi2(e));
  } while (e!=d);

  if(nb<3)
  {
	CGoGNout << "Warning : cannot cut 2 volumes without creating a degenerated face " << CGoGNendl;
	return d;
  }
  else
  {
	 //triangulate around the vertex
	do {
		if(phi1(phi1(phi1(e)))!=e)
		{
			splitFace(phi_1(e),phi1(e));
			//CGoGNout << "split" << CGoGNendl;
		}
		else
			tet++;

		e=phi1(phi2(e));
	} while (e!=d);

//	CGoGNout << "#tet= " << tet << CGoGNendl;
//	CGoGNout << "#nb= " << nb << CGoGNendl;

	//si toute ces faces ne sont pas triangulaires (on insere une face)
	if(tet != nb) {
		//CGoGNout << "new face" << CGoGNendl;
		dNew=newFace(nb);
		Dart d3 = newFace(nb);
		sewVolumes(dNew,d3);

		//sew a face following the triangles
		Dart dTurn=dNew;
		do {
			Dart d1 = phi1(e);
			Dart dSym = phi2(d1);
			phi2unsew(d1);
			phi2sew(dTurn,d1);
			phi2sew(phi3(dTurn),dSym);
			dTurn = phi1(dTurn);
			e=phi1(phi2(e));
		}while(e!=d);
	}
	else
		dNew = d;
  }

  return dNew;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
309 310


Sylvain Thery's avatar
Sylvain Thery committed
311
bool GMap3::foreach_dart_of_vertex(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
312
{
Sylvain Thery's avatar
Sylvain Thery committed
313
	DartMarkerStore mv(*this,thread);	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	bool found = false;					// Last functor return value

	std::list<Dart> darts_list;			//Darts that are traversed
	darts_list.push_back(d);			//Start with the dart d
	std::list<Dart>::iterator darts;

	mv.mark(d);

	for(darts = darts_list.begin(); !found && darts != darts_list.end() ; ++darts)
	{
		Dart dc = *darts;

		Dart dx = beta1(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		dx = beta2(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		dx = beta3(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		found = f(dc);
	}

	return found;
}


// TODO:optimize traversal of edges ?
Sylvain Thery's avatar
Sylvain Thery committed
355
bool GMap3::foreach_dart_of_edge(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
356
{
Sylvain Thery's avatar
Sylvain Thery committed
357
	DartMarkerStore mv(*this,thread);			// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	bool found = false;					// Last functor return value
	std::list<Dart> darts_list;		//Darts that are traversed
	darts_list.push_back(d);			//Start with the dart d
	std::list<Dart>::iterator darts;

	mv.mark(d);

	for(darts = darts_list.begin(); !found && darts != darts_list.end() ; ++darts)
	{
		Dart dc = *darts;

		Dart dx = beta0(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		dx = beta2(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		dx = beta3(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		found = f(dc);
	}

	return found;

}

Sylvain Thery's avatar
Sylvain Thery committed
397
bool GMap3::foreach_dart_of_face(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
398
{
399
	return GMap1::foreach_dart_of_face(d, f, thread) || (beta3(d)==d) || GMap1::foreach_dart_of_face(beta3(d), f, thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
400 401
}

Sylvain Thery's avatar
Sylvain Thery committed
402
bool GMap3::foreach_dart_of_volume(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
403
{
Sylvain Thery's avatar
Sylvain Thery committed
404
	return GMap2::foreach_dart_of_cc(d, f, thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
405 406 407
}


Sylvain Thery's avatar
Sylvain Thery committed
408
bool GMap3::foreach_dart_of_cc(Dart d, FunctorType& f, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
409
{
Sylvain Thery's avatar
Sylvain Thery committed
410
	DartMarkerStore mv(*this,thread);	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	bool found = false;					// Last functor return value

	std::list<Dart> darts_list;			//Darts that are traversed
	darts_list.push_back(d);			//Start with the dart d
	std::list<Dart>::iterator darts;

	mv.mark(d);

	for(darts = darts_list.begin(); !found && darts != darts_list.end() ; ++darts)
	{
		Dart dc = *darts;

		Dart dx = beta0(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		dx = beta1(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		dx = beta2(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		dx = beta3(dc);
		if (!mv.isMarked(dx))
		{
			mv.mark(dx);
			darts_list.push_back(dx);
		}

		found =  f(dc);
	}

	return found;
}









} // end namespace