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

ExtractSelection 选择和提取数据集中的特定点,以及如何反转该选择

一:主要的知识点

1、说明

本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程——逐行解析官网所有Python示例-CSDN博客

2、知识点纪要

本段代码主要涉及的有①vtkSelectionNode的作用及使用方法


二:代码及注释

import vtkmodules.vtkRenderingOpenGL2 from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkFiltersSources import vtkPointSource from vtkmodules.vtkCommonCore import vtkIdTypeArray from vtkmodules.vtkCommonDataModel import vtkSelectionNode, vtkSelection from vtkmodules.vtkFiltersExtraction import vtkExtractSelection from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid from vtkmodules.vtkRenderingCore import ( vtkActor, vtkCamera, vtkDataSetMapper, vtkRenderWindow, vtkRenderWindowInteractor, vtkRenderer ) def main(): colors = vtkNamedColors() # 创建一个包含 50 个点的随机点云数据 point_source = vtkPointSource() point_source.SetNumberOfPoints(50) point_source.Update() print("point_source有", point_source.GetOutput().GetNumberOfPoints(), '个input points') """ vtkIdTypeArray 专门用来存放 点 ID 或 cell ID(索引),它的数据类型就是 vtkIdType 在vtk中, 数组(例如 vtkIdTypeArray, vtkDoubleArray)不仅是一个一维向量,它实际上是一个 二维表 tuple(元组) = 一行数据 component(分量) = 每行里面的列数 所以 如果 SetNumberOfComponents(1),每个 tuple 就是一个 标量(一列) 如果 SetNumberOfComponents(3),每个 tuple 就是一个 三维向量(比如点的 (x, y, z)) SetNumberOfTuples(n) 定义数组有多少个 tuple(行数) SetNumberOfComponents(n) 定义每个 tuple 有多少个分量(component)(行数) SetNumberOfValues(n) 直接指定总的存储单元个数(底层一维存储大小)SetNumberOfValues(9) 等价于 (NumberOfTuples × NumberOfComponents) = 9 如果前面 SetNumberOfComponents(3),那就意味着有 3 个 tuple(每行 3 个数) """ ids = vtkIdTypeArray() ids.SetNumberOfComponents(1) # ids.SetNumberOfTuples(1) # ids.SetNumberOfValues(1) for i in range(10, 20): ids.InsertNextValue(i) """ vtkSelectionNode 是一个 VTK 类,它的主要作用是定义和存储一个数据选择的特定条件。 可以把它看作是构建一个复杂选择查询的最小单元 vtkSelectionNode 本身并不能单独完成数据选择,它必须作为 vtkSelection 对象的组成部分才能生效。 每个 vtkSelectionNode 实例都包含了数据筛选的三个关键信息: 字段类型 (FieldType): 告诉 VTK 你要选择什么类型的数据。常见的类型有: vtkSelectionNode.POINT:选择点数据。 vtkSelectionNode.CELL:选择单元(面、线、体)数据。 vtkSelectionNode.FIELD:选择字段数据。 内容类型 (ContentType): 决定了你将如何指定选择标准。常见的类型有: vtkSelectionNode.INDICES:通过索引 ID 列表来选择数据(就像你代码中那样)。 vtkSelectionNode.THRESHOLDS:通过数据阈值来选择数据(例如,选择所有温度高于 50 度的点)。 vtkSelectionNode.FRUSTUM:通过摄像机视锥体来选择数据(例如,选择视线范围内的所有对象)。 选择列表 (SelectionList): 这是一个 vtkIdList 或其他数据数组,包含了实际的筛选值。例如,如果你选择了 INDICES 类型,这个列表就会包含你想要选中的所有点的 ID。 """ selection_node = vtkSelectionNode() selection_node.SetFieldType(vtkSelectionNode.POINT) selection_node.SetContentType(vtkSelectionNode.INDICES) selection_node.SetSelectionList(ids) """ vtkSelection 将一个或多个 vtkSelectionNode 对象组合在一起,形成一个完整的、可供过滤器使用的选择规则 AddNode添加的多个Node,查询的关系或的关系,例如NodeA选择所有 ID 在 1 到 10 的点 NodeB选择所有 ID 在 21 到 30 的点 那么 vtkExtractSelection 最终会返回 ID 在 1 到 10 以及 21 到 30 的所有点。这相当于一个逻辑或(OR)操作 如果想要实现and的操作,则需要建立管道链接 """ selection = vtkSelection() selection.AddNode(selection_node) """ vtkExtractSelection 它的作用是根据一个或多个选择条件,从数据集中提取出一个子集 当 vtkExtractSelection 运行时,它会遍历原始数据集中的所有元素(点或单元),检查每个元素是否满足 vtkSelection 中定义的任何一个条件。 所有满足条件的元素都会被提取出来,形成一个新的、更小的数据集作为输出 """ extract_selection = vtkExtractSelection() extract_selection.SetInputConnection(0, point_source.GetOutputPort()) extract_selection.SetInputData(1, selection) extract_selection.Update() selected = vtkUnstructuredGrid() selected.ShallowCopy(extract_selection.GetOutput()) # 选中的反转 selection_node.GetProperties().Set(vtkSelectionNode().INVERSE(), 1) # invert the selection. extract_selection.Update() not_selected = vtkUnstructuredGrid() not_selected.ShallowCopy(extract_selection.GetOutput()) input_mapper = vtkDataSetMapper() input_mapper.SetInputConnection(point_source.GetOutputPort()) input_actor = vtkActor() input_actor.SetMapper(input_mapper) input_actor.GetProperty().SetColor(colors.GetColor3d("MidnightBlue")) input_actor.GetProperty().SetPointSize(5) selected_mapper = vtkDataSetMapper() selected_mapper.SetInputData(selected) selected_actor = vtkActor() selected_actor.SetMapper(selected_mapper) selected_actor.GetProperty().SetColor(colors.GetColor3d("MidnightBlue")) selected_actor.GetProperty().SetPointSize(5) not_selected_mapper = vtkDataSetMapper() not_selected_mapper.SetInputData(not_selected) not_selected_actor = vtkActor() not_selected_actor.SetMapper(not_selected_mapper) not_selected_actor.GetProperty().SetColor(colors.GetColor3d("MidnightBlue")) not_selected_actor.GetProperty().SetPointSize(5) # There will be one render window. render_window = vtkRenderWindow() render_window.SetSize(900, 300) render_window.SetWindowName("ExtractSelectedIds") # And one interactor. interactor = vtkRenderWindowInteractor() interactor.SetRenderWindow(render_window) # Define viewport ranges. # (xmin, ymin, xmax, ymax) left_viewport = [0.0, 0.0, 0.33, 1.0] center_viewport = [0.33, 0.0, 0.66, 1.0] right_viewport = [0.66, 0.0, 1.0, 1.0] # Create a camera for all renderers. camera = vtkCamera() # Setup the renderers left_renderer = vtkRenderer() render_window.AddRenderer(left_renderer) left_renderer.SetViewport(left_viewport) left_renderer.SetBackground(colors.GetColor3d("BurlyWood")) left_renderer.SetActiveCamera(camera) center_renderer = vtkRenderer() render_window.AddRenderer(center_renderer) center_renderer.SetViewport(center_viewport) center_renderer.SetBackground(colors.GetColor3d("orchid_dark")) center_renderer.SetActiveCamera(camera) right_renderer = vtkRenderer() render_window.AddRenderer(right_renderer) right_renderer.SetViewport(right_viewport) right_renderer.SetBackground(colors.GetColor3d("CornflowerBlue")) right_renderer.SetActiveCamera(camera) left_renderer.AddActor(input_actor) center_renderer.AddActor(selected_actor) right_renderer.AddActor(not_selected_actor) left_renderer.ResetCamera() render_window.Render() interactor.Start() if __name__ == '__main__': main()
http://www.gsyq.cn/news/118888.html

相关文章:

  • 小熊猫Dev-C++快速上手教程:零基础搭建C/C++开发环境
  • 无需重造轮子!Kotaemon提供开箱即用的RAG组件
  • TLS网络安全协议巩固知识基础题(5)
  • 再见 PotPlayer!更好用的开源播放器,来了
  • 基于GoFrame与微内核架构的企业级物联网平台设计与实现
  • 3步解锁Wallpaper Engine创意工坊:这款下载器如何让壁纸获取变得如此简单?
  • 图灵电子书全场限时折扣,新书老书同步参与!
  • Bypass Paywalls Clean终极指南:轻松绕过付费墙的5种简单方法
  • 嵌入式学习!(一)C++学习(16)入门-12/17
  • 5大付费墙绕过技术深度解析:Bypass Paywalls Clean终极使用指南
  • 中新全新医疗健康枢纽落户重庆,重庆鹏瑞利健康城项目启动 | 美通社头条
  • 百事可乐无糖推出全新草莓奶昔味产品
  • Springboot商洛市精准扶贫管理系统h906y(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 哔哩下载姬DownKyi:专业级B站视频内容管理解决方案深度解析
  • Redis 生产环境命令管控规范
  • PMOST管防反接功能
  • GKD自动化工具:解放双手的终极手机助手 [特殊字符]✨
  • 终极免费抽奖神器:Magpie-LuckyDraw全平台部署指南
  • 5-FAM,单一异构体,5-Carboxyfluorescein, 5-FAM, 单一异构体
  • Quasar 705 T Amidite,Quasar 705 T 核苷酸酰胺化试剂,化学特性
  • 百度贴吧用户脚本终极指南:告别繁琐操作,体验贴吧新境界
  • Unity RestClient 终极指南:告别回调地狱的异步网络编程
  • 驱动开发系列74 - GPU中的I2C
  • 思考与练习之答案与解析(第六章 程序控制结构)
  • 问财数据获取终极方案:Python量化分析新利器
  • 词库转换全攻略:告别输入法迁移困扰的终极解决方案
  • springboot甘肃非物质文化网站的设计与开发(11509)
  • Python包管理革命:在AI工作流中如何选择pip与uv
  • 基于SpringBoot的企业客户管理系统(11503)
  • Webpack模块解析陷阱:当“default“成为你的调试噩梦