Home > matGeom > geom3d > sph2cart2.m

sph2cart2

PURPOSE ^

SPH2CART2 Convert spherical coordinates to cartesian coordinates.

SYNOPSIS ^

function varargout = sph2cart2(theta, phi, rho)

DESCRIPTION ^

SPH2CART2 Convert spherical coordinates to cartesian coordinates.

   C = SPH2CART2(S)
   C = SPH2CART2(THETA, PHI)       (assuming rho = 1)
   C = SPH2CART2(THETA, PHI, RHO)   
   [X, Y, Z] = SPH2CART2(THETA, PHI, RHO);

   S = [theta phi rho] (spherical coordinate).
   C = [X Y Z]  (cartesian coordinate)

   The following convention is used:
   THETA is the colatitude, in radians, 0 for north pole, +pi for south
   pole, pi/2 for points with z=0. 
   PHI is the azimuth, in radians, defined as matlab cart2sph: angle from
   Ox axis, counted counter-clockwise.
   RHO is the distance of the point to the origin.
   Discussion on choice for convention can be found at:
   http://www.physics.oregonstate.edu/bridge/papers/spherical.pdf

   Example
     xyz = sph2cart2(pi/2, 0, 10)
     xyz =
        10.0000         0    0.0000

     xyz = sph2cart2(pi/2, pi/2, 10)
     xyz =
         0.0000   10.0000    0.0000

     % check consistency with cart2sph2
     sph2cart2(cart2sph2(0.7, 0.8, 5))
     ans =
         0.7000    0.8000    5.0000

   See also:
     angles3d, cart2sph2, sph2cart, sph2cart2d, eulerAnglesToRotation3d

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = sph2cart2(theta, phi, rho)
0002 %SPH2CART2 Convert spherical coordinates to cartesian coordinates.
0003 %
0004 %   C = SPH2CART2(S)
0005 %   C = SPH2CART2(THETA, PHI)       (assuming rho = 1)
0006 %   C = SPH2CART2(THETA, PHI, RHO)
0007 %   [X, Y, Z] = SPH2CART2(THETA, PHI, RHO);
0008 %
0009 %   S = [theta phi rho] (spherical coordinate).
0010 %   C = [X Y Z]  (cartesian coordinate)
0011 %
0012 %   The following convention is used:
0013 %   THETA is the colatitude, in radians, 0 for north pole, +pi for south
0014 %   pole, pi/2 for points with z=0.
0015 %   PHI is the azimuth, in radians, defined as matlab cart2sph: angle from
0016 %   Ox axis, counted counter-clockwise.
0017 %   RHO is the distance of the point to the origin.
0018 %   Discussion on choice for convention can be found at:
0019 %   http://www.physics.oregonstate.edu/bridge/papers/spherical.pdf
0020 %
0021 %   Example
0022 %     xyz = sph2cart2(pi/2, 0, 10)
0023 %     xyz =
0024 %        10.0000         0    0.0000
0025 %
0026 %     xyz = sph2cart2(pi/2, pi/2, 10)
0027 %     xyz =
0028 %         0.0000   10.0000    0.0000
0029 %
0030 %     % check consistency with cart2sph2
0031 %     sph2cart2(cart2sph2(0.7, 0.8, 5))
0032 %     ans =
0033 %         0.7000    0.8000    5.0000
0034 %
0035 %   See also:
0036 %     angles3d, cart2sph2, sph2cart, sph2cart2d, eulerAnglesToRotation3d
0037 %
0038 
0039 % ------
0040 % Author: David Legland
0041 % e-mail: david.legland@inrae.fr
0042 % INRAE - BIA Research Unit - BIBS Platform (Nantes)
0043 % created the 18/02/2005.
0044 %
0045 
0046 %   HISTORY
0047 %   22/03/2005: make test for 2 args, and add radius if not specified for
0048 %       1 arg.
0049 %   03/11/2006: change convention for angle: uses order [THETA PHI RHO]
0050 
0051 % Process input arguments
0052 if nargin == 1
0053     phi     = theta(:, 2);
0054     if size(theta, 2) > 2
0055         rho = theta(:, 3);
0056     else
0057         rho = ones(size(phi));
0058     end
0059     theta   = theta(:, 1);
0060     
0061 elseif nargin == 2
0062     rho     = ones(size(theta));
0063     
0064 end
0065 
0066 % conversion
0067 rz = rho .* sin(theta);
0068 x  = rz  .* cos(phi);
0069 y  = rz  .* sin(phi);
0070 z  = rho .* cos(theta);
0071 
0072 % format output
0073 if nargout <= 1
0074     varargout{1} = [x, y, z];
0075 else
0076     varargout{1} = x;
0077     varargout{2} = y;
0078     varargout{3} = z;
0079 end
0080

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