Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

curvature.hpp 13.4 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#include "Algo/Geometry/localFrame.h"
#include "Geometry/matrix.h"
#include "Topology/generic/cellmarker.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
28
#include "Algo/Selection/collector.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

extern "C"
{
	#include "C_BLAS_LAPACK/INCLUDE/f2c.h"
	#include "C_BLAS_LAPACK/INCLUDE/clapack.h"
}
#undef max
#undef min

namespace CGoGN
{

namespace Algo
{

namespace Geometry
{

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
48
49
50
51
52
53
54
55
56
void computeCurvatureVertices_QuadraticFitting(
	typename PFP::MAP& map,
	const typename PFP::TVEC3& position,
	const typename PFP::TVEC3& normal,
	typename PFP::TREAL& kmax,
	typename PFP::TREAL& kmin,
	typename PFP::TVEC3& Kmax,
	typename PFP::TVEC3& Kmin,
	const FunctorSelect& select)
Pierre Kraemer's avatar
Pierre Kraemer committed
57
{
58
	CellMarker marker(map, VERTEX);
Pierre Kraemer's avatar
Pierre Kraemer committed
59
60
61
62
63
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !marker.isMarked(d))
		{
			marker.mark(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
64
			computeCurvatureVertex_QuadraticFitting<PFP>(map, d, position, normal, kmax, kmin, Kmax, Kmin) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
65
66
67
68
69
		}
	}
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
70
71
72
73
74
75
76
77
78
void computeCurvatureVertex_QuadraticFitting(
	typename PFP::MAP& map,
	Dart dart,
	const typename PFP::TVEC3& position,
	const typename PFP::TVEC3& normal,
	typename PFP::TREAL& kmax,
	typename PFP::TREAL& kmin,
	typename PFP::TVEC3& Kmax,
	typename PFP::TVEC3& Kmin)
Pierre Kraemer's avatar
Pierre Kraemer committed
79
80
81
82
83
84
{
	typedef typename PFP::REAL REAL ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::MATRIX33 MATRIX33 ;

	VEC3 n = normal[dart] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
85

Pierre Kraemer's avatar
Pierre Kraemer committed
86
87
88
89
90
91
	MATRIX33 localFrame = Algo::Geometry::vertexLocalFrame<PFP>(map, dart, position, n) ;
	MATRIX33 invLocalFrame ;
	localFrame.invert(invLocalFrame) ;

	REAL a, b, c, d, e;
	//vertexCubicFitting(map,dart,localFrame,a,b,c,d,e,f,g,h,i) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
92
	vertexQuadraticFitting<PFP>(map, dart, localFrame, position, normal, a, b, c, d, e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
93

Pierre Kraemer's avatar
Pierre Kraemer committed
94
	REAL kmax_v, kmin_v, Kmax_x, Kmax_y ;
Pierre Kraemer's avatar
Pierre Kraemer committed
95
	//int res = slaev2_(&e, &f, &g, &maxC, &minC, &dirX, &dirY) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
96
	/*int res = */slaev2_(&a, &b, &c, &kmax_v, &kmin_v, &Kmax_x, &Kmax_y) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
97

Pierre Kraemer's avatar
Pierre Kraemer committed
98
99
100
	VEC3 Kmax_v(Kmax_x, Kmax_y, 0.0f) ;
	Kmax_v = invLocalFrame * Kmax_v ;
	VEC3 Kmin_v = n ^ Kmax_v ;
Pierre Kraemer's avatar
Pierre Kraemer committed
101

Pierre Kraemer's avatar
Pierre Kraemer committed
102
	if (kmax_v < kmin_v)
103
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
104
105
106
107
		kmax[dart] = -kmax_v ;
		kmin[dart] = -kmin_v ;
		Kmax[dart] = Kmax_v ;
		Kmin[dart] = Kmin_v ;
108
109
110
	}
	else
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
111
112
113
114
		kmax[dart] = -kmin_v ;
		kmin[dart] = -kmax_v ;
		Kmax[dart] = Kmin_v ;
		Kmin[dart] = Kmax_v ;
115
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
116
117
118
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
119
void vertexQuadraticFitting(
Pierre Kraemer's avatar
Pierre Kraemer committed
120
121
122
123
124
125
126
		typename PFP::MAP& map,
		Dart dart,
		typename PFP::MATRIX33& localFrame,
		const typename PFP::TVEC3& position,
		const typename PFP::TVEC3& normal,
		float& a, float& b, float& c, float& d, float& e)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
127
	typename PFP::VEC3 p = position[dart] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
128
129
130
131
132
133
134
135

	LinearSolver<CPUSolverTraits> solver(5) ;
	solver.set_least_squares(true) ;
	solver.begin_system() ;
	Dart it = dart ;
	do
	{
		// 1-ring vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
136
137
138
139
		typename PFP::VEC3 v = position[map.phi2(it)] ;
		quadraticFittingAddVertexPos<PFP>(v, p, localFrame, solver) ;
		typename PFP::VEC3 n = normal[map.phi2(it)] ;
		quadraticFittingAddVertexNormal<PFP>(v, n, p, localFrame, solver) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
		// 2-ring vertices
//		Dart d2 = map.phi1(map.phi1(map.phi2(map.phi1(it)))) ;
//		VEC3 v2 = position[d2] ;
//		quadricFittingAddVertexPos(v2, p, localFrame, solver) ;
//		VEC3 n2 = normal[d2] ;
//		quadricFittingAddVertexNormal(v2, n2, p, localFrame, solver) ;
		it = map.phi1( map.phi2(it)) ;
	} while (it != dart) ;
	solver.end_system() ;
	solver.solve() ;

	a = solver.variable(0).value() ;
	b = solver.variable(1).value() ;
	c = solver.variable(2).value() ;
	d = solver.variable(3).value() ;
	e = solver.variable(4).value() ;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
159
void quadraticFittingAddVertexPos(typename PFP::VEC3& v, typename PFP::VEC3& p, typename PFP::MATRIX33& localFrame, LinearSolver<CPUSolverTraits>& solver)
Pierre Kraemer's avatar
Pierre Kraemer committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{
	typename PFP::VEC3 vec = v - p ;
	vec = localFrame * vec ;
	solver.begin_row() ;

	solver.add_coefficient(0, vec[0]*vec[0]) ;
	solver.add_coefficient(1, vec[0]*vec[1]) ;
	solver.add_coefficient(2, vec[1]*vec[1]) ;
	solver.add_coefficient(3, vec[0]) ;
	solver.add_coefficient(4, vec[1]) ;

	solver.set_right_hand_side(vec[2]) ;
	solver.end_row() ;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
176
void quadraticFittingAddVertexNormal(typename PFP::VEC3& v, typename PFP::VEC3& n, typename PFP::VEC3& p, typename PFP::MATRIX33& localFrame, LinearSolver<CPUSolverTraits>& solver)
Pierre Kraemer's avatar
Pierre Kraemer committed
177
178
179
180
181
182
{
	typename PFP::VEC3 vec = v - p ;
	vec = localFrame * vec ;
	typename PFP::VEC3 norm = localFrame * n ;
	solver.begin_row() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
183
	solver.add_coefficient(0, 2.0f * vec[0]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
184
185
186
187
	solver.add_coefficient(1, vec[1]) ;
	solver.add_coefficient(2, 0) ;
	solver.add_coefficient(3, 1.0f) ;
	solver.add_coefficient(4, 0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
188
	solver.set_right_hand_side(-1.0f * norm[0] / norm[2]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
189
190
191
192
193
	solver.end_row() ;

	solver.begin_row() ;
	solver.add_coefficient(0, 0) ;
	solver.add_coefficient(1, vec[0]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
194
	solver.add_coefficient(2, 2.0f * vec[1]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
195
196
	solver.add_coefficient(3, 0) ;
	solver.add_coefficient(4, 1.0f) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
197
	solver.set_right_hand_side(-1.0f * norm[1] / norm[2]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
198
199
200
201
	solver.end_row() ;
}
/*
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
202
void vertexCubicFitting(Dart dart, gmtl::Vec3f& normal, float& a, float& b, float& c, float& d, float& e, float& f, float& g, float& h, float& i)
Pierre Kraemer's avatar
Pierre Kraemer committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{
	gmtl::Matrix33f localFrame, invLocalFrame ;
	Algo::Geometry::vertexLocalFrame<PFP>(m_map,dart,normal,localFrame) ;
	gmtl::invertFull(invLocalFrame, localFrame) ;
	gmtl::Vec3f p = m_map.getVertexEmb(dart)->getPosition() ;
	solverC->reset(false) ;
	solverC->set_least_squares(true) ;
	solverC->begin_system() ;
	Dart it = dart ;
	do
	{
		// 1-ring vertices
		gmtl::Vec3f v = m_map.getVertexEmb(m_map.phi2(it))->getPosition() ;
		cubicFittingAddVertexPos(v,p,localFrame) ;
		gmtl::Vec3f n = m_normalsV[m_map.getVertexEmb(m_map.phi2(it))->getLabel()] ;
		cubicFittingAddVertexNormal(v,n,p,localFrame) ;
		// 2-ring vertices
//		Dart d2 = m_map.phi1(m_map.phi1(m_map.phi2(m_map.phi1(it)))) ;
//		gmtl::Vec3f v2 = m_map.getVertexEmb(d2)->getPosition() ;
//		cubicFittingAddVertexPos(v2,p,localFrame) ;
//		gmtl::Vec3f n2 = m_normalsV[m_map.getVertexEmb(d2)->getLabel()] ;
//		cubicFittingAddVertexNormal(v2,n2,p,localFrame) ;

		it = m_map.phi1( m_map.phi2(it)) ;
	} while (it != dart) ;
	solverC->end_system() ;
	solverC->solve() ;
	a = solverC->variable(0).value() ;
	b = solverC->variable(1).value() ;
	c = solverC->variable(2).value() ;
	d = solverC->variable(3).value() ;
	e = solverC->variable(4).value() ;
	f = solverC->variable(5).value() ;
	g = solverC->variable(6).value() ;
	h = solverC->variable(7).value() ;
	i = solverC->variable(8).value() ;

//	normal = gmtl::Vec3f(-h, -i, 1.0f) ;
//	gmtl::normalize(normal) ;
//	normal = invLocalFrame * normal ;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
246
void cubicFittingAddVertexPos(gmtl::Vec3f& v, gmtl::Vec3f& p, gmtl::Matrix33f& localFrame)
Pierre Kraemer's avatar
Pierre Kraemer committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{
	gmtl::Vec3f vec = v - p ;
	vec = localFrame * vec ;
	solverC->begin_row() ;

	solverC->add_coefficient(0, vec[0]*vec[0]*vec[0]) ;
	solverC->add_coefficient(1, vec[0]*vec[0]*vec[1]) ;
	solverC->add_coefficient(2, vec[0]*vec[1]*vec[1]) ;
	solverC->add_coefficient(3, vec[1]*vec[1]*vec[1]) ;
	solverC->add_coefficient(4, vec[0]*vec[0]) ;
	solverC->add_coefficient(5, vec[0]*vec[1]) ;
	solverC->add_coefficient(6, vec[1]*vec[1]) ;
	solverC->add_coefficient(7, vec[0]) ;
	solverC->add_coefficient(8, vec[1]) ;

	solverC->set_right_hand_side(vec[2]) ;
	solverC->end_row() ;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
267
void cubicFittingAddVertexNormal(gmtl::Vec3f& v, gmtl::Vec3f& n, gmtl::Vec3f& p, gmtl::Matrix33f& localFrame)
Pierre Kraemer's avatar
Pierre Kraemer committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{
	gmtl::Vec3f vec = v - p ;
	vec = localFrame * vec ;
	gmtl::Vec3f norm = localFrame * n ;
	solverC->begin_row() ;

	solverC->add_coefficient(0, 3.0f*vec[0]*vec[0]) ;
	solverC->add_coefficient(1, 2.0f*vec[0]*vec[1]) ;
	solverC->add_coefficient(2, vec[1]*vec[1]) ;
	solverC->add_coefficient(3, 0) ;
	solverC->add_coefficient(4, 2.0f*vec[0]) ;
	solverC->add_coefficient(5, vec[1]) ;
	solverC->add_coefficient(6, 0) ;
	solverC->add_coefficient(7, 1.0f) ;
	solverC->add_coefficient(8, 0) ;
	solverC->set_right_hand_side(-1.0f*norm[0]/norm[2]) ;
	solverC->end_row() ;

	solverC->begin_row() ;
	solverC->add_coefficient(0, 0) ;
	solverC->add_coefficient(1, vec[0]*vec[0]) ;
	solverC->add_coefficient(2, 2.0f*vec[0]*vec[1]) ;
	solverC->add_coefficient(3, 3.0f*vec[1]*vec[1]) ;
	solverC->add_coefficient(4, 0) ;
	solverC->add_coefficient(5, vec[0]) ;
	solverC->add_coefficient(6, 2.0f*vec[1]) ;
	solverC->add_coefficient(7, 0) ;
	solverC->add_coefficient(8, 1.0f) ;
	solverC->set_right_hand_side(-1.0f*norm[1]/norm[2]) ;
	solverC->end_row() ;
}
*/
Pierre Kraemer's avatar
Pierre Kraemer committed
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

template <typename PFP>
void computeCurvatureVertices_NormalCycles(
	typename PFP::MAP& map,
	typename PFP::REAL radius,
	const typename PFP::TVEC3& position,
	const typename PFP::TVEC3& normal,
	const typename PFP::TREAL& angles,
	typename PFP::TREAL& kmax,
	typename PFP::TREAL& kmin,
	typename PFP::TVEC3& Kmax,
	typename PFP::TVEC3& Kmin,
	typename PFP::TVEC3& Knormal,
	const FunctorSelect& select = SelectorTrue())
{
	CellMarker marker(map, VERTEX);
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !marker.isMarked(d))
		{
			marker.mark(d);
			computeCurvatureVertex_NormalCycles<PFP>(map, d, radius, position, normal, angles, kmax, kmin, Kmax, Kmin, Knormal) ;
		}
	}
}

template <typename PFP>
void computeCurvatureVertex_NormalCycles(
	typename PFP::MAP& map,
	Dart dart,
	typename PFP::REAL radius,
	const typename PFP::TVEC3& position,
	const typename PFP::TVEC3& normal,
	const typename PFP::TREAL& angles,
	typename PFP::TREAL& kmax,
	typename PFP::TREAL& kmin,
	typename PFP::TVEC3& Kmax,
	typename PFP::TVEC3& Kmin,
	typename PFP::TVEC3& Knormal)
{
340
341
342
343
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

	Algo::Selection::Collector_WithinSphere<PFP> neigh(map, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
344
345
346
	neigh.init(dart, radius) ;
	neigh.collect() ;
	neigh.computeArea() ;
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407

	VEC3 center = position[neigh.getCenter()] ;

	typename PFP::MATRIX33 tensor(0) ;

	// inside
	const std::vector<Dart>& vd1 = neigh.getInsideEdges() ;
	for (std::vector<Dart>::const_iterator it = vd1.begin(); it != vd1.end(); ++it)
	{
		const VEC3 e = position[map.phi2(*it)] - position[*it] ;
		tensor += Geom::transposed_vectors_mult(e,e) * angles[*it] * (1 / e.norm()) ;
	}

	// border
	const std::vector<Dart>& vd2 = neigh.getBorder() ;
	for (std::vector<Dart>::const_iterator it = vd2.begin(); it != vd2.end(); ++it)
	{
		const VEC3 e = position[map.phi2(*it)] - position[*it] ;
		const REAL alpha = neigh.intersect_SphereEdge(*it, map.phi2(*it)) ;
		tensor += Geom::transposed_vectors_mult(e,e) * angles[*it] * (1 / e.norm()) * alpha ;
	}

	tensor /= neigh.getArea() ;

	long int n = 3, lda = 3, info, lwork = 9 ;
	char jobz='V', uplo = 'U' ;
	float work[9] ;
	float w[3] ;
	float a[3*3] = {
		tensor(0,0), 0.0f, 0.0f,
		tensor(1,0), tensor(1,1), 0.0f,
		tensor(2,0), tensor(2,1), tensor(2,2)
	} ;
	// Solve eigenproblem
	ssyev_(&jobz, &uplo, (integer*)&n, a, (integer*)&lda, w, work, (integer*)&lwork, (integer*)&info) ;
	// Check for convergence
	if(info > 0)
		std::cerr << "clapack ssyev_ failed to compute eigenvalues : exit with code " << info << std::endl ;
	// sort eigen components : w[s[0]] has minimal absolute value ; kmin = w[s[1]] <= w[s[2]] = kmax
	int s[3] = {0, 1, 2} ;
	int tmp ;
	if (abs(w[s[2]]) < abs(w[s[1]])) { tmp = s[1] ; s[1] = s[2] ; s[2] = tmp ; }
	if (abs(w[s[1]]) < abs(w[s[0]])) { tmp = s[0] ; s[0] = s[1] ; s[1] = tmp ; }
	if (w[s[2]] < w[s[1]]) { tmp = s[1] ; s[1] = s[2] ; s[2] = tmp ; }

	kmin[dart] = w[s[1]] ;
	kmax[dart] = w[s[2]] ;
	VEC3& dirMin = Kmin[dart] ;
	dirMin[0] = a[3*s[2]];
	dirMin[1] = a[3*s[2]+1];
	dirMin[2] = a[3*s[2]+2]; // warning : Kmin and Kmax are switched
	VEC3& dirMax = Kmax[dart] ;
	dirMax[0] = a[3*s[1]];
	dirMax[1] = a[3*s[1]+1];
	dirMax[2] = a[3*s[1]+2]; // warning : Kmin and Kmax are switched
	VEC3& dirNormal = Knormal[dart] ;
	dirNormal[0] = a[3*s[0]];
	dirNormal[1] = a[3*s[0]+1];
	dirNormal[2] = a[3*s[0]+2];
	if (dirNormal * normal[dart] < 0)
		dirNormal *= -1; // change orientation
Pierre Kraemer's avatar
Pierre Kraemer committed
408
409
}

Pierre Kraemer's avatar
Pierre Kraemer committed
410
411
412
413
414
} // namespace Geometry

} // namespace Algo

} // namespace CGoGN