def euler_to_quaternion(euler: Euler, sequence: str = "ZYX", degrees: bool = False) -> Quaternion: """ Transform a quaternion into Euler angles. :param euler: An Euler object. :param sequence: Rotation sequence for the Euler angles. :param degrees: Set to true if the provided Euler angles are in degrees. Default is radians. :return: Quaternion object. """ rotation_object: Rotation = Rotation.from_euler(seq=sequence, angles=euler.as_np_array(), degrees=degrees) quaternion: Quaternion = Quaternion.from_array(rotation_object.as_quat(), frame="robot") return quaternion
def quaternion_to_euler( quaternion: Quaternion, sequence: str = "ZYX", degrees: bool = False ) -> Euler: """ Transform a quaternion into Euler angles. :param quaternion: A Quaternion object. :param sequence: Rotation sequence for the Euler angles. :param degrees: Set to true if the resulting Euler angles should be in degrees. Default is radians. :return: Euler object. """ rotation_object: Rotation = Rotation.from_quat(quaternion.as_np_array()) euler: Euler = Euler.from_array( rotation_object.as_euler(sequence, degrees=degrees), from_="robot", to_="asset", ) return euler
import numpy as np import pytest from alitra import Euler, Quaternion from alitra.convert.euler import euler_to_quaternion @pytest.mark.parametrize( "euler, expected", [ ( Euler(psi=0, phi=0, theta=0, from_="robot", to_="asset"), Quaternion(x=0, y=0, z=0, w=1, frame="robot"), ), ( Euler( psi=1.5707963, # Z theta=0.5235988, # Y phi=1.0471976, # X from_="robot", to_="asset", ), Quaternion(x=0.1830127, y=0.5, z=0.5, w=0.6830127, frame="robot"), ), ], ) def test_quaternion_to_euler(euler, expected): quaternion: Quaternion = euler_to_quaternion( euler=euler, sequence="ZYX", degrees=False )
def test_transform(): with pytest.raises(ValueError): euler = Euler(from_="robot", to_="asset") translation = Translation(1, 1, from_="robot", to_="asset") Transform.from_euler_ZYX(translation, euler=euler, from_="asset", to_="robot")
def test_euler(): with pytest.raises(ValueError): Euler.from_array(np.array([1, 1]), from_="robot", to_="asset")
import numpy as np import pytest from alitra import AlignFrames, Euler, FrameTransform, PointList, Translation, Transform @pytest.mark.parametrize( "eul_rot, ref_translations, p_robot,rotation_axes", [ ( Euler(psi=np.pi * -0.0, from_="robot", to_="asset"), Translation(x=200030, y=10000, from_="robot", to_="asset"), PointList.from_array( np.array([ [10, 1, 0], [20, 2, 0], [30, 7, 0], [40, 5, 0], ]), frame="robot", ), "z", ), ( Euler(psi=np.pi / 2, from_="robot", to_="asset"), Translation(x=10, y=0, from_="robot", to_="asset"), PointList.from_array( np.array([[5, 0, 0], [5, 2, 0], [7, 5, 0], [3, 5, 0]]), frame="robot", ), "z",