Home > matGeom > geom3d > drawSphere.m

drawSphere

PURPOSE ^

DRAWSPHERE Draw a sphere as a mesh.

SYNOPSIS ^

function varargout = drawSphere(varargin)

DESCRIPTION ^

DRAWSPHERE Draw a sphere as a mesh.

   drawSphere(SPHERE)
   Where SPHERE = [XC YC ZC R], draw the sphere centered on the point with
   coordinates [XC YC ZC] and with radius R, using a quad mesh.

   drawSphere(CENTER, R)
   Where CENTER = [XC YC ZC], specifies the center and the radius with two
   arguments.

   drawSphere(XC, YC, ZC, R)
   Specifiy sphere center and radius as four arguments.

   drawSphere(..., NAME, VALUE);
   Specifies one or several options using parameter name-value pairs.
   Available options are usual drawing options, as well as:
   'nPhi'    the number of arcs used for drawing the meridians
   'nTheta'  the number of circles used for drawing the parallels

   H = drawSphere(...)
   Return a handle to the graphical object created by the function.

   [X Y Z] = drawSphere(...)
   Return the coordinates of the vertices used by the sphere. In this
   case, the sphere is not drawn.

   Example
   % Draw four spheres with different centers
     figure(1); clf; hold on;
     drawSphere([10 10 30 5]);
     drawSphere([20 30 10 5]);
     drawSphere([30 30 30 5]);
     drawSphere([30 20 10 5]);
     view([-30 20]); axis equal; l = light;

   % Draw sphere with different settings
     figure(1); clf;
     drawSphere([10 20 30 10], 'linestyle', ':', 'facecolor', 'r');
     axis([0 50 0 50 0 50]); axis equal;
     l = light;

   % The same, but changes style using graphic handle
     figure(1); clf;
     h = drawSphere([10 20 30 10]);
     set(h, 'linestyle', ':');
     set(h, 'facecolor', 'r');
     axis([0 50 0 50 0 50]); axis equal;
     l = light;
   
   % Draw a sphere with high resolution
     figure(1); clf;
     h = drawSphere([10 20 30 10], 'nPhi', 360, 'nTheta', 180);
     l = light; view(3);


   See also
   spheres, circles3d, sphere, drawEllipsoid

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawSphere(varargin)
0002 %DRAWSPHERE Draw a sphere as a mesh.
0003 %
0004 %   drawSphere(SPHERE)
0005 %   Where SPHERE = [XC YC ZC R], draw the sphere centered on the point with
0006 %   coordinates [XC YC ZC] and with radius R, using a quad mesh.
0007 %
0008 %   drawSphere(CENTER, R)
0009 %   Where CENTER = [XC YC ZC], specifies the center and the radius with two
0010 %   arguments.
0011 %
0012 %   drawSphere(XC, YC, ZC, R)
0013 %   Specifiy sphere center and radius as four arguments.
0014 %
0015 %   drawSphere(..., NAME, VALUE);
0016 %   Specifies one or several options using parameter name-value pairs.
0017 %   Available options are usual drawing options, as well as:
0018 %   'nPhi'    the number of arcs used for drawing the meridians
0019 %   'nTheta'  the number of circles used for drawing the parallels
0020 %
0021 %   H = drawSphere(...)
0022 %   Return a handle to the graphical object created by the function.
0023 %
0024 %   [X Y Z] = drawSphere(...)
0025 %   Return the coordinates of the vertices used by the sphere. In this
0026 %   case, the sphere is not drawn.
0027 %
0028 %   Example
0029 %   % Draw four spheres with different centers
0030 %     figure(1); clf; hold on;
0031 %     drawSphere([10 10 30 5]);
0032 %     drawSphere([20 30 10 5]);
0033 %     drawSphere([30 30 30 5]);
0034 %     drawSphere([30 20 10 5]);
0035 %     view([-30 20]); axis equal; l = light;
0036 %
0037 %   % Draw sphere with different settings
0038 %     figure(1); clf;
0039 %     drawSphere([10 20 30 10], 'linestyle', ':', 'facecolor', 'r');
0040 %     axis([0 50 0 50 0 50]); axis equal;
0041 %     l = light;
0042 %
0043 %   % The same, but changes style using graphic handle
0044 %     figure(1); clf;
0045 %     h = drawSphere([10 20 30 10]);
0046 %     set(h, 'linestyle', ':');
0047 %     set(h, 'facecolor', 'r');
0048 %     axis([0 50 0 50 0 50]); axis equal;
0049 %     l = light;
0050 %
0051 %   % Draw a sphere with high resolution
0052 %     figure(1); clf;
0053 %     h = drawSphere([10 20 30 10], 'nPhi', 360, 'nTheta', 180);
0054 %     l = light; view(3);
0055 %
0056 %
0057 %   See also
0058 %   spheres, circles3d, sphere, drawEllipsoid
0059 
0060 %   ---------
0061 %   author: David Legland
0062 %   INRA - TPV URPOI - BIA IMASTE
0063 %   created the 17/02/2005
0064 %
0065 
0066 %   HISTORY
0067 %   2006-05-19 use centered sphere with radius 1 when no input specified
0068 %   2007-01-04 typo
0069 %   2010-11-08 code cleanup, add doc
0070 
0071 % Check if axes handle is specified
0072 if isAxisHandle(varargin{1})
0073     hAx = varargin{1};
0074     varargin(1)=[];
0075 elseif nargout ~= 3
0076     hAx = gca;
0077 end
0078 
0079 % process input options: when a string is found, assumes this is the
0080 % beginning of options
0081 options = {'FaceColor', 'g', 'LineStyle', 'none'};
0082 for i = 1:length(varargin)
0083     if ischar(varargin{i})
0084         if length(varargin) == 1
0085             options = {'FaceColor', varargin{1}, 'LineStyle', 'none'};
0086         else
0087             options = [options(1:end) varargin(i:end)];
0088         end
0089         varargin = varargin(1:i-1);
0090         break;
0091     end
0092 end
0093 
0094 % Parse the input (try to extract center coordinates and radius)
0095 if isempty(varargin)
0096     % no input: assumes unit sphere
0097     xc = 0;    yc = 0; zc = 0;
0098     r = 1;
0099     
0100 elseif length(varargin) == 1
0101     % one argument: concatenates center and radius
0102     sphere = varargin{1};
0103     xc = sphere(:,1);
0104     yc = sphere(:,2);
0105     zc = sphere(:,3);
0106     r  = sphere(:,4);
0107     
0108 elseif length(varargin) == 2
0109     % two arguments, corresponding to center and radius
0110     center = varargin{1};
0111     xc = center(1);
0112     yc = center(2);
0113     zc = center(3);
0114     r  = varargin{2};
0115     
0116 elseif length(varargin) == 4
0117     % four arguments, corresponding to XC, YX, ZC and R
0118     xc = varargin{1};
0119     yc = varargin{2};
0120     zc = varargin{3};
0121     r  = varargin{4};
0122 else
0123     error('drawSphere: please specify center and radius');
0124 end
0125 
0126 % number of meridians
0127 nPhi    = 32;
0128 ind = find(strcmpi('nPhi', options(1:2:end)));
0129 if ~isempty(ind)
0130     ind = ind(1);
0131     nPhi = options{2*ind};
0132     options(2*ind-1:2*ind) = [];
0133 end
0134     
0135 % number of parallels
0136 nTheta  = 16;
0137 ind = find(strcmpi('nTheta', options(1:2:end)));
0138 if ~isempty(ind)
0139     ind = ind(1);
0140     nTheta = options{2*ind};
0141     options(2*ind-1:2*ind) = [];
0142 end
0143 
0144 % compute spherical coordinates
0145 theta   = linspace(0, pi, nTheta+1);
0146 phi     = linspace(0, 2*pi, nPhi+1);
0147 
0148 % convert to cartesian coordinates
0149 sintheta = sin(theta);
0150 x = xc + cos(phi')*sintheta*r;
0151 y = yc + sin(phi')*sintheta*r;
0152 z = zc + ones(length(phi),1)*cos(theta)*r;
0153 
0154 % Process output
0155 if nargout == 0
0156     % no output: draw the sphere
0157     surf(hAx, x, y, z, options{:});
0158     
0159 elseif nargout == 1
0160     % one output: compute
0161     varargout{1} = surf(hAx, x, y, z, options{:});
0162     
0163 elseif nargout == 3
0164     varargout{1} = x; 
0165     varargout{2} = y; 
0166     varargout{3} = z; 
0167 end
0168

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019