%COMP 256 Assignment 2
%Adrian Ilie

function res=homography(f,ep,rp)
%computes a homography for rectification (as in Hartley)

%compute translation from rp to origin
T=eye(3);
T(1,3)=-rp(1);
T(2,3)=-rp(2);

%compute the rotation of translated epipole to (f,0,1)'
%translate the epipole
et=T*ep;

%compute rotation
if et(1)~=0 %not vertical line
  theta=atan(et(2)/et(1));
else
  theta=pi;  
end
R=eye(3);
R(1,1)=cos(theta);
R(2,2)=cos(theta);
R(1,2)=sin(theta);
R(2,1)=-sin(theta);

%rotate the epipole
etr=R*et;

%send the epipole to infinity
K=eye(3);
K(3,1)=-1/etr(1);

res=K*R*T;
