function [skel, expmapchannels] = bvh2expmap_skipbad(skel,eulerchannels,lastexpmapchannels, frameN)


% This is a function for converting from Euler BVH data to 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, expmapchannels] = bvh2expmap(skel,eulerchannels)


for ii=1:length(skel.tree)
    if ~isempty(skel.tree(ii).posInd)
        %don't touch translational dimensions
        expmapchannels(skel.tree(ii).posInd) = eulerchannels(skel.tree(ii).posInd);
    end
    
    counter = length(expmapchannels)+1;
    
    
    if ~isempty(skel.tree(ii).rotInd)
        xangle = deg2rad(eulerchannels(skel.tree(ii).rotInd(1)));
        yangle = deg2rad(eulerchannels(skel.tree(ii).rotInd(2)));
        zangle = deg2rad(eulerchannels(skel.tree(ii).rotInd(3)));
        
        %3x3 Rotation Matrix
        thisRotation = rotationMatrix(xangle, yangle, zangle, skel.tree(ii).order);
        
        if (abs(norm(rotmat2quat(thisRotation))-1)>1E-3)
            disp(['Error at frame ',num2str(frameN),' input quaternion is not norm 1. Using the previous frame instead']);
           
            expmapchannels(counter:counter+2) = lastexpmapchannels(counter:counter+2);
        else
            
            r = rotmat2expmap(thisRotation);
            
            %Conversion to Exponential Map
            %Overwriting roational channels
            expmapchannels(:,skel.tree(ii).rotInd) = r;
            
            %write out the exponential map dimensions
            expmapchannels(counter:counter+2) = r;
       end     
            skel.tree(ii).expmapInd = counter:counter+2;
            counter = counter+3;
        
    end
    
    
    
end


