#!/usr/bin/env python
"""
Axis-angle rotations
"""
import math
import numpy as np
# Local modules
import baldor as br
[docs]def to_euler(axis, angle, axes='sxyz'):
"""
Return Euler angles from a rotation in the axis-angle representation.
Parameters
----------
axis: array_like
axis around which the rotation occurs
angle: float
angle of rotation
axes: str, optional
Axis specification; one of 24 axis sequences as string or encoded tuple
Returns
-------
ai: float
First rotation angle (according to axes).
aj: float
Second rotation angle (according to axes).
ak: float
Third rotation angle (according to axes).
"""
T = br.axis_angle.to_transform(axis, angle)
return br.transform.to_euler(T, axes)
[docs]def to_quaternion(axis, angle, isunit=False):
"""
Return quaternion from a rotation in the axis-angle representation.
Parameters
----------
axis: array_like
axis around which the rotation occurs
angle: float
angle of rotation
Returns
-------
q: array_like
Quaternion in w, x, y z (real, then vector) format
Notes
-----
Quaternions :math:`w + ix + jy + kz` are represented as :math:`[w, x, y, z]`.
"""
u = np.array(axis)
if not isunit:
# Cannot divide in-place because input vector may be integer type,
# whereas output will be float type; this may raise an error in versions
# of numpy > 1.6.1
u = u / math.sqrt(np.dot(u, u))
t2 = angle / 2.
st2 = math.sin(t2)
return np.concatenate(([math.cos(t2)], u*st2))