current position:Home>Highlight actor using Python VTK

Highlight actor using Python VTK

2022-01-31 21:37:43 Pie Da Da Xing

This is my participation 11 The fourth of the yuegengwen challenge 16 God , Check out the activity details :2021 One last more challenge

VTK,(visualizationtoolkit) It is a free software system with open resources , Mainly used in 3D computer graphics 、 Image processing and visualization .Vtk It is designed and implemented on the basis of object-oriented principle , Its kernel uses C++ Built , Contains about 250,000 Line code ,2000 Multiple classes , There are also several conversion interfaces , So it's free to pass Java,Tcl/Tk and Python Use of various languages vtk.

This article explains how to use it Python-VTK In the same window , Highlight the selected actor. The code in this example , When the ball in the window actor When selected , It turns red , And the actor Three times the patch edge information .

Main function introduction :

NewPickedActor.GetProperty(): Through this function , You can set actor The nature of , Such as color 、 Surface style, etc .

vtk.vtkSphereSource(): Function to create a sphere , Through a for Loop created 10 A sphere .

vtk.vtkMinimalStandardRandomSequence(): VTK The random number generator of , For the ten spheres in the code , Randomly generate the size and position of the sphere .

MouseInteractorHighLightActor: Definition actor Operation method , This is a control method of mouse operation control .

leftButtonPressEvent(self, obj, event): This is an event trigger function , When the left mouse button clicks the corresponding actor when , Will trigger the function , Point to point actor Highlight .

The main codes are as follows :

#!/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())

        #  Create a new 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())
            #  Highlight the selected sphere , And show the edges 
            self.NewPickedActor.GetProperty().SetColor(colors.GetColor3d('Red'))
            self.NewPickedActor.GetProperty().SetDiffuse(1.0)
            self.NewPickedActor.GetProperty().SetSpecular(0.0)
            self.NewPickedActor.GetProperty().EdgeVisibilityOn()

            #  Save the last selected actor
            self.LastPickedActor = self.NewPickedActor

        self.OnLeftButtonDown()
        return


def main():
    #  establish render and window
    renderer = vtk.vtkRenderer()
    renderer.SetBackground(colors.GetColor3d('SteelBlue'))

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

    #  establish interactor( interactions )
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renwin)

    #  Interactive operation method 
    style = MouseInteractorHighLightActor()
    style.SetDefaultRenderer(renderer)
    interactor.SetInteractorStyle(style)

    randomSequence = vtk.vtkMinimalStandardRandomSequence()
    # randomSequence.SetSeed(1043618065)
    # randomSequence.SetSeed(5170)
    randomSequence.SetSeed(8775070)
    #  Add sphere 
    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)

    #  function 
    interactor.Initialize()
    renwin.Render()
    interactor.Start()


if __name__ == '__main__':
    main()
 Copy code 

The results are as follows : No sphere selected : image.png

After the sphere has been selected : image.png

copyright notice
author[Pie Da Da Xing],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201312137389504.html

Random recommended