2026/2/28 6:25:02
网站建设
项目流程
wordpress站内跳转,正能量网址能直接用的,响应式手机网站制作,抖音代运营服务合同一#xff1a;主要的知识点
1、说明
本文只是教程内容的一小段#xff0c;因博客字数限制#xff0c;故进行拆分。主教程链接#xff1a;vtk教程——逐行解析官网所有Python示例-CSDN博客
2、知识点纪要
本段代码主要涉及的有①着色方式#xff0c;②vtkStripper的作用…一主要的知识点1、说明本文只是教程内容的一小段因博客字数限制故进行拆分。主教程链接vtk教程——逐行解析官网所有Python示例-CSDN博客2、知识点纪要本段代码主要涉及的有①着色方式②vtkStripper的作用二代码及注释import vtkmodules.vtkRenderingOpenGL2 from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkCommonCore import vtkIdList from vtkmodules.vtkCommonDataModel import vtkPlane from vtkmodules.vtkFiltersCore import vtkCutter, vtkStripper from vtkmodules.vtkFiltersSources import vtkSphereSource from vtkmodules.vtkRenderingCore import ( vtkActor, vtkPolyDataMapper, vtkRenderWindow, vtkRenderWindowInteractor, vtkRenderer ) def main(): colors vtkNamedColors() lineColor colors.GetColor3d(peacock) modeColor colors.GetColor3d(silver) backgroundColor colors.GetColor3d(wheat) modelSource vtkSphereSource() plane vtkPlane() cutter vtkCutter() cutter.SetCutFunction(plane) cutter.SetInputConnection(modelSource.GetOutputPort()) cutter如何去切取决去vtkPlane设置的平面的法向量 如果设置的平面的法向量为(0,0,1) 则表示切割球体的平面为平行于XY平面。 -0.5, 0.5表示这个平面从-0.5的z值高度沿着法向量的方向一直移动到0.5中间取10个值 cutter.GenerateValues(10, -0.5, 0.5) modelMapper vtkPolyDataMapper() modelMapper.SetInputConnection(modelSource.GetOutputPort()) model vtkActor() model.SetMapper(modelMapper) model.GetProperty().SetDiffuseColor(modeColor) SetInterpolationToFlat 将模型的着色方式设置为平面着色 当启用平面着色时VTK 会对每个多边形如三角形或四边形使用一个单一的、统一的颜色来填充。这意味着 着色Shading每个面片的颜色是恒定的从一个顶点到另一个顶点不会有渐变。 光照Lighting光照计算只会在每个面片的中心进行一次然后整个面片都用这个颜色来渲染。 model.GetProperty().SetInterpolationToFlat() vtkStripper 的作用是把 线段或三角形片元 按照拓扑关系合并成更长的 polyline折线或 triangle strip三角带。 没有 vtkStripper 的话vtkCutter 切出来的结果可能是一堆独立的小线段。 加上 vtkStripper这些小线段如果端点连续就会被自动拼接成更长的线条更利于渲染或后续处理 stripper vtkStripper() stripper.SetInputConnection(cutter.GetOutputPort()) JoinContiguousSegmentsOn 决定 vtkStripper 是否把 首尾相接的线段 stripper.JoinContiguousSegmentsOn() linesMapper vtkPolyDataMapper() linesMapper.SetInputConnection(stripper.GetOutputPort()) lines vtkActor() lines.SetMapper(linesMapper) lines.GetProperty().SetDiffuseColor(lineColor) lines.GetProperty().SetLineWidth(3.) renderer vtkRenderer() renderWindow vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindow.SetSize(640, 480) renderWindow.SetWindowName(ExtractPolyLinesFromPolyData) interactor vtkRenderWindowInteractor() interactor.SetRenderWindow(renderWindow) # Add the actors to the renderer. renderer.AddActor(model) renderer.AddActor(lines) renderer.SetBackground(backgroundColor) renderer.GetActiveCamera().Azimuth(-45) renderer.GetActiveCamera().Elevation(-22.5) renderer.ResetCamera() # This starts the event loop and as a side effect causes an # initial render. renderWindow.Render() interactor.Start() # 获取线条的数目 numberOfLines cutter.GetOutput().GetNumberOfLines() print(-----------Lines without using vtkStripper) print(There are {0} lines in the polydata.format(numberOfLines)) numberOfLines stripper.GetOutput().GetNumberOfLines() points stripper.GetOutput().GetPoints() cells stripper.GetOutput().GetLines() cells.InitTraversal() # 重置遍历器将内部的迭代器指针移到第一个单元的位置 print(-----------Lines using vtkStripper) print(There are {0} lines in the polydata.format(numberOfLines)) indices vtkIdList() lineCount 0 while cells.GetNextCell(indices): # 类比于for cell in cells: # indices 里现在就是当前 cell 的点索引 print(Line {0}:.format(lineCount)) for i in range(indices.GetNumberOfIds()): point points.GetPoint(indices.GetId(i)) print(\t({0:0.6f} ,{1:0.6f}, {2:0.6f}).format(point[0], point[1], point[2])) lineCount 1 if __name__ __main__: main()