DISTANCEPOINTS3D Compute euclidean distance between pairs of 3D Points. D = distancePoints3d(P1, P2) return distance between points P1 and P2, given as [X Y Z]. If P1 and P2 are two arrays of points, result is a N1*N2 array containing distance between each point of P1 and each point of P2. D = distancePoints3d(P1, P2, NOR) with NOR being 1, 2, or Inf, corresponfing to the norm used. Default is 2 (euclidean norm). 1 correspond to manhattan (or taxi driver) distance and Inf to maximum difference in each coordinate. See also: points3d, minDistancePoints, distancePoints --------- author : David Legland INRA - TPV URPOI - BIA IMASTE created the 18/02/2005.
0001 function dist = distancePoints3d(p1, p2, varargin) 0002 %DISTANCEPOINTS3D Compute euclidean distance between pairs of 3D Points. 0003 % 0004 % D = distancePoints3d(P1, P2) return distance between points P1 and 0005 % P2, given as [X Y Z]. 0006 % 0007 % If P1 and P2 are two arrays of points, result is a N1*N2 array 0008 % containing distance between each point of P1 and each point of P2. 0009 % 0010 % 0011 % D = distancePoints3d(P1, P2, NOR) 0012 % with NOR being 1, 2, or Inf, corresponfing to the norm used. Default is 0013 % 2 (euclidean norm). 1 correspond to manhattan (or taxi driver) distance 0014 % and Inf to maximum difference in each coordinate. 0015 % 0016 % 0017 % See also: 0018 % points3d, minDistancePoints, distancePoints 0019 % 0020 % --------- 0021 % 0022 % author : David Legland 0023 % INRA - TPV URPOI - BIA IMASTE 0024 % created the 18/02/2005. 0025 % 0026 0027 % HISTORY 0028 % 21/02/2005: add different norms 0029 % 28/08/2007: deprecate 0030 0031 norm = 2; 0032 if length(varargin)==1 0033 norm = varargin{1}; 0034 end 0035 0036 % compute difference of coordinate for each pair of points 0037 ptsDiff = bsxfun(@minus, p2, p1); 0038 0039 % Return dist based on the type of measurement requested 0040 switch(norm) 0041 case 1 0042 dist = sum(abs(ptsDiff),2); 0043 case 2 0044 dist = vectorNorm3d(ptsDiff); 0045 case Inf 0046 dist = max(abs(ptsDiff), [], 2); 0047 otherwise 0048 dist = power(sum(power(ptsDiff, norm),2), 1/norm); 0049 end