diff --git a/src/Python.md b/src/Python.md index 70934953d6831354f1a0c3f2c34be80f0555e410..6effb5fa39a523083182f901d05103ac025da2b6 100644 --- a/src/Python.md +++ b/src/Python.md @@ -42,6 +42,7 @@ If you are new to VTK then these [tutorials](#tutorial) will help to get you sta | -------------- | ------------- | ------- | [CSVReadEdit](/Python/IO/CSVReadEdit) | Read and edit a CSV file using pandas and numpy. [CSVReadEdit1](/Python/IO/CSVReadEdit1) | Read and edit a CSV file using pandas and vtkDelimitedTextReader. +[PolyDataToImageDataConverter](/Python/IO/PolyDataToImageDataConverter) | Converts PolyData to binary LabelImage. [ReadExodusData](/Python/IO/ReadExodusData) | A simple script for reading and viewing ExodusII data interactively. [ReadPLOT3D](/Python/IO/ReadPLOT3D) | Read CFD (computational fluid dynamics) data produced by PLOT3D. [ReadSLC](/Python/IO/ReadSLC) | Read an SLC file. diff --git a/src/Python/IO/PolyDataToImageDataConverter.py b/src/Python/IO/PolyDataToImageDataConverter.py new file mode 100644 index 0000000000000000000000000000000000000000..7df7cb5f2d6602e3eda4243790c6e7aadc725c2c --- /dev/null +++ b/src/Python/IO/PolyDataToImageDataConverter.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +from vtkmodules.vtkCommonDataModel import vtkImageData +from vtkmodules.vtkIOGeometry import vtkSTLReader +from vtkmodules.vtkIOImage import vtkMetaImageWriter +from vtkmodules.vtkImagingStencil import ( + vtkImageStencil, + vtkPolyDataToImageStencil +) + + +def get_program_parameters(): + import argparse + parser = argparse.ArgumentParser(description='Converts the polydata to imagedata.') + parser.add_argument('filename', help='A filename e.g. headMesh.stl') + args = parser.parse_args() + return args.filename + + +def main(): + mesh_filename = get_program_parameters() + + reader = vtkSTLReader() + reader.SetFileName(mesh_filename) + reader.Update() + mesh = reader.GetOutput() + bounds = mesh.GetBounds() + + spacing1 = 0.1 + pixel_padding = 5 + origin_shift = pixel_padding * spacing1 + spacing = [spacing1, spacing1, spacing1] + origin = [bounds[0] - origin_shift, bounds[2] - origin_shift, bounds[4] - origin_shift] + extent = [0, int((bounds[1] - bounds[0]) / spacing1) + 2 * pixel_padding, 0, + int((bounds[3] - bounds[2]) / spacing1) + 2 * pixel_padding, 0, + int((bounds[5] - bounds[4]) / spacing1) + 2 * pixel_padding] + + blank_image = vtkImageData() + blank_image.SetExtent(extent) + blank_image.AllocateScalars(3, 1) # VTK_UNSIGNED_CHAR, 1 component + blank_image.GetPointData().GetScalars().Fill(0) + blank_image.SetSpacing(spacing) + blank_image.SetOrigin(origin) + + dataToStencil = vtkPolyDataToImageStencil() + dataToStencil.SetInputData(mesh) + dataToStencil.SetOutputSpacing(spacing) + dataToStencil.SetOutputOrigin(origin) + + stencil = vtkImageStencil() + stencil.SetInputData(blank_image) + stencil.SetStencilConnection(dataToStencil.GetOutputPort()) + stencil.ReverseStencilOn() + stencil.SetBackgroundValue(255) + stencil.Update() + mask = stencil.GetOutput() + + writer = vtkMetaImageWriter() + writer.SetFileName("output.mha") + writer.SetInputData(mask) + writer.Write() + + +if __name__ == '__main__': + main()