% Input:
% result: vector (containing the evaluation results)
% s: function handle (support function representing the same set as results)
% L: Matrix (containing row-wise direction vectors)
% R: Matrix (reset matrix)
% options: cell array
% W: function handle (support function
% nextstate: struct
% enable_invariants: boolean
% additional_dimension_constraints: vector

% Output:
% omega: function handle (support function of the set after the
% transformation with the reset matrix)

% Description:
% Generates the Support function of the set results/s after being resetted
% by R and W and intersects it with the invariant of the target location

function [omega] = calcReset(result,s,L,R,options,W,nextstate,enable_invariants,additional_dimension_constraints)

% assure R is not empty
if isempty(R)
    R = eye(size(L,2));
end;

accesssize = 1:size(result,1);
v = [zeros(1,size(L,2)-1) 1 ];
if ~isempty(additional_dimension_constraints) && ~ismember(v,L,'rows')
    result = [result;repmat(additional_dimension_constraints,1,size(result,2))];
    extendedL = [L; v; -v ];
else
    extendedL = L;
end;

if ~isempty(result)
    % assure the set is evaluationable
    %s = reconstructOmega(extendedL,result,L,options{2},options{3},[],'Reset calculation');   
    
    s = calcPolytopeSP(extendedL,result,options{2},options{3},zeros(size(L,2),1),[],[]);
end;

% reset with transformation matrix R and add W
omega = @(l) s( (l' * R)' ) + W(l);

if enable_invariants % => implies that results is not empty
    % transformations to intersect efficiently with target invariant without introducing problems
    result(accesssize) = sptotemplatepolyhedra(omega,L);

    if ~isempty(nextstate.complete_invariant_evaluation)
        result(accesssize,:) = min(result(accesssize,:),nextstate.complete_invariant_evaluation);   % autoreduces the results to accesssize
    end;
    
    % assure the set is evaluationable
    % omega = reconstructOmega(extendedL,results,L,options{2},options{3},[],'Reset calculation');
    omega = calcPolytopeSP(extendedL,result,options{2},options{3},zeros(size(L,2),1),[],[]);
end;
