]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/cart2sph.m
update packages
[CreaPhase.git] / octave_packages / m / general / cart2sph.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{theta}, @var{phi}, @var{r}] =} cart2sph (@var{x}, @var{y}, @var{z})
21 ## @deftypefnx {Function File} {[@var{theta}, @var{phi}, @var{r}] =} cart2sph (@var{C})
22 ## @deftypefnx {Function File} {@var{S} =} cart2sph (@dots{})
23 ## Transform Cartesian to spherical 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{x}, @var{y}, and @var{z} must be the same shape, or scalar.
29 ## If called with a single matrix argument then each row of @var{c}
30 ## represents the Cartesian coordinate (@var{x}, @var{y}, @var{z}).
31 ##
32 ## If only a single return argument is requested then return a matrix
33 ## @var{s} where each row represents one spherical coordinate
34 ## (@var{theta}, @var{phi}, @var{r}).
35 ## @seealso{sph2cart, cart2pol, pol2cart}
36 ## @end deftypefn
37
38 ## Author: Kai Habel <kai.habel@gmx.de>
39 ## Adapted-by: jwe
40
41 function [theta, phi, r] = cart2sph (x, y, z)
42
43   if (nargin != 1 && nargin != 3)
44     print_usage ();
45   endif
46
47   if (nargin == 1)
48     if (ismatrix (x) && columns (x) == 3)
49       z = x(:,3);
50       y = x(:,2);
51       x = x(:,1);
52     else
53       error ("cart2sph: matrix input must have 3 columns [X, Y, Z]");
54     endif
55   elseif (nargin == 3)
56     if (! ((ismatrix (x) && ismatrix (y) && ismatrix (z))
57             && (size_equal (x, y) || isscalar (x) || isscalar (y))
58             && (size_equal (x, z) || isscalar (x) || isscalar (z))
59             && (size_equal (y, z) || isscalar (y) || isscalar (z))))
60       error ("cart2sph: X, Y, Z must be matrices of the same size, or scalar");
61     endif
62   endif
63
64   theta = atan2 (y, x);
65   phi = atan2 (z, sqrt (x .^ 2 + y .^ 2));
66   r = sqrt (x .^ 2 + y .^ 2 + z .^ 2);
67
68   if (nargout <= 1)
69     theta = [theta, phi, r];
70   endif
71
72 endfunction
73
74 %!test
75 %! x = [0, 1, 2];
76 %! y = [0, 1, 2];
77 %! z = [0, 1, 2];
78 %! [t, p, r] = cart2sph (x, y, z);
79 %! assert (t, [0, pi/4, pi/4], eps);
80 %! assert (p, [0, 1, 1]*atan(sqrt(0.5)), eps);
81 %! assert (r, [0, 1, 2]*sqrt(3), eps);
82
83 %!test
84 %! x = 0;
85 %! y = [0, 1, 2];
86 %! z = [0, 1, 2];
87 %! [t, p, r] = cart2sph (x, y, z);
88 %! assert (t, [0, 1, 1] * pi/2, eps);
89 %! assert (p, [0, 1, 1] * pi/4, eps);
90 %! assert (r, [0, 1, 2] * sqrt(2), eps);
91
92 %!test
93 %! x = [0, 1, 2];
94 %! y = 0;
95 %! z = [0, 1, 2];
96 %! [t, p, r] = cart2sph (x, y, z);
97 %! assert (t, [0, 0, 0]);
98 %! assert (p, [0, 1, 1] * pi/4);
99 %! assert (r, [0, 1, 2] * sqrt(2));
100
101 %!test
102 %! x = [0, 1, 2];
103 %! y = [0, 1, 2];
104 %! z = 0;
105 %! [t, p, r] = cart2sph (x, y, z);
106 %! assert (t, [0, 1, 1] * pi/4);
107 %! assert (p, [0, 0, 0]);
108 %! assert (r, [0, 1, 2] * sqrt(2));
109
110 %!test
111 %! C = [0, 0, 0; 1, 0, 1; 2, 0, 2];
112 %! S = [0, 0, 0; 0, pi/4, sqrt(2); 0, pi/4, 2*sqrt(2)];
113 %! assert (cart2sph(C), S, eps);