美文网首页
python VTK库如何自定义交互风格

python VTK库如何自定义交互风格

作者: 小黄不头秃 | 来源:发表于2024-02-25 17:34 被阅读0次

    在使用vtk库的时候往往我们需要监听鼠标的移动、点击事件、滚轮滚动事件、案件按下事件等等。那这一部分就需要我们去写一个新的类,继承交互风格类。

    VTK(Visualization Toolkit)中,有多种交互风格可供选择,每种风格都具有不同的特点和用途。以下是一些常见的VTK交互风格及其区别:

    1. 轨迹球相机交互风格(vtkInteractorStyleTrackballCamera

      • 这是默认的交互风格,也是最常用的。
      • 允许用户通过鼠标拖动来旋转、缩放和平移视图。
      • 适用于浏览和交互式操作3D场景。
    2. 飞行相机交互风格(vtkInteractorStyleFlight

      • 模拟了飞行模式,类似于飞行模拟器中的控制。
      • 用户可以通过键盘和鼠标控制相机的位置和方向。
      • 适用于虚拟现实应用和飞行模拟。
    3. 路径交互风格(vtkInteractorStylePath

      • 用于沿路径浏览数据。
      • 用户可以按照预定义的路径或自定义路径移动相机。
      • 适用于医学图像浏览和路径规划。
    4. 游戏交互风格(vtkInteractorStyleGame

      • 模拟了游戏中的相机控制。
      • 用户可以使用键盘和鼠标来移动相机。
      • 适用于游戏和虚拟现实应用。
    5. 旋转交互风格(vtkInteractorStyleJoystickActor

      • 允许用户通过鼠标或其他输入设备旋转物体。
      • 适用于需要精确旋转物体的应用。
    6. 轮廓交互风格(vtkInteractorStyleRubberBandZoom

      • 允许用户通过鼠标绘制一个矩形来放大或缩小视图。
      • 适用于局部放大或缩小感兴趣的区域。

    代码:

    class myInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
        def __init__(self, parent=None):
            self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent)
            self.AddObserver("RightButtonPressEvent", self.rightButtonPressEvent)
            self.AddObserver('KeyPressEvent', self.onKeyPress)
            self.AddObserver('MouseWheelForwardEvent', self.mouseWheelForwardEvent)
            self.AddObserver('MouseWheelBackwardEvent', self.mouseWheelBackwardEvent)
            self.AddObserver('MouseMoveEvent', self.mouseMoveEvent)
    
        def leftButtonPressEvent(self, obj, event):
            clickPos = self.GetInteractor().GetEventPosition()       # <<<-----<
            print(f'left button down: {clickPos}')
            self.OnLeftButtonDown()
    
        def rightButtonPressEvent(self, obj, event):
            clickPos = self.GetInteractor().GetEventPosition()       # <<<-----<
            print(f'right button down: {clickPos}')
            self.OnRightButtonDown()
    
        def onKeyPress(self, obj, event):
            # 获取按下的键
            key = self.GetInteractor().GetKeySym()
            print(f"Key {key} pressed")
            self.OnKeyPress()
    
        def mouseWheelForwardEvent(self, obj, event):
            print("forward")
            self.OnMouseWheelForward()
    
        def mouseWheelBackwardEvent(self, obj, event):
            print("backward")
            self.OnMouseWheelBackward()
    
        def mouseMoveEvent(self, obj, event):
            clickPos = self.GetInteractor().GetEventPosition()
            print(f"mouse move to {clickPos}")
            self.OnMouseMove()
    

    相关文章

      网友评论

          本文标题:python VTK库如何自定义交互风格

          本文链接:https://www.haomeiwen.com/subject/jiuaadtx.html