This is the 16th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
VTK (visual visualizationtoolkit) is an open source free software system for 3d computer graphics, image processing and visualization. Vtk is designed and implemented on the basis of object-oriented principles. Its core is built in C++, contains approximately 250,000 lines of code, over 2000 classes, and contains several conversion interfaces, so it is also free to use Vtk through Java, Tcl/Tk, and Python languages.
This article showed you how to use python-vtk to highlight selected actors in the same window. In this example, when the spherical actor in the window is selected, it turns red and displays the actor edge information three times.
Main functions:
NewPickedActor. GetProperty () : through the function, can set up the nature of the actor, such as surface color, style, etc.
Vtk.vtkspheresource () : a function to create spheres. This article creates 10 spheres through a for loop.
VTK. VtkMinimalStandardRandomSequence () : VTK random number generator, used in the code in ten spheres, randomly generated sphere size and location.
MouseInteractorHighLightActor: define the actor operation method, this is a mouse operation control method of control.
LeftButtonPressEvent (self, obj, event) : This is an event-triggering function that is triggered when the left mouse button is clicked on the corresponding actor, highlighting the actor.
The main code is 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 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() :
Create render and window
renderer = vtk.vtkRenderer()
renderer.SetBackground(colors.GetColor3d('SteelBlue'))
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(renderer)
renwin.SetSize(640.480)
renwin.SetWindowName('HighlightPickedActor')
Create interactors
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)
# run
interactor.Initialize()
renwin.Render()
interactor.Start()
if __name__ == '__main__':
main()
Copy the code
The following information is displayed: Sphere is not selected:
After the sphere is selected: