Slerp
In computer graphics, Slerp is shorthand for spherical linear interpolation, introduced by Ken Shoemake in the context of quaternion interpolation for the purpose of animating 3D rotation. It refers to constant-speed motion along a unit-radius great circle arc, given the ends and an interpolation parameter between 0 and 1.
Geometric Slerp
Slerp has a geometric formula independent of quaternions, and independent of the dimension of the space in which the arc is embedded. This formula, a symmetric weighted sum credited to Glenn Davis, is based on the fact that any point on the curve must be a linear combination of the ends. Let p0 and p1 be the first and last points of the arc, and let t be the parameter, 0 ≤ t ≤ 1. Compute Ω as the angle subtended by the arc, so that cos Ω = p0 ∙ p1, the n-dimensional dot product of the unit vectors from the origin to the ends. The geometric formula is then
The symmetry lies in the fact that . In the limit as Ω → 0, this formula reduces to the corresponding symmetric formula for linear interpolation,
A Slerp path is, in fact, the spherical geometry equivalent of a path along a line segment in the plane; a great circle is a spherical geodesic.
More familiar than the general Slerp formula is the case when the end vectors are perpendicular, in which case the formula is p0 cos θ + p1 sin θ. Letting θ = t π/2, and applying the trigonometric identity cos θ = sin (π/2 − θ), this becomes the Slerp formula. The factor of 1/sin Ω in the general formula is a normalization, since a vector p1 at an angle of Ω to p0 projects onto the perpendicular ⊥p0 with a length of only sin Ω.
Some special cases of Slerp admit more efficient calculation. When a circular arc is to be drawn into a raster image, the preferred method is some variation of Bresenham's circle algorithm. Evaluation at the special parameter values 0 and 1 trivially yields p0 and p1, respectively; and bisection, evaluation at ½, simplifies to (p0 + p1)/2, normalized. Another special case, common in animation, is evaluation with fixed ends and equal parametric steps. If pk−1 and pk are two consecutive values, and if c is twice their dot product (constant for all steps), then the next value, pk+1, is the reflection pk+1 = c pk − pk−1.
Quaternion Slerp
When Slerp is applied to unit quaternions, the quaternion path maps to a path through 3D rotations in a standard way. The effect is a rotation with uniform angular velocity around a fixed rotation axis. When the initial end point is the identity quaternion, Slerp gives a segment of a one-parameter subgroup of both the Lie group of 3D rotations, SO(3), and its universal covering group of unit quaternions, S3. Slerp gives a straightest and shortest path between its quaternion end points, and maps to a rotation through an angle of 2Ω. However, because the covering is double (q and −q map to the same rotation), the rotation path may turn either the "short way" (less than 180°) or the "long way" (more than 180°). Long paths can be prevented by negating one end if the dot product, cos Ω, is negative, thus ensuring that −90° ≤ Ω ≤ 90°.
Slerp also has expressions in terms of quaternion algebra, all using exponentiation. Real powers of a quaternion are defined in terms of the quaternion exponential function, written as e q and given by the power series equally familiar from calculus, complex analysis and matrix algebra:
Writing a unit quaternion q in versor form, cos Ω + v sin Ω, with v a unit 3-vector, and noting that the quaternion square v2 equals −1 (implying a quaternion version of Euler's formula), we have e v Ω = q, and q t = cos t Ω + v sin t Ω. The identification of interest is q = q1 q0−1, so that the real part of q is cos Ω, the same as the geometric dot product used above. Here are four equivalent quaternion expressions for Slerp.
The derivative of Slerp(q0, q1; t) with respect to t, assuming the ends are fixed, is log(q1q0−1) times the function value, where the quaternion natural logarithm in this case yields half the 3D angular velocity vector. The initial tangent vector is parallel transported to each tangent along the curve; thus the curve is, indeed, a geodesic.
In the tangent space at any point on a quaternion Slerp curve, the inverse of the exponential map transforms the curve into a line segment. Slerp curves not extending through a point fail to transform into lines in that point's tangent space.
Quaternion Slerps are commonly used to construct smooth animation curves by mimicking affine constructions like the de Casteljau algorithm for Bézier curves. Since the sphere is not an affine space, familiar properties of affine constructions may fail, though the constructed curves may otherwise be entirely satisfactory. For example, the de Casteljau algorithm may be used to split a curve in affine space; this does not work on a sphere.
The two-valued Slerp can be extended to interpolate among many unit quaternions[1], but the extension loses the fixed execution-time of the Slerp algorithm.
Source code
The below C++ code illustrates an implementation of the Slerp algorithm that handles some common edge cases.
Quaternion slerp(Quaternion v0, Quaternion v1, double t) {
// Only unit quaternions are valid rotations.
// Normalize to avoid undefined behavior.
v0.normalize();
v1.normalize();
// Compute the cosine of the angle between the two vectors.
double dot = dot_product(v0, v1);
// If the dot product is negative, slerp won't take
// the shorter path. Note that v1 and -v1 are equivalent when
// the negation is applied to all four components. Fix by
// reversing one quaternion.
if (dot < 0.0f) {
v1 = -v1;
dot = -dot;
}
const double DOT_THRESHOLD = 0.9995;
if (dot > DOT_THRESHOLD) {
// If the inputs are too close for comfort, linearly interpolate
// and normalize the result.
Quaternion result = v0 + t*(v1 - v0);
result.normalize();
return result;
}
// Since dot is in range [0, DOT_THRESHOLD], acos is safe
double theta_0 = acos(dot); // theta_0 = angle between input vectors
double theta = theta_0*t; // theta = angle between v0 and result
double sin_theta = sin(theta); // compute this value only once
double sin_theta_0 = sin(theta_0); // compute this value only once
double s0 = cos(theta) - dot * sin_theta / sin_theta_0; // == sin(theta_0 - theta) / sin(theta_0)
double s1 = sin_theta / sin_theta_0;
return (s0 * v0) + (s1 * v1);
}
The following is the same algorithm except in Python. It uses NumPy and can calculate multiple slerp interpolations between the two quaternions.
import numpy as np
def slerp(v0, v1, t_array):
"""Spherical linear interpolation."""
# >>> slerp([1,0,0,0], [0,0,0,1], np.arange(0, 1, 0.001))
t_array = np.array(t_array)
v0 = np.array(v0)
v1 = np.array(v1)
dot = np.sum(v0 * v1)
if dot < 0.0:
v1 = -v1
dot = -dot
DOT_THRESHOLD = 0.9995
if dot > DOT_THRESHOLD:
result = v0[np.newaxis,:] + t_array[:,np.newaxis] * (v1 - v0)[np.newaxis,:]
return (result.T / np.linalg.norm(result, axis=1)).T
theta_0 = np.arccos(dot)
sin_theta_0 = np.sin(theta_0)
theta = theta_0 * t_array
sin_theta = np.sin(theta)
s0 = np.cos(theta) - dot * sin_theta / sin_theta_0
s1 = sin_theta / sin_theta_0
return (s0[:,np.newaxis] * v0[np.newaxis,:]) + (s1[:,np.newaxis] * v1[np.newaxis,:])
References
- Pennec, Xavier (March 1998). "Computing the Mean of Geometric Features Application to the Mean Rotation". Retrieved 19 June 2020. Cite journal requires
|journal=
(help)
External links
- Shoemake, Ken. "Animating Rotation with Quaternion Curves" (PDF). SIGGRAPH 1985.
- Erik B., Dam; Martin, Koch; Lillholm, Martin (July 17, 1998). "Quaternions, Interpolation and Animation" (PDF). University of Copenhagen. Archived (PDF) from the original on 2017-08-30.
- Blow, Jonathan (February 26, 2004). "Understanding Slerp, Then Not Using It". Archived from the original on 2017-08-25.
- Martin, Brian (June 23, 1999). "Brian Martin on Quaternion Animation". Archived from the original on 2016-03-24.