geometryApproximator.hpp 13.3 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

namespace Decimation
{

/************************************************************************************
 *                            QUADRIC ERROR METRIC                                  *
 ************************************************************************************/
template <typename PFP>
bool Approximator_QEM<PFP>::init()
{
40
	m_quadric = this->m_map.template getAttribute<Utils::Quadric<REAL>, VERTEX>("QEMquadric") ;
41
	// Does not require to be valid (if it is not, altenatives will be used).
Pierre Kraemer's avatar
Pierre Kraemer committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

	if(this->m_predictor)
	{
		return false ;
	}
	return true ;
}

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

	// get some darts
	Dart dd = m.phi2(d) ;

58
	Utils::Quadric<REAL> q1, q2 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
59
60
61
62
63
64
	if(!m_quadric.isValid()) // if the selector is not QEM, compute local error quadrics
	{
		// compute the error quadric associated to v1
		Dart it = d ;
		do
		{
65
			Utils::Quadric<REAL> q(this->m_attrV[0]->operator[](it), this->m_attrV[0]->operator[](m.phi1(it)), this->m_attrV[0]->operator[](m.phi_1(it))) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
66
			q1 += q ;
67
			it = m.phi2_1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
68
69
70
71
72
73
		} while(it != d) ;

		// compute the error quadric associated to v2
		it = dd ;
		do
		{
74
			Utils::Quadric<REAL> q(this->m_attrV[0]->operator[](it), this->m_attrV[0]->operator[](m.phi1(it)), this->m_attrV[0]->operator[](m.phi_1(it))) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
75
			q2 += q ;
76
			it = m.phi2_1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
77
78
79
80
81
82
83
84
		} while(it != dd) ;
	}
	else // if the selector is QEM, use the error quadrics computed by the selector
	{
		q1 = m_quadric[d] ;
		q2 = m_quadric[dd] ;
	}

85
	Utils::Quadric<REAL> quad ;
86
87
	quad += q1 ;    // compute the sum of the
	quad += q2 ;    // two vertices quadrics
Pierre Kraemer's avatar
Pierre Kraemer committed
88
89

	VEC3 res ;
90
	bool opt = quad.findOptimizedPos(res) ; // try to compute an optimized position for the contraction of this edge
Pierre Kraemer's avatar
Pierre Kraemer committed
91
92
	if(!opt)
	{
93
94
95
		VEC3 p1 = this->m_attrV[0]->operator[](d) ;    // let the new vertex lie
		VEC3 p2 = this->m_attrV[0]->operator[](dd) ;   // on either one of the two endpoints
		VEC3 p12 = (p1 + p2) / 2.0f ;   // or the middle of the edge
Pierre Kraemer's avatar
Pierre Kraemer committed
96
97
98
		REAL e1 = quad(p1) ;
		REAL e2 = quad(p2) ;
		REAL e12 = quad(p12) ;
99
100
		REAL minerr = std::min(std::min(e1, e2), e12) ; // consider only the one for
		if(minerr == e12) this->m_approx[0][d] = p12 ;             // which the error is minimal
101
102
		else if(minerr == e1) this->m_approx[0][d] = p1 ;
		else this->m_approx[0][d] = p2 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
103
104
	}
	else
105
		this->m_approx[0][d] = res ;
Pierre Kraemer's avatar
Pierre Kraemer committed
106
107
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
108
109
110
111
112
/************************************************************************************
 *                            QUADRIC ERROR METRIC (for half-edge criteria)         *
 ************************************************************************************/

template <typename PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
113
bool Approximator_QEMhalfEdge<PFP>::init()
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
114
{
115
	m_quadric = this->m_map.template getAttribute<Utils::Quadric<REAL>, VERTEX>("QEMquadric") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
116
117
118
119
120
121
122
123
124

	if(this->m_predictor)
	{
		return false ;
	}
	return true ;
}

template <typename PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
125
void Approximator_QEMhalfEdge<PFP>::approximate(Dart d)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
126
127
128
129
130
131
{
	MAP& m = this->m_map ;

	// get some darts
	Dart dd = m.phi2(d) ;

132
	Utils::Quadric<REAL> q1, q2 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
133
134
135
136
137
138
	if(!m_quadric.isValid()) // if the selector is not QEM, compute local error quadrics
	{
		// compute the error quadric associated to v1
		Dart it = d ;
		do
		{
139
			Utils::Quadric<REAL> q(this->m_attrV[0]->operator[](it), this->m_attrV[0]->operator[](m.phi1(it)), this->m_attrV[0]->operator[](m.phi_1(it))) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
140
			q1 += q ;
141
			it = m.phi2_1(it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
142
143
144
145
146
147
		} while(it != d) ;

		// compute the error quadric associated to v2
		it = dd ;
		do
		{
148
			Utils::Quadric<REAL> q(this->m_attrV[0]->operator[](it), this->m_attrV[0]->operator[](m.phi1(it)), this->m_attrV[0]->operator[](m.phi_1(it))) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
149
			q2 += q ;
150
			it = m.phi2_1(it) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
151
152
153
154
155
156
157
158
		} while(it != dd) ;
	}
	else // if the selector is QEM, use the error quadrics computed by the selector
	{
		q1 = m_quadric[d] ;
		q2 = m_quadric[dd] ;
	}

159
	Utils::Quadric<REAL> quad ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
160
161
162
163
164
165
	quad += q1 ;	// compute the sum of the
	quad += q2 ;	// two vertices quadrics

	VEC3 res ;
	bool opt = quad.findOptimizedPos(res) ;	// try to compute an optimized position for the contraction of this edge
	if(!opt)
166
		this->m_approx[0][d] = this->m_attrV[0]->operator[](d) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
167
	else
168
		this->m_approx[0][d] = res ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
169
170
}

Pierre Kraemer's avatar
Pierre Kraemer committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/************************************************************************************
 *							         MID EDGE                                       *
 ************************************************************************************/

template <typename PFP>
bool Approximator_MidEdge<PFP>::init()
{
	if(this->m_predictor)
	{
		if(! (	this->m_predictor->getType() == P_TangentPredict1
			 || this->m_predictor->getType() == P_TangentPredict2 ) )
		{
			return false ;
		}
	}
	return true ;
}

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

	// get some darts
	Dart dd = m.phi2(d) ;

	// get the contracted edge vertices positions
198
199
	VEC3 v1 = this->m_attrV[0]->operator[](d) ;
	VEC3 v2 = this->m_attrV[0]->operator[](dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
200
201

	// Compute the approximated position
202
	this->m_approx[0][d] = (v1 + v2) / REAL(2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
203
204
205
206
207
208
209

	if(this->m_predictor)
	{
		Dart dd = m.phi2(d) ;
		Dart d2 = m.phi2(m.phi_1(d)) ;
		Dart dd2 = m.phi2(m.phi_1(dd)) ;

210
		// VEC3 v2 = this->m_attrV[0]->operator[](dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
211
212
213

		// temporary edge collapse
		m.extractTrianglePair(d) ;
214
		unsigned int newV = m.template setOrbitEmbeddingOnNewCell<VERTEX>(d2) ;
215
		this->m_attrV[0]->operator[](newV) = this->m_approx[0][d] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
216
217
218

		// compute the detail vector
		this->m_predictor->predict(d2, dd2) ;
219
		this->m_detail[0][d] = v1 - this->m_predictor->getPredict(0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
220
221
222

		// vertex split to reset the initial connectivity and embeddings
		m.insertTrianglePair(d, d2, dd2) ;
223
224
		m.template setOrbitEmbedding<VERTEX>(d, m.template getEmbedding<VERTEX>(d)) ;
		m.template setOrbitEmbedding<VERTEX>(dd, m.template getEmbedding<VERTEX>(dd)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	}
}

/************************************************************************************
 *							       HALF COLLAPSE                                    *
 ************************************************************************************/

template <typename PFP>
bool Approximator_HalfCollapse<PFP>::init()
{
	if(this->m_predictor)
	{
		if(! ( this->m_predictor->getType() == P_HalfCollapse ) )
		{
			return false ;
		}
	}
	return true ;
}

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

250
251
	for (unsigned int i = 0 ; i < this->m_attrV.size() ; ++i)
		this->m_approx[i][d] = this->m_attrV[i]->operator[](d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
252
253
254
255
256
257
258

	if(this->m_predictor)
	{
		Dart dd = m.phi2(d) ;
		Dart d2 = m.phi2(m.phi_1(d)) ;
		Dart dd2 = m.phi2(m.phi_1(dd)) ;

259
		VEC3 v2 = this->m_attrV[0]->operator[](dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
260
261
262

		// temporary edge collapse
		m.extractTrianglePair(d) ;
263
		unsigned int newV = m.template setOrbitEmbeddingOnNewCell<VERTEX>(d2) ;
264
265
266
267
		for (unsigned int i = 0 ; i < this->m_attrV.size() ; ++i)
		{
			this->m_attrV[i]->operator[](newV) = this->m_approx[i][d] ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
268
269
270

		// compute the detail vector
		this->m_predictor->predict(d2, dd2) ;
271
272
273
274
		for (unsigned int i = 0 ; i < this->m_attrV.size() ; ++i)
		{
			this->m_detail[i][d] = v2 - this->m_predictor->getPredict(1) ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
275
276
277

		// vertex split to reset the initial connectivity and embeddings
		m.insertTrianglePair(d, d2, dd2) ;
278
279
		m.template setOrbitEmbedding<VERTEX>(d, m.template getEmbedding<VERTEX>(d)) ;
		m.template setOrbitEmbedding<VERTEX>(dd, m.template getEmbedding<VERTEX>(dd)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	}
}

/************************************************************************************
 *							      CORNER CUTTING                                    *
 ************************************************************************************/

template <typename PFP>
bool Approximator_CornerCutting<PFP>::init()
{
	if(this->m_predictor)
	{
		if(! ( this->m_predictor->getType() == P_CornerCutting ) )
		{
			return false ;
		}
	}
	return true ;
}

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

	// get some darts
	Dart dd = m.phi2(d) ;
307
	// Dart d1 = m.phi2(m.phi1(d)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
308
	Dart d2 = m.phi2(m.phi_1(d)) ;
309
	// Dart dd1 = m.phi2(m.phi1(dd)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
310
311
312
	Dart dd2 = m.phi2(m.phi_1(dd)) ;

	// get the contracted edge vertices positions
313
314
	VEC3 v1 = this->m_attrV[0]->operator[](d) ;
	VEC3 v2 = this->m_attrV[0]->operator[](dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
315
316
317
318
319
320
321

	// compute the alpha value according to vertices valences
	REAL k1 = 0 ;
	Dart it = d ;
	do
	{
		++k1 ;
322
		it = m.phi2_1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
323
324
325
326
327
328
	} while(it != d) ;
	REAL k2 = 0 ;
	it = dd ;
	do
	{
		++k2 ;
329
		it = m.phi2_1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
330
331
332
333
334
335
336
337
338
	} while(it != dd) ;
	REAL alpha = (k1-1) * (k2-1) / (k1*k2-1) ;

	// Compute the mean of v1 half-ring
	VEC3 m1(0) ;
	unsigned int count = 0 ;
	it = d2 ;
	do
	{
339
		m1 += this->m_attrV[0]->operator[](m.phi1(it));
340
		it = m.phi2_1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
341
342
343
344
345
346
347
348
349
350
		++count ;
	} while (it != d) ;
	m1 /= REAL(count) ;

	// Compute the mean of v2 half-ring
	VEC3 m2(0) ;
	count = 0 ;
	it = dd2 ;
	do
	{
351
		m2 += this->m_attrV[0]->operator[](m.phi1(it));
352
		it = m.phi2_1(it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
353
354
355
356
357
358
359
360
361
362
		++count ;
	} while (it != dd) ;
	m2 /= REAL(count) ;

	// Compute the a1 approximation
	VEC3 a1 = ( REAL(1) / (REAL(1) - alpha) ) * ( v1 - (alpha * m1) ) ;
	// Compute the a2 approximation
	VEC3 a2 = ( REAL(1) / (REAL(1) - alpha) ) * ( v2 - (alpha * m2) ) ;

	// Compute the final approximated position
363
	this->m_approx[0][d] = (a1 + a2) / REAL(2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
364
365
366

	if(this->m_predictor)
	{
367
		this->m_detail[0][d] = (REAL(1) - alpha) * ( (a1 - a2) / REAL(2) ) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
368
369
370
	}
}

Sauvage's avatar
Sauvage committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/************************************************************************************
 *                            NORMAL AREA METRIC                                    *
 ************************************************************************************/
template <typename PFP>
bool Approximator_NormalArea<PFP>::init()
{
	edgeMatrix = this->m_map.template getAttribute<Geom::Matrix<3,3,REAL>, EDGE>("NormalAreaMatrix") ;
	assert(edgeMatrix.isValid());

//	m_quadric = this->m_map.template getAttribute<Utils::Quadric<REAL>, VERTEX>("QEMquadric") ;
	// Does not require to be valid (if it is not, altenatives will be used).

	if(this->m_predictor)
	{
		return false ;
	}
	return true ;
}

template <typename PFP>
void Approximator_NormalArea<PFP>::approximate(Dart d)
{
393
394
395
396
397
	typedef typename PFP::REAL REAL;
	typedef Geom::Matrix<3,3,REAL> MATRIX;
	typedef Eigen::Matrix<REAL,3,1> E_VEC3;
	typedef Eigen::Matrix<REAL,3,3> E_MATRIX;

Sauvage's avatar
Sauvage committed
398
399
	MAP& m = this->m_map ;
	Dart dd = m.phi2(d);
400
401
	MATRIX M1; // init zero included
	MATRIX M2; // init zero included
Sauvage's avatar
Sauvage committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429

	assert(! m.isBoundaryEdge(d));

	Traversor2VF<MAP> td (m,d);
	Dart it = td.begin();
	it = td.next();
	Dart it2 = td.next();
	while( it2 != td.end())
	{
		M1 += edgeMatrix[m.phi1(it)];
		it = it2;
		it2 = td.next();
	}

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

	const VEC3 & v1 = (*this->m_attrV[0])[d] ;
	const VEC3 & v2 = (*this->m_attrV[0])[dd] ;

430
431
	/* version plus sûre : sans cast avec recopie
	E_MATRIX A ;
Sauvage's avatar
Sauvage committed
432
433
434
	A << M1(0,0)+M2(0,0) , M1(0,1)+M2(0,1) , M1(0,2)+M2(0,2) , M1(1,0)+M2(1,0) , M1(1,1)+M2(1,1) , M1(1,2)+M2(1,2) , M1(2,0)+M2(2,0) , M1(2,1)+M2(2,1) , M1(2,2)+M2(2,2) ;

	VEC3 mb = M1*v1 + M2*v2 ;
435
	E_VEC3 b (mb[0],mb[1],mb[2]);
Sauvage's avatar
Sauvage committed
436

437
438
	Eigen::LDLT<E_MATRIX> decompo (A);
	E_VEC3 x = decompo.solve(b);
Sauvage's avatar
Sauvage committed
439
440

	this->m_approx[0][d] = VEC3 (x(0),x(1),x(2)) ;
441
442
443
444
445
446
447
448
449
450
	 */

	/* version legerement moins gourmande et plus risquee : avec cast sans recopie */
	VEC3 mb = M1*v1 + M2*v2 ;
	M1 += M2;

	Eigen::LDLT<E_MATRIX> decompo (Utils::convertRef<E_MATRIX>(M1));
	E_VEC3 x = decompo.solve(Utils::convertRef<E_VEC3>(mb));

	this->m_approx[0][d] = Utils::convertRef<VEC3>(x) ;
Sauvage's avatar
Sauvage committed
451
452
453
}


Pierre Kraemer's avatar
Pierre Kraemer committed
454
455
456
457
458
} //namespace Decimation

} //namespace Algo

} //namespace CGoGN