Orientation Wrapping

Hey

I was trying to get quick prototype of Orientation Wrapping.
I got some basic part working, (character rotate in the correct direction), what I’m stuck at is combing reorientation of character with animation switching in cardinal direction.

I just run out of ideas how to reliably calculate cardinal direction of movement of character.
Calculating direction’s of movement is quite trivial when character is moving only in one direction, getting all angles etc as well, but as soon character start turning or moving diagonally it all fall apart.
And when combined with Reorienting of root bone, hell opens (animation just start to change directions seemgly randomly).

I wonder how the cardinal directions are calculated in Paragon, as they look really stable almost as per bone blend of different animations.