]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/sph2cart.m
update packages
[CreaPhase.git] / octave_packages / m / general / sph2cart.m
1 ## Copyright (C) 2000-2012 Kai Habel
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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.
24 ##
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}).
31 ##
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}
36 ## @end deftypefn
37
38 ## Author: Kai Habel <kai.habel@gmx.de>
39 ## Adapted-by: jwe
40
41 function [x, y, z] = sph2cart (theta, phi, r)
42
43   if (nargin != 1 && nargin != 3)
44     print_usage ();
45   endif
46
47   if (nargin == 1)
48     if (ismatrix (theta) && columns (theta) == 3)
49       r = theta(:,3);
50       phi = theta(:,2);
51       theta = theta(:,1);
52     else
53       error ("sph2cart: matrix input must have 3 columns [THETA, PHI, R]");
54     endif
55   elseif (nargin == 3)
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");
61     endif
62   endif
63
64   x = r .* cos (phi) .* cos (theta);
65   y = r .* cos (phi) .* sin (theta);
66   z = r .* sin (phi);
67
68   if (nargout <= 1)
69     x = [x, y, z];
70   endif
71
72 endfunction
73
74 %!test
75 %! t = [0, 0, 0];
76 %! p = [0, 0, 0];
77 %! r = [0, 1, 2];
78 %! [x, y, z] = sph2cart (t, p, r);
79 %! assert (x, r);
80 %! assert (y, [0, 0, 0]);
81 %! assert (z, [0, 0, 0]);
82
83 %!test
84 %! t = 0;
85 %! p = [0, 0, 0];
86 %! r = [0, 1, 2];
87 %! [x, y, z] = sph2cart (t, p, r);
88 %! assert (x, r);
89 %! assert (y, [0, 0, 0]);
90 %! assert (z, [0, 0, 0]);
91
92 %!test
93 %! t = [0, 0, 0];
94 %! p = 0;
95 %! r = [0, 1, 2];
96 %! [x, y, z] = sph2cart (t, p, r);
97 %! assert (x, r);
98 %! assert (y, [0, 0, 0]);
99 %! assert (z, [0, 0, 0]);
100
101 %!test
102 %! t = [0, 0.5, 1]*pi;
103 %! p = [0, 0, 0];
104 %! r = 1;
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);
109
110 %!test
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);
114