subdivision3.hpp 12.7 KB
Newer Older
untereiner's avatar
untereiner 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           *
untereiner's avatar
untereiner 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/                                           *
untereiner's avatar
untereiner committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Geometry/centroid.h"
#include "Algo/Modelisation/subdivision.h"
#include "Algo/Modelisation/extrusion.h"

namespace CGoGN
{

namespace Algo
{

namespace Modelisation
{

template <typename PFP>
Dart cut3Ear(typename PFP::MAP& map, Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
41 42 43 44 45 46 47 48
	Dart e = d;
	int nb = 0;
	Dart dNew;

	Dart dRing;
	Dart dRing2;

	//count the valence of the vertex
untereiner's avatar
untereiner committed
49 50
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
51 52 53
		nb++;
		e = map.phi1(map.phi2(e));
	} while (e != d);
untereiner's avatar
untereiner committed
54

Pierre Kraemer's avatar
Pierre Kraemer committed
55 56 57 58 59 60 61 62 63 64 65 66 67
	if(nb < 3)
	{
		CGoGNout << "Warning : cannot cut 2 volumes without creating a degenerated face " << CGoGNendl;
		return d;
	}
	else
	{
		//triangulate around the vertex
		do
		{
			Dart dN = map.phi1(map.phi2(e));
			if(map.template phi<111>(e) != e)
				map.splitFace(map.phi_1(e), map.phi1(e));
untereiner's avatar
untereiner committed
68

Pierre Kraemer's avatar
Pierre Kraemer committed
69 70
			dRing = map.phi1(e);
			dRing2 = map.phi2(dRing);
untereiner's avatar
untereiner committed
71

Pierre Kraemer's avatar
Pierre Kraemer committed
72
			map.unsewFaces(dRing);
untereiner's avatar
untereiner committed
73

Pierre Kraemer's avatar
Pierre Kraemer committed
74 75
			e = dN;
		} while (e != d);
untereiner's avatar
untereiner committed
76

Pierre Kraemer's avatar
Pierre Kraemer committed
77 78 79 80
		map.closeHole(dRing);
		map.closeHole(dRing2);
		map.sewVolumes(map.phi2(dRing), map.phi2(dRing2));
	}
untereiner's avatar
untereiner committed
81

Pierre Kraemer's avatar
Pierre Kraemer committed
82 83
	return map.phi2(dRing);
}
untereiner's avatar
untereiner committed
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 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
//template <typename PFP, typename EMBV, typename EMB>
//void catmullClarkVol(typename PFP::MAP& map, EMBV& attributs, const FunctorSelect& selected)
//{
//	std::vector<Dart> l_centers;
//	std::vector<Dart> l_vertices;
//
//	DartMarkerNoUnmark mv(map);
//	CellMarkerNoUnmark me(map, EDGE);
//	CellMarker mf(map, FACE);
//
//	AutoAttributeHandler< EMB > attBary(map, VOLUME);
//	CellMarker vol(map, VOLUME);
//
//	//pre-computation : compute the centroid of all volume
//	for (Dart d = map.begin(); d != map.end(); map.next(d))
//	{
//		if(selected(d) && !vol.isMarked(d))
//		{
//			vol.mark(d);
//			attBary[d] = Algo::Geometry::volumeCentroidGen<PFP,EMBV,EMB>(map, d, attributs);
//		}
//	}
//
//	// first pass: cut edges
//	for (Dart d = map.begin(); d != map.end(); map.next(d))
//	{
//		//memorize each vertices per volumes
//		if(selected(d) && !mv.isMarked(d))
//		{
//			l_vertices.push_back(d);
////			mv.markOrbitInParent<typename PFP::MAP>(VERTEX,d);
//			mv.markOrbit(PFP::MAP::ORBIT_IN_PARENT(VERTEX),d);
//		}
//
//		//cut edges
//		if (selected(d) && !me.isMarked(d))
//		{
//			Dart f = map.phi1(d);
//			map.cutEdge(d);
//			Dart e = map.phi1(d) ;
//
//			attributs[e] =  attributs[d];
//			attributs[e] += attributs[f];
//			attributs[e] *= 0.5;
//
//			me.mark(d);
//			me.mark(e);
//
//			//mark new vertices
//			mv.markOrbit(VERTEX, e);
//
//			Dart dd = d;
//			do
//			{
//				mf.mark(dd) ;
//				mf.mark(map.phi2(dd));
//				dd = map.alpha2(dd);
//			} while(dd != d);
//		}
//	}
//
//	// second pass: quandrangule faces
//	std::map<Dart,Dart> toSew;
//	for (Dart d = map.begin(); d != map.end(); map.next(d))
//	{
//		mv.unmark(d);
//
//		if (selected(d) && mf.isMarked(d)) // for each face not subdivided
//		{
//			mf.unmark(d);
//			// compute center skip darts of new vertices non embedded
//			EMB center = AttribOps::zero<EMB,PFP>();
//			unsigned int count = 0 ;
//			Dart it = d;
//
//			do
//			{
//				me.unmark(it);
//				me.unmark(map.phi1(it));
//
//				center += attributs[it];
//				++count ;
//
//				it = map.template phi<11>(it) ;
//			} while(it != d) ;
//
//			center /= double(count);
//
//			Dart cf = quadranguleFace<PFP>(map, d);	// quadrangule the face
//			attributs[cf] = center;					// affect the data to the central vertex
//		}
//	}
//
//	//third pass : create the inner faces
//	for (std::vector<Dart>::iterator it = l_vertices.begin(); it != l_vertices.end(); ++it)
//	{
//		Dart d = *it;
//		//unsew all around the vertex
//		//there are 2 links to unsew for each face around (-> quadrangulation)
//		std::vector<Dart> v;
//		do
//		{
//			v.push_back(map.phi1(map.phi1(d)));
//			v.push_back(map.phi1(d));
//
//			d = map.phi2(map.phi_1(d));
//		}
//		while(d != *it);
//
////		do
////		{
////			Dart dN = map.phi1(map.phi2(d));
////
////	 		Dart dRing = map.phi1(d);
////
////	 		if(map.phi2(dRing)!=dRing)
////	 		{
////	 			toSew.insert(std::pair<Dart,Dart>(dRing,map.phi2(dRing)));
////	 			v.push_back(dRing);
////	 		}
////
////	 		dRing = map.phi1(dRing);
////
////	 		if(map.phi2(dRing)!=dRing)
////	 		{
////	 			toSew.insert(std::pair<Dart,Dart>(dRing,map.phi2(dRing)));
////	 			v.push_back(dRing);
////	 		}
////
////			d = dN;
////		} while (d != *it);
//
////		//close the generated hole and create the central vertex
////		//unsigned int degree = map.closeHole(map.phi1(d));
//
//		//TODO : pb de face en trop avec splitVolume
//		//map.splitVolume(v);
//
//		//
//
////		Dart e = v.front();
////		for(std::vector<Dart>::iterator it = v.begin() ; it != v.end() ; ++it)
////			if(map.Map2::isBoundaryEdge(*it))
////				map.unsewFaces(*it);
//
////		Dart dd = map.phi1(map.phi2(map.phi1(d)));
////		map.splitFace(map.phi_1(dd),map.phi1(dd));
////		Dart dS = map.phi1(dd);
////		map.cutEdge(dS);
//
////		attributs[map.phi1(dS)] = attBary[d];
//
//
//		//TODO : test with vertices with degree higher than 3
////		for(unsigned int i=0; i < (degree/2)-2; ++i)
////		{
////			map.splitFace(map.phi2(dS),map.template phi<111>(map.phi2(dS)));
////			dS = map.template phi<111>(map.phi2(dS));
////		}
//	}
//
////	map.deleteVolume(map.phi3(map.phi2(map.phi1(l_vertices.front()))));
//
//	map.check();
//
//	//sew all faces leading to the central vertex
//	for (std::map<Dart,Dart>::iterator it = toSew.begin(); it != toSew.end(); ++it)
//	{
//
////		Dart f1 = map.phi2((*it).first);
////		Dart f2 = map.phi2((*it).second);
////		if(map.isBoundaryFace(f1) && map.isBoundaryFace(f2))
////		{
////			map.sewVolumes(f1, f2);
////		}
//
//		//Dart dT = map.phi2(it->first);
////		if(dT==map.phi3(dT))
////		{
////			map.sewVolumes(dT,map.phi2(it->second));
////		}
//	}
//}


untereiner's avatar
untereiner committed
270 271 272
template <typename PFP, typename EMBV, typename EMB>
void catmullClarkVol(typename PFP::MAP& map, EMBV& attributs, const FunctorSelect& selected)
{
273
	//std::vector<Dart> l_centers;
untereiner's avatar
untereiner committed
274 275 276
	std::vector<Dart> l_vertices;

	//pre-computation : compute the centroid of all volume
277 278
	AutoAttributeHandler< EMB > attBary(map, VOLUME);
	TraversorW<typename PFP::MAP> traW(map, selected);
untereiner's avatar
untereiner committed
279 280
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
281
		attBary[d] = Algo::Geometry::volumeCentroidGen<PFP,EMBV,EMB>(map, d, attributs);
untereiner's avatar
untereiner committed
282 283
	}

284 285 286 287 288
	//subdivision
	//1. cut edges
	DartMarkerNoUnmark mv(map);
	TraversorE<typename PFP::MAP> travE(map);
	for (Dart d = travE.begin(); d != travE.end(); d = travE.next())
untereiner's avatar
untereiner committed
289 290 291 292 293
	{
		//memorize each vertices per volumes
		if(selected(d) && !mv.isMarked(d))
		{
			l_vertices.push_back(d);
294
			mv.markOrbit(PFP::MAP::ORBIT_IN_PARENT(VERTEX),d);
untereiner's avatar
untereiner committed
295 296
		}

297 298 299
		Dart f = map.phi1(d);
		map.cutEdge(d) ;
		Dart e = map.phi1(d) ;
untereiner's avatar
untereiner committed
300

301 302 303
		attributs[e] =  attributs[d];
		attributs[e] += attributs[f];
		attributs[e] *= 0.5;
untereiner's avatar
untereiner committed
304

untereiner's avatar
untereiner committed
305 306
		travE.skip(d) ;
		travE.skip(e) ;
untereiner's avatar
untereiner committed
307 308
	}

309 310 311
	//2. split faces - quadrangule faces
	TraversorF<typename PFP::MAP> travF(map) ;
	for (Dart d = travF.begin(); d != travF.end(); d = travF.next())
untereiner's avatar
untereiner committed
312
	{
313
		EMB center = Algo::Geometry::faceCentroidGen<PFP,EMBV,EMB>(map,d,attributs);
untereiner's avatar
untereiner committed
314

315 316 317
		Dart dd = map.phi1(d) ;
		Dart next = map.phi1(map.phi1(dd)) ;
		map.splitFace(dd, next) ;
untereiner's avatar
untereiner committed
318

319 320
		Dart ne = map.phi2(map.phi_1(dd)) ;
		map.cutEdge(ne) ;
untereiner's avatar
untereiner committed
321
		travF.skip(dd) ;
untereiner's avatar
untereiner committed
322

323
		attributs[map.phi1(ne)] = center;
untereiner's avatar
untereiner committed
324

325 326 327 328 329
		dd = map.phi1(map.phi1(next)) ;
		while(dd != ne)
		{
			Dart tmp = map.phi1(ne) ;
			map.splitFace(tmp, dd) ;
untereiner's avatar
untereiner committed
330
			travF.skip(tmp) ;
331
			dd = map.phi1(map.phi1(dd)) ;
untereiner's avatar
untereiner committed
332
		}
333

untereiner's avatar
untereiner committed
334
		travF.skip(ne) ;
untereiner's avatar
untereiner committed
335 336
	}

337 338 339 340
	//3. create inside volumes

	std::vector<std::pair<Dart, Dart> > subdividedFaces;
	subdividedFaces.reserve(2048);
untereiner's avatar
untereiner committed
341 342
	for (std::vector<Dart>::iterator it = l_vertices.begin(); it != l_vertices.end(); ++it)
	{
343 344 345
		Dart e = *it;
		std::vector<Dart> v ;

untereiner's avatar
untereiner committed
346 347
		do
		{
348 349 350 351 352
			v.push_back(map.phi1(e));
			v.push_back(map.phi1(map.phi1(e)));

			if(!map.PFP::MAP::ParentMap::isBoundaryEdge(map.phi1(e)))
				subdividedFaces.push_back(std::pair<Dart,Dart>(map.phi1(e),map.phi2(map.phi1(e))));
untereiner's avatar
untereiner committed
353

354 355 356 357
			if(!map.PFP::MAP::ParentMap::isBoundaryEdge(map.phi1(map.phi1(e))))
				subdividedFaces.push_back(std::pair<Dart,Dart>(map.phi1(map.phi1(e)),map.phi2(map.phi1(map.phi1(e)))));

			e = map.phi2(map.phi_1(e));
untereiner's avatar
untereiner committed
358
		}
359
		while(e != *it);
untereiner's avatar
untereiner committed
360

361 362 363
		//
		// SplitSurfaceInVolume
		//
untereiner's avatar
untereiner committed
364

365 366
		std::vector<Dart> vd2 ;
		vd2.reserve(v.size());
untereiner's avatar
untereiner committed
367

368 369 370 371 372
		// save the edge neighbors darts
		for(std::vector<Dart>::iterator it2 = v.begin() ; it2 != v.end() ; ++it2)
		{
			vd2.push_back(map.phi2(*it2));
		}
untereiner's avatar
untereiner committed
373

374
		assert(vd2.size() == v.size());
untereiner's avatar
untereiner committed
375

376
		map.PFP::MAP::ParentMap::splitSurface(v, true, false);
untereiner's avatar
untereiner committed
377

378 379 380 381 382
		// follow the edge path a second time to embed the vertex, edge and volume orbits
		for(unsigned int i = 0; i < v.size(); ++i)
		{
			Dart dit = v[i];
			Dart dit2 = vd2[i];
untereiner's avatar
untereiner committed
383

384 385 386 387 388 389
			// embed the vertex embedded from the origin volume to the new darts
			if(map.isOrbitEmbedded(VERTEX))
			{
				map.copyDartEmbedding(VERTEX, map.phi2(dit), map.phi1(dit));
				map.copyDartEmbedding(VERTEX, map.phi2(dit2), map.phi1(dit2));
			}
untereiner's avatar
untereiner committed
390

391 392 393 394 395 396 397
			// embed the edge embedded from the origin volume to the new darts
			if(map.isOrbitEmbedded(EDGE))
			{
				unsigned int eEmb = map.getEmbedding(EDGE, dit) ;
				map.setDartEmbedding(EDGE, map.phi2(dit), eEmb);
				map.setDartEmbedding(EDGE, map.phi2(dit2), eEmb);
			}
untereiner's avatar
untereiner committed
398

399 400 401 402 403 404 405
			// embed the volume embedded from the origin volume to the new darts
			if(map.isOrbitEmbedded(VOLUME))
			{
				map.copyDartEmbedding(VOLUME, map.phi2(dit), dit);
				map.copyDartEmbedding(VOLUME, map.phi2(dit2), dit2);
			}
		}
untereiner's avatar
untereiner committed
406

407 408 409
		//
		//
		//
untereiner's avatar
untereiner committed
410

411 412 413
		Dart dd = map.phi2(map.phi1(*it));
		Dart next = map.phi1(map.phi1(dd)) ;
		map.PFP::MAP::ParentMap::splitFace(dd, next);
untereiner's avatar
untereiner committed
414

415 416 417 418 419 420 421 422
		if (map.isOrbitEmbedded(VERTEX))
		{
			map.copyDartEmbedding(VERTEX, map.phi_1(next), dd) ;
			map.copyDartEmbedding(VERTEX, map.phi_1(dd), next) ;
		}

		Dart ne = map.phi2(map.phi_1(dd));
		map.PFP::MAP::ParentMap::cutEdge(ne);
untereiner's avatar
untereiner committed
423

424 425 426

//		dd = map.phi1(map.phi1(next)) ;
//		while(dd != ne)
untereiner's avatar
untereiner committed
427
//		{
428 429 430 431 432 433 434 435 436 437
//			Dart tmp = map.phi1(ne) ;
//			map.PFP::MAP::ParentMap::splitFace(tmp, dd);
//
//			if (map.isOrbitEmbedded(VERTEX))
//			{
//				map.copyDartEmbedding(VERTEX, map.phi_1(dd), tmp) ;
//				map.copyDartEmbedding(VERTEX, map.phi_1(tmp), dd) ;
//			}
//
//			dd = map.phi1(map.phi1(dd)) ;
untereiner's avatar
untereiner committed
438
//		}
439 440
//
	}
untereiner's avatar
untereiner committed
441

442 443 444
//		setCurrentLevel(getMaxLevel()) ;
//		//4 couture des relations precedemment sauvegarde
//		for (std::vector<std::pair<Dart,Dart> >::iterator it = subdividedFaces.begin(); it != subdividedFaces.end(); ++it)
untereiner's avatar
untereiner committed
445
//		{
446 447 448 449 450 451
//			Dart f1 = phi2((*it).first);
//			Dart f2 = phi2((*it).second);
//
//			//if(isBoundaryFace(f1) && isBoundaryFace(f2))
//			if(phi3(f1) == f1 && phi3(f2) == f2)
//				sewVolumes(f1, f2, false);
untereiner's avatar
untereiner committed
452
//		}
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
//		embedOrbit(VERTEX, centralDart, getEmbedding(VERTEX, centralDart));
		//attributs[map.phi1(ne)] = attBary[*it];
//
//		setCurrentLevel(getMaxLevel() - 1) ;
//	}
//
//	//A optimiser
//
//	TraversorE<typename PFP::MAP> travE2(map);
//	for (Dart d = travE2.begin(); d != travE2.end(); d = travE2.next())
//	{
//		map.embedOrbit(VERTEX, map.phi1(d), map.getEmbedding(VERTEX, map.phi1(d)));
//	}
//
//	TraversorF<typename PFP::MAP> travF2(map) ;
//	for (Dart d = travF2.begin(); d != travF2.end(); d = travF2.next())
//	{
//		map.embedOrbit(VERTEX, map.phi2(map.phi1(d)), map.getEmbedding(VERTEX, map.phi2(map.phi1(d))));
//	}


untereiner's avatar
untereiner committed
474 475 476 477 478 479 480 481
}

} //namespace Modelisation

} //namespace Algo

} //namespace CGoGN