function points = lineToBorderPoints(lines,imageSize)
%lineToBorderPoints Compute the intersection points of lines and image border.
%   POINTS = lineToBorderPoints(LINES,IMAGESIZE) computes the intersection
%   points between one or more lines with the image border.
% 
%     LINES is a M-by-3 matrix where each row has the format of [A,B,C]
%     which defines a line as A * x + B * y + C = 0. M is the number of
%     lines.
%
%     IMAGESIZE is the size of the image, and is in the format returned by
%     the function SIZE.
%
%     POINTS is an M-by-4 matrix where each row has the format of
%     [x1,y1,x2,y2], where [x1,y1] and [x2,y2] are the two intersection
%     points. When a line and the image border do not intersect, the
%     function returns [-1,-1,-1,-1].
%
%   Class Support
%   -------------
%   LINES must be double or single. IMAGESIZE must be double, single, or
%   integer.
%
%   Example
%   -------
%   % Load and display an image.
%   I = imread('rice.png');
%   figure; imshow(I); hold on;
%   % Define a line: 2 * x + y - 300 = 0
%   aLine = [2,1,-300];
%   % Compute the intersection points of the line and the image border.
%   points = lineToBorderPoints(aLine, size(I));
%   line(points([1,3]), points([2,4]));
%
% See also epipolarLine, line, size, vision.ShapeInserter.

% Copyright 2010 The MathWorks, Inc.
% $Revision: 1.1.8.3 $  $Date: 2011/02/21 07:17:33 $

isXY = cvstGetCoordsChoice('fcnLineToBorderPoints');
checkInputs(lines, imageSize, isXY);
points = cvalgLineToBorderPoints(lines, imageSize, isXY);

%========================================================================== 
function checkInputs(lines, imageSize, isXY)
%--------------------------------------------------------------------------
% Check LINES
%--------------------------------------------------------------------------
if isXY
  lineSize = [NaN, 3];
else
  lineSize = [3, NaN];
end

validateattributes(lines, {'double', 'single'}, ...
  {'2d', 'nonsparse', 'nonempty', 'real', 'size', lineSize},...
  mfilename, 'LINES');

%--------------------------------------------------------------------------
% Check IMAGESIZE
%--------------------------------------------------------------------------
validateattributes(imageSize, {'single', 'double', 'int8', 'int16', ...
  'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'}, ...
  {'vector', 'nonsparse', 'nonempty', 'real', 'positive', 'integer'},...
  mfilename, 'IMAGESIZE');

if length(imageSize) < 2
  error('vision:lineToBorderPoints:invalidIMAGESIZE', ...
    'Expected IMAGESIZE to be a vector of two or more elements.');
end
