Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python VTK to map the surface distance of 3D model

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to use Python VTK mapping 3D model surface distance related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this article on how to use Python VTK mapping 3D model surface distance article will have a harvest, let's take a look.

Data preparation: need to prepare two stl files, Python need to install VTK library

Step 1: data reading first defines the stl file reading interface through vtk.vtkSTLReader (), and then the data of stl in the vtk workflow can be obtained through reader1.GetOutput ().

Step 2: remove the duplicate points. You can remove the repeated points in the model through vtk.vtkCleanPolyData ().

Step 3: calculate the distance using vtk.vtkDistancePolyDataFilter () and use the data filtered out in the previous step as input. For example, distanceFilter.SetInputConnection (1, clean1.GetOutputPort ()), where the first parameter is the label of the input data, counting from 0; the second parameter is the input data. We complete the distance mapping by outputting vtkDistancePolyDataFilter to mapper.

Step 4: color configuration lut = vtk.vtkLookupTable () is equivalent to a palette function, and the final mapped color range can be adjusted by changing its parameters. ScalarBar = vtk.vtkScalarBarActor () is the color bar that maps the distance values to colors according to the results of the previous palette.

Import vtkinput1 = vtk.vtkPolyData () reader1 = vtk.vtkSTLReader () reader1.SetFileName ('model1.stl') reader1.Update () input1 = reader1.GetOutput () # read model Ainput2 = vtk.vtkPolyData () reader2 = vtk.vtkSTLReader () reader2.SetFileName (' model2.stl') reader2.Update () input2 = reader2.GetOutput () # read model B# data merge You can combine and display two models clean1 = vtk.vtkCleanPolyData () clean1.SetInputData (input1) clean2 = vtk.vtkCleanPolyData () clean2.SetInputData (input2) distanceFilter = vtk.vtkDistancePolyDataFilter () distanceFilter.SetInputConnection (1, clean1.GetOutputPort ()) distanceFilter.SetInputConnection (0 Clean2.GetOutputPort () distanceFilter.SignedDistanceOff () distanceFilter.Update () # calculate distance distanceFilter.GetOutputPort () mapper = vtk.vtkPolyDataMapper () # configure mappermapper.SetInputConnection (distanceFilter.GetOutputPort ()) mapper.SetScalarRange (# set color mapping range distanceFilter.GetOutput (). GetPointData (). GetScalars (). GetRange () [0] DistanceFilter.GetOutput (). GetPointData (). GetScalars (). GetRange () actor = vtk.vtkActor () actor.SetMapper (mapper) actor1 = vtk.vtkActor () actor1.SetMapper (mapper) lut = vtk.vtkLookupTable () lut.SetHueRange (0.2,0.7) # Color transformation parameters (self-color adjustment) # lut.SetAlphaRange (1.0,1.0) # lut.SetValueRange (1.0,1.0) # lut.SetSaturationRange (1.0,1.0) ) # lut.SetNumberOfTableValues (256) mapper.SetLookupTable (lut) mapper2 = vtk.vtkPolyDataMapper () mapper2.SetInputData ((distanceFilter.GetSecondDistanceOutput () mapper2.SetScalarRange (# sets the color mapping range distanceFilter.GetSecondDistanceOutput () .GetPointData () .GetScalars () .GetRange () [0] DistanceFilter.GetSecondDistanceOutput () .GetPointData () .GetScalars () .GetRange () actor2 = vtk.vtkActor () actor2.SetMapper (mapper2) scalarBar = vtk.vtkScalarBarActor () # set color_barscalarBar.SetLookupTable (mapper.GetLookupTable ()) scalarBar.SetTitle ("SD (mm)") scalarBar.SetNumberOfLabels (5) # sets the number of tick labels to display. Set the position of the ribbon scalarBar.SetMaximumNumberOfColors (10) # scalarBar.GetPositionCoordinate (). SetCoordinateSystemToNormalizedViewport () # scalarBar.GetPositionCoordinate (). SetValue (0.01,0.49) # the smaller the parameter, the more to the left The larger the second parameter, the higher the # scalarBar.SetWidth (0.16) # scalarBar.SetHeight (0.5) # scalarBar.SetTextPositionToPrecedeScalarBar () # whether the title and scale mark should precede the scalar bar (text will appear on the left side of the bar) # # set the margin between the title and the bar # scalarBar.SetVerticalTitleSeparation (10) # # set the title color scalarBar.DrawTickLabelsOn () scalarBar.GetTitleTextProperty (). SetColor (0,0,0) scalarBar.GetLabelTextProperty (). SetColor (0,0) 0) arender = vtk.vtkRenderer () arender.SetViewport (0,0.0,1,1.0) renWin = vtk.vtkRenderWindow () renWin.AddRenderer (arender) iren = vtk.vtkRenderWindowInteractor () iren.SetRenderWindow (renWin) style = vtk.vtkInteractorStyleTrackballActor () iren.SetInteractorStyle (style) aCamera = vtk.vtkCamera () aCamera.SetViewUp (0,0,1) aCamera.SetPosition (0,0,1) 0) aCamera.ComputeViewPlaneNormal () aCamera.Azimuth () aCamera.Elevation (30.0) aCamera.Dolly (1.5) arender.AddActor (actor) # arender.AddActor (actor1) arender.SetActiveCamera (aCamera) arender.ResetCamera () arender.SetBackground (1,1,1) arender.ResetCameraClippingRange () arender.AddActor2D (scalarBar) renWin.Render () iren.Initialize () iren.Start ()

Example of the result:

This is the end of the article on "how to map the surface distance of a 3D model with Python VTK". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to map the surface distance of 3D models with Python VTK". If you want to learn more, you are welcome to follow 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report