-
Notifications
You must be signed in to change notification settings - Fork 2
/
projray2hypersphere.m
285 lines (235 loc) · 6.34 KB
/
projray2hypersphere.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
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
function [dataProj,facetPts,dataBary,facetIDs,tvals] = ...
projray2hypersphere(meshpts,facetPtIDs,datanorm,tol,maxnormQ,invmethod)
arguments
meshpts double
facetPtIDs double
datanorm double
tol(1,1) double = 1e-6
maxnormQ(1,1) logical = false
invmethod char {mustBeMember(invmethod,{'mldivide','pinv','extendedCross'})} = 'mldivide'
end
% PROJRAY2HYPERSPHERE project ray to hypersphere, compute barycentric coordinates, compute intersecting facet
%--------------------------------------------------------------------------
% Author: Sterling Baird
%
% Date: 2020-06-24
%
% Inputs:
%
% Outputs:
% facetIDs === list of facet IDs (i.e. rows of K triangulation)
% for which an intersection was found.
%
% Dependencies:
% numStabBary.m (if using invMethod == 'extendedCross')
%
% References:
% https://math.stackexchange.com/q/1256236/798661
% https://math.stackexchange.com/q/3731002
% https://github.com/tomilov/quickhull/issues/8
%
% Notes:
% To relax the requirement that pts need to be close to on the unit
% sphere, then there's a set of lines in projray2hypersphere.m that can
% be changed or removed.
%
% if (t(j) <= 0.1) || (t(j) >= 2.1)
% posQ(j) = 0;
% continue
% end
%--------------------------------------------------------------------------
% invmethod = 'mldivide'; %'pinv', 'mldivide', 'extendedCross'
adjSize = size(facetPtIDs);
%%
%setup vectors and matrices for ray projection
p = cell(adjSize);
nmatTemp = cell(adjSize);
nmat = p;
nvec = p;
ddet = zeros(adjSize(1),1);
dmat = cell(adjSize(1),1);
for j = 1:adjSize(1) %loop through facets
for k = 1:adjSize(2) %loop through vertices of facet
%package vertices
p{j,k} = meshpts(facetPtIDs(j,k),:);
end
%package vertex matrix
nmatTemp{j} = vertcat(p{j,:});
dmat{j} = nmatTemp{j};
for k = 1:adjSize(2)
nmat{j,k} = nmatTemp{j};
nmat{j,k}(:,k) = 1;
end
for k = 1:adjSize(2) %loop through dimensions
nvec{j}(k) = det(nmat{j,k});
end
ddet(j) = det(dmat{j});
end
%%
%project ray onto each facet
a = cell([adjSize(1),1]);
lambda = a; %initialize
posQ = zeros([adjSize(1),1]);
warnID1 = 'MATLAB:nearlySingularMatrix';
warnID2 = 'MATLAB:singularMatrix';
warnID3 = 'symbolic:mldivide:InconsistentSystem';
warning('off',warnID1)
warning('off',warnID2)
warning('off',warnID3)
for j = 1:adjSize(1)
if ddet(j) ~= 0
t(j) = ddet(j)/dot(datanorm,nvec{j});
tol2 = 1e-12;
if (t(j) <= tol2) || (t(j) >= 1/tol2)
posQ(j) = 0;
continue
end
a{j} = -datanorm*-t(j);
switch invmethod
case 'mldivide'
lambda{j} = (dmat{j}'\a{j}')'; %barycentric coordinates
posQ(j) = all(lambda{j} >= -tol) && ~any(lambda{j} == Inf) && (sum(lambda{j}) >= 1-tol); % && any(c{j} > 0)
case 'extendedCross'
%compute numerically stable barycentric coordinates
lambda{j} = numStabBary(nmatTemp{j},a{j});
% disp(lambda{j})
posQ(j) = all(lambda{j} >= -tol) && (sum(lambda{j}) >= 1-tol);
end
end
end
warning('on',warnID1)
warning('on',warnID2)
warning('on',warnID3)
posQtot = sum(posQ);
%%
% find facet that intersects ray and compile relevant data for datapoint
if posQtot > 0
idList = find(posQ > 0);
if (length(idList) >= 2) && maxnormQ
%disp('taking datapoint with largest norm')
[~, maxpos] = max(vecnorm(vertcat(a{idList})'));
id = idList(maxpos);
else
id = idList;
end
dataProj = a{id}; %datapoint projected onto intersecting facet
facetPts = vertcat(p{id,:}); %each row is a vertex
dataBary = lambda{id}; %barycentric datapoints (non-generalized)
facetIDs = id;
tvals = t(logical(posQ));
%disp(dataBary{i});
else
dataProj = [];
facetPts = [];
dataBary = [];
facetIDs = [];
tvals = [];
end
end %projray2hypersphere
%--------------------------CODE GRAVEYARD----------------------------------
%{
vpaQ = false;
if vpaQ
precision = 32;
end
waitbarQ = false;
if waitbarQ
disp([int2str(adjSize(1)) ' neighboring facets']);
f = waitbar(0,['looping through setup for ' int2str(adjSize(1)) ' neighboring facets']);
end
if vpaQ
t(j) = double(ddet(j)/dot(datanorm,nvec{j}));
else
t(j) = ddet(j)/dot(datanorm,nvec{j});
end
if ~strcmp(invmethod,'extendedCross')
warning('off',warnID1)
warning('off',warnID2)
warning('off',warnID3)
end
case 'pinv'
lambda{j} = (pinv(dmat{j}')*a{j}')';
posQ(j) = all(lambda{j} > 0) && (sum(lambda{j}) >= 1-tol);
%c{j} = (vpa(dmat{j})'\vpa(a{j})')'; %barycentric coordinates
if waitbarQ && (mod(j,100) == 0)
waitbar(j/adjSize(1),f);
end
if posQ(j) == 1
%test for warnings on the intersecting facet
warning('on',warnID1)
warning('on',warnID2)
warning('on',warnID3)
lastwarn('')
(dmat{j}'\a{j}')'; %barycentric coordinates
lstwarn = warning('query','last');
if strcmp(lstwarn,{warnID1,warnID2,warnID3})
posQ(j) = 0; %don't consider a point as intersecting a facet if one of the warnings triggers
end
end
if waitbarQ
close(f)
end
%turn warnings back on
warning('on',warnID1)
warning('on',warnID2)
warning('on',warnID3)
if posQtot == 0
%warning('no intersecting facet detected')
end
% info about multiple facets
% if sum(posQ) == 2
% %warning('two facets detected')
% disp(['common facets found: ' int2str(sum(posQ))])
% elseif sum(posQ) >= 3
% %warning('3+ facets detected')
% disp(['common facets found: ' int2str(sum(posQ))])
% end
%take max norm
% if length(idList) >= 2
% %disp('taking datapoint with largest norm')
% [~, maxpos] = max(vecnorm(vertcat(a{idList})'));
% id = idList(maxpos);
% else
% id = idList;
% end
for j = 1:adjSize(1) %loop through facets
for k = 1:adjSize(2) %loop through vertices of facet
p{j,k} = meshpts(facetPtIDs(j,k),:);
end
nmatTemp{j} = vertcat(p{j,:});
dmat{j} = nmatTemp{j};
for k = 1:adjSize(2)
nmat{j,k} = nmatTemp{j};
nmat{j,k}(:,k) = 1;
end
for k = 1:adjSize(2) %loop through dimensions
if vpaQ
nvec{j}(k) = det(vpa(nmat{j,k},precision));
else
nvec{j}(k) = det(nmat{j,k});
end
end
if vpaQ
ddet(j) = det(vpa(dmat{j},precision));
else
ddet(j) = det(dmat{j});
end
if waitbarQ && (mod(j,100) == 0)
waitbar(j/adjSize(1),f);
end
end
if waitbarQ
close(f);
end
if waitbarQ
f = waitbar(0,['looping through projection for ' int2str(adjSize(1)) ' neighboring facets']);
end
%take max norm
if length(idList) >= 2
%disp('taking datapoint with largest norm')
[~, maxpos] = max(vecnorm(vertcat(a{idList})'));
id = idList(maxpos);
else
id = idList;
end
%}