从Mayavi / Vtk的旋转矩阵到(俯仰,滚转,偏航)的正确转换

时间:2014-06-30 03:29:30

标签: python rotation vtk mayavi rotational-matrices

我的问题

我想用3x3旋转矩阵旋转mayavi.mlab.imshow个对象。我可以找到旋转此对象的唯一方法是将对象的actor.orientation设置为[pitch, roll, yaw](此顺序继承自vtk)。我唯一的问题是我找不到将旋转矩阵转换为mayavi请求的参数的方法。

如何使用旋转矩阵旋转mayavi中的对象,或者我应该使用哪种变换来获得Mayavi / Vtk使用的正确(俯仰,滚转和偏航)?

近乎解决方案

我找到了一些代码over here来将旋转矩阵转换为不同类型的欧拉角(根据旋转顺序)。如果我错误地认为欧拉角相当于俯仰,滚转,偏航,那么请纠正我。我尝试了所有不同的转换,但未能找到正确的转换。

尝试每个组合

我通过旋转矩阵旋转x,y和z向量并测试mayavi.mlab.imshow对象上的参数来测试所有不同的变换。我在Rtranspose(R)上使用了所有可用的转换,并在所有9个可用订单中使用了Euler参数,但找不到正确的组合:

import pylab as pl
import cameraTools #my own lib
from mayavi import mlab

im = pl.imread('dice.png', format='png')[:,:,0]*255 #1 color channel
rot = pl.r_[30, 80, 230]

R_orig = cameraTools.composeRotation(*(rot*pl.pi/180))
RList = [R_orig, R_orig.T]
for ii, inOrder in enumerate(['sxyz','sxzx','syxz','szxz','rzyx','rxzx','rzxy','rzxz','sxyx','syzx','syxy','szyx','rxyx','rxzy','ryxy','rxyz','sxzy','syzy','szxy','szyz','ryzx','ryzy','ryxz','rzyz']):
    tries = 0
    for outOrder in [[0,1,2], [0,2,1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]:
        for R in  RList:
            for vector, color in zip([[800, 0, 0], [0, 800, 0], [0, 0, 800]],
                                     [(1., 0., 0.), (0., 1., 0.), (0., 0., 1.)]):
                c = pl.c_[[0, tries*1000, ii*1000]]
                if ii == 0 and tries == 0: vector = pl.r_[vector]*5 #point out the first vector
                lin = R_orig.dot(pl.c_[[0,0,0], vector]) + c
                mlab.plot3d(*lin,
                            color = color,
                            tube_radius=5)

            lin3D    = mlab.imshow(im, colormap="gray")
            rxyz = pl.array(cameraTools.euler_from_matrix(R, inOrder))*180/pi
            i,j,k = outOrder
            lin3D.actor.orientation = [rxyz[i], rxyz[j], rxyz[k]]
            lin3D.actor.position    = c.flatten()
            tries +=1


mlab.draw()
mlab.show() 

来自Mayavi的输出,左上角的项目是起点。

Mayavi output

1 个答案:

答案 0 :(得分:1)

对不起,好像我没有集中精力。答案在第3行第5列,输入顺序为syxz,输出顺序为'[1,0,2]'。我现在使用以下函数将旋转矩阵转换为所需的欧拉角:

def rotationToVtk(R):
    '''
    Concert a rotation matrix into the Mayavi/Vtk rotation paramaters (pitch, roll, yaw)
    '''
    def euler_from_matrix(matrix):
        """Return Euler angles (syxz) from rotation matrix for specified axis sequence.
        :Author:
          `Christoph Gohlke <http://www.lfd.uci.edu/~gohlke/>`_

        full library with coplete set of euler triplets (combinations of  s/r x-y-z) at
            http://www.lfd.uci.edu/~gohlke/code/transformations.py.html

        Note that many Euler angle triplets can describe one matrix.
        """
        # epsilon for testing whether a number is close to zero
        _EPS = np.finfo(float).eps * 5.0

        # axis sequences for Euler angles
        _NEXT_AXIS = [1, 2, 0, 1]
        firstaxis, parity, repetition, frame = (1, 1, 0, 0) # ''

        i = firstaxis
        j = _NEXT_AXIS[i+parity]
        k = _NEXT_AXIS[i-parity+1]

        M = np.array(matrix, dtype='float', copy=False)[:3, :3]
        if repetition:
            sy = np.sqrt(M[i, j]*M[i, j] + M[i, k]*M[i, k])
            if sy > _EPS:
                ax = np.arctan2( M[i, j],  M[i, k])
                ay = np.arctan2( sy,       M[i, i])
                az = np.arctan2( M[j, i], -M[k, i])
            else:
                ax = np.arctan2(-M[j, k],  M[j, j])
                ay = np.arctan2( sy,       M[i, i])
                az = 0.0
        else:
            cy = np.sqrt(M[i, i]*M[i, i] + M[j, i]*M[j, i])
            if cy > _EPS:
                ax = np.arctan2( M[k, j],  M[k, k])
                ay = np.arctan2(-M[k, i],  cy)
                az = np.arctan2( M[j, i],  M[i, i])
            else:
                ax = np.arctan2(-M[j, k],  M[j, j])
                ay = np.arctan2(-M[k, i],  cy)
                az = 0.0

        if parity:
            ax, ay, az = -ax, -ay, -az
        if frame:
            ax, az = az, ax
        return ax, ay, az
    r_yxz = pl.array(euler_from_matrix(R))*180/pi
    r_xyz = r_yxz[[1, 0, 2]]
    return r_xyz