LERP, NLERP and SLERP

WHAT IS THAT AND WHY DO YOU NEED THIS?

Whenever you've got 2 numbers and you want something between them, you want some kind of interpolation. And the same in quaternions - you have to quaternions that describe the same no one is 100% correct but 100% incorrect either, so you want a quaternion that is a combination of these two. So you can use the simplest idea of combining two numbers:

(1)q3=αq1+(1α)q2

this is LERP (linear interpolation) but since, for orientation porpuse quaternion need to have norm of 1, after this interpolation you need to normalize result:

(2)q3=norm(αq1+(1α)q2)

and that is NLERP (normalized linear interpolation).

Now, how these methods could be interpreted, and what are the consequences in real applications? 

If we imagine a circle with a radius of 1 which is going across quaternions that we want to interpolate that would be the great arc that should be followed to achieve quaternions between those two. But if we use (1) q3 will not have a norm of 1. Why? - because with LERP we assume that quaternions between q1 and q2 lay on a straight line, but in reality, they are on the arc, and components of these quaternions don't change linearly. We can make the norm of quaternion go back to 1 by normalization (see (2)). But this process only "stretches" components of quaternions to put them back on the arc. So interpolated quaternions are not evenly distributed over the arc. You can see in the picture below:

LERP and NLERP methods presentation

And now if we consider rotation between q(0) and q(1) with a constant time interval this motion will not has constant speed at the middle of movement speed will be higher than at the beginning and at the end (visualization and more you can find on this page). The problem of this solution is that it divides distance into equal parts not an angle between quaternions.

The SLERP (spherical linear interpolation) method is the solution to the problem of equal division of the angle. At first, a geometric solution will be presented to better illustrate its operation. Then the actual method will be shown using the properties of quaternions. 
In order to keep the distribution of points on the arc along which the rotation takes place, the angle between the start and endpoints should be divided, not the distance that separates them. In the case where the quaternions are orthogonal (q1q2=0) the situation simplifies to that known from 2D rotation (see figure bellow):
(3)SLERP(q1,q2,t)=cos(tπ2)q1+sin(tπ2)q2where t∈<0,1>  
SLERP when quaternions are orthogonal

In most cases, however, the quaternions are not orthogonal to each other. Therefore, you must first determine the difference between quaternion q2 and the projection of quaternion q2 onto quaternion q1. After normalization, the resulting quaternion is unitary and orthogonal to quaternion q1:
(4)q=norm.(q2cosθq1)=norm.(q2(q1q2)q1)==q2(q1q2)q1||q2(q1q2)q1||=q2(q1q2)q1||q2sinθ||=q2(q1q2)q1sinθnext it can be written:(5)SLERP(q1,q2,t)=cos(tθ)q1+sin(tθ)q=cos(tθ)q1+sin(tθ)q2(q1q2)q1sinθ

SLERP method in the general case

The above equations are based on geometric operations for vectors. They are correct, but quaternions allow to simplify these calculations considerably. For quaternions, you can determine the quaternion qΔ by which the quaternion q1 must be multiplied to obtain q2:
 (6)qΔ=q2q11=q2q1
knowing qΔ we can write:
  (7)SLERP(q1,q2,t)=qΔtq1=(q2q1)tq1
At the end the function qt must be defined; according to the exponential form of quaternion:
 (8)qt=etθv=cos(tθ)+vsin(tθ)where t∈<0,1>
for qΔt:
 (9)qΔt=cos(tθx)+vsin(tθx)where θx=arccos(Re(qΔ)) and v=Im(qΔ)||Im(qΔ)||
finally:
 (10)SLERP(q1,q2,t)=cos(tarccos(Re(q2q1)))+Im(q2q1)||Im(q2q1)||sin(tarccos(Re(q2q1))) 

It is worth noting that for small angles the NLERP method does not give large errors and can be successfully used. Moreover, when you're using NLERP just to find a quaternion between you can use it as it is less computationally expensive. 

One more thing - the shortest path

The above equations ensure movement along the appropriate arc, but the shortest rotation path is not guaranteed. Because of the double coverage (see this post), the position defined by quaternion q is the same as the rotation described by q. However, interpolation using these two quaternions is not identical (one corresponds to rotation about angle θ and the other about (360θ). Therefore, before performing the interpolation, it is necessary to check which quaternion (q or q) is closer to the other quaternion being interpolated. A simple solution is to find the angle between the interpolated quaternions. If cosθ=q1q2<0 then it means that the angle is greater than 90 (which corresponds in 3D space to a rotation of more than 180°) and you should multiply by 1 one of the quaternions and then proceed to interpolation. It is worth noting that it does not matter in which of the quaternions the sign is swapped, because they still describe the same position in 3D space.

So finally we can write steps on how to interpolate quaternions q1 and q2:

  1. check if quaternions are on the same hemisphere of the hypersphere: cosθ=q1q20,
  2. if not multiply one of interpolated quaternion by 1: q1=1q1 or q2=1q2,
  3. decide which method you want to use: NLERP or SLERP,
  4. use formula (2) or (10).

Comments

Popular posts from this blog

Hardware - how to start?