Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
403 jab 1
/*
2
 *  curvature.cpp
3
 *  GEL
4
 *
5
 *  Created by J. Andreas Bærentzen on 23/09/08.
6
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
7
 *
8
 */
9
 
10
#include "curvature.h"
11
 
12
#include <GLGraphics/gel_glut.h>
13
 
14
#include <iostream>
15
#include <CGLA/eigensolution.h>
16
#include <CGLA/Vec2d.h>
17
#include <CGLA/Vec3d.h>
18
#include <CGLA/Mat3x3d.h>
19
#include <CGLA/Mat2x2d.h>
20
#include <CGLA/Mat2x3d.h>
21
 
22
#include <HMesh/VertexCirculator.h>
23
#include <HMesh/FaceCirculator.h>
24
#include <HMesh/x3d_save.h>
25
#include <HMesh/x3d_load.h>
26
#include <HMesh/obj_load.h>
27
#include <HMesh/build_manifold.h>
28
#include <HMesh/mesh_optimization.h>
29
 
30
#include <LinAlg/Matrix.h>
31
#include <LinAlg/Vector.h>
32
#include <LinAlg/LapackFunc.h>
33
 
34
using namespace std;
35
using namespace HMesh;
36
using namespace LinAlg;
37
using namespace CGLA;
38
 
39
namespace {
40
	double scal = 0.001;
41
	double vector_scal = 0.001;
42
 
43
	template<class T> 
44
	void smooth_something_on_mesh(Manifold& m, vector<T>& vec, int smooth_steps)
45
	{
46
		for(int iter=0;iter<smooth_steps;++iter)
47
		{
48
			vector<T> new_vec(m.no_vertices());
49
			for(VertexIter vi=m.vertices_begin(); vi != m.vertices_end(); ++vi)
50
			{
51
				int i = vi->touched;
52
				new_vec[i] = vec[i];
53
				for(VertexCirculator vc(vi); !vc.end();++vc)
54
				{
55
					int j = vc.get_vertex()->touched;
56
					new_vec[i] += vec[j];
57
				}
58
				new_vec[i] /=(valency(vi)+ 1.0);
59
			}
60
			swap(vec,new_vec);
61
		}		
62
	}
63
}
64
 
65
double voronoi_area(VertexIter v)
66
{
67
	double area_mixed = 0;
68
	//For each triangle T from the 1-ring neighborhood of x
69
	for(VertexCirculator vc(v); !vc.end(); ++vc)
70
	{
71
		FaceIter f = vc.get_face();
72
		double f_area = area(f);
73
 
74
		HalfEdgeIter he = vc.get_halfedge();
75
		Vec3d v1(he->vert->pos);
76
		Vec3d v2(he->next->vert->pos);
77
		Vec3d v0(he->next->next->vert->pos);
78
 
79
		double a0 = acos(dot(v1-v0, v2-v0)/(length(v1-v0)*length(v2-v0)));
80
		double a1 = acos(dot(v2-v1, v0-v1)/(length(v2-v1)*length(v0-v1)));
81
		double a2 = acos(dot(v0-v2, v1-v2)/(length(v0-v2)*length(v1-v2)));
82
 
83
		if(a0>(M_PI/2.0) && a1>(M_PI/2.0) && a2>(M_PI/2.0)) // f is non-obtuse
84
		{
85
			// Add Voronoi formula (see Section 3.3)
86
			area_mixed += (1.0/8) * 
87
			((1.0/tan(a1)) * sqr_length(v2-v0) + 
88
			 (1.0/tan(a2)) * sqr_length(v1-v0));
89
		}
90
		else // Voronoi inappropriate
91
		{
92
			// Add either area(f)/4 or area(f)/2
93
			if(a0>M_PI/2.0)// the angle of f at x is obtuse
94
				area_mixed += f_area/2;
95
			else
96
				area_mixed += f_area/4;
97
		}
98
	}
99
	return area_mixed;
100
}
101
 
102
double barycentric_area(VertexIter v)
103
{
104
	double barea = 0;
105
	//For each triangle T from the 1-ring neighborhood of x
106
	for(VertexCirculator vc(v); !vc.end(); ++vc)
107
	{
108
		FaceIter f = vc.get_face();
109
		barea += area(f)/3.0;
110
	}
111
	return barea;
112
}
113
 
114
const Vec3d mean_curvature_normal(VertexIter v)
115
{
116
	if(!is_boundary(v))
117
	{
118
		Vec3d vertex(v->pos);
119
		Vec3d curv_normal(0);
120
		double a_sum = 0;
121
		for(VertexCirculator vc(v); !vc.end(); ++vc)
122
		{
123
			HalfEdgeIter h = vc.get_halfedge();
124
			Vec3d nbr(h->vert->pos);
125
			Vec3d left(h->next->vert->pos);
126
			Vec3d right(h->opp->prev->opp->vert->pos);
127
 
128
			double d_left = dot(normalize(nbr-left),
129
								normalize(vertex-left));
130
			double d_right = dot(normalize(nbr-right),
131
								 normalize(vertex-right));
132
			double a_left  = acos(min(1.0, max(-1.0, d_left)));
133
			double a_right = acos(min(1.0, max(-1.0, d_right)));
134
 
135
			double w = 1.0/tan(a_left) + 1.0/tan(a_right);
136
 
137
			curv_normal += w * (vertex-nbr);
138
 
139
			a_sum += area(vc.get_face());
140
		}
141
 
142
		return curv_normal/(4*a_sum);
143
	}
144
	return Vec3d(0);
145
}
146
 
147
double sum_curvatures(Manifold& m, vector<double>& curvature)
148
{
149
	double sum = 0;
150
	for(VertexIter v=m.vertices_begin(); v!=m.vertices_end(); ++v)
151
	{
152
		if(!is_boundary(v))
153
		{
154
			sum += curvature[v->touched] * voronoi_area(v);
155
		}
156
	}
157
	return sum;
158
}
159
 
160
 
161
const double gaussian_curvature_angle_defect(VertexIter v)
162
{
163
	if(!is_boundary(v))
164
	{
165
		Vec3f vertex(v->pos);
166
		vector<Vec3d> edges;
167
		for(VertexCirculator vc(v); !vc.end(); ++vc)
168
		{
169
			Vec3d e(normalize(vc.get_vertex()->pos-vertex));
170
			edges.push_back(e);
171
		}
172
		int N=edges.size();
173
		double angle_sum = 0;
174
		for(int i=0;i<N;++i)
175
		{
176
			double dot_prod = 
177
			s_max(-1.0, s_min(1.0, dot(edges[i],edges[(i+1)%N])));
178
			angle_sum += acos(dot_prod);
179
		}
180
		return (2*M_PI - angle_sum)/voronoi_area(v);
181
	}
182
	return 0;
183
}
184
 
185
const Mat3x3d curvature_tensor(HalfEdgeIter h)
186
{
187
	if(!is_boundary(h))
188
	{
189
		Vec3d edge(h->vert->pos - h->opp->vert->pos);
190
		double edge_len = length(edge);
191
		edge /= edge_len;
192
 
193
		Vec3d h_norm(normal(h->face));
194
		Vec3d h_opp_norm(normal(h->opp->face));
195
 
196
		Vec3d nc = cross(h_norm, h_opp_norm);
197
 
198
		double sign = (dot(nc, edge) >= 0) ? 1 : -1;
199
		double beta = asin(nc.length());
200
 
201
		Mat3x3d m;
202
		outer_product(edge, edge, m);
203
		return sign * edge_len * beta * m;
204
	}
205
	return Mat3x3d(0);
206
}
207
 
208
const Mat3x3d curvature_tensor_from_edges(VertexIter v)
209
{
210
	Mat3x3d curv_tensor(0);
211
 
212
	if(!is_boundary(v))
213
	{
214
		for(VertexCirculator vc(v); !vc.end(); ++vc)
215
		{
216
			curv_tensor += 0.5*curvature_tensor(vc.get_halfedge());
217
		}
218
		curv_tensor /= voronoi_area(v);
219
	}
220
	return curv_tensor;
221
}
222
 
223
 
224
void curvature_tensor_paraboloid(VertexIter v,
225
								 Mat2x2d& curv_tensor,
226
								 Mat3x3d& frame)
227
{
228
	if(!is_boundary(v))
229
	{
230
		// First estimate the normal and compute a transformation matrix
231
		// which takes us into tangent plane coordinates.
232
		Vec3d Norm = Vec3d(normal(v));
233
		Vec3d X,Y;
234
		orthogonal(Norm,X,Y);
235
		frame = Mat3x3d(X,Y,Norm);
236
		Vec3d centre(v->pos);
237
 
238
		vector<Vec3d> points;
239
		for(VertexCirculator vc(v); !vc.end(); ++vc)
240
			points.push_back(Vec3d(vc.get_vertex()->pos));
241
 
242
		int N = points.size();
243
 
244
		CVector b(N);
245
		// Compute the matrix of parameter values
246
		CMatrix PMat(N, 3);
247
		for(int i=0;i<N;++i)
248
		{
249
			Vec3d p = frame * (points[i]-centre);
250
			b[i] = p[2];
251
 
252
			PMat.set(i,0,0.5*sqr(p[0]));
253
			PMat.set(i,1,p[0]*p[1]);
254
			PMat.set(i,2,0.5*sqr(p[1]));
255
		}
256
 
257
		// Compute the coefficients of the polynomial surface
258
		CVector x(3);
259
		x = LinearLSSolve(PMat,b);
260
		if(isnan(x[0])) cout << __LINE__ << " " << PMat << b << endl ;
261
 
262
		// Finally compute the shape tensor from the coefficients
263
		// using the first and second fundamental forms.
264
		curv_tensor =Mat2x2d(x[0],x[1],x[1],x[2]);
265
	}
266
}
267
 
268
void curvature_tensors_from_edges(Manifold& m, 
269
								  vector<Mat3x3d>& curvature_tensors)
270
{
271
	curvature_tensors.resize(m.no_vertices());
272
	int i=0;
273
	for(VertexIter vi=m.vertices_begin(); vi != m.vertices_end(); ++vi,++i)
274
	{
275
		vi->touched = i;
276
		curvature_tensors[i] = curvature_tensor_from_edges(vi);
277
	}
278
}
279
 
280
void smooth_curvature_tensors(Manifold& m,																	
281
							  vector<Mat3x3d>& curvature_tensors)
282
{
283
	assert(curvature_tensors.size() == m.no_vertices());
284
	vector<Mat3x3d> tmp_curvature_tensors(m.no_vertices());
285
	double tmp_area;
286
	int i=0;
287
	for(VertexIter vi=m.vertices_begin(); vi != m.vertices_end(); ++vi,++i)
288
		if(!is_boundary(vi))
289
		{
290
			double a = voronoi_area(vi);
291
			tmp_curvature_tensors[i] = curvature_tensors[i] * a;
292
			tmp_area = a;
293
			int n=1;
294
			for(VertexCirculator vc(vi); !vc.end(); ++vc)
295
				if(!is_boundary(vc.get_vertex()))
296
				{
297
					int j = vc.get_vertex()->touched;
298
					double a = voronoi_area(vc.get_vertex());
299
					tmp_curvature_tensors[i] += curvature_tensors[j]*a;
300
					tmp_area += a;
301
					++n;
302
				}
303
			tmp_curvature_tensors[i] /= tmp_area;
304
		}
305
	curvature_tensors = tmp_curvature_tensors;
306
}
307
 
308
void gaussian_curvature_angle_defects(Manifold& m,
309
									  vector<double>& curvature,
310
									  int smooth_steps)
311
{
312
	m.enumerate_vertices();
313
	curvature.resize(m.no_vertices());
314
	for(VertexIter vi=m.vertices_begin(); vi != m.vertices_end(); ++vi)
315
	{
316
		int i = vi->touched;
317
		curvature[i] = gaussian_curvature_angle_defect(vi);
318
	}
319
	smooth_something_on_mesh(m, curvature, smooth_steps);
320
}
321
 
322
void mean_curvatures(Manifold& m, vector<double>& curvature,int smooth_steps)
323
{
324
	curvature.resize(m.no_vertices());
325
	int i=0;
326
	for(VertexIter vi=m.vertices_begin(); vi != m.vertices_end(); ++vi,++i)
327
	{
328
		vi->touched = i;
329
		Vec3d N = mean_curvature_normal(vi);
330
		curvature[i] = length(N)*sign(dot(N,Vec3d(normal(vi))));
331
	}	
332
	smooth_something_on_mesh(m, curvature, smooth_steps);	
333
}
334
 
335
 
336
void curvature_paraboloids(Manifold& m,
337
						   vector<Vec3d>& min_curv_direction,
338
						   vector<Vec3d>& max_curv_direction,
339
						   vector<double>& curvature)
340
{
341
	int i=0;
342
	for(VertexIter vi=m.vertices_begin(); vi != m.vertices_end(); ++vi,++i)
343
	{
344
		vi->touched = i;
345
		Mat2x2d tensor;
346
		Mat3x3d frame;
347
		curvature_tensor_paraboloid(vi, tensor, frame);
348
 
349
		Mat2x2d Q,L;
350
		int s = power_eigensolution(tensor, Q, L);
351
 
352
		if(s<2)	
353
			cout << tensor << Q << L << endl;
354
 
355
		int max_idx=0;
356
		int min_idx=1;
357
 
358
		if(fabs(L[max_idx][max_idx])<fabs(L[min_idx][min_idx])) swap(max_idx, min_idx);
359
 
360
		Mat3x3d frame_t = transpose(frame);
361
 
362
		max_curv_direction[i] = 
363
		frame_t * Vec3d(Q[max_idx][0], Q[max_idx][1], 0);
364
 
365
		min_curv_direction[i] = 
366
		frame_t * Vec3d(Q[min_idx][0], Q[min_idx][1], 0);
367
 
368
		curvature[i] = L[0][0]*L[1][1];
369
	}
370
}
371
 
372
 
373
void curvature_from_tensors(Manifold& m,
374
							const vector<Mat3x3d>& curvature_tensors,
375
							vector<Vec3d>& min_curv_direction,
376
							vector<Vec3d>& max_curv_direction,
377
							vector<double>& curvature)
378
{
379
	assert(curvature_tensors.size() == m.no_vertices());
380
	min_curv_direction.resize(m.no_vertices());
381
	max_curv_direction.resize(m.no_vertices());
382
	curvature.resize(m.no_vertices());
383
	double max_val = -1e30;
384
	for(VertexIter vi=m.vertices_begin(); vi != m.vertices_end(); ++vi)
385
	{
386
		int i = vi->touched;
387
		Mat3x3d C,Q,L;
388
		C = curvature_tensors[i];
389
		int s = power_eigensolution(C, Q, L);
390
		Vec3d dmin, dmax;
391
		if(s==0)
392
		{
393
			Vec3d n(normal(vi));
394
			orthogonal(n,dmin, dmax);
395
			curvature[i] = 0;
396
			cout << " rank 0 " << endl;
397
		}
398
		else if(s==1)
399
		{
400
			Vec3d n(normal(vi));
401
			dmin = normalize(Q[0]);
402
			dmax = cross(n, dmin);
403
			curvature[i] = 0;
404
			cout << " rank 1 " << endl;
405
		}
406
		else
407
		{
408
			/*				Vec3d l(fabs(L[0][0]), fabs(L[1][1]), fabs(L[2][2]));
409
 
410
			 int z_idx=2;
411
			 if(s==3)
412
			 {
413
			 if(l[0] < l[1])
414
			 z_idx = l[0]<l[2] ? 0 : 2;
415
			 else
416
			 z_idx = l[1]<l[2] ? 1 : 2;
417
			 }
418
			 int max_idx = (z_idx + 1) % 3;
419
			 int min_idx = (z_idx + 2) % 3;
420
 
421
			 if(l[max_idx] < l[min_idx]) swap(max_idx, min_idx);
422
			 */
423
			int max_idx = 0;
424
			int min_idx = 1;
425
			// Yes - the biggest eigenvalue corresponds to the min direction
426
			// and vice versa.
427
			dmin = normalize(Q[max_idx]);
428
			dmax = normalize(Q[min_idx]);
429
 
430
			curvature[i] = L[max_idx][max_idx]*L[min_idx][min_idx];
431
 
432
		}
433
		min_curv_direction[i] = dmin;
434
		max_curv_direction[i] = dmax;
435
		max_val = max(fabs(curvature[i]), max_val);
436
 
437
	}
438
	scal = 1.0/max_val;
439
}
440