function crdxy = planarrep(crd);

%
% crdxy = planarrep(crd)
%
% Represents a cloud of points collected on a 3D plane in 2D coordinate
% system
%
% Input:
%   crd:    n x 3 matrix, data coordinates
%           row denotes points, column denotes coordinates (X, Y, Z)
%
% Output:
%   crd:    n x 2 matrix, planar coordinates
%           row denotes points, column denotes coordinates (X, Y)
%
% Dependencies:
%
%   None
%
% Author: 
%   Ahmet Erdemir, erdemira@ccf.org, August 1, 2008.
%

% fit plane to original data
n = size(crd,1);
coeff = crd\ones(n, 1);

% project coordinates onto plane
s = (1. - coeff'*crd')/norm(coeff);
crdp = crd + ((ones(3,1)*s).*(coeff*ones(1,n)))';
k = coeff/norm(coeff);

% project k on global coordinate system
kp = abs(k'*eye(3,3)); 
% select global axis with minimum k component
[kpm, kpi] = min(kp);
u = zeros(3,1); u(kpi) = 1;

j = cross(k, u);
i = cross(j, k);

Rgp = [i j k];

crdxy = (Rgp'*crdp')';
crdxy(:,3) = [];

