-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathestimate_gradient.m
More file actions
386 lines (312 loc) · 11.7 KB
/
estimate_gradient.m
File metadata and controls
386 lines (312 loc) · 11.7 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
function grad_v = estimate_gradient(varargin)
% [areas_vols,grad,grad_v,grad_v_norm]
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
% Replacing `estimate_grad_fun` and `estimate_grad_crit`
% nargin = 2; % 3
T = varargin{1};
samples = varargin{2};
if nargin == 3
criticality = varargin{3};
end
ndims = size(samples,2)-1;
n_triang = size(T,1); % number of triangles
% x = samples(:,1);
% y = samples(:,2);
% z = samples(:,3);
%
% TR = triangulation(T.ConnectivityList,x,y,z);
% F = featureEdges(TR,pi/6)';
% figure
% plot3(x(F),y(F),z(F),'k');
% Precompute value sources based on nargin
if nargin == 2
% Gradient of the function values
get_value = @(vertex_idx) samples(vertex_idx, ndims+1); % get from samples
elseif nargin == 3
% Gradient of the criticalities
get_value = @(vertex_idx) criticality(vertex_idx); % get from criticality
end
if ndims == 2
% samples = [x y value]
vertices = T.ConnectivityList;
% Extract coordinates of vertices (i, j, k) for all triangles
% It is possible to change samples to T.Points
i_coords = samples(vertices(:,1), 1:ndims); % [n_triang x ndims]
j_coords = samples(vertices(:,2), 1:ndims); % [n_triang x ndims]
k_coords = samples(vertices(:,3), 1:ndims); % [n_triang x ndims]
% Extract function/criticality values for all vertices
i_f = get_value(vertices(:,1)); % [n_triang x 1]
j_f = get_value(vertices(:,2)); % [n_triang x 1]
k_f = get_value(vertices(:,3)); % [n_triang x 1]
% Compute edge vectors (i.e., triangle sides)
ik = i_coords - k_coords; % [n_triang x ndims]
ji = j_coords - i_coords; % [n_triang x ndims]
% Compute triangle areas
areas = 0.5 * ...
abs((j_coords(:,1) - i_coords(:,1)) .* (k_coords(:,2) - i_coords(:,2)) - ...
(k_coords(:,1) - i_coords(:,1)) .* (j_coords(:,2) - i_coords(:,2))); % [n_triang x 1]
% Alternative way
% area_t = 0.5 * abs(i(1)*j(2) + j(1)*k(2) + k(1)*i(2) - j(1)*i(2) - k(1)*j(2) - i(1)*k(2));
% Rotation matrix for 90 degrees
rotation_matrix = [0 -1; 1 0]; % 90-degree rotation in 2D
% Rotate vectors using the rotation matrix
ikr = ik * rotation_matrix'; % Rotate ik (results in [n_triang x 2])
jir = ji * rotation_matrix'; % Rotate ji (results in [n_triang x 2])
% ang = (pi/180) * 90; % 90 degrees
% % NORM IS THE SAME AFTER ROTATION
% % Rotate vector 1
% [th,r] = cart2pol(ik(1),ik(2));
% [xr1,yr1] = pol2cart(th+ang, r);
% % Rotate vector 2
% [th,r] = cart2pol(ji(1),ji(2));
% [xr2,yr2] = pol2cart(th+ang, r);
%
% ikr = [xr1 yr1];
% jir = [xr2 yr2];
grad = (j_f - i_f) .* (ikr ./ (2 * areas)) + ...
(k_f - i_f) .* (jir ./ (2 * areas));
% disp(grad)
elseif ndims == 3
% volums = zeros(n_triang,1);
grad = zeros(n_triang,ndims);
% samples = [x y z value]
vertices = T.ConnectivityList;
% Extract coordinates of vertices (i, j, k) for all triangles
% It is possible to change samples to T.Points
i_coords = samples(vertices(:,1), 1:ndims); % [n_triang x ndims]
j_coords = samples(vertices(:,2), 1:ndims); % [n_triang x ndims]
k_coords = samples(vertices(:,3), 1:ndims); % [n_triang x ndims]
h_coords = samples(vertices(:,4), 1:ndims); % [n_triang x ndims]
% Extract function/criticality values for all vertices
i_f = get_value(vertices(:,1)); % [n_triang x 1]
j_f = get_value(vertices(:,2)); % [n_triang x 1]
k_f = get_value(vertices(:,3)); % [n_triang x 1]
h_f = get_value(vertices(:,4)); % [n_triang x 1]
% Compute edge vectors (i.e., triangle sides)
ji = j_coords - i_coords; % [n_triang x ndims]
ki = k_coords - i_coords; % [n_triang x ndims]
hi = h_coords - i_coords; % [n_triang x ndims]
ik = i_coords - k_coords;
hk = h_coords - k_coords;
ih = i_coords - h_coords;
jh = j_coords - h_coords;
ji_f = j_f - i_f;
ki_f = k_f - i_f;
hi_f = h_f - i_f;
% Number of triangles
parfor t = 1:n_triang
volum_t = (1/6) * abs(det([ji(t,:); ki(t,:); hi(t,:)]));
if volum_t ~= 0
% volums(t) = volum_t;
grad(t,:) = (ji_f(t)) * (cross(ik(t,:),hk(t,:)) / (2*volum_t)) + ...
(ki_f(t)) * (cross(ih(t,:),jh(t,:)) / (2*volum_t)) + ...
(hi_f(t)) * (cross(ki(t,:),ji(t,:)) / (2*volum_t));
% If the volume is 0, we leave the gradient in the tetrahedron
% with value 0
% because the solid angle will be 0 (or practically 0)
% anyway and we can ignore its value
% When the 4 points are on the same plane,
% the volume could be 0 (det=0)
% % Example:
% P1 = [1.8000, 0.9500, 1.1000];
% P2 = [1.6500, 0.9500, 0.9000];
% P3 = [1.8500, 0.9000, 0.7500];
% P4 = [2.0000, 0.9000, 0.9500];
%
% X = [1.8000, 1.6500, 1.8500, 2.0000];
% Y = [0.9500, 0.9500, 0.9000, 0.9000];
% Z = [1.1000, 0.9000, 0.7500, 0.9500];
%
% % Points
% scatter3(X,Y,Z,'.','LineWidth',100)
% xlim([1.5, 2]);
% ylim([0.8, 1]);
% zlim([0.6, 1.2]);
%
% % Vectors
% PA = [P1;P2];
% PB = [P1;P3];
% PC = [P1;P4];
%
% plot3(PA(:,1),PA(:,2),PA(:,3),'r','LineWidth',2)
% hold on
% plot3(PB(:,1),PB(:,2),PB(:,3),'r','LineWidth',2)
% hold on
% plot3(PC(:,1),PC(:,2),PC(:,3),'r','LineWidth',2)
% grid on
% xlim([1.5, 2]);
% ylim([0.8, 1]);
% zlim([0.6, 1.2]);
end
end % [parfor]
% disp(grad)
end
% % TO RETURN
% if ndims == 2
% areas_vols = areas;
% elseif ndims == 3
% areas_vols = volums;
% end
% PLOT
% if ndims == 2
% for p = 1:size(T.Points,1)
% figure
% triplot(T)
% hold on
% triplot(T(V{p,:},:),samples(:,1),samples(:,2),'Color','r')
% hold on
% plot(T.Points(p,1),T.Points(p,2),'k.','MarkerSize',12)
% hold off
% end
% elseif ndims == 3
% for p = 1:size(T.Points,1)
% figure
% tetramesh(T,'FaceAlpha',0.3)
% hold on
% tetramesh(T(V{p,:},:),samples(:,1),samples(:,2),samples(:,3),'Color','r')
% hold on
% plot(T.Points(p,1),T.Points(p,2),T.Points(p,3),'k.','MarkerSize',12)
% hold off
% end
% end
%% AVERAGE GRADIENT ON STAR (AGS)
% Compute star (neighbourhood)
V = vertexAttachments(T);
% V{p,:}
% Extract points once for reuse
points = T.Points;
grad_v = zeros(size(points,1),ndims);
% grad_v_norm = zeros(size(points,1),1);
% % Create a reduced version of T.ConnectivityList for each vertex
% vertex_to_triangles = cell(size(points,1), 1);
% for p = 1:size(points,1)
% vertex_to_triangles{p} = T.ConnectivityList(V{p,:},:); % Extract relevant triangles
% end
if ndims == 2
% Number of vertex
for p = 1:size(points,1)
% Get triangle indices connected to vertex `p`
triangle_ids = V{p,:};
% Triangles
triangl = T(triangle_ids,:);
% Initialize storage for angles and gradient sums
num_triangles = size(triangl,1);
angles = zeros(num_triangles,1);
sum_grad = zeros(1,ndims);
% Loop over connected triangles to compute neighbours angles
for i = 1:num_triangles
tri_id = triangle_ids(i);
point_ids = triangl(i,:);
point_ids(point_ids==p) = [];
P1 = points(p,:);
P2 = points(point_ids(1),:);
P3 = points(point_ids(2),:);
% In radians
angle = atan2(2*areas(tri_id), dot(P2-P1,P3-P1));
% Convert to degrees
% angle = angle*180/pi;
% Accumulate weighted gradient
angles(i) = angle;
sum_grad = sum_grad + angle*grad(tri_id,:);
end
g = sum_grad/sum(angles); % The sum of the angles cannot be 0
% disp(g)
grad_v(p,:) = g;
% Euclidean norm
% grad_v_norm(p) = norm(g);
end
elseif ndims == 3
% Number of vertex
for p = 1:size(points,1)
% Get triangle indices connected to vertex `p`
triangle_ids = V{p,:};
% Triangles
triangl = T(triangle_ids,:);
% Initialize storage for angles and gradient sums
num_triangles = size(triangl,1);
angles = zeros(num_triangles,1);
sum_grad = zeros(1,ndims);
% Loop over connected triangles to compute neighbours angles
for i = 1:num_triangles
tri_id = triangle_ids(i);
point_ids = triangl(i,:);
point_ids(point_ids==p) = [];
P1 = points(p,:);
P2 = points(point_ids(1),:);
P3 = points(point_ids(2),:);
P4 = points(point_ids(3),:);
% In steradians
angle = abs(Solid_Angle_Triangle(P1,P2,P3,P4)); % without sign
% angle = solidangle(P2-P1,P3-P1,P4-P1);
% When the 3 edges forming the angle are on the same plane,
% the function 'solidangle' could return complex values
% % Example:
% P1 = [1.2000, 1.7000, 1.4000];
% P2 = [1.5000, 1.8000, 1.1000];
% P3 = [1.3000, 1.6000, 1.4000];
% P4 = [1.4000, 1.9000, 1.1000];
%
% X = [1.2000, 1.5000, 1.3000, 1.4000];
% Y = [1.7000, 1.8000, 1.6000, 1.9000];
% Z = [1.4000, 1.1000, 1.4000, 1.1000];
%
% % Points
% scatter3(X,Y,Z,'.','LineWidth',100)
% xlim([1, 1.6]);
% ylim([1.4, 2]);
% zlim([1, 1.6]);
%
% % Vectors
% PA = [P1;P2];
% PB = [P1;P3];
% PC = [P1;P4];
%
% plot3(PA(:,1),PA(:,2),PA(:,3),'r','LineWidth',2)
% hold on
% plot3(PB(:,1),PB(:,2),PB(:,3),'r','LineWidth',2)
% hold on
% plot3(PC(:,1),PC(:,2),PC(:,3),'r','LineWidth',2)
% grid on
% xlim([1, 1.6]);
% ylim([1.4, 2]);
% zlim([1, 1.6]);
% Accumulate weighted gradient
angles(i) = angle;
sum_grad = sum_grad + angle*grad(tri_id,:);
end
% To check (ndims == 3):
% - The sum of the angles of a vertex point must sum to (4*pi)/8 = 1.5708
% - The sum of an angle on one side of the cube must sum to (4*pi)/2 = 6.2832
% - The sum of an interior point must sum to 4*pi = 12.5664
% if ndims == 3
% if p<=8
% fprintf('corner: %f\n',sum(angles));
% elseif sum(angles)<12
% fprintf('point [%f, %f, %f]: %f\n',points(p,:),sum(angles));
% else
% fprintf('%f\n',sum(angles));
% end
% end
% The same could be done for 2 dimension
% Note that a center point has 4π sr.
% Therefore, a corner of the cube will have π/2 sr.
sa = sum(angles);
g = sum_grad/sa; % The sum of the angles cannot be 0
% disp(g)
% Condition to de-emphasize parameter space edges.
% If we are at an edge, we reduce the gradient value by 2 %%%%%%%% CHECK
if sa < 4*pi
grad_v(p,:) = g/2;
else
grad_v(p,:) = g;
end
% Euclidean norm
% grad_v_norm(p) = norm(g);
end
end
% CHECK:
% faceNormal
% nearestNeighbor
% vertexNormal
end