1## Copyright (C) 2021 David Legland
2## All rights reserved.
3##
4## Redistribution and use in source and binary forms, with or without
5## modification, are permitted provided that the following conditions are met:
6##
7##     1 Redistributions of source code must retain the above copyright notice,
8##       this list of conditions and the following disclaimer.
9##     2 Redistributions in binary form must reproduce the above copyright
10##       notice, this list of conditions and the following disclaimer in the
11##       documentation and/or other materials provided with the distribution.
12##
13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23##
24## The views and conclusions contained in the software and documentation are
25## those of the authors and should not be interpreted as representing official
26## policies, either expressed or implied, of the copyright holders.
27
28function varargout = cyl2cart(varargin)
29%CYL2CART  Convert cylindrical to cartesian coordinates.
30%
31%   CART = cyl2cart(CYL)
32%   convert the 3D cylindrical coordinates of points CYL (given by
33%   [THETA R Z] where THETA, R, and Z have the same size) into cartesian
34%   coordinates CART, given by [X Y Z].
35%   The transforms is the following :
36%   X = R*cos(THETA);
37%   Y = R*sin(THETA);
38%   Z remains inchanged.
39%
40%   CART = cyl2cart(THETA, R, Z)
41%   provides coordinates as 3 different parameters
42%
43%   Example
44%   cyl2cart([-1 0 2])
45%   gives : 4.7124    1.0000     2.0000
46%
47%   See also angles3d, cart2pol, cart2sph2, cart2cyl
48%
49%
50% ------
51% Author: David Legland
52% e-mail: david.legland@jouy.inra.fr
53% Created: 2006-03-23
54% Copyright 2006 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas).
55
56% process input parameters
57if length(varargin)==1
58    var = varargin{1};
59    theta = var(:,1);
60    r = var(:,2);
61    z = var(:,3);
62elseif length(varargin)==3
63    theta = varargin{1};
64    r = varargin{2};
65    z = varargin{3};
66end
67
68% convert coordinates
69dim = size(theta);
70x = reshape(r(:).*cos(theta(:)), dim);
71y = reshape(r(:).*sin(theta(:)), dim);
72
73% process output parameters
74if nargout==0 ||nargout==1
75    if length(dim)>2 || dim(2)>1
76        varargout{1} = {x y z};
77    else
78        varargout{1} = [x y z];
79    end
80elseif nargout==3
81    varargout{1} = x;
82    varargout{2} = y;
83    varargout{3} = z;
84end
85