DISTANCEPOINTEDGE3D Minimum distance between a 3D point and a 3D edge. DIST = distancePointEdge3d(POINT, EDGE); Return the euclidean distance between edge EDGE and point POINT. EDGE has the form: [x1 y1 z1 x2 y2 z2], and POINT is [x y z]. If EDGE is N-by-6 array, result is N-by-1 array computed for each edge. If POINT is a N-by-3 array, the result is computed for each point. If both POINT and EDGE are array, they must have the same number of rows, and the result is computed for each couple point(i,:);edge(i,:). [DIST POS] = distancePointEdge3d(POINT, EDGE); Also returns the position of closest point on the edge. POS is comprised between 0 (first point) and 1 (last point). See also edges3d, points3d, distancePoints3d, distancePointLine3d
0001 function [dist, t] = distancePointEdge3d(point, edge) 0002 %DISTANCEPOINTEDGE3D Minimum distance between a 3D point and a 3D edge. 0003 % 0004 % DIST = distancePointEdge3d(POINT, EDGE); 0005 % Return the euclidean distance between edge EDGE and point POINT. 0006 % EDGE has the form: [x1 y1 z1 x2 y2 z2], and POINT is [x y z]. 0007 % 0008 % If EDGE is N-by-6 array, result is N-by-1 array computed for each edge. 0009 % If POINT is a N-by-3 array, the result is computed for each point. 0010 % If both POINT and EDGE are array, they must have the same number of 0011 % rows, and the result is computed for each couple point(i,:);edge(i,:). 0012 % 0013 % [DIST POS] = distancePointEdge3d(POINT, EDGE); 0014 % Also returns the position of closest point on the edge. POS is 0015 % comprised between 0 (first point) and 1 (last point). 0016 % 0017 % See also 0018 % edges3d, points3d, distancePoints3d, distancePointLine3d 0019 % 0020 0021 % ------ 0022 % Author: David Legland 0023 % E-mail: david.legland@inrae.fr 0024 % Created: 2004-04-07 0025 % Copyright 2004-2024 INRA - CEPIA URPOI - MIA MathCell 0026 0027 % direction vector of each edge 0028 vl = edge(:, 4:6) - edge(:, 1:3); 0029 0030 % compute position of points projected on the supporting line 0031 % (Size of t is the max number of edges or points) 0032 t = line3dPosition(point, [edge(:,1:3) vl]); 0033 0034 % change position to ensure projected point is located on the edge 0035 t(t < 0) = 0; 0036 t(t > 1) = 1; 0037 0038 % difference of coordinates between projected point and base point 0039 p0 = bsxfun(@plus, edge(:,1:3), [t .* vl(:,1) t .* vl(:,2) t .* vl(:,3)]); 0040 p0 = bsxfun(@minus, point, p0); 0041 0042 % compute distance between point and its projection on the edge 0043 dist = sqrt(sum(p0 .* p0, 2));