# 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`

:

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:

`The TriangleMeshInterpolator`

that we use here finds interpolates by finding for each point in the Euclidean space the closest
point on the surface and uses the corresponding deformation as the deformation at the given point. The point on the
surface is in turn obtained by barycentric interpolation of the corresponding vertex points. As a result of the interpolation,
we obtain a deformation field over the entire real space, which can be evaluated at any 3D Point:

*Remark: This approach is general: Any discrete object in Scalismo can be interpolated.
If we don't know anything about the structure of the domain, we can use the NearestNeighborInterpolator.
For most domain, however, more specialised interpolators are defined. To interpolate an image for example,
we can use efficient linear or b-spline interpolation schemes.*

### 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: