subdivision.hpp 30.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

Pierre Kraemer's avatar
Pierre Kraemer committed
30 31 32 33 34 35
namespace CGoGN
{

namespace Algo
{

36 37 38
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
39 40 41 42 43 44 45 46
namespace Modelisation
{

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

	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
}

65
template <typename PFP, typename EMBV>
66
void trianguleFaces(typename PFP::MAP& map, EMBV& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
67
{
68 69
	typedef typename EMBV::DATA_TYPE EMB;

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::faceCentroid<PFP, EMBV>(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 86 87 88 89
//template <typename PFP>
//void trianguleFaces(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
//{
//	trianguleFaces<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3>(map, position) ;
//}
90

Pierre Kraemer's avatar
Pierre Kraemer committed
91 92
template <typename PFP>
void trianguleFaces(
93
	typename PFP::MAP& map,
94 95
	VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position,
	const FaceAttribute<typename PFP::VEC3, typename PFP::MAP>& positionF
96
)
Pierre Kraemer's avatar
Pierre Kraemer committed
97
{
98
	TraversorF<typename PFP::MAP> t(map) ;
99
	for (Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
100
	{
101 102 103 104
		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
105
		{
106
			t.skip(fit);
107
			fit = map.phi2_1(fit);
108
		} while(fit != cd);
Pierre Kraemer's avatar
Pierre Kraemer committed
109 110 111
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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
}

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

136 137
	DartMarker<MAP> me(map) ;
	DartMarker<MAP> mf(map) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140 141

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

Pierre Kraemer's avatar
Pierre Kraemer committed
149 150 151 152
			attributs[e] = attributs[d];
			attributs[e] += attributs[f];
			attributs[e] *= 0.5;

Pierre Kraemer's avatar
Pierre Kraemer committed
153 154 155
			me.template markOrbit<EDGE>(d);
			me.template markOrbit<EDGE>(e);
			mf.template markOrbit<VERTEX>(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157 158 159 160 161
		}
	}

	// second pass: quandrangule faces
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
162
		if ( !map.template isBoundaryMarked<2>(d) && !mf.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
163
		{
164
			EMB center = Geometry::faceCentroid<PFP, EMBV>(map, d, attributs);	// compute center
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166 167 168 169
			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
170
				mf.template markOrbit<FACE>(e);
171
				e = map.phi2_1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
172 173 174 175 176
			} while (e != cf);
		}
	}
}

177 178 179 180 181
//template <typename PFP>
//void quadranguleFaces(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
//{
//	quadranguleFaces<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3>(map, position) ;
//}
182

Sylvain Thery's avatar
Sylvain Thery committed
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

// ORIGINALE
//template <typename PFP, typename EMBV>
//void CatmullClarkSubdivision(typename PFP::MAP& map, EMBV& attributs)
//{
//	typedef typename PFP::MAP MAP;
//	typedef typename EMBV::DATA_TYPE EMB;
//
//	std::vector<Dart> l_middles;
//	std::vector<Dart> l_verts;
//
//	CellMarkerNoUnmark<MAP, VERTEX> m0(map);
//	DartMarkerNoUnmark<MAP> mf(map);
//	DartMarkerNoUnmark<MAP> me(map);
//
//	// first pass: cut edges
//	for (Dart d = map.begin(); d != map.end(); map.next(d))
//	{
//		if ( !map.template isBoundaryMarked<2>(d) && !me.isMarked(d))
//		{
//			if (!m0.isMarked(d))
//			{
//				m0.mark(d);
//				l_verts.push_back(d);
//			}
//			Dart d2 = map.phi2(d);
//			if (!m0.isMarked(d2))
//			{
//				m0.mark(d2);
//				l_verts.push_back(d2);
//			}
//
//			Dart f = map.phi1(d);
//			Dart e = map.cutEdge(d);
//
//			attributs[e] = attributs[d];
//			attributs[e] += attributs[f];
//			attributs[e] *= 0.5;
//
//			me.template markOrbit<EDGE>(d);
//			me.template markOrbit<EDGE>(e);
//
//			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))
//	{
//		if ( !map.template isBoundaryMarked<2>(d) && mf.isMarked(d)) // for each face not subdivided
//		{
//			// compute center skip darts of new vertices non embedded
////			EMB center = AttribOps::zero<EMB,PFP>();
//			EMB center(0.0);
//			unsigned int count = 0 ;
//			mf.template unmarkOrbit<FACE>(d) ;
//			Dart it = d;
//			do
//			{
//				center += attributs[it];
//				++count ;
//				me.template unmarkOrbit<PFP::MAP::EDGE_OF_PARENT>(it);
//
//				it = map.phi1(it) ;
//				me.template unmarkOrbit<PFP::MAP::EDGE_OF_PARENT>(it);
//				it = map.phi1(it) ;
//			} while(it != d) ;
//			center /= float(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
//		if (!map.isBoundaryEdge(x))
//		{
//			Dart f1 = map.phi_1(x);
//			Dart f2 = map.phi2(map.phi1(map.phi2(x)));
////			EMB temp = AttribOps::zero<EMB,PFP>();
////			temp = attributs[f1];
//			EMB temp = attributs[f1];
//			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>();
//		EMB temp(0.0);
//		EMB temp2(0.0);
//
//		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;
//			x = map.phi2_1(x);
//		} while (x != *vert);
//
//		EMB emcp = attributs[*vert];
//		emcp *= float((n-2)*n);		// V' = (n-2)/n*V + 1/n2 *(F+E)
//		emcp += temp;
//		emcp += temp2;
//		emcp /= float(n*n);
//
//		attributs[*vert] = emcp ;
//	}
//}


315
template <typename PFP, typename EMBV>
316
void CatmullClarkSubdivision(typename PFP::MAP& map, EMBV& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
317
{
Sylvain Thery's avatar
Sylvain Thery committed
318

319
	typedef typename PFP::MAP MAP;
320 321
	typedef typename EMBV::DATA_TYPE EMB;

Pierre Kraemer's avatar
Pierre Kraemer committed
322 323 324
	std::vector<Dart> l_middles;
	std::vector<Dart> l_verts;

325 326 327
	CellMarkerNoUnmark<MAP, VERTEX> m0(map);
	DartMarkerNoUnmark<MAP> mf(map);
	DartMarkerNoUnmark<MAP> me(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
328 329 330 331

	// first pass: cut edges
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
332
		if ( !map.template isBoundaryMarked<2>(d) && !me.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
333 334 335 336 337 338
		{
			if (!m0.isMarked(d))
			{
				m0.mark(d);
				l_verts.push_back(d);
			}
339 340
			Dart d2 = map.phi2(d);
			if (!m0.isMarked(d2))
Pierre Kraemer's avatar
Pierre Kraemer committed
341
			{
342 343
				m0.mark(d2);
				l_verts.push_back(d2);
Pierre Kraemer's avatar
Pierre Kraemer committed
344 345 346
			}

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

349
			attributs[e] = attributs[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
350 351 352
			attributs[e] += attributs[f];
			attributs[e] *= 0.5;

Pierre Kraemer's avatar
Pierre Kraemer committed
353 354
			me.template markOrbit<EDGE>(d);
			me.template markOrbit<EDGE>(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
355 356 357 358 359 360 361 362 363 364 365

			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))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
366
		if ( !map.template isBoundaryMarked<2>(d) && mf.isMarked(d)) // for each face not subdivided
Pierre Kraemer's avatar
Pierre Kraemer committed
367 368
		{
			// compute center skip darts of new vertices non embedded
369
			EMB center(0.0);
Pierre Kraemer's avatar
Pierre Kraemer committed
370
			unsigned int count = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
371
			mf.template unmarkOrbit<FACE>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
372 373 374 375 376
			Dart it = d;
			do
			{
				center += attributs[it];
				++count ;
Pierre Kraemer's avatar
Pierre Kraemer committed
377
				me.template unmarkOrbit<PFP::MAP::EDGE_OF_PARENT>(it);
378

379
				it = map.phi1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
380
				me.template unmarkOrbit<PFP::MAP::EDGE_OF_PARENT>(it);
381
				it = map.phi1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
382
			} while(it != d) ;
383
			center /= float(count);
Pierre Kraemer's avatar
Pierre Kraemer committed
384 385
			Dart cf = quadranguleFace<PFP>(map, d);	// quadrangule the face
			attributs[cf] = center;					// affect the data to the central vertex
Sylvain Thery's avatar
Sylvain Thery committed
386

Pierre Kraemer's avatar
Pierre Kraemer committed
387 388 389 390
		}
	}

	// Compute edge points
Sylvain Thery's avatar
Sylvain Thery committed
391
	for (typename std::vector<Dart>::iterator mid = l_middles.begin(); mid != l_middles.end(); ++mid)
Pierre Kraemer's avatar
Pierre Kraemer committed
392 393 394
	{
		Dart x = *mid;
		// other side of the edge
395
		if (!map.isBoundaryEdge(x))
Pierre Kraemer's avatar
Pierre Kraemer committed
396 397
		{
			Dart f1 = map.phi_1(x);
398
			Dart f2 = map.phi2(map.phi1(map.phi2(x)));
Sylvain Thery's avatar
Sylvain Thery committed
399
			attributs[x] += (attributs[f1] + attributs[f2]) / 4.0 - (attributs[x] / 2.0);
Pierre Kraemer's avatar
Pierre Kraemer committed
400 401 402 403 404
		}
		// else nothing to do point already in the middle of segment
	}

	// Compute vertex points
Sylvain Thery's avatar
Sylvain Thery committed
405
	for (typename std::vector<Dart>::iterator vert = l_verts.begin(); vert != l_verts.end(); ++vert)
Pierre Kraemer's avatar
Pierre Kraemer committed
406 407 408
	{
		m0.unmark(*vert);

Sylvain Thery's avatar
Sylvain Thery committed
409 410
		EMB sumFace(0.0); // Sum_F
		EMB sumEdge(0.0);// Sum_E
Pierre Kraemer's avatar
Pierre Kraemer committed
411

Sylvain Thery's avatar
Sylvain Thery committed
412
		int n = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
413 414 415 416 417 418
		Dart x = *vert;
		do
		{
			Dart m = map.phi1(x);
			Dart f = map.phi2(m);

Sylvain Thery's avatar
Sylvain Thery committed
419 420
			sumFace += attributs[f];
			sumEdge += attributs[m];
Pierre Kraemer's avatar
Pierre Kraemer committed
421
			++n;
422
			x = map.phi2_1(x);
Pierre Kraemer's avatar
Pierre Kraemer committed
423 424
		} while (x != *vert);

Sylvain Thery's avatar
Sylvain Thery committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
		EMB deltaV = attributs[*vert] * float(-3*n);	// (-3 * attributs[*vert]
		deltaV += sumFace;								// + sumFace/n
		deltaV += 2.0*sumEdge;							// + sumEdge/n)
		deltaV /= float(n*n);								// /n

		attributs[*vert] += deltaV;
	}
}






template <typename PFP, typename EMBV>
void CatmullClarkInterpolSubdivision(typename PFP::MAP& map, EMBV& attributs)
{
	typedef typename PFP::MAP MAP;
	typedef typename EMBV::DATA_TYPE EMB;

445
	VertexAutoAttribute<EMB, typename PFP::MAP> facesAverage(map);
Sylvain Thery's avatar
Sylvain Thery committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

	std::vector<Dart> l_vertices;
	std::vector<Dart> l_edges;
	std::vector<Dart> l_faces;
	l_vertices.reserve(attributs.nbElements() + 10);
	l_faces.reserve(l_vertices.capacity() / 2);
	l_edges.reserve(l_faces.capacity() * 2);

	DartMarker<MAP> mf(map);
	DartMarker<MAP> me(map);

	for (Vertex v : allVerticesOf(map))
	{
		l_vertices.push_back(v);
	}

	// first step: cut edges
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (!me.isMarked(d))
		{
Sylvain Thery's avatar
Sylvain Thery committed
467
			mf.template markOrbit<EDGE>(d);
Sylvain Thery's avatar
Sylvain Thery committed
468 469 470 471 472 473
			Dart f = map.phi1(d);
			Dart e = map.cutEdge(d);
			attributs[e] = (attributs[d] + attributs[f]) /2.0;
			me.template markOrbit<EDGE>(d);
			me.template markOrbit<EDGE>(e);
			// warning store the dart that does not belong to the boundary
Sylvain Thery's avatar
Sylvain Thery committed
474
			if (map.template isBoundaryMarked<2>(e))
Sylvain Thery's avatar
Sylvain Thery committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
				l_edges.push_back(map.phi2(d));
			else
				l_edges.push_back(e);
		}
	}


	// second step: quandrangule faces
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (!map.template isBoundaryMarked<2>(d) && mf.isMarked(d)) 
		{
			EMB center(0.0);
			unsigned int count = 0;
			mf.template unmarkOrbit<FACE>(d);
			Dart it = d;
			do
			{
				center += attributs[it];
				++count;
				// unmark me on the fly 
				me.template unmarkOrbit<PFP::MAP::EDGE_OF_PARENT>(it);
				it = map.phi1(it);
				me.template unmarkOrbit<PFP::MAP::EDGE_OF_PARENT>(it);
				it = map.phi1(it); // do not forget to skip the middle edge vertices
			} while (it != d);
			center /= float(count);
			Dart cf = quadranguleFace<PFP>(map, d);	// quadrangule the face
			attributs[cf] = center;					// affect the data to the central vertex
			l_faces.push_back(cf);
		}
	}
	
	// compute face average of "vertices"
	for (Dart v : l_vertices)
	{
		EMB average(0.0);
		int n = 0;
		for (Face x : facesIncidentToVertex2(map, v))
		{
			Dart f = map.template phi<11>(x);
			++n;
			average += attributs[f];
		}
		facesAverage[v] = average / float(n);
	}

	// compute face average of "edges"
	for (Dart e : l_edges)
	{
Sylvain Thery's avatar
Sylvain Thery committed
525
		if (!map.template isBoundaryMarked<2>(map.phi2(e))) // faster than map.isBoundaryEdge(e)
Sylvain Thery's avatar
Sylvain Thery committed
526 527 528 529 530 531 532 533
		{
			Dart ff = map.phi_1(e);
			Dart f = map.template phi<211>(e);
			facesAverage[e] = (attributs[ff] + attributs[f]) / 2.0;
		}
		else
			facesAverage[e] = attributs[e];
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
534

Sylvain Thery's avatar
Sylvain Thery committed
535 536 537 538 539
	// Move edge points
	for (Dart x: l_edges)
	{
		Dart xb = map.phi2(x);

Sylvain Thery's avatar
Sylvain Thery committed
540
		if (map.template isBoundaryMarked<2>(x))
Sylvain Thery's avatar
Sylvain Thery committed
541 542
			std::cout << "ERROR " << attributs[x];

Sylvain Thery's avatar
Sylvain Thery committed
543
		if (!map.template isBoundaryMarked<2>(xb))
Sylvain Thery's avatar
Sylvain Thery committed
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
		{
			Dart v1 = map.phi1(x);
			Dart v2 = map.phi_1(map.phi2(map.phi_1(x)));
			attributs[x] -= (facesAverage[v1] + facesAverage[v2]) / 4.0 - facesAverage[x] / 2.0;
		}
		else // do the 4 point scheme
		{
			Dart a = map.template phi<1112>(xb); 
			Dart b = map.template phi<12>(xb);
			Dart c = map.phi1(x);
			Dart d = map.phi2(map.phi_1(map.phi_1(map.phi_1(xb))));
			// use the 4 old vertices and not the two edge-points because we are writing on edge-points
			attributs[x] -= (attributs[a] + attributs[b] + attributs[c] + attributs[d]) / 16.0  - attributs[x] / 4.0;
		}
	}

	// Move face points
	for (Dart f : l_faces)
	{
		EMB sumVert(0.0); 
		EMB sumEdge(0.0);

		int n = 0;
		Dart x = f;
		do
		{
			Dart m = map.phi1(x);
			Dart v = map.phi1(m);
			sumVert += facesAverage[v];
			sumEdge += facesAverage[m];
			++n;
			x = map.phi2_1(x);
		} while (x != f);

		EMB deltaF = (double(-3 * n)*attributs[f] + sumVert + 2.0*sumEdge) / double(n*n);
		attributs[f] -= deltaF;
Pierre Kraemer's avatar
Pierre Kraemer committed
580 581 582
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
583 584 585 586 587





588 589 590 591 592
//template <typename PFP>
//void CatmullClarkSubdivision(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
//{
//	CatmullClarkSubdivision<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3>(map, position) ;
//}
593

Pierre Kraemer's avatar
Pierre Kraemer committed
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
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 ;
	}
}

618
template <typename PFP, typename EMBV>
619
void LoopSubdivision(typename PFP::MAP& map, EMBV& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
620
{
621
	typedef typename PFP::MAP MAP;
622 623
	typedef typename EMBV::DATA_TYPE EMB;

Pierre Kraemer's avatar
Pierre Kraemer committed
624 625 626
	std::vector<Dart> l_middles;
	std::vector<Dart> l_verts;

627 628 629
	CellMarkerNoUnmark<MAP, VERTEX> m0(map);
	DartMarkerNoUnmark<MAP> mv(map);
	DartMarkerNoUnmark<MAP> me(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
630 631 632 633

	// first pass cut edges
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
634
		if ( !map.template isBoundaryMarked<2>(d) && !me.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
635 636 637 638 639 640 641 642 643 644 645 646 647
		{
			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);
648
			Dart e = map.cutEdge(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
649 650 651 652 653

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

Pierre Kraemer's avatar
Pierre Kraemer committed
654 655
			me.template markOrbit<EDGE>(d);
			me.template markOrbit<EDGE>(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
656

Pierre Kraemer's avatar
Pierre Kraemer committed
657
			mv.template markOrbit<VERTEX>(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
658 659 660 661 662 663 664 665 666

			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;
667
		if (!map.isBoundaryEdge(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
668
		{
669
			Dart dd = map.phi2(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
670 671
			attributs[d] *= 0.75;
			Dart e1 = map.template phi<111>(d);
672 673 674 675 676

//			EMB temp(0.0);
//			temp += attributs[e1];

			EMB temp = attributs[e1];
Pierre Kraemer's avatar
Pierre Kraemer committed
677 678
			e1 = map.phi_1(map.phi_1(dd));
			temp += attributs[e1];
679
			temp *= 1.0 / 8.0;
Pierre Kraemer's avatar
Pierre Kraemer committed
680 681 682 683 684 685 686 687 688 689
			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);

690 691
//		EMB temp = AttribOps::zero<EMB,PFP>();
		EMB temp(0.0);
Pierre Kraemer's avatar
Pierre Kraemer committed
692 693 694 695 696 697 698
		int n = 0;
		Dart x = *vert;
		do
		{
			Dart y = map.phi1(map.phi1(x));
			temp += attributs[y];
			++n;
699
			x = map.phi2_1(x);
Pierre Kraemer's avatar
Pierre Kraemer committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
		} 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
724 725
			me.template unmarkOrbit<FACE>(d) ;
			mv.template unmarkOrbit<FACE>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

			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);
		}
	}
}

742 743 744 745 746 747
//template <typename PFP>
//void LoopSubdivision(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position)
//{
////	LoopSubdivision<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3>(map, position) ;
//	LoopSubdivisionGen<PFP, VertexAttribute<typename PFP::VEC3> >(map, position) ;
//}
748

749
template <typename PFP, typename EMBV>
750
void TwoNPlusOneSubdivision(typename PFP::MAP& map, EMBV& attributs, float size)
751
{
752
	typedef typename PFP::MAP MAP;
753 754
	typedef typename EMBV::DATA_TYPE EMB;

755 756
	CellMarker<MAP, EDGE> m0(map);
	CellMarker<MAP, FACE> m1(map);
757 758

	std::vector<Dart> dOrig;
759

760 761 762
	//first pass cut edge
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
763
		if(!m0.isMarked(d))
764
		{
765
			if(!m1.isMarked(d))
766
			{
767 768 769
				m1.mark(d);
				dOrig.push_back(d);
			}
770

771 772 773 774 775
//			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));
776
			}
777 778 779 780

			EMB e1 = attributs[d];
			EMB e2 = attributs[map.phi1(d)];
			map.cutEdge(d);
781
			attributs[map.phi1(d)] = e1*(1.0f-size)+e2*size;
782
			map.cutEdge(map.phi1(d));
783
			attributs[map.phi1(map.phi1(d))] = e2*(1.0f-size)+e1*size;
784 785 786
			m0.mark(d);
			m0.mark(map.phi1(d));
			m0.mark(map.template phi<11>(d));
787 788 789
		}
	}

790
	CGoGNout << "nb orig : " << dOrig.size() << CGoGNendl;
791

792
	DartMarkerNoUnmark<MAP> mCorner(map);
793 794 795
//	//second pass create corner face
	for (std::vector<Dart>::iterator it = dOrig.begin(); it != dOrig.end(); ++it)
	{
796
//		EMB c = Geometry::faceCentroid<PFP>(map,*it,attributs);
797 798 799 800 801 802
		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)));
803 804 805
//			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);
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
			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));
	}
}

823 824 825

template <typename PFP, typename EMBV>
void DooSabin(typename PFP::MAP& map, EMBV& position)
Sylvain Thery's avatar
Sylvain Thery committed
826
{
827
	typedef typename PFP::MAP MAP;
828 829
	typedef typename EMBV::DATA_TYPE EMB;

830
	DartMarker<MAP> dm(map);
Sylvain Thery's avatar
Sylvain Thery committed
831 832 833 834 835 836 837 838 839 840
	// 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
841
	for(Dart d = map.begin(); d != map.end(); map.next(d))
Sylvain Thery's avatar
Sylvain Thery committed
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	{
		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>())
					{
859 860
						Algo::Topo::setOrbitEmbedding<VERTEX>(map, nf, map.template getEmbedding<EDGE>(e));
						Algo::Topo::setOrbitEmbedding<VERTEX>(map, map.template phi<11>(nf), map.template getEmbedding<EDGE>(e2));
Sylvain Thery's avatar
Sylvain Thery committed
861 862
					}

Sylvain Thery's avatar
Sylvain Thery committed
863
					dm.template markOrbit<FACE>(nf);
Sylvain Thery's avatar
Sylvain Thery committed
864 865 866
					fp.push_back(map.phi1(nf));
					fp.push_back(map.phi_1(nf));
				}
Sylvain Thery's avatar
Sylvain Thery committed
867
				dm.template markOrbit<EDGE1>(e);
Sylvain Thery's avatar
Sylvain Thery committed
868
				e = map.phi1(e);
869
			} while (e!=d);
Sylvain Thery's avatar
Sylvain Thery committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
		}
	}
	// 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
				{
885 886
//					map.template setOrbitEmbedding<EDGE>(d,map.template getEmbedding<EDGE>(map.phi2(d)));
					Algo::Topo::setOrbitEmbedding<VERTEX>(map, d, map.template getEmbedding<EDGE>(map.phi2(d)));
Sylvain Thery's avatar
Sylvain Thery committed
887 888 889 890 891 892
					d = map.phi1(d);
				} while (d != df);
			}
		}
	}

893
	std::vector<EMB> buffer;
Sylvain Thery's avatar
Sylvain Thery committed
894 895 896 897
	buffer.reserve(8);
	for (std::vector<Dart>::iterator di=faces.begin(); di != faces.end(); ++di)
	{
		Dart e = *di;
898
		typename PFP::VEC3 center = Geometry::faceCentroid<PFP>(map,e,position);
Sylvain Thery's avatar
Sylvain Thery committed
899 900 901 902 903 904 905 906 907

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

		int N = buffer.size();
908
		for (int i = 0; i < N; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
909
		{
910
			EMB P(0);
911
			for (int j = 0; j < N; ++j)
Sylvain Thery's avatar
Sylvain Thery committed
912 913 914
			{
				if (j==i)
				{
915
					/*float*/typename PFP::REAL c1 = double(N+5)/double(4*N);
Sylvain Thery's avatar
Sylvain Thery committed
916 917 918 919
					P += buffer[j]*c1;
				}
				else
				{
920
					/*float*/typename PFP::REAL c2 = (3.0+2.0*cos(2.0*M_PI*(double(i-j))/double(N))) /(4.0*N);
Sylvain Thery's avatar
Sylvain Thery committed
921 922 923
					P+= c2*buffer[j];
				}
			}
924 925
//			map.template setOrbitEmbeddingOnNewCell<VERTEX>(e);
			Algo::Topo::setOrbitEmbeddingOnNewCell<VERTEX>(map,e);
Sylvain Thery's avatar
Sylvain Thery committed
926 927 928 929 930 931 932
			position[e] = P;
			e = map.phi1(e);
		}
		buffer.clear();
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
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
953 954
}

Pierre Kraemer's avatar
Pierre Kraemer committed
955
//template <typename PFP>
956
//void Sqrt3Subdivision(typename PFP::MAP& map, VertexAttribute<VEC3>& position)
Pierre Kraemer's avatar
Pierre Kraemer committed
957 958 959 960
//{
//	typedef typename PFP::VEC3 VEC3 ;
//	typedef typename PFP::REAL REAL ;
//
961
//	FaceAttribute<VEC3> positionF = map.template getAttribute<VEC3, FACE>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
962
//	if(!positionF.isValid())
963
//		positionF = map.template addAttribute<VEC3, FACE>("position") ;
964
//	Geometry::computeCentroidFaces<PFP>(map, position, positionF) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
965
//
966
//	computeDual<PFP>(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
967
//
968
//	VertexAttribute<VEC3> tmp = position ;
Pierre Kraemer's avatar
Pierre Kraemer committed
969 970 971 972 973 974
//	position = positionF ;
//	positionF = tmp ;
//
//	CellMarker m(map, VERTEX) ;
//	m.markAll() ;
//
975
//	trianguleFaces<PFP>(map, position, positionF);
Pierre Kraemer's avatar
Pierre Kraemer committed
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
//
//	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
1001

1002
template <typename PFP>
1003
void computeDual(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
1004
{
1005 1006 1007
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;

1008
	// Face Attribute -> after dual new Vertex Attribute
1009
	FaceAttribute<VEC3, MAP> positionF  = map.template getAttribute<VEC3, FACE,MAP>("position") ;
1010
	if(!positionF.isValid())
1011
		positionF = map.template addAttribute<VEC3, FACE,MAP>("position") ;
1012

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	// Compute Centroid for the faces
	Algo::Surface::Geometry::computeCentroidFaces<PFP>(map, position, positionF) ;

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


template <typename PFP>
1023
void computeBoundaryConstraintDual(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
1024
{
1025
	typedef typename PFP::MAP MAP ;
1026
	typedef typename PFP::MAP MAP ;
1027 1028 1029
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

1030
	// Face Attribute -> after dual new Vertex Attribute
1031
	FaceAttribute<VEC3, MAP> positionF  = map.template getAttribute<VEC3, FACE,MAP>("position") ;
1032
	if(!positionF.isValid())
1033
		positionF = map.template addAttribute<VEC3, FACE,MAP>("position") ;
1034 1035 1036

	//Triangule boundary faces & compute for each new face the centroid
	std::vector<Dart> boundsDart;
1037
	DartMarkerStore<MAP> mf(map);
1038 1039
	for(Dart dit = map.begin() ; dit != map.end() ; map.next(dit))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
1040
		if(!mf.isMarked(dit) && map.template isBoundaryMarked<2>(dit))
1041 1042 1043 1044 1045 1046 1047 1048
		{
			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)) ;

1049
			positionF[dit] = (position[dit] + position[map.phi2(dit)]) * REAL(0.5);
Pierre Kraemer's avatar
Pierre Kraemer committed
1050
			mf.template markOrbit<FACE>(dit);
1051 1052 1053 1054 1055 1056 1057 1058

			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)) ;
1059
				positionF[prev] = (position[prev] + position[map.phi1(prev)]) * REAL(0.5);
Pierre Kraemer's avatar
Pierre Kraemer committed
1060
				mf.template markOrbit<FACE>(prev);
1061 1062 1063
				dd = next ;
			}

1064
			positionF[dprev] = (position[dprev] + position[map.phi1(dprev)]) * REAL(0.5);
Pierre Kraemer's avatar
Pierre Kraemer committed
1065
			mf.template markOrbit<FACE>(dprev);
1066 1067 1068 1069 1070 1071 1072 1073 1074
		}
	}

	// 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))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
1075
		if(mf.isMarked(dit) && map.template isBoundaryMarked<2>(dit))
1076 1077
		{
			map.fillHole(dit);
Pierre Kraemer's avatar
Pierre Kraemer committed
1078
			mf.template unmarkOrbit<FACE>(dit);
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
		}
	}

	// 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
1092

1093
template <typename PFP>
1094
void computeBoundaryConstraintKeepingOldVerticesDual(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
1095
{
1096
	typedef typename PFP::MAP MAP ;
1097
	typedef typename PFP::MAP MAP ;
1098 1099 1100
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

1101
	// Face Attribute -> after dual new Vertex Attribute
1102
	FaceAttribute<VEC3, MAP> positionF  = map.template getAttribute<VEC3, FACE,MAP>("position") ;
1103
	if(!positionF.isValid())
1104
		positionF = map.template addAttribute<VEC3, FACE,MAP>("position") ;
1105 1106 1107

	//Triangule boundary faces & compute for each new face the centroid
	std::vector<Dart> boundsDart;
1108
	DartMarkerStore<MAP> mf(map);
1109 1110
	for(Dart dit = map.begin() ; dit != map.end() ; map.next(dit))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
1111
		if(!mf.isMarked(dit) && map.template isBoundaryMarked<2>(dit))
1112 1113 1114 1115 1116 1117 1118 1119
		{
			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)) ;

1120
			positionF[dit] = (position[dit] + position[map.phi2(dit)]) * REAL(0.5);
Pierre Kraemer's avatar
Pierre Kraemer committed
1121
			mf.template markOrbit<FACE>(dit);
1122 1123 1124 1125 1126 1127 1128 1129

			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)) ;
1130
				positionF[prev] = (position[prev] + position[map.phi1(prev)]) * REAL(0.5);
Pierre Kraemer's avatar
Pierre Kraemer committed
1131
				mf.template markOrbit<FACE>(prev);