Mercurial > hg > octave-lyh
diff scripts/general/sph2cart.m @ 10688:7357e37f34fa
coordinate transforms: add option to operate on column matrix of coordinates.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 08 Jun 2010 21:47:05 -0700 |
parents | a8ce6bdecce5 |
children | fd0a3ac60b0e |
line wrap: on
line diff
--- a/scripts/general/sph2cart.m +++ b/scripts/general/sph2cart.m @@ -17,13 +17,22 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{theta}, @var{phi}, @var{r}) +## @deftypefn {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{theta}, @var{phi}, @var{r}) +## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{S}) +## @deftypefnx {Function File} {C =} sph2cart (@dots{}) ## Transform spherical to Cartesian coordinates. -## @var{x}, @var{y} and @var{z} must be the same shape, or scalar. +## ## @var{theta} describes the angle relative to the positive x-axis. ## @var{phi} is the angle relative to the xy-plane. ## @var{r} is the distance to the origin @w{(0, 0, 0)}. -## @seealso{pol2cart, cart2pol, cart2sph} +## @var{theta}, @var{phi}, and @var{r} must be the same shape, or scalar. +## If called with a single matrix argument then each row of @var{s} +## represents the spherical coordinate (@var{theta}, @var{phi}, @var{r}). +## +## If only a single return argument is requested then return a matrix +## @var{C} where each row represents one Cartesian coordinate +## (@var{x}, @var{y}, @var{z}). +## @seealso{cart2sph, pol2cart, cart2pol} ## @end deftypefn ## Author: Kai Habel <kai.habel@gmx.de> @@ -31,21 +40,33 @@ function [x, y, z] = sph2cart (theta, phi, r) - if (nargin != 3) + if (nargin != 1 && nargin != 3) print_usage (); endif - if ((ismatrix (theta) && ismatrix (phi) && ismatrix (r)) - && (size_equal (theta, phi) || isscalar (theta) || isscalar (phi)) - && (size_equal (theta, r) || isscalar (theta) || isscalar (r)) - && (size_equal (phi, r) || isscalar (phi) || isscalar (r))) + if (nargin == 1) + if (ismatrix (theta) && columns (theta) == 3) + r = theta(:,3); + phi = theta(:,2); + theta = theta(:,1); + else + error ("sph2cart: matrix input must have 3 columns [THETA, PHI, R]"); + endif + elseif (nargin == 3) + if (! ((ismatrix (theta) && ismatrix (phi) && ismatrix (r)) + && (size_equal (theta, phi) || isscalar (theta) || isscalar (phi)) + && (size_equal (theta, r) || isscalar (theta) || isscalar (r)) + && (size_equal (phi, r) || isscalar (phi) || isscalar (r)))) + error ("sph2cart: THETA, PHI, and R must be matrices of the same size, or scalar"); + endif + endif - x = r .* cos (phi) .* cos (theta); - y = r .* cos (phi) .* sin (theta); - z = r .* sin (phi); + x = r .* cos (phi) .* cos (theta); + y = r .* cos (phi) .* sin (theta); + z = r .* sin (phi); - else - error ("sph2cart: arguments must be matrices of same size, or scalar"); + if (nargout <= 1) + x = [x, y, z]; endif endfunction @@ -86,3 +107,8 @@ %! assert (y, [0, 1, 0], eps); %! assert (z, [0, 0, 0], eps); +%!test +%! S = [ 0, 0, 1; 0.5*pi, 0, 1; pi, 0, 1]; +%! C = [ 1, 0, 0; 0, 1, 0; -1, 0, 0]; +%! assert (sph2cart(S), C, eps); +