In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you how to use Python VTK to highlight the relevant knowledge points of actor, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
Foreword:
VTK, (visualizationtoolkit) is an open source free software system, mainly used for three-dimensional computer graphics, image processing and visualization. Vtk is designed and implemented on the basis of object-oriented principle. Its kernel is built with C++. It contains about 250000 lines of code, more than 2000 classes, and several conversion interfaces, so it is also free to use vtk through various languages of Java,Tcl/Tk and Python.
Introduction of main functions:
NewPickedActor.GetProperty (): through this function, you can set the properties of actor, such as color, surface style, and so on.
Vtk.vtkSphereSource (): the function to create spheres. In this article, 10 spheres are created through a for loop.
Vtk.vtkMinimalStandardRandomSequence (): VTK's random number generator, which is used for ten spheres in the code to randomly generate the size and position of the sphere.
MouseInteractorHighLightActor: defines the actor operation method, which is the control method of a mouse operation control.
LeftButtonPressEvent (self, obj, event): this is an event trigger function. When the left mouse button clicks on the corresponding actor, the function is triggered and the actor for the point is highlighted.
The main code is as follows:
#! / usr/bin/env python# noinspection PyUnresolvedReferencesimport vtkcolors = vtk.vtkNamedColors () NUMBER_OF_SPHERES = 10class 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 display the edge self.NewPickedActor.GetProperty (). SetColor (colors.GetColor3d ('Red')) self.NewPickedActor.GetProperty (). SetDiffuse (1. 0) self.NewPickedActor.GetProperty (). SetSpecular (0) self.NewPickedActor.GetProperty (). EdgeVisibilityOn () # Save the last selected actor self.LastPickedActor = self.NewPickedActor Self.OnLeftButtonDown () returndef main (): # create render and window renderer = vtk.vtkRenderer () renderer.SetBackground (colors.GetColor3d ('SteelBlue')) renwin = vtk.vtkRenderWindow () renwin.AddRenderer (renderer) renwin.SetSize Renwin.SetWindowName ('HighlightPickedActor') # establish interactor interactor = vtk.vtkRenderWindowInteractor () interactor.SetRenderWindow (renwin) # Interactive 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) 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 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 ()
The result is as follows: no sphere selected:
When the sphere is selected:
These are all the contents of the article "how to use Python VTK to highlight actor". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.