旋转#
- class scipy.spatial.transform.Rotation#
三维旋转。
此类提供了一个接口,用于从以下内容初始化并表示旋转:
四元数
旋转矩阵
旋转向量
修正罗德里格斯参数
欧拉角
以下旋转操作是支持的:
向量上的应用
旋转组合
旋转反转
旋转索引
在旋转中进行索引是支持的,因为单个
Rotation实例可以存储多个旋转变换。要创建
Rotation对象,请使用from_...方法(见下文示例)。Rotation(...)不应该直接实例化。- 属性:
single此实例是否表示单次旋转。
方法
此对象中包含的旋转次数。
from_quat(cls, quat, *[, scalar_first])从四元数初始化。
from_matrix(cls, matrix)从旋转矩阵初始化。
from_rotvec(cls, rotvec[, degrees])从旋转向量初始化。
from_mrp(cls, mrp)从修正罗德里格斯参数 (MRPs) 初始化。
from_euler(cls, seq, angles[, degrees])从欧拉角初始化。
from_davenport(cls, axes, order, angles[, ...])从 Davenport 角度初始化。
as_quat(self[, canonical, scalar_first])表示为四元数。
as_matrix(self)表示为旋转矩阵。
as_rotvec(self[, degrees])表示为旋转向量。
as_mrp(self)表示为修正罗德里格斯参数(MRPs)。
as_euler(self, seq[, degrees])表示为欧拉角。
as_davenport(self, axes, order[, degrees])表示为 Davenport 角。
concatenate(cls, rotations)将一系列
Rotation对象连接成一个单一对象。apply(self, vectors[, inverse])将此旋转应用于一组向量。
将此旋转与另一个组合。
将此旋转自身组合 n 次。
inv(self)反转此旋转。
magnitude(self)获取旋转的大小。
approx_equal(self, Rotation other[, atol, ...])确定另一个旋转是否与此旋转大致相等。
mean(self[, weights])获取旋转的平均值。
reduce(self[, left, right, return_indices])使用提供的旋转组减少此旋转。
create_group(cls, group[, axis])创建一个3D旋转组。
从对象中提取给定索引处的旋转(s)。
identity(cls[, num])获取身份旋转(s)。
random(cls[, num, random_state])生成均匀分布的旋转。
align_vectors(cls, a, b[, weights, ...])估计一个旋转,以最佳对齐两组向量。
参见
注释
Added in version 1.2.0.
示例
>>> from scipy.spatial.transform import Rotation as R >>> import numpy as np
一个
Rotation实例可以用上述任何一种格式进行初始化,并转换为其他任何一种格式。底层对象与用于初始化的表示形式无关。考虑绕 z 轴逆时针旋转 90 度。这对应于以下四元数(标量在后格式):
>>> r = R.from_quat([0, 0, np.sin(np.pi/4), np.cos(np.pi/4)])
旋转可以用其他任何格式表示:
>>> 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.as_rotvec() array([0. , 0. , 1.57079633]) >>> r.as_euler('zyx', degrees=True) array([90., 0., 0.])
相同的旋转可以使用旋转矩阵来初始化:
>>> r = R.from_matrix([[0, -1, 0], ... [1, 0, 0], ... [0, 0, 1]])
其他格式的表示:
>>> r.as_quat() array([0. , 0. , 0.70710678, 0.70710678]) >>> r.as_rotvec() array([0. , 0. , 1.57079633]) >>> r.as_euler('zyx', degrees=True) array([90., 0., 0.])
与此旋转对应的旋转向量由以下公式给出:
>>> r = R.from_rotvec(np.pi/2 * np.array([0, 0, 1]))
其他格式的表示:
>>> r.as_quat() array([0. , 0. , 0.70710678, 0.70710678]) >>> 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.as_euler('zyx', degrees=True) array([90., 0., 0.])
from_euler方法在支持的输入格式范围上非常灵活。这里我们初始化一个关于单个轴的单次旋转:>>> r = R.from_euler('z', 90, degrees=True)
同样,对象是独立于表示的,可以转换为任何其他格式:
>>> r.as_quat() array([0. , 0. , 0.70710678, 0.70710678]) >>> 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.as_rotvec() array([0. , 0. , 1.57079633])
也可以使用任何一个
from_...函数在一个实例中初始化多个旋转。这里我们使用from_euler方法初始化了一个包含3个旋转的堆栈:>>> r = R.from_euler('zyx', [ ... [90, 0, 0], ... [0, 45, 0], ... [45, 60, 30]], degrees=True)
其他表示法现在也返回一个包含3次旋转的堆栈。例如:
>>> r.as_quat() array([[0. , 0. , 0.70710678, 0.70710678], [0. , 0.38268343, 0. , 0.92387953], [0.39190384, 0.36042341, 0.43967974, 0.72331741]])
将上述旋转应用于一个向量:
>>> v = [1, 2, 3] >>> r.apply(v) array([[-2. , 1. , 3. ], [ 2.82842712, 2. , 1.41421356], [ 2.24452282, 0.78093109, 2.89002836]])
Rotation实例可以像单个一维数组或列表一样进行索引和切片:>>> r.as_quat() array([[0. , 0. , 0.70710678, 0.70710678], [0. , 0.38268343, 0. , 0.92387953], [0.39190384, 0.36042341, 0.43967974, 0.72331741]]) >>> p = r[0] >>> p.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]]) >>> q = r[1:3] >>> q.as_quat() array([[0. , 0.38268343, 0. , 0.92387953], [0.39190384, 0.36042341, 0.43967974, 0.72331741]])
实际上它可以转换为 numpy.array:
>>> r_array = np.asarray(r) >>> r_array.shape (3,) >>> r_array[0].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]])
可以使用
*运算符组合多个旋转:>>> r1 = R.from_euler('z', 90, degrees=True) >>> r2 = R.from_rotvec([np.pi/4, 0, 0]) >>> v = [1, 2, 3] >>> r2.apply(r1.apply(v)) array([-2. , -1.41421356, 2.82842712]) >>> r3 = r2 * r1 # Note the order >>> r3.apply(v) array([-2. , -1.41421356, 2.82842712])
旋转可以使用
**运算符与自身组合:>>> p = R.from_rotvec([1, 0, 0]) >>> q = p ** 2 >>> q.as_rotvec() array([2., 0., 0.])
最后,也可以反转旋转:
>>> r1 = R.from_euler('z', [90, 45], degrees=True) >>> r2 = r1.inv() >>> r2.as_euler('zyx', degrees=True) array([[-90., 0., 0.], [-45., 0., 0.]])
以下函数可用于通过展示它们如何变换标准 x, y, z 坐标轴来使用 Matplotlib 绘制旋转:
>>> import matplotlib.pyplot as plt
>>> def plot_rotated_axes(ax, r, name=None, offset=(0, 0, 0), scale=1): ... colors = ("#FF6666", "#005533", "#1199EE") # Colorblind-safe RGB ... loc = np.array([offset, offset]) ... for i, (axis, c) in enumerate(zip((ax.xaxis, ax.yaxis, ax.zaxis), ... colors)): ... axlabel = axis.axis_name ... axis.set_label_text(axlabel) ... axis.label.set_color(c) ... axis.line.set_color(c) ... axis.set_tick_params(colors=c) ... line = np.zeros((2, 3)) ... line[1, i] = scale ... line_rot = r.apply(line) ... line_plot = line_rot + loc ... ax.plot(line_plot[:, 0], line_plot[:, 1], line_plot[:, 2], c) ... text_loc = line[1]*1.2 ... text_loc_rot = r.apply(text_loc) ... text_plot = text_loc_rot + loc[0] ... ax.text(*text_plot, axlabel.upper(), color=c, ... va="center", ha="center") ... ax.text(*offset, name, color="k", va="center", ha="center", ... bbox={"fc": "w", "alpha": 0.8, "boxstyle": "circle"})
创建三个旋转 - 恒等旋转和两个使用内在和外在约定的欧拉旋转:
>>> r0 = R.identity() >>> r1 = R.from_euler("ZYX", [90, -30, 0], degrees=True) # intrinsic >>> r2 = R.from_euler("zyx", [90, -30, 0], degrees=True) # extrinsic
将所有三个旋转添加到单个图中:
>>> ax = plt.figure().add_subplot(projection="3d", proj_type="ortho") >>> plot_rotated_axes(ax, r0, name="r0", offset=(0, 0, 0)) >>> plot_rotated_axes(ax, r1, name="r1", offset=(3, 0, 0)) >>> plot_rotated_axes(ax, r2, name="r2", offset=(6, 0, 0)) >>> _ = ax.annotate( ... "r0: Identity Rotation\n" ... "r1: Intrinsic Euler Rotation (ZYX)\n" ... "r2: Extrinsic Euler Rotation (zyx)", ... xy=(0.6, 0.7), xycoords="axes fraction", ha="left" ... ) >>> ax.set(xlim=(-1.25, 7.25), ylim=(-1.25, 1.25), zlim=(-1.25, 1.25)) >>> ax.set(xticks=range(-1, 8), yticks=[-1, 0, 1], zticks=[-1, 0, 1]) >>> ax.set_aspect("equal", adjustable="box") >>> ax.figure.set_size_inches(6, 5) >>> plt.tight_layout()
显示图表:
>>> plt.show()
这些示例提供了对
Rotation类的概览,并突出了主要功能。如需更全面的示例,了解支持的输入和输出格式范围,请参阅各个方法的示例。