pyvtk

2023-09-21  本文已影响0人  c之气三段

vtkwidget

from vtkmodules.qt.QVTKRenderWindowInteractor import *
from vtkmodules.all import *
from .VtkInteractor.VtkInteractorManager import *
from enum import Enum


class ViewDirectType(Enum):
    View_DefaultView = 0
    View_FrontView = 1
    View_BackView = 2
    View_LeftView = 3
    View_RightView = 4
    View_PlanView = 5
    View_UpwardnView = 6
    View_RefreshView = 7
    View_ResetView = 8


class VtkWidget(QVTKRenderWindowInteractor):
    def __init__(self, parent=None):
        super(VtkWidget, self).__init__(parent)
        vtkOutputWindow.SetGlobalWarningDisplay(0)
        # 创建窗口
        self.renderer = vtkRenderer()
        self.renderer.SetBackground(1.0, 1.0, 1.0)  # 设置页面底部颜色值
        self.renderer.SetBackground2(0.529, 0.8078, 0.92157)  # 设置页面顶部颜色值
        self.renderer.SetGradientBackground(1)
        self.GetRenderWindow().AddRenderer(self.renderer)  # 添加图层
        self.interactorManager = VtkInteractorManager()
        self.interactor = self.interactorManager.creatInteractor(InteractorEnum.VtkActorViewInteractor)
        self.GetRenderWindow().GetInteractor().SetInteractorStyle(self.interactor)
        # 相机
        self.camera = vtkCamera()
        self.camera.SetParallelProjection(True)  # 透视投影
        self.renderer.SetActiveCamera(self.camera)
        # 色卡
        self.scalarBarActor = vtkScalarBarActor()
        self.cardcolorTable = vtkLookupTable()
        self.cardcolorTable.SetNumberOfColors(255)
        self.cardcolorTable.SetHueRange(0.67, 0)
        self.cardcolorTable.Build()
        self.scalarBarActor.GetTitleTextProperty().SetColor(0.3, 0, 1)
        self.scalarBarActor.GetTitleTextProperty().SetFontSize(20);
        self.scalarBarActor.GetLabelTextProperty().SetColor(0.5, 0.5, 0.5);
        self.scalarBarActor.GetLabelTextProperty().SetFontSize(15);
        self.scalarBarActor.SetUnconstrainedFontSize(True);
        self.scalarBarActor.SetMaximumWidthInPixels(50);
        self.scalarBarActor.SetMaximumHeightInPixels(400);
        self.scalarBarActor.SetLookupTable(self.cardcolorTable)
        self.scalarBarActor.SetNumberOfLabels(8)  # 显示数据的个数
        self.scalarBarActor.SetOrientationToVertical()  # 垂直
        self.scalarBarActor.SetVerticalTitleSeparation(10)  # 当方向为垂直时,设置标题和条形之间的边距
        self.scalarBarActor.VisibilityOn()
        self.renderer.AddActor2D(self.scalarBarActor)
        # 光照
        self.light = vtkLight()
        self.light.SetFocalPoint(0, 0, 0)
        self.light.SetColor(1, 1, 1)
        self.light.SetIntensity(1.0)
        self.light.SetPosition(0, 0, 1)
        self.light.PositionalOff()  # 方向光
        self.light.SetLightTypeToCameraLight()  # 跟随相机移动
        self.renderer.AddLight(self.light)
        # 坐标轴
        self.axesWidget = vtkOrientationMarkerWidget()
        self.axesActor = vtkAxesActor()
        self.axesWidget.SetOutlineColor(0.9, 0.6, 0.1)
        self.axesWidget.SetOrientationMarker(self.axesActor)
        self.axesWidget.SetViewport(0.0, 0.0, 0.2, 0.2)
        self.axesWidget.SetInteractor(self.GetRenderWindow().GetInteractor())
        self.axesWidget.SetEnabled(1)  # 部件模式
        self.axesWidget.InteractiveOff()  # 静止被移动
        # 初始化vtk窗口
        self.GetRenderWindow().Initialize()
        self.GetRenderWindow().Start()
        # 默认角度
        self.setViewType(ViewDirectType.View_DefaultView)
        self.GetRenderWindow().Render()

    def setViewType(self, viewDirectType):
        if viewDirectType == ViewDirectType.View_RefreshView:
            self.GetRenderWindow().Render()
            return 
        if viewDirectType == ViewDirectType.View_ResetView:
            self.renderer.ResetCamera()
            return 
        if viewDirectType == ViewDirectType.View_DefaultView:
            self.renderer.GetActiveCamera().SetPosition(0.5,0.3,0.3)
            self.renderer.GetActiveCamera().SetViewUp(0.0, 0, 1.0)
        elif viewDirectType == ViewDirectType.View_FrontView:
            self.renderer.GetActiveCamera().SetPosition(1.0, 0.0, 0.0)
            self.renderer.GetActiveCamera().SetViewUp(0.0, 0.0, 1.0)
        elif viewDirectType == ViewDirectType.View_BackView:
            self.renderer.GetActiveCamera().SetPosition(-1.0, 0.0, 0.0)
            self.renderer.GetActiveCamera().SetViewUp(0.0, 0, 1.0)
        elif viewDirectType == ViewDirectType.View_LeftView:
            self.renderer.GetActiveCamera().SetPosition(0.0, -1.0, 0.0)
            self.renderer.GetActiveCamera().SetViewUp(0.0, 0, 1.0)
        elif viewDirectType == ViewDirectType.View_RightView:
            self.renderer.GetActiveCamera().SetPosition(1.0, 0.0, 0.0)
            self.renderer.GetActiveCamera().SetViewUp(0.0, 0.0, 1.0)
        elif viewDirectType == ViewDirectType.View_PlanView:
            self.renderer.GetActiveCamera().SetPosition(0.0, 0.0, 1.0)
            self.renderer.GetActiveCamera().SetViewUp(1.0, 0.0, 0.0)
        elif viewDirectType == ViewDirectType.View_UpwardnView:
            self.renderer.GetActiveCamera().SetPosition(0.0, 0.0, -1.0)
            self.renderer.GetActiveCamera().SetViewUp(-1.0, 0.0, 0.0)
        self.renderer.GetActiveCamera().SetFocalPoint(0.0, 0.0, 0.0)
        self.refreshView()


上一篇下一篇

猜你喜欢

热点阅读