scipy.spatial.transform.Rotation.

from_quat#

classmethod Rotation.from_quat(cls, quat, *, scalar_first=False)#

從四元數初始化。

可以使用單位範數四元數 [1] 來表示 3 維空間中的旋轉。

四元數的 4 個分量分為純量部分 w 和向量部分 (x, y, z),並且可以從旋轉的角度 theta 和軸 n 表示如下

w = cos(theta / 2)
x = sin(theta / 2) * n_x
y = sin(theta / 2) * n_y
z = sin(theta / 2) * n_z

有 2 種約定來排序四元數中的分量

  • 純量優先順序 – (w, x, y, z)

  • 純量最後順序 – (x, y, z, w)

此選擇由 scalar_first 參數控制。預設為 False,並且假定為純量最後順序。

進階使用者可能會對四元數表示法 [2] 的 3D 空間「雙重覆蓋」感興趣。 截至 1.11.0 版,保證保留與四元數 q 對應的 Rotation r 的以下子集(且僅此子集)運算的雙重覆蓋屬性: r = Rotation.from_quat(q)r.as_quat(canonical=False)r.inv() 以及使用 * 運算子(例如 r*r)的組合。

參數:
quatarray_like,形狀為 (N, 4) 或 (4,)

每一列都是一個(可能非單位範數)四元數,表示主動旋轉。每個四元數都將被正規化為單位範數。

scalar_firstbool,選用

純量分量在前還是最後。預設為 False,即假定為純量最後順序。

返回:
rotationRotation 實例

包含由輸入四元數表示的旋轉的物件。

參考文獻

[2]

Hanson, Andrew J. “Visualizing quaternions.” Morgan Kaufmann Publishers Inc., San Francisco, CA. 2006.

範例

>>> from scipy.spatial.transform import Rotation as R

旋轉可以從四元數初始化,其純量最後(預設)或純量優先分量順序如下所示

>>> r = R.from_quat([0, 0, 0, 1])
>>> r.as_matrix()
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
>>> r = R.from_quat([1, 0, 0, 0], scalar_first=True)
>>> r.as_matrix()
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

可以透過傳遞二維陣列,在單一物件中初始化多個旋轉

>>> r = R.from_quat([
... [1, 0, 0, 0],
... [0, 0, 0, 1]
... ])
>>> r.as_quat()
array([[1., 0., 0., 0.],
       [0., 0., 0., 1.]])
>>> r.as_quat().shape
(2, 4)

也可以堆疊單一旋轉

>>> r = R.from_quat([[0, 0, 0, 1]])
>>> r.as_quat()
array([[0., 0., 0., 1.]])
>>> r.as_quat().shape
(1, 4)

四元數在初始化之前會先正規化。

>>> r = R.from_quat([0, 0, 1, 1])
>>> r.as_quat()
array([0.        , 0.        , 0.70710678, 0.70710678])