Commit 591d5152 authored by Marc van Wanrooij's avatar Marc van Wanrooij

Cartesian to double-polar coordinates transformation and vice versa

parent 7a32dc0e
function [A,E,R] = cart2dpol(X,Y,Z)
% [A,E,R] = CART2DPOL(X,Y,Z)
%
% transforms corresponding elements of data stored in Cartesian [X,Y,Z] to
% double-polar coordinates (azimuth angle A, elevation angle E, and radius
% R). The arrays X, Y and Z must be the same size. Angles A and E are
% returned in degrees.
%
% If X is a matrix with three columns (and Y and Z are not given), they are
% taken to be [X Y Z].
%
% See also DPOL2CART, DPOL2SPH, SPH2DPOL, DPOL2IPOL, IPOL2DPOL
% AUTHOR: Marc M. van Wanrooij
%% Initialization
if nargin==1
Y = X(:,2);
Z = X(:,3);
X = X(:,1);
end
R2D = 180/pi; % radians to degrees
%% Coordinate transformation
A = R2D * atan2(Y, sqrt (X.^2 + Z.^2));
E = R2D * atan2(X, sqrt (Y.^2 + Z.^2));
sel = z<0 & y>=0;
E(sel) = -180-E(sel);
sel = z<0 & y<0;
E(sel) = 180-E(sel);
R = sqrt(sum([X Y Z].^2,2));
%% Output
if nargout==1
A = [A E R];
end
\ No newline at end of file
function varargout = dpol2cart(A,E,R)
% [X,Y,Z] = DPOL2CART(A,E,R)
%
% transforms corresponding elements of data stored in double-polar (azimuth
% angle A, elevation angle E, and radius R) to Cartesian [X,Y,Z]
% coordinates. The arrays A, E and R must be the same size. Angles A and
% E are in degrees.
%
% If A is a matrix with three columns (and E and R are not given), they are
% taken to be [A E R]. If A is a matrix with two columns (and E and R are not given), they are
% taken to be [A E] and R is set to unity.
%
% See also DPOL2CART, DPOL2SPH, SPH2DPOL, DPOL2IPOL, IPOL2DPOL
% AUTHOR: Marc M. van Wanrooij
%% Initialization
if nargin==1 && size(A,2)==2
E = A(:,2);
A = A(:,1);
end
if nargin<3
R = ones(size(A));
end
%% Express angles in radians
D2R = pi/180; % radians to degrees
A = A*D2R;
E = E*D2R;
%% Convert to Cartesian Coordinates
X = R*sin(A); % Left-right
Y = R*sin(E); % Up-Down
signZ = sign(cos(A).*cos(E)); % front-back
absZ = abs(sqrt(R.^2-X.^2-Y.^2));
Z = signZ .* absZ;
%% Output
if nargout == 1 || nargout == 0
varargout(1) = {[X Y Z]};
elseif nargout == 3
varargout(1) = {X};
varargout(2) = {Y};
varargout(3) = {Z};
else
disp('Wrong number of output arguments');
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment