MIDPOINT3D Middle point of two 3D points or of a 3D edge. MID = midPoint3d(P1, P2) Compute the middle point of the two points P1 and P2. MID = midPoint3d(EDGE) Compute the middle point of the edge given by EDGE. EDGE has the format: [X1 Y1 Z1 X2 Y2 Z2], and MID has the format [XMID YMID ZMID], with XMID = (X1+X2)/2, YMID = (Y1+Y2)/2 and ZMID = (Z1+Z2)/2. [MIDX MIDY] = midPoint3d(...) Return the result as two separate variables or arrays. Works also when EDGE is a N-by-6 array, in this case the result is a N-by-3 array containing the midPoint3d of each edge. Example P1 = [10 20 30]; P2 = [30 40 50]; % edge input midPoint3d([P1 P2]) ans = 20 30 40 % two points input midPoint3d(P1, P2) ans = 20 30 40 % three outputs [xm ym zm] = midPoint3d(P1, P2) xm = 20 ym = 30 zm = 40 See also edges3d, points3d
0001 function varargout = midPoint3d(varargin) 0002 %MIDPOINT3D Middle point of two 3D points or of a 3D edge. 0003 % 0004 % MID = midPoint3d(P1, P2) 0005 % Compute the middle point of the two points P1 and P2. 0006 % 0007 % MID = midPoint3d(EDGE) 0008 % Compute the middle point of the edge given by EDGE. 0009 % EDGE has the format: [X1 Y1 Z1 X2 Y2 Z2], and MID has the format 0010 % [XMID YMID ZMID], 0011 % with XMID = (X1+X2)/2, YMID = (Y1+Y2)/2 and ZMID = (Z1+Z2)/2. 0012 % 0013 % [MIDX MIDY] = midPoint3d(...) 0014 % Return the result as two separate variables or arrays. 0015 % 0016 % Works also when EDGE is a N-by-6 array, in this case the result is a 0017 % N-by-3 array containing the midPoint3d of each edge. 0018 % 0019 % 0020 % Example 0021 % P1 = [10 20 30]; 0022 % P2 = [30 40 50]; 0023 % % edge input 0024 % midPoint3d([P1 P2]) 0025 % ans = 0026 % 20 30 40 0027 % 0028 % % two points input 0029 % midPoint3d(P1, P2) 0030 % ans = 0031 % 20 30 40 0032 % 0033 % % three outputs 0034 % [xm ym zm] = midPoint3d(P1, P2) 0035 % xm = 0036 % 20 0037 % ym = 0038 % 30 0039 % zm = 0040 % 40 0041 % 0042 % See also 0043 % edges3d, points3d 0044 % 0045 0046 % ------ 0047 % Author: David Legland 0048 % E-mail: david.legland@inrae.fr 0049 % Created: 2010-08-08, using Matlab 7.9.0.529 (R2009b) 0050 % Copyright 2010-2024 INRA - Cepia Software Platform 0051 0052 if nargin == 1 0053 % input is a 3D edge 0054 edge = varargin{1}; 0055 mid = [mean(edge(:, [1 4]), 2) mean(edge(:, [2 5]), 2) mean(edge(:, [3 6]), 2)]; 0056 0057 elseif nargin == 2 0058 % input are two points 0059 p1 = varargin{1}; 0060 p2 = varargin{2}; 0061 0062 % assert inputs are equal 0063 n1 = size(p1, 1); 0064 n2 = size(p2, 1); 0065 if n1>1 && n2==1 0066 p2 = repmat(p2, n1, 1); 0067 elseif n2>1 && n1==1 0068 p1 = repmat(p1, n2, 1); 0069 elseif n1~=n2 0070 error('geom3d:midPoint3d', ... 0071 'Inputs must have same size, or one must have length 1'); 0072 end 0073 0074 % compute middle point 0075 mid = (p1 + p2) / 2; 0076 end 0077 0078 % process output arguments 0079 if nargout<=1 0080 varargout{1} = mid; 0081 else 0082 varargout = {mid(:,1), mid(:,2), mid(:,3)}; 0083 end