1 ## Copyright (C) 2000-2012 Kai Habel
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{theta}, @var{phi}, @var{r})
21 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} sph2cart (@var{S})
22 ## @deftypefnx {Function File} {C =} sph2cart (@dots{})
23 ## Transform spherical to Cartesian coordinates.
25 ## @var{theta} describes the angle relative to the positive x-axis.
26 ## @var{phi} is the angle relative to the xy-plane.
27 ## @var{r} is the distance to the origin @w{(0, 0, 0)}.
28 ## @var{theta}, @var{phi}, and @var{r} must be the same shape, or scalar.
29 ## If called with a single matrix argument then each row of @var{s}
30 ## represents the spherical coordinate (@var{theta}, @var{phi}, @var{r}).
32 ## If only a single return argument is requested then return a matrix
33 ## @var{C} where each row represents one Cartesian coordinate
34 ## (@var{x}, @var{y}, @var{z}).
35 ## @seealso{cart2sph, pol2cart, cart2pol}
38 ## Author: Kai Habel <kai.habel@gmx.de>
41 function [x, y, z] = sph2cart (theta, phi, r)
43 if (nargin != 1 && nargin != 3)
48 if (ismatrix (theta) && columns (theta) == 3)
53 error ("sph2cart: matrix input must have 3 columns [THETA, PHI, R]");
56 if (! ((ismatrix (theta) && ismatrix (phi) && ismatrix (r))
57 && (size_equal (theta, phi) || isscalar (theta) || isscalar (phi))
58 && (size_equal (theta, r) || isscalar (theta) || isscalar (r))
59 && (size_equal (phi, r) || isscalar (phi) || isscalar (r))))
60 error ("sph2cart: THETA, PHI, and R must be matrices of the same size, or scalar");
64 x = r .* cos (phi) .* cos (theta);
65 y = r .* cos (phi) .* sin (theta);
78 %! [x, y, z] = sph2cart (t, p, r);
80 %! assert (y, [0, 0, 0]);
81 %! assert (z, [0, 0, 0]);
87 %! [x, y, z] = sph2cart (t, p, r);
89 %! assert (y, [0, 0, 0]);
90 %! assert (z, [0, 0, 0]);
96 %! [x, y, z] = sph2cart (t, p, r);
98 %! assert (y, [0, 0, 0]);
99 %! assert (z, [0, 0, 0]);
102 %! t = [0, 0.5, 1]*pi;
105 %! [x, y, z] = sph2cart (t, p, r);
106 %! assert (x, [1, 0, -1], eps);
107 %! assert (y, [0, 1, 0], eps);
108 %! assert (z, [0, 0, 0], eps);
111 %! S = [ 0, 0, 1; 0.5*pi, 0, 1; pi, 0, 1];
112 %! C = [ 1, 0, 0; 0, 1, 0; -1, 0, 0];
113 %! assert (sph2cart(S), C, eps);