-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointTriangleDistance.m
executable file
·287 lines (271 loc) · 6.58 KB
/
pointTriangleDistance.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
286
287
function [dist,PP0] = pointTriangleDistance(TRI,P)
% calculate distance between a point and a triangle in 3D
% SYNTAX
% dist = pointTriangleDistance(TRI,P)
% [dist,PP0] = pointTriangleDistance(TRI,P)
%
% DESCRIPTION
% Calculate the distance of a given point P from a triangle TRI.
% Point P is a row vector of the form 1x3. The triangle is a matrix
% formed by three rows of points TRI = [P1;P2;P3] each of size 1x3.
% dist = pointTriangleDistance(TRI,P) returns the distance of the point P
% to the triangle TRI.
% [dist,PP0] = pointTriangleDistance(TRI,P) additionally returns the
% closest point PP0 to P on the triangle TRI.
%
% Author: Gwendolyn Fischer
% Release: 1.0
% Release date: 09/02/02
% Release: 1.1 Fixed Bug because of normalization
% Release: 1.2 Fixed Bug because of typo in region 5 20101013
% Release: 1.3 Fixed Bug because of typo in region 2 20101014
% Possible extention could be a version tailored not to return the distance
% and additionally the closest point, but instead return only the closest
% point. Could lead to a small speed gain.
% Example:
% %% The Problem
% P0 = [0.5 -0.3 0.5];
%
% P1 = [0 -1 0];
% P2 = [1 0 0];
% P3 = [0 0 0];
%
% vertices = [P1; P2; P3];
% faces = [1 2 3];
%
% %% The Engine
% [dist,PP0] = pointTriangleDistance([P1;P2;P3],P0);
%
% %% Visualization
% [x,y,z] = sphere(20);
% x = dist*x+P0(1);
% y = dist*y+P0(2);
% z = dist*z+P0(3);
%
% figure
% hold all
% patch('Vertices',vertices,'Faces',faces,'FaceColor','r','FaceAlpha',0.8);
% plot3(P0(1),P0(2),P0(3),'b*');
% plot3(PP0(1),PP0(2),PP0(3),'*g')
% surf(x,y,z,'FaceColor','b','FaceAlpha',0.3)
% view(3)
% The algorithm is based on
% "David Eberly, 'Distance Between Point and Triangle in 3D',
% Geometric Tools, LLC, (1999)"
% http:\\www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf
%
% ^t
% \ |
% \reg2|
% \ |
% \ |
% \ |
% \|
% *P2
% |\
% | \
% reg3 | \ reg1
% | \
% |reg0\
% | \
% | \ P1
% -------*-------*------->s
% |P0 \
% reg4 | reg5 \ reg6
%% Do some error checking
if nargin<2
error('pointTriangleDistance: too few arguments see help.');
end
P = P(:)';
if size(P,2)~=3
error('pointTriangleDistance: P needs to be of length 3.');
end
if size(TRI)~=[3 3]
error('pointTriangleDistance: TRI needs to be of size 3x3.');
end
% ToDo: check for colinearity and/or too small triangles.
% rewrite triangle in normal form
B = TRI(1,:);
E0 = TRI(2,:)-B;
%E0 = E0/sqrt(sum(E0.^2)); %normalize vector
E1 = TRI(3,:)-B;
%E1 = E1/sqrt(sum(E1.^2)); %normalize vector
D = B - P;
a = dot(E0,E0);
b = dot(E0,E1);
c = dot(E1,E1);
d = dot(E0,D);
e = dot(E1,D);
f = dot(D,D);
det = a*c - b*b; % do we have to use abs here?
s = b*e - c*d;
t = b*d - a*e;
% Terible tree of conditionals to determine in which region of the diagram
% shown above the projection of the point into the triangle-plane lies.
if (s+t) <= det
if s < 0
if t < 0
%region4
if (d < 0)
t = 0;
if (-d >= a)
s = 1;
sqrDistance = a + 2*d + f;
else
s = -d/a;
sqrDistance = d*s + f;
end
else
s = 0;
if (e >= 0)
t = 0;
sqrDistance = f;
else
if (-e >= c)
t = 1;
sqrDistance = c + 2*e + f;
else
t = -e/c;
sqrDistance = e*t + f;
end
end
end %of region 4
else
% region 3
s = 0;
if e >= 0
t = 0;
sqrDistance = f;
else
if -e >= c
t = 1;
sqrDistance = c + 2*e +f;
else
t = -e/c;
sqrDistance = e*t + f;
end
end
end %of region 3
else
if t < 0
% region 5
t = 0;
if d >= 0
s = 0;
sqrDistance = f;
else
if -d >= a
s = 1;
sqrDistance = a + 2*d + f;% GF 20101013 fixed typo d*s ->2*d
else
s = -d/a;
sqrDistance = d*s + f;
end
end
else
% region 0
invDet = 1/det;
s = s*invDet;
t = t*invDet;
sqrDistance = s*(a*s + b*t + 2*d) ...
+ t*(b*s + c*t + 2*e) + f;
end
end
else
if s < 0
% region 2
tmp0 = b + d;
tmp1 = c + e;
if tmp1 > tmp0 % minimum on edge s+t=1
numer = tmp1 - tmp0;
denom = a - 2*b + c;
if numer >= denom
s = 1;
t = 0;
sqrDistance = a + 2*d + f; % GF 20101014 fixed typo 2*b -> 2*d
else
s = numer/denom;
t = 1-s;
sqrDistance = s*(a*s + b*t + 2*d) ...
+ t*(b*s + c*t + 2*e) + f;
end
else % minimum on edge s=0
s = 0;
if tmp1 <= 0
t = 1;
sqrDistance = c + 2*e + f;
else
if e >= 0
t = 0;
sqrDistance = f;
else
t = -e/c;
sqrDistance = e*t + f;
end
end
end %of region 2
else
if t < 0
%region6
tmp0 = b + e;
tmp1 = a + d;
if (tmp1 > tmp0)
numer = tmp1 - tmp0;
denom = a-2*b+c;
if (numer >= denom)
t = 1;
s = 0;
sqrDistance = c + 2*e + f;
else
t = numer/denom;
s = 1 - t;
sqrDistance = s*(a*s + b*t + 2*d) ...
+ t*(b*s + c*t + 2*e) + f;
end
else
t = 0;
if (tmp1 <= 0)
s = 1;
sqrDistance = a + 2*d + f;
else
if (d >= 0)
s = 0;
sqrDistance = f;
else
s = -d/a;
sqrDistance = d*s + f;
end
end
end
%end region 6
else
% region 1
numer = c + e - b - d;
if numer <= 0
s = 0;
t = 1;
sqrDistance = c + 2*e + f;
else
denom = a - 2*b + c;
if numer >= denom
s = 1;
t = 0;
sqrDistance = a + 2*d + f;
else
s = numer/denom;
t = 1-s;
sqrDistance = s*(a*s + b*t + 2*d) ...
+ t*(b*s + c*t + 2*e) + f;
end
end %of region 1
end
end
end
% account for numerical round-off error
if (sqrDistance < 0)
sqrDistance = 0;
end
dist = sqrt(sqrDistance);
if nargout>1
PP0 = B + s*E0 + t*E1;
end