gpt4 book ai didi

python - 如何使用mayavi.tools.pipeline.transform_data完成一个绕x轴的旋转

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:31 27 4
gpt4 key购买 nike

我正在尝试围绕 X 轴旋转我的数据。我发现我应该使用 mayavi.tools.pipeline.transform_data 函数,但我想不出使用它的方法...

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

最佳答案

这是一个使用 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 轴旋转零度(以显示不旋转的情况)。它可以是任何其他值,如下所示。运行上面的代码会产生以下结果:

enter image description here

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

enter image description here

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

警告:在 C:\pisi\tmp\VTK-5.6.0-1\work\VTK\Common\vtkTransform.cxx 中,第 202 行
vtkTransform (000000000D3AB3A0):InternalUpdate:进行 hack 以支持遗留代码。这在 VTK 4.2 中已弃用。可能会在未来的版本中删除。

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

关于python - 如何使用mayavi.tools.pipeline.transform_data完成一个绕x轴的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21232047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com