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

AppendFilter使用AppendFilter合并两个不同的数据并展示

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①vtkUnstructuredGrid无法显示点的原因,②使用appendfilter合并不同数据


二:代码及注释

#!/usr/bin/env python # noinspection PyUnresolvedReferences import vtkmodules.vtkInteractionStyle # noinspection PyUnresolvedReferences import vtkmodules.vtkRenderingOpenGL2 from vtkmodules.vtkCommonColor import vtkNamedColors from vtkmodules.vtkCommonCore import vtkPoints from vtkmodules.vtkCommonDataModel import ( vtkPolyData, vtkUnstructuredGrid,vtkVertex ) from vtkmodules.vtkFiltersCore import vtkAppendFilter from vtkmodules.vtkFiltersSources import ( vtkPointSource, vtkSphereSource ) from vtkmodules.vtkRenderingCore import ( vtkActor, vtkDataSetMapper, vtkGlyph3DMapper, vtkRenderWindow, vtkRenderWindowInteractor, vtkRenderer ) def main(): colors = vtkNamedColors() # Create 5 points (vtkPolyData) """ vtkPointSource 快速生成一个包含随机点的 vtkPolyData 对象,以方便测试和演示 会自动在默认的坐标范围内(一个以原点为中心,半径为 0.5 的球体内部)随机生成点 """ pointSource = vtkPointSource() pointSource.SetNumberOfPoints(5) pointSource.Update() polydata = pointSource.GetOutput() print('There are', polydata.GetNumberOfPoints(), 'points in the polydata.') # Create 2 points in a vtkUnstructuredGrid points = vtkPoints() points.InsertNextPoint(0, 0, 0) points.InsertNextPoint(0, 0, 1) ug = vtkUnstructuredGrid() ug.SetPoints(points) print('There are', ug.GetNumberOfPoints(), 'points in the unstructured.') """ vtkDataSetMapper 默认会画出 几何单元 (cells),而不是裸的点 ug里面的点,并没有创建默认的vertex单元。所以当下面的 renderer.AddActor(sphereActor) 这一行代码被注释掉,就只能看到5个点 要想看看到完整的7个点,就要给ug中的各个点创建vertex单元 for i in range(points.GetNumberOfPoints()): vertex = vtkVertex() vertex.GetPointIds().SetId(0, i) ug.InsertNextCell(vertex.GetCellType(), vertex.GetPointIds()) """ # Combine the two data sets appendFilter = vtkAppendFilter() appendFilter.AddInputData(polydata) appendFilter.AddInputData(ug) appendFilter.Update() # combined = vtkUnstructuredGrid() combined = appendFilter.GetOutput() print('There are', combined.GetNumberOfPoints(), 'points combined.') # Create a mapper and actor mapper = vtkDataSetMapper() mapper.SetInputConnection(appendFilter.GetOutputPort()) actor = vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetPointSize(5) # Map the points to spheres sphereActor = point_to_glyph(appendFilter.GetOutput().GetPoints(), 0.05) sphereActor.GetProperty().SetColor(colors.GetColor3d("Gold")) # Create a renderer, render window, and interactor renderer = vtkRenderer() renderWindow = vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindowInteractor = vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) # Add the actor to the scene renderer.AddActor(actor) renderer.AddActor(sphereActor) renderer.SetBackground(colors.GetColor3d('RoyalBlue')) # Render and interact renderWindow.SetWindowName('AppendFilter') renderWindow.Render() renderWindowInteractor.Start() def point_to_glyph(points, scale): """ Convert points to glyphs. :param points: The points to glyph. :param scale: The scale, used to determine the size of the glyph representing the point, expressed as a fraction of the largest side of the bounding box surrounding the points. e.g. 0.05 :return: The actor. """ bounds = points.GetBounds() max_len = 0.0 for i in range(0, 3): max_len = max(bounds[i + 1] - bounds[i], max_len) sphere_source = vtkSphereSource() sphere_source.SetRadius(scale * max_len) pd = vtkPolyData() pd.SetPoints(points) mapper = vtkGlyph3DMapper() mapper.SetInputData(pd) mapper.SetSourceConnection(sphere_source.GetOutputPort()) mapper.ScalarVisibilityOff() mapper.ScalingOff() actor = vtkActor() actor.SetMapper(mapper) return actor if __name__ == '__main__': main()
http://www.cnnetsun.cn/news/61764.html

相关文章:

  • Happy LLM:Github爆火!手把手教你从0手搓个大模型!
  • SSM线上学习系统8e88w(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
  • 深度解析:MindsDB与ChromaDB向量数据库集成的高效实战指南
  • 32、深入了解Samba与Linux安全策略
  • 26、调试 Shell 程序的实用方法
  • Symbolic 英文单词学习
  • AI开发全流程工具链:从编码辅助到模型部署的实战指南
  • 英语综合练习题
  • 电力物联网系统能够发挥什么作用
  • 压气站SCADA数据采集远程监控系统方案
  • 12、高级渗透测试与中间人攻击技术详解
  • Vue3 生命周期全面解析:从创建到销毁的完整指南
  • 3个让我后悔的StyleGAN2数据集错误:从失败到成功的真实经历
  • 电商数据采集 API 接口:全流程采集与分析指南(附实战代码)
  • 7、Docker 镜像构建、注册与存储全解析
  • Python语法基础笔记(四)
  • 13、找回丢失文件的实用方法
  • 14、Linux 用户与用户组管理全解析
  • 30亿参数撬动87%成本下降:ERNIE 4.5 VL重塑多模态AI产业格局
  • PaperXie AI毕业论文写作功能深度实测:从选题到成稿,一个被低估的学术效率引擎如何重塑我的研究流程
  • torchtune终极部署指南:从微调到生产环境的完整链路
  • 科研认知减负革命:书匠策AI如何将文献“噪音”转化为创新“信号”
  • SSM 还是 Spring Boot?2025 年计算机毕设选题避坑指南与 50 个优质题目推荐
  • 27、Linux与UNIX系统管理操作指南
  • Nordic经过全球认证的、多传感器、电池供电的蜂窝物联网原型平台:Thingy91X套件
  • 基于springboot + vue学生选课信息管理系统
  • 【MWORKS使用技巧73】如何在Sysplorer中将仿真数据导入Syslab进行分析与绘图?
  • 2026毕设ssm+vue基于的住房保障系统论文+程序
  • Cursor
  • 为不同的wordpress页面调用不同的顶部菜单