function [ Dist ] = HullDist( Xi, Xj, L, U)
%HULLDIST Finds shortest distance between two hulls
%   The values of L and U define what type of hull the feature vectors
%   describe.

% In the case of a full affine hull: L=-inf , U=+inf. 
% In other words, the bounds are inactive
%
% In the case of a convex hull: L=0, U<1
%
% In the case of a reduced affine hull: L<0 , U>=1
%
%
%
%   Inputs:
%   Xi: Feature vector matrix of one set
%   Xj: Feature vector matrix of other set
%   L: Lower bound of alpha
%   U: Upper bound on alpha
%
%   Outputs:
%   Dist: The minimal distance between the selected hulls of the input features


size_ai=size(Xi,2);
size_aj=size(Xj,2);

cvx_begin quiet
% Variables: ai, aj
    variable ai(size_ai,1)
    variable aj(size_aj,1)
    
    minimize norm(Xi*ai - Xj*aj)
    
    subject to
        sum(ai)==1;
        sum(aj)==1;
        ai>=L;
        ai<=U;
        aj>=L;
        aj<=U;
cvx_end

Dist=cvx_optval;

end

