function [distance emptyValue] = genericNonlinearCondition(x, grassStruct)
% compute minmal distance of projected extreme rays to cone
%
% THIS IS NO USER FUNCTION

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2013 Alexander Reinhold
%
% This program 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.
%
% This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

% Get the desired projection matrix
projectionMatrix = computeProjectionFromGrass(x, grassStruct);

% Take the enclosing extreme points of the data in scaled-PCA space and
%   compute their projection:
pointMatrixProjected = projectionMatrix * grassStruct.extremePointMatrix;

if any(all(...
        grassStruct.coneNormalVectorMatrix' * pointMatrixProjected < 0 ...
        , 1))
    % all projected vectors are inside the cone, everything OK
    distance = 0;
        
    %! Debug:
%     disp('validd')
%     color = [0 1 0];
    %/
else
    % some projected extreme ray is outside the cone - all
    % original to projected distances are:
    distanceVector = sqrt(sum((grassStruct.extremePointMatrix...
                             - pointMatrixProjected).^2, 1));
    % the shortest distance represents the distance of the current range of
    %   projection to a valid range of projection
    distance = min(distanceVector);
    
    %! Debug:
%     disp('INVALID')
%     color = [1 0 0];
    %/
end

if nargout > 1
    emptyValue = [];
end

%! Debug
% global thisDbgPoint
% set(thisDbgPoint, 'color', color, 'markerSize', 8)
%/