-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExactMinBoundSphere3D.m
183 lines (153 loc) · 4.47 KB
/
ExactMinBoundSphere3D.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
function [R, C, Xb]=ExactMinBoundSphere3D(X)
% Compute exact minimum bounding sphere of a 3D point cloud (or a
% triangular surface mesh) using Welzl's algorithm [1].
%
% INPUT:
% - X : M-by-3 list of point co-ordinates OR a triangular surface
% mesh specified as a (i) 'TriRep' object, (ii) 'triangulation'
% object, (iii) 1-by-2 cell such that TR={Tri,V}, where Tri is
% an M-by-3 array of faces and V is an N-by-3 array of vertex
% coordinates, (iv) structure with 'faces' and 'vertices'
% fields, such as the one returned by the 'isosurface'
% function.
%
% OUTPUT:
% - R : radius of the minimum bounding sphere of X.
% - C : 1-by-3 vector specifying centroid co-ordinates of the
% minimum bounding sphere of X.
% - Xb : K-by-3 array of point co-ordinates from which R and C were
% computed; 2<=K<=4. Xb is a subset of X. See function
% 'FitSphere2Points' for more info.
%
% REREFERENCES:
% [1] Welzl, E. (1991), 'Smallest enclosing disks (balls and ellipsoids)',
% Lecture Notes in Computer Science, Vol. 555, pp. 359-370
%
% AUTHOR: Anton Semechko (a.semechko@gmail.com)
%
if isnumeric(X) && ismatrix(X)
if sum(~isfinite(X(:)))>0
error('Point coordinates contain NaN or Inf entries. Remove them and try again.')
elseif size(X,2)~=3
error('This function works only for 3D data')
end
else
try
% Input may be a mesh
[~,X]=GetMeshData(X);
if size(X,2)==2
[R,C,Xb]=ExactMinBoundCircle(X);
return
end
catch
error('Invalid format for 1st input argument (X)')
end
end
% Get minimum bounding sphere
% -------------------------------------------------------------------------
if size(X,1)>3
% Check points for co-planarity
Xo=mean(X,1);
dX=bsxfun(@minus,X,Xo);
[U,D]=svd((dX'*dX)/size(X,1),0);
D=diag(D);
if D(3)<1E-15
[R,C,Xb]=ExactMinBoundCircle(dX*U(:,1:2));
C=(U(:,1:2)*C')'+Xo;
Xb=bsxfun(@plus,(U(:,1:2)*Xb')',Xo);
return
end
% Get convex hull of the point set
F=convhull(X);
F=unique(F(:));
X=X(F,:);
end
try
% Remove duplicates
X=uniquetol(X,eps,'ByRows',true);
catch
% older version of Matlab; 'uniquetol' is unavailable
end
if size(X,1)<4
[R,C]=FitSphere2Points(X);
Xb=X;
return
end
% Randomly permute the point set
idx=randperm(size(X,1));
X=X(idx(:),:);
if size(X,1)<1E3
try
% Center and radius of the sphere
[R,C]=B_MinSphere(X,[]);
% Coordinates of the points used to compute parameters of the
% minimum bounding sphere
D=sum(bsxfun(@minus,X,C).^2,2);
[D,idx]=sort(abs(D-R^2));
Xb=X(idx(1:4),:);
D=D(1:4);
Xb=Xb(D/R*100<1E-3,:);
[~,idx]=sort(Xb(:,1));
Xb=Xb(idx,:);
return
catch
end
end
% If we got to this point, then either size(X,1)>=1E3 or recursion depth
% limit was reached. So need to break-up point-set into smaller sets and
% then recombine the results.
M=size(X,1);
dM=max(min(floor(M/4),300),4);
res=mod(M,dM);
n=ceil(M/dM);
idx=dM*ones(1,n);
if res>0, idx(end)=res; end
if res<0.25*dM && res>0
idx(n-1)=idx(n-1)+idx(n);
idx(n)=[];
n=n-1;
end
X=mat2cell(X,idx,3);
Xb=[];
for i=1:n
% Center and radius of the sphere
[R,C,Xi]=B_MinSphere([Xb;X{i}],[]);
% 40 points closest to the sphere
if i<n
D=abs(sum(bsxfun(@minus,Xi,C).^2,2)-R^2);
else
D=abs(sqrt(sum(bsxfun(@minus,Xi,C).^2,2))-R);
end
[D,idx]=sort(D);
Xb=Xi(idx(1:min(40,numel(D))),:);
end
% Points on the bounding sphere
D=D(1:min(40,numel(D)));
Xb=Xb(D/R*100<1E-3,:);
if size(Xb,1)>4,Xb=Xb(1:4,:); end
[~,idx]=sort(Xb(:,1));
Xb=Xb(idx,:);
function [R,C,P]=B_MinSphere(P,B)
if size(B,1)==4 || isempty(P)
[R,C]=FitSphere2Points(B); % fit sphere to boundary points (B)
return
end
% Remove the last (i.e., end) point, p, from the P list
P_new=P;
P_new(end,:)=[];
p=P(end,:);
% Check if p is on or inside the bounding sphere. If not, it must be
% part of the new boundary.
[R,C,P_new]=B_MinSphere(P_new,B);
if isnan(R) || isinf(R) || R<=eps
chk=true;
else
chk=norm(p-C)>(R+eps);
end
if chk
B=[p;B];
[R,C]=B_MinSphere(P_new,B);
P=[p;P_new];
end
end
end