在 python 中体素化 VTU 文件

voxelize VTU file in python

我怎样才能对附件 unstractured file (VTU) using 进行 voxilize?理想情况下,我想定义区域 (xmin=-9, xmax=9)(ymin=-9, ymax=9) 以及像素数(例如每个方向的 (256,256) )并将该区域提取到像素网格上并存储在文件中(带有空心区域已满)。

VTUfile

这是我如何将文件读入 polyData

import vtk

# Read the source file.
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName("internal.vtu")
reader.Update()  
polydata = reader.GetOutput()

如何将 polydata now 转换为包含上述详细信息的文件?

谢谢

不确定这是否有帮助,我最近在使用 vtkplotter 时遇到了类似的问题,这可能对您有用:

from vtkplotter import *
import numpy as np

g = load('internal.vtu')
g.pointColors("p", cmap='terrain')

pts = g.points()
field = g.getPointArray('U')
ars = Arrows(pts-field/5, pts+field/5, c='k')

zpr = np.linspace(-15,15, num=25)
probes = np.c_[np.zeros_like(zpr)-10, np.zeros_like(zpr), zpr]

str_lns = streamLines(g, probes, activeVectors='U', maxPropagation=60, lw=2)
str_lns.pointColors(cmap='jet')
str_lns.GetProperty().LightingOff()
str_lns.mapper().SetResolveCoincidentTopologyToPolygonOffset()

show(g, ars, Points(probes), str_lns, elevation=90)

vol = interpolateToVolume(g, dims=(50,2,50), bounds=(-9,9,0,0,-9,9))
# vol.imagedata() # retrieves the vtkImageData obj
lego = vol.legosurface(cmap='terrain')

show(lego, newPlotter=True, elevation=90)

用纯 VTK 回答

您应该使用 vtkResampleToImage 过滤器。 https://vtk.org/doc/nightly/html/classvtkResampleToImage.html(文档是面向c++的,但是python中的方法是一样的)

请注意,reader 的输出是一个 vtkUnstructuredGrid 对象。多数据是另一种用于 3D 表面对象的 vtk 数据集。看这里了解更多:https://lorensen.github.io/VTKExamples/site/VTKFileFormats/#dataset-format

import vtk

reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName("/home/nicolas/Tmp/discourse/internal.vtu")
reader.Update()

toImage = vtk.vtkResampleToImage()
toImage.SetInputConnection(reader.GetOutputPort())
toImage.SetUseInputBounds(False)
toImage.SetSamplingDimensions(256,1,256)
toImage.SetSamplingBounds(-9, 9, -0.1, 0, -9, 9)
toImage.Update()

imageData = toImage.GetOutput()
imageData.GetPointData().SetActiveScalars("U")

# Visualization
mapper = vtk.vtkDataSetMapper()
mapper.SetInputConnection(toImage.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderWindow.Render()
renderWindowInteractor.Start()

# Write to file 
writer = vtk.vtkXMLImageDataWriter()
writer.SetFileName(filename)
writer.SetInputData(imageData)
writer.Write()