[node,face,elem]=meshunitsphere(tsize,maxvol) create the surface and/or volumetric mesh of a unit sphere centered at [0 0 0] and radius 1 author: Qianqian Fang, <q.fang at neu.edu> input: tsize: maximum size of the surface triangles (from 0 to 1) maxvol: maximum volume of the tetrahedron; if one wants to return elem without specifying maxvol, maxvol=tsize^3 output: node: node coordinates, 3 columns for x, y and z respectively face: integer array with dimensions of NB x 3, each row represents a surface mesh face element elem: integer array with dimensions of NE x 4, each row represents a tetrahedron. If ignored, this function only produces the surface example: [node,face]=meshunitsphere(0.05); [node,face,elem]=meshunitsphere(0.05,0.01); plotmesh(node,elem,'x>0'); axis equal; -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
0001 function [node,face,elem]=meshunitsphere(tsize,maxvol) 0002 % 0003 % [node,face,elem]=meshunitsphere(tsize,maxvol) 0004 % 0005 % create the surface and/or volumetric mesh of a unit sphere 0006 % centered at [0 0 0] and radius 1 0007 % 0008 % author: Qianqian Fang, <q.fang at neu.edu> 0009 % 0010 % input: 0011 % tsize: maximum size of the surface triangles (from 0 to 1) 0012 % maxvol: maximum volume of the tetrahedron; if one wants to return 0013 % elem without specifying maxvol, maxvol=tsize^3 0014 % 0015 % output: 0016 % node: node coordinates, 3 columns for x, y and z respectively 0017 % face: integer array with dimensions of NB x 3, each row represents 0018 % a surface mesh face element 0019 % elem: integer array with dimensions of NE x 4, each row represents 0020 % a tetrahedron. If ignored, this function only produces the surface 0021 % 0022 % example: 0023 % [node,face]=meshunitsphere(0.05); 0024 % [node,face,elem]=meshunitsphere(0.05,0.01); 0025 % plotmesh(node,elem,'x>0'); axis equal; 0026 % 0027 % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net) 0028 % 0029 0030 dim=60; 0031 esize=tsize*dim; 0032 thresh=dim/2-1; 0033 [xi,yi,zi]=meshgrid(0:0.5:dim,0:0.5:dim,0:0.5:dim); 0034 dist=thresh-sqrt((xi-30).^2+(yi-30).^2+(zi-30).^2); 0035 dist(find(dist<0))=0; 0036 clear xi yi zi; 0037 0038 % extract a level-set at v=thresh, being a sphere with R=thresh 0039 % the maximum element size of the surface triangles is tsize*dim 0040 0041 [node,face]=vol2restrictedtri(dist,1,[dim dim dim],dim*dim*dim,30,esize,esize,40000); 0042 node=(node-0.5)*0.5; 0043 0044 [node,face]=removeisolatednode(node,face); 0045 0046 node=(node-30)/28; 0047 r0=sqrt(sum((node.*node)')); 0048 node=node.*repmat(1./r0(:),1,3); 0049 0050 if(nargout==3) 0051 if(nargin==1) maxvol=tsize*tsize*tsize; end 0052 [node,elem,face]=surf2mesh(node,face,[-1 -1 -1]*1.1,[1 1 1]*1.1,1,maxvol,[],[]); 0053 elem=elem(:,1:4); 0054 end 0055 face=face(:,1:3);