% Input:
% L: matrix (containing directions)

% Output:
% reducedL: matrix of integers

% Description:
% Generates matrix m from L, which contains the mapping of each direction from
% L to its correspondent opposite in L, and computes the minimal set of
% direction-opposite direction pairs such that the pairs cover all
% directions in L. Hence, reducedL contains the indices of all directions
% exactly once. However, 0 might appear multiple times for directions not
% having an opposite direction in L

function [reducedL] = reduceL(L)

m = findLpairs(L);

primaryL = [];
list = [];  % directions covered by indexes from primaryL
for i=1:size(m,1)
    if ~ismember(m(i,1),list) && ~ismember(m(i,2),list)   % the actual direction under consideration and its opposite have not been considered yet
        primaryL = [primaryL m(i,1)];
        list = [list m(i,1)];
        if m(i,2)~=0
            % the actual transition covers another direction
            list = [list m(i,2)];
        end;
    end;
end;

reducedL = m(primaryL,:);



% Input:
% L: matrix (containing row-wise directions)

% Output:
% m: matrix

% Description:
% contains for each direction l from L the index of the opposite direction in
% L in its 2nd column. The first column contains the index of l in L
% If there is no opposite direction the 2nd column contains 0
function [m] = findLpairs(L)

m = zeros(size(L,1),2);
for i=1:size(L,1)
    m(i,1) = i;
    l = -L(i,:);
    
    [member,index] = ismember(l,L,'rows');
    if member
        m(i,2) = index;
    end;
end; 