Home > matGeom > geom3d > drawDome.m

drawDome

PURPOSE ^

DRAWDOME Draw a dome (half-sphere, semi-sphere) as a mesh.

SYNOPSIS ^

function varargout = drawDome(varargin)

DESCRIPTION ^

DRAWDOME Draw a dome (half-sphere, semi-sphere) as a mesh.

   drawDome(DOME)
   Where DOME = [XC YC ZC R], draw the dome centered on the point with
   coordinates [XC YC ZC] and with radius R, using a quad mesh.
 
   drawDome(Dome, V)
   Where DOME = [XC YC ZC R] and V is a vector in the direction of the top
 
   drawDome(CENTER, R, V)
   Where CENTER = [XC YC ZC], specifies the center and the radius with two
   arguments and vector as third argument.
 
   drawdrawDome(XC, YC, ZC, R, V)
   Specifiy dome center, radius and vector as five arguments.

   drawDome(..., 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 = drawDome(...)
   Return a handle to the graphical object created by the function.

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

   Example
   % Draw four domes with different centers
     figure(1); clf; hold on;
     drawDome([0 0 1 1], 'FaceColor', 'b', 'EdgeColor', 'k', 'LineStyle', ':');
     drawDome([0 1 0 1], [0 1 0]);
     drawDome([0 -1 0 0.5], [1 0 0]);
     drawDome([0 -5 4 10], 'FaceAlpha', 0.5, 'EdgeColor', 'r', 'LineStyle', '-');
     view([-30 20]); axis equal; l = light;

   % Draw dome with different settings
     figure(1); clf;
     drawDome([10 20 30 10], [0 0 1], '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 = drawDome([10 20 30 10], [1 0 0]);
     set(h, 'linestyle', ':');
     set(h, 'facecolor', 'r');
     axis([0 50 0 50 0 50]); axis equal;
     l = light;
   
   % Draw a dome with high resolution
     figure(1); clf;
     h = drawDome([10 20 30 10], 'nPhi', 360, 'nTheta', 180);
     l = light; view(3);


   See also
   drawSphere

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawDome(varargin)
0002 %DRAWDOME Draw a dome (half-sphere, semi-sphere) as a mesh.
0003 %
0004 %   drawDome(DOME)
0005 %   Where DOME = [XC YC ZC R], draw the dome centered on the point with
0006 %   coordinates [XC YC ZC] and with radius R, using a quad mesh.
0007 %
0008 %   drawDome(Dome, V)
0009 %   Where DOME = [XC YC ZC R] and V is a vector in the direction of the top
0010 %
0011 %   drawDome(CENTER, R, V)
0012 %   Where CENTER = [XC YC ZC], specifies the center and the radius with two
0013 %   arguments and vector as third argument.
0014 %
0015 %   drawdrawDome(XC, YC, ZC, R, V)
0016 %   Specifiy dome center, radius and vector as five arguments.
0017 %
0018 %   drawDome(..., NAME, VALUE);
0019 %   Specifies one or several options using parameter name-value pairs.
0020 %   Available options are usual drawing options, as well as:
0021 %   'nPhi'    the number of arcs used for drawing the meridians
0022 %   'nTheta'  the number of circles used for drawing the parallels
0023 %
0024 %   H = drawDome(...)
0025 %   Return a handle to the graphical object created by the function.
0026 %
0027 %   [X Y Z] = drawdrawDome(...)
0028 %   Return the coordinates of the vertices used by the dome. In this
0029 %   case, the dome is not drawn.
0030 %
0031 %   Example
0032 %   % Draw four domes with different centers
0033 %     figure(1); clf; hold on;
0034 %     drawDome([0 0 1 1], 'FaceColor', 'b', 'EdgeColor', 'k', 'LineStyle', ':');
0035 %     drawDome([0 1 0 1], [0 1 0]);
0036 %     drawDome([0 -1 0 0.5], [1 0 0]);
0037 %     drawDome([0 -5 4 10], 'FaceAlpha', 0.5, 'EdgeColor', 'r', 'LineStyle', '-');
0038 %     view([-30 20]); axis equal; l = light;
0039 %
0040 %   % Draw dome with different settings
0041 %     figure(1); clf;
0042 %     drawDome([10 20 30 10], [0 0 1], 'linestyle', ':', 'facecolor', 'r');
0043 %     axis([0 50 0 50 0 50]); axis equal;
0044 %     l = light;
0045 %
0046 %   % The same, but changes style using graphic handle
0047 %     figure(1); clf;
0048 %     h = drawDome([10 20 30 10], [1 0 0]);
0049 %     set(h, 'linestyle', ':');
0050 %     set(h, 'facecolor', 'r');
0051 %     axis([0 50 0 50 0 50]); axis equal;
0052 %     l = light;
0053 %
0054 %   % Draw a dome with high resolution
0055 %     figure(1); clf;
0056 %     h = drawDome([10 20 30 10], 'nPhi', 360, 'nTheta', 180);
0057 %     l = light; view(3);
0058 %
0059 %
0060 %   See also
0061 %   drawSphere
0062 
0063 %   ---------
0064 %   author: Moritz Schappler
0065 %   created the 27/07/2013
0066 %
0067 
0068 %   HISTORY
0069 %   2013-07-27 initial version as copy of drawSphere with a few changes
0070 %   2020-05-18 changes based on current version of geom3d
0071 
0072 % Check if axes handle is specified
0073 if isAxisHandle(varargin{1})
0074     hAx = varargin{1};
0075     varargin(1)=[];
0076 elseif nargout ~= 3
0077     hAx = gca;
0078 end
0079 
0080 % process input options: when a string is found, assumes this is the
0081 % beginning of options
0082 options = {'FaceColor', 'g', 'LineStyle', 'none'};
0083 for i = 1:length(varargin)
0084     if ischar(varargin{i})
0085         if length(varargin) == 1
0086             options = {'FaceColor', varargin{1}, 'LineStyle', 'none'};
0087         else
0088             options = [options(1:end) varargin(i:end)];
0089         end
0090         varargin = varargin(1:i-1);
0091         break;
0092     end
0093 end
0094 
0095 % Parse the input (try to extract center coordinates and radius)
0096 if isempty(varargin)
0097     % no input: assumes unit dome
0098     xc = 0;    yc = 0; zc = 0;
0099     r = 1;
0100     v = [0;0;1]; 
0101 elseif length(varargin) == 1
0102     % one argument: concatenates center and radius
0103     dome = varargin{1};
0104     xc = dome(:,1);
0105     yc = dome(:,2);
0106     zc = dome(:,3);
0107     r  = dome(:,4);
0108     v = [0;0;1]; 
0109 elseif length(varargin) == 2
0110     % two arguments: concatenates center and radius with Rotation
0111     dome = varargin{1};
0112     xc = dome(:,1);
0113     yc = dome(:,2);
0114     zc = dome(:,3);
0115     r  = dome(:,4);
0116     v = varargin{2}; 
0117 elseif length(varargin) == 3
0118     % three arguments, corresponding to center and radius and rotation
0119     center = varargin{1};
0120     xc = center(1);
0121     yc = center(2);
0122     zc = center(3);
0123     r  = varargin{2};
0124     v  = varargin{3};
0125 elseif length(varargin) == 5
0126     % five arguments, corresponding to XC, YX, ZC, R and V
0127     xc = varargin{1};
0128     yc = varargin{2};
0129     zc = varargin{3};
0130     r  = varargin{4};
0131     v  = varargin{5};
0132 else
0133     error('drawDome: please specify center and radius');
0134 end
0135 
0136 % Rotation given by z-Axis. Calculate rotation matrix
0137 v = v(:) / norm(v(:));
0138 if all(abs(v(:)-[0;0;1]) < 1e-10)
0139     RM = eye(3);
0140 elseif all(abs(v(:) - [0;0;-1]) < 1e-10)
0141     RM = [[1;0;0], [0; -1; 0], [0; 0; -1]];
0142 else
0143     % z-axis given by argument
0144     ez = v(:);
0145     % x-axis perpendicular
0146     ex = cross(ez, [0; 0; 1]); ex = ex/norm(ex);
0147     % y-axis to create right-handed coordinate system
0148     ey = cross(ez, ex);
0149     RM = [ex, ey, ez];
0150 end
0151 
0152 % number of meridians
0153 nPhi = 32;
0154 ind = find(strcmpi('nPhi', options(1:2:end)));
0155 if ~isempty(ind)
0156     ind = ind(1);
0157     nPhi = options{2*ind};
0158     options(2*ind-1:2*ind) = [];
0159 end
0160     
0161 % number of parallels
0162 nTheta  = 8;
0163 ind = find(strcmpi('nTheta', options(1:2:end)));
0164 if ~isempty(ind)
0165     ind = ind(1);
0166     nTheta = options{2*ind};
0167     options(2*ind-1:2*ind) = [];
0168 end
0169 
0170 % compute spherical coordinates
0171 theta   = linspace(0, pi/2, nTheta+1);
0172 phi     = linspace(0, 2*pi, nPhi+1);
0173 
0174 % convert to Cartesian coordinates and rotate
0175 % Rotate the Dome
0176 x = zeros(nPhi+1, nTheta+1);
0177 y = x;
0178 z = x;
0179 
0180 sintheta = sin(theta);
0181 dx = cos(phi')*sintheta*r;
0182 dy = sin(phi')*sintheta*r;
0183 dz = ones(length(phi),1)*cos(theta)*r;
0184 
0185 for i = 1:nPhi+1
0186     for j = 1:nTheta+1
0187         dxyz = RM*[dx(i, j);dy(i, j);dz(i, j)];
0188         x(i, j) = xc + dxyz(1);
0189         y(i, j) = yc + dxyz(2);    
0190         z(i, j) = zc + dxyz(3);   
0191     end
0192 end
0193 
0194 % Process output
0195 if nargout == 0
0196     % no output: draw the dome
0197     surf(hAx, x, y, z, options{:});
0198     
0199 elseif nargout == 1
0200     % one output: compute
0201     varargout{1} = surf(hAx, x, y, z, options{:});
0202     
0203 elseif nargout == 3
0204     varargout{1} = x; 
0205     varargout{2} = y; 
0206     varargout{3} = z; 
0207 end

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