% corners2d                                               Steve Mann, 1992
% convert 4 corners to dechirp parameters
% dechirp pulls out the 4 specified points to the corners.
%
% the coordinates are normalized on [0,1)
%
% user supplies [x1,y1,x2,y2,x3,y3,x4,y4]; function returns [a,b,c,d,e,f,g,h]
%
% Example:
% [a b c d e f g h]=corners2d(0.1,.04, .03,.98, .95,.04, .94,.96)
% or
% dechirp_vector=corners2d([0.1,.04, .03,.98, .95,.04, .94,.96])
%
% Note that identity on RHS (corners) is [0,0  0,1  1,0  1,1]
% while the identity on the RHS (p-chirp parameters) is [1 0 0 0 1 0 0 0]
%
% See also: corners2r.c (help in .m file), pcompose.m, pchirp.c

function [a,b,c,d,e,f,g,h] = dechirp_mapping(x1,y1,x2,y2,x3,y3,x4,y4);

input_isvector = 0;
if nargin == 1
  input_isvector = 1;
  if length(x1) ~= 8  %  !=, thinks ! is a shell escape.
    disp('***** dechirp input parameter vector is not 8 units long *****')
  end%if
  y1=x1(2);
  x2=x1(3);
  y2=x1(4);
  x3=x1(5);
  y3=x1(6);
  x4=x1(7);
  y4=x1(8);
  x1=x1(1); % destroy vector x1 now
end%if

      % disp('about to define 4 corners on [0,1)');
      % x1 = m1/in_vdim;  y1 = n1/in_hdim;
      % x2 = m2/in_vdim;  y2 = n2/in_hdim;
      % x3 = m3/in_vdim;  y3 = n3/in_hdim;
      % x4 = m4/in_vdim;  y4 = n4/in_hdim;
      % compute the 8 P-chirplet parameters, using letters of the alphabet,
      %   in order, starting with ``a'', as in the wavelet: (a,b,...) */

      c =  x1;
      f =  y1;   % Emacs: control space (Mark set) then goto end of eq'n
                 %   then Meta-x indent-region */
      a = -((x2*x3 - x3*x4)*(-f + y2)/...
	    (-(x3*y2) + x4*y2 + x2*y3- x4*y3 - x2*y4 + x3*y4)) - ...
	      (x2*x3 - x3*x4)*(-f + y3)/...
		(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		  (-(x2*x3) + x3*x4)*(-f + y4)/...
		    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		      (-c + x3)*(-(x4*y2) - x2*y3 + x4*y3 + x2*y4)/...
			(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
			  (-c + x4)*(x3*y2 - x3*y4)/...
			    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) -...
			      (-c + x2)*(-(x3*y2) + x3*y4)/...
				(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4);

      b = -((-(x2*x3) + x2*x4)*(-f + y2)/...
	    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4)) - ...
	      (-(x2*x3) + x2*x4)*(-f + y3)/...
		(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		  (x2*x3 - x2*x4)*(-f + y4)/...
		    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		      (-c + x3)*(x2*y3 - x2*y4)/...
			(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
			  (-c + x4)*(-(x2*y3) + x2*y4)/...
			    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) -...
			      (-c + x2)*(x3*y2 - x4*y2 + x4*y3 - x3*y4)/...
				(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4);

      d = -((-f + y2)*(x2*y3 - x4*y3)/...
	    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4)) - ...
	      (-(x2*y3) + x4*y3)*(-f + y4)/...
		(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		  (-f + y3)*(x3*y2 - x4*y2 + x2*y4 - x3*y4)/...
		    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		      (-c + x4)*(y2*y3 - y3*y4)/...
			(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
			  (-c + x2)*(-(y2*y3) + y3*y4)/...
			    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) -...
			      (-c + x3)*(-(y2*y3) + y3*y4)/...
				(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4);

      e = -((-(x3*y2) + x4*y2)*(-f + y3)/...
	    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4)) - ...
	      (x3*y2 - x4*y2)*(-f + y4)/...
		(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		  (-f + y2)*(-(x2*y3) + x4*y3 + x2*y4 - x3*y4)/...
		    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		      (-c + x2)*(y2*y3 - y2*y4)/...
			(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
			  (-c + x3)*(y2*y3 - y2*y4)/...
			    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) -...
			      (-c + x4)*(-(y2*y3) + y2*y4)/...
				(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4);

      g = -((x2 - x4)*(-f + y2)/...
	    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4)) - ...
	      (x2 - x4)*(-f + y3)/...
		(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		  (-c + x4)*(y2 - y4)/...
		    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		      (-x2 + x4)*(-f + y4)/...
			(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
			  (-c + x2)*(-y2 + y4)/...
			    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) -...
			      (-c + x3)*(-y2 + y4)/(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4);

      h = -((-x3 + x4)*(-f + y2)/...
	    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4)) - ...
	      (-x3 + x4)*(-f + y3)/...
		(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		  (-c + x2)*(y3 - y4)/...
		    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
		      (-c + x3)*(y3 - y4)/...
			(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
			  (x3 - x4)*(-f + y4)/...
			    (-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4) - ...
			      (-c + x4)*(-y3 + y4)/(-(x3*y2) + x4*y2 + x2*y3 - x4*y3 - x2*y4 + x3*y4);

if input_isvector
  a = [a,b,c,d,e,f,g,h];
end%if

