# From meshes to deformation fields

*In this tutorial, we show how the deformation fields that relate two meshes can be computed and visualized.*

##### Related resources

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

- Modelling Shape Deformations (Video)

##### Preparation

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

We will also load three meshes and visualize them in Scalismo-ui.

### Representing meshes as deformations

In the following we show how we can represent a mesh as a reference mesh plus a deformation field. This is possible because the meshes are all in correspondence; I.e. they all have the same number of points and points with the same id in the meshes represent the same point/region in the mesh.

Let's say *face_0*, is the reference mesh:

Now any mesh, which is in correspondence with this reference, can be represented as a deformation field. The deformation field is defined on this reference mesh; I.e. the points of the reference mesh are the domain on which the deformation field is defined.

The deformations can be computed by taking the difference between the corresponding point of the mesh and the reference:

From these deformations, we can then create a `DiscreteVectorField`

:

Similar to discrete scalar images, a Discrete Vector Field is defined
over a discrete domain. In contrast to images, the domain does not need to be
structured (a grid for example) and can be any arbitrary finite set of points. In the above example code, we defined the domain to be the reference mesh points, which
is of type `UnstructuredPointsDomain[_3D]`

, as we can easily check:

As for images, the deformation vector associated with a particular point id in a *DiscreteVectorField* can be retrieved via its point id:

We can visualize this deformation field in Scalismo-ui using the usual `show`

command:

We can see that the deformation vectors indeed point from the reference to *face_1*.
To see the effect better we need to remove *face2* from the ui,
make the reference transparent

*Exercise: generate the rest of the deformation fields that represent the rest of the faces in the dataset and display them.*

### Deformation fields over continuous domains:

The deformation field that we computed above is discrete as it is defined only over the mesh points. Since the real-world objects that we model are continuous, and the discretization of our meshes is rather arbitrary, this is not ideal. In Scalismo we usually prefer to work with continuous domains. Whenever we have an object in Scalismo, which is defined on a discrete domain, we can obtain a continuous representation, by means of interpolation.

To turn our deformation field into a continuous deformation field, we need to define an `Interpolator`

and call the `interpolate`

method:

As we do not know much about the structure of the points that define the mesh,
we use a `NearestNeighborInterpolator`

, which means that for every point on
which we want to evaluate the deformation, the nearest point on the mesh is
found and returned.

The resulting deformation field is now defined over the entire real space and can be evaluated at any 3D Point:

*Remark: This approach is general: Any discrete object in Scalismo can be interpolated.
For more structured domains, such as the DiscreteImageDomain, we can use
more sophisticated interpolation schemes, such as linear or b-spline interpolation.*

### The mean deformation and the mean mesh

Given a set of meshes, we are often interested to compute mesh that represents the mean shape. This is equivalent to computing the mean deformation $\overline{u}$, and to apply this deformation to them mean mesh.

To compute the mean deformation, we compute for each point in our mesh the sample mean of the deformations at this point in the deformation fields:

We can now apply the deformation to every point of the reference mesh, to obtain the mean mesh. To do this, the easiest way is to first genenerate a transformation from the deformation field, which we can use to map every point of the reference to its mean:

To obtain the mean mesh, we simply apply this transformation to the reference mesh:

Finally, we display the result: