Code covered by the BSD License  

Highlights from
Meshing a spherical shell

Meshing a spherical shell

by

 

This small function meshes a spherical shell and returns to unique vertices and proper faces matrix

mesh_sph (rho,theta,phi)
function [vert faces] = mesh_sph (rho,theta,phi)
% mesh_sph meshes the given spherical shell which is defined bu rho the
% radius theta the azimuth angle and phi the polar angle. For more info
% type doc sph2cart.  Theta is  0<theta<2*pi and pi/2<phi<pi/2
% rho is always positive double. In order to determine the mesh size 
% choose an increment for each angle as shown in the examples
% Returns to vertices matrix `vert` and faces matrix `faces`
%Examples: 
% Meshing a full sphere with radius 1, 8 faces along theta and 4 faces 
%along phi 
%[v f] =  mesh_sph(1,0:pi/4:2*pi, -pi/2:pi/8:pi/2)
%
% Meshing an upper hemi-sphere with 4 faces along theta and 8 faces along
% phi
%[v f] =  mesh_sph(1,0:pi/2:2*pi, 0:pi/16:pi/2)

    R=rho; P=phi; T=theta;
    m = length(T);
    n = length(P);
    p = 1;
    sz =  m*n*p;
  
    [P T] = meshgrid (P,T);
    
    P = reshape(P, sz, 1); 
    T = reshape(T, sz, 1); 
    
    vert = [T P]; 
    
%%
    ind_faces = 1:length(vert);
    j=1;
    for i=1:(m)*(n-1);
      if ~rem(i,m)==0
          faces_in(j,:)= [ind_faces(i), ind_faces(i+1), ...
                           ind_faces(i+1+m), ind_faces(i+m)];        
          j=j+1;
      end
    end
[x,y,z] = sph2cart(T,P,R);
vert =[x y z];
tol = 10^-12;
vert(abs(vert-0)<tol)=0;
[vert , ~, b]=unique(vert,'rows');
faces = faces_in;
for i =1:length(b);
    faces(faces_in==i)=b(i);
end
for i=1:length(faces)
     a = faces(i,:);
     [b, ia] = unique(a, 'first');
%      a(~ismember(1:length(a),ia)) = NaN;
     if length(ia)<4
         a = [b NaN];
     end
     faces(i,:)=a;   
end
end
  

Contact us