File: distancePointTriangle3d.m

package info (click to toggle)
octave-matgeom 1.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,584 kB
  • sloc: objc: 469; makefile: 10
file content (242 lines) | stat: -rw-r--r-- 7,522 bytes parent folder | download
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
## Copyright (C) 2024 David Legland
## All rights reserved.
## 
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
## 
##     1 Redistributions of source code must retain the above copyright notice,
##       this list of conditions and the following disclaimer.
##     2 Redistributions in binary form must reproduce the above copyright
##       notice, this list of conditions and the following disclaimer in the
##       documentation and/or other materials provided with the distribution.
## 
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
## 
## The views and conclusions contained in the software and documentation are
## those of the authors and should not be interpreted as representing official
## policies, either expressed or implied, of the copyright holders.

function [dist, proj] = distancePointTriangle3d(point, triangle)
%DISTANCEPOINTTRIANGLE3D Minimum distance between a 3D point and a 3D triangle.
%
%   DIST = distancePointTriangle3d(PT, TRI);
%   Computes the minimum distance between the point PT and the triangle
%   TRI. The Point PT is given as a row vector of three coordinates. The
%   triangle TRI is given as a 3-by-3 array containing the coordinates of
%   each vertex in a row of the array:
%   TRI = [...
%      X1 Y1 Z1;...
%      X2 Y2 Z2;...
%      X3 Y3 Z3];
%
%   [DIST, PROJ] = distancePointTriangle3d(PT, TRI);
%   Also returns the coordinates of the projeced point.
%
%   Example
%      tri = [1 0 0; 0 1 0;0 0 1];
%      pt = [0 0 0];
%      dist = distancePointTriangle3d(pt, tri)
%      dist =
%           0.5774
%
%   See also 
%   meshes3d, distancePointMesh, distancePointEdge3d, distancePointPlane
%
%   Reference
%   * David Eberly (1999), "Distance Between Point and Triangle in 3D"
%   https://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf
%   * see <a href="matlab:
%     web('https://fr.mathworks.com/matlabcentral/fileexchange/22857-distance-between-a-point-and-a-triangle-in-3d')
%   ">Distance between a point and a triangle in 3d</a>, by Gwendolyn Fischer.
%   (same algorithm, but different order of input argument)
%
%   * https://fr.mathworks.com/matlabcentral/fileexchange/22857-distance-between-a-point-and-a-triangle-in-3d

% ------
% Author: David Legland
% E-mail: david.legland@inrae.fr
% Created: 2018-03-08, using Matlab 9.3.0.713579 (R2017b)
% Copyright 2018-2023 INRA - Cepia Software Platform

% triangle origin and vectors
p1 = triangle(1,:);
v12 = triangle(2,:) - p1;
v13 = triangle(3,:) - p1;

% identify coefficients of second order equation
a = dot(v12, v12, 2);
b = dot(v12, v13, 2);
c = dot(v13, v13, 2);
diffP = p1 - point;
d = dot(v12, diffP, 2);
e = dot(v13, diffP, 2);
% f = dot(diffP, diffP, 2);

% compute position of projected point in the plane of the triangle
det = a * c - b * b ;
s = b * e - c * d ;
t = b * d - a * e ;

% switch depending on the region where the projection occur
if s + t < det
    if s < 0
        if t < 0
            % region 4
            % The minimum distance must occur 
            % * on the line t = 0
            % * on the line s = 0 with t >= 0
            % * at the intersection of the two lines
            
            if d < 0
                % minimum on edge t = 0 with s > 0.
                t = 0;
                if a <= -d
                    s = 1;
                else
                    s = -d / a;
                end
            else
                % minimum on edge s = 0
                s = 0;
                if e >= 0
                    t = 0;
                elseif c <= -e
                    t = 1;
                else
                    t = -e / c;
                end
            end
        else
            % region 3
            % The minimum distance must occur on the line s = 0
            s = 0;
            if e >= 0
                t = 0;
            else
                if c <= -e
                    t = 1;
                else
                    t = -e / c;
                end
            end
        end
        
    else
        if t < 0
            % region 5
            % The minimum distance must occur on the line t = 0
            t = 0;
            if d >= 0
                s = 0;
            else
                if a <= -d
                    s = 1;
                else
                    s = -d / a;
                end
            end
        else
            % region 0
            % the minimum distance occurs inside the triangle
            s = s / det;
            t = t / det;
        end
    end
else
    if s < 0
        % region 2
        % The minimum distance must occur:
        % * on the line s + t = 1
        % * on the line s = 0 with t <= 1
        % * or at the intersection of the two (s=0; t=1)
        
        tmp0 = b + d;
        tmp1 = c + e;
        
        if tmp1 > tmp0
            % minimum on edge s+t = 1, with s > 1
            numer = tmp1 - tmp0;
            denom = a - 2 * b + c;
            if numer >= denom
                s = 1;
            else
                s = numer / denom;
            end
            t = 1 - s;
        else
            % minimum on edge s = 0, with t <= 1
            s = 0;
            if tmp1 <= 0
                t = 1;
            elseif e >= 0
                t = 0;
            else
                t = -e / c;
            end
        end
        
    elseif t < 0
        % region 6
        % The minimum distance must occur
        % * on the line s + t = 1
        % * on the line t = 0, with s <= 1
        % * at the intersection of the two lines
        tmp0 = b + e;
        tmp1 = a + d;
        
        if tmp1 > tmp0 
            % minimum on edge s+t=1, with t > 0
            numer = tmp1 - tmp0;
            denom = a - 2 * b + c;
            if numer > denom
                t = 1;
            else
                t = numer / denom;
            end
            s = 1 - t;
            
        else
            % minimum on edge t = 0 with s <= 1
            t = 0;
            if tmp1 <= 0
                s = 1;
            elseif d >= 0
                s = 0;
            else
                s = -d / a;
            end
        end
        
    else
        % region 1
        % The minimum distance must occur on the line s + t = 1
        numer = (c + e) - (b + d);
        if numer <= 0
            s = 0;
        else
            denom = a - 2 * b + c;
            if numer >= denom
                s = 1;
            else
                s = numer / denom;
            end
        end
        
        t = 1 - s;
    end
end

% compute coordinates of closest point on plane
proj = p1 + s * v12 + t * v13;

% distance between point and closest point on plane
dist = sqrt(sum((point - proj).^2));