当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。