當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python SciPy Rotation.apply用法及代碼示例


本文簡要介紹 python 語言中 scipy.spatial.transform.Rotation.apply 的用法。

用法:

Rotation.apply(self, vectors, inverse=False)#

將此旋轉應用於一組向量。

如果原始幀通過這個旋轉旋轉到最終幀,那麽它對向量的應用可以通過兩種方式看到:

  • As a projection of vector components expressed in the final frame to the original frame.

  • As the physical rotation of a vector being glued to the original frame as it rotates. In this case the vector components are expressed in the original frame before and after the rotation.

在旋轉矩陣方麵,此應用程序與 self.as_matrix().dot(vectors) 相同。

參數

vectors 數組,形狀 (3,) 或 (N, 3)

每個向量[i]代表 3D 空間中的一個向量。單個向量可以用形狀 (3, ) 或 (1, 3) 指定。給定的旋轉數和向量數必須遵循標準 numpy 廣播規則:其中之一等於 1,或者兩者都彼此相等。

inverse 布爾值,可選

如果為 True,則將旋轉的倒數應用於輸入向量。默認為假。

返回

rotated_vectors ndarray,形狀 (3,) 或 (N, 3)

對輸入向量應用旋轉的結果。形狀取決於以下情況:

  • If object contains a single rotation (as opposed to a stack with a single rotation) and a single vector is specified with shape (3,), then rotated_vectors has shape (3,).

  • In all other cases, rotated_vectors has shape (N, 3), where N is either the number of rotations or vectors.

例子

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

應用於單個向量的單次旋轉:

>>> vector = np.array([1, 0, 0])
>>> r = R.from_rotvec([0, 0, np.pi/2])
>>> r.as_matrix()
array([[ 2.22044605e-16, -1.00000000e+00,  0.00000000e+00],
       [ 1.00000000e+00,  2.22044605e-16,  0.00000000e+00],
       [ 0.00000000e+00,  0.00000000e+00,  1.00000000e+00]])
>>> r.apply(vector)
array([2.22044605e-16, 1.00000000e+00, 0.00000000e+00])
>>> r.apply(vector).shape
(3,)

應用於多個向量的單次旋轉:

>>> vectors = np.array([
... [1, 0, 0],
... [1, 2, 3]])
>>> r = R.from_rotvec([0, 0, np.pi/4])
>>> r.as_matrix()
array([[ 0.70710678, -0.70710678,  0.        ],
       [ 0.70710678,  0.70710678,  0.        ],
       [ 0.        ,  0.        ,  1.        ]])
>>> r.apply(vectors)
array([[ 0.70710678,  0.70710678,  0.        ],
       [-0.70710678,  2.12132034,  3.        ]])
>>> r.apply(vectors).shape
(2, 3)

單個向量上的多次旋轉:

>>> r = R.from_rotvec([[0, 0, np.pi/4], [np.pi/2, 0, 0]])
>>> vector = np.array([1,2,3])
>>> r.as_matrix()
array([[[ 7.07106781e-01, -7.07106781e-01,  0.00000000e+00],
        [ 7.07106781e-01,  7.07106781e-01,  0.00000000e+00],
        [ 0.00000000e+00,  0.00000000e+00,  1.00000000e+00]],
       [[ 1.00000000e+00,  0.00000000e+00,  0.00000000e+00],
        [ 0.00000000e+00,  2.22044605e-16, -1.00000000e+00],
        [ 0.00000000e+00,  1.00000000e+00,  2.22044605e-16]]])
>>> r.apply(vector)
array([[-0.70710678,  2.12132034,  3.        ],
       [ 1.        , -3.        ,  2.        ]])
>>> r.apply(vector).shape
(2, 3)

多個向量上的多次旋轉。每次旋轉都應用於相應的向量:

>>> r = R.from_euler('zxy', [
... [0, 0, 90],
... [45, 30, 60]], degrees=True)
>>> vectors = [
... [1, 2, 3],
... [1, 0, -1]]
>>> r.apply(vectors)
array([[ 3.        ,  2.        , -1.        ],
       [-0.09026039,  1.11237244, -0.86860844]])
>>> r.apply(vectors).shape
(2, 3)

也可以應用反向旋轉:

>>> r = R.from_euler('zxy', [
... [0, 0, 90],
... [45, 30, 60]], degrees=True)
>>> vectors = [
... [1, 2, 3],
... [1, 0, -1]]
>>> r.apply(vectors, inverse=True)
array([[-3.        ,  2.        ,  1.        ],
       [ 1.09533535, -0.8365163 ,  0.3169873 ]])

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.spatial.transform.Rotation.apply。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。