function [isIn,epipole] = cvalgIsEpipoleInImage(f,imageSize,isXY)
% Algorithm for Determining whether the epipole is inside the image.
%   This function supports the isEpipoleInImage function.

% Copyright 2010 The MathWorks, Inc.
% $Revision: 1.1.6.3 $  $Date: 2011/02/21 07:17:28 $
%#codegen

% Compute the homogeneous coordinates for the epipole.
[~, ~, v] = svd(f);

if isXY
  epipoleHomogeneous = v(:, 3)';
else
  epipoleHomogeneous = v(:, 3);
end

outputClass = class(f);
if epipoleHomogeneous(3) ~= 0
  % Compute the Cartesian coordinates for the epipole and check if the
  % epipole is inside the image.
  epipole = epipoleHomogeneous(1:2) / epipoleHomogeneous(3);
  if isXY
    imageOrigin = cast([0.5, 0.5], outputClass);
    imageEnd = imageOrigin + cast([imageSize(2),imageSize(1)],outputClass);
  else
    imageOrigin = cast([-0.5; -0.5], outputClass);
    imageEnd = imageOrigin + cast([imageSize(1);imageSize(2)],outputClass);
  end
  
  isIn = all(epipole >= imageOrigin & epipole <= imageEnd);
else
  % The epipole is at the location of infinity. 
  epipole = sign(epipoleHomogeneous(1:2)) * realmax(outputClass);
  isIn = false;
end
