function [ data, startFace ] = ty_pre(data)
  %addpath('/home/omid/Code/deepmove/Libs/MocapToolbox/MOCAP0p136');
  %addpath('/home/omid/Code/deepmove/Libs/TaylorScripts');
  addpath('matlab');

  %skel = load('skel_ms3x.mat').skel_ms3x;
  
  newrep = []; %the "body-centred" representation for orientation
  grdvel = []; %the relative-to-body representation for x,y velocity
              %the third component is the delta for newrep(3), i.e. about z               
              

  myvec = [0 0 1]; %straight ahead
  myvec2 = [1 0 0]; %out right hip
  vertvec = [0 1 0]; %positive vertical axis
  
  
  
  %find the rotation matrix based on the root Exponential map angles
  %for this frame
  %rootRotInd = skel.tree(1).expmapInd;
  fprintf(1, '----- 1');


    %BEGIN FIRST PART - BODY CENTRED COORDINATES  
    for ii=1:size(data,1)
      
      fdata = data(ii,:); %single frame    

      R = expmap2rotmat(fdata(:,4:6));
      % R = rotationMatrix(fdata(:,4), fdata(:,5), fdata(:,6), 'xyz');
    
      %what do our normal vectors become?
      newvec = myvec*R; %calculate myvec rotated by the root orientation
      U = myvec2*R;
      
      %record the angles the new normal vectors make with the -z axis    
      ct = (newvec*-vertvec')/norm(newvec);    
      %data(ii,1) = acos(ct);  
      newrep(ii,1) = acos(ct);
      ct2 = (U*-vertvec')/norm(U);
      %data(ii,2) = acos(ct2);
      newrep(ii,2) = acos(ct2);
          
      %We want our "about" vertical to be meaningful, so the angle being
      %stored is the "change" from the "rest" position
      %Since atan2 always works with the x axis as its base angle, for the
      %CMU data (with the subject by default facing the z-axis), 
      %we will measure U
      %With the MIT data, "rest" position is to face straight ahead in the
      %x-axis, so we naturally measure newvec
      %projection is simply the x and z components

      %Note that angle will be calculated from the x axis
      %In the CMU case, this is U (in the zero position)
      ux = U(1);
      uz = U(3);
  
      
      %Using the atan2 function we can calculate the angle between -pi and pi
      %given the horizontal and vertical components        

      newrep(ii,3) = atan2(uz,ux);
    end
    
    startFace = newrep(1,3);
    %END FIRST PART - BODY CENTRED COORDINATES
    %BEGIN SECOND PART - GROUND PLANE VECTORS
    
    %Since the atan2 function returns an angle between -pi and pi, we now do
    %phase unwrapping to recognize the fact that within a sequence we may
    %turn more than 2pi radians
    %The unwrapping maintains the continuity of the turn  
    newrep(:,3) = unwrap(newrep(:,3));

    for ii=1:size(data,1)
      if ii<size(data,1)
        %store differences - dimension 3: about-vertical rotation
        

        % dimension 1: - x position, dimension 3: z position
        mydif = [newrep(ii+1,3) data(ii+1,[1 3])] - ...
          [newrep(ii,3) data(ii,[1 3])];
      
              
        %We also want to represent our horizontal position in terms of the
        %body-centred coordinate system
        mh = norm([mydif(2) mydif(3)]); %mag  of the horizontal movemt vector
        %angle of the horizontal position vector (counterclk from x axis)
        apos = atan2(mydif(3),mydif(2));
        %angle of the normal vector straight out of the body (projection in xz)
        %No need to restrict it to 0,2*pi (or any range)
        %Nor do we need to restrict the offset to any range
        %As the offset (whatever it is, goes through cos & sin)
        anorm = newrep(ii,3);
        

        %For CMU, the u (cos) component is the compoent in line with the
        %"straight out of right hip" vector      
        %u component of movement vector 
        grdvel(ii,1) = mh*cos(anorm - apos);
        %v component of movement vector 
        grdvel(ii,2) = mh*sin(anorm - apos);
        
        %Store the differences in the data cell for the about-vertical rot
        grdvel(ii,3) = mydif(1);
      else
        %don't care about the last frame, because we're taking differences
        grdvel(ii,1:3) = grdvel(ii-1,1:3);
      end
    end

    %Absolute vertical position - dimension 2 is the one we don't want to
    %touch
  
    data(:,6) = data(:,2);   

    data(:,1:2) = newrep(:,1:2); %body-centred orientations
    data(:,3) = grdvel(:,3);     %delta about-z orientation
    data(:,4:5) = grdvel(:,1:2); %ground plane relative velocities      
end