% est_pchirp2weighteddirect...                         Steve Mann, 1992-1993
%
% estimate the 8 projective parameters from a pair of images, arbitrary weight
%
% sign convention: m (or X) down, n (or Y) to right
% properly handles situation when one or both images contain some NaN points
%
% See also: pseudo2p, pchirp2, etc...

function [p1,p2,p3,p4,p5,p6,p7,p8]=f(E1,E2);  % two images in, 0,1,or 8 outs

%%%%mask = isnan(E1).*isnan(E2);  % NaN mask: regions where either is undefined
% doesnt work because nanmask makes it still nan

[El,Em,En] = derivatives(E1,E2);
[M,N]= size(El);                    % size of any of the 3 derivative matrices
[n m]=meshdom(1:N,M:-1:1);          % variables ``x'' and ``y''
%disp('est_pseudo: converting from pixels to units on [0,1)')      doesnt work
%m=m/M;    doesnt work
%n=n/N;    doesnt work

DERIVATIVES = ...
 [...
  [sum2nan(m.*Em .* m.*Em) sum2nan(m.*Em .* n.*Em) sum2nan(m.*Em .* Em   ) sum2nan(m.*Em .* m.*En) sum2nan(m.*Em .* n.*En) sum2nan(m.*Em .* En   ) sum2nan(m.*Em .* m.*Et) sum2nan(m.*Em .* n.*Et)];...
  [sum2nan(n.*Em .* m.*Em) sum2nan(n.*Em .* n.*Em) sum2nan(n.*Em .* Em   ) sum2nan(n.*Em .* m.*En) sum2nan(n.*Em .* n.*En) sum2nan(n.*Em .* En   ) sum2nan(n.*Em .* m.*Et) sum2nan(n.*Em .* n.*Et)];...
  [sum2nan(Em    .* m.*Em) sum2nan(Em    .* n.*Em) sum2nan(Em    .* Em   ) sum2nan(Em    .* m.*En) sum2nan(Em    .* n.*En) sum2nan(Em    .* En   ) sum2nan(Em    .* m.*Et) sum2nan(Em    .* n.*Et)];...
  [sum2nan(m.*En .* m.*Em) sum2nan(m.*En .* n.*Em) sum2nan(m.*En .* Em   ) sum2nan(m.*En .* m.*En) sum2nan(m.*En .* n.*En) sum2nan(m.*En .* En   ) sum2nan(m.*En .* m.*Et) sum2nan(m.*En .* n.*Et)];...
  [sum2nan(n.*En .* m.*Em) sum2nan(n.*En .* n.*Em) sum2nan(n.*En .* Em   ) sum2nan(n.*En .* m.*En) sum2nan(n.*En .* n.*En) sum2nan(n.*En .* En   ) sum2nan(n.*En .* m.*Et) sum2nan(n.*En .* n.*Et)];...
  [sum2nan(En    .* m.*Em) sum2nan(En    .* n.*Em) sum2nan(En    .* Em   ) sum2nan(En    .* m.*En) sum2nan(En    .* n.*En) sum2nan(En    .* En   ) sum2nan(En    .* m.*Et) sum2nan(En    .* n.*Et)];...
  [sum2nan(m.*Et .* m.*Em) sum2nan(m.*Et .* n.*Em) sum2nan(m.*Et .* Em   ) sum2nan(m.*Et .* m.*En) sum2nan(m.*Et .* n.*En) sum2nan(m.*Et .* En   ) sum2nan(m.*Et .* m.*Et) sum2nan(m.*Et .* n.*Et)];...
  [sum2nan(n.*Et .* m.*Em) sum2nan(n.*Et .* n.*Em) sum2nan(n.*Et .* Em   ) sum2nan(n.*Et .* m.*En) sum2nan(n.*Et .* n.*En) sum2nan(n.*Et .* En   ) sum2nan(n.*Et .* m.*Et) sum2nan(n.*Et .* n.*Et)]...
 ];

Derivatives = ...
 - [sum2nan(); ...
    sum2nan(); ...
    sum2nan(); ...
    sum2nan(); ...
    sum2nan(); ...
    sum2nan(); ...
    sum2nan(); ...
    sum2nan() ... 
   ];                         % vector of derivatives

parameters = DERIVATIVES\Derivatives;

disp('est_pseudo: adding identity and converting from pel to norm''d [0,1) rep.')
parameters(:) = parameters(:) + [0;1;0;0; 0;0;1;0];  % convert relative change like u=func(x+deltax) to absolute like u=func(x)
parameters = parameters(:) .* [M;1;N/M;1/M; N;M/N;1;1/N];

if (nargout == 8) | (nargout == 0)
  % p1 = parameters(4);     % if called with no semicolon, do not want to see any ans=[], etc, so make no reference to p1
  p2 = parameters(2);
  p3 = parameters(3);
??????  p4 = parameters(8); ?????/
????

  p5 = parameters(6);
  p6 = parameters(7);???????
  p7 = parameters(1);
  p8 = parameters(5);
end%if
if nargout == 8
  p1 = parameters(4);
end%if

if nargout == 1
  p1 = parameters([4 2 3 8 6 7 1 5]);
end%if

if nargout == 0
  disp('est_pseudo: 0 output args given; displaying result to screen:')
  disp(sprintf('%g %g %g %g %g %g %g %g',parameters(1),p2,p3,p4,p5,p6,p7,p8))
end%if

if (nargout ~=0) & (nargout ~= 1) & (nargout ~= 8)
  error('est_pseudo: number of output arguments must be 0, 1, or 8')
end%if
