py3dframe.Frame.from_quaternion#
- classmethod Frame.from_quaternion(translation=None, quaternion=None, *, parent=None, setup_only=False, convention=0, scalar_first=True)[source]#
Create a Frame object from a quaternion and translation.
The quaternion must be in the [w, x, y, z] format if
scalar_firstis True and in the [x, y, z, w] format ifscalar_firstis False.See also
from_rotation()method to create a Frame from a Rotation object instead of a quaternion.from_rotation_matrix()method to create a Frame from a rotation matrix instead of a quaternion.from_euler_angles()method to create a Frame from euler angles instead of a quaternion.from_rotation_vector()method to create a Frame from a rotation vector instead of a quaternion.from_axes()method to create a Frame from its axes (origin and basis vectors) instead of a quaternion and a translation
- Parameters:
translation (numpy.ndarray, optional) – The translation vector of the transformation. It must be a 3 elements vector with shape (3, 1). Default is None - the zero vector.
quaternion (numpy.ndarray, optional) – The quaternion of the transformation. It must be a 4 elements vector [w, x, y, z] (scalar first convention). Default is None - the identity quaternion.
parent (Optional[Frame], optional) – The parent frame of the frame. Default is None - the global frame.
setup_only (bool, optional) – If True, the parent frame will be used only to define the frame and not to link the frames. Default is False.
convention (int, optional) – Integer in
[0, 7]selecting the convention to express the transformation. Default is 0.scalar_first (bool, optional) – If True, the quaternion is in the [w, x, y, z] format. If False, the quaternion is in the [x, y, z, w] format. Default is True.
- Returns:
The Frame object created from the given quaternion and translation.
- Return type:
Examples
Lets create a frame from a quaternion and a translation.
We want to create the frame defined by the following origin and basis vectors in the parent frame coordinates:
Origin: \(O_F = [-1, -2, -3]\)
X-axis: \(\mathbf{e}_1 = [1, 1, 0] / \sqrt{2}\)
Y-axis: \(\mathbf{e}_2 = [-1, 1, 0] / \sqrt{2}\)
Z-axis: \(\mathbf{e}_3 = [0, 0, 1]\)
With convention 0, the frame is defined by the following formula:
\[\mathbf{X}_E = \mathbf{R} \mathbf{X}_F + \mathbf{T}\]For \(\mathbf{X}_F = 0\) we have \(\mathbf{X}_E = O_F = \mathbf{T}\) so the translation vector is directly the origin of the frame in the parent frame coordinates. The rotation matrix corresponding to the basis vectors in the convention 0 can be described with quaternion \(q = [w, x, y, z] = [0.5 \sqrt{2 + \sqrt{2}}, 0, 0, \sqrt{2}/(4 w)]\).
import numpy from py3dframe import Frame w = 0.5 * numpy.sqrt(2 + numpy.sqrt(2)) z = numpy.sqrt(2) / (4 * w) quaternion = numpy.array([w, 0, 0, z]) # [w, x, y, z] origin = numpy.array([-1, -2, -3]) t = origin parent = ... # Define the parent frame if needed, otherwise parent=None to use the canonical frame. frame = Frame.from_quaternion(translation=t, quaternion=quaternion, convention=0, parent=parent) print("Origin of the frame:", frame.origin) print("X-axis of the frame:", frame.x_axis) print("Y-axis of the frame:", frame.y_axis) print("Z-axis of the frame:", frame.z_axis) # Output: # Origin of the frame: [[-1.] [-2.] [-3.]] # X-axis of the frame: [[ 0.70710678] [ 0.70710678] [ 0. ]] # Y-axis of the frame: [[-0.70710678] [ 0.70710678] [ 0. ]] # Z-axis of the frame: [[0.] [0.] [1.]]
If you like to work with a given convention you can specify it with the
conventionparameter. The translation and the quaternion must be given in this convention to create the frame.You can also create the frame with convention 0 and then change the convention of the frame with the :attr`convention` attribute according to your preferences.