# AlignTwoPolyDatas Example

**URL:** <https://discourse.vtk.org/t/aligntwopolydatas-example/1103>\
**Category:** Development\
**Created:** [June 8, 2019, 9:29pm UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103 "2019-06-08T21:29:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)\
**Post date:** [June 8, 2019, 9:29pm UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/1 "2019-06-08T21:29:03Z")

</div>

Folks,

Recently, a user wanted to align two models from a longitudinal study. He did not have access to the original image data. If he had the images, he could have used [3DSlicer](https://www.slicer.org/) or [Elastix](http://elastix.isi.uu.nl/). They both have state-of-the-art intensity driven registration algorithms.

VTK has several “registration” techniques: [vtkLandmarkTransform](https://vtk.org/doc/nightly/html/classvtkLandmarkTransform.html#details),[vtkIterativeClosestPointTransform](https://vtk.org/doc/nightly/html/classvtkIterativeClosestPointTransform.html#details), [vtkThinPlateSplineTransform](https://vtk.org/doc/nightly/html/classvtkThinPlateSplineTransform.html#details).  
Each of these algorithms requires landmarks to exist in both the source and target models.

I created an example, [AlignTwoPolyDatas](https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/AlignTwoPolyDatas/) that uses a [vtkOBBTree](https://vtk.org/doc/nightly/html/classvtkOBBTree.html#details) to create oriented bounding boxes for each model. The example uses the corners of the bounding boxes as landmarks for the landmark transform. Then, that transform is refined with the iterative closest point transform. For the original, oriented bounding boxes and iterative closest point transform, the example computes a metric using the [vtkHausdorffDistancePointSetFilter](https://vtk.org/doc/nightly/html/classvtkHausdorffDistancePointSetFilter.html#details). The example picks the best of the three approaches and displays the aligned models. [AlignTwoPolyDatas](https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/AlignTwoPolyDatas/)’ description provides more details.

Here are the results for the user’s time sequence. The technique can also align “similar” objects.

Here is the user’s data: ![arteries](https://discourse.vtk.org/uploads/default/original/1X/ce57ddf4dff0bafa10991d93c536fffc8b065c23.png)  
and a shark and the great white shark: ![sharks](https://discourse.vtk.org/uploads/default/original/1X/6b9e0766a6075b0e799f6aa21b3d690291c1275b.png)  
and finally a cow and a horse: ![cowhorse](https://discourse.vtk.org/uploads/default/original/1X/73b8c66293830250a7a568ba3f201aa96c6a32cb.png).

Enjoy the [VTKExamples](https://lorensen.github.io/VTKExamples/site/),

Bill

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)\
**Post date:** [June 9, 2019, 7:59am UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/2 "2019-06-09T07:59:22Z")

</div>

The last link **AlignTwoPolyDatas** is referring to: [vtkOBBTree](https://vtk.org/doc/nightly/html/classvtkOBBTree.html#details)

---

<div class="post-metadata">

**Author:** ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)\
**Post date:** [June 9, 2019, 11:09pm UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/3 "2019-06-09T23:09:46Z")

</div>

Fixed, Thanks

---

<div class="post-metadata">

**Author:** ![Patrick\_Laurin](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@Patrick\_Laurin](https://discourse.vtk.org/u/Patrick_Laurin)\
**Post date:** [June 17, 2019, 6:32pm UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/4 "2019-06-17T18:32:45Z")

</div>

Perhaps [https://vtk.org/doc/nightly/html/classvtkProcrustesAlignmentFilter.html](https://vtk.org/doc/nightly/html/classvtkProcrustesAlignmentFilter.html) for “similar” objects?

---

<div class="post-metadata">

**Author:** ![Kaushik\_Tiwari](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kaushik_tiwari/32/3318_2.png) [@Kaushik\_Tiwari](https://discourse.vtk.org/u/Kaushik_Tiwari)\
**Post date:** [May 10, 2021, 10:46am UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/5 "2021-05-10T10:46:04Z")

</div>

Since the orientations of the bounding boxes may differ, **the AlignBoundingBoxes function tries ten different rotations.** For each rotation, it computes the Hausdorff distance between the target’s OBB corners and the transformed source’s OBB corners. Finally, transform the original source using the smallest distance.

_ **How to apply more than 10 rotation** _???

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)\
**Post date:** [May 11, 2021, 10:44pm UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/6 "2021-05-11T22:44:44Z")

</div>

In the function `BestBoundingBox` change:

```auto
  auto delta = 90.0;
  for (auto i = 0; i < 4; ++i)
  {
    ...

```

to suit what you want to do.

Remember also that you may need to reorient the target to get a better fit.

---

<div class="post-metadata">

**Author:** ![Kaushik\_Tiwari](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kaushik_tiwari/32/3318_2.png) [@Kaushik\_Tiwari](https://discourse.vtk.org/u/Kaushik_Tiwari)\
**Post date:** [June 24, 2021, 6:20am UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/7 "2021-06-24T06:20:47Z")

</div>

How to reorient the target?? sorry I am a beginner .

---

<div class="post-metadata">

**Author:** ![Kaushik\_Tiwari](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kaushik_tiwari/32/3318_2.png) [@Kaushik\_Tiwari](https://discourse.vtk.org/u/Kaushik_Tiwari)\
**Post date:** [June 24, 2021, 8:58am UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/8 "2021-06-24T08:58:10Z")

</div>

sir, can you send me your e-mail. That would be great help for me.

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)\
**Post date:** [June 26, 2021, 6:22am UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/9 "2021-06-26T06:22:41Z")

</div>

You will probably need to transform your target object so that its orientation is similar to the reference object as shown in the images above. Generally this is done using vtkTransform and vtkTransformPolyDataFilter or something similar. To get a feel for what happens, you could open Paraview load a cone source and apply the transform filter.  
Here are several examples that also may be of use:

- [TransformPolyData](https://kitware.github.io/vtk-examples/site/Cxx/Filtering/TransformPolyData)
- [OrientedArrow](https://kitware.github.io/vtk-examples/site/Cxx/GeometricObjects/OrientedArrow)
- [TransformFilter](https://kitware.github.io/vtk-examples/site/Cxx/PolyData/TransformFilter)
- [ExternalContour](https://kitware.github.io/vtk-examples/site/Cxx/PolyData/ExternalContour)

---

<div class="post-metadata">

**Author:** ![Edwin\_de\_Jager](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edwin_de_jager/32/5614_2.png) [@Edwin\_de\_Jager](https://discourse.vtk.org/u/Edwin_de_Jager)\
**Post date:** [August 7, 2022, 9:24am UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/10 "2022-08-07T09:24:32Z")

</div>

Hi there,

I am a bit new to all of this but keen to learn. I was wondering if someone can advise me on how to save the aligned surface as a vtk from this example? I prefer using the Python version. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)\
**Post date:** [August 8, 2022, 1:37am UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/11 "2022-08-08T01:37:59Z")

</div>

You can do something like this:

```nohighlight
    writer = vtkPolyDataWriter()
    if best_distance == distance_before_align:
        writer.SetInputData(original_source_polydata)
    elif best_distance == distance_after_align:
        writer.SetInputData(source_polydata)
    else:
        writer.SetInputData(transform.GetOutput())
    writer.SetFileName('AlignedSource.vtk')
    writer.Write()
    writer.SetInputData(tpd.GetOutput())
    writer.SetFileName('Target.vtk')
    writer.Write()

```

This will give you the aligned source and target as VTK files that you could load into ParaView.

```auto

```

---

<div class="post-metadata">

**Author:** ![Edwin\_de\_Jager](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edwin_de_jager/32/5614_2.png) [@Edwin\_de\_Jager](https://discourse.vtk.org/u/Edwin_de_Jager)\
**Post date:** [August 8, 2022, 11:25am UTC](https://discourse.vtk.org/t/aligntwopolydatas-example/1103/12 "2022-08-08T11:25:07Z")

</div>

Thank you!
