VTK SetInputConnection Vs SetInputData

SetInputConnection is for a filter to connect to another filter.

bFilter.SetInputConnection(aFilter.GetOutputPort())

SetInputData is for a filter to connect to a dataset

bFilter.SetInputData(aFilter.GetOutput())

The difference in behavior is that SetInputConnection setup the auto pipeline update mechanism, a render or update() down in the pipeline will trigger all preceding filters to update

reader.SetFileName('name.stl')

aFilter.SetInputConnection(reader.GetOutputPort())
// aFilter do some setting

bFilter.SetInputConnection(aFilter.GetOutputPort())
// bFilter do some setting

bFilter.Update()

SetInputData however need manual update() in each stage for the whole pipeline to work.

reader.SetFileName('name.stl')
reader.Update()

aFilter.SetInputData(reader.GetOutput())
// aFilter do some setting
aFilter.Update()

bFilter.SetInputData(aFilter.GetOutput())
// bFilter do some setting

bFilter.Update()

If any update() misses, you will get empty result error.

### 如何使用 VTK 库将 Actors 保存为 vtk 格式文件 在 VTK 中,可以利用 `vtkPolyDataWriter` 或者其他特定类型的写入器来保存几何数据。对于 Actor 的保存,通常先获取其映射的数据集(通常是 PolyData),再通过合适的写入器将其导出。 下面展示一段 Python 代码示例,该代码展示了如何创建一个简单的圆柱体 actor 并将其对应的 polydata 导出至 .vtk 文件: ```python import vtk def create_cylinder_actor(): # 创建圆柱源 cylinder_source = vtk.vtkCylinderSource() cylinder_source.SetResolution(100) # 映射器设置 mapper = vtk.vtkPolyDataMapper() if vtk.VTK_MAJOR_VERSION <= 5: mapper.SetInput(cylinder_source.GetOutput()) else: mapper.SetInputConnection(cylinder_source.GetOutputPort()) # 设置actor属性 actor = vtk.vtkActor() actor.SetMapper(mapper) return actor, mapper def save_poly_data_to_vtk_file(poly_data, file_name="output.vtk"): writer = vtk.vtkPolyDataWriter() writer.SetFileName(file_name) if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(poly_data) else: writer.SetInputData(poly_data) writer.Write() if __name__ == "__main__": actor, mapper = create_cylinder_actor() # 获取polydata并保存 poly_data = mapper.GetInput() save_poly_data_to_vtk_file(poly_data=poly_data) ``` 上述脚本首先定义了一个辅助函数 `create_cylinder_actor()` 来构建一个带有适当配置的圆柱形 Actor 和 Mapper 对象[^1]。接着定义了另一个名为 `save_poly_data_to_vtk_file()` 函数负责接收 PolyData 实例以及目标文件路径作为参数,并调用 `vtkPolyDataWriter` 将这些数据写出到指定位置[^2]。 最后,在主程序部分实例化所需的组件并将它们关联起来;随后提取由 Mapper 处理过的输入数据即原始几何信息,并传递给之前提到的帮助函数完成实际存储操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值