CART2SPH2 Convert cartesian coordinates to spherical coordinates. [THETA PHI RHO] = cart2sph2([X Y Z]) [THETA PHI RHO] = cart2sph2(X, Y, Z) 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: cart2sph2([1 0 0]) returns [pi/2 0 1]; cart2sph2([1 1 0]) returns [pi/2 pi/4 sqrt(2)]; cart2sph2([0 0 1]) returns [0 0 1]; See also: angles3d, sph2cart2, cart2sph, cart2sph2d
0001 function varargout = cart2sph2(varargin) 0002 %CART2SPH2 Convert cartesian coordinates to spherical coordinates. 0003 % 0004 % [THETA PHI RHO] = cart2sph2([X Y Z]) 0005 % [THETA PHI RHO] = cart2sph2(X, Y, Z) 0006 % 0007 % The following convention is used: 0008 % THETA is the colatitude, in radians, 0 for north pole, +pi for south 0009 % pole, pi/2 for points with z=0. 0010 % PHI is the azimuth, in radians, defined as matlab cart2sph: angle from 0011 % Ox axis, counted counter-clockwise. 0012 % RHO is the distance of the point to the origin. 0013 % Discussion on choice for convention can be found at: 0014 % http://www.physics.oregonstate.edu/bridge/papers/spherical.pdf 0015 % 0016 % Example: 0017 % cart2sph2([1 0 0]) returns [pi/2 0 1]; 0018 % cart2sph2([1 1 0]) returns [pi/2 pi/4 sqrt(2)]; 0019 % cart2sph2([0 0 1]) returns [0 0 1]; 0020 % 0021 % See also: 0022 % angles3d, sph2cart2, cart2sph, cart2sph2d 0023 % 0024 0025 % --------- 0026 % author : David Legland 0027 % INRA - TPV URPOI - BIA IMASTE 0028 % created the 18/02/2005. 0029 % 0030 0031 % HISTORY 0032 % 02/11/2006: update doc, and manage case RHO is empty 0033 % 03/11/2006: change convention for angle : uses order [THETA PHI RHO] 0034 % 27/06/2007: manage 2 output arguments 0035 0036 % parse input angles based on input argument number 0037 if length(varargin) == 1 0038 xyz = varargin{1}; 0039 elseif length(varargin) == 3 0040 xyz = [varargin{1} varargin{2} varargin{3}]; 0041 end 0042 0043 % ensure z coordinate is specified 0044 if size(xyz, 2) == 2 0045 xyz(:,3) = 1; 0046 end 0047 0048 % convert to spherical coordinates 0049 [p, t, r] = cart2sph(xyz(:,1), xyz(:,2), xyz(:,3)); 0050 0051 % format output arguments 0052 if nargout == 1 || nargout == 0 0053 varargout{1} = [pi/2-t p r]; 0054 elseif nargout==2 0055 varargout{1} = pi/2-t; 0056 varargout{2} = p; 0057 else 0058 varargout{1} = pi/2-t; 0059 varargout{2} = p; 0060 varargout{3} = r; 0061 end 0062