edgeSelector.hpp 59.5 KB
Newer Older
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2013, IGG Team, ICube, University of Strasbourg           *
Kenneth Vanhoey's avatar
Kenneth Vanhoey 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/                                           *
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
21 22 23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Sylvain Thery's avatar
Sylvain Thery committed
24
#include <Algo/Topo/embedding.h>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
25 26 27 28 29 30 31

namespace CGoGN
{

namespace Algo
{

32 33 34
namespace Surface
{

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
namespace Decimation
{

/************************************************************************************
 *                                  MAP ORDER                                       *
 ************************************************************************************/

template <typename PFP>
bool EdgeSelector_MapOrder<PFP>::init()
{
	cur = this->m_map.begin() ;
	return true ;
}

template <typename PFP>
50
bool EdgeSelector_MapOrder<PFP>::nextEdge(Dart& d) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
51
{
Pierre Kraemer's avatar
Pierre Kraemer committed
52
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
53 54 55 56 57 58 59
	if(cur == m.end())
		return false ;
	d = cur ;
	return true ;
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
60
void EdgeSelector_MapOrder<PFP>::updateAfterCollapse(Dart /*d2*/, Dart /*dd2*/)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
61
{
Pierre Kraemer's avatar
Pierre Kraemer committed
62
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
63
	cur = m.begin() ;
64
	while(!m.edgeCanCollapse(cur))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
	{
		m.next(cur) ;
		if(cur == m.end())
			break ;
	}
}

/************************************************************************************
 *                                    RANDOM                                        *
 ************************************************************************************/

template <typename PFP>
bool EdgeSelector_Random<PFP>::init()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
79
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
80 81 82 83 84 85 86

	darts.reserve(m.getNbDarts()) ;
	darts.clear() ;

	for(Dart d = m.begin(); d != m.end(); m.next(d))
		darts.push_back(d) ;

Sylvain Thery's avatar
Sylvain Thery committed
87 88
	srand((unsigned int )(time(NULL))) ;
	int remains = int(darts.size()) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	for(unsigned int i = 0; i < darts.size()-1; ++i) // generate the random permutation
	{
		int r = (rand() % remains) + i ;
		// swap ith and rth elements
		Dart tmp = darts[i] ;
		darts[i] = darts[r] ;
		darts[r] = tmp ;
		--remains ;
	}
	cur = 0 ;
	allSkipped = true ;

	return true ;
}

template <typename PFP>
105
bool EdgeSelector_Random<PFP>::nextEdge(Dart& d) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
106 107 108 109 110 111 112 113
{
	if(cur == darts.size() && allSkipped)
		return false ;
	d = darts[cur] ;
	return true ;
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
114
void EdgeSelector_Random<PFP>::updateAfterCollapse(Dart /*d2*/, Dart /*dd2*/)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
115
{
Pierre Kraemer's avatar
Pierre Kraemer committed
116
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
117 118 119 120 121 122 123 124 125
	allSkipped = false ;
	do
	{
		++cur ;
		if(cur == darts.size())
		{
			cur = 0 ;
			allSkipped = true ;
		}
126
	} while(!m.edgeCanCollapse(darts[cur])) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
127 128
}

129 130 131
template <typename PFP>
void EdgeSelector_Random<PFP>::updateWithoutCollapse()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
132
	MAP& m = this->m_map ;
133 134 135 136 137 138 139 140 141
	allSkipped = false ;
	do
	{
		++cur ;
		if(cur == darts.size())
		{
			cur = 0 ;
			allSkipped = true ;
		}
142
	} while(!m.edgeCanCollapse(darts[cur])) ;
143 144
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
145 146 147 148 149 150 151
/************************************************************************************
 *                                 EDGE LENGTH                                      *
 ************************************************************************************/

template <typename PFP>
bool EdgeSelector_Length<PFP>::init()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
152
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
153 154 155

	edges.clear() ;

156
	for (Edge e : allEdgesOf(m))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
157
	{
158
		initEdgeInfo(e.dart) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
159 160 161 162 163 164 165 166
	}

	cur = edges.begin() ; // init the current edge to the first one

	return true ;
}

template <typename PFP>
167
bool EdgeSelector_Length<PFP>::nextEdge(Dart& d) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
168 169 170 171 172 173 174 175 176 177
{
	if(cur == edges.end() || edges.empty())
		return false ;
	d = (*cur).second ;
	return true ;
}

template <typename PFP>
void EdgeSelector_Length<PFP>::updateBeforeCollapse(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
178
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
179

180
	EdgeInfo* edgeE = &(edgeInfo[d]) ;
181 182
	if(edgeE->valid)
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
183

184 185 186
	edgeE = &(edgeInfo[m.phi1(d)]) ;
	if(edgeE->valid)					// remove all
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
187

188 189 190
	edgeE = &(edgeInfo[m.phi_1(d)]) ;	// the concerned edges
	if(edgeE->valid)
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
191 192 193 194
									// from the multimap
	Dart dd = m.phi2(d) ;
	if(dd != d)
	{
195 196 197
		edgeE = &(edgeInfo[m.phi1(dd)]) ;
		if(edgeE->valid)
			edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
198

199 200 201
		edgeE = &(edgeInfo[m.phi_1(dd)]) ;
		if(edgeE->valid)
			edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
202 203 204 205 206 207
	}
}

template <typename PFP>
void EdgeSelector_Length<PFP>::updateAfterCollapse(Dart d2, Dart dd2)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
208
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
209 210 211 212

	Dart vit = d2 ;
	do
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
213 214
		updateEdgeInfo(m.phi1(vit), false) ;		// must recompute some edge infos in the
		if(vit == d2 || vit == dd2)					// neighborhood of the collapsed edge
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
215
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
216 217
			initEdgeInfo(vit) ;						// various optimizations are applied here by
													// treating differently :
218
			Dart vit2 = m.phi12(m.phi1(vit)) ;		// - edges for which the criteria must be recomputed
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220
			Dart stop = m.phi2(vit) ;				// - edges that must be re-embedded
			do										// - edges for which only the collapsibility must be re-tested
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
221 222 223
			{
				updateEdgeInfo(vit2, false) ;
				updateEdgeInfo(m.phi1(vit2), false) ;
224
				vit2 = m.phi12(vit2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
225 226 227 228 229
			} while(vit2 != stop) ;
		}
		else
			updateEdgeInfo(vit, true) ;

230
		vit = m.phi2_1(vit) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
231 232 233 234 235
	} while(vit != d2) ;

	cur = edges.begin() ; // set the current edge to the first one
}

236 237 238 239 240 241 242 243 244 245
template <typename PFP>
void EdgeSelector_Length<PFP>::updateWithoutCollapse()
{
	EdgeInfo& einfo = edgeInfo[(*cur).second] ;
	einfo.valid = false ;
	edges.erase(einfo.it) ;

	cur = edges.begin();
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
246 247 248
template <typename PFP>
void EdgeSelector_Length<PFP>::initEdgeInfo(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
249
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
250 251 252 253 254 255 256 257 258 259 260
	EdgeInfo einfo ;
	if(m.edgeCanCollapse(d))
		computeEdgeInfo(d, einfo) ;
	else
		einfo.valid = false ;
	edgeInfo[d] = einfo ;
}

template <typename PFP>
void EdgeSelector_Length<PFP>::updateEdgeInfo(Dart d, bool recompute)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
261
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
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
	EdgeInfo& einfo = edgeInfo[d] ;
	if(recompute)
	{
		if(einfo.valid)
			edges.erase(einfo.it) ;			// remove the edge from the multimap
		if(m.edgeCanCollapse(d))
			computeEdgeInfo(d, einfo) ;
		else
			einfo.valid = false ;
	}
	else
	{
		if(m.edgeCanCollapse(d))
		{									// if the edge can be collapsed now
			if(!einfo.valid)				// but it was not before
				computeEdgeInfo(d, einfo) ;
		}
		else
		{									// if the edge cannot be collapsed now
			if(einfo.valid)					// and it was before
			{
				edges.erase(einfo.it) ;
				einfo.valid = false ;
			}
		}
	}
}

template <typename PFP>
void EdgeSelector_Length<PFP>::computeEdgeInfo(Dart d, EdgeInfo& einfo)
{
293
	VEC3 vec = Algo::Surface::Geometry::vectorOutOfDart<PFP>(this->m_map, d, position) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
294 295 296 297 298 299 300 301 302 303 304
	einfo.it = edges.insert(std::make_pair(vec.norm2(), d)) ;
	einfo.valid = true ;
}

/************************************************************************************
 *                            QUADRIC ERROR METRIC                                  *
 ************************************************************************************/

template <typename PFP>
bool EdgeSelector_QEM<PFP>::init()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
305
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
306

307 308
	assert(m_positionApproximator.getType() != A_hQEM || !"Approximator(hQEM) and selector (EdgeSelector_QEM) are not compatible") ;
	assert(m_positionApproximator.getType() != A_hHalfCollapse || !"Approximator(hHalfCollapse) and selector (EdgeSelector_QEM) are not compatible") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
309

310 311
	if(m_positionApproximator.getApproximatedAttributeName() != m_position.name())
	{
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
312
		return false ;
313
	}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
314

315
	for (Vertex v : allVerticesOf(m))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
316
	{
317 318
		Utils::Quadric<REAL> q ;	// create one quadric
		quadric[v] = q ;				// per vertex
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
319 320
	}

321
	for (Face f : allFacesOf(m))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
322
	{
323 324 325 326 327 328 329
		Dart d = f.dart;
		Dart d1 = m.phi1(d) ;	// for each triangle,
		Dart d_1 = m.phi_1(d) ;	// initialize the quadric of the triangle
		Utils::Quadric<REAL> q(m_position[d], m_position[d1], m_position[d_1]) ;
		quadric[d] += q ;		// and add the contribution of
		quadric[d1] += q ;		// this quadric to the ones
		quadric[d_1] += q ;		// of the 3 incident vertices
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
330 331
	}

332 333 334
	edges.clear() ;

	for (Edge e : allEdgesOf(m))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
335
	{
336 337
		initEdgeInfo(e.dart) ;	// init the edges with their optimal position
	}							// and insert them in the multimap according to their error
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
338 339 340 341 342 343 344

	cur = edges.begin() ; // init the current edge to the first one

	return true ;
}

template <typename PFP>
345
bool EdgeSelector_QEM<PFP>::nextEdge(Dart& d) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
346 347 348 349 350 351 352 353 354 355
{
	if(cur == edges.end() || edges.empty())
		return false ;
	d = (*cur).second ;
	return true ;
}

template <typename PFP>
void EdgeSelector_QEM<PFP>::updateBeforeCollapse(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
356
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
357

358 359 360
	EdgeInfo *edgeE = &(edgeInfo[d]) ;
	if(edgeE->valid)
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
361

362 363 364
	edgeE = &(edgeInfo[m.phi1(d)]) ;
	if(edgeE->valid)					// remove all
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
365

366 367 368
	edgeE = &(edgeInfo[m.phi_1(d)]) ;	// the concerned edges
	if(edgeE->valid)
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
369 370 371 372
									// from the multimap
	Dart dd = m.phi2(d) ;
	if(dd != d)
	{
373 374 375
		edgeE = &(edgeInfo[m.phi1(dd)]) ;
		if(edgeE->valid)
			edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
376

377 378 379
		edgeE = &(edgeInfo[m.phi_1(dd)]) ;
		if(edgeE->valid)
			edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
380 381 382 383 384 385 386 387 388 389
	}

	tmpQ.zero() ;			// compute quadric for the new
	tmpQ += quadric[d] ;	// vertex as the sum of those
	tmpQ += quadric[dd] ;	// of the contracted vertices
}

template <typename PFP>
void EdgeSelector_QEM<PFP>::updateAfterCollapse(Dart d2, Dart dd2)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
390
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
391 392 393 394 395 396

	quadric[d2] = tmpQ ;

	Dart vit = d2 ;
	do
	{
397 398
		updateEdgeInfo(m.phi1(vit), false) ;		// must recompute some edge infos in the
		if(vit == d2 || vit == dd2)					// neighborhood of the collapsed edge
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
399
		{
400 401
			initEdgeInfo(vit) ;						// various optimizations are applied here by
													// treating differently :
402
			Dart vit2 = m.phi12(m.phi1(vit)) ;		// - edges for which the criteria must be recomputed
403 404
			Dart stop = m.phi2(vit) ;				// - edges that must be re-embedded
			do										// - edges for which only the collapsibility must be re-tested
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
405 406 407
			{
				updateEdgeInfo(vit2, false) ;
				updateEdgeInfo(m.phi1(vit2), false) ;
408
				vit2 = m.phi12(vit2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
409 410 411 412 413
			} while(vit2 != stop) ;
		}
		else
			updateEdgeInfo(vit, true) ;

414
		vit = m.phi2_1(vit) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
415 416 417 418 419
	} while(vit != d2) ;

	cur = edges.begin() ; // set the current edge to the first one
}

420 421 422 423 424 425 426 427 428 429 430
template <typename PFP>
void EdgeSelector_QEM<PFP>::updateWithoutCollapse()
{
	EdgeInfo& einfo = edgeInfo[(*cur).second] ;
	einfo.valid = false ;
	edges.erase(einfo.it) ;

	//edges.erase(cur) ;
	cur = edges.begin();
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
431 432 433
template <typename PFP>
void EdgeSelector_QEM<PFP>::initEdgeInfo(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
434 435
	MAP& m = this->m_map ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
436 437 438 439 440 441 442 443 444 445 446
	EdgeInfo einfo ;
	if(m.edgeCanCollapse(d))
		computeEdgeInfo(d, einfo) ;
	else
		einfo.valid = false ;
	edgeInfo[d] = einfo ;
}

template <typename PFP>
void EdgeSelector_QEM<PFP>::updateEdgeInfo(Dart d, bool recompute)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
447 448
	MAP& m = this->m_map ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	EdgeInfo& einfo = edgeInfo[d] ;
	if(recompute)
	{
		if(einfo.valid)
			edges.erase(einfo.it) ;		// remove the edge from the multimap
		if(m.edgeCanCollapse(d))
			computeEdgeInfo(d, einfo) ;
		else
			einfo.valid = false ;
	}
	else
	{
		if(m.edgeCanCollapse(d))
		{								 	// if the edge can be collapsed now
			if(!einfo.valid)				// but it was not before
				computeEdgeInfo(d, einfo) ;
		}
		else
		{								 // if the edge cannot be collapsed now
			if(einfo.valid)				 // and it was before
			{
				edges.erase(einfo.it) ;
				einfo.valid = false ;
			}
		}
	}
}

template <typename PFP>
void EdgeSelector_QEM<PFP>::computeEdgeInfo(Dart d, EdgeInfo& einfo)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
480 481
	MAP& m = this->m_map ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
482 483
	Dart dd = m.phi2(d) ;

484
	Utils::Quadric<REAL> quad ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
485 486 487
	quad += quadric[d] ;	// compute the sum of the
	quad += quadric[dd] ;	// two vertices quadrics

488
	m_positionApproximator.approximate(d) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
489

490
	REAL err = quad(m_positionApproximator.getApprox(d)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
491 492 493 494 495 496 497 498 499 500 501 502

	einfo.it = edges.insert(std::make_pair(err, d)) ;
	einfo.valid = true ;
}

/************************************************************************************
 *                            QUADRIC ERROR METRIC (Memoryless version)             *
 ************************************************************************************/

template <typename PFP>
bool EdgeSelector_QEMml<PFP>::init()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
503
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
504

505 506
	assert(m_positionApproximator.getType() != A_hQEM || !"Approximator(hQEM) and selector (EdgeSelector_QEMml) are not compatible") ;
	assert(m_positionApproximator.getType() != A_hHalfCollapse || !"Approximator(hHalfCollapse) and selector (EdgeSelector_QEMml) are not compatible") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
507

508 509
	if(m_positionApproximator.getApproximatedAttributeName() != m_position.name())
	{
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
510
		return false ;
511
	}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
512

513
	for (Vertex v : allVerticesOf(m))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
514
	{
515 516
		Utils::Quadric<REAL> q ;	// create one quadric
		quadric[v] = q ;				// per vertex
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
517 518
	}

519
	for (Face f : allFacesOf(m))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
520
	{
521 522 523 524 525 526 527
		Dart d = f.dart;
		Dart d1 = m.phi1(d) ;	// for each triangle,
		Dart d_1 = m.phi_1(d) ;	// initialize the quadric of the triangle
		Utils::Quadric<REAL> q(m_position[d], m_position[d1], m_position[d_1]) ;
		quadric[d] += q ;		// and add the contribution of
		quadric[d1] += q ;		// this quadric to the ones
		quadric[d_1] += q ;		// of the 3 incident vertices
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
528 529
	}

530 531 532
	edges.clear() ;

	for (Edge e : allEdgesOf(m))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
533
	{
534 535
		initEdgeInfo(e.dart) ;	// init the edges with their optimal position
	}							// and insert them in the multimap according to their error
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
536 537 538 539 540 541 542

	cur = edges.begin() ; // init the current edge to the first one

	return true ;
}

template <typename PFP>
543
bool EdgeSelector_QEMml<PFP>::nextEdge(Dart& d) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
544 545 546 547 548 549 550 551 552 553
{
	if(cur == edges.end() || edges.empty())
		return false ;
	d = (*cur).second ;
	return true ;
}

template <typename PFP>
void EdgeSelector_QEMml<PFP>::updateBeforeCollapse(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
554
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
555

556 557 558
	EdgeInfo *edgeE = &(edgeInfo[d]) ;
	if(edgeE->valid)
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
559

560 561 562
	edgeE = &(edgeInfo[m.phi1(d)]) ;
	if(edgeE->valid)					// remove all
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
563

564 565 566
	edgeE = &(edgeInfo[m.phi_1(d)]) ;	// the concerned edges
	if(edgeE->valid)
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
567 568 569 570
									// from the multimap
	Dart dd = m.phi2(d) ;
	if(dd != d)
	{
571 572 573
		edgeE = &(edgeInfo[m.phi1(dd)]) ;
		if(edgeE->valid)
			edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
574

575 576 577
		edgeE = &(edgeInfo[m.phi_1(dd)]) ;
		if(edgeE->valid)
			edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
578 579 580 581 582 583 584 585 586 587
	}
}

/**
 * Update quadric of a vertex
 * Discards quadrics of d and assigns freshly calculated
 * quadrics depending on the actual planes surrounding d
 * @param dart d
 */
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
588 589
void EdgeSelector_QEMml<PFP>::recomputeQuadric(const Dart d, const bool recomputeNeighbors)
{
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
	MAP& m = this->m_map;
	Utils::Quadric<REAL>& q = quadric[d];
	q.zero() ;
	Traversor2VF<MAP> tf(m, d);
	for (Dart f = tf.begin() ; f != tf.end() ; f = tf.next())
	{
		q += Utils::Quadric<REAL>(m_position[f], m_position[m.phi1(f)], m_position[m.phi_1(f)]) ;
	}

	if (recomputeNeighbors)
	{
		Traversor2VVaE<MAP> tv(m, d);
		for (Dart v = tv.begin() ; v != tv.end() ; v = tv.next())
		{
			recomputeQuadric(v, false);
		}
	}

//	Dart dFront,dBack ;
//	Dart dInit = d ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
610

611 612
//	// Init Front
//	dFront = dInit ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
613

614
//	quadric[d].zero() ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
615

616 617 618 619
//   	do {
//   		// Make step
//   		dBack = this->m_map.phi2(dFront) ;
//       	dFront = this->m_map.phi2_1(dFront) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
620

621 622 623 624 625
//       	if (dBack != dFront) { // if dFront is no border
//           	quadric[d] += Utils::Quadric<REAL>(this->m_position[d],this->m_position[this->m_map.phi2(dFront)],this->m_position[dBack]) ;
//       	}
//       	if (recomputeNeighbors)
//       		recomputeQuadric(dBack, false) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
626

627
//    } while(dFront != dInit) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
628 629 630 631 632
}

template <typename PFP>
void EdgeSelector_QEMml<PFP>::updateAfterCollapse(Dart d2, Dart dd2)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
633
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
634 635 636 637 638 639 640 641 642 643 644 645 646

	// for local vertex and neighbors
	recomputeQuadric(d2, true) ;

	Dart vit = d2 ;
	do
	{
		updateEdgeInfo(m.phi1(vit), true) ;			// must recompute some edge infos in the
		if(vit == d2 || vit == dd2)					// neighborhood of the collapsed edge
			initEdgeInfo(vit) ;						// various optimizations are applied here by
		else										// treating differently :
			updateEdgeInfo(vit, true) ;

647
		Dart vit2 = m.phi12(m.phi1(vit)) ;		// - edges for which the criteria must be recomputed
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
648 649 650 651 652
		Dart stop = m.phi2(vit) ;					// - edges that must be re-embedded
		do											// - edges for which only the collapsibility must be re-tested
		{
			updateEdgeInfo(vit2, true) ;
			updateEdgeInfo(m.phi1(vit2), false) ;
653
			vit2 = m.phi12(vit2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
654 655
		} while(vit2 != stop) ;

656
		vit = m.phi2_1(vit) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
657 658 659 660 661
	} while(vit != d2) ;

	cur = edges.begin() ; // set the current edge to the first one
}

662 663 664 665 666 667 668 669 670 671
template <typename PFP>
void EdgeSelector_QEMml<PFP>::updateWithoutCollapse()
{
	EdgeInfo& einfo = edgeInfo[(*cur).second] ;
	einfo.valid = false ;
	edges.erase(einfo.it) ;

	cur = edges.begin();
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
672 673 674
template <typename PFP>
void EdgeSelector_QEMml<PFP>::initEdgeInfo(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
675 676
	MAP& m = this->m_map ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
677 678 679 680 681 682 683 684 685 686 687
	EdgeInfo einfo ;
	if(m.edgeCanCollapse(d))
		computeEdgeInfo(d, einfo) ;
	else
		einfo.valid = false ;
	edgeInfo[d] = einfo ;
}

template <typename PFP>
void EdgeSelector_QEMml<PFP>::updateEdgeInfo(Dart d, bool recompute)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
688 689
	MAP& m = this->m_map ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	EdgeInfo& einfo = edgeInfo[d] ;
	if(recompute)
	{
		if(einfo.valid)
			edges.erase(einfo.it) ;		// remove the edge from the multimap
		if(m.edgeCanCollapse(d))
			computeEdgeInfo(d, einfo) ;
		else
			einfo.valid = false ;
	}
	else
	{
		if(m.edgeCanCollapse(d))
		{								 	// if the edge can be collapsed now
			if(!einfo.valid)				// but it was not before
				computeEdgeInfo(d, einfo) ;
		}
		else
		{								 // if the edge cannot be collapsed now
			if(einfo.valid)				 // and it was before
			{
				edges.erase(einfo.it) ;
				einfo.valid = false ;
			}
		}
	}
}

template <typename PFP>
void EdgeSelector_QEMml<PFP>::computeEdgeInfo(Dart d, EdgeInfo& einfo)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
721 722
	MAP& m = this->m_map ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
723 724
	Dart dd = m.phi2(d) ;

725
	Utils::Quadric<REAL> quad ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
726 727 728
	quad += quadric[d] ;	// compute the sum of the
	quad += quadric[dd] ;	// two vertices quadrics

729
	m_positionApproximator.approximate(d) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
730

731
	REAL err = quad(m_positionApproximator.getApprox(d)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
732 733 734 735
	einfo.it = edges.insert(std::make_pair(err, d)) ;
	einfo.valid = true ;
}

736 737 738 739 740 741 742
/************************************************************************************
 *                            Metric based on Face Normal and Area deviation        *
 ************************************************************************************/

template <typename PFP>
bool EdgeSelector_NormalArea<PFP>::init()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
743
	MAP& m = this->m_map ;
744

745
	if(m_positionApproximator.getApproximatedAttributeName() != m_position.name())
746 747
	{
		return false ;
748
	}
749

750
	for (Edge e : allEdgesOf(m))
751
	{
752
		computeEdgeMatrix(e.dart);
753 754
	}

755 756 757
	edges.clear() ;

	for (Edge e : allEdgesOf(m))
758
	{
759
		initEdgeInfo(e.dart) ;	// init "edgeInfo" and "edges"
760 761 762 763 764 765 766 767
	}

	cur = edges.begin() ; // init the current edge to the first one

	return true ;
}

template <typename PFP>
768
bool EdgeSelector_NormalArea<PFP>::nextEdge(Dart& d) const
769 770 771 772 773 774 775 776 777 778
{
	if(cur == edges.end() || edges.empty())
		return false ;
	d = (*cur).second ;
	return true ;
}

template <typename PFP>
void EdgeSelector_NormalArea<PFP>::updateBeforeCollapse(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
779 780
	MAP& m = this->m_map ;

781 782
	assert(!m.isBoundaryEdge(d));

783 784 785 786 787 788
	EdgeInfo* edgeE = &(edgeInfo[d]) ;
	if(edgeE->valid)
	{
		edges.erase(edgeE->it) ;
		edgeE->valid = false;
	}
789

790 791 792 793 794 795 796 797 798 799 800 801 802
	edgeE = &(edgeInfo[m.phi1(d)]) ;
	if(edgeE->valid)					// remove all
	{
		edges.erase(edgeE->it) ;
		edgeE->valid = false;
	}

	edgeE = &(edgeInfo[m.phi_1(d)]) ;	// the concerned edges
	if(edgeE->valid)
	{
		edges.erase(edgeE->it) ;
		edgeE->valid = false;
	}
803 804
									// from the multimap
	Dart dd = m.phi2(d) ;
805 806 807 808 809 810
	edgeE = &(edgeInfo[m.phi1(dd)]) ;
	if(edgeE->valid)
	{
		edges.erase(edgeE->it) ;
		edgeE->valid = false;
	}
811

812 813 814 815 816 817
	edgeE = &(edgeInfo[m.phi_1(dd)]) ;
	if(edgeE->valid)
	{
		edges.erase(edgeE->it) ;
		edgeE->valid = false;
	}
818 819 820
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
821
void EdgeSelector_NormalArea<PFP>::updateAfterCollapse(Dart d2, Dart /*dd2*/)
822
{
Pierre Kraemer's avatar
Pierre Kraemer committed
823
	MAP& m = this->m_map ;
824 825 826 827 828

	// update the edge matrices
	Traversor2VE<MAP> te (m,d2);
	for(Dart dit = te.begin() ; dit != te.end() ; dit = te.next())
	{
829
		computeEdgeMatrix(dit);
830 831 832 833 834
	}

	// update the multimap

	Traversor2VVaE<MAP> tv (m,d2);
835
	CellMarkerStore<MAP, EDGE> eMark (m);
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	for(Dart dit = tv.begin() ; dit != tv.end() ; dit = tv.next())
	{
		Traversor2VE<MAP> te2 (m,dit);
		for(Dart dit2 = te2.begin() ; dit2 != te2.end() ; dit2 = te2.next())
		{
			if (!eMark.isMarked(dit2))
			{
				updateEdgeInfo(dit2) ;
				eMark.mark(dit2);
			}
		}
	}

	cur = edges.begin() ; // set the current edge to the first one
}

template <typename PFP>
void EdgeSelector_NormalArea<PFP>::initEdgeInfo(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
855 856
	MAP& m = this->m_map ;

857 858 859 860 861 862 863 864 865 866 867
	EdgeInfo einfo ;
	if(m.edgeCanCollapse(d))
		computeEdgeInfo(d, einfo) ;
	else
		einfo.valid = false ;
	edgeInfo[d] = einfo ;
}

template <typename PFP>
void EdgeSelector_NormalArea<PFP>::updateEdgeInfo(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
868 869
	MAP& m = this->m_map ;

870 871 872 873
	EdgeInfo& einfo = edgeInfo[d] ;

	if(einfo.valid)
		edges.erase(einfo.it) ;		// remove the edge from the multimap
874

875 876 877 878 879 880 881 882 883
	if(m.edgeCanCollapse(d))
		computeEdgeInfo(d, einfo) ;
	else
		einfo.valid = false ;
}

template <typename PFP>
void EdgeSelector_NormalArea<PFP>::computeEdgeInfo(Dart d, EdgeInfo& einfo)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
884 885
	MAP& m = this->m_map ;

886 887 888 889 890 891 892 893 894 895
	Dart dd = m.phi2(d);
	Geom::Matrix33f M1; // init zero included
	Geom::Matrix33f M2; // init zero included

	assert(! m.isBoundaryEdge(d));

	Traversor2VF<MAP> td (m,d);
	Dart it = td.begin();
	it = td.next();
	Dart it2 = td.next();
896
	while (it2 != td.end())
897 898 899 900 901 902 903 904 905 906
	{
		M1 += edgeMatrix[m.phi1(it)];
		it = it2;
		it2 = td.next();
	}

	Traversor2VF<MAP> tdd (m,dd);
	it = tdd.begin();
	it = tdd.next();
	it2 = tdd.next();
907
	while (it2 != tdd.end())
908 909 910 911 912 913
	{
		M2 += edgeMatrix[m.phi1(it)];
		it = it2;
		it2 = tdd.next();
	}

914 915
	m_positionApproximator.approximate(d) ;
	const VEC3& a = m_positionApproximator.getApprox(d) ;
916

917 918
	const VEC3 av1 = a - m_position[d] ;
	const VEC3 av2 = a - m_position[dd] ;
919 920 921

	REAL err = av1 * (M1 * av1) + av2 * (M2 * av2);

922 923 924 925 926 927 928
/*
	// TODO : test to normalize by area
	// TODO : not border-safe
 	REAL area = 0.0;
	Traversor2EEaV<MAP> ta (m,d);
	for (Dart dita = ta.begin(); dita != ta.end(); dita=ta.next())
	{
929
		area += Algo::Geometry::triangleArea<PFP>(m,dita,m_position);
930 931 932 933
	}
	err /= area ; // résultats sensiblment identiques à ceux sans pris en compte de l'aire.
//	err /= area*area ; // ca favorise la contraction des gros triangles : maillages très in-homogènes et qualité géométrique mauvaise
*/
934

935
	einfo.it = edges.insert(std::make_pair(err, d)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
936 937 938
	einfo.valid = true ;
}

939 940 941
template <typename PFP>
void EdgeSelector_NormalArea<PFP>::computeEdgeMatrix(Dart d)
{
942
	const VEC3 e = Algo::Surface::Geometry::vectorOutOfDart<PFP>(this->m_map, d, m_position) ;
943 944 945 946
	edgeMatrix[d].identity();
	edgeMatrix[d] *= e.norm2();
	edgeMatrix[d] -= Geom::transposed_vectors_mult(e,e) ;
	// TODO : test : try to normalize by area
Sauvage's avatar
Sauvage committed
947
//	edgeMatrix[d] /= e.norm2(); // pas d'amélioration significative par rapport à la version sans normalisation
948 949 950
//	edgeMatrix[d] /= e.norm2() * e.norm2(); // étonnament bon : sur certains maillages équivalant à la QEMml
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
951 952 953 954 955 956 957
/************************************************************************************
 *                                   CURVATURE                                      *
 ************************************************************************************/

template <typename PFP>
bool EdgeSelector_Curvature<PFP>::init()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
958
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
959

960 961
	assert(m_positionApproximator.getType() != A_hQEM || !"Approximator(hQEM) and selector (EdgeSelector_Curvature) are not compatible") ;
	assert(m_positionApproximator.getType() != A_hHalfCollapse || !"Approximator(hHalfCollapse) and selector (EdgeSelector_Curvature) are not compatible") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
962

963 964
	if(m_positionApproximator.getApproximatedAttributeName() != m_position.name())
	{
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
965
		return false ;
966
	}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
967 968 969

	edges.clear() ;

970
	for (Edge e : allEdgesOf(m))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
971
	{
972 973
		initEdgeInfo(e.dart) ;	// init the edges with their optimal position
	}							// and insert them in the multimap according to their error
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
974 975 976 977 978 979 980

	cur = edges.begin() ; // init the current edge to the first one

	return true ;
}

template <typename PFP>
981
bool EdgeSelector_Curvature<PFP>::nextEdge(Dart& d) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
982 983 984 985 986 987 988 989 990 991
{
	if(cur == edges.end() || edges.empty())
		return false ;
	d = (*cur).second ;
	return true ;
}

template <typename PFP>
void EdgeSelector_Curvature<PFP>::updateBeforeCollapse(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
992
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
993

994 995 996
	EdgeInfo *edgeE = &(edgeInfo[d]) ;
	if(edgeE->valid)
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
997

998 999 1000
	edgeE = &(edgeInfo[m.phi1(d)]) ;
	if(edgeE->valid)					// remove all
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1001

1002 1003 1004
	edgeE = &(edgeInfo[m.phi_1(d)]) ;	// the concerned edges
	if(edgeE->valid)
		edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1005 1006 1007 1008
									// from the multimap
	Dart dd = m.phi2(d) ;
	if(dd != d)
	{
1009 1010 1011
		edgeE = &(edgeInfo[m.phi1(dd)]) ;
		if(edgeE->valid)
			edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1012

1013 1014 1015
		edgeE = &(edgeInfo[m.phi_1(dd)]) ;
		if(edgeE->valid)
			edges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1016 1017 1018 1019 1020 1021
	}
}

template <typename PFP>
void EdgeSelector_Curvature<PFP>::updateAfterCollapse(Dart d2, Dart dd2)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1022
	MAP& m = this->m_map ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1023

1024
	normal[d2] = Algo::Surface::Geometry::vertexNormal<PFP>(m, d2, m_position) ;
1025
	Algo::Surface::Geometry::computeCurvatureVertex_NormalCycles<PFP>(m, d2, radius, m_position, normal, edgeangle, edgearea, kmax, kmin, Kmax, Kmin, Knormal) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1026 1027 1028 1029 1030

	Dart vit = d2 ;
	do
	{
		Dart nVert = m.phi1(vit) ;
1031
		normal[nVert] = Algo::Surface::Geometry::vertexNormal<PFP>(m, nVert, m_position) ;
1032
		Algo::Surface::Geometry::computeCurvatureVertex_NormalCycles<PFP>(m, nVert, radius, m_position, normal, edgeangle, edgearea, kmax, kmin, Kmax, Kmin, Knormal) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1033

1034 1035
		updateEdgeInfo(m.phi1(vit), false) ;		// must recompute some edge infos in the
		if(vit == d2 || vit == dd2)					// neighborhood of the collapsed edge
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1036
		{
1037 1038
			initEdgeInfo(vit) ;						// various optimizations are applied here by
													// treating differently :
1039
			Dart vit2 = m.phi12(m.phi1(vit)) ;		// - edges for which the criteria must be recomputed
1040 1041
			Dart stop = m.phi2(vit) ;				// - edges that must be re-embedded
			do										// - edges for which only the collapsibility must be re-tested
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1042 1043 1044
			{
				updateEdgeInfo(vit2, false) ;
				updateEdgeInfo(m.phi1(vit2), false) ;
1045
				vit2 = m.phi12(vit2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1046 1047 1048 1049 1050
			} while(vit2 != stop) ;
		}
		else
			updateEdgeInfo(vit, true) ;

1051
		vit = m.phi2_1(vit) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1052 1053 1054 1055 1056
	} while(vit != d2) ;

	cur = edges.begin() ; // set the current edge to the first one
}

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
template <typename PFP>
void EdgeSelector_Curvature<PFP>::updateWithoutCollapse()
{
	EdgeInfo& einfo = edgeInfo[(*cur).second] ;
	einfo.valid = false ;
	edges.erase(einfo.it) ;

	cur = edges.begin();
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1067 1068 1069
template <typename PFP>
void EdgeSelector_Curvature<PFP>::initEdgeInfo(Dart d)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1070 1071
	MAP& m = this->m_map ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
	EdgeInfo einfo ;
	if(m.edgeCanCollapse(d))
		computeEdgeInfo(d, einfo) ;
	else
		einfo.valid = false ;
	edgeInfo[d] = einfo ;
}

template <typename PFP>
void EdgeSelector_Curvature<PFP>::updateEdgeInfo(Dart d, bool recompute)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1083 1084
	MAP& m = this->m_map ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	EdgeInfo& einfo = edgeInfo[d] ;
	if(recompute)
	{
		if(einfo.valid)
			edges.erase(einfo.it) ;			// remove the edge from the multimap
		if(m.edgeCanCollapse(d))
			computeEdgeInfo(d, einfo) ;
		else
			einfo.valid = false ;
	}
	else
	{
		if(m.edgeCanCollapse(d))
		{									// if the edge can be collapsed now
			if(!einfo.valid)				// but it was not before
				computeEdgeInfo(d, einfo) ;
		}
		else
		{									// if the edge cannot be collapsed now
			if(einfo.valid)					// and it was before
			{
				edges.erase(einfo.it) ;
				einfo.valid = false ;
			}
		}
	}
}

template <typename PFP>
void EdgeSelector_Curvature<PFP>::computeEdgeInfo(Dart d, EdgeInfo& einfo)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1116 1117
	MAP& m = this->m_map ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1118 1119
	Dart dd = m.phi2(d) ;

1120 1121
	unsigned int v1 = m.template getEmbedding<VERTEX>(d) ;
	unsigned int v2 = m.template getEmbedding<VERTEX>(dd) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1122

1123
	m_positionApproximator.approximate(d) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1124 1125 1126 1127 1128

	// temporary edge collapse
	Dart d2 = m.phi2(m.phi_1(d)) ;
	Dart dd2 = m.phi2(m.phi_1(dd)) ;
	m.extractTrianglePair(d) ;
1129 1130
	unsigned int newV = Algo::Topo::setOrbitEmbeddingOnNewCell<VERTEX>(m, d2);
	m_position[newV] = m_positionApproximator.getApprox(d) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1131 1132

	// compute things on the coarse version of the mesh
1133
	normal[newV] = Algo::Surface::Geometry::vertexNormal<PFP>(m, d2, m_position) ;
1134
	Algo::Surface::Geometry::computeCurvatureVertex_NormalCycles<PFP>(m, d2, radius, m_position, normal, edgeangle, edgearea, kmax, kmin, Kmax, Kmin, Knormal) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1135

1136 1137 1138
//	VEC3 norm = normal[newV] ;
	REAL mCurv = (kmax[newV] + kmin[newV]) / REAL(2) ;
//	VEC3 cDir1 = Kmax[newV] ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1139 1140 1141

	// vertex split to reset the initial connectivity and embeddings
	m.insertTrianglePair(d, d2, dd2) ;
1142 1143
	Algo::Topo::setOrbitEmbedding<VERTEX>(m, d, v1);
	Algo::Topo::setOrbitEmbedding<VERTEX>(m, dd, v2);
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1144 1145 1146

	REAL err = 0 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
1147 1148
//	REAL norm_deviation_1 = REAL(1) / fabs(norm * normal[v1]) ;
//	REAL norm_deviation_2 = REAL(1) / fabs(norm * normal[v2]) ;
1149
//	err += norm_deviation_1 + norm_deviation_2 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1150

Pierre Kraemer's avatar
Pierre Kraemer committed
1151 1152
	REAL mCurv_deviation_1 = fabs(mCurv - (kmax[v1] + kmin[v1] / REAL(2))) ;
	REAL mCurv_deviation_2 = fabs(mCurv - (kmax[v2] + kmin[v2] / REAL(2))) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1153 1154
	err += mCurv_deviation_1 + mCurv_deviation_2 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
1155 1156
//	REAL cDir1_deviation_1 = REAL(1) / fabs(cDir1 * Kmax[v1]) ;
//	REAL cDir1_deviation_2 = REAL(1) / fabs(cDir1 * Kmax[v2]) ;
1157
//	err += cDir1_deviation_1 + cDir1_deviation_2 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1158 1159 1160 1161 1162

	einfo.it = edges.insert(std::make_pair(err, d)) ;
	einfo.valid = true ;
}

1163 1164 1165 1166 1167
/************************************************************************************
 *                            CURVATURE TENSOR                                      *
 ************************************************************************************/

template <