# Trilinear interpolation

Trilinear interpolation is a method of multivariate interpolation on a 3-dimensional regular grid. It approximates the value of a function at an intermediate point ${\displaystyle (x,y,z)}$ within the local axial rectangular prism linearly, using function data on the lattice points. For an arbitrary, unstructured mesh (as used in finite element analysis), other methods of interpolation must be used; if all the mesh elements are tetrahedra (3D simplices), then barycentric coordinates provide a straightforward procedure.

Trilinear interpolation is frequently used in numerical analysis, data analysis, and computer graphics.

## Compared to linear and bilinear interpolation

Trilinear interpolation is the extension of linear interpolation, which operates in spaces with dimension ${\displaystyle D=1}$, and bilinear interpolation, which operates with dimension ${\displaystyle D=2}$, to dimension ${\displaystyle D=3}$. These interpolation schemes all use polynomials of order 1, giving an accuracy of order 2, and it requires ${\displaystyle 2^{D}=8}$ adjacent pre-defined values surrounding the interpolation point. There are several ways to arrive at trilinear interpolation, which is equivalent to 3-dimensional tensor B-spline interpolation of order 1, and the trilinear interpolation operator is also a tensor product of 3 linear interpolation operators.

## Method

Eight corner points on a cube surrounding the interpolation point C
Depiction of 3D interpolation
A geometric visualisation of trilinear interpolation. The product of the value at the desired point and the entire volume is equal to the sum of the products of the value at each corner and the partial volume diagonally opposite the corner.

On a periodic and cubic lattice, let ${\displaystyle x_{d}}$, ${\displaystyle y_{d}}$, and ${\displaystyle z_{d}}$ be the differences between each of ${\displaystyle x}$, ${\displaystyle y}$, ${\displaystyle z}$ and the smaller coordinate related, that is:

${\displaystyle \ x_{d}=(x-x_{0})/(x_{1}-x_{0})}$
${\displaystyle \ y_{d}=(y-y_{0})/(y_{1}-y_{0})}$
${\displaystyle \ z_{d}=(z-z_{0})/(z_{1}-z_{0})}$

where ${\displaystyle x_{0}}$ indicates the lattice point below ${\displaystyle x}$, and ${\displaystyle x_{1}}$ indicates the lattice point above ${\displaystyle x}$ and similarly for ${\displaystyle y_{0},y_{1},z_{0}}$ and ${\displaystyle z_{1}}$.

First we interpolate along ${\displaystyle x}$ (imagine we are "pushing" the face of the cube defined by ${\displaystyle C_{0jk}}$ to the opposing face, defined by ${\displaystyle C_{1jk}}$), giving:

${\displaystyle \ c_{00}=c_{000}(1-x_{d})+c_{100}x_{d}}$
${\displaystyle \ c_{01}=c_{001}(1-x_{d})+c_{101}x_{d}}$
${\displaystyle \ c_{10}=c_{010}(1-x_{d})+c_{110}x_{d}}$
${\displaystyle \ c_{11}=c_{011}(1-x_{d})+c_{111}x_{d}}$

Where ${\displaystyle c_{000}}$ means the function value of ${\displaystyle (x_{0},y_{0},z_{0}).}$ Then we interpolate these values (along ${\displaystyle y}$, "pushing" from ${\displaystyle C_{i0k}}$ to ${\displaystyle C_{i1k}}$), giving:

${\displaystyle \ c_{0}=c_{00}(1-y_{d})+c_{10}y_{d}}$
${\displaystyle \ c_{1}=c_{01}(1-y_{d})+c_{11}y_{d}}$

Finally we interpolate these values along ${\displaystyle z}$ (walking through a line):

${\displaystyle \ c=c_{0}(1-z_{d})+c_{1}z_{d}.}$

This gives us a predicted value for the point.

The result of trilinear interpolation is independent of the order of the interpolation steps along the three axes: any other order, for instance along ${\displaystyle x}$, then along ${\displaystyle y}$, and finally along ${\displaystyle z}$, produces the same value.

The above operations can be visualized as follows: First we find the eight corners of a cube that surround our point of interest. These corners have the values ${\displaystyle c_{000}}$, ${\displaystyle c_{100}}$, ${\displaystyle c_{010}}$, ${\displaystyle c_{110}}$, ${\displaystyle c_{001}}$, ${\displaystyle c_{101}}$, ${\displaystyle c_{011}}$, ${\displaystyle c_{111}}$.

Next, we perform linear interpolation between ${\displaystyle c_{000}}$ and ${\displaystyle c_{100}}$ to find ${\displaystyle c_{00}}$, ${\displaystyle c_{001}}$ and ${\displaystyle c_{101}}$ to find ${\displaystyle c_{01}}$, ${\displaystyle c_{011}}$ and ${\displaystyle c_{111}}$ to find ${\displaystyle c_{11}}$, ${\displaystyle c_{010}}$ and ${\displaystyle c_{110}}$ to find ${\displaystyle c_{10}}$.

Now we do interpolation between ${\displaystyle c_{00}}$ and ${\displaystyle c_{10}}$ to find ${\displaystyle c_{0}}$, ${\displaystyle c_{01}}$ and ${\displaystyle c_{11}}$ to find ${\displaystyle c_{1}}$. Finally, we calculate the value ${\displaystyle c}$ via linear interpolation of ${\displaystyle c_{0}}$ and ${\displaystyle c_{1}}$

In practice, a trilinear interpolation is identical to two bilinear interpolation combined with a linear interpolation:

${\displaystyle c\approx \ l(b(c_{000},c_{010},c_{100},c_{110}),b(c_{001},c_{011},c_{101},c_{111}))}$

### Alternative algorithm

An alternative way to write the solution to the interpolation problem is

${\displaystyle f(x,y,z)\approx a_{0}+a_{1}x+a_{2}y+a_{3}z+a_{4}xy+a_{5}xz+a_{6}yz+a_{7}xyz}$

where the coefficients are found by solving the linear system

{\displaystyle {\begin{aligned}{\begin{bmatrix}1&x_{0}&y_{0}&z_{0}&x_{0}y_{0}&x_{0}z_{0}&y_{0}z_{0}&x_{0}y_{0}z_{0}\\1&x_{1}&y_{0}&z_{0}&x_{1}y_{0}&x_{1}z_{0}&y_{0}z_{0}&x_{1}y_{0}z_{0}\\1&x_{0}&y_{1}&z_{0}&x_{0}y_{1}&x_{0}z_{0}&y_{1}z_{0}&x_{0}y_{1}z_{0}\\1&x_{1}&y_{1}&z_{0}&x_{1}y_{1}&x_{1}z_{0}&y_{1}z_{0}&x_{1}y_{1}z_{0}\\1&x_{0}&y_{0}&z_{1}&x_{0}y_{0}&x_{0}z_{1}&y_{0}z_{1}&x_{0}y_{0}z_{1}\\1&x_{1}&y_{0}&z_{1}&x_{1}y_{0}&x_{1}z_{1}&y_{0}z_{1}&x_{1}y_{0}z_{1}\\1&x_{0}&y_{1}&z_{1}&x_{0}y_{1}&x_{0}z_{1}&y_{1}z_{1}&x_{0}y_{1}z_{1}\\1&x_{1}&y_{1}&z_{1}&x_{1}y_{1}&x_{1}z_{1}&y_{1}z_{1}&x_{1}y_{1}z_{1}\end{bmatrix}}{\begin{bmatrix}a_{0}\\a_{1}\\a_{2}\\a_{3}\\a_{4}\\a_{5}\\a_{6}\\a_{7}\end{bmatrix}}={\begin{bmatrix}c_{000}\\c_{100}\\c_{010}\\c_{110}\\c_{001}\\c_{101}\\c_{011}\\c_{111}\end{bmatrix}},\end{aligned}}}

yielding the result

{\displaystyle {\begin{aligned}\ a_{0}=&{\frac {c_{000}x_{1}y_{1}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{001}x_{1}y_{1}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{010}x_{1}y_{0}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{011}x_{1}y_{0}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{100}x_{0}y_{1}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{101}x_{0}y_{1}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{110}x_{0}y_{0}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{111}x_{0}y_{0}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}},\end{aligned}}}
{\displaystyle {\begin{aligned}\ a_{1}=&{\frac {c_{000}y_{1}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{001}y_{1}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{010}y_{0}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{011}y_{0}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{100}y_{1}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{101}y_{1}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{110}y_{0}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{111}y_{0}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}},\end{aligned}}}
{\displaystyle {\begin{aligned}\ a_{2}=&{\frac {c_{000}x_{1}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{001}x_{1}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{010}x_{1}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{011}x_{1}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{100}x_{0}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{101}x_{0}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{110}x_{0}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{111}x_{0}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}},\end{aligned}}}
{\displaystyle {\begin{aligned}\ a_{3}=&{\frac {c_{000}x_{1}y_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{001}x_{1}y_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{010}x_{1}y_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{011}x_{1}y_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{100}x_{0}y_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{101}x_{0}y_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{110}x_{0}y_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{111}x_{0}y_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}},\end{aligned}}}
{\displaystyle {\begin{aligned}\ a_{4}=&{\frac {c_{000}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{001}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{010}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{011}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{100}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{101}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{110}z_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{111}z_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}},\end{aligned}}}
{\displaystyle {\begin{aligned}\ a_{5}=&{\frac {c_{000}y_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{001}y_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{010}y_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{011}y_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{100}y_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{101}y_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{110}y_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{111}y_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}},\end{aligned}}}
{\displaystyle {\begin{aligned}\ a_{6}=&{\frac {c_{000}x_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{001}x_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{010}x_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{011}x_{1}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{100}x_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{101}x_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{110}x_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{111}x_{0}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}},\end{aligned}}}
{\displaystyle {\begin{aligned}\ a_{7}=&{\frac {c_{000}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{001}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+\\&{\frac {c_{010}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{011}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{100}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}+{\frac {c_{101}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+\\&{\frac {c_{110}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{0}-z_{1})}}+{\frac {c_{111}}{(x_{0}-x_{1})(y_{0}-y_{1})(z_{1}-z_{0})}}.\end{aligned}}}