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

% Output:
% index2: vector

% Description:
% Computes a mapping from the row-wise directions in directions to the row
% wise entries in L. The normalization and rounding operations have to be
% equal to the procedures used in extendDirections (used to extend L) in
% order to be able to find such a mapping.

function [index2] = computeMappingToL(directions,L)
global precision;

if isempty(L)
    error('No Matrix to search!');
end;

if ~isempty(directions)
    % scale directions to length 1 and round in order to generate a certain
    % tolerance (this procedure has to be identic to the generation of
    % additional dimensions of L to be able to find such directions in L
    directions = directions./repmat(sqrt(sum(abs(directions).^2,2)),1,size(directions,2));
    directions = round(directions*precision)/precision;
    
    index2 = zeros(size(directions,1),1);

    for i=1:size(directions,1)
        [member,index] = ismember(directions(i,:),L,'rows');

        if member
            index2(i) = index(1);
        else
            error('There is a direction not contained in L!');
        end;
    end;
else
    index2 = [];
end;