Algorithm Search⌘K

Algorithmadvanced

Verbs and adverbs motion interpolation

Also called verbs and adverbs.

Interpolate a set of example motions over a space of control parameters with radial basis functions plus a linear term.

Time
O(n (a + q)) evaluateO(n^3 + n^2 (a + q)) fit
Space
O(n (n + q)) model
Where
a = the number of adverbs; n = the number of examples; q = the number of values per example
Properties
deterministicexactofflineiterative
Origin
Charles Rose, Michael F. Cohen, Bobby Bodenheimer, 1998

Verbs and adverbs makes a new motion for any point in a space of control parameters from a small set of example motions. A verb is an action, such as walk or reach. Its adverbs are the numbers that describe how it is done, such as the speed and the turn of a walk, or the target of a reach. Each of the nn examples has an adverb point pip_i and the values yiy_i of its time aligned joint curves. The interpolant is

y(p)=c0+Cp+∑i=1nwi Ri(∣p−pi∣),y(p) = c_0 + C p + \sum_{i=1}^{n} w_i\, R_i(|p - p_i|),

a linear part fitted by least squares, plus radial basis functions that pass exactly through the examples. The linear part gives a sensible trend between and beyond the examples; the radial part adds the detail that the linear part misses, and it is zero far from all examples. Rose, Cohen and Bodenheimer (1998) presented the method with a verb graph that joins verbs by transitions.

In plain words

You have captured a few walks: slow, fast, turning left, turning right, and some in between. Each walk has a label with its speed and its turn. Now you want a walk with a speed and turn that you did not capture. The method first finds the straight line trend: how the joint curves change, on average, as the speed or the turn goes up. That trend alone does not go through each captured walk exactly. So the method adds small bumps, one at each captured walk, that fix the difference there and fade away with distance. The new walk is the trend plus the bumps.

The problem

The input is nn example motions of one verb, all time aligned to a common structure of keytimes (for a walk: heel strike, toe off, the next heel strike), so that frame uu of the normalized time means the same moment of the step in every example. Each example ii has an adverb vector pi∈Rap_i \in \mathbb{R}^a and a value vector yi∈Rqy_i \in \mathbb{R}^q: for example the control points of the B-spline curves of all joints, and the keytimes. The task is a function y(p)y(p) that reproduces every example, y(pi)=yiy(p_i) = y_i, is smooth, and stays close to a linear trend away from the examples.

The output for a query pp is a value vector; rebuilding the joint curves from it and playing them over the interpolated keytimes gives the motion. The interpolation acts on each value separately: all values use the same radial functions, and each value has its own coefficients.

The algorithm

  1. FIT-VERB(P, Y) ▷ adverb points pᵢ, value vectors yᵢ
  2. 1X ← the rows (1, pᵢ) ▷ an n × (a + 1) matrix
  3. 2[c₀; C] ← (XᵀX)⁻¹ XᵀY ▷ the least squares linear part
  4. 3eᵢ ← yᵢ - (c₀ + C pᵢ) for every example ▷ the residuals
  5. 4αᵢ ← 2 · min over k ≠ i of |pᵢ - pₖ| ▷ the support of Rᵢ, a choice of the code
  6. 5K[r][i] ← Rᵢ(pᵣ) = B(2 |pᵣ - pᵢ| / αᵢ) ▷ B is the cubic B-spline, zero beyond 2
  7. 6W ← K⁻¹ E ▷ the radial weights, one row per example
  8. EVALUATE(p)
  9. 7return c₀ + C p + Σᵢ wᵢ Rᵢ(p)

The radial function is the uniform cubic B-spline B(s)=2/3−s2+s3/2B(s) = 2/3 - s^2 + s^3/2 for 0≤s<10 \le s < 1, B(s)=(2−s)3/6B(s) = (2 - s)^3 / 6 for 1≤s<21 \le s < 2, and 00 beyond. So RiR_i is 2/32/3 at pip_i and zero at the distance αi\alpha_i and beyond. The paper uses radial cubic B-splines with a width that depends on the spacing of the examples; the rule of line L4, twice the distance to the nearest other example, is the choice of the code. Because the αi\alpha_i differ, KK is not symmetric, and the code solves it by Gaussian elimination with partial pivoting.

The interpolant is linear in the example values, so it can also be written as y(p)=∑ibi(p) yiy(p) = \sum_i b_i(p)\, y_i with weights bi(p)b_i(p) that depend only on the adverb points: a weighted blend of the examples. This form is how the method drives a blend of motions directly.

Example

By hand. One adverb, three examples p=0,1,2p = 0, 1, 2 with the scalar values y=0,1,0y = 0, 1, 0. Line L2: the mean of pp is 1 and the mean of yy is 1/31/3, and ∑(p−1)(y−1/3)=1/3+0−1/3=0\sum (p - 1)(y - 1/3) = 1/3 + 0 - 1/3 = 0, so the slope is 0 and the linear part is the constant 1/31/3. Line L3: the residuals are −1/3-1/3, 2/32/3, −1/3-1/3. Line L4: every example has its nearest neighbor at distance 1, so every αi=2\alpha_i = 2 and Ri(p)=B(∣p−pi∣)R_i(p) = B(|p - p_i|). Line L5: B(0)=2/3B(0) = 2/3, B(1)=1/6B(1) = 1/6 and B(2)=0B(2) = 0, so

K=(2/31/601/62/31/601/62/3),Kw=(−1/32/3−1/3).K = \begin{pmatrix} 2/3 & 1/6 & 0 \\ 1/6 & 2/3 & 1/6 \\ 0 & 1/6 & 2/3 \end{pmatrix}, \qquad K w = \begin{pmatrix} -1/3 \\ 2/3 \\ -1/3 \end{pmatrix}.

By symmetry w1=w3=sw_1 = w_3 = s and w2=tw_2 = t, with 23s+16t=−13\tfrac23 s + \tfrac16 t = -\tfrac13 and 13s+23t=23\tfrac13 s + \tfrac23 t = \tfrac23, so s=−6/7s = -6/7 and t=10/7t = 10/7. At p=0.5p = 0.5: B(0.5)=0.479167B(0.5) = 0.479167 for the first two examples and B(1.5)=0.020833B(1.5) = 0.020833 for the third, and

y(0.5)=13+(−67+107)(0.479167)−67(0.020833)=0.589286.y(0.5) = \tfrac13 + \left(-\tfrac67 + \tfrac{10}{7}\right)(0.479167) - \tfrac67 (0.020833) = 0.589286 .

At p=1p = 1 the value is 1, the example. At p=3p = 3 only the third radial function reaches, R3=B(1)=1/6R_3 = B(1) = 1/6, and y(3)=1/3−1/7=0.190476y(3) = 1/3 - 1/7 = 0.190476. At p=−2p = -2 no radial function reaches, and y=1/3y = 1/3, the linear part.

A measured case. The test uses two adverbs, a speed in [0.5,2.5][0.5, 2.5] and a turn in [−1,1][-1, 1], and 8 values per example from a smooth nonlinear function. With 25 random examples (seed 5) and 200 random test points, the squared error, summed over the 8 values and averaged over the test points, is 0.0026 for the full interpolant and 0.0171 for the linear part alone. This is a measurement on one function, not a bound.

Correctness

The proof file works under two hypotheses: (H1) the adverb points do not all lie on one hyperplane, so XTXX^T X is nonsingular; (H2) the matrix KK is nonsingular. H2 is not proved for these radial functions; the code raises an error if KK is singular. Under them it shows:

  • Lines L1 to L3 give the least squares linear part, and the residuals are orthogonal to the constant and to every adverb.
  • The interpolant reproduces every example exactly (Theorem 1).
  • If the examples come from a linear function, the radial weights are zero and the interpolant is that linear function everywhere (Theorem 2).
  • Beyond the distance αi\alpha_i from every example, only the linear part is left.

How well the interpolant matches motion that was not captured is only measured. The choice of adverbs, the time alignment, and the radial widths are tuning, and that an interpolated walk looks natural is a visual heuristic.

Proof: The verb interpolant reproduces its examples, is exact on linear data, and is linear far from the examplesfull

We use the notation of the page. The arithmetic is exact. There are nn examples with adverb points pi∈Rap_i \in \mathbb{R}^a and value vectors yi∈Rqy_i \in \mathbb{R}^q, and the pip_i are distinct. XX is the nn by (a+1)(a + 1) matrix with rows (1,piT)(1, p_i^T). The radial function of example ii is Ri(p)=B(2∣p−pi∣/αi)R_i(p) = B(2|p - p_i| / \alpha_i), where BB is the cubic B-spline of the page and αi\alpha_i is twice the distance from pip_i to the nearest other example. KK is the nn by nn matrix Kri=Ri(pr)K_{ri} = R_i(p_r).

Two hypotheses are needed, and the code raises an error when either fails:

  • (H1) XTXX^T X is nonsingular, that is, the points pip_i do not all lie on one hyperplane of Ra\mathbb{R}^a (so n≥a+1n \ge a + 1);
  • (H2) KK is nonsingular.

H2 is not proved for this choice of radial functions. The αi\alpha_i differ, so KK is not symmetric, and the theory of positive definite radial kernels does not apply.

The linear part

Lemma 1. Lines L1 to L3 give the least squares fit: the coefficient matrix A=(XTX)−1XTYA = (X^T X)^{-1} X^T Y minimizes ∑i∣yi−(c0+Cpi)∣2\sum_i |y_i - (c_0 + C p_i)|^2, and the residuals ei=yi−(c0+Cpi)e_i = y_i - (c_0 + C p_i) satisfy ∑iei=0\sum_i e_i = 0 and ∑ipil ei=0\sum_i p_{il}\, e_i = 0 for every adverb ll.

Proof. The objective is a convex quadratic in AA, and its gradient is −2XT(Y−XA)-2 X^T (Y - X A). It is zero exactly at the solution of the normal equations XTXA=XTYX^T X A = X^T Y, which is unique under H1. The two families of equations in the claim are the rows of XT(Y−XA)=0X^T (Y - X A) = 0: the row of ones and the row of adverb ll. □\square

The radial part

Lemma 2. Ri(pi)=B(0)=2/3R_i(p_i) = B(0) = 2/3, and Ri(p)=0R_i(p) = 0 when ∣p−pi∣≥αi|p - p_i| \ge \alpha_i.

Proof. BB is 2/3−s2+s3/22/3 - s^2 + s^3/2 on [0,1)[0, 1), (2−s)3/6(2 - s)^3/6 on [1,2)[1, 2), and 0 for s≥2s \ge 2, and 2∣p−pi∣/αi≥22|p - p_i|/\alpha_i \ge 2 exactly when ∣p−pi∣≥αi|p - p_i| \ge \alpha_i. □\square

BB is C2C^2: at s=1s = 1 both pieces have the value 1/61/6, the slope −1/2-1/2 and the second derivative 11, and at s=2s = 2 the second piece has value, slope and second derivative 0.

Theorem 1. Under H1 and H2, the interpolant y(p)=c0+Cp+∑iwiRi(p)y(p) = c_0 + C p + \sum_i w_i R_i(p) of line L7 satisfies y(pr)=yry(p_r) = y_r for every example rr.

Proof. Line L6 solves KW=EK W = E, where row rr of EE is the residual ere_r. So ∑iwiRi(pr)=∑iKriwi=er\sum_i w_i R_i(p_r) = \sum_i K_{ri} w_i = e_r, and y(pr)=c0+Cpr+er=yry(p_r) = c_0 + C p_r + e_r = y_r. ■\blacksquare

Theorem 2. Under H1 and H2, if the examples are linear, yi=c0∗+C∗piy_i = c_0^* + C^* p_i, then W=0W = 0 and y(p)=c0∗+C∗py(p) = c_0^* + C^* p for every pp.

Proof. The true coefficients give zero residuals, so they minimize the least squares objective, and by uniqueness (H1) the fit returns them. Then E=0E = 0, and KW=0K W = 0 gives W=0W = 0 under H2. ■\blacksquare

Theorem 3. If ∣p−pi∣≥αi|p - p_i| \ge \alpha_i for every ii, then y(p)=c0+Cpy(p) = c_0 + C p.

Proof. Lemma 2 makes every radial term zero. ■\blacksquare

So, far from all examples, the interpolant falls back to the least squares hyperplane, and it does not oscillate there.

What is not proved

The interpolation error at a new adverb point is not bounded on this page. The test measures it on one smooth synthetic verb: the radial part lowers the mean squared error at 200 random points compared with the linear part alone. That is a measurement of one case, not a property of the method.

Complexity

The linear part needs XTXX^T X and XTYX^T Y in O(na(a+q))O(n a (a + q)) and a solve in O(a3+a2q)O(a^3 + a^2 q). The matrix KK costs O(n2a)O(n^2 a), and its solve costs O(n3+n2q)O(n^3 + n^2 q). With a<na < n, fitting costs O(n3+n2(a+q))O(n^3 + n^2 (a + q)) time. Evaluation costs O(aq)O(a q) for the linear part and O(n(a+q))O(n (a + q)) for the radial part. The model stores KK during the fit and the weights, O(n(n+q))O(n (n + q)) space. The paper uses tens of examples per verb, so the cost is small; the values qq (control points of all joints) dominate.

Properties

  • Exact at the examples. Under H1 and H2, every example is reproduced.
  • Linear far away. Outside the supports of all radial functions, the result is the linear trend, which keeps extrapolation sane for a small distance.
  • Linear in the values. The result is a weighted sum of the example values, with weights that depend only on the adverb points.
  • Not convex. Weights can be negative, so the result is not always between the examples; the hand example has a negative radial weight.

Variants and improvements

  • Pseudo examples. Later work by Rose, Sloan and Cohen adds examples made from the interpolant itself to make the weights better behaved over the space.
  • Weights per example. Precompute the cardinal weights bi(p)b_i(p) on a grid of adverbs and blend the examples with them, as in a blend tree.
  • Other kernels. Gaussian, thin plate or polyharmonic radial functions with a polynomial part; a Gaussian process gives the same form with a statistical reading.
  • Pose space deformation. Pose space deformation uses the same radial interpolation for corrective shapes of a mesh over joint angles instead of motions over adverbs.
  • Automatic alignment. Registration curves replace the hand set keytimes by an automatic timewarp.

When to use it

Use verbs and adverbs when a few captured or hand made examples must cover a continuous range of a parameter, such as walk speed or reach target, and when exact reproduction of the examples matters. It needs good time alignment of the examples, and a sensible choice of adverbs. For a large database with no parameterization, motion matching searches the data instead.

Pitfalls

  • Bad time alignment. If the keytimes do not match, the interpolant blends different phases, and the feet slide.
  • Points on a line. If all examples vary only in speed, there is no information about the turn; the fit is singular in that direction (H1 fails).
  • Close examples. Two examples very close in adverb space but with different values make KK ill conditioned and the weights large.
  • Extrapolation. Far outside the examples, only the linear trend is left; a walk at three times the fastest example speed is a linear guess.
  • The name. "Motion interpolation" is also an alias of motion blending, which mixes clips with weights given by the user.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

verbs_and_adverbs.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/verbs-and-adverbs/. The authors are listed in prov/G3VDNR.jsonl.
"""Verbs and adverbs interpolation (Rose, Cohen and Bodenheimer 1998).

A verb has n time aligned example motions. Example i has an adverb vector
p_i (a numbers, for example speed and turn) and a value vector y_i (q
numbers, for example the control points of every joint curve after time
alignment to a common key time structure). The interpolant is

    y(p) = c_0 + C p + sum_i w_i R_i(|p - p_i|),

computed in two steps:
  1. the linear part (c_0, C) by least squares over the examples;
  2. the radial part: the residuals of step 1 are interpolated exactly by
     the radial functions R_i(d) = B(2 d / alpha_i), where B is the cubic
     B-spline with support [0, 2] and alpha_i is twice the distance from p_i
     to the nearest other example. So R_i is zero beyond alpha_i.

The matrix K[k][i] = R_i(|p_k - p_i|) of step 2 is not symmetric, because
the alpha_i differ, and its invertibility is not proved; solve() raises an
error if it is singular.
"""

import math


def bspline(s):
    """The uniform cubic B-spline B(s), s >= 0, with B(0) = 2/3, zero for s >= 2."""
    s = abs(s)
    if s >= 2.0:
        return 0.0
    if s >= 1.0:
        return (2.0 - s) ** 3 / 6.0
    return 2.0 / 3.0 - s * s + 0.5 * s ** 3


def solve(A, b, eps=1e-14):
    """Gaussian elimination with partial pivoting. b has one row per row of
    A, and each row of b holds one value per right hand side."""
    n = len(A)
    A = [row[:] for row in A]
    B = [list(r) for r in b]
    for c in range(n):
        p = max(range(c, n), key=lambda r: abs(A[r][c]))
        if abs(A[p][c]) < eps:
            raise ValueError("singular matrix")
        A[c], A[p] = A[p], A[c]
        B[c], B[p] = B[p], B[c]
        for r in range(c + 1, n):
            f = A[r][c] / A[c][c]
            if f:
                for k in range(c, n):
                    A[r][k] -= f * A[c][k]
                for k in range(len(B[r])):
                    B[r][k] -= f * B[c][k]
    X = [[0.0] * len(B[0]) for _ in range(n)]
    for r in range(n - 1, -1, -1):
        for k in range(len(B[0])):
            s = B[r][k] - sum(A[r][t] * X[t][k] for t in range(r + 1, n))
            X[r][k] = s / A[r][r]
    return X


def dist(a, b):
    return math.sqrt(sum((x - y) ** 2 for x, y in zip(a, b)))


class Verb:
    def __init__(self, adverbs, values):
        self.P = [list(p) for p in adverbs]
        self.Y = [list(y) for y in values]
        n, a = len(self.P), len(self.P[0])
        self.n, self.a, self.q = n, a, len(self.Y[0])
        # step 1: least squares for the linear part, by the normal equations
        X = [[1.0] + p for p in self.P]  # @line L1
        XtX = [[sum(X[r][i] * X[r][j] for r in range(n)) for j in range(a + 1)] for i in range(a + 1)]
        XtY = [[sum(X[r][i] * self.Y[r][k] for r in range(n)) for k in range(self.q)] for i in range(a + 1)]
        self.lin = solve(XtX, XtY)  # @line L2
        resid = [[self.Y[r][k] - self.linear(self.P[r])[k] for k in range(self.q)] for r in range(n)]  # @line L3
        # step 2: radial functions through the residuals
        self.alpha = []
        for i in range(n):
            near = min(dist(self.P[i], self.P[k]) for k in range(n) if k != i) if n > 1 else 1.0
            self.alpha.append(2.0 * near)  # @line L4
        K = [[self.radial(i, self.P[r]) for i in range(n)] for r in range(n)]  # @line L5
        self.w = solve(K, resid)  # @line L6
        self.resid = resid

    def linear(self, p):
        return [self.lin[0][k] + sum(self.lin[1 + l][k] * p[l] for l in range(self.a))
                for k in range(self.q)]

    def radial(self, i, p):
        return bspline(2.0 * dist(p, self.P[i]) / self.alpha[i])

    def __call__(self, p):
        y = self.linear(p)
        for i in range(self.n):
            r = self.radial(i, p)
            if r:
                for k in range(self.q):
                    y[k] += self.w[i][k] * r
        return y  # @line L7
test_verbs_and_adverbs.py 89 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/verbs-and-adverbs/. The authors are listed in prov/G3VDNR.jsonl.
"""Tests of verbs and adverbs interpolation.

The interpolant reproduces every example, reproduces linear data exactly
everywhere with zero radial weights, has a least squares linear part (the
residuals are orthogonal to the columns), and is only linear far from the
examples. On a smooth nonlinear verb, the error at new adverbs is measured
against the linear part alone. Examples are made in code with a fixed seed.
"""

import math
import random

import verbs_and_adverbs as va


def curve(p, samples=8):
    """Control values of a joint curve for adverbs p = (speed, turn)."""
    speed, turn = p
    return [0.4 * speed * math.sin(2 * math.pi * k / samples) + 0.3 * turn * math.cos(speed + k)
            + 0.1 * speed * turn for k in range(samples)]


def examples(rng, n):
    P = [(rng.uniform(0.5, 2.5), rng.uniform(-1.0, 1.0)) for _ in range(n)]
    return P, [curve(p) for p in P]


def test_bspline_values():
    assert va.bspline(0.0) == 2.0 / 3.0
    assert abs(va.bspline(1.0) - 1.0 / 6.0) < 1e-15
    assert va.bspline(2.0) == 0.0 and va.bspline(3.0) == 0.0
    h = 1e-5
    for s in (1.0,):
        d1 = (va.bspline(s + h) - va.bspline(s - h)) / (2 * h)
        assert abs(d1 + 0.5) < 1e-6


def test_examples_are_reproduced():
    rng = random.Random(1)
    P, Y = examples(rng, 12)
    v = va.Verb(P, Y)
    for p, y in zip(P, Y):
        assert max(abs(a - b) for a, b in zip(v(p), y)) < 1e-9


def test_linear_data_is_exact():
    rng = random.Random(2)
    P = [(rng.uniform(0, 1), rng.uniform(0, 1), rng.uniform(0, 1)) for _ in range(10)]
    f = lambda p: [1.0 + 2.0 * p[0] - p[1] + 0.5 * p[2], -3.0 + p[2]]
    v = va.Verb(P, [f(p) for p in P])
    assert max(abs(x) for row in v.w for x in row) < 1e-9
    for _ in range(50):
        p = (rng.uniform(-1, 2), rng.uniform(-1, 2), rng.uniform(-1, 2))
        assert max(abs(a - b) for a, b in zip(v(p), f(p))) < 1e-9


def test_linear_part_is_least_squares():
    rng = random.Random(3)
    P, Y = examples(rng, 15)
    v = va.Verb(P, Y)
    for k in range(v.q):
        r = [v.resid[i][k] for i in range(v.n)]
        assert abs(sum(r)) < 1e-9
        for l in range(v.a):
            assert abs(sum(ri * p[l] for ri, p in zip(r, P))) < 1e-9


def test_far_from_examples_only_linear():
    rng = random.Random(4)
    P, Y = examples(rng, 10)
    v = va.Verb(P, Y)
    far = (20.0, 20.0)
    assert v(far) == v.linear(far)


def test_measured_error_against_linear_only():
    rng = random.Random(5)
    P, Y = examples(rng, 25)
    v = va.Verb(P, Y)
    err_full = err_lin = 0.0
    for _ in range(200):
        p = (rng.uniform(0.7, 2.3), rng.uniform(-0.8, 0.8))
        y = curve(p)
        err_full += sum((a - b) ** 2 for a, b in zip(v(p), y))
        err_lin += sum((a - b) ** 2 for a, b in zip(v.linear(p), y))
    # measured on this seed: the radial part lowers the squared error
    assert err_full < err_lin

History

Charles Rose, Michael F. Cohen and Bobby Bodenheimer described verbs and adverbs in 1998, with the keytime alignment, the radial interpolation with a linear part, and a verb graph whose edges are transitions between verbs. Peter-Pike Sloan, Charles Rose and Michael Cohen later used the same interpolation for shapes, and Rose, Sloan and Cohen applied it to inverse kinematics from examples. Radial basis function interpolation itself is older and comes from the interpolation of scattered data.

References

  1. Charles Rose, Michael F. Cohen and Bobby Bodenheimer, Verbs and adverbs, multidimensional motion interpolation, IEEE Computer Graphics and Applications 18(5), 1998, 32 to 40, the verbs and adverbs, the time alignment by keytimes, the interpolation by a linear part plus radial basis functions, and the verb graph
  2. Lucas Kovar and Michael Gleicher, Flexible automatic motion blending with registration curves, ACM SIGGRAPH/Eurographics Symposium on Computer Animation, 2003, 214 to 224, the automatic time alignment of example motions for a weighted blend
  3. Rick Parent, Computer Animation, Algorithms and Techniques, 3rd edition, Morgan Kaufmann, 2012, the material on motion capture data and its processing

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.evaluate
  • a full proof of time.fit
  • a full proof of space.model
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example