news 2026/5/9 9:41:12

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/7 17:14:32

Java字节流:数据传输的底层万能通道

Java字节流是IO体系中处理数据的“基石”,以8位byte为最小传输单位,能无差别读写所有类型数据(文本、图片、视频等),是程序与磁盘、网络等外部设备交互的底层桥梁。无论是文件存储、网络通信还是二进制数据处理&#x…

作者头像 李华
网站建设 2026/5/9 9:27:36

基因研究到药物研发,亚马逊云科技生成式AI如何加速癌症终结?

亚马逊云科技技术通过加速基因分析、改进早期检测、加速药物研发以及扩大医疗服务覆盖范围,为全球医疗服务提供者对抗癌症提供了支持。每年2月4日的世界癌症日提醒着我们,全球数以百万计的人受到了癌症的影响。在亚马逊云科技,我们正与研究人…

作者头像 李华
网站建设 2026/5/6 2:09:52

vue基于Spring Boot的心理咨询预约管理系统应用和研究_32h35596

目录具体实现截图项目介绍论文大纲核心代码部分展示项目运行指导结论源码获取详细视频演示 :文章底部获取博主联系方式!同行可合作具体实现截图 本系统(程序源码数据库调试部署讲解)同时还支持java、ThinkPHP、Node.js、Spring B…

作者头像 李华
网站建设 2026/5/3 13:48:03

亚马逊云科技如何为生命科学行业提供更强大的AI支持?

随着DeepSeek-R1推理模型的广泛应用,其强大的推理能力逐渐显现。与人类的思维方式类似,大语言模型分为“慢思考”和“快思考”两种模式。慢思考适用于复杂的多步推理任务,如撰写文章、制定研究计划等;而快思考则用于一步完成的简单…

作者头像 李华
网站建设 2026/5/8 0:26:15

vue基于Spring Boot的现代化动物园观光游览系统的应用和研究_716v8zj0

目录具体实现截图项目介绍论文大纲核心代码部分展示项目运行指导结论源码获取详细视频演示 :文章底部获取博主联系方式!同行可合作具体实现截图 本系统(程序源码数据库调试部署讲解)同时还支持java、ThinkPHP、Node.js、Spring B…

作者头像 李华
网站建设 2026/5/9 9:33:33

27岁从传统行业裸辞转网络安全,我是如何实现弯道超车的

27 岁从传统行业裸辞转网络安全,我是如何做到的? 27 岁女生从传统行业裸辞转网络安全,3 个月拿到大厂 offer:这行真的没你想的那么难 后台经常收到私信,问我一个做了 4 年传统行业(之前是线下品牌运营&am…

作者头像 李华