# Iterative Closest Points for rigid alignment

The goal in this tutorial is to derive an implementation of the classical Iterative Closest Points (ICP) algorithm in the context of rigid alignment of shapes.

##### Related resources

The following resources from our online course may provide some helpful context for this tutorial:

##### Preparation

As in the previous tutorials, we start by importing some commonly used objects and initializing the system.

## Automatic rigid alignment

We start by loading and visualizing two meshes

As you can see here, the meshes are not aligned. As in previous tutorials, we could identify corresponding points
to align the meshes. The downside is, that this requires some manual intervention.
In this tutorial we will instead use the Iterative Closest Point (ICP) method to perform this rigid alignment step **automatically**.

### Candidate correspondences

We have seen before that finding the best rigid transformation when given correct correspondences has a closed-form solution. The problem we are facing here is that we do not have these correspondences. The idea of the ICP algorithm is, that we can approximate the correspondences, by simply assuming that the corresponding point is always the closest point on the mesh.

Let's select a few points from the mesh.

The exact number of points is not important. It is only important that we select points, which are approximately uniformly distributed over the surface.

In the next step, we find the corresponding points in the other mesh:

Note that we used here not `mesh1`

directly, but passed the mesh from which we find the closest points as an argument,
which we called the `MovingMesh`

. The reason is, that this will later be iteratively transformed to come closer to our target mesh `mesh2`

.

Let us now visualize the the chosen correspondences:

As expected, the obtained correspondences are clearly not good, as they tend to focus on only one side of the target face. Nevertheless, we can apply Procrustes analysis based on these correspondences and retrieve a rigid transformation, which brings us closer to the target.

**Well, no surprise here.** Given the poor quality of the candidate correspondences, we obtained a poor rigid alignment.
This said, when considering where we started from, that is the original position, we did get closer to the target.

The second important idea of the ICP algorithm comes is now to **iterate** this steps in the hope that it will converge.
Let's try it out:

As you can see, the candidate correspondences are still clearly wrong, but start to be more spread around the target face. Also the resulting rigid transformation seems to bring our mesh a bit closer to the target.

Finally, we change our implementation such that we can perform an arbitrary number of iterations:

Let's now run it with 150 iterations:

As you can see here, the quality of the candidate correspondences did indeed result in a proper
**automatic** rigid alignment of Paola to the target. One should not forget, however, that the ICP method is
very sensitive to the initial position, and might easily get stuck in a local minimum.