function [skel, eulerchannels] = expmap2euler(skel,expmapchannels)


% This is a function for converting to Euler BVH data from exponential maps
% It assumes that Neil Lawrence's MOCAP toolbox has been used to create a
% skeleton structure "skel" and matrix of channels "eulerchannels".
% This function proceeds through the skeleton, replacing the Euler angles
% for rotational dimensions with the expmap dof
%
% Usage: [skel, eulerchannels] = expmap2euler(skel,eulerchannels)


for ii=1:length(skel.tree)
    if ~isempty(skel.tree(ii).posInd)
        %don't touch translational dimensions
        eulerchannels(skel.tree(ii).posInd) = expmapchannels(skel.tree(ii).posInd);
    end
    
    counter = length(eulerchannels)+1;
    
    if ~isempty(skel.tree(ii).rotInd)
        
        R = RotationMatrix(expmapchannels(:,skel.tree(ii).expmapInd),'exponentialMap');
        
        euch = R.GetEulerAngles();
        
        eulerchannels(counter:counter+2) = euch(2,:);
        
        skel.tree(ii).rotInd = counter:counter+2;
        counter = counter+3;
        
    end
end


