vtkStaticCellLocator intersectWithLine

 

代码来自 VTK\Filters\Sources\Testing\Python\TestStaticCellLocatorLineIntersection.py

 

  1 # -*- coding : UTF-8 -*-
  2 # @file   : testStaticCellLocatorLineIntersection.py
  3 # @Time   : 2022-04-26 14:24
  4 # @Author : wmz
  5 
  6 #!/usr/bin/env python
  7 import vtk
  8 # from vtk.util.misc import vtkGetDataRoot
  9 # VTK_DATA_ROOT = vtkGetDataRoot()
 10 
 11 # retrieve named colors
 12 def GetRGBColor(colorName):
 13     '''
 14         Return the red, green and blue components for a
 15         color as doubles.
 16     '''
 17     rgb = [0.0, 0.0, 0.0]  # black
 18     vtk.vtkNamedColors().GetColorRGB(colorName, rgb)
 19     return rgb
 20 
 21 # Control resolution of test (sphere resolution)
 22 res = 9
 23 
 24 # Create the RenderWindow, Renderer
 25 #
 26 ren = vtk.vtkRenderer()
 27 renWin = vtk.vtkRenderWindow()
 28 renWin.AddRenderer( ren )
 29 
 30 iren = vtk.vtkRenderWindowInteractor()
 31 iren.SetRenderWindow(renWin)
 32 
 33 # Create pipeline. Two spheres: one is the target to
 34 # be intersected against, and is placed inside a static
 35 # cell locator. The second bounds these, it's points
 36 # serve as starting points that shoot rays towards the
 37 # center of the fist sphere.
 38 #
 39 sphere = vtk.vtkSphereSource()
 40 sphere.SetThetaResolution(2*res)
 41 sphere.SetPhiResolution(res)
 42 sphere.Update()
 43 
 44 mapper = vtk.vtkPolyDataMapper()
 45 mapper.SetInputConnection(sphere.GetOutputPort())
 46 
 47 actor = vtk.vtkActor()
 48 actor.SetMapper(mapper)
 49 
 50 # Now the locator
 51 loc = vtk.vtkStaticCellLocator()
 52 loc.SetDataSet(sphere.GetOutput())
 53 loc.SetNumberOfCellsPerNode(5)
 54 loc.BuildLocator()
 55 
 56 locPD = vtk.vtkPolyData()
 57 loc.GenerateRepresentation(4,locPD)
 58 locMapper = vtk.vtkPolyDataMapper()
 59 locMapper.SetInputData(locPD)
 60 locActor = vtk.vtkActor()
 61 locActor.SetMapper(locMapper)
 62 locActor.GetProperty().SetRepresentationToWireframe()
 63 
 64 # Now the outer sphere
 65 sphere2 = vtk.vtkSphereSource()
 66 sphere2.SetThetaResolution(res)
 67 sphere2.SetPhiResolution(int(res/2))
 68 sphere2.SetRadius(3*sphere.GetRadius())
 69 sphere2.Update()
 70 
 71 # Generate intersection points
 72 center = sphere.GetCenter()
 73 polyInts = vtk.vtkPolyData()
 74 pts = vtk.vtkPoints()
 75 spherePts = sphere2.GetOutput().GetPoints()
 76 numRays = spherePts.GetNumberOfPoints()
 77 pts.SetNumberOfPoints(numRays + 1)
 78 
 79 polyRays = vtk.vtkPolyData()
 80 rayPts = vtk.vtkPoints()
 81 rayPts.SetNumberOfPoints(numRays + 1)
 82 lines = vtk.vtkCellArray()
 83 
 84 t = vtk.reference(0.0)
 85 subId = vtk.reference(0)
 86 cellId = vtk.reference(0)
 87 xyz = [0.0,0.0,0.0]
 88 xInt = [0.0,0.0,0.0]
 89 pc = [0.0,0.0,0.0]
 90 
 91 pts.SetPoint(0,center)
 92 rayPts.SetPoint(0,center)
 93 for i in range(0, numRays):
 94     spherePts.GetPoint(i,xyz)
 95     rayPts.SetPoint(i+1,xyz)
 96     cellId = vtk.reference(i);
 97     hit = loc.IntersectWithLine(xyz, center, 0.001, t, xInt, pc, subId, cellId)
 98     if ( hit == 0 ):
 99         print("Missed: {}".format(i))
100         pts.SetPoint(i+1,center)
101     else:
102         pts.SetPoint(i+1,xInt)
103     lines.InsertNextCell(2)
104     lines.InsertCellPoint(0)
105     lines.InsertCellPoint(i+1)
106 
107 polyInts.SetPoints(pts)
108 
109 polyRays.SetPoints(rayPts)
110 polyRays.SetLines(lines)
111 
112 # Glyph the intersection points
113 glyphSphere = vtk.vtkSphereSource()
114 glyphSphere.SetPhiResolution(6)
115 glyphSphere.SetThetaResolution(12)
116 
117 glypher = vtk.vtkGlyph3D()
118 glypher.SetInputData(polyInts)
119 glypher.SetSourceConnection(glyphSphere.GetOutputPort())
120 glypher.SetScaleFactor(0.05)
121 
122 glyphMapper = vtk.vtkPolyDataMapper()
123 glyphMapper.SetInputConnection(glypher.GetOutputPort())
124 
125 glyphActor = vtk.vtkActor()
126 glyphActor.SetMapper(glyphMapper)
127 glyphActor.GetProperty().SetColor(GetRGBColor('peacock'))
128 
129 linesMapper = vtk.vtkPolyDataMapper()
130 linesMapper.SetInputData(polyRays)
131 
132 linesActor = vtk.vtkActor()
133 linesActor.SetMapper(linesMapper)
134 linesActor.GetProperty().SetColor(GetRGBColor('tomato'))
135 
136 # Render it
137 ren.AddActor(actor)
138 ren.AddActor(glyphActor)
139 ren.AddActor(locActor)
140 ren.AddActor(linesActor)
141 
142 ren.GetActiveCamera().SetPosition(1,1,1)
143 ren.GetActiveCamera().SetFocalPoint(0,0,0)
144 ren.ResetCamera()
145 
146 renWin.Render()
147 iren.Start()

执行结果:

 

posted @ 2022-04-26 14:35  巨鹿王十二  阅读(174)  评论(0编辑  收藏  举报