如何使用Python VTK高亮显示actor

 更新时间:2022年04月18日 13:41:29   作者:派大大大星  
这篇文章主要介绍了如何使用Python VTK高亮显示actor,通过Python-VTK在同一个窗口中,高亮显示选中的actor。本例子中的代码,当窗口中的圆球actor被选中时,会变成红色,并且会显示actor三遍面片边缘信息,下文相关内容需要的小伙伴可以参考一下

前言:

VTK,(visualizationtoolkit)是一个开放资源的免费软件系统,主要用于三维计算机图形学、图像处理和可视化。Vtk是在面向对象原理的基础上设计和实现的,它的内核是用C++构建的,包含有大约250,000行代码,2000多个类,还包含有几个转换界面,因此也可以自由的通过Java,Tcl/Tk和Python各种语言使用vtk。

主要函数介绍:

NewPickedActor.GetProperty(): 通过该函数,可以设置actor的性质,如颜色、表面样式等。

vtk.vtkSphereSource(): 创建球体的函数,文中通过一个for循环创建了10个球体。

vtk.vtkMinimalStandardRandomSequence(): VTK的随机数生成器,用于代码中的十个球体,随机生成球体的大小和位置。

MouseInteractorHighLightActor: 定义actor操作方法,这个是一个鼠标操作控件的控制方法。

leftButtonPressEvent(self, obj, event): 这是一个事件触发函数,当鼠标左键点击对应的actor时,会触发函数,对点的actor高亮显示。

主要代码如下:

#!/usr/bin/env python

# noinspection PyUnresolvedReferences
import vtk

colors = vtk.vtkNamedColors()
NUMBER_OF_SPHERES = 10


class MouseInteractorHighLightActor(vtk.vtkInteractorStyleTrackballCamera):

    def __init__(self, parent=None):
        self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)

        self.LastPickedActor = None
        self.LastPickedProperty = vtk.vtkProperty()

    def leftButtonPressEvent(self, obj, event):
        clickPos = self.GetInteractor().GetEventPosition()

        picker = vtk.vtkPropPicker()
        picker.Pick(clickPos[0], clickPos[1], 0, self.GetDefaultRenderer())

        # 创建一个新的actor
        self.NewPickedActor = picker.GetActor()

        # If something was selected
        if self.NewPickedActor:
            # If we picked something before, reset its property
            if self.LastPickedActor:
                self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty)

            # Save the property of the picked actor so that we can
            # restore it next time
            self.LastPickedProperty.DeepCopy(self.NewPickedActor.GetProperty())
            # 高亮选中球体,并显示边缘
            self.NewPickedActor.GetProperty().SetColor(colors.GetColor3d('Red'))
            self.NewPickedActor.GetProperty().SetDiffuse(1.0)
            self.NewPickedActor.GetProperty().SetSpecular(0.0)
            self.NewPickedActor.GetProperty().EdgeVisibilityOn()

            # 保存最后一个选中的actor
            self.LastPickedActor = self.NewPickedActor

        self.OnLeftButtonDown()
        return


def main():
    # 创建render和window
    renderer = vtk.vtkRenderer()
    renderer.SetBackground(colors.GetColor3d('SteelBlue'))

    renwin = vtk.vtkRenderWindow()
    renwin.AddRenderer(renderer)
    renwin.SetSize(640, 480)
    renwin.SetWindowName('HighlightPickedActor')

    # 建立interactor(交互操作)
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renwin)

    # 交互操作方法
    style = MouseInteractorHighLightActor()
    style.SetDefaultRenderer(renderer)
    interactor.SetInteractorStyle(style)

    randomSequence = vtk.vtkMinimalStandardRandomSequence()
    # randomSequence.SetSeed(1043618065)
    # randomSequence.SetSeed(5170)
    randomSequence.SetSeed(8775070)
    # 添加球体
    for i in range(NUMBER_OF_SPHERES):
        source = vtk.vtkSphereSource()

        # random position and radius
        x = randomSequence.GetRangeValue(-5.0, 5.0)
        randomSequence.Next()
        y = randomSequence.GetRangeValue(-5.0, 5.0)
        randomSequence.Next()
        z = randomSequence.GetRangeValue(-5.0, 5.0)
        randomSequence.Next()
        radius = randomSequence.GetRangeValue(0.5, 1.0)
        randomSequence.Next()

        source.SetRadius(radius)
        source.SetCenter(x, y, z)
        source.SetPhiResolution(11)
        source.SetThetaResolution(21)

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(source.GetOutputPort())
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)

        r = randomSequence.GetRangeValue(0.4, 1.0)
        randomSequence.Next()
        g = randomSequence.GetRangeValue(0.4, 1.0)
        randomSequence.Next()
        b = randomSequence.GetRangeValue(0.4, 1.0)
        randomSequence.Next()

        actor.GetProperty().SetDiffuseColor(r, g, b)
        actor.GetProperty().SetDiffuse(.8)
        actor.GetProperty().SetSpecular(.5)
        actor.GetProperty().SetSpecularColor(colors.GetColor3d('White'))
        actor.GetProperty().SetSpecularPower(30.0)

        renderer.AddActor(actor)

    # 运行
    interactor.Initialize()
    renwin.Render()
    interactor.Start()


if __name__ == '__main__':
    main()

显示结果如下: 未选择球体: 

image.png

已选取球体后: 

image.png

到此这篇关于如何使用Python VTK高亮显示actor的文章就介绍到这了,更多相关Python VTK高亮显示actor内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • tensorflow 初始化未初始化的变量实例

    tensorflow 初始化未初始化的变量实例

    今天小编就为大家分享一篇tensorflow 初始化未初始化的变量实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • python3.7 使用pymssql往sqlserver插入数据的方法

    python3.7 使用pymssql往sqlserver插入数据的方法

    这篇文章主要介绍了python3.7 使用pymssql往sqlserver插入数据的方法,代码很简单,感兴趣的朋友跟随小编一起看看吧
    2019-07-07
  • Python中Selenium模拟JQuery滑动解锁实例

    Python中Selenium模拟JQuery滑动解锁实例

    这篇文章主要介绍了Python中Selenium模拟JQuery滑动解锁实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Python元类的进阶应用深度探索

    Python元类的进阶应用深度探索

    这篇文章主要介绍了Python元类的进阶应用深度探索,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • Python实现快速将pdf文件剪切成多个图片

    Python实现快速将pdf文件剪切成多个图片

    这篇文章主要为大家详细介绍了如何使用Python实现快速将pdf文件剪切成多个图片,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • 用python打印菱形的实操方法和代码

    用python打印菱形的实操方法和代码

    在本篇文章里小编给大家分享了关于用python打印菱形的实操方法和代码,对此有需要的朋友们可以学习下。
    2019-06-06
  • python利用beautifulSoup实现爬虫

    python利用beautifulSoup实现爬虫

    这篇文章主要介绍了python利用beautifulSoup实现爬虫,需要的朋友可以参考下
    2014-09-09
  • Python 获取命令行参数内容及参数个数的实例

    Python 获取命令行参数内容及参数个数的实例

    今天小编就为大家分享一篇Python 获取命令行参数内容及参数个数的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • python asyncio 协程库的使用

    python asyncio 协程库的使用

    这篇文章主要介绍了python asyncio 协程库的使用,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2021-01-01
  • Django使用原生SQL查询数据库详解

    Django使用原生SQL查询数据库详解

    本文介绍了Django ORM的优缺点,然后介绍了使用原生SQL进行查询的优点,包括更灵活、更高效等。接着介绍了如何在Django中使用原生SQL进行查询,包括利用Django的connection对象进行查询以及使用Django的CursorWrapper类进行封装。最后提醒了使用原生SQL查询的注意事项。
    2023-04-04

最新评论