% p2corners.m   convert p to rechirp                    Steve Mann, 1992
%
% the coordinates are normalized on [0,1);   X down, Y to right
%
% user supplies [a,b,c,d,e,f,g,h]; function returns [x,y,u,v]
%                x=[x1;x2;x3;x4], etc...
%                                                                Y axis
% Example: corners2r(p2corners())                   -------------->
%          should be identity                      |
%                                                  |
%                                                  v
%                                                   X axis 
%
% or
%
%
% See also: corners2r.c (help in .m file), corners2d, pcompose.m, pchirp.c

function [c1,c2,c3,c4,c5,c6,c7,c8] = pchirplikexdrmapping(a,b,c,d,e,f,g,h);

input_ismatrix = 0;
if nargin == 1
  input_ismatrix = 1;
  [M,N] = size(a);
  if max(M,N) ~= 8
     error('p2corners: you must have 1 vector or 8 scalar input parameters')
  end%if
  if min(M,N) ~= 1
     error('p2corners: must be a vector, not a matrix')
  end%if
  b=a(2);
  c=a(3);
  d=a(4);
  e=a(5);
  f=a(6);
  g=a(7);
  h=a(8);
  a=a(1); % destroy vector a now
end%if
if (nargin ~= 1) & (nargin ~= 8)
  error('p2corners: have not decided how to handle case other than 1 or 8 input arguments')
end%if

corners=pinv([...
  a b 0   0   0   0   0   0     ;  ...
  0 0 a   b   0   0   0   0     ;  ...
  0 0 0   0   a-g b-h 0   0     ;  ...
  0 0 0   0   0   0   a-g b-h   ;  ...
  d e 0   0   0   0   0   0     ;  ...
  0 0 d-g e-h 0   0   0   0     ;  ...
  0 0 0   0   d   e   0   0     ;  ...
  0 0 0   0   0   0   d-g e-h      ...
]) *   ...
[-c;  ...
 -c;  ...
 1-c; ...
 1-c; ...
 -f;  ...
 1-f; ...
 -f;  ...
 1-f; ...
];

if (nargout~=1)&(nargout~=8)&(nargout~=0)
  error('p2corners: wrong number of output arguments')
end%if

if nargout==0
 disp('p2corners: no output args given; screen display')
 disp(sprintf('%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f',corners(1:4)))
 disp(sprintf('%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f',corners(5:8)))
end%if

if nargout==8
  c1=corners(1); c2=corners(2); c3=corners(3); c4=corners(4);
  c5=corners(5); c6=corners(6); c7=corners(7); c8=corners(8);
else%ifnargout=1
  c1=corners; % all of them in vector to first and only output
end%if

