function [F] = basic_cost_nonlin(x)
%Joshua Stough
%COMP 290-89, Pollefeys
%Assignment 2, Computing F

%The cost being computed here is 
%the average distance to the corresponding
%epipoles for the two directions.


global inliers X XP

Fund = reshape(x,3,3);

%F = XP'*Fund*X; Didn't work at all.

lx = Fund*X;
lxp = Fund'*XP;
%normalize the lines.
lxnorms = sqrt(sum(lx(1:2,:).^2));
lxpnorms = sqrt(sum(lxp(1:2,:).^2));
lxnorms(lxnorms == 0) = 1e-15;
lxpnorms(lxpnorms == 0) = 1e-15;

lx = lx./(ones(3,1)*lxnorms);
lxp = lxp./(ones(3,1)*lxpnorms);

%perpedicular distance from a point to the line it
%is supposed to be on is the dot product.
%square distances in image1 and two.
dist_1 = abs(sum(lxp.*X));
dist_2 = abs(sum(lx.*XP));

F = mean([dist_1; dist_2])';
