news 2026/4/22 19:42:42

CellPicking 网格面的选择与变色(vtkCellPicker)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
CellPicking 网格面的选择与变色(vtkCellPicker)

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①vtkCellPicker在3D场景中选取几何对象,②vtkExtractSelection与vtkSelection实现的选择与提取


二:代码及注释

import vtkmodules.vtkRenderingOpenGL2 import vtkmodules.vtkInteractionStyle from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkFiltersSources import vtkPlaneSource from vtkmodules.vtkFiltersCore import vtkTriangleFilter from vtkmodules.vtkRenderingCore import vtkActor, vtkRenderer, vtkRenderWindow, vtkRenderWindowInteractor, \ vtkPolyDataMapper, vtkDataSetMapper, vtkCellPicker from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera from vtkmodules.vtkCommonCore import vtkIdTypeArray from vtkmodules.vtkCommonDataModel import vtkSelection, vtkSelectionNode, vtkUnstructuredGrid from vtkmodules.vtkFiltersExtraction import vtkExtractSelection class MouseInteractorStyle(vtkInteractorStyleTrackballCamera): def __init__(self, data): self.AddObserver('LeftButtonPressEvent', self.left_button_press_event) self.data = data self.selected_mapper = vtkDataSetMapper() self.selected_actor = vtkActor() def left_button_press_event(self, obj, event): colors = vtkNamedColors() pos = self.GetInteractor().GetEventPosition() """ vtkCellPicker 用于在3D 场景中通过鼠标点击选取几何对象(cells) 工作原理: 将屏幕坐标 (x, y) 转换为 3D 世界坐标射线(ray),用这条射线与场景中的几何体逐个求交,找到射线距离相机最近的相交点。 """ picker = vtkCellPicker() picker.SetTolerance(0.00005) # 设置拾取容差(默认 1e-6) picker.Pick(pos[0], pos[1], 0, self.GetDefaultRenderer()) # 执行拾取操作,参数为窗口坐标 world_position = picker.GetPickPosition() print(f'Cell id is: {picker.GetCellId()}') if picker.GetCellId() != -1: # 表示点击到了某个网格面 print(f'Pick position is: ({world_position[0]:.6g}, {world_position[1]:.6g}, {world_position[2]:.6g})') """ 在vtk中,不能直接讲一个索引id扔给过滤器进行提取,需要一个包含ID、类型和域的完整的选择指令 """ # 作用存放所有被选中的元素的 ID 列表 ids = vtkIdTypeArray() ids.SetNumberOfComponents(1) ids.InsertNextValue(picker.GetCellId()) # 作用:定义这次选择的"规则"和"内容",这是对ID列表赋予意义的关键 selection_node = vtkSelectionNode() selection_node.SetFieldType(vtkSelectionNode.CELL) # 告诉VTK,选择的是几何单元(CELL) selection_node.SetContentType(vtkSelectionNode.INDICES) # 告诉VTK,选择方法是基于索引,而不是位置或者是范围 selection_node.SetSelectionList(ids) # 将前面准备好的 ID 数组 ids 放入这个节点 # 作用:定义选择的对象,顶级容器,用来存储一个或多个选择节点 # 可能希望同时选择“单元 54”和“点 10”,这时候就需要多个选择节点 selection = vtkSelection() """ vtkSelection 它代表着 “被选中的一组元素(点、单元、块、节点等)”,是执行选中、高亮、提取等操作的基础数据结构 """ selection.AddNode(selection_node) """ vtkExtractSelection vtkSelection 和 vtkExtractSelection 这两个类经常是配合使用的, 它们一起构成了 VTK 中实现“选择与提取(Selection & Extraction)”的核心机制 根据 selection 中定义的 ID,从 self.data 中剪切出对应的几何体,并生成一个新的、更小的数据集 """ extract_selection = vtkExtractSelection() """ 端口0,输入原始数据 端口1,vtkSelection对象 """ extract_selection.SetInputData(0, self.data) extract_selection.SetInputData(1, selection) extract_selection.Update() """ 将过滤器产生的临时输出数据,复制到一个持久的、可供渲染器使用的对象中 """ selected = vtkUnstructuredGrid() selected.ShallowCopy(extract_selection.GetOutput()) print(f'Number of points in the selection: {selected.GetNumberOfPoints()}') print(f'Number of cells in the selection : {selected.GetNumberOfCells()}') self.selected_mapper.SetInputData(selected) self.selected_actor.SetMapper(self.selected_mapper) self.selected_actor.GetProperty().EdgeVisibilityOn() self.selected_actor.GetProperty().SetColor(colors.GetColor3d('Tomato')) self.selected_actor.GetProperty().SetLineWidth(3) self.GetInteractor().GetRenderWindow().GetRenderers().GetFirstRenderer().AddActor(self.selected_actor) """ 作用是恢复基类的默认交互行为 背景:MouseInteractorStyle 继承自 vtkInteractorStyleTrackballCamera 鼠标左键在基类中负责旋转相机,在子类中负责单元格的选取 当点集鼠标左键时,会有限执行自定义的逻辑,再执行self.OnLeftButtonDown(),这里是调用了父类的OnLeftButtonDown()方法 保证了执行完自定义的拾取操作之后,程序仍然能够执行基类的默认操作——即启动3D场景的旋转 """ self.OnLeftButtonDown() def main(): colors = vtkNamedColors() plane_source = vtkPlaneSource() plane_source.SetResolution(10, 10) plane_source.Update() triangle_filter = vtkTriangleFilter() triangle_filter.SetInputConnection(plane_source.GetOutputPort()) triangle_filter.Update() mapper = vtkPolyDataMapper() mapper.SetInputConnection(triangle_filter.GetOutputPort()) actor = vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetColor(colors.GetColor3d("SeaGreen")) actor.GetProperty().EdgeVisibilityOn() renderer = vtkRenderer() ren_win = vtkRenderWindow() ren_win.AddRenderer(renderer) ren_win.SetWindowName('CellPicking') iren = vtkRenderWindowInteractor() iren.SetRenderWindow(ren_win) renderer.AddActor(actor) renderer.SetBackground(colors.GetColor3d('PaleTurquoise')) style = MouseInteractorStyle(triangle_filter.GetOutput()) style.SetDefaultRenderer(renderer) iren.SetInteractorStyle(style) ren_win.Render() iren.Initialize() iren.Start() if __name__ == '__main__': main()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/20 18:54:01

Steam创意工坊下载终极指南:免客户端轻松获取模组资源

Steam创意工坊下载终极指南:免客户端轻松获取模组资源 【免费下载链接】WorkshopDL WorkshopDL - The Best Steam Workshop Downloader 项目地址: https://gitcode.com/gh_mirrors/wo/WorkshopDL 还在为无法使用Steam创意工坊模组而烦恼吗?Worksh…

作者头像 李华
网站建设 2026/4/23 8:17:16

揭秘静态反射元数据提取全过程:3步实现零成本运行时洞察

第一章:静态反射元数据获取在现代编程语言中,静态反射是一种在编译期或运行期获取类型结构信息的机制。与动态反射不同,静态反射通过预定义的元数据描述类型,避免了运行时性能损耗,同时支持更安全的类型操作。元数据的…

作者头像 李华
网站建设 2026/4/10 13:47:11

是否支持多语言?GLM-4.6V-Flash-WEB功能实测指南

是否支持多语言?GLM-4.6V-Flash-WEB功能实测指南 智谱最新开源,视觉大模型。 1. 引言:为何关注GLM-4.6V-Flash-WEB的多语言能力? 随着多模态大模型在图像理解、图文生成等场景中的广泛应用,跨语言理解能力已成为衡量模…

作者头像 李华
网站建设 2026/4/13 0:02:49

骨骼检测模型部署秘籍:避开环境坑,云端1小时搞定

骨骼检测模型部署秘籍:避开环境坑,云端1小时搞定 引言 作为一名全栈开发者,你是否遇到过这样的困境:客户急需一个骨骼检测API演示,但自己从零开始配置Docker镜像时,总是遇到各种环境依赖问题,…

作者头像 李华
网站建设 2026/4/20 20:37:05

10分钟用Node.js搭建博客原型:从安装到上线

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 生成一个简易博客系统原型,要求:1. 基于Node.js和Express 2. 支持Markdown文章发布 3. 包含用户评论功能 4. 响应式前端界面 5. 使用SQLite存储数据 6. 一键…

作者头像 李华
网站建设 2026/4/10 17:19:26

一键启动Qwen2.5-0.5B-Instruct:网页推理零配置部署指南

一键启动Qwen2.5-0.5B-Instruct:网页推理零配置部署指南 在大模型快速落地的今天,越来越多开发者希望以最低门槛体验前沿语言模型的能力。然而,复杂的环境配置、显存管理与服务搭建常常成为第一道障碍。针对这一痛点,Qwen2.5-0.5…

作者头像 李华