Create a 3D mesh representing a sphere. [V, F] = sphereMesh(S) Creates a 3D mesh representing the sphere S given by [xc yc zy r]. [V, F] = sphereMesh(); Assumes sphere is the unit sphere centered at the origin. [V, F] = sphereMesh(S, 'nTheta', NT, 'nPhi', NP); Specifies the number of discretisation steps for the meridians and the parallels. Default values are nTheta = 16 and nPhi = 32. Example s = [10 20 30 40]; [v, f] = sphereMesh(s); drawMesh(v, f); view(3); axis equal; light; lighting gouraud; See also meshes3d, drawSphere, ellipsoidMesh, cylinderMesh, surfToMesh
0001 function varargout = sphereMesh(sphere, varargin) 0002 % Create a 3D mesh representing a sphere. 0003 % 0004 % [V, F] = sphereMesh(S) 0005 % Creates a 3D mesh representing the sphere S given by [xc yc zy r]. 0006 % 0007 % [V, F] = sphereMesh(); 0008 % Assumes sphere is the unit sphere centered at the origin. 0009 % 0010 % [V, F] = sphereMesh(S, 'nTheta', NT, 'nPhi', NP); 0011 % Specifies the number of discretisation steps for the meridians and the 0012 % parallels. Default values are nTheta = 16 and nPhi = 32. 0013 % 0014 % 0015 % Example 0016 % s = [10 20 30 40]; 0017 % [v, f] = sphereMesh(s); 0018 % drawMesh(v, f); 0019 % view(3); axis equal; light; lighting gouraud; 0020 % 0021 % See also 0022 % meshes3d, drawSphere, ellipsoidMesh, cylinderMesh, surfToMesh 0023 % 0024 0025 % ------ 0026 % Author: David Legland 0027 % e-mail: david.legland@inra.fr 0028 % Created: 2012-10-25, using Matlab 7.9.0.529 (R2009b) 0029 % Copyright 2012 INRA - Cepia Software Platform. 0030 0031 if nargin == 0 0032 sphere = [0 0 0 1]; 0033 end 0034 0035 % number of meridians 0036 nPhi = 32; 0037 0038 % number of parallels 0039 nTheta = 16; 0040 0041 % process input arguments 0042 while length(varargin) > 1 0043 paramName = varargin{1}; 0044 switch lower(paramName) 0045 case 'ntheta', nTheta = varargin{2}; 0046 case 'nphi', nPhi = varargin{2}; 0047 otherwise 0048 error(['Could not recognise parameter: ' paramName]); 0049 end 0050 varargin(1:2) = []; 0051 end 0052 0053 % extract sphere data 0054 xc = sphere(:,1); 0055 yc = sphere(:,2); 0056 zc = sphere(:,3); 0057 r = sphere(:,4); 0058 0059 0060 % compute spherical coordinates 0061 theta = linspace(0, pi, nTheta+1); 0062 phi = linspace(0, 2*pi, nPhi+1); 0063 0064 % convert to cartesian coordinates 0065 sintheta = sin(theta); 0066 x = xc + cos(phi') * sintheta * r; 0067 y = yc + sin(phi') * sintheta * r; 0068 z = zc + ones(length(phi),1) * cos(theta) * r; 0069 0070 % convert to FV mesh 0071 [vertices, faces] = surfToMesh(x, y, z, 'yperiodic', true); 0072 0073 % format output 0074 varargout = formatMeshOutput(nargout, vertices, faces);