对向量时间序列应用四元数旋转

2024-06-01 11:18:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python numpy数组中有一个3D向量的时间序列,类似于以下内容:

array([[-0.062, -0.024,  1.   ],
       [-0.071, -0.03 ,  0.98 ],
       [-0.08 , -0.035,  0.991],
       [-0.083, -0.035,  0.98 ],
       [-0.083, -0.035,  0.977],
       [-0.082, -0.035,  0.993],
       [-0.08 , -0.034,  1.006],
       [-0.081, -0.032,  1.008],
       .......

我想将每个向量绕指定轴旋转指定角度theta。我一直在使用四元数来实现一个向量的这一点,正如在henneray的答案中发现的here

v1 = np.array ([1, -2, 0])
axis = np.array([-4, -2,  3])
theta = 1.5

rot_axis = np.insert(axis, 0, 0, axis=0)
axis_angle = (theta*0.5) * rot_axis/np.linalg.norm(rot_axis)
vec = quat.quaternion(*v1)
qlog = quat.quaternion(*axis_angle)
q = np.exp(qlog)
v_prime = q * vec * np.conjugate(q)
v_prime_vec = v_prime.imag

我的问题是,对v1中的每个向量应用相同旋转的最快方法是什么

如果v1包含向量的2D数组,则无法从v1创建四元数,因此我可以使用循环依次旋转每个数组元素;然而,在上面链接中henneray的回答中提到,四元数可以应用于“适当矢量化的numpy数组”。有人对如何实施这一点有什么建议吗

(一个附带问题:如果我的thetaaxis变量是与v1长度相等的数组,那么是否也可以使用相同的方法通过相应的旋转来旋转v1中的每个向量?)


Tags: numpynp数组array向量primev1axis
2条回答

首先需要将[x,y,z]笛卡尔向量转换为第一分量等于零[0,x,y,z]的4向量。然后您可以将其转换为四元数数组以进行矢量化计算

下面的函数获取笛卡尔向量数组,并围绕单个旋转轴旋转它们。您需要确保该轴的范数等于旋转角度θ

def rotate_vectors(vecs, axis):
    """
    Rotate a list of 3D [x,y,z] vectors about corresponding 3D axis
    [x,y,z] with norm equal to the rotation angle in radians

    Parameters
         
    vectors : numpy.ndarray with shape [n,3]
        list of [x,y,z] cartesian vector coordinates
    axis : numpy.ndarray with shape [3]
        [x,y,z] axis to rotate corresponding vectors about
    """
    # Make an 4 x n array of zeros
    vecs4 = np.zeros([vecs.shape[0],vecs.shape[1]+1])
    # Fill the imaginary i, j, k components with x, y, z values, leaving the real part w=0
    vecs4[:,1:] = vecs
    # Convert to quaternion array
    vecsq = quat.as_quat_array(vecs4)

    # Make a rotation quaternion
    qrot = quat.from_rotation_vector(axis)
    # Rotate vectors
    vecsq_rotated = qrot * vecsq * qrot.conjugate()
    # Cast quaternion array to float and return only imaginary components (ignore real part)
    return quat.as_float_array(vecsq_rotated)[:,1:]

作为一个额外的功能,此函数使用一组旋转轴按相应的轴旋转每个向量

def rotate_vectors_each(vecs, axes):
    """
    Rotate a list of 3D [x,y,z] vectors about corresponding 3D axes
    [x,y,z] with norm equal to the rotation angle in radians

    Parameters
         
    vectors : numpy.ndarray with shape [n,3]
        list of [x,y,z] cartesian vector coordinates
    axes : numpy.ndarray with shape [n,3]
        axes to rotate corresponding vectors about
        n = pulse shape time domain
        3 = [x,y,z]
    """
    # Make an 4 x n array of zeros
    vecs4 = np.zeros([vecs.shape[0],vecs.shape[1]+1])
    # Fill the imaginary i, j, k components with x, y, z values, leaving the real part w=0
    vecs4[:,1:] = vecs
    # Convert to quaternion array
    vecsq = quat.as_quat_array(vecs4)

    # Make an 4 x n array of zeros
    rots4 = np.zeros([rots.shape[0],rots.shape[1]+1])
    # Fill the imaginary i, j, k components with x, y, z values, leaving the real part w=0
    rots4[:,1:] = rots
    # Convert to quaternion array and take exponential
    qrots = np.exp(quat.as_quat_array(0.5 * rots4))

    # Rotate vectors
    vecsq_rotated = qrots * vecsq * qrots.conjugate()

    return quat.as_float_array(vecsq_rotated)[:,1:]

请注意,由于轴角度和四元数表示之间存在如此多的转换,因此与旋转矩阵代数相比,这不会给您带来多少性能改进。四元数只有在通过许多顺序旋转来旋转向量时才真正有用,因此可以堆叠四元数乘法

旋转计算本身的一种“快速”方法是将四元数转换为3x3方向余弦矩阵,将向量置于单个3xN连续矩阵中,然后调用BLAS库例程(例如dgemm)进行标准矩阵乘法。一个好的具有大N的BLAS库可以进行多线程计算

相关问题 更多 >