DRAWELLIPSE3D Draw a 3D ellipse. Possible calls for the function : drawEllipse3d([XC YC ZC A B THETA PHI]) drawEllipse3d([XC YC ZC A B THETA PHI PSI]) drawEllipse3d([XC YC ZC A B], [THETA PHI]) drawEllipse3d([XC YC ZC A B], [THETA PHI PSI]) drawEllipse3d([XC YC ZC A B], THETA, PHI) drawEllipse3d([XC YC ZC], A, B, THETA, PHI) drawEllipse3d([XC YC ZC A B], THETA, PHI, PSI) drawEllipse3d([XC YC ZC], A, B, THETA, PHI, PSI) drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI) drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI, PSI) where XC, YC, ZY are coordinate of ellipse center, A and B are the half-lengths of the major and minor axes of the ellipse, PHI and THETA are 3D angle (in degrees) of the normal to the plane containing the ellipse (PHI between 0 and 360 corresponding to longitude, and THETA from 0 to 180, corresponding to angle with vertical). H = drawEllipse3d(...) return handle on the created LINE object Example figure; axis([-10 10 -10 10 -10 10]); hold on; ellXY = [0 0 0 8 5 0 0 0]; drawEllipse3d(ellXY, 'color', [.8 0 0], 'linewidth', 2) ellXZ = [0 0 0 8 2 90 90 90]; drawEllipse3d(ellXZ, 'color', [0 .8 0], 'linewidth', 2) ellYZ = [0 0 0 5 2 90 0 90]; drawEllipse3d(ellYZ, 'color', [0 0 .8], 'linewidth', 2)
0001 function varargout = drawEllipse3d(varargin) 0002 %DRAWELLIPSE3D Draw a 3D ellipse. 0003 % 0004 % Possible calls for the function : 0005 % drawEllipse3d([XC YC ZC A B THETA PHI]) 0006 % drawEllipse3d([XC YC ZC A B THETA PHI PSI]) 0007 % drawEllipse3d([XC YC ZC A B], [THETA PHI]) 0008 % drawEllipse3d([XC YC ZC A B], [THETA PHI PSI]) 0009 % drawEllipse3d([XC YC ZC A B], THETA, PHI) 0010 % drawEllipse3d([XC YC ZC], A, B, THETA, PHI) 0011 % drawEllipse3d([XC YC ZC A B], THETA, PHI, PSI) 0012 % drawEllipse3d([XC YC ZC], A, B, THETA, PHI, PSI) 0013 % drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI) 0014 % drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI, PSI) 0015 % 0016 % where XC, YC, ZY are coordinate of ellipse center, A and B are the 0017 % half-lengths of the major and minor axes of the ellipse, 0018 % PHI and THETA are 3D angle (in degrees) of the normal to the plane 0019 % containing the ellipse (PHI between 0 and 360 corresponding to 0020 % longitude, and THETA from 0 to 180, corresponding to angle with 0021 % vertical). 0022 % 0023 % H = drawEllipse3d(...) 0024 % return handle on the created LINE object 0025 % 0026 % Example 0027 % figure; axis([-10 10 -10 10 -10 10]); hold on; 0028 % ellXY = [0 0 0 8 5 0 0 0]; 0029 % drawEllipse3d(ellXY, 'color', [.8 0 0], 'linewidth', 2) 0030 % ellXZ = [0 0 0 8 2 90 90 90]; 0031 % drawEllipse3d(ellXZ, 'color', [0 .8 0], 'linewidth', 2) 0032 % ellYZ = [0 0 0 5 2 90 0 90]; 0033 % drawEllipse3d(ellYZ, 'color', [0 0 .8], 'linewidth', 2) 0034 % 0035 0036 % ------ 0037 % Author: David Legland 0038 % e-mail: david.legland@inra.fr 0039 % Created: 2008-05-07 0040 % Copyright 2008 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas). 0041 0042 % HISTORY 0043 0044 % Possible calls for the function, with number of arguments : 0045 % drawEllipse3d([XC YC ZC A B THETA PHI]) 1 0046 % drawEllipse3d([XC YC ZC A B THETA PHI PSI]) 1 0047 % drawEllipse3d([XC YC ZC A B], [THETA PHI]) 2 0048 % drawEllipse3d([XC YC ZC A B], [THETA PHI PSI]) 2 0049 % drawEllipse3d([XC YC ZC A B], THETA, PHI) 3 0050 % drawEllipse3d([XC YC ZC A B], THETA, PHI, PSI) 4 0051 % drawEllipse3d([XC YC ZC], A, B, THETA, PHI) 5 0052 % drawEllipse3d([XC YC ZC], A, B, THETA, PHI, PSI) 6 0053 % drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI) 7 0054 % drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI, PSI) 8 0055 0056 0057 % extract drawing options 0058 ind = find(cellfun(@ischar, varargin), 1, 'first'); 0059 options = {}; 0060 if ~isempty(ind) 0061 options = varargin(ind:end); 0062 varargin(ind:end) = []; 0063 end 0064 0065 if length(varargin)==1 0066 % get center and radius 0067 ellipse = varargin{1}; 0068 xc = ellipse(:,1); 0069 yc = ellipse(:,2); 0070 zc = ellipse(:,3); 0071 a = ellipse(:,4); 0072 b = ellipse(:,5); 0073 0074 % get colatitude of normal 0075 if size(ellipse, 2)>=6 0076 theta = ellipse(:,6); 0077 else 0078 theta = zeros(size(ellipse, 1), 1); 0079 end 0080 0081 % get azimut of normal 0082 if size(ellipse, 2)>=7 0083 phi = ellipse(:,7); 0084 else 0085 phi = zeros(size(ellipse, 1), 1); 0086 end 0087 0088 % get roll 0089 if size(ellipse, 2)==8 0090 psi = ellipse(:,8); 0091 else 0092 psi = zeros(size(ellipse, 1), 1); 0093 end 0094 0095 elseif length(varargin)==2 0096 % get center and radius 0097 ellipse = varargin{1}; 0098 xc = ellipse(:,1); 0099 yc = ellipse(:,2); 0100 zc = ellipse(:,3); 0101 a = ellipse(:,4); 0102 b = ellipse(:,5); 0103 0104 % get angle of normal 0105 angle = varargin{2}; 0106 theta = angle(:,1); 0107 phi = angle(:,2); 0108 0109 % get roll 0110 if size(angle, 2)==3 0111 psi = angle(:,3); 0112 else 0113 psi = zeros(size(angle, 1), 1); 0114 end 0115 0116 elseif length(varargin)==3 0117 % get center and radius 0118 ellipse = varargin{1}; 0119 xc = ellipse(:,1); 0120 yc = ellipse(:,2); 0121 zc = ellipse(:,3); 0122 a = ellipse(:,4); 0123 b = ellipse(:,5); 0124 0125 % get angle of normal and roll 0126 theta = varargin{2}; 0127 phi = varargin{3}; 0128 psi = zeros(size(phi, 1), 1); 0129 0130 elseif length(varargin)==4 0131 % get center and radius 0132 ellipse = varargin{1}; 0133 xc = ellipse(:,1); 0134 yc = ellipse(:,2); 0135 zc = ellipse(:,3); 0136 0137 if size(ellipse, 2)==5 0138 a = ellipse(:,4); 0139 b = ellipse(:,5); 0140 end 0141 0142 theta = varargin{2}; 0143 phi = varargin{3}; 0144 psi = varargin{4}; 0145 0146 elseif length(varargin)==5 0147 % get center and radius 0148 ellipse = varargin{1}; 0149 xc = ellipse(:,1); 0150 yc = ellipse(:,2); 0151 zc = ellipse(:,3); 0152 a = varargin{2}; 0153 b = varargin{3}; 0154 theta = varargin{4}; 0155 phi = varargin{5}; 0156 psi = zeros(size(phi, 1), 1); 0157 0158 elseif length(varargin)==6 0159 ellipse = varargin{1}; 0160 xc = ellipse(:,1); 0161 yc = ellipse(:,2); 0162 zc = ellipse(:,3); 0163 a = varargin{2}; 0164 b = varargin{3}; 0165 theta = varargin{4}; 0166 phi = varargin{5}; 0167 psi = varargin{6}; 0168 0169 elseif length(varargin)==7 0170 xc = varargin{1}; 0171 yc = varargin{2}; 0172 zc = varargin{3}; 0173 a = varargin{4}; 0174 b = varargin{5}; 0175 theta = varargin{6}; 0176 phi = varargin{7}; 0177 psi = zeros(size(phi, 1), 1); 0178 0179 elseif length(varargin)==8 0180 xc = varargin{1}; 0181 yc = varargin{2}; 0182 zc = varargin{3}; 0183 a = varargin{4}; 0184 b = varargin{5}; 0185 theta = varargin{6}; 0186 phi = varargin{7}; 0187 psi = varargin{8}; 0188 0189 else 0190 error('drawEllipse3d: please specify center and radius'); 0191 end 0192 0193 % uses 60 intervals 0194 t = linspace(0, 2*pi, 61)'; 0195 0196 % polyline approximation of ellipse, centered and parallel to main axes 0197 x = a * cos(t); 0198 y = b * sin(t); 0199 z = zeros(length(t), 1); 0200 base = [x y z]; 0201 0202 % compute transformation from local basis to world basis 0203 trans = localToGlobal3d(xc, yc, zc, theta, phi, psi); 0204 0205 % transform points composing the ellipse 0206 ellipse = transformPoint3d(base, trans); 0207 0208 % draw the curve 0209 h = drawPolyline3d(ellipse, options{:}); 0210 0211 if nargout > 0 0212 varargout = {h}; 0213 end 0214