# Interpolating vectors on a grid

If you have values on a grid and you want to find the value of a point within one of the cells, you can use techniques like bilinear or bicubic interpolation to get the data at that point.

What technique(s) would be used if the data on the grid was a vector (a surface normal specifically)?

I know that quaternions are good for slerping, which I think could be good in this case to do a "bilinear slerp", but quaternions require basis vectors that represent an orientation, not just a single vector.

Also, I'm not sure how you'd use quaternions to apply something like bicubic interpolation.

What methods would be good in this case, to interpolate vectors on a grid?

• Slerp is a possibility for normals. It stands for "spherical lerp"—unit vectors lie on a sphere as much as unit quaternions do. – John Calsbeek Feb 19 '16 at 7:08
• I've found out that nlerp is a decent method: just interpolate your components and re-normalize. I can do a bicubic version of this by doing each component individually. Found some decent links too: keithmaggio.wordpress.com/2011/02/15/… and number-none.com/product/… . – Alan Wolfe Feb 19 '16 at 14:50
• If you know your grid vectors don't vary more than, say, 120 degrees from grid point to grid point (i.e. trying to avoid problems with "which way do you interpolate), why wouldn't bicubic interpolation with renormalisation be "good enough"? – Simon F Feb 19 '16 at 14:55

I did some research and found the answer I was looking for. The three most common ways to interpolate vectors are:

1. Slerp - short for "spherical interpolation", this is the most correct way, but is also the costliest. In practice you likely do not need the precision.
2. lerp - short for "linear interpolation", you just do a regular linear interpolation between the vectors and use that as a result.
3. nlerp - short for "normalized linear interpolation" you just normalize the result of a lerp. Useful if you need your interpolated vector to be a normalized vector.

In practice, lerp/nlerp are pretty good at getting a pretty close interpolated direction so long as the angle they are interpolating between is sufficiently small (say, 90 degrees, or 120 like Simon mentions in his comment), and nlerp is of course good at keeping the right length, if you need a normalized vector. If you want to preserve the length while interpolating between non normalized vectors, you could always interpolate the length and direction separately.

Here is an example of the three interpolations on a large angle. Dark grey = start vector, light grey = end vector. Green = slerp, blue = lerp, orange = nlerp. Here is an example of a medium sized angle (~90 degrees) interpolating the same time t between the angles: Lastly, here's a smaller angle (~35 degrees). You can see that the results of lerp / nlerp are more accurate as the angle between the interpolated vectors gets smaller. If you do lerp or nlerp, you can definitely do both bilinear as well as bicubic interpolation.

Using slerp, you can do bilinear interpolation, but I'm not sure how bicubic would translate.

I generated these images by taking screenshots from an interactive shadertoy demo I made to demonstrate these differences. You can see that shadertoy here:
• @AlanWolfe: Side-note: I can write up an answer than pre-computes the slerp results into a texture and looks-up the values at runtime, if you're interested. Given that the color value is the output normal, and if we can assume both lookup vectors are normal-or-nearly-normal-length, I believe the lookup table can be reduced to only plausible inputs, without covering the whole input space (4-dimensional, 2 for each input vector) and without resorting to using vector angles as inputs (2-dimensional, but still incurs the atan2 cost). – Slipp D. Thompson Mar 2 '16 at 20:40