Ok, the point is that once we derive our formulas for the joint angles (A_j and B_j) using the 3rd column of the tranformation matrix as described in my last post we have something like this.
A_j = acos(2*Kwy - 1)
Sa_j = sin(A_j)
Ca_j = cos(A_j)
B_j = asin(2*(sqrt(2)*Kwz*Sa_j - (Ca_j + 1)*Kwx)/(Ca_j*Ca_j + 2*Sa_j*Sa_j + 2*Ca_j + 1))
If you plug that into the remap and test the simulation you will notice that there are ranges of tool-orientations where the resulting angles are offset to what you need to get the spindle correctly oriented. In some ranges it will fit in others it is the complement of what you need or the you need to change the sign of the value. These 'areas' is what I call 'quadrants' in the remap. A lot of this came through trial and error along the lines of 'from here to here need to add 'pi' and from there to there I need to subtract half a pi and change the sign'.
Unfortunately I don't recall how I came up with the conditionals:
if sqrt(2)*Kwz*Sa_j <= Sa_j*Sa_j:
I'll see if that comes back to me.
Here is a somewhat expanded, later version:
NB: clearly some of the comments in the code make no sense. Like this one:
# Kwz < 0 AND Kwz < 0
These are a remnant of the stage where I thought I could keep the quadrants apart by checking whether individual elements of the tool-vector were positive or negative. Alas it's not quite so simple.