news 2026/7/5 9:52:30

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/7/5 0:34:28

零基础跟我学做AI Agent(第1课:环境安装)

从今天开始,作者介绍一系列AI Agent(智能体)的开发或部署。需要的基础只是要对Python有个大概的了解,几乎是从0开始实践。涉及到的代码都会比较简单,一般不会超过50行。 一、Agent简介 AI Agent简称Agent,以…

作者头像 李华
网站建设 2026/7/6 3:35:49

2026年6大房产中介系统评测

在房产中介行业数字化转型加速的背景下,选择一款适配自身业务的管理系统成为提升运营效率、降低成本的关键。目前市场上的房产中介系统种类繁多,功能侧重点各有不同,让不少中介从业者难以抉择。本次评测聚焦6款主流房产中介系统,从…

作者头像 李华
网站建设 2026/7/4 6:52:23

12、Unix 文件管理全攻略

Unix 文件管理全攻略 1. 文件复制 在使用 cp 命令复制文件时,要特别注意路径名的准确性。如果使用 cp -R 时给出错误的路径名,可能会导致将目录树复制到自身,程序会一直运行直到文件系统被填满。 cp 命令复制文件时,新文件的所有权会变更为执行 cp 命令的用户。若…

作者头像 李华
网站建设 2026/7/6 2:46:12

13、在Unix系统中查找文件和信息的实用方法

在Unix系统中查找文件和信息的实用方法 在现代计算机的使用过程中,随着存储系统的不断增大,查找文件和信息成为了一项基本挑战。无论是文件和目录管理得井井有条、使用便于记忆的名称,还是存在大量随意命名的文件和目录,总会有需要根据文件内容、文件名或其他属性来查找文…

作者头像 李华
网站建设 2026/7/4 9:30:42

14、深入探索文件查找:从 `find` 到 Spotlight

深入探索文件查找:从 find 到 Spotlight 在操作系统中,高效地查找文件是一项重要的技能。本文将详细介绍 find 命令的使用方法,包括根据文件名、文件大小、权限和修改时间等条件进行查找,还会介绍 find 的好搭档 xargs 以及 Mac OS X 中的 Spotlight 功能。 1. …

作者头像 李华