Search code examples
pythonpixelvtk

voxelize VTU file in python


How can I voxilize the attached unstractured file (VTU) using ? Ideally I want to define the region (xmin=-9, xmax=9) and (ymin=-9, ymax=9) and the number of pixels (e.g (256,256) in each direction) and extract that region onto the pixel grid and store in file (with hollow regions filled).

VTUfile

here is how I read the file into polyData

import vtk

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

How can I convert the polydata now into the file with above details?

Thanks


Solution

  • Not sure if this helps, I had a similar problem using vtkplotter recently which might turn useful to you:

    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)
    

    enter image description here

    enter image description here