subdivision.hpp 22.7 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25
#include "Algo/Geometry/basic.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
26 27 28
#include "Algo/Geometry/centroid.h"
#include "Topology/generic/autoAttributeHandler.h"

thery's avatar
thery committed
29 30 31
#define _USE_MATH_DEFINES
#include <math.h>

Pierre Kraemer's avatar
Pierre Kraemer committed
32 33 34 35 36 37
namespace CGoGN
{

namespace Algo
{

38 39 40
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
41 42 43 44 45 46 47 48
namespace Modelisation
{

template <typename PFP>
Dart trianguleFace(typename PFP::MAP& map, Dart d)
{
	Dart d1 = map.phi1(d);
	if (d1 == d)
49
		CGoGNout << "Warning: triangulation of a face with only one edge" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
50
	if (map.phi1(d1) == d)
51
		CGoGNout << "Warning: triangulation of a face with only two edges" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

	map.splitFace(d, d1) ;
	map.cutEdge(map.phi_1(d)) ;
	Dart x = map.phi2(map.phi_1(d)) ;
	Dart dd = map.template phi<111>(x) ;
	while(dd != x)
	{
		Dart next = map.phi1(dd) ;
		map.splitFace(dd, map.phi1(x)) ;
		dd = next ;
	}

	return map.phi2(x);	// Return a dart of the central vertex
}

template <typename PFP, typename EMBV, typename EMB>
68
void trianguleFaces(typename PFP::MAP& map, EMBV& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
69
{
70
	TraversorF<typename PFP::MAP> t(map) ;
71
	for (Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
72
	{
73
		EMB center = Geometry::faceCentroidGen<PFP,EMBV,EMB>(map, d, attributs);	// compute center
74 75 76 77
		Dart cd = trianguleFace<PFP>(map, d);	// triangule the face
		attributs[cd] = center;					// affect the data to the central vertex
		Dart fit = cd ;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
78
		{
79
			t.skip(fit);
80
			fit = map.phi2(map.phi_1(fit));
81
		} while(fit != cd);
Pierre Kraemer's avatar
Pierre Kraemer committed
82 83 84
	}
}

85
template <typename PFP>
86
void trianguleFaces(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
87
{
88
	trianguleFaces<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3>(map, position) ;
89 90
}

Pierre Kraemer's avatar
Pierre Kraemer committed
91 92 93
template <typename PFP>
void trianguleFaces(
		typename PFP::MAP& map,
94
		VertexAttribute<typename PFP::VEC3>& position, const FaceAttribute<typename PFP::VEC3>& positionF)
Pierre Kraemer's avatar
Pierre Kraemer committed
95
{
96
	TraversorF<typename PFP::MAP> t(map) ;
97
	for (Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
98
	{
99 100 101 102
		Dart cd = trianguleFace<PFP>(map, d);	// triangule the face
		position[cd] = positionF[d];			// affect the data to the central vertex
		Dart fit = cd ;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
103
		{
104
			t.skip(fit);
105
			fit = map.phi2_1(fit);
106
		} while(fit != cd);
Pierre Kraemer's avatar
Pierre Kraemer committed
107 108 109
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
template <typename PFP>
Dart quadranguleFace(typename PFP::MAP& map, Dart d)
{
	d = map.phi1(d) ;
	map.splitFace(d, map.template phi<11>(d)) ;
	map.cutEdge(map.phi_1(d)) ;
	Dart x = map.phi2(map.phi_1(d)) ;
	Dart dd = map.template phi<1111>(x) ;
	while(dd != x)
	{
		Dart next = map.template phi<11>(dd) ;
		map.splitFace(dd, map.phi1(x)) ;
		dd = next ;
	}

	return map.phi2(x);	// Return a dart of the central vertex
}

template <typename PFP, typename EMBV, typename EMB>
129
void quadranguleFaces(typename PFP::MAP& map, EMBV& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
130 131 132 133 134 135 136
{
	DartMarker me(map) ;
	DartMarker mf(map) ;

	// first pass: cut the edges
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
137
		if ( !map.isBoundaryMarked2(d) && !me.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139
		{
			Dart f = map.phi1(d);
David Cazier's avatar
David Cazier committed
140
			Dart e = map.cutEdge(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
141 142
//			TODO trouver pourquoi lerp bug avec ECell
//			attributs[m] = AttribOps::lerp<EMB,PFP>(attributs[d],attributs[f], 0.5);
143

Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146 147
			attributs[e] = attributs[d];
			attributs[e] += attributs[f];
			attributs[e] *= 0.5;

Pierre Kraemer's avatar
Pierre Kraemer committed
148 149 150
			me.markOrbit<EDGE>(d);
			me.markOrbit<EDGE>(e);
			mf.markOrbit<VERTEX>(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
151 152 153 154 155 156
		}
	}

	// second pass: quandrangule faces
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
157
		if ( !map.isBoundaryMarked2(d) && !mf.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
158
		{
159
			EMB center = Geometry::faceCentroidGen<PFP,EMBV,EMB>(map, d, attributs);	// compute center
Pierre Kraemer's avatar
Pierre Kraemer committed
160 161 162 163 164
			Dart cf = quadranguleFace<PFP>(map, d);	// quadrangule the face
			attributs[cf] = center;					// affect the data to the central vertex
			Dart e = cf;
			do
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
165
				mf.markOrbit<FACE>(e);
166
				e = map.phi2_1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
167 168 169 170 171
			} while (e != cf);
		}
	}
}

172
template <typename PFP>
173
void quadranguleFaces(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
174
{
175
	quadranguleFaces<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3>(map, position) ;
176 177
}

Pierre Kraemer's avatar
Pierre Kraemer committed
178
template <typename PFP, typename EMBV, typename EMB>
179
void CatmullClarkSubdivision(typename PFP::MAP& map, EMBV& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
180 181 182 183
{
	std::vector<Dart> l_middles;
	std::vector<Dart> l_verts;

Pierre Kraemer's avatar
Pierre Kraemer committed
184
	CellMarkerNoUnmark<VERTEX> m0(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
185 186 187 188 189 190
	DartMarkerNoUnmark mf(map);
	DartMarkerNoUnmark me(map);

	// first pass: cut edges
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
191
		if ( !map.isBoundaryMarked2(d) && !me.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
192 193 194 195 196 197
		{
			if (!m0.isMarked(d))
			{
				m0.mark(d);
				l_verts.push_back(d);
			}
198 199
			Dart d2 = map.phi2(d);
			if (!m0.isMarked(d2))
Pierre Kraemer's avatar
Pierre Kraemer committed
200
			{
201 202
				m0.mark(d2);
				l_verts.push_back(d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
203 204 205
			}

			Dart f = map.phi1(d);
206
			Dart e = map.cutEdge(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
207

208
			attributs[e] = attributs[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
209 210 211
			attributs[e] += attributs[f];
			attributs[e] *= 0.5;

Pierre Kraemer's avatar
Pierre Kraemer committed
212 213
			me.markOrbit<EDGE>(d);
			me.markOrbit<EDGE>(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215 216 217 218 219 220 221 222 223 224

			mf.mark(d) ;
			mf.mark(map.phi2(e)) ;

			l_middles.push_back(e);
		}
	}

	// second pass: quandrangule faces
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
225
		if ( !map.isBoundaryMarked2(d) && mf.isMarked(d)) // for each face not subdivided
Pierre Kraemer's avatar
Pierre Kraemer committed
226 227 228 229
		{
			// compute center skip darts of new vertices non embedded
			EMB center = AttribOps::zero<EMB,PFP>();
			unsigned int count = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
230
			mf.unmarkOrbit<FACE>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
231 232 233 234 235
			Dart it = d;
			do
			{
				center += attributs[it];
				++count ;
Pierre Kraemer's avatar
Pierre Kraemer committed
236
				me.unmarkOrbit<PFP::MAP::EDGE_OF_PARENT>(it);
237

238
				it = map.phi1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
239
				me.unmarkOrbit<PFP::MAP::EDGE_OF_PARENT>(it);
240
				it = map.phi1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243 244 245 246 247 248 249 250 251 252
			} 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
		}
	}

	// Compute edge points
	for(typename std::vector<Dart>::iterator mid = l_middles.begin(); mid != l_middles.end(); ++mid)
	{
		Dart x = *mid;
		// other side of the edge
253
		if (!map.isBoundaryEdge(x))
Pierre Kraemer's avatar
Pierre Kraemer committed
254 255
		{
			Dart f1 = map.phi_1(x);
256
			Dart f2 = map.phi2(map.phi1(map.phi2(x)));
Pierre Kraemer's avatar
Pierre Kraemer committed
257
			EMB temp = AttribOps::zero<EMB,PFP>();
258
			temp = attributs[f1];
Pierre Kraemer's avatar
Pierre Kraemer committed
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
			temp += attributs[f2];			// E' = (V0+V1+F1+F2)/4
			temp *= 0.25;
			attributs[x] *= 0.5;
			attributs[x] += temp;
		}
		// else nothing to do point already in the middle of segment
	}

	// Compute vertex points
	for(typename std::vector<Dart>::iterator vert = l_verts.begin(); vert != l_verts.end(); ++vert)
	{
		m0.unmark(*vert);

		EMB temp = AttribOps::zero<EMB,PFP>();
		EMB temp2 = AttribOps::zero<EMB,PFP>();

		unsigned int n = 0;
		Dart x = *vert;
		do
		{
			Dart m = map.phi1(x);
			Dart f = map.phi2(m);
			Dart v = map.template phi<11>(f);

			temp += attributs[f];
			temp2 += attributs[v];

			++n;
287
			x = map.phi2_1(x);
Pierre Kraemer's avatar
Pierre Kraemer committed
288 289 290 291 292 293 294 295 296 297 298 299
		} while (x != *vert);

		EMB emcp = attributs[*vert];
		emcp *= double((n-2)*n);		// V' = (n-2)/n*V + 1/n2 *(F+E)
		emcp += temp;
		emcp += temp2;
		emcp /= double(n*n);

		attributs[*vert] = emcp ;
	}
}

300
template <typename PFP>
301
void CatmullClarkSubdivision(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
302
{
303
	CatmullClarkSubdivision<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3>(map, position) ;
304 305
}

Pierre Kraemer's avatar
Pierre Kraemer committed
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
inline double betaF(unsigned int n)
{
	switch(n)
	{
		case 1: return 0.234375 ;
		case 2: return 0.609375 ;
		case 3: return 0.5625 ;
		case 4: return 0.484375 ;
		case 5: return 0.420466 ;
		case 6: return 0.375 ;
		case 7: return 0.343174 ;
		case 8: return 0.320542 ;
		case 9: return 0.304065 ;
		case 10: return 0.291778 ;
		case 11: return 0.282408 ;
		case 12: return 0.27512 ;
		case 13: return 0.26935 ;
		case 14: return 0.264709 ;
		default:
			double t = 3.0 + 2.0 * cos((2.0*M_PI)/double(n)) ;
			return 5.0/8.0 - (t * t) / 64.0 ;
	}
}

template <typename PFP, typename EMBV, typename EMB>
331
void LoopSubdivision(typename PFP::MAP& map, EMBV& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
332 333 334 335
{
	std::vector<Dart> l_middles;
	std::vector<Dart> l_verts;

Pierre Kraemer's avatar
Pierre Kraemer committed
336
	CellMarkerNoUnmark<VERTEX> m0(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
337 338 339 340 341 342
	DartMarkerNoUnmark mv(map);
	DartMarkerNoUnmark me(map);

	// first pass cut edges
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
343
		if ( !map.isBoundaryMarked2(d) && !me.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
344 345 346 347 348 349 350 351 352 353 354 355 356
		{
			if (!m0.isMarked(d))
			{
				m0.mark(d);
				l_verts.push_back(d);
			}
			if (!m0.isMarked(map.phi2(d)))
			{
				m0.mark(map.phi2(d));
				l_verts.push_back(map.phi2(d));
			}

			Dart f = map.phi1(d);
357
			Dart e = map.cutEdge(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
358 359 360 361 362

			attributs[e] =  attributs[d];
			attributs[e] += attributs[f];
			attributs[e] *= 0.5;

Pierre Kraemer's avatar
Pierre Kraemer committed
363 364
			me.markOrbit<EDGE>(d);
			me.markOrbit<EDGE>(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
365

Pierre Kraemer's avatar
Pierre Kraemer committed
366
			mv.markOrbit<VERTEX>(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
367 368 369 370 371 372 373 374 375

			l_middles.push_back(e);
		}
	}

	// Compute edge points
	for(typename std::vector<Dart>::iterator mid = l_middles.begin(); mid != l_middles.end(); ++mid)
	{
		Dart d = *mid;
376
		if (!map.isBoundaryEdge(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
377
		{
378
			Dart dd = map.phi2(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
379 380 381 382 383 384
			attributs[d] *= 0.75;
			Dart e1 = map.template phi<111>(d);
			EMB temp = AttribOps::zero<EMB,PFP>();
			temp = attributs[e1];
			e1 = map.phi_1(map.phi_1(dd));
			temp += attributs[e1];
385
			temp *= 1.0 / 8.0;
Pierre Kraemer's avatar
Pierre Kraemer committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
			attributs[d] += temp;
		}
		// else nothing to do point already in the middle of segment
	}

	// Compute vertex points
	for(typename std::vector<Dart>::iterator vert = l_verts.begin(); vert != l_verts.end(); ++vert)
	{
		m0.unmark(*vert);

		EMB temp = AttribOps::zero<EMB,PFP>();
		int n = 0;
		Dart x = *vert;
		do
		{
			Dart y = map.phi1(map.phi1(x));
			temp += attributs[y];
			++n;
404
			x = map.phi2_1(x);
Pierre Kraemer's avatar
Pierre Kraemer committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
		} while ((x != *vert));
		EMB emcp = attributs[*vert];
		if (n == 6)
		{
			temp /= 16.0;
			emcp *= 10.0/16.0;
			emcp += temp;
		}
		else
		{
			double beta = betaF(n) ;
			temp *= (beta / double(n));
			emcp *= (1.0 - beta);
			emcp += temp;
		}
		attributs[*vert] = emcp;
	}

	// insert new edges
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (mv.isMarked(d))
		{
			// unmark the darts of the face
Pierre Kraemer's avatar
Pierre Kraemer committed
429 430
			me.unmarkOrbit<FACE>(d) ;
			mv.unmarkOrbit<FACE>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

			Dart dd = d;
			Dart e = map.template phi<11>(dd) ;
			map.splitFace(dd, e);

			dd = e;
			e = map.template phi<11>(dd) ;
			map.splitFace(dd, e);

			dd = e;
			e = map.template phi<11>(dd) ;
			map.splitFace(dd, e);
		}
	}
}

447
template <typename PFP>
448
void LoopSubdivision(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
449
{
450
	LoopSubdivision<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3>(map, position) ;
451 452
}

453
template <typename PFP, typename EMBV, typename EMB>
454
void TwoNPlusOneSubdivision(typename PFP::MAP& map, EMBV& attributs, float size)
455
{
Pierre Kraemer's avatar
Pierre Kraemer committed
456
	CellMarker<EDGE> m0(map);
457
	CellMarker<FACE> m1(map);
458 459

	std::vector<Dart> dOrig;
460

461 462 463
	//first pass cut edge
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
464
		if(!m0.isMarked(d))
465
		{
466
			if(!m1.isMarked(d))
467
			{
468 469 470
				m1.mark(d);
				dOrig.push_back(d);
			}
471

472 473 474 475 476
//			if(selected(map.phi2(d)) && !m1.isMarked(map.phi2(d)))   TODO CHECK THIS ALGO
			if (!m1.isMarked(map.phi2(d)))
			{
				m1.mark(map.phi2(d));
				dOrig.push_back(map.phi2(d));
477
			}
478 479 480 481

			EMB e1 = attributs[d];
			EMB e2 = attributs[map.phi1(d)];
			map.cutEdge(d);
482
			attributs[map.phi1(d)] = e1*(1.0f-size)+e2*size;
483
			map.cutEdge(map.phi1(d));
484
			attributs[map.phi1(map.phi1(d))] = e2*(1.0f-size)+e1*size;
485 486 487
			m0.mark(d);
			m0.mark(map.phi1(d));
			m0.mark(map.template phi<11>(d));
488 489 490
		}
	}

491
	CGoGNout << "nb orig : " << dOrig.size() << CGoGNendl;
492 493 494 495 496

	DartMarkerNoUnmark mCorner(map);
//	//second pass create corner face
	for (std::vector<Dart>::iterator it = dOrig.begin(); it != dOrig.end(); ++it)
	{
497
//		EMB c = Geometry::faceCentroid<PFP>(map,*it,attributs);
498 499 500 501 502 503
		Dart dd = *it;
		do
		{
			map.splitFace(map.phi1(dd),map.phi_1(dd));
			map.cutEdge(map.phi1(dd));
			mCorner.mark(map.phi2(map.phi1(dd)));
504 505 506
//			attributs[map.template phi<11>(dd)] = c*(1.0-size)+ attributs[dd]*size;
			attributs[map.template phi<11>(dd)] = attributs[dd] 	+ Geometry::vectorOutOfDart<PFP>(map,dd,attributs)
																	- Geometry::vectorOutOfDart<PFP>(map,map.phi_1(dd),attributs);
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
			dd = map.phi1(map.phi1(map.phi1(map.phi2(map.phi1(dd)))));
		} while(!mCorner.isMarked(dd));
	}

	//third pass create center face
	for (std::vector<Dart>::iterator it = dOrig.begin(); it != dOrig.end(); ++it)
	{
		Dart dd = map.phi2(map.phi1(*it));
		do {
			mCorner.unmark(dd);
			Dart dNext = map.phi1(map.phi1(map.phi1(dd)));
			map.splitFace(dd,dNext);
			dd = dNext;
		} while(mCorner.isMarked(dd));
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
template <typename PFP>
void DooSabin(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
{
	DartMarker dm(map);
	// storage of boundary of hole (missing vertex faces)
	std::vector<Dart> fp;
	fp.reserve(16384);

	// storage of initial faces for position updating
	std::vector<Dart> faces;
	faces.reserve(16384);


	// create the edge faces
	for(Dart d=map.begin(); d != map.end(); map.next(d))
	{
		if (!dm.isMarked(d))
		{
			faces.push_back(d);
			Dart e = d;
			do
			{
				Dart e2 = map.phi2(e);
				if (!dm.isMarked(e2))
				{
					map.unsewFaces(e,false);
					Dart nf = map.newFace(4,false);
					map.sewFaces(e,nf,false);
					map.sewFaces(e2,map.template phi<11>(nf),false);
					// take care of edge embedding
					if(map.template isOrbitEmbedded<EDGE>())
					{
						map.template setOrbitEmbedding<EDGE>(nf, map.template getEmbedding<EDGE>(e));
						map.template setOrbitEmbedding<EDGE>(map.template phi<11>(nf), map.template getEmbedding<EDGE>(e2));
					}

					dm.markOrbit<FACE>(nf);
					fp.push_back(map.phi1(nf));
					fp.push_back(map.phi_1(nf));
				}
				dm.markOrbit<EDGE1>(e);
				e = map.phi1(e);
			}while (e!=d);
		}
	}
	// fill (create) the new  vertex faces
	for (std::vector<Dart>::iterator di=fp.begin(); di != fp.end(); ++di)
	{
		if (map.phi2(*di) == *di)
		{
			map.PFP::MAP::TOPO_MAP::closeHole(*di,false);

			if(map.template isOrbitEmbedded<EDGE>())
			{
				Dart df = map.phi2(*di);
				Dart d = df;
				do
				{
					map.template setOrbitEmbedding<EDGE>(d,map.template getEmbedding<EDGE>(map.phi2(d)));
					d = map.phi1(d);
				} while (d != df);
			}
		}
	}

	std::vector<typename PFP::VEC3> buffer;
	buffer.reserve(8);
	for (std::vector<Dart>::iterator di=faces.begin(); di != faces.end(); ++di)
	{
		Dart e = *di;
594
		typename PFP::VEC3 center = Geometry::faceCentroid<PFP>(map,e,position);
Sylvain Thery's avatar
Sylvain Thery committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

		do
		{
			// compute DoSabin
			buffer.push_back(position[e]);
			e = map.phi1(e);
		}while (e != * di);

		int N = buffer.size();
		for (int i=0; i<N; ++i)
		{
			typename PFP::VEC3 P(0,0,0);
			for (int j=0; j<N; ++j)
			{
				if (j==i)
				{
					float c1 = float(N+5)/float(4*N);
					P += buffer[j]*c1;
				}
				else
				{
					float c2 = (3.0+2.0*cos(2.0*M_PI*(double(i-j))/double(N))) /(4.0*N);
					P+= c2*buffer[j];
				}
			}
			map.template setOrbitEmbeddingOnNewCell<VERTEX>(e);
			position[e] = P;
			e = map.phi1(e);
		}
		buffer.clear();
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
inline double sqrt3_K(unsigned int n)
{
	switch(n)
	{
		case 1: return 0.333333 ;
		case 2: return 0.555556 ;
		case 3: return 0.5 ;
		case 4: return 0.444444 ;
		case 5: return 0.410109 ;
		case 6: return 0.388889 ;
		case 7: return 0.375168 ;
		case 8: return 0.365877 ;
		case 9: return 0.359328 ;
		case 10: return 0.354554 ;
		case 11: return 0.350972 ;
		case 12: return 0.348219 ;
		default:
			double t = cos((2.0*M_PI)/double(n)) ;
			return (4.0 - t) / 9.0 ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
648 649
}

Pierre Kraemer's avatar
Pierre Kraemer committed
650
//template <typename PFP>
651
//void Sqrt3Subdivision(typename PFP::MAP& map, VertexAttribute<VEC3>& position)
Pierre Kraemer's avatar
Pierre Kraemer committed
652 653 654 655
//{
//	typedef typename PFP::VEC3 VEC3 ;
//	typedef typename PFP::REAL REAL ;
//
656
//	FaceAttribute<VEC3> positionF = map.template getAttribute<VEC3, FACE>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
657
//	if(!positionF.isValid())
658
//		positionF = map.template addAttribute<VEC3, FACE>("position") ;
659
//	Geometry::computeCentroidFaces<PFP>(map, position, positionF) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
660
//
661
//	computeDual<PFP>(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
662
//
663
//	VertexAttribute<VEC3> tmp = position ;
Pierre Kraemer's avatar
Pierre Kraemer committed
664 665 666 667 668 669
//	position = positionF ;
//	positionF = tmp ;
//
//	CellMarker m(map, VERTEX) ;
//	m.markAll() ;
//
670
//	trianguleFaces<PFP>(map, position, positionF);
Pierre Kraemer's avatar
Pierre Kraemer committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
//
//	for(Dart d = map.begin(); d != map.end(); map.next(d))
//	{
//		if(!m.isMarked(d))
//		{
//			m.mark(d) ;
//			VEC3 P = position[d] ;
//			VEC3 newP(0) ;
//			unsigned int val = 0 ;
//			Dart vit = d ;
//			do
//			{
//				newP += position[map.phi2(vit)] ;
//				++val ;
//				vit = map.phi2_1(vit) ;
//			} while(vit != d) ;
//			REAL K = sqrt3_K(val) ;
//			newP *= REAL(3) ;
//			newP -= REAL(val) * P ;
//			newP *= K / REAL(2 * val) ;
//			newP += (REAL(1) - K) * P ;
//			position[d] = newP ;
//		}
//	}
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
696

697 698 699 700 701 702 703
template <typename PFP>
void computeDual(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
{
	// Face Attribute -> after dual new Vertex Attribute
	FaceAttribute<typename PFP::VEC3> positionF  = map.template getAttribute<typename PFP::VEC3, FACE>("position") ;
	if(!positionF.isValid())
		positionF = map.template addAttribute<typename PFP::VEC3, FACE>("position") ;
704

705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	// Compute Centroid for the faces
	Algo::Surface::Geometry::computeCentroidFaces<PFP>(map, position, positionF) ;

	// Compute the Dual mesh
	map.computeDual();
	position = positionF ;
}


template <typename PFP>
void computeBoundaryConstraintDual(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
{
	// Face Attribute -> after dual new Vertex Attribute
	FaceAttribute<typename PFP::VEC3> positionF  = map.template getAttribute<typename PFP::VEC3, FACE>("position") ;
	if(!positionF.isValid())
		positionF = map.template addAttribute<typename PFP::VEC3, FACE>("position") ;

	//Triangule boundary faces & compute for each new face the centroid
	std::vector<Dart> boundsDart;
	DartMarkerStore mf(map);
	for(Dart dit = map.begin() ; dit != map.end() ; map.next(dit))
	{
		if(!mf.isMarked(dit) && map.isBoundaryMarked2(dit))
		{
			boundsDart.push_back(dit);
			Dart db = dit;
			Dart d1 = map.phi1(db);
			Dart dprev = map.phi_1(db);
			map.splitFace(db, d1) ;
			map.cutEdge(map.phi_1(db)) ;

			positionF[dit] = (position[dit] + position[map.phi2(dit)]) * typename PFP::REAL(0.5);
			mf.markOrbit<FACE>(dit);

			Dart x = map.phi2(map.phi_1(db)) ;
			Dart dd = map.phi1(map.phi1(map.phi1(x)));
			while(dd != x)
			{
				Dart next = map.phi1(dd) ;
				Dart prev = map.phi_1(dd);
				map.splitFace(dd, map.phi1(x)) ;
				positionF[prev] = (position[prev] + position[map.phi1(prev)]) * typename PFP::REAL(0.5);
				mf.markOrbit<FACE>(prev);
				dd = next ;
			}

			positionF[dprev] = (position[dprev] + position[map.phi1(dprev)]) * typename PFP::REAL(0.5);
			mf.markOrbit<FACE>(dprev);
		}
	}

	// Compute Centroid for the other faces
	Algo::Surface::Geometry::computeCentroidFaces<PFP>(map, position, positionF) ;

	// Fill the holes
	for(Dart dit = map.begin() ; dit != map.end() ; map.next(dit))
	{
		if(mf.isMarked(dit) && map.isBoundaryMarked2(dit))
		{
			map.fillHole(dit);
			mf.unmarkOrbit<FACE>(dit);
		}
	}

	// Compute the Dual mesh
	map.computeDual();
	position = positionF ;

	// Create the new border with the old boundary edges
	for(std::vector<Dart>::iterator it = boundsDart.begin() ; it != boundsDart.end() ; ++it)
	{
		map.createHole(map.phi2(map.phi1(*it)));
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
779

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
template <typename PFP>
void computeBoundaryConstraintKeepingOldVerticesDual(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
{
	// Face Attribute -> after dual new Vertex Attribute
	FaceAttribute<typename PFP::VEC3> positionF  = map.template getAttribute<typename PFP::VEC3, FACE>("position") ;
	if(!positionF.isValid())
		positionF = map.template 	addAttribute<typename PFP::VEC3, FACE>("position") ;

	//Triangule boundary faces & compute for each new face the centroid
	std::vector<Dart> boundsDart;
	DartMarkerStore mf(map);
	for(Dart dit = map.begin() ; dit != map.end() ; map.next(dit))
	{
		if(!mf.isMarked(dit) && map.isBoundaryMarked2(dit))
		{
			boundsDart.push_back(dit);
			Dart db = dit;
			Dart d1 = map.phi1(db);
			Dart dprev = map.phi_1(db);
			map.splitFace(db, d1) ;
			map.cutEdge(map.phi_1(db)) ;

			positionF[dit] = (position[dit] + position[map.phi2(dit)]) * typename PFP::REAL(0.5);
			mf.markOrbit<FACE>(dit);

			Dart x = map.phi2(map.phi_1(db)) ;
			Dart dd = map.phi1(map.phi1(map.phi1(x)));
			while(dd != x)
			{
				Dart next = map.phi1(dd) ;
				Dart prev = map.phi_1(dd);
				map.splitFace(dd, map.phi1(x)) ;
				positionF[prev] = (position[prev] + position[map.phi1(prev)]) * typename PFP::REAL(0.5);
				mf.markOrbit<FACE>(prev);
				dd = next ;
			}

			positionF[dprev] = (position[dprev] + position[map.phi1(dprev)]) * typename PFP::REAL(0.5);
			mf.markOrbit<FACE>(dprev);
		}
	}

	// Compute Centroid for the other faces
	Algo::Surface::Geometry::computeCentroidFaces<PFP>(map, position, positionF) ;

	// Fill the holes
	for(Dart dit = map.begin() ; dit != map.end() ; map.next(dit))
	{
		if(mf.isMarked(dit) && map.isBoundaryMarked2(dit))
		{
			map.fillHole(dit);
			mf.unmarkOrbit<FACE>(dit);
		}
	}

	// Compute the Dual mesh
	map.computeDual();

	//Saving old position VertexAttribute to a FaceAttribute
	FaceAttribute<typename PFP::VEC3> temp;
	temp = position;
	position = positionF ;
	positionF = temp;

	// Create the new border with the old boundary edges
	for(std::vector<Dart>::iterator it = boundsDart.begin() ; it != boundsDart.end() ; ++it)
	{
		map.createHole(map.phi2(map.phi1(*it)));
	}

	// Manage old vertices with new FaceAttribute
	for(Dart dit = map.begin() ; dit != map.end() ; map.next(dit))
	{
		if(!mf.isMarked(dit) && map.isBoundaryMarked2(dit))
		{
			Dart nd = map.cutEdge(dit);
			position[nd] = positionF[map.phi2(dit)];
			mf.markOrbit<EDGE>(dit);
			mf.markOrbit<EDGE>(nd);
		}
	}
861 862
}

863 864 865 866
} // namespace Modelisation

} // namespace Surface

Pierre Kraemer's avatar
Pierre Kraemer committed
867 868 869
} // namespace Algo

} // namespace CGoGN