【问题标题】:How to use mayavi.tools.pipeline.transform_data to complete a rotation around the x-axis如何使用 mayavi.tools.pipeline.transform_data 完成绕 x 轴的旋转
【发布时间】:2014-01-20 10:53:37
【问题描述】:

我正在尝试围绕 X 轴旋转我的数据。我想我应该使用 mayavi.tools.pipeline.transform_data 函数,但我不知道如何使用它...

我需要对数据应用旋转矩阵,但我不知道如何使用该函数...有什么提示吗?

【问题讨论】:

  • 您要轮换哪种类型的数据?您需要使用 mayavi 执行旋转还是可以使用 VTK 方法和过滤器? (为了在渲染之前执行旋转)
  • 我想使用 vtk 过滤器在 x,z 基础上执行 delaunay2D 而不是 x,y (因此 x 轴旋转)。先前后后..
  • 尝试将vtkTransform 的输出绑定到您的 delaunay 过滤器的输入。使用vtkTransform::RotateX (double angle) 方法在变换过滤器中进行旋转。在处理 Delaunay 过滤器之前,可能会对您的数据执行绕 X 轴的旋转
  • 在我的 x、z 数据和 y 上使用 delaunay 数据作为高程后,我有点作弊并使用 mlab.view 旋转场景。我可能会看看 vtkTransform 但我对复杂性有点失望......

标签: python vtk enthought mayavi


【解决方案1】:

这是一个使用 Mayavi transform_data 旋转内置圆柱体对象的示例:

import numpy as np
from mayavi import mlab
from mayavi.sources.builtin_surface import BuiltinSurface
from mayavi.modules.surface import Surface
from mayavi.filters.transform_data import TransformData

def rotMat3D(axis, angle, tol=1e-12):
    """Return the rotation matrix for 3D rotation by angle `angle` degrees about an
    arbitrary axis `axis`.
    """
    t = np.radians(angle)
    x, y, z = axis
    R = (np.cos(t))*np.eye(3) +\
    (1-np.cos(t))*np.matrix(((x**2,x*y,x*z),(x*y,y**2,y*z),(z*x,z*y,z**2))) + \
    np.sin(t)*np.matrix(((0,-z,y),(z,0,-x),(-y,x,0)))
    R[np.abs(R)<tol]=0.0
    return R

# Main code

fig = mlab.figure()

engine = mlab.get_engine()

# Add a cylinder builtin source
cylinder_src = BuiltinSurface()
engine.add_source(cylinder_src)
cylinder_src.source = 'cylinder'
cylinder_src.data_source.center = np.array([ 0.,  0.,  0.])
cylinder_src.data_source.radius = 1.0
cylinder_src.data_source.capping = False
cylinder_src.data_source.resolution = 25

# Add transformation filter to rotate cylinder about an axis
transform_data_filter = TransformData()
engine.add_filter(transform_data_filter, cylinder_src)
Rt = np.eye(4)
Rt[0:3,0:3] = rotMat3D((1,0,0), 0) # in homogeneous coordinates
Rtl = list(Rt.flatten()) # transform the rotation matrix into a list

transform_data_filter.transform.matrix.__setstate__({'elements': Rtl})
transform_data_filter.widget.set_transform(transform_data_filter.transform)
transform_data_filter.filter.update()
transform_data_filter.widget.enabled = False   # disable the rotation control further.

# Add surface module to the cylinder source
cyl_surface = Surface()
engine.add_filter(cyl_surface, transform_data_filter)
# add color property
cyl_surface.actor.property.color = (1.0, 0.0, 0.0)

mlab.show()

在上面的例子中,Rt[0:3,0:3] = rotMat3D((1,0,0), 0) 行创建了一个旋转矩阵(在这个例子中没有平移分量,尽管它也可能在那里),用于将对象绕 x 轴旋转 0 度(以显示无旋转情况)。它可以是任何其他值,如下所示。运行上面的代码会产生以下结果:

您现在可以通过将上面的行修改为Rt[0:3,0:3] = rotMat3D((1,0,0), 90) 来将对象旋转 90 度,这将产生下图:

请注意,您很可能还会收到如下警告:

Warning: In C:\pisi\tmp\VTK-5.6.0-1\work\VTK\Common\vtkTransform.cxx, line 202 vtkTransform (000000000D3AB3A0): InternalUpdate: doing hack to support legacy code. This is deprecated in VTK 4.2. May be removed in a future version.

这是因为 Mayavi 尚未更新为使用新的 VTK 管道。

【讨论】:

    猜你喜欢
    • 2021-07-12
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2012-07-23
    • 2017-09-03
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多