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 % INRA - CEPIA URPOI - MIA MathCell 0024 % created the 07/04/2004. 0025 % 0026 0027 % HISTORY 0028 % 2005-06-24 rename, and change arguments sequence 0029 % 2009-04-30 add possibility to return position of closest point 0030 % 2011-04-14 add checkup for degenerate edges, improve speed, update doc 0031 0032 % direction vector of each edge 0033 vl = edge(:, 4:6) - edge(:, 1:3); 0034 0035 % compute position of points projected on the supporting line 0036 % (Size of t is the max number of edges or points) 0037 t = linePosition3d(point, [edge(:,1:3) vl]); 0038 0039 % change position to ensure projected point is located on the edge 0040 t(t < 0) = 0; 0041 t(t > 1) = 1; 0042 0043 % difference of coordinates between projected point and base point 0044 p0 = bsxfun(@plus, edge(:,1:3), [t .* vl(:,1) t .* vl(:,2) t .* vl(:,3)]); 0045 p0 = bsxfun(@minus, point, p0); 0046 0047 % compute distance between point and its projection on the edge 0048 dist = sqrt(sum(p0 .* p0, 2));