# composite transform convertion problem

**URL:** https://discourse.itk.org/t/composite-transform-convertion-problem/3749
**Category:** Engineering
**Created:** [January 8, 2021, 4:39pm UTC](https://discourse.itk.org/t/composite-transform-convertion-problem/3749 "2021-01-08T16:39:20Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [January 8, 2021, 4:39pm UTC](https://discourse.itk.org/t/composite-transform-convertion-problem/3749/1 "2021-01-08T16:39:20Z")

</div>

Hello all,  
I was wondering if we can convert a `CompositeTransformType` to a `TransformType`?  
Here is my sample of code:

`using TransformType = itk::Euler3DTransform< double >; TransformType::Pointer first_transformation = TransformType::New(); TransformType::Pointer second_transformation = TransformType::New(); CompositeTransformType::Pointer composite_transformation = CompositeTransformType::New(); composite_transformation->AddTransform( first_transformation ); composite_transformation->AddTransform( second_transformation ); TransformType::Pointer total_transformation = TransformType::New();`

and I want to compute the following procedure:

` total_transformation = composite_transformation;`  
or maybe  
`total_transformation->SetTransform( composite_transformation )`

Is there any mean to convert a `CompositeTransformType` to a `TransformType`?

Thank you for your patience…

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [January 8, 2021, 5:35pm UTC](https://discourse.itk.org/t/composite-transform-convertion-problem/3749/2 "2021-01-08T17:35:03Z")

</div>

By looking at the documentation of [CompositeTransform](https://itk.org/Doxygen/html/classitk_1_1CompositeTransform.html), it cannot “flatten” the transform queue. You will have to compose the transforms yourself, by multiplying matrices or something similar. Perhaps [VersorRigid3DTransform](https://itk.org/Doxygen/html/classitk_1_1VersorRigid3DTransform.html) is more convenient for that?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [January 8, 2021, 5:35pm UTC](https://discourse.itk.org/t/composite-transform-convertion-problem/3749/3 "2021-01-08T17:35:45Z")

</div>

Perhaps @hjmjohnson, @blowekamp or @zivy can provide more useful advice?

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [January 8, 2021, 7:27pm UTC](https://discourse.itk.org/t/composite-transform-convertion-problem/3749/4 "2021-01-08T19:27:23Z")

</div>

Hello @pierrectl,

As @dzenanz pointed out, unfortunately there is no straightforward way to “flatten” a composite transform into a single basic transformation (rigid/affine/…/BSpline/DisplacementField).

This is primarily because the CompositeTransform can contain any of these transformation types some of which have a global domain (rigid/affine…) and some a local domain (BSpline/DisplacementField).  
If you have a combination of transformations with global and local domains there is no way to flatten them into a single basic transformation type.

If all of your transformations have a global domain then you could convert them all into affine transformations and combine those, retaining the global nature. This will require you to access the internal data (matrix, center, translation) and manually do the computations.

If all of your transformations have a local domain, you could (a) identify the union of all those domains. (b) create a DisplacementFieldTransform with that domain from the CompositeTransformation (see [TransformToDisplacementFieldFilter](https://itk.org/Doxygen/html/classitk_1_1TransformToDisplacementFieldFilter.html)).

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [January 8, 2021, 8:18pm UTC](https://discourse.itk.org/t/composite-transform-convertion-problem/3749/5 "2021-01-08T20:18:41Z")

</div>

@pierrectl is trying to compose two `Euler3DTransform`s, if I understood correctly. Those are two transforms with global domain. Computing internal data (matrix, center, translation) is probably easier with versor than affine.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [January 8, 2021, 8:45pm UTC](https://discourse.itk.org/t/composite-transform-convertion-problem/3749/6 "2021-01-08T20:45:26Z")

</div>

Hi @dzenanz,

Affine is the most general case and it’s straightforward, illustrated in the section titled **CompositeTransform** in [this SimpleITK Jupyter notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/22_Transforms.ipynb), flattening a rigid and affine transformations into a single affine. Different syntax from ITK C++, but similar semantics.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [January 9, 2021, 7:04pm UTC](https://discourse.itk.org/t/composite-transform-convertion-problem/3749/7 "2021-01-09T19:04:01Z")

</div>

We implemented flattening of composite transforms and computing displacement field from an arbitrary complex transform hierarchy in 3D Slicer and everything works very nicely.

By “flattening” of composite transforms we mean creating a list of transforms from an arbitrary composite transform hierarchy (a composite transform can contain other composite transforms) and it is fairly simple, see implementation [here](https://github.com/Slicer/Slicer/blob/bbc4e03eb100682195eb1a2a9fcc6b4dbb987f8d/Libs/MRML/Core/vtkMRMLTransformNode.cxx#L115-L155).

Computing a displacement field is straightforward, too, we just require the user to specify an image grid (origin, spacing, and axis directions) and we fill it with displacement that is computed by applying all the transforms in the composite transform. See implementation [here](https://github.com/Slicer/Slicer/blob/bbc4e03eb100682195eb1a2a9fcc6b4dbb987f8d/Modules/Loadable/Transforms/Logic/vtkSlicerTransformLogic.cxx#L678-L725). All we needed for this is to define the transformation everywhere not just inside the domain (with a smooth transition at the domain boundary to ensure that the transform remains invertible, if possible).

---

<div class="post-metadata">

### Author: ![pierrectl](https://discourse.itk.org/user_avatar/discourse.itk.org/pierrectl/32/1420_2.png) [@pierrectl](https://discourse.itk.org/u/pierrectl)
#### Post date: [January 11, 2021, 3:24pm UTC](https://discourse.itk.org/t/composite-transform-convertion-problem/3749/8 "2021-01-11T15:24:26Z")

</div>

Thanks to all of you.  
@zivy I work with global transformations: I did the way you tell me, by accessing center and translation vectors and rotation matrix of each transformation and compute the composite one “by hand”.
