-
Notifications
You must be signed in to change notification settings - Fork 326
/
compute_curvature.m
179 lines (155 loc) · 4.42 KB
/
compute_curvature.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
function [Umin,Umax,Cmin,Cmax,Cmean,Cgauss,Normal] = compute_curvature(V,F,options)
% compute_curvature - compute principal curvature directions and values
%
% [Umin,Umax,Cmin,Cmax,Cmean,Cgauss,Normal] = compute_curvature(V,F,options);
%
% Umin is the direction of minimum curvature
% Umax is the direction of maximum curvature
% Cmin is the minimum curvature
% Cmax is the maximum curvature
% Cmean=(Cmin+Cmax)/2
% Cgauss=Cmin*Cmax
% Normal is the normal to the surface
%
% options.curvature_smoothing controls the size of the ring used for
% averaging the curvature tensor.
%
% The algorithm is detailed in
% David Cohen-Steiner and Jean-Marie Morvan.
% Restricted Delaunay triangulations and normal cycle.
% In Proc. 19th Annual ACM Symposium on Computational Geometry,
% pages 237-246, 2003.
% and also in
% Pierre Alliez, David Cohen-Steiner, Olivier Devillers, Bruno LeŽvy, and Mathieu Desbrun.
% Anisotropic Polygonal Remeshing.
% ACM Transactions on Graphics, 2003.
% Note: SIGGRAPH '2003 Conference Proceedings
%
% Copyright (c) 2007 Gabriel Peyre
orient = 1;
options.null = 0;
naver = getoptions(options, 'curvature_smoothing', 3);
verb = getoptions(options, 'verb', 1);
[V,F] = check_face_vertex(V,F);
n = size(V,2);
m = size(F,2);
% associate each edge to a pair of faces
Af = -triangulation2adjacency(F);
i = [F(1,:) F(2,:) F(3,:)];
j = [F(2,:) F(3,:) F(1,:)];
s = [1:m 1:m 1:m];
Af = sparse(i,j,s,n,n);
%% PATCH %%%
Af(Af>m) = 0;
%% END PATCH %%%
[i,j,s1] = find(Af); % direct link
[i,j,s2] = find(Af'); % reverse link
I = find( (s1>0) + (s2>0) == 2 );
% links edge->faces
E = [s1(I) s2(I)];
i = i(I); j = j(I);
% only directed edges
I = find(i<j);
E = E(I,:);
i = i(I); j = j(I);
ne = length(i); % number of directed edges
% normalized edge
e = V(:,j) - V(:,i);
d = sqrt(sum(e.^2,1));
e = e ./ repmat(d,3,1);
% avoid too large numerics
% d = d./mean(d);
% normals to faces
[tmp,normal] = compute_normal(V,F);
% inner product of normals
dp = sum( normal(:,E(:,1)) .* normal(:,E(:,2)), 1 );
% angle un-signed
beta = acos(clamp(dp,-1,1));
% sign
cp = crossp( normal(:,E(:,1))', normal(:,E(:,2))' )';
si = orient * sign( sum( cp.*e,1 ) );
% angle signed
beta = beta .* si;
% tensors
T = zeros(3,3,ne);
for x=1:3
for y=1:x
T(x,y,:) = reshape( e(x,:).*e(y,:), 1,1,ne );
T(y,x,:) = T(x,y,:);
end
end
T = T.*repmat( reshape(d.*beta,1,1,ne), [3,3,1] );
% T = T.*repmat( reshape(beta,1,1,ne), [3,3,1] );
% do pooling on vertices
Tv = zeros(3,3,n);
w = zeros(1,1,n);
for k=1:ne
% progressbar(k,ne);
Tv(:,:,i(k)) = Tv(:,:,i(k)) + T(:,:,k);
Tv(:,:,j(k)) = Tv(:,:,j(k)) + T(:,:,k);
w(:,:,i(k)) = w(:,:,i(k)) + 1;
w(:,:,j(k)) = w(:,:,j(k)) + 1;
end
w(w<eps) = 1;
Tv = Tv; % ./repmat(w,[3,3,1]);
%%
% Compute area around each V
% area of each face
a = V(:,F(3,:)) - V(:,F(1,:));
b = V(:,F(2,:)) - V(:,F(1,:));
ab = crossp(a',b')';
Af = sqrt(sum(ab.^2))/2;
% area of each vertex
m = size(F,2);
U = sparse( [1:m, 1:m, 1:m], [F(1,:) F(2,:) F(3,:)], [Af,Af,Af] );
Av = full(sum(U,1));
% normalize
Tv = Tv ./ repmat( reshape(Av,[1 1 n]), [3 3 1] );
% do averaging to smooth the field
options.niter_averaging = naver;
for x=1:3
for y=1:3
a = Tv(x,y,:);
a = perform_mesh_smoothing(F,V,a(:),options);
Tv(x,y,:) = reshape( a, 1,1,n );
end
end
% extract eigenvectors and eigenvalues
U = zeros(3,3,n);
D = zeros(3,n);
for k=1:n
if verb==1
progressbar(k,n);
end
[u,d] = eig(Tv(:,:,k));
d = real(diag(d));
% sort acording to [norma,min curv, max curv]
[tmp,I] = sort(abs(d));
D(:,k) = d(I);
U(:,:,k) = real(u(:,I));
end
Umin = squeeze(U(:,3,:));
Umax = squeeze(U(:,2,:));
Cmin = D(2,:)';
Cmax = D(3,:)';
Normal = squeeze(U(:,1,:));
Cmean = (Cmin+Cmax)/2;
Cgauss = Cmin.*Cmax;
% enforce than min<max
I = find(Cmin>Cmax);
Cmin1 = Cmin; Umin1 = Umin;
Cmin(I) = Cmax(I); Cmax(I) = Cmin1(I);
Umin(:,I) = Umax(:,I); Umax(:,I) = Umin1(:,I);
% try to re-orient the normals
normal = compute_normal(V,F);
s = sign( sum(Normal.*normal,1) );
Normal = Normal .* repmat(s, 3,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function z = crossp(x,y)
% x and y are (m,3) dimensional
z = x;
z(:,1) = x(:,2).*y(:,3) - x(:,3).*y(:,2);
z(:,2) = x(:,3).*y(:,1) - x(:,1).*y(:,3);
z(:,3) = x(:,1).*y(:,2) - x(:,2).*y(:,1);