halfEdgeSelector.hpp 25.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
24
25
26
27
28
29
30
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

31
32
33
namespace Surface
{

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
34
35
36
namespace Decimation
{

37
38
39
/*****************************************************************************************************************
 *                                 HALF-EDGE MEMORYLESS QEM METRIC                                               *
 *****************************************************************************************************************/
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
40
41
42
43
44
45
46
47
48
49
50
51
52

template <typename PFP>
bool HalfEdgeSelector_QEMml<PFP>::init()
{
	MAP& m = this->m_map ;

	bool ok = false ;
	for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = this->m_approximators.begin();
		it != this->m_approximators.end() && !ok;
		++it)
	{
		if((*it)->getApproximatedAttributeName() == "position")
		{
53
			m_positionApproximator = reinterpret_cast<Approximator<PFP, VEC3,DART>* >(*it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
54
55
56
57
58
59
60
			ok = true ;
		}
	}

	if(!ok)
		return false ;

61
	CellMarker<VERTEX> vMark(m) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
62
63
64
65
	for(Dart d = m.begin(); d != m.end(); m.next(d))
	{
		if(!vMark.isMarked(d))
		{
66
			Utils::Quadric<REAL> q ;	// create one quadric
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
67
68
69
70
71
72
73
74
75
76
77
78
			quadric[d] = q ;	// per vertex
			vMark.mark(d) ;
		}
	}

	DartMarker mark(m) ;
	for(Dart d = m.begin(); d != m.end(); m.next(d))
	{
		if(!mark.isMarked(d))
		{
			Dart d1 = m.phi1(d) ;				// for each triangle,
			Dart d_1 = m.phi_1(d) ;				// initialize the quadric of the triangle
79
			Utils::Quadric<REAL> q(this->m_position[d], this->m_position[d1], this->m_position[d_1]) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
80
81
82
			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
Pierre Kraemer's avatar
Pierre Kraemer committed
83
			mark.markOrbit<FACE>(d) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
		}
	}

	// Init multimap for each Half-edge
	halfEdges.clear() ;

	for(Dart d = m.begin(); d != m.end(); m.next(d))
	{
		initHalfEdgeInfo(d) ;	// init the edges with their optimal info
	}							// and insert them in the multimap according to their error

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

	return true ;
}

template <typename PFP>
101
bool HalfEdgeSelector_QEMml<PFP>::nextEdge(Dart& d) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
102
103
104
105
106
107
108
109
110
111
112
113
{
	if(cur == halfEdges.end() || halfEdges.empty())
		return false ;
	d = (*cur).second ;
	return true ;
}

template <typename PFP>
void HalfEdgeSelector_QEMml<PFP>::updateBeforeCollapse(Dart d)
{
	MAP& m = this->m_map ;

114
115
116
	HalfEdgeInfo* edgeE = &(halfEdgeInfo[d]) ;
	if(edgeE->valid)
		halfEdges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
117

118
119
120
	edgeE = &(halfEdgeInfo[m.phi1(d)]) ;
	if(edgeE->valid)						// remove all
		halfEdges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
121

122
123
124
	edgeE = &(halfEdgeInfo[m.phi_1(d)]) ;	// the halfedges that will disappear
	if(edgeE->valid)
		halfEdges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
125
126
										// from the multimap
	Dart dd = m.phi2(d) ;
127
	assert(dd != d) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
128
129
	if(dd != d)
	{
130
131
132
		edgeE = &(halfEdgeInfo[dd]) ;
		if(edgeE->valid)
			halfEdges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
133

134
135
136
		edgeE = &(halfEdgeInfo[m.phi1(dd)]) ;
		if(edgeE->valid)
			halfEdges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
137

138
139
140
		edgeE = &(halfEdgeInfo[m.phi_1(dd)]) ;
		if(edgeE->valid)
			halfEdges.erase(edgeE->it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
	}
}

template <typename PFP>
void HalfEdgeSelector_QEMml<PFP>::recomputeQuadric(const Dart d, const bool recomputeNeighbors) {
	Dart dFront,dBack ;
	Dart dInit = d ;

	// Init Front
	dFront = dInit ;

	quadric[d].zero() ;

   	do {
   		// Make step
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
156
   		dBack = this->m_map.phi1(dFront) ;
157
       	dFront = this->m_map.phi2_1(dFront) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
158

159
       	if (this->m_map.phi2(dFront) != dFront) { // if dFront is no border
160
           	quadric[d] += Utils::Quadric<REAL>(this->m_position[d],this->m_position[dBack],this->m_position[this->m_map.phi1(dFront)]) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
161
       	}
162

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
163
164
165
166
167
168
169
       	if (recomputeNeighbors)
       		recomputeQuadric(dBack, false) ;

    } while(dFront != dInit) ;
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
170
void HalfEdgeSelector_QEMml<PFP>::updateAfterCollapse(Dart d2, Dart /*dd2*/)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
171
172
173
174
175
176
177
178
179
{
	MAP& m = this->m_map ;

	recomputeQuadric(d2, true) ;

	Dart vit = d2 ;
	do
	{
		updateHalfEdgeInfo(vit, true) ;
180
		Dart d = m.phi2(vit) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
181
		if (d != vit)
182
183
			updateHalfEdgeInfo(d, true) ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
184
		updateHalfEdgeInfo(m.phi1(vit), true) ;
185
		d = m.phi2(m.phi1(vit)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
186
		if (d != m.phi1(vit))
187
			updateHalfEdgeInfo(d, true) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
188
189

		Dart stop = m.phi2(vit) ;
190
		assert (stop != vit) ;
191
		Dart vit2 = m.phi12(m.phi1(vit)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
192
193
		do {
			updateHalfEdgeInfo(vit2, true) ;
194
			d = m.phi2(vit2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
195
			if (d != vit2)
196
197
				updateHalfEdgeInfo(d, true) ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
198
			updateHalfEdgeInfo(m.phi1(vit2), false) ;
199
			d = m.phi2(m.phi1(vit2)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
200
			if (d != m.phi1(vit2))
201
				updateHalfEdgeInfo(d, false) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
202

203
			vit2 = m.phi12(vit2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
204
		} while (stop != vit2) ;
205
		vit = m.phi2_1(vit) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
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
	} while(vit != d2) ;

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

template <typename PFP>
void HalfEdgeSelector_QEMml<PFP>::initHalfEdgeInfo(Dart d)
{
	MAP& m = this->m_map ;
	HalfEdgeInfo heinfo ;
	if(m.edgeCanCollapse(d))
		computeHalfEdgeInfo(d, heinfo) ;
	else
		heinfo.valid = false ;

	halfEdgeInfo[d] = heinfo ;
}

template <typename PFP>
void HalfEdgeSelector_QEMml<PFP>::updateHalfEdgeInfo(Dart d, bool recompute)
{
	MAP& m = this->m_map ;
	HalfEdgeInfo& heinfo = halfEdgeInfo[d] ;
	if(recompute)
	{
		if(heinfo.valid)
			halfEdges.erase(heinfo.it) ;			// remove the edge from the multimap
		if(m.edgeCanCollapse(d))
			computeHalfEdgeInfo(d, heinfo) ;
		else
			heinfo.valid = false ;
	}
	else
	{
		if(m.edgeCanCollapse(d))
		{								 	// if the edge can be collapsed now
			if(!heinfo.valid)				 // but it was not before
				computeHalfEdgeInfo(d, heinfo) ;
		}
		else
		{								 // if the edge cannot be collapsed now
			if(heinfo.valid)				 // and it was before
			{
				halfEdges.erase(heinfo.it) ;
				heinfo.valid = false ;
			}
		}
	}
}

template <typename PFP>
void HalfEdgeSelector_QEMml<PFP>::computeHalfEdgeInfo(Dart d, HalfEdgeInfo& heinfo)
{
	MAP& m = this->m_map ;
260
	Dart dd = m.phi1(d) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
261

262
	Utils::Quadric<REAL> quad ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
263
264
265
266
267
268
269
270
271
272
	quad += quadric[d] ;	// compute the sum of the
	quad += quadric[dd] ;	// two vertices quadrics

	m_positionApproximator->approximate(d) ;

	REAL err = quad(m_positionApproximator->getApprox(d)) ;
	heinfo.it = halfEdges.insert(std::make_pair(err, d)) ;
	heinfo.valid = true ;
}

273
274
275
/*****************************************************************************************************************
 *                                 HALF-EDGE QEMextColor METRIC                                                  *
 *****************************************************************************************************************/
276
277
278
279
280
281
282
283
284
285

template <typename PFP>
bool HalfEdgeSelector_QEMextColor<PFP>::init()
{
	MAP& m = this->m_map ;

	// Verify availability of required approximators
	unsigned int ok = 0 ;
	for (unsigned int approxindex = 0 ; approxindex < this->m_approximators.size() ; ++approxindex)
	{
286
287
288
289
		assert(this->m_approximators[approxindex]->getType() == A_hQEM
				|| this->m_approximators[approxindex]->getType() == A_hHalfCollapse
				|| !"Approximator for selector (HalfEdgeSelector_QEMextColor) must be of a half-edge approximator") ;

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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
		bool saved = false ;
		for (unsigned int attrindex = 0 ; attrindex < this->m_approximators[approxindex]->getNbApproximated() ; ++ attrindex)
		{
			// constraint : 2 approximators in specific order
			if(ok == 0 && this->m_approximators[approxindex]->getApproximatedAttributeName(attrindex) == "position")
			{
				++ok ;
				m_approxindex_pos = approxindex ;
				m_attrindex_pos = attrindex ;
				m_pos = this->m_position ;
				if (!saved)
				{
					m_approx.push_back(reinterpret_cast<Approximator<PFP, VEC3, DART>* >(this->m_approximators[approxindex])) ;
					saved = true ;
				}
			}
			else if(ok == 1 && this->m_approximators[approxindex]->getApproximatedAttributeName(attrindex) == "color")
			{
				++ok ;
				m_approxindex_color = approxindex ;
				m_attrindex_color = attrindex ;
				m_color = m.template getAttribute<typename PFP::VEC3, VERTEX>("color") ;
				assert(m_color.isValid() || !"EdgeSelector_QEMextColor: color attribute is not valid") ;
				if (!saved)
				{
					m_approx.push_back(reinterpret_cast<Approximator<PFP, VEC3, DART>* >(this->m_approximators[approxindex])) ;
					saved = true ;
				}
			}
		}
	}

	if(ok != 2)
		return false ;

	TraversorV<MAP> travV(m);
	for(Dart dit = travV.begin() ; dit != travV.end() ; dit = travV.next())
	{
		Utils::QuadricNd<REAL,6> q ;		// create one quadric
		m_quadric[dit] = q ;		// per vertex
	}

	// Compute quadric per vertex
	TraversorF<MAP> travF(m) ;
	for(Dart dit = travF.begin() ; dit != travF.end() ; dit = travF.next()) // init QEM quadrics
	{
		Dart d1 = m.phi1(dit) ;					// for each triangle,
		Dart d_1 = m.phi_1(dit) ;					// initialize the quadric of the triangle

   		VEC6 p0, p1, p2 ;
   		for (unsigned int i = 0 ; i < 3 ; ++i)
   		{
   			p0[i] = this->m_position[dit][i] ;
   			p0[i+3] = this->m_color[dit][i] ;
   			p1[i] = this->m_position[d1][i] ;
   			p1[i+3] = this->m_color[d1][i] ;
   			p2[i] = this->m_position[d_1][i] ;
   			p2[i+3] = this->m_color[d_1][i] ;
   		}
		Utils::QuadricNd<REAL,6> q(p0,p1,p2) ;
		m_quadric[dit] += q ;						// and add the contribution of
		m_quadric[d1] += q ;						// this quadric to the ones
		m_quadric[d_1] += q ;						// of the 3 incident vertices
	}


	// Init multimap for each Half-edge
	halfEdges.clear() ;

	for(Dart d = m.begin(); d != m.end(); m.next(d))
	{
		initHalfEdgeInfo(d) ;	// init the edges with their optimal info
	}							// and insert them in the multimap according to their error

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

	return true ;
}

template <typename PFP>
370
bool HalfEdgeSelector_QEMextColor<PFP>::nextEdge(Dart& d) const
371
372
373
374
375
376
377
378
379
380
381
382
{
	if(cur == halfEdges.end() || halfEdges.empty())
		return false ;
	d = (*cur).second ;
	return true ;
}

template <typename PFP>
void HalfEdgeSelector_QEMextColor<PFP>::updateBeforeCollapse(Dart d)
{
	MAP& m = this->m_map ;

383
384
385
	HalfEdgeInfo* edgeE = &(halfEdgeInfo[d]) ;
	if(edgeE->valid)
		halfEdges.erase(edgeE->it) ;
386

387
388
389
	edgeE = &(halfEdgeInfo[m.phi1(d)]) ;
	if(edgeE->valid)						// remove all
		halfEdges.erase(edgeE->it) ;
390

391
392
393
	edgeE = &(halfEdgeInfo[m.phi_1(d)]) ;	// the halfedges that will disappear
	if(edgeE->valid)
		halfEdges.erase(edgeE->it) ;
394
395
396
397
398
										// from the multimap
	Dart dd = m.phi2(d) ;
	assert(dd != d) ;
	if(dd != d)
	{
399
400
401
		edgeE = &(halfEdgeInfo[dd]) ;
		if(edgeE->valid)
			halfEdges.erase(edgeE->it) ;
402

403
404
405
		edgeE = &(halfEdgeInfo[m.phi1(dd)]) ;
		if(edgeE->valid)
			halfEdges.erase(edgeE->it) ;
406

407
408
409
		edgeE = &(halfEdgeInfo[m.phi_1(dd)]) ;
		if(edgeE->valid)
			halfEdges.erase(edgeE->it) ;
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
	}
}

template <typename PFP>
void HalfEdgeSelector_QEMextColor<PFP>::recomputeQuadric(const Dart d, const bool recomputeNeighbors) {
	Dart dFront,dBack ;
	Dart dInit = d ;

	// Init Front
	dFront = dInit ;

	m_quadric[d].zero() ;

 	do
   	{
   		// Make step
   		dBack = this->m_map.phi2(dFront) ;
       	dFront = this->m_map.phi2_1(dFront) ;

       	if (dBack != dFront)
       	{ // if dFront is no border
   			Dart d2 = this->m_map.phi2(dFront) ;

       		VEC6 p0, p1, p2 ;
       		for (unsigned int i = 0 ; i < 3 ; ++i)
       		{

       			p0[i] = this->m_position[d][i] ;
       			p0[i+3] = this->m_color[d][i] ;
       			p1[i] = this->m_position[d2][i] ;
       			p1[i+3] = this->m_color[d2][i] ;
       			p2[i] = this->m_position[dBack][i] ;
       			p2[i+3] = this->m_color[dBack][i] ;
       		}
       		m_quadric[d] += Utils::QuadricNd<REAL,6>(p0,p1,p2) ;
       	}
       	if (recomputeNeighbors)
       		recomputeQuadric(dBack, false) ;

    } while(dFront != dInit) ;
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
453
void HalfEdgeSelector_QEMextColor<PFP>::updateAfterCollapse(Dart d2, Dart /*dd2*/)
454
455
456
457
458
459
460
461
462
463
{
	MAP& m = this->m_map ;

	recomputeQuadric(d2, true) ;

	Dart vit = d2 ;
	do
	{
		updateHalfEdgeInfo(vit, true) ;
		Dart d = m.phi2(vit) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
464
		if (d != vit)
465
466
467
468
			updateHalfEdgeInfo(d, true) ;

		updateHalfEdgeInfo(m.phi1(vit), true) ;
		d = m.phi2(m.phi1(vit)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
469
		if (d != m.phi1(vit))
470
471
472
473
474
475
476
477
			updateHalfEdgeInfo(d, true) ;

		Dart stop = m.phi2(vit) ;
		assert (stop != vit) ;
		Dart vit2 = m.phi12(m.phi1(vit)) ;
		do {
			updateHalfEdgeInfo(vit2, true) ;
			d = m.phi2(vit2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
478
			if (d != vit2)
479
480
481
482
				updateHalfEdgeInfo(d, true) ;

			updateHalfEdgeInfo(m.phi1(vit2), false) ;
			d = m.phi2(m.phi1(vit2)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
483
			if (d != m.phi1(vit2))
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
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
				updateHalfEdgeInfo(d, false) ;

			vit2 = m.phi12(vit2) ;
		} while (stop != vit2) ;
		vit = m.phi2_1(vit) ;
	} while(vit != d2) ;

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

template <typename PFP>
void HalfEdgeSelector_QEMextColor<PFP>::initHalfEdgeInfo(Dart d)
{
	MAP& m = this->m_map ;
	HalfEdgeInfo heinfo ;
	if(m.edgeCanCollapse(d))
		computeHalfEdgeInfo(d, heinfo) ;
	else
		heinfo.valid = false ;

	halfEdgeInfo[d] = heinfo ;
}

template <typename PFP>
void HalfEdgeSelector_QEMextColor<PFP>::updateHalfEdgeInfo(Dart d, bool recompute)
{
	MAP& m = this->m_map ;
	HalfEdgeInfo& heinfo = halfEdgeInfo[d] ;
	if(recompute)
	{
		if(heinfo.valid)
			halfEdges.erase(heinfo.it) ;			// remove the edge from the multimap
		if(m.edgeCanCollapse(d))
			computeHalfEdgeInfo(d, heinfo) ;
		else
			heinfo.valid = false ;
	}
	else
	{
		if(m.edgeCanCollapse(d))
		{								 	// if the edge can be collapsed now
			if(!heinfo.valid)				 // but it was not before
				computeHalfEdgeInfo(d, heinfo) ;
		}
		else
		{								 // if the edge cannot be collapsed now
			if(heinfo.valid)				 // and it was before
			{
				halfEdges.erase(heinfo.it) ;
				heinfo.valid = false ;
			}
		}
	}
}

template <typename PFP>
void HalfEdgeSelector_QEMextColor<PFP>::computeHalfEdgeInfo(Dart d, HalfEdgeInfo& heinfo)
{
	MAP& m = this->m_map ;
	Dart dd = m.phi1(d) ;

	// New position
	Utils::QuadricNd<REAL,6> quad ;
	quad += m_quadric[d] ;	// compute the sum of the
	quad += m_quadric[dd] ;	// two vertices quadrics

	// compute all approximated attributes
	for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = this->m_approximators.begin() ;
			it != this->m_approximators.end() ;
			++it)
	{
		(*it)->approximate(d) ;
	}

	// get pos
	const VEC3& newPos = this->m_approx[m_approxindex_pos]->getApprox(d,m_attrindex_pos) ; // get newPos
	// get col
	const VEC3& newCol = this->m_approx[m_approxindex_color]->getApprox(d,m_attrindex_color) ; // get newCol

	// compute error
	VEC6 newEmb ;
	for (unsigned int i = 0 ; i < 3 ; ++i)
	{
		newEmb[i] = newPos[i] ;
		newEmb[i+3] = newCol[i] ;
	}

	const REAL& err = quad(newEmb) ;

	// Check if errated values appear
	if (err < -1e-6)
		heinfo.valid = false ;
	else
	{
		heinfo.it = this->halfEdges.insert(std::make_pair(std::max(err,REAL(0)), d)) ;
		heinfo.valid = true ;
	}
}

583
584
585
/*****************************************************************************************************************
 *                                 HALF-EDGE COLOR GRADIENT ERR                                                  *
 *****************************************************************************************************************/
586
587

template <typename PFP>
588
bool HalfEdgeSelector_ColorGradient<PFP>::init()
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
589
590
591
592
593
594
595
596
{
	MAP& m = this->m_map ;


	// Verify availability of required approximators
	unsigned int ok = 0 ;
	for (unsigned int approxindex = 0 ; approxindex < this->m_approximators.size() ; ++approxindex)
	{
597
598
599
		assert(this->m_approximators[approxindex]->getType() == A_hQEM
				|| this->m_approximators[approxindex]->getType() == A_hHalfCollapse
				|| !"Approximator for selector (HalfEdgeSelector_ColorExperimental) must be of a half-edge approximator") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
600
601
602
603
604
605
606
607
608
609

		bool saved = false ;
		for (unsigned int attrindex = 0 ; attrindex < this->m_approximators[approxindex]->getNbApproximated() ; ++ attrindex)
		{
			// constraint : 2 approximators in specific order
			if(ok == 0 && this->m_approximators[approxindex]->getApproximatedAttributeName(attrindex) == "position")
			{
				++ok ;
				m_approxindex_pos = approxindex ;
				m_attrindex_pos = attrindex ;
610
				m_pos = this->m_position ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
611
612
613
614
615
616
				if (!saved)
				{
					m_approx.push_back(reinterpret_cast<Approximator<PFP, VEC3, DART>* >(this->m_approximators[approxindex])) ;
					saved = true ;
				}
			}
617
			else if(ok == 1 && this->m_approximators[approxindex]->getApproximatedAttributeName(attrindex) == "color")
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
618
619
			{
				++ok ;
620
621
622
623
				m_approxindex_color = approxindex ;
				m_attrindex_color = attrindex ;
				m_color = m.template getAttribute<typename PFP::VEC3, VERTEX>("color") ;
				assert(m_color.isValid() || !"EdgeSelector_QEMextColor: color attribute is not valid") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
624
625
626
627
628
629
630
631
632
				if (!saved)
				{
					m_approx.push_back(reinterpret_cast<Approximator<PFP, VEC3, DART>* >(this->m_approximators[approxindex])) ;
					saved = true ;
				}
			}
		}
	}

633
634
	if(ok != 2)
			return false ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
635

636
637
	CellMarker<VERTEX> vMark(m) ;
	for(Dart d = m.begin(); d != m.end(); m.next(d))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
638
	{
639
640
641
642
643
644
		if(!vMark.isMarked(d))
		{
			Utils::Quadric<REAL> q ;	// create one quadric
			m_quadric[d] = q ;	// per vertex
			vMark.mark(d) ;
		}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
645
646
	}

647
648
	DartMarker mark(m) ;
	for(Dart d = m.begin(); d != m.end(); m.next(d))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
649
	{
650
651
652
653
654
655
656
657
658
659
		if(!mark.isMarked(d))
		{
			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(this->m_position[d], this->m_position[d1], this->m_position[d_1]) ;
			m_quadric[d] += q ;					// and add the contribution of
			m_quadric[d1] += q ;					// this quadric to the ones
			m_quadric[d_1] += q ;					// of the 3 incident vertices
			mark.markOrbit<FACE>(d) ;
		}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
660
661
662
663
664
	}

	// Init multimap for each Half-edge
	halfEdges.clear() ;

665
	for(Dart d = m.begin(); d != m.end(); m.next(d))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
666
	{
667
668
		initHalfEdgeInfo(d) ;	// init the edges with their optimal info
	}							// and insert them in the multimap according to their error
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
669
670
671
672
673
674
675

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

	return true ;
}

template <typename PFP>
676
bool HalfEdgeSelector_ColorGradient<PFP>::nextEdge(Dart& d) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
677
678
679
680
681
682
683
684
{
	if(cur == halfEdges.end() || halfEdges.empty())
		return false ;
	d = (*cur).second ;
	return true ;
}

template <typename PFP>
685
void HalfEdgeSelector_ColorGradient<PFP>::updateBeforeCollapse(Dart d)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
{
	MAP& m = this->m_map ;

	const Dart& v0 = m.phi1(d) ;

	Traversor2VVaE<MAP> tv(m,v0) ;
	for (Dart v = tv.begin() ; v != tv.end() ; v = tv.next())
	{
		Traversor2VE<MAP> te(m,v) ;
		for (Dart he = te.begin() ; he != te.end() ; he = te.next())
		{
			HalfEdgeInfo* edgeE = &(halfEdgeInfo[he]) ;
			if(edgeE->valid)
			{
				edgeE->valid = false ;
				halfEdges.erase(edgeE->it) ;
			}
			Dart de = m.phi2(he) ;
			edgeE = &(halfEdgeInfo[de]) ;
			if(edgeE->valid)
			{
				edgeE->valid = false ;
				halfEdges.erase(edgeE->it) ;
			}
		}
	}
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

//	HalfEdgeInfo* edgeE = &(halfEdgeInfo[d]) ;
//	if(edgeE->valid)
//		halfEdges.erase(edgeE->it) ;
//
//	edgeE = &(halfEdgeInfo[m.phi1(d)]) ;
//	if(edgeE->valid)						// remove all
//		halfEdges.erase(edgeE->it) ;
//
//	edgeE = &(halfEdgeInfo[m.phi_1(d)]) ;	// the halfedges that will disappear
//	if(edgeE->valid)
//		halfEdges.erase(edgeE->it) ;
//										// from the multimap
//	Dart dd = m.phi2(d) ;
//	assert(dd != d) ;
//	if(dd != d)
//	{
//		edgeE = &(halfEdgeInfo[dd]) ;
//		if(edgeE->valid)
//			halfEdges.erase(edgeE->it) ;
//
//		edgeE = &(halfEdgeInfo[m.phi1(dd)]) ;
//		if(edgeE->valid)
//			halfEdges.erase(edgeE->it) ;
//
//		edgeE = &(halfEdgeInfo[m.phi_1(dd)]) ;
//		if(edgeE->valid)
//			halfEdges.erase(edgeE->it) ;
//	}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
741
742
743
}

template <typename PFP>
744
void HalfEdgeSelector_ColorGradient<PFP>::recomputeQuadric(const Dart d)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
{
	Dart dFront,dBack ;
	Dart dInit = d ;

	// Init Front
	dFront = dInit ;

	m_quadric[d].zero() ;

	do {
		// Make step
		dBack = this->m_map.phi1(dFront) ;
		dFront = this->m_map.phi2_1(dFront) ;

		if (this->m_map.phi2(dFront) != dFront) { // if dFront is no border
			m_quadric[d] += Utils::Quadric<REAL>(this->m_position[d],this->m_position[dBack],this->m_position[this->m_map.phi1(dFront)]) ;
		}
	} while(dFront != dInit) ;
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
766
void HalfEdgeSelector_ColorGradient<PFP>::updateAfterCollapse(Dart d2, Dart /*dd2*/)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
{
	MAP& m = this->m_map ;

	const Dart& v1 = d2 ;

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

	for (Dart v = tv.begin() ; v != tv.end() ; v = tv.next())
	{
		Traversor2VE<MAP> te(m,v) ;
		for (Dart e = te.begin() ; e != te.end() ; e = te.next())
		{
			updateHalfEdgeInfo(e) ;
			updateHalfEdgeInfo(m.phi2(e)) ;
		}
	}

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

template <typename PFP>
793
void HalfEdgeSelector_ColorGradient<PFP>::initHalfEdgeInfo(Dart d)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
794
795
796
{
	MAP& m = this->m_map ;
	HalfEdgeInfo heinfo ;
797

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
798
799
800
801
802
803
804
805
806
	if(m.edgeCanCollapse(d))
		computeHalfEdgeInfo(d, heinfo) ;
	else
		heinfo.valid = false ;

	halfEdgeInfo[d] = heinfo ;
}

template <typename PFP>
807
void HalfEdgeSelector_ColorGradient<PFP>::updateHalfEdgeInfo(Dart d)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
808
809
810
811
812
813
814
815
816
{
	MAP& m = this->m_map ;
	HalfEdgeInfo& heinfo = halfEdgeInfo[d] ;

	if(!heinfo.valid && m.edgeCanCollapse(d))
		computeHalfEdgeInfo(d, heinfo) ;
}

template <typename PFP>
817
void HalfEdgeSelector_ColorGradient<PFP>::computeHalfEdgeInfo(Dart d, HalfEdgeInfo& heinfo)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
{
	MAP& m = this->m_map ;
	Dart dd = m.phi1(d) ;

	Utils::Quadric<REAL> quad ;
	quad += m_quadric[d] ;	// compute the sum of the
	quad += m_quadric[dd] ;	// two vertices quadrics


	// compute all approximated attributes
	for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = this->m_approximators.begin() ;
			it != this->m_approximators.end() ;
			++it)
	{
		(*it)->approximate(d) ;
	}

	// Get all approximated attributes
	// New position
	const VEC3& newPos = this->m_approx[m_approxindex_pos]->getApprox(d,m_attrindex_pos) ; // get newPos
838
839
	// New normal
//	const VEC3& newColor = this->m_approx[m_approxindex_color]->getApprox(d,m_attrindex_color) ; // get new color
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
840
841
842
843

	const Dart& v0 = dd ;
	const Dart& v1 = d ;

844
845
	assert(newPos == m_pos[v1]) ;
	assert( this->m_approx[m_approxindex_color]->getApprox(d,m_attrindex_color) == m_color[v1]) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
846
847
848
849
850
851
852

	// Compute errors
	// Position
	Utils::Quadric<REAL> quadGeom ;
	quadGeom += m_quadric[d] ;	// compute the sum of the
	quadGeom += m_quadric[dd] ;	// two vertices quadrics

853
854
855
	//std::cout << quadGeom(newPos) / (alpha/M_PI + quadHF(newHF)) << std::endl ;
	// sum of QEM metric and color gradient metric
	const REAL t = 0.01 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
856
	const REAL& err =
857
858
			t*quadGeom(newPos) + // geom
			(1-t)*computeGradientColorError(v0,v1).norm()/sqrt(3) // color
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
859
			;
860
861
862

	/*std::cout << quadGeom(newPos) << std::endl ;
	std::cerr << computeGradientColorError(v0,v1).norm()/sqrt(3) << std::endl ;*/
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
863
864
865
866
867
868
869
870
871
872
873
874

	// Check if errated values appear
	if (err < -1e-6)
		heinfo.valid = false ;
	else
	{
		heinfo.it = this->halfEdges.insert(std::make_pair(std::max(err,REAL(0)), d)) ;
		heinfo.valid = true ;
	}
}

template <typename PFP>
875
876
typename PFP::VEC3
HalfEdgeSelector_ColorGradient<PFP>::computeGradientColorError(const Dart& v0, const Dart& v1) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
877
878
879
{
	MAP& m = this->m_map ;

880
	Traversor2VF<MAP> tf(m,v0) ; // all faces around vertex v0
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
881

882
883
884
885
886
	const VEC3& P0 = m_pos[v0] ;
	const VEC3& P1 = m_pos[v1] ;
	const VEC3& c0 = m_color[v0] ;
	const VEC3& c1 = m_color[v1] ;
	const VEC3 d = P1 - P0 ; // displacement vector
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
887

888
889
890
	VEC3 count ;
	REAL areaSum = 0 ;
	for (Dart fi = tf.begin() ; fi != tf.end() ; fi = tf.next()) // foreach "blue" face
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
891
	{
892
		// get the data
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
893
894
895
896
		const Dart& vi = m.phi1(fi) ;
		const Dart& vj = m.phi_1(fi) ;
		const VEC3& Pi = this->m_position[vi] ;
		const VEC3& Pj = this->m_position[vj] ;
897
898
		const VEC3& ci = m_color[vi] ;
		const VEC3& cj = m_color[vj] ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
899

900
901
902
903
		// utils
		const VEC3 ei = P0 - Pj ;
		const VEC3 ej = Pi - P0 ;
		//const VEC3 e0 = Pj - Pi ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
904

905
906
907
908
909
		const REAL areaIJ0sq = (ei ^ ej).norm2() ;
		const REAL areaIJ0 = sqrt(areaIJ0sq)/2. ;
		areaSum += areaIJ0 ;
		// per-channel treatment
		for (unsigned int c = 0 ; c < 3 ;  ++c)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
910
		{
911
912
913
914
915
916
917
918
			// color gradient for channel i
			VEC3 grad = (ei.norm2()*(ci[c]-c1[c]) + (ei*ej)*(cj[c]-c1[c]))*ej ;
			grad -= (ej.norm2()*(cj[c]-c1[c]) + (ei*ej)*(ci[c]-c1[c]))*ei ;
			const REAL denom = areaIJ0sq ;
			if (denom < 1e-9) // case flat triangles
				grad = VEC3(0,0,0) ;
			else
				grad /= denom ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
919

920
921
			// color change error for channel i
			count[c] += areaIJ0 * pow(d*grad,2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
922
923
924
		}
	}

925
926
	const VEC3 colDiff = c1 - c0 ;
	for (unsigned int c = 0 ; c < 3 ; ++c)
927
	{
928
		count[c] += pow(colDiff[c],2) * areaSum ;
929
930
	}

931
	return count ;
932
933
934
935
936
937
}

} // namespace Decimation

} // namespace Surface

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
938
939
940
} // namespace Algo

} // namespace CGoGN