CREATELINE3D Create a line with various inputs. Line is represented in a parametric form : [x0 y0 z0 dx dy dz] x = x0 + t*dx y = y0 + t*dy; z = z0 + t*dz; L = createLine3d(P1, P2); Returns the line going through the two given points P1 and P2. L = createLine3d(X0, Y0, Z0, DX, DY, DZ); Returns the line going through the point [x0, y0, z0], and with direction vector given by [DX DY DZ]. L = createLine3d(P0, DX, DY, DZ); Returns the line going through point P0 given by [x0, y0, z0] and with direction vector given by [DX DY DZ]. L = createLine3d(THETA, PHI); Create a line originated at (0,0) and with angles theta and phi. L = createLine3d(P0, THETA, PHI); Create a line with direction given by theta and phi, and which contains point P0. Note : in all cases, parameters can be vertical arrays of the same dimension. The result is then an array of lines, of dimensions [N*6]. See also: lines3d --------- author : David Legland INRA - TPV URPOI - BIA IMASTE created the 17/02/2005.
0001 function line = createLine3d(varargin) 0002 %CREATELINE3D Create a line with various inputs. 0003 % 0004 % Line is represented in a parametric form : [x0 y0 z0 dx dy dz] 0005 % x = x0 + t*dx 0006 % y = y0 + t*dy; 0007 % z = z0 + t*dz; 0008 % 0009 % 0010 % L = createLine3d(P1, P2); 0011 % Returns the line going through the two given points P1 and P2. 0012 % 0013 % L = createLine3d(X0, Y0, Z0, DX, DY, DZ); 0014 % Returns the line going through the point [x0, y0, z0], and with 0015 % direction vector given by [DX DY DZ]. 0016 % 0017 % L = createLine3d(P0, DX, DY, DZ); 0018 % Returns the line going through point P0 given by [x0, y0, z0] and with 0019 % direction vector given by [DX DY DZ]. 0020 % 0021 % L = createLine3d(THETA, PHI); 0022 % Create a line originated at (0,0) and with angles theta and phi. 0023 % 0024 % L = createLine3d(P0, THETA, PHI); 0025 % Create a line with direction given by theta and phi, and which contains 0026 % point P0. 0027 % 0028 % 0029 % Note : in all cases, parameters can be vertical arrays of the same 0030 % dimension. The result is then an array of lines, of dimensions [N*6]. 0031 % 0032 % See also: 0033 % lines3d 0034 % 0035 % --------- 0036 % 0037 % author : David Legland 0038 % INRA - TPV URPOI - BIA IMASTE 0039 % created the 17/02/2005. 0040 % 0041 0042 % HISTORY 0043 % 30/11/2005 add more cases 0044 % 04/01/2007 remove unused variables 0045 0046 % NOTE : A 3d line can also be represented with a 1*7 array : 0047 % [x0 y0 z0 dx dy dz t]. 0048 % whith 't' being one of the following : 0049 % - t=0 : line is a singleton (x0,y0) 0050 % - t=1 : line is an edge segment, between points (x0,y0) and (x0+dx, 0051 % y0+dy). 0052 % - t=Inf : line is a Ray, originated from (x0,y0) and going to infinity 0053 % in the direction(dx,dy). 0054 % - t=-Inf : line is a Ray, originated from (x0,y0) and going to infinity 0055 % in the direction(-dx,-dy). 0056 % - t=NaN : line is a real straight line, and contains all points 0057 % verifying the above equation. 0058 % This seems to be a convenient way to represent uniformly all kind of 0059 % lines (including edges, rays, and even point). 0060 % 0061 % NOTE2 : Another form is the 1*8 array : 0062 % [x0 y0 z0 dx dy dz t1 t2]. 0063 % with t1 and t2 giving first and last position of the edge on the 0064 % supporting straight line, and with t2>t1. 0065 0066 if length(varargin)==1 0067 error('Wrong number of arguments in ''createLine'' '); 0068 0069 elseif length(varargin)==2 0070 % 2 input parameters. They can be : 0071 % - 2 points, then 2 arrays of 1*2 double. 0072 v1 = varargin{1}; 0073 v2 = varargin{2}; 0074 if size(v1, 2)==3 0075 % first input parameter is first point, and second input is the 0076 % second point. 0077 line = [v1(:,1) v1(:,2) v1(:,3) v2(:,1)-v1(:,1) v2(:,2)-v1(:,2) v2(:,3)-v1(:,3)]; 0078 elseif size(v1, 2)==1 0079 % first parameter is angle with the vertical 0080 % second parameter is horizontal angle with Ox axis 0081 theta = varargin{1}; 0082 phi = varargin{2}; 0083 0084 sit = sin(theta); 0085 p0 = zeros([size(theta, 1) 3]); 0086 0087 line = [p0 cos(phi)*sit sin(phi)*sit cos(theta)]; 0088 end 0089 0090 elseif length(varargin)==3 0091 % 3 input parameters : 0092 % first parameter is a point of the line 0093 % second parameter is angle with the vertical 0094 % third parameter is horizontal angle with Ox axis 0095 p0 = varargin{1}; 0096 theta = varargin{2}; 0097 phi = varargin{3}; 0098 0099 if size(p0, 2)~=3 0100 error('first argument should be a 3D point'); 0101 end 0102 0103 sit = sin(theta); 0104 line = [p0 cos(phi)*sit sin(phi)*sit cos(theta)]; 0105 0106 elseif length(varargin)==4 0107 % 4 input parameters : 0108 p0 = varargin{1}; 0109 dx = varargin{2}; 0110 dy = varargin{3}; 0111 dz = varargin{4}; 0112 0113 if size(p0, 2)~=3 0114 error('first argument should be a 3D point'); 0115 end 0116 0117 line = [p0 dx dy dz]; 0118 elseif length(varargin)==5 0119 % 5 input parameters : 0120 % first parameter is distance of lin to origin 0121 % second parameter is angle with the vertical 0122 % third parameter is horizontal angle with Ox axis 0123 x0 = varargin{1}; 0124 y0 = varargin{1}; 0125 z0 = varargin{1}; 0126 theta = varargin{2}; 0127 phi = varargin{3}; 0128 0129 sit = sin(theta); 0130 line = [x0 y0 z0 cos(phi)*sit sin(phi)*sit cos(theta)]; 0131 0132 elseif length(varargin)==6 0133 % 6 input parameters, given as separate arguments for x0, y0, z0 and 0134 % dx, dy and dz 0135 % (not very useful, but anyway...) 0136 x0 = varargin{1}; 0137 y0 = varargin{2}; 0138 z0 = varargin{3}; 0139 dx = varargin{4}; 0140 dy = varargin{5}; 0141 dz = varargin{6}; 0142 0143 line = [x0 y0 z0 dx dy dz]; 0144 end