当前位置: 首页 > news >正文

ExtractPolyLinesFromPolyData切割一个三维模型(球体),并可视化切割后产生的多条等高线

一:主要的知识点

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()
http://www.cnnetsun.cn/news/119669.html

相关文章:

  • AI 编程的“90% 陷阱”:为什么你生成代码 1 分钟,修 Bug 却要 1 小时?
  • 终极免费抽奖神器:Magpie-LuckyDraw全平台部署指南
  • 技术人才职业发展:从工具思维到价值创造的成长阶梯
  • 百度贴吧用户脚本终极指南:告别繁琐操作,体验贴吧新境界
  • 等待节点-–-behaviac
  • Nginx性能优化实战:从基础配置到高级调优的完整指南
  • ThingsGateway:开源智能设备管理平台的终极指南
  • KolodaView开源项目贡献指南
  • 5‘-Thiol Modifier C6 S-S Amidite,5‘-硫醇修饰剂 C6 双硫键核苷酸酰胺化试剂
  • Python:SOLID 面向对象设计原则
  • 专业级鼠标性能测试工具:从数据采集到精准分析的全链路解析
  • Magpie-LuckyDraw:5分钟上手的多平台炫酷抽奖系统终极指南
  • 魔兽争霸III现代化修复工具:全面解决兼容性问题的终极指南
  • 数字内容获取革命:智能绕过付费墙的完整解决方案
  • 256台H100服务器算力中心的带外管理网络建设方案
  • 深入理解指针(7)
  • 从卷 Java 到冲网安!计算机人 2025 自救路线:附 40-150 万安全岗 + 技能衔接清单
  • python大数据的基于k-means算法的校园美食推荐系统_j4eg7g7z--论文
  • MouseTester专业指南:3步完成鼠标性能精准诊断
  • [鸿蒙2025领航者闯关]图标资源统一管理
  • 区分__proto__和prototype
  • 西门子PLC地址知识点
  • EmotiVoice开源项目依赖项管理最佳实践
  • 如何彻底解决腾讯游戏卡顿问题:sguard_limit资源限制器完整指南
  • MiniGPT-4终极优化指南:5个简单技巧实现3倍推理加速
  • 鼠标性能测试终极指南:从新手到专家的完整解决方案
  • 终极指南:如何用pbxproj轻松玩转Xcode项目文件
  • 移动端AI部署革命:Paddle-Lite如何让深度学习模型在手机上流畅运行
  • 类型安全强化学习实战:从Gymnasium类型提示到项目稳健性提升
  • OBS直播教程:OBS多路推流插件如何下载?如何安装?怎么用?