# vtkDiscreteMarchingCubes Output Transform compared to original vtkOrientedImageData

**URL:** <https://discourse.slicer.org/t/vtkdiscretemarchingcubes-output-transform-compared-to-original-vtkorientedimagedata/7608>\
**Category:** Development\
**Tags:** segmentation, transforms, 3d-model\
**Created:** [July 16, 2019, 3:05pm UTC](https://discourse.slicer.org/t/vtkdiscretemarchingcubes-output-transform-compared-to-original-vtkorientedimagedata/7608 "2019-07-16T15:05:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@basti](https://discourse.slicer.org/u/basti)\
**Post date:** [July 16, 2019, 3:05pm UTC](https://discourse.slicer.org/t/vtkdiscretemarchingcubes-output-transform-compared-to-original-vtkorientedimagedata/7608/1 "2019-07-16T15:05:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 16, 2019, 4:05pm UTC](https://discourse.slicer.org/t/vtkdiscretemarchingcubes-output-transform-compared-to-original-vtkorientedimagedata/7608/2 "2019-07-16T16:05:27Z")

</div>

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

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/Logic/vtkSlicerSegmentationsModuleLogic.h#L209>

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/Logic/vtkSlicerSegmentationsModuleLogic.h#L149>

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 16, 2019, 4:17pm UTC](https://discourse.slicer.org/t/vtkdiscretemarchingcubes-output-transform-compared-to-original-vtkorientedimagedata/7608/3 "2019-07-16T16:17:17Z")

</div>

I fully agree, just use these existing methods for that. You can find examples in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Export_model_nodes_from_segmentation_node).

We plan to retire vtkOrientedImageData in Slicer5 by this fall (as image orientation is now part of [vtkImageData](https://vtk.org/doc/nightly/html/classvtkImageData.html#a14849687420465eedec8290c1d7f88a9)) and that will make things much simpler.
