%  SplitSeqRot Compute the AD or DA splittings of a sequence
%
%    [Glrot,Glind,Grrot,Grind]=SplitSeqRot(Grot, Gind,tp,fr)
%    Grot and Gind determine the input sequence of n-1 rotations.
%    Glrot, Glind -> The left part of the sequence 
%    Grrot, Grind -> The right part of the sequene
%    tp is a character array indicating the positions:
%       'ad' means ascending left and descending right
%       'da' means descending left and ascending right
%
%    fr is an optional value to specify the position of the free rotation
%    this is standard set to l (left), other possibility is r (right)
%    In case tp = ad, the first rotation is free
%    In case tp = da, the last rotation is free
%
%    Software written by Raf Vandebril
%    raf.vandebril-at-cs.kuleuven.be
%    Revision Date: September 8, 2010
%


function [Glrot,Glind,Grrot,Grind]=SplitSeqRot(Grot, Gind,tp,fr)
  
   % The order needs to be specified
     if (~exist('tp'))
       error('No valid order specified, please specify ad or da')
     end
     if ~( (tp=='ad') | (tp=='da'))
       error('No valid value for tp, please specify ad or da')
     end
     
   % Check if the position for the first transformation is specified
   if (~exist('fr'))
     fr='l'
   end
   
     
     
   % Initialization
     if ~exist('n')
       n=max(Gind(:,2));
     end
    
  
   % Free memory for the variables
   Glrot=[];
   Glind=[];
   Grrot=[];
   Grind=[];
 
   % To make this task easy, we reorder the rotations, 
   % according to the first column, so we can go from top to bottom
   [SG,I]=sort(Gind(:,1));
   Gind=Gind(I,:); % Rorder the index storage
    Grot=Grot(:,I); % Rorder the rotations themselves

   
   
   % We make two separate loops for ordering the rotations, otherwise it
   % gets to complicated
   
   if (tp=='ad')
     % In case the left sequence is ascending and the right, we 
     % can freely choose the position of the first rotation
     if (fr=='l')
       Glrot(:,1)=Grot(:,1);
       Glind(1,:)=Gind(1,:);
     else
       Grrot(:,1)=Grot(:,1);
       Grind(1,:)=Gind(1,:);
     end
     % The trailing rotation is however blocked, and one cannot choose
     % where it will go to
     
     % The position of a rotation is determined by the relative position
     % wrt to the preceding rotation.
     
     for i=2:n-1
       if (Gind(i,3)<Gind(i-1,3))      
         Glrot(:,end+1)=Grot(:,i);
         Glind(end+1,:)=Gind(i,:);
       else
         Grrot(:,end+1)=Grot(:,i);
         Grind(end+1,:)=Gind(i,:);
       end
     end
     
   else
     % In case the left sequence is descending and the right one
     % ascending, the position of a rotation is determined by the
     % position of the next one. Since for the trailing rotation, there
     % is no next one, it can move freely
     
     for i=1:n-2
       if (Gind(i,3) < Gind(i+1,3))      
         Glrot(:,end+1)=Grot(:,i);
         Glind(end+1,:)=Gind(i,:);
       else
         Grrot(:,end+1)=Grot(:,i);
         Grind(end+1,:)=Gind(i,:);
       end
     end
     
     % Since the trailing rotation is free, specify now its position.
     if (fr=='l')
       Glrot(:,end+1)=Grot(:,n-1);
       Glind(end+1,:)=Gind(n-1,:);
     else
       Grrot(:,end+1)=Grot(:,n-1);
       Grind(end+1,:)=Gind(n-1,:);
     end
     
   end
   
  
