0001 function varargout = drawEllipsoid(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 nPhi = 32;
0043
0044
0045 nTheta = 16;
0046
0047
0048 drawEllipses = false;
0049 ellipseColor = 'k';
0050 ellipseWidth = 1;
0051
0052 drawAxes = false;
0053 axesColor = 'k';
0054 axesWidth = 2;
0055
0056
0057
0058
0059
0060 [hAx, varargin] = parseAxisHandle(varargin{:});
0061
0062
0063 elli = varargin{1};
0064 varargin(1) = [];
0065
0066
0067 options = {'FaceColor', 'g', 'linestyle', 'none'};
0068
0069 while length(varargin) > 1
0070 switch lower(varargin{1})
0071 case 'nphi'
0072 nPhi = varargin{2};
0073
0074 case 'ntheta'
0075 nTheta = varargin{2};
0076
0077 case 'drawellipses'
0078 drawEllipses = varargin{2};
0079
0080 case 'ellipsecolor'
0081 ellipseColor = varargin{2};
0082
0083 case 'ellipsewidth'
0084 ellipseWidth = varargin{2};
0085
0086 case 'drawaxes'
0087 drawAxes = varargin{2};
0088
0089 case 'axescolor'
0090 axesColor = varargin{2};
0091
0092 case 'axeswidth'
0093 axesWidth = varargin{2};
0094
0095 otherwise
0096
0097 options = [options varargin(1:2)];
0098 end
0099
0100 varargin(1:2) = [];
0101 end
0102
0103
0104
0105
0106
0107 xc = elli(:,1);
0108 yc = elli(:,2);
0109 zc = elli(:,3);
0110 a = elli(:,4);
0111 b = elli(:,5);
0112 c = elli(:,6);
0113 k = pi / 180;
0114 ellPhi = elli(:,7) * k;
0115 ellTheta = elli(:,8) * k;
0116 ellPsi = elli(:,9) * k;
0117
0118
0119
0120
0121
0122 sca = createScaling3d(a, b, c);
0123 rotZ = createRotationOz(ellPhi);
0124 rotY = createRotationOy(ellTheta);
0125 rotX = createRotationOx(ellPsi);
0126 tra = createTranslation3d([xc yc zc]);
0127
0128
0129 trans = tra * rotZ * rotY * rotX * sca;
0130
0131
0132
0133
0134
0135 theta = linspace(0, pi, nTheta+1);
0136 phi = linspace(0, 2*pi, nPhi+1);
0137
0138
0139 sintheta = sin(theta);
0140 x = cos(phi') * sintheta;
0141 y = sin(phi') * sintheta;
0142 z = ones(length(phi),1) * cos(theta);
0143
0144
0145 [x, y, z] = transformPoint3d(x, y, z, trans);
0146
0147
0148
0149
0150 if drawEllipses
0151
0152 nVertices = 120;
0153 t = linspace(0, 2*pi, nVertices+1);
0154
0155
0156 xc1 = cos(t');
0157 yc1 = sin(t');
0158 zc1 = zeros(size(t'));
0159
0160
0161 xc2 = cos(t');
0162 yc2 = zeros(size(t'));
0163 zc2 = sin(t');
0164
0165
0166 xc3 = zeros(size(t'));
0167 yc3 = cos(t');
0168 zc3 = sin(t');
0169
0170
0171 [xc1, yc1, zc1] = transformPoint3d(xc1, yc1, zc1, trans);
0172 [xc2, yc2, zc2] = transformPoint3d(xc2, yc2, zc2, trans);
0173 [xc3, yc3, zc3] = transformPoint3d(xc3, yc3, zc3, trans);
0174 end
0175
0176
0177
0178 if drawAxes
0179 axesEndings = [-1 0 0; +1 0 0; 0 -1 0; 0 +1 0; 0 0 -1; 0 0 +1];
0180 axesEndings = transformPoint3d(axesEndings, trans);
0181 end
0182
0183
0184
0185
0186 ellipseOptions = {'color', ellipseColor, 'LineWidth', ellipseWidth};
0187 axesOptions = {'color', axesColor, 'LineWidth', axesWidth};
0188
0189
0190 if nargout == 0
0191
0192 surf(x, y, z, options{:});
0193
0194 if drawEllipses
0195 plot3(hAx, xc1, yc1, zc1, ellipseOptions{:});
0196 plot3(hAx, xc2, yc2, zc2, ellipseOptions{:});
0197 plot3(hAx, xc3, yc3, zc3, ellipseOptions{:});
0198 end
0199
0200 if drawAxes
0201 drawEdge3d(hAx, [axesEndings(1,:), axesEndings(2,:)], axesOptions{:});
0202 drawEdge3d(hAx, [axesEndings(3,:), axesEndings(4,:)], axesOptions{:});
0203 drawEdge3d(hAx, [axesEndings(5,:), axesEndings(6,:)], axesOptions{:});
0204 end
0205
0206 elseif nargout == 1
0207
0208 varargout{1} = surf(x, y, z, options{:});
0209 if drawEllipses
0210 plot3(hAx, xc1, yc1, zc1, ellipseOptions{:});
0211 plot3(hAx, xc2, yc2, zc2, ellipseOptions{:});
0212 plot3(hAx, xc3, yc3, zc3, ellipseOptions{:});
0213 end
0214
0215 elseif nargout == 3
0216
0217 varargout{1} = x;
0218 varargout{2} = y;
0219 varargout{3} = z;
0220 if drawEllipses
0221 plot3(hAx, xc1, yc1, zc1, ellipseOptions{:});
0222 plot3(hAx, xc2, yc2, zc2, ellipseOptions{:});
0223 plot3(hAx, xc3, yc3, zc3, ellipseOptions{:});
0224 end
0225
0226 elseif nargout == 4 && drawEllipses
0227
0228 varargout{1} = surf(hAx, x, y, z, options{:});
0229 varargout{2} = plot3(hAx, xc1, yc1, zc1, ellipseOptions{:});
0230 varargout{3} = plot3(hAx, xc2, yc2, zc2, ellipseOptions{:});
0231 varargout{4} = plot3(hAx, xc3, yc3, zc3, ellipseOptions{:});
0232
0233 end
0234