Algorithm Search⌘K

Techniquecore

Cage-based deformation

Also called cage deformation.

Enclose a detailed model in a coarse polyhedral cage, bind each point by cage coordinates, and deform the model by moving the cage vertices.

Cage-based deformation moves a detailed model with a coarse cage around it. The cage is a closed polygon in 2D or a closed triangle mesh in 3D, with far fewer vertices than the model. Each point pp of the model is bound once to the cage by coordinates wi(p)w_i(p) with ∑iwi(p)=1\sum_i w_i(p) = 1 and ∑iwi(p)ci=p\sum_i w_i(p)c_i = p. When the user moves the cage vertices to ci′c'_i, the point moves to p′=∑iwi(p)ci′p' = \sum_i w_i(p)c'_i. The model can be a mesh, a point cloud or a volume, because only its points matter. The choice of coordinates decides the quality: mean value coordinates (Floater 2003 in 2D, Ju, Schaefer and Warren 2005 in 3D) have a closed form, harmonic coordinates are never negative, and Green coordinates keep angles.

In plain words

Put a detailed toy inside a simple wire box that roughly follows its shape. Each point of the toy remembers how much each corner of the box pulls on it, as a list of weights that add up to 1. Now bend the box by moving its corners. Each point of the toy goes to the same weighted mix of the moved corners, so the toy bends with the box. The animator works with a few corners instead of thousands of points. If the box does not move, the weights put every point back where it was.

When it applies

  • The model is too detailed, or in a form that is hard to edit directly, such as a scanned mesh or a point cloud.
  • A coarse shape around the model can describe the edit: a squash, a bend, a bulge, a pose of a limb.
  • The same model is deformed many times, so binding once and deforming often pays off.
  • The edit should be smooth in space; sharp folds and cuts are not what cages do.

The template

  1. Build the cage. Make a closed polygon or triangle mesh that encloses the model and roughly follows it, with a few vertices where the user wants control.
  2. Choose coordinates. Pick a kind of generalized barycentric coordinates for the cage: mean value, harmonic or Green coordinates, or another kind.
  3. Bind. Compute the weights wi(p)w_i(p) of every model point once and store them.
  4. Deform. For each pose of the cage, compute p′=∑iwi(p)ci′p' = \sum_i w_i(p)c'_i for every point. With the weights as an N×nN \times n matrix WW, this is one matrix product P′=WC′P' = WC'.
  1. CAGE-DEFORM(points, c, c')
  2. 1for each point p, w(p) ← the coordinates of p in the cage c ▷ once, at bind time
  3. 2for each point p do
  4. 3p' ← Σ_i w_i(p) c'_i
  5. 4return the points p'

With closed-form coordinates and a cage of nn vertices, line L1 costs Θ(n)\Theta(n) per point and lines L2 and L3 cost Θ(n)\Theta(n) per point per pose, so NN points cost Θ(Nn)\Theta(Nn) for the binding and for each pose, and Θ(Nn)\Theta(Nn) memory for the weights. If the coordinates have the partition of unity and linear precision, the rest cage gives back every point, and an affine map of the cage moves the model by the same affine map, because ∑iwi(Aci+t)=Ap+t\sum_i w_i(Ac_i + t) = Ap + t. The implementation uses 2D mean value coordinates, and the proof shows these properties for them.

Classic examples

Mean value coordinates in 2D. The coordinate of cage vertex ii at pp is w~i/∑jw~j\tilde w_i/\sum_j\tilde w_j with w~i=(tan⁡(αi−1/2)+tan⁡(αi/2))/∣ci−p∣\tilde w_i = (\tan(\alpha_{i-1}/2) + \tan(\alpha_i/2))/|c_i - p|, where αi\alpha_i is the signed angle at pp from cic_i to ci+1c_{i+1}; see Floater's mean value coordinates. For the L-shaped cage with the vertices (0,0)(0, 0), (2,0)(2, 0), (2,1)(2, 1), (1,1)(1, 1), (1,2)(1, 2), (0,2)(0, 2), the point (0.5,0.5)(0.5, 0.5) has the coordinates about (0.5427,0.1464,0.0427,0.0792,0.0427,0.1464)(0.5427, 0.1464, 0.0427, 0.0792, 0.0427, 0.1464). Slide the top edge of the L by 0.50.5 to the right, so that (1,2)(1, 2) goes to (1.5,2)(1.5, 2) and (0,2)(0, 2) to (0.5,2)(0.5, 2). The point (0.5,1.5)(0.5, 1.5) in the upper arm moves to about (0.8464,1.5)(0.8464, 1.5): it follows the top edge by 0.3460.346 of its move, and its height does not change, because the cage moved only in xx.

Mean value coordinates in 3D. Ju, Schaefer and Warren (2005) extended the coordinates to closed triangle meshes with a closed form per triangle, and used them to deform models with cages.

Harmonic coordinates. Harmonic coordinates (Joshi and others 2007) solve the Laplace equation inside the cage. They are never negative, so they behave well in concave cages, but they need a numerical solve.

Green coordinates. Green coordinates (Lipman, Levin and Cohen-Or 2008) add a weight per cage edge or face for its normal. They give conformal maps in 2D, but do not reproduce general affine maps and do not interpolate the cage.

Lattices. Free-form deformation is the special case of a box cage with Bernstein weights, and extended free-form deformation joins such cages.

Variations

  • Point and bone handles. Bounded biharmonic weights compute weights for cages, points and bones together by a constrained optimization, and blend affine transforms instead of only positions, as in linear blend skinning.
  • Sparse weights. Coordinates that are 0 far from a vertex, or weights cut below a threshold and renormalized, make WW sparse and the deformation cheaper.
  • Normals and frames. Transform the normals with the inverse transpose of the Jacobian of p↦∑iwi(p)ci′p \mapsto \sum_i w_i(p)c'_i, which needs the gradients of the coordinates.
  • Cages on the GPU. The deformation is a matrix product, so it runs well on graphics hardware.

Pitfalls

  • Negative weights. Mean value coordinates can be negative in a concave cage. In the L-shaped cage, the point (0.875,1.375)(0.875, 1.375) has the weight −0.0097-0.0097 for the vertex (2,1)(2, 1), which it sees from the back side of an edge: dragging that vertex right moves the point left. Use harmonic coordinates if this matters.
  • Points outside the cage. The coordinates are made for points inside. Mean value coordinates exist outside too, but they give a poor deformation there, and harmonic coordinates do not exist there.
  • Cage too far from the model. A loose cage gives weak, blurred control; a cage that cuts the model leaves parts outside.
  • Self-intersecting edits. A cage that folds over itself gives a folded model; nothing in the method prevents it.
  • Memory. Dense weights take NnNn numbers; for large models with many cage vertices, store sparse weights.
  • Not all coordinates interpolate. With Green coordinates the model does not touch the moved cage, so a model that must stay inside the cage needs care.

Code

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.

cage_based_deformation.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/cage-based-deformation/. The authors are listed in prov/K9W6TX.jsonl.
"""Cage-based deformation in 2D with mean value coordinates (Floater 2003).

A cage is a closed polygon c_0..c_{n-1}. Each point p of the shape gets
weights w_i(p) once, at bind time. When the cage vertices move to c'_i,
the point moves to p' = sum_i w_i(p) c'_i. The weights here are the mean
value coordinates

    w_i = (tan(a_{i-1} / 2) + tan(a_i / 2)) / r_i,   normalized to sum 1,

where r_i = |c_i - p| and a_i is the signed angle at p from c_i to c_{i+1}.
The half-angle tangent is computed as cross / (r_i r_{i+1} + dot), which
stays finite when p is on the line of an edge but not on the edge.
"""

import math


def mean_value_coordinates(p, cage, eps=1e-14):
    """Return the n mean value coordinates of p with respect to the cage."""
    n = len(cage)
    d = [(c[0] - p[0], c[1] - p[1]) for c in cage]
    r = [math.hypot(*v) for v in d]
    for i in range(n):
        if r[i] <= eps:
            return [1.0 if k == i else 0.0 for k in range(n)]
    tan_half = []
    for i in range(n):
        j = (i + 1) % n
        cr = d[i][0] * d[j][1] - d[i][1] * d[j][0]
        dt = d[i][0] * d[j][0] + d[i][1] * d[j][1]
        den = r[i] * r[j] + dt
        if den <= eps * r[i] * r[j]:
            # p lies on edge i: interpolate linearly along the edge.
            s = r[i] / (r[i] + r[j])
            out = [0.0] * n
            out[i], out[j] = 1 - s, s
            return out
        tan_half.append(cr / den)
    w = [(tan_half[i - 1] + tan_half[i]) / r[i] for i in range(n)]
    total = sum(w)
    return [x / total for x in w]


class Binding:
    """Bind a set of points to a cage once, then deform them for any new cage."""

    def __init__(self, points, cage):
        self.cage = [tuple(map(float, c)) for c in cage]
        self.weights = [mean_value_coordinates(p, self.cage) for p in points]  # @line L1

    def deform(self, new_cage):
        out = []
        for w in self.weights:  # @line L2
            x = sum(wi * c[0] for wi, c in zip(w, new_cage))  # @line L3
            y = sum(wi * c[1] for wi, c in zip(w, new_cage))
            out.append((x, y))
        return out  # @line L4
test_cage_based_deformation.py 89 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/cage-based-deformation/. The authors are listed in prov/K9W6TX.jsonl.
import math
import random

from cage_based_deformation import Binding, mean_value_coordinates

L_SHAPE = [(0, 0), (2, 0), (2, 1), (1, 1), (1, 2), (0, 2)]
SQUARE = [(0, 0), (1, 0), (1, 1), (0, 1)]


def inside_l(p):
    return 0 < p[0] < 2 and 0 < p[1] < 2 and not (p[0] >= 1 and p[1] >= 1)


def sample(rng, count):
    out = []
    while len(out) < count:
        p = (rng.uniform(0, 2), rng.uniform(0, 2))
        if inside_l(p):
            out.append(p)
    return out


def test_partition_of_unity_and_linear_precision():
    rng = random.Random(71)
    for p in sample(rng, 50):
        w = mean_value_coordinates(p, L_SHAPE)
        assert abs(sum(w) - 1) < 1e-12
        x = sum(a * c[0] for a, c in zip(w, L_SHAPE))
        y = sum(a * c[1] for a, c in zip(w, L_SHAPE))
        assert math.dist((x, y), p) < 1e-12


def test_square_center_and_vertices():
    w = mean_value_coordinates((0.5, 0.5), SQUARE)
    assert all(abs(a - 0.25) < 1e-12 for a in w)
    assert mean_value_coordinates((1, 0), SQUARE) == [0.0, 1.0, 0.0, 0.0]
    w = mean_value_coordinates((0.25, 0.0), SQUARE)
    assert abs(w[0] - 0.75) < 1e-12 and abs(w[1] - 0.25) < 1e-12


def test_negative_weight_in_nonconvex_cage():
    w = mean_value_coordinates((0.875, 1.375), L_SHAPE)
    assert abs(w[2] + 0.0097336) < 1e-6


def test_nonnegative_in_convex_cage():
    rng = random.Random(72)
    hexagon = [(math.cos(k * math.pi / 3), math.sin(k * math.pi / 3)) for k in range(6)]
    for _ in range(50):
        p = (rng.uniform(-0.8, 0.8), rng.uniform(-0.8, 0.8))
        if math.hypot(*p) < 0.85:
            assert min(mean_value_coordinates(p, hexagon)) > 0


def test_identity_and_affine_invariance():
    rng = random.Random(73)
    pts = sample(rng, 30)
    b = Binding(pts, L_SHAPE)
    assert all(math.dist(p, q) < 1e-12 for p, q in zip(pts, b.deform(L_SHAPE)))

    def aff(p):
        return (1.3 * p[0] - 0.4 * p[1] + 2.0, 0.2 * p[0] + 0.9 * p[1] - 1.0)

    moved = b.deform([aff(c) for c in L_SHAPE])
    assert all(math.dist(aff(p), q) < 1e-12 for p, q in zip(pts, moved))
    # The coordinates do not change under a similarity of the cage and the point.
    for p in pts[:10]:
        w1 = mean_value_coordinates(p, L_SHAPE)
        sim = [(2 * c[0] + 1, 2 * c[1] - 3) for c in L_SHAPE]
        w3 = mean_value_coordinates((2 * p[0] + 1, 2 * p[1] - 3), sim)
        assert max(abs(a - c) for a, c in zip(w1, w3)) < 1e-12


def test_worked_example():
    # Slide the top edge of the L by 0.5 to the right; the point (0.5, 1.5) follows by about 0.346.
    moved = [(0, 0), (2, 0), (2, 1), (1, 1), (1.5, 2), (0.5, 2)]
    (q,) = Binding([(0.5, 1.5)], L_SHAPE).deform(moved)
    assert abs(q[0] - 0.8463525) < 1e-6 and abs(q[1] - 1.5) < 1e-12


def test_point_on_line_of_edge():
    # (1, 0.5) is on the line x = 1 of the edge (1, 1) -> (1, 2), but not on it.
    w = mean_value_coordinates((1.0, 0.5), L_SHAPE)
    assert abs(sum(w) - 1) < 1e-12
    x = sum(a * c[0] for a, c in zip(w, L_SHAPE))
    y = sum(a * c[1] for a, c in zip(w, L_SHAPE))
    assert math.dist((x, y), (1.0, 0.5)) < 1e-12

Exercises

  1. Show that if the coordinates satisfy ∑iwi=1\sum_i w_i = 1 and ∑iwici=p\sum_i w_ic_i = p, then moving every cage vertex by an affine map A(x)=Mx+tA(x) = Mx + t moves every bound point by AA.

    Answer. ∑iwiA(ci)=M∑iwici+t∑iwi=Mp+t=A(p)\sum_i w_iA(c_i) = M\sum_i w_ic_i + t\sum_i w_i = Mp + t = A(p).

  2. Compute the mean value coordinates of the center of the unit square, and of the point (0.25,0)(0.25, 0) on its bottom edge.

    Answer. At the center every signed angle is π/2\pi/2 and every distance is 2/2\sqrt2/2, so all w~i\tilde w_i are equal and each coordinate is 1/41/4. On the bottom edge the coordinates are linear along the edge: (0.75,0.25,0,0)(0.75, 0.25, 0, 0).

  3. In the L-shaped cage, why is the weight of the vertex (2,1)(2, 1) negative at (0.875,1.375)(0.875, 1.375)?

    Answer. Seen from the point, the turn from (2,0)(2, 0) to (2,1)(2, 1) is counterclockwise, but the turn from (2,1)(2, 1) to (1,1)(1, 1) is clockwise: the point sees the edge from (2,1)(2, 1) to (1,1)(1, 1) from its back side. So α2<0\alpha_2 < 0, and tan⁡(α2/2)\tan(\alpha_2/2) outweighs tan⁡(α1/2)\tan(\alpha_1/2), which makes w~\tilde w of (2,1)(2, 1) negative.

  4. A model has 10510^5 points and its cage has 50 vertices. How many numbers does the binding store, and how many multiply-adds does one pose cost in 3D?

    Answer. 105⋅50=5⋅10610^5 \cdot 50 = 5 \cdot 10^6 weights, and 3⋅5⋅106=1.5⋅1073 \cdot 5 \cdot 10^6 = 1.5 \cdot 10^7 multiply-adds per pose, one set for each coordinate.

  5. Explain why the deformed point (0.8464,1.5)(0.8464, 1.5) of the slide example keeps its height 1.51.5.

    Answer. The moved cage differs from the rest cage only in the xx coordinates of two vertices, so ∑iwici,y′=∑iwici,y=1.5\sum_i w_ic'_{i,y} = \sum_i w_ic_{i,y} = 1.5 by linear precision.

Proof: Mean value coordinates sum to 1 and reproduce linear functions, so the cage deformation is the identity at rest and reproduces affine mapsfull

We use the notation of the page. The cage is a simple polygon c0,…,cn−1c_0, \dots, c_{n-1}, and pp is a point inside it that is not on the boundary. Let ri=∣ci−p∣r_i = |c_i - p|, ei=(ci−p)/rie_i = (c_i - p)/r_i, and let αi∈(−π,π)\alpha_i \in (-\pi, \pi) be the signed angle at pp from eie_i to ei+1e_{i+1}, with indices modulo nn. The unnormalized weights are w~i=(tan⁡(αi−1/2)+tan⁡(αi/2))/ri\tilde w_i = (\tan(\alpha_{i-1}/2) + \tan(\alpha_i/2))/r_i, and W=∑iw~iW = \sum_i \tilde w_i.

The half-angle tangent

Lemma 1. With ×\times the 2D cross product and ⋅\cdot the dot product, tan⁡(αi/2)=(ci−p)×(ci+1−p)riri+1+(ci−p)⋅(ci+1−p)\tan(\alpha_i/2) = \dfrac{(c_i - p) \times (c_{i+1} - p)}{r_ir_{i+1} + (c_i - p) \cdot (c_{i+1} - p)}, and the denominator is positive.

Proof. The numerator is riri+1sin⁡αir_ir_{i+1}\sin\alpha_i and the denominator is riri+1(1+cos⁡αi)r_ir_{i+1}(1 + \cos\alpha_i), which is positive because αi≠±π\alpha_i \ne \pm\pi, since pp is not on edge ii. And sin⁡α/(1+cos⁡α)=tan⁡(α/2)\sin\alpha/(1 + \cos\alpha) = \tan(\alpha/2). □\square

This is the form the implementation uses. It stays accurate when pp is on the line of an edge but not on the edge, where αi=0\alpha_i = 0; the denominator is 0 only when pp is on the edge, where the implementation returns the linear weights along the edge.

Linear precision

Lemma 2. For every angle α∈(−π,π)\alpha \in (-\pi, \pi) and the unit vector u(θ)=(cos⁡θ,sin⁡θ)u(\theta) = (\cos\theta, \sin\theta): ∫θθ+αu(t) dt=tan⁡(α/2) (u(θ)+u(θ+α))\int_\theta^{\theta + \alpha}u(t)\,dt = \tan(\alpha/2)\,(u(\theta) + u(\theta + \alpha)).

Proof. Rotate so that θ=0\theta = 0. The left side is (sin⁡α,1−cos⁡α)(\sin\alpha, 1 - \cos\alpha). The right side is tan⁡(α/2)(1+cos⁡α,sin⁡α)\tan(\alpha/2)(1 + \cos\alpha, \sin\alpha), and tan⁡(α/2)=sin⁡α/(1+cos⁡α)=(1−cos⁡α)/sin⁡α\tan(\alpha/2) = \sin\alpha/(1 + \cos\alpha) = (1 - \cos\alpha)/\sin\alpha, so the two agree (for α=0\alpha = 0 both sides are 0). □\square

Theorem 1. ∑iw~i(ci−p)=0\sum_i \tilde w_i(c_i - p) = 0.

Proof. Let θi\theta_i be the angle of eie_i, chosen continuously along the boundary, so that θi+1=θi+αi\theta_{i+1} = \theta_i + \alpha_i. Because pp is inside the simple polygon, the boundary winds once around pp, and ∑iαi=2π\sum_i\alpha_i = 2\pi. By Lemma 2, ∑itan⁡(αi/2)(ei+ei+1)=∑i∫θiθi+1u(t) dt=∫θ0θ0+2πu(t) dt=0\sum_i\tan(\alpha_i/2)(e_i + e_{i+1}) = \sum_i\int_{\theta_i}^{\theta_{i+1}}u(t)\,dt = \int_{\theta_0}^{\theta_0 + 2\pi}u(t)\,dt = 0. Collect the terms of each eie_i: it appears with tan⁡(αi−1/2)\tan(\alpha_{i-1}/2) and with tan⁡(αi/2)\tan(\alpha_i/2). So ∑i(tan⁡(αi−1/2)+tan⁡(αi/2))ei=∑iw~iriei=∑iw~i(ci−p)=0\sum_i(\tan(\alpha_{i-1}/2) + \tan(\alpha_i/2))e_i = \sum_i\tilde w_ir_ie_i = \sum_i\tilde w_i(c_i - p) = 0. □\square

Theorem 2. If W≠0W \ne 0, the coordinates wi=w~i/Ww_i = \tilde w_i/W satisfy ∑iwi=1\sum_iw_i = 1 and ∑iwici=p\sum_iw_ic_i = p.

Proof. The first claim is the normalization. By Theorem 1, ∑iwi(ci−p)=0\sum_iw_i(c_i - p) = 0, so ∑iwici=p∑iwi=p\sum_iw_ic_i = p\sum_iw_i = p. □\square

If the cage is convex, or more generally if pp lies in the kernel of a star-shaped cage, every αi\alpha_i is in (0,π)(0, \pi), so every w~i>0\tilde w_i > 0, and W>0W > 0: the coordinates are positive. For an arbitrary simple polygon, Hormann and Floater (2006) show that W>0W > 0 at every point that is not on the boundary; we quote this and do not prove it here. The tests check the sum and the linear precision at random points of an L-shaped cage, and the positivity in a convex hexagon.

The deformation

Theorem 3. With the coordinates of Theorem 2, line L3 returns pp for the rest cage c′=cc' = c, and A(p)A(p) for the moved cage ci′=A(ci)c'_i = A(c_i) with an affine map A(x)=Mx+tA(x) = Mx + t.

Proof. ∑iwiA(ci)=M∑iwici+t∑iwi=Mp+t\sum_iw_iA(c_i) = M\sum_iw_ic_i + t\sum_iw_i = Mp + t. The rest cage is the case M=IM = I, t=0t = 0. □\square

Theorem 4. The coordinates do not change when the cage and the point are moved together by a similarity x↦λQx+tx \mapsto \lambda Qx + t with a rotation QQ and λ>0\lambda > 0.

Proof. The signed angles αi\alpha_i do not change, and every rir_i is multiplied by λ\lambda. So every w~i\tilde w_i is divided by λ\lambda, and the normalized wiw_i do not change. □\square

The boundary cases

If p=cip = c_i, the implementation returns the unit weight of vertex ii, so p′=ci′p' = c'_i. If pp is on edge ii at the fraction s=ri/(ri+ri+1)s = r_i/(r_i + r_{i+1}) from cic_i, it returns the weights 1−s1 - s and ss, so ∑iwici=p\sum_iw_ic_i = p, and p′p' is the point at the same fraction of the moved edge. So points on the cage follow the cage boundary linearly. That the coordinates of inside points tend to these boundary values is a known property of mean value coordinates; we quote it and do not prove it here.

References

  1. Tao Ju, Scott Schaefer and Joe Warren, Mean value coordinates for closed triangular meshes, SIGGRAPH 2005, ACM Transactions on Graphics 24(3), 561-566, 2005, the whole paper (mean value coordinates for closed triangle meshes in 3D and their use to deform a model with a cage)
  2. Michael S. Floater, Mean value coordinates, Computer Aided Geometric Design 20(1), 19-27, 2003, the whole paper (the 2D coordinates from the tangents of half angles, the partition of unity and the linear precision)
  3. Kai Hormann and Michael S. Floater, Mean value coordinates for arbitrary planar polygons, ACM Transactions on Graphics 25(4), 1424-1441, 2006, the whole paper (mean value coordinates for polygons that are not star-shaped, with signed angles)
  4. Jesús R. Nieto and Antonio Susín, Cage based deformations: a survey, in Deformation Models, Lecture Notes in Computational Vision and Biomechanics, Springer, 75-99, 2013 (online 2012), the whole survey (the cage coordinates and their properties)

Where this leads

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 proofH2 full proof
  • correctness: no review record reviews/K9W6TX/proof-correctness.yaml
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
  • three classic examples