function c = EMP8_cost(s)
% EMP8_cost        Return (and show) the cost (energy) for a labeled image
%                  EMP8: Energy Minimization using Potts model and 8 neighbors
%
% This function use:
%   EMP8_cost_mex(..)  to find cost for input labeling
%
%  Cost is both returned in scaled form, each term in range 0 to 1, as
%     Etot = Edata + lambda*(E4 + E8); 
%  or as (large) integers for sum in Ri (R scaled and represented as int16)
%     Ctot = Cdata + C4 + C8;
%  where Cdata is sum of the cost in Ri for labeling Li
%  and C4 = n4*bF4 and C8 = n8*bF8
%  and n4 is number of horizontal and vertical edges between labeled pixels
%  and n8 is number of diagonal 'edges' between labeled pixels
%
% Use of function:
% ----------------
%   c = EMP8_cost(s)
% 
%     c   is a struct with cost results in each field
%     s   is a struct as returned by EMP8_argin, see help for that file
% 
% Example:  (see also EMP8_ex2.m)
% -------------------------------------------------------
%   clear all; tim = 4;  sigma = 5;  
%   s = EMP8_argin(tim, sigma, tim, 'v',2, 'lambda',3);   
%   newLambda = 5; 
%   t = sprintf(s.formatCost, ['lambda=',num2str(newLambda)], s.Ci.Edata, newLambda, ...
%               s.Ci.E4, s.Ci.E8, s.Ci.Edata+newLambda*(s.Ci.E4+s.Ci.E8), 100*s.Ci.errorRate);
%   disp(t); disp(' ');
%   s.lambda = newLambda;  
%   s.bF4 = -1;              
%   c = EMP8_cost(s);

%----------------------------------------------------------------------
% Copyright (c) 2013.  Karl Skretting.  All rights reserved.
% University of Stavanger, Signal Processing Group
% Mail:  karl.skretting@uis.no   Homepage:  http://www.ux.uis.no/~karlsk/
% 
% HISTORY:  dd.mm.yyyy
% Ver. 1.0  21.05.2013  Make function
% Ver. 1.1  10.12.2013  Use s (struct) as input
% Ver. 1.2  14.10.2014  s is not changed as it is not returned
%----------------------------------------------------------------------

if ~exist('EMP8_cost_mex','file') 
    % old function (EMP8_cost.mold and LIS_cost.m) had implemented cost
    % also without using mex-file (only for swUpq == 1)
    c = [mfilename,': EMP8_cost_mex is not availble.'];
    disp(c);
    return;
end

param = [s.M,s.I,s.J,-1, s.bF4,s.bF8,s.swUpq,0, s.lambda, s.dR, s.minsumRi, s.maxsumRi, ...
         s.numPix1, s.numPix2, s.shapeFactor, -1];   % 16 parameters for mex-file
if (s.bF4 < 0) 
    param(6) = s.bF8percent; 
elseif (s.bF8 < 0)  % and (bF4 >= 0)
    param(6) = floor(s.bF4*(s.bF8percent/100)+0.5);
end
%
tic;
if (s.swUpq == 1)
    Ci = EMP8_cost_mex(s.Ri, s.Li, param);
elseif (s.swUpq == 2)
    Ci = EMP8_cost_mex(s.Ri, s.Li, s.Upq, param);
elseif (s.swUpq == 3)
    Ci = EMP8_cost_mex(s.Ri, s.Li, s.Uim, s.Utab, param);
end
Ci(18) = toc;
if (numel(s.Lt) == (s.I*s.J))
    er = nnz(s.Li ~= s.Lt)/(s.I*s.J);
    Ci(20) = er;
else
    er = -0.01;
end
c = costTilStruct(Ci);

if (s.verbose > 1)  % very verbose
    disp([mfilename,': Find cost for input labeling, s.Li.']);
    disp([' c.bF4=',int2str(c.bF4),', c.bF8=',int2str(c.bF8),', lambda=',num2str(s.lambda), ...
        ', bF8percent=',int2str(s.bF8percent)  ]);
    disp([' c.dR=',int2str(c.dR),', c.minsumRi=',int2str(c.minsumRi),...
        ', c.maxsumRi=',int2str(c.maxsumRi)  ]);
    c.ti = sprintf(s.formatCost, 'Li', Ci([10,13,11,12,9]), 100*er);
    if (er < 0) 
        % it is nicer to print nothing rather the negative error rate
        c.ti = [c.ti(1:72),'.'];
    end
    disp(c.ti);
end

return


function s = costTilStruct(c)
if (numel(c)<20)
    c = [c(:);-ones(20-numel(c),1)];
end
s = struct('Ctot',c(1),'Cdata',c(2),'C4',c(3),'C8',c(4),...
           'bF4',c(5),'bF8',c(6),'n4',c(7),'n8',c(8),...
           'Etot',c(9),'Edata',c(10),'E4',c(11),'E8',c(12),...
           'lambda',c(13),'dR',c(14),'minsumRi',c(15),'maxsumRi',c(16),...
           'updated',c(17),'time',c(18),'unused',c(19),'errorRate',c(20));
return;
       

% function s = paramTilStruct(p)
% if (numel(p)<16)
%     p = [p(:);-ones(16-numel(p),1)];
% end
% s = struct('M',p(1),'I',p(2),'J',p(3),'P',p(4),...
%            'bF4',p(5),'bF8',p(6),'swUpq',p(7),'verbose',p(8),...
%            'alpha',p(9),'dR',p(10),'minsumRi',p(11),'maxsumRi',p(12),...
%            'lowerSize',p(13),'upperSize',p(14),'shapeFactor',p(15),'unused',p(16));
% return;
% 

