繁体   English   中英

如何在 Python 中使用 Mayavi 在 tvtk 渲染场景上旋转演员?

[英]How to rotate actor on tvtk rendered scene with Mayavi in Python?

我玩了一点Mayavi ,尤其是tvtk但我很难找到将字形以与默认方向不同的方向放置在场景中的示例。

基于这个例子,我用两个圆柱体准备了以下场景,一个红色一个蓝色,

import mayavi.mlab as mlab

from tvtk.api import tvtk
from tvtk.common import configure_input_data

v = mlab.figure()

# Create two cyllinders
cyl1 = tvtk.CylinderSource(center=(0, 0, 0), radius=1.0, height=0.5, capping=True, resolution=24)
cyl2 = tvtk.CylinderSource(center=(3, 0, 0), radius=1.0, height=0.5, capping=True, resolution=24)


# The mapper converts them into position in 3D
cylinder_mapper1 = tvtk.PolyDataMapper()
configure_input_data(cylinder_mapper1, cyl1.output)
cyl1.update()

cylinder_mapper2 = tvtk.PolyDataMapper()
configure_input_data(cylinder_mapper2, cyl2.output)
cyl2.update()

# Assign them differrent colors
p1 = tvtk.Property(opacity=1.0, color=(1, 0, 0))
p2 = tvtk.Property(opacity=1.0, color=(0, 0, 1))

# The actor is the actually object in the scene.
cyl1_actor = tvtk.Actor(mapper=cylinder_mapper1, property=p1)
v.scene.add_actor(cyl1_actor)

cyl2_actor = tvtk.Actor(mapper=cylinder_mapper2, property=p2)
v.scene.add_actor(cyl2_actor)

# Choose a view angle, and display the figure
mlab.view(90, 45, 7.5, [1.5, 0, 0])
mlab.savefig(filename='cylinders.png')

我在这里试图实现的是改变其中之一的方向。 无论是通过提供旋转矩阵,还是通过调用方法,只要我能以某种方式独立旋转字形(而不仅仅是旋转整个场景),这都无关紧要。

上面的例子呈现

代码示例的渲染输出

关于之前已经提出的其他问题,这个问题似乎很有用,但它不是旋转演员,而是围绕 Y 轴旋转整个场景。 scipy-cookbook 中还有一个示例,但同样 - 仅使用默认方向。

tvtk 文档中的例子并不丰富,几乎很难相信没有涉及交替方向的例子。 该文档仅说明类和方法对应于遵循某些约定的 C++ 对应项。

  1. tvtk 类名本质上类似于 VTK 类,只是前面没有烦人的“vtk”。 唯一的困难在于以数字开头的类。 例如,“vtk3DSImporter”变为“3DSImporter”。 这在 Python 中是非法的,因此使用的类名是“ThreeDSImporter”。 因此,如果第一个字符是数字,则将其替换为等效的非数字字符串。 很少有这样的类,所以这不是什么大问题。
  2. tvtk 方法名称是经过深思熟虑的样式名称,而不是 CamelCase。 也就是说,如果一个 VTK 方法被称为 AddItem,则等效的 tvtk 名称是 add_item。 这样做是为了与 enthought 包中使用的名称保持一致。
  3. 许多 VTK 方法被方便的属性取代。 在上面的例子中,我们使用了 m.input = cs.output 和 p.representation = 'w' 而不是 m.SetInput(cs.GetOutput()) 和 p.SetRepresentationToWireframe() 等。其中一些属性真的是特质。
  4. 与 VTK 对象不同,可以在对象初始化时通过在类实例化时将对象的属性(特征)作为关键字参数传递来设置 tvtk 对象的属性。 例如 cs = tvtk.ConeSource(radius=0.1, height=0.5)。

也许在 C++ 中广泛使用 VTK 的人会发现它很有用,但对我来说它不是很有帮助。 尽管如此,我还是尝试查找 VTK C++ 文档,它确实给了我一些线索。 vtkActor 类确实从vtkProp3DvtkProp继承了一些类型。 vtkProp3D似乎承认与场景中的对象方向相关的方法和属性,但我不清楚如何设置它们。 tvtk库只是 C++ 库之上的一个包装器,这使得仅inspect哪些属性被接受是不可能的。

我想为了互联网,也许我们应该准备一个适当的例子,用Mayavitvtk渲染一个场景,同时承认演员的位置和方向。 大多数示例忽略方向并使用球体,因此方向似乎不相关。

让我们总结一下。

  1. 如果有人提供了一个使用 Mayavi 和tvtk渲染场景的正确示例的链接,该场景涉及具有不同位置和方向的多个字形,我将接受这样的答案。
  2. 为 (1) 制作自定义示例也将被接受。
  3. 也许有人也可以评论一下字形的位置和在场景中映射这个字形的演员之间的区别。 正如您在示例中看到的,当我创建CylinderSource我明确指定了中心所在的位置。 这令人困惑,因为它应该是决定场景中字形位置的演员中心。

非常感谢!

好吧,事实证明Actor接受以度为单位的orientation参数(而不是以弧度为单位!)。 此外,角色的位置必须在创建角色时指定,而不是在创建字形时指定!

cyl1_actor = tvtk.Actor(position=(0, 0, 0), mapper=cylinder_mapper1, property=p1, orientation=(0, 0, 90))

渲染

渲染输出

我是通过反复试验找到的...... Mayavi包中的tvtk代码是生成的,所以尝试通过研究GitHub存储库来查找哪些属性是没有意义的。 此外,您可能会将tvtkActor与没有方向属性的Mayavi 的 Actor混淆。 哦,实际上有一个属性名称“属性”,所以尝试搜索它并获得数千个结果,因为property一词在任何地方都被广泛使用......

编辑:

我注意到我一直在字形中心指定演员的位置,这是错误的! 这里更新了整个源:

import mayavi.mlab as mlab

from tvtk.api import tvtk
from tvtk.common import configure_input_data

v = mlab.figure()

# Create two cyllinders
cyl1 = tvtk.CylinderSource(center=(0, 0, 0), radius=1.0, height=0.5, capping=True, resolution=24)
cyl2 = tvtk.CylinderSource(center=(0, 0, 0), radius=1.0, height=0.5, capping=True, resolution=24)


# The mapper converts them into position in 3D
cylinder_mapper1 = tvtk.PolyDataMapper()
configure_input_data(cylinder_mapper1, cyl1.output)
cyl1.update()

cylinder_mapper2 = tvtk.PolyDataMapper()
configure_input_data(cylinder_mapper2, cyl2.output)
cyl2.update()

# Assign them differrent colors
p1 = tvtk.Property(opacity=1.0, color=(1, 0, 0))
p2 = tvtk.Property(opacity=1.0, color=(0, 0, 1))

# The actor is the actually object in the scene.
cyl1_actor = tvtk.Actor(position=(0, 0, 0), mapper=cylinder_mapper1, property=p1)
v.scene.add_actor(cyl1_actor)

cyl2_actor = tvtk.Actor(position=(3, 0, 0), mapper=cylinder_mapper2, property=p2, orientation=(0, 0, 90))
v.scene.add_actor(cyl2_actor)

# Choose a view angle, and display the figure
mlab.view(90, 45, 7.5, [1.5, 0, 0])
mlab.savefig(filename='cylinders.png')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM