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
0001 function [dist, proj] = distancePointTriangle3d(point, triangle) 0002 %DISTANCEPOINTTRIANGLE3D Minimum distance between a 3D point and a 3D triangle. 0003 % 0004 % DIST = distancePointTriangle3d(PT, TRI); 0005 % Computes the minimum distance between the point PT and the triangle 0006 % TRI. The Point PT is given as a row vector of three coordinates. The 0007 % triangle TRI is given as a 3-by-3 array containing the coordinates of 0008 % each vertex in a row of the array: 0009 % TRI = [... 0010 % X1 Y1 Z1;... 0011 % X2 Y2 Z2;... 0012 % X3 Y3 Z3]; 0013 % 0014 % [DIST, PROJ] = distancePointTriangle3d(PT, TRI); 0015 % Also returns the coordinates of the projeced point. 0016 % 0017 % Example 0018 % tri = [1 0 0; 0 1 0;0 0 1]; 0019 % pt = [0 0 0]; 0020 % dist = distancePointTriangle3d(pt, tri) 0021 % dist = 0022 % 0.5774 0023 % 0024 % See also 0025 % meshes3d, distancePointMesh, distancePointEdge3d, distancePointPlane 0026 % 0027 % Reference 0028 % * David Eberly (1999), "Distance Between Point and Triangle in 3D" 0029 % https://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf 0030 % * see <a href="matlab: 0031 % web('https://fr.mathworks.com/matlabcentral/fileexchange/22857-distance-between-a-point-and-a-triangle-in-3d') 0032 % ">Distance between a point and a triangle in 3d</a>, by Gwendolyn Fischer. 0033 % (same algorithm, but different order of input argument) 0034 % 0035 % * https://fr.mathworks.com/matlabcentral/fileexchange/22857-distance-between-a-point-and-a-triangle-in-3d 0036 0037 % ------ 0038 % Author: David Legland 0039 % E-mail: david.legland@inrae.fr 0040 % Created: 2018-03-08, using Matlab 9.3.0.713579 (R2017b) 0041 % Copyright 2018-2024 INRA - Cepia Software Platform 0042 0043 % triangle origin and vectors 0044 p1 = triangle(1,:); 0045 v12 = triangle(2,:) - p1; 0046 v13 = triangle(3,:) - p1; 0047 0048 % identify coefficients of second order equation 0049 a = dot(v12, v12, 2); 0050 b = dot(v12, v13, 2); 0051 c = dot(v13, v13, 2); 0052 diffP = p1 - point; 0053 d = dot(v12, diffP, 2); 0054 e = dot(v13, diffP, 2); 0055 % f = dot(diffP, diffP, 2); 0056 0057 % compute position of projected point in the plane of the triangle 0058 det = a * c - b * b ; 0059 s = b * e - c * d ; 0060 t = b * d - a * e ; 0061 0062 % switch depending on the region where the projection occur 0063 if s + t < det 0064 if s < 0 0065 if t < 0 0066 % region 4 0067 % The minimum distance must occur 0068 % * on the line t = 0 0069 % * on the line s = 0 with t >= 0 0070 % * at the intersection of the two lines 0071 0072 if d < 0 0073 % minimum on edge t = 0 with s > 0. 0074 t = 0; 0075 if a <= -d 0076 s = 1; 0077 else 0078 s = -d / a; 0079 end 0080 else 0081 % minimum on edge s = 0 0082 s = 0; 0083 if e >= 0 0084 t = 0; 0085 elseif c <= -e 0086 t = 1; 0087 else 0088 t = -e / c; 0089 end 0090 end 0091 else 0092 % region 3 0093 % The minimum distance must occur on the line s = 0 0094 s = 0; 0095 if e >= 0 0096 t = 0; 0097 else 0098 if c <= -e 0099 t = 1; 0100 else 0101 t = -e / c; 0102 end 0103 end 0104 end 0105 0106 else 0107 if t < 0 0108 % region 5 0109 % The minimum distance must occur on the line t = 0 0110 t = 0; 0111 if d >= 0 0112 s = 0; 0113 else 0114 if a <= -d 0115 s = 1; 0116 else 0117 s = -d / a; 0118 end 0119 end 0120 else 0121 % region 0 0122 % the minimum distance occurs inside the triangle 0123 s = s / det; 0124 t = t / det; 0125 end 0126 end 0127 else 0128 if s < 0 0129 % region 2 0130 % The minimum distance must occur: 0131 % * on the line s + t = 1 0132 % * on the line s = 0 with t <= 1 0133 % * or at the intersection of the two (s=0; t=1) 0134 0135 tmp0 = b + d; 0136 tmp1 = c + e; 0137 0138 if tmp1 > tmp0 0139 % minimum on edge s+t = 1, with s > 1 0140 numer = tmp1 - tmp0; 0141 denom = a - 2 * b + c; 0142 if numer >= denom 0143 s = 1; 0144 else 0145 s = numer / denom; 0146 end 0147 t = 1 - s; 0148 else 0149 % minimum on edge s = 0, with t <= 1 0150 s = 0; 0151 if tmp1 <= 0 0152 t = 1; 0153 elseif e >= 0 0154 t = 0; 0155 else 0156 t = -e / c; 0157 end 0158 end 0159 0160 elseif t < 0 0161 % region 6 0162 % The minimum distance must occur 0163 % * on the line s + t = 1 0164 % * on the line t = 0, with s <= 1 0165 % * at the intersection of the two lines 0166 tmp0 = b + e; 0167 tmp1 = a + d; 0168 0169 if tmp1 > tmp0 0170 % minimum on edge s+t=1, with t > 0 0171 numer = tmp1 - tmp0; 0172 denom = a - 2 * b + c; 0173 if numer > denom 0174 t = 1; 0175 else 0176 t = numer / denom; 0177 end 0178 s = 1 - t; 0179 0180 else 0181 % minimum on edge t = 0 with s <= 1 0182 t = 0; 0183 if tmp1 <= 0 0184 s = 1; 0185 elseif d >= 0 0186 s = 0; 0187 else 0188 s = -d / a; 0189 end 0190 end 0191 0192 else 0193 % region 1 0194 % The minimum distance must occur on the line s + t = 1 0195 numer = (c + e) - (b + d); 0196 if numer <= 0 0197 s = 0; 0198 else 0199 denom = a - 2 * b + c; 0200 if numer >= denom 0201 s = 1; 0202 else 0203 s = numer / denom; 0204 end 0205 end 0206 0207 t = 1 - s; 0208 end 0209 end 0210 0211 % compute coordinates of closest point on plane 0212 proj = p1 + s * v12 + t * v13; 0213 0214 % distance between point and closest point on plane 0215 dist = sqrt(sum((point - proj).^2));