我正在尝试围绕X轴旋转我的数据。我发现我应该使用mayavi.tools.pipeline.transform_data函数,但我无法找到使用它的方法......
我需要对数据应用旋转矩阵,但我无法弄清楚如何使用函数...任何提示?
答案 0 :(得分:5)
以下是使用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轴旋转零度(到显示无旋转情况)。它可以是任何其他值,如下所示。运行上面的代码会产生以下结果:
现在可以通过将上面一行修改为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管道。