function selectEpipolarLines(image1, image2, F)
%Joshua Stough
%COMP 290-89, Pollefeys
%Assignment 2, Computing F
%This function displays both images and allows the 
%user to select a point in one of the images.  Upon
%selection, both epipolar lines are shown in the
%two images.
    
I1 = double(rgb2gray(image1));
I1 = I1/max(max(I1));
I2 = double(rgb2gray(image2));
I2 = I2/max(max(I2));

s1 = size(I1);
Overlay1 = zeros(s1(1),s1(2),3);
s2 = size(I2);
Overlay2 = zeros(s2(1),s2(2),3);

Overlay1(:,:,1) = I1;
Overlay1(:,:,2) = I1;
Overlay1(:,:,3) = I1;

Overlay2(:,:,1) = I2;
Overlay2(:,:,2) = I2;
Overlay2(:,:,3) = I2;

h(1) = figure('Name','Image 1'); 
imshow(Overlay1, 'truesize');

h(2) = figure('Name','Image 2'); 
imshow(Overlay2, 'truesize');

%Let the user select a point in one image and
%redisplay both with highlights.  Then allow
%selection of a point in the other image.

[U,S,V] = svd(F);
%F*e = 0;
e = V(:,3);

[U,S,V] = svd(F');
ep = V(:,3);

c = -1;
quitter = 0;
while 1
    c = c + 1;
    if mod(c,2) == 0
        figure(h(1))
    else
        figure(h(2))
    end
    
    [x,y] = ginput(1);
    disp(sprintf('(%f , %f)', x, y));
    
    
    %HERE IS THE ONE PLACE WHERE COORDINATES ARE
    %REVERSED BECAUSE OF THE CONFUSING MATLAB 
    %SYNTAX.
    
    pix = [y x];
    if (isempty(pix)) 
        disp('Thanks for trying me out.');
        break
    end
    
    if mod(c,2) == 0
        lp = F*[pix 1]'; %on image 2;
        %l is then F'(e' x lp)
        l = F'*cross(e,lp);
    else
        l = F'*[pix 1]';
        lp = F*cross(ep,l);
    end
    
    %Now, pix is the pixel chosen, l is the
    %epipolar line in image1 and lp is the 
    %epipolar line in image2.
    
	if mod(c,2) == 0
        if (pix(1) > 0 & pix(2) > 0 & pix(1) <= s1(1) & pix(2) <= s1(2))
            Overlay1(pix(1)-1,pix(2)-1,2) = 1;
            Overlay1(pix(1)-1,pix(2),2) = 1;
            Overlay1(pix(1)-1,pix(2)+1,2) = 1;
            Overlay1(pix(1),pix(2)-1,2) = 1;
            %Overlay1(pix(1),pix(2),2) = 1;
            Overlay1(pix(1),pix(2)+1,2) = 1;
            Overlay1(pix(1)+1,pix(2)-1,2) = 1;
            Overlay1(pix(1)+1,pix(2),2) = 1;
            Overlay1(pix(1)+1,pix(2)+1,2) = 1;
            
            Overlay1(pix(1)-1,pix(2)-1,3) = 1;
            Overlay1(pix(1)-1,pix(2),3) = 1;
            Overlay1(pix(1)-1,pix(2)+1,3) = 1;
            Overlay1(pix(1),pix(2)-1,3) = 1;
            %Overlay1(pix(1),pix(2),3) = 1;
            Overlay1(pix(1),pix(2)+1,3) = 1;
            Overlay1(pix(1)+1,pix(2)-1,3) = 1;
            Overlay1(pix(1)+1,pix(2),3) = 1;
            Overlay1(pix(1)+1,pix(2)+1,3) = 1;
        end
        
	else
        %and the image2 point in cyan, on overlay 1.
        if (pix(1) > 0 & pix(2) > 0 & pix(1) <= s2(1) & pix(2) <= s2(2))
            Overlay2(pix(1)-1,pix(2)-1,2) = 1;
            Overlay2(pix(1)-1,pix(2),2) = 1;
            Overlay2(pix(1)-1,pix(2)+1,2) = 1;
            Overlay2(pix(1),pix(2)-1,2) = 1;
            %Overlay2(pix(1),pix(2),2) = 1;
            Overlay2(pix(1),pix(2)+1,2) = 1;
            Overlay2(pix(1)+1,pix(2)-1,2) = 1;
            Overlay2(pix(1)+1,pix(2),2) = 1;
            Overlay2(pix(1)+1,pix(2)+1,2) = 1;
            
            Overlay2(pix(1)-1,pix(2)-1,3) = 1;
            Overlay2(pix(1)-1,pix(2),3) = 1;
            Overlay2(pix(1)-1,pix(2)+1,3) = 1;
            Overlay2(pix(1),pix(2)-1,3) = 1;
            %Overlay2(pix(1),pix(2),3) = 1;
            Overlay2(pix(1),pix(2)+1,3) = 1;
            Overlay2(pix(1)+1,pix(2)-1,3) = 1;
            Overlay2(pix(1)+1,pix(2),3) = 1;
            Overlay2(pix(1)+1,pix(2)+1,3) = 1;
        end
	end
   
    %Potentially faster way to draw the lines on the images.
    if abs(l(2)) < 1e-15, l(2) = 1e-15; end
    if abs(lp(2)) < 1e-15, lp(2) = 1e-15; end
    
    si_l = [-l(1)/l(2) -l(3)/l(2)];
    si_lp = [-lp(1)/lp(2) -lp(3)/lp(2)];
    
    figure(h(1))
    line([sum(si_l) (si_l(1)*s1(1)+si_l(2))], [1 s1(1)], 'Color', [0 1 0]);
    
    figure(h(2))
    line([sum(si_lp) (si_lp(1)*s2(1)+si_lp(2))], [1 s2(1)], 'Color', [0 1 0]);
    
end
    
    
    
    
