% interactive dechirp rechirp with correspondences (2 images)       Steve Mann
%
% mouse clicks...
%
% Examples: p = idr(4,5);    % p between figures 4 and 5
%                            % (assumes relevant images displayed there)

function p = nasty(fignum1,fignum2)

error('idrcorr.m: not done yet')

disp('idr: reversing roles of (x,y) and (u,v); still not sure why')

[M,N]=size(A);

x=[];  % not sure how long to make these
y=[];
u=[];
v=[];
fprintf('idr: click for (x,y)\r')
[y1,x1,button1]=ginput(1);  % Matlab x,y reversed from m,n
%point(x1,y1,'+')
fprintf('idr: click for (u,v)\r')
[v1,u1,button1]=ginput(1);
%point(u1,v1,'x')
%fprintf('\idr: got point 1\n\r')

fprintf('idr: click for (x,y)\r')
[y2,x2,button1]=ginput(1);
%point(x2,x2,'+')
fprintf('idr: click for (u,v)\r')
[v2,u2,button1]=ginput(1);
%point(u2,v2,'x')
%fprintf('\idr: got point 2\n\r')

fprintf('idr: click for (x,y)\r')
[y3,x3,button1]=ginput(1);
%point(x3,x3,'+')
fprintf('idr: click for (u,v)\r')
[v3,u3,button1]=ginput(1);
%point(u3,v3,'x')
%fprintf('\idr: got point 3\n\r');

fprintf('idr: click for (x,y)\r')
[y4,x4,button1]=ginput(1);
%point(x4,x4,'+')
fprintf('idr: click for (u,v)\r')
[v4,u4,button1]=ginput(1);
%point(u4,v4,'x')
fprintf('idr: got all 4 points\r');

lrtb=axis;
left=lrtb(1); right=lrtb(2); top=lrtb(3); bottom=lrtb(4);
pixelwidth=(right-left)/N;
pixelheight=(bottom-top)/M;
% pixels begin at upperleft corner (e.g. try tvs(eye(3)) to see how arranged)
x=[x1;x2;x3;x4];
y=[y1;y2;y3;y4];
u=[u1;u2;u3;u4];
v=[v1;v2;v3;v4];

%disp(sprintf('idr: x=[%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f]',x));
%disp(sprintf('idr: y=[%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f]',y));
%disp(sprintf('idr: u=[%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f]',u));
%disp(sprintf('idr: v=[%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f]',v));

y=y-left-pixelwidth/2;  % e.g. upr left corner is negative, considering size of pixel
x=x-top-pixelheight/2;
v=v-left-pixelwidth/2;
u=u-top-pixelheight/2;

%disp(sprintf('idr: x=[%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f]',x));
%disp(sprintf('idr: y=[%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f]',y));
%disp(sprintf('idr: u=[%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f]',u));
%disp(sprintf('idr: v=[%+10.4f,      %+10.4f,      %+10.4f,      %+10.4f]',v));

y=y/(right-left-pixelwidth);
x=x/(bottom-top-pixelheight);
v=v/(right-left-pixelwidth);
u=u/(bottom-top-pixelheight);
% now centers of extreme pixels should be on the interval 0..1

disp(sprintf('idr: x=[%+10.4f, %+10.4f, %+10.4f, %+10.4f]',x));
disp(sprintf('idr: y=[%+10.4f, %+10.4f, %+10.4f, %+10.4f]',y));
disp(sprintf('idr: u=[%+10.4f, %+10.4f, %+10.4f, %+10.4f]',u));
disp(sprintf('idr: v=[%+10.4f, %+10.4f, %+10.4f, %+10.4f]',v));

%%%p=corr2p(x,y,u,v)
p=corr2p(u,v,x,y);
%%%p=corr2p(v,u,y,x)

%B=pchirp2(p,A,1,4);  % bilinear interp and quadrilateral antialias
disp('idr: using no antialias, in order to avoid crossover artifacts')
B=pchirp2(p,A);  % default bilinear interp and default no antialias
%figure
%tvs(B)

if nargout==1
  disp('idr: only 1 output arg, so displaying p vector to screen:')
  disp(sprintf('p_vec: %+10.6f %+10.6f %+10.6f    %+10.6f %+10.6f %+10.6f',...
               p(1),p(2),p(3),p(4),p(5),p(6)))
  disp(sprintf('       %+10.6f                          %+10.6f',p(7),p(8)))

end%if

if nargout == 0
  error('idr: you must have an output argument to capture the dr image')
end%if

