Skip to content

Interpolation

Interpolation is a technique for creating a continuous function from a set of discrete samples with values. At the given samples, the function should return the already-known values; and elsewhere it should return something. Depending on the interpolation scheme, the resulting function may exhibit different level of smoothness and continuity. Interpolation is used everywhere in Computer Graphics, and is even used right now for rendering this web page.

Note

When I was learning this stuff, I regularly confused Interpolation with Approximation. When doing Approximation, we try to build a function that best approximate a set of discrete samples, without necessarily passing by the prescribed values. This includes, for instance, linear regression or the least squares approximation.

There are too many interpolation schemes to explain them all, so we will focus here on the most commonly used: linear and cubic interpolation. More advanced references are available at the bottom of this page if you want to learn more.

Linear interpolation

Linear interpolation, also called lerp or mix, is a weighted average of two values. Let's look at one formula, which is the same in all dimensions:

\[ lerp(a, b, t) = a + (b - a) * t \]

Warning

TODO: Write the equivalent in code here!

The parameter \(t\) is called the interpolant, and represents how much we want to interpolation to have values close to \(a\) or \(b\). When \(t=1\), this is equal to \(a + b - a\) so the end value is \(b\). When \(t=0\), then \((b -a)=0\), so the end value is \(a\). When \(t=0.5\), the end value is an average of \(a\) and \(b\).

Info

If the interpolant \(t\) is outside of the \([0, 1]\) range, then we are doing linear extrapolation.

We can try to look at linear interpolation in a geometric way: starting from a value \(a\), we go in the direction \((b-a)\) by a certain amount \(t\). This is illustrated on the figure below.

Floating-point errors

While perfectly valid from a mathematical point of view, it often results in floating-point imprecisions when implementing it on a computer. This is a very common difference between maths and code: in the end, our computations are done using a discrete number of bytes (float and double), which will often leads to precision issues. A more stable definition of linear interpolation is used:

\[ lerp(a, b, t) = (1 - t) * a + t * b \]

This is just a rewrite of the first equation that is strictly equivalent mathematically speaking, but more stable when implemented in code.

Warning

TODO: Write the equivalent in code here!

Bilinear & Trilinear interpolation

TODO: Explain that bilinear and trilinear are just lerps done multiple time in all directions. Show a figure with that + code.

Slerp: Spherical Lerp

TODO: Spherical linear interpolation, which interpolates between two points as if they were on the same circle arc. See figure below for visual differences. Put formula in here directly + code, and explain quickly when it is used.

Continuity and Derivability

TODO: \(C^0\) continuity only so there are no holes between our values, which is good, but means that there are discontinuities in the derivative at the sample locations. When we want better, we go with higher order interpolation scheme, such as quadratic or cubic explained below. Show the discontinuity in a figure.

Cubic interpolation

TODO: Explain the more complex interpolation scheme, with code as well.

Exercices

TODO: find some ideas for an exercise.

References