vtkDiscreteMarchingCubes Output Transform compared to original vtkOrientedImageData

Dear forum,
I’m trying to create a vtkPolyData out of a vtkOrientedImageData. Given a segmentation ‘Segmentation’ with a segment with id ‘Segment_1’, the output model is strangely transformed to the input oriented imagedata:

seg = slicer.util.getNode('Segmentation')
segID = 'Segment_1'

import vtkSegmentationCorePython
modelsLogic = slicer.modules.models.logic()

orImg = vtkSegmentationCorePython.vtkOrientedImageData()
slicer.vtkSlicerSegmentationsModuleLogic.GetSegmentBinaryLabelmapRepresentation(seg, segID, orImg)

inputDiscreteCubes = vtk.vtkDiscreteMarchingCubes()
inputDiscreteCubes.SetInputData(orImg)
inputDiscreteCubes.GenerateValues(1,0,0)
inputDiscreteCubes.Update()

modelNode = modelsLogic.AddModel(inputDiscreteCubes.GetOutput())

It is possible to solve this by first removing the ImageToWorld transform of the oriented imagedata and applying this transform to the model later again:

seg = slicer.util.getNode('Segmentation')
segID = 'Segment_1'

import vtkSegmentationCorePython
modelsLogic = slicer.modules.models.logic()

orImg = vtkSegmentationCorePython.vtkOrientedImageData()
slicer.vtkSlicerSegmentationsModuleLogic.GetSegmentBinaryLabelmapRepresentation(seg, segID, orImg)

mat = vtk.vtkMatrix4x4()
orImg.GetImageToWorldMatrix(mat)
orImg.SetImageToWorldMatrix(vtk.vtkMatrix4x4())

inputDiscreteCubes = vtk.vtkDiscreteMarchingCubes()
inputDiscreteCubes.SetInputData(orImg)
inputDiscreteCubes.GenerateValues(1,0,0)
inputDiscreteCubes.Update()

transform = vtk.vtkTransform()
transform.SetMatrix(mat)
transformFilter=vtk.vtkTransformPolyDataFilter()
transformFilter.SetTransform(transform)
transformFilter.SetInputData(inputDiscreteCubes.GetOutput())
transformFilter.Update()

modelNode = modelsLogic.AddModel(transformFilter.GetOutput())

But why does it not work in the first place without removing and reapplying the transform? And which transform has to be applied to the resulting model without removing the matrix from the oriented imagedata?

Thank you, Sebastian

Instead of reimplementing existing features, I would strongly suggest using what is already available and tested:

I fully agree, just use these existing methods for that. You can find examples in the script repository.

We plan to retire vtkOrientedImageData in Slicer5 by this fall (as image orientation is now part of vtkImageData) and that will make things much simpler.