Home > atmlab > gridcreation > uniformsphere > RandSampleSphere.m

RandSampleSphere

PURPOSE ^

Generate a random or stratified sampling a unit sphere.

SYNOPSIS ^

function X=RandSampleSphere(N,spl)

DESCRIPTION ^

 Generate a random or stratified sampling a unit sphere.

 INPUT ARGUMENTS:
   - N   : desired number of point samples. N=200 is default.
   - spl : can be 'stratified' or 'random'. The former setting is the
           default.

 OUTPUT:
   - X  : N-by-3 array of sample point coordinates.

 AUTHOR: Anton Semechko (a.semechko@gmail.com)
 DATE: June.2012

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

RandSampleSphere.m

SOURCE CODE ^

0001 function X=RandSampleSphere(N,spl)
0002 % Generate a random or stratified sampling a unit sphere.
0003 %
0004 % INPUT ARGUMENTS:
0005 %   - N   : desired number of point samples. N=200 is default.
0006 %   - spl : can be 'stratified' or 'random'. The former setting is the
0007 %           default.
0008 %
0009 % OUTPUT:
0010 %   - X  : N-by-3 array of sample point coordinates.
0011 %
0012 % AUTHOR: Anton Semechko (a.semechko@gmail.com)
0013 % DATE: June.2012
0014 %
0015 
0016 if nargin<1 || isempty(N), N=200; end
0017 if nargin<2 || isempty(spl), spl='uniform'; end
0018 
0019 switch spl
0020     
0021     case 'uniform'
0022         
0023         % Generate uniform (although random) sampling of the sphere
0024         z=2*rand(N,1)-1;
0025         t=2*pi*rand(N,1);
0026         r=sqrt(1-z.^2);
0027         x=r.*cos(t);
0028         y=r.*sin(t);
0029         
0030     case 'stratified'
0031         
0032         % Uniformly sample the unfolded right cylinder
0033         lon=2*pi*rand(N,1);
0034         z=2*rand(N,1)-1;
0035         
0036         % Convert z to latitude
0037         lat=acos(z);
0038         
0039         % Convert spherical to rectangular co-ords
0040         x=cos(lon).*sin(lat);
0041         y=sin(lon).*sin(lat);
0042     
0043     otherwise
0044         
0045         error('Invalid option')
0046 
0047 end
0048 
0049 X=[x,y,z];

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005