function score = distfxn( orig, poten, thresh, overlap, height )
% DISTFXN Calculate  compatibility between rectangles for word groupings
%
%   SCORE = DISTFXN( ORIG, POTEN ), where ORIG is the 1x4 integer matrix that
%   represents the first, or potential predecessor, rectangle.  The matrix
%   is of the form [ starting_X_coordinate starting_Y_coordinate width
%   height ]. POTEN is the 1x4 integer matrix (of the same form) that
%   represents the second, or potential successor, rectangle.  SCORE is the
%   score we give to how good of a successor poten is to orig, with -Inf
%   designating incompatibility.
%
%   DISTFXN(...,THRESH), where THRESH is the threshold for determining
%   how far apart words can be before being thrown out as invalid
%   neighbors, normalized on average of the words' heights.  The
%   default is .60.  If you want to use the default for THRESH but not
%   the later optional parameters of DISTFXN, pass an empty matrix
%   where THRESH should be passed.
%
%   DISTFXN(...,OVERLAP), where OVERLAP is the threshold for
%   determining how much words can overlap before being regarded as
%   invalid neighbors, normalized on the smaller word's height.
%   The default is .15.  If you want to use the default for OVERLAP but not
%   the later optional parameters of DISTFXN, pass an empty matrix where
%   OVERLAP should be passed.
%
%   DISTFXN(...,HEIGHT), where HEIGHT is the threshold for
%   determining how much words can differ in height before being thrown out
%   as invalid neighbors, normalized on the smaller word's height.
%
%   DISTFXN uses the method described in Section 3 of the following article:
%
%     J. Weinman, Z. Butler, D. Knoll, and J. Feild, Toward Integrated Scene Text
%     Reading. IEEE TPAMI 36(2):375--387, 2014.doi:10.1109/TPAMI.2013.126
%
%   This function is to be input to WORDCAT, as the
%   scoring function.  It takes all rectangles that overlap in the x
%   direction and immediately calls them invalid successors.  It then
%   scores the remaining rectangles based on distance from the two
%   points left endpoints of the predecessor.  If the potential
%   successor is too far away, we just throw it out.
%
%   See also WORDCAT

% $Id$
 
% Jerod Weinman, Zach Butler, Dugan Knoll, Alexander Turner, Yirong Jing
% jerod@acm.org
% Copyright 2011, 2012, 2013, 2014
 
%    This file is part of PIONEER WordNorm.
%
%    PIONEER WordNorm is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    PIONEER WordNorm is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with PIONEER WordNorm.  If not, see <http://www.gnu.org/licenses/>.

% Copyright (c) 2012-2014
% Jerod Weinman
% jerod@acm.org


% Initialize defaults if they are not given
if(nargin < 5)
    height = .45;
end
if(nargin < 4 || isempty(overlap))
    overlap = .15;
end
if(nargin < 3 || isempty(thresh))
    thresh = .60;
end

% Check overlap case
if(poten(1) - (orig(1) + orig(3)) < (-(overlap) * min([orig(4) poten(4)])))
    score = -Inf;
    return;
end;
% Check for drastic height differences
if(max([orig(4) poten(4)]) * height > min([orig(4) poten(4)]))
    score = -Inf;
    return;
end;
% Compute distance from the top and bottom right of the predecessor to the
% top and bottom left of the successor
dist1 = sqrt( (orig(1) + orig(3) - poten(1) )^2 + (orig(2) - poten(2))^2 );
dist2 = sqrt( (orig(1) + orig(3) - poten(1) )^2 + (orig(2) + orig(4) - ...
    poten(2) - poten(4))^2 );
score = -(dist1 + dist2) / 2;
% If the distance is too far, throw out this case.
if(score < -(thresh * (orig(4) + poten(4))))
    score = -Inf;
end;
