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 of the model is bound once to the cage by coordinates with and . When the user moves the cage vertices to , the point moves to . 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
- 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.
- Choose coordinates. Pick a kind of generalized barycentric coordinates for the cage: mean value, harmonic or Green coordinates, or another kind.
- Bind. Compute the weights of every model point once and store them.
- Deform. For each pose of the cage, compute for every point. With the weights as an matrix , this is one matrix product .
CAGE-DEFORM(points, c, c')- 1
for each point p, w(p) ← the coordinates of p in the cage c ▷ once, at bind time - 2
for each point p do - 3
p' ← Σ_i w_i(p) c'_i - 4
return the points p'
With closed-form coordinates and a cage of vertices, line L1 costs per point and lines L2 and L3 cost per point per pose, so points cost for the binding and for each pose, and 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 . 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 at is with , where is the signed angle at from to ; see Floater's mean value coordinates. For the L-shaped cage with the vertices , , , , , , the point has the coordinates about . Slide the top edge of the L by to the right, so that goes to and to . The point in the upper arm moves to about : it follows the top edge by of its move, and its height does not change, because the cage moved only in .
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 sparse and the deformation cheaper.
- Normals and frames. Transform the normals with the inverse transpose of the Jacobian of , 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 has the weight for the vertex , 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 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.
# 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
-
Show that if the coordinates satisfy and , then moving every cage vertex by an affine map moves every bound point by .
Answer. .
-
Compute the mean value coordinates of the center of the unit square, and of the point on its bottom edge.
Answer. At the center every signed angle is and every distance is , so all are equal and each coordinate is . On the bottom edge the coordinates are linear along the edge: .
-
In the L-shaped cage, why is the weight of the vertex negative at ?
Answer. Seen from the point, the turn from to is counterclockwise, but the turn from to is clockwise: the point sees the edge from to from its back side. So , and outweighs , which makes of negative.
-
A model has 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. weights, and multiply-adds per pose, one set for each coordinate.
-
Explain why the deformed point of the slide example keeps its height .
Answer. The moved cage differs from the rest cage only in the coordinates of two vertices, so 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 , and is a point inside it that is not on the boundary. Let , , and let be the signed angle at from to , with indices modulo . The unnormalized weights are , and .
The half-angle tangent
Lemma 1. With the 2D cross product and the dot product, , and the denominator is positive.
Proof. The numerator is and the denominator is , which is positive because , since is not on edge . And .
This is the form the implementation uses. It stays accurate when is on the line of an edge but not on the edge, where ; the denominator is 0 only when is on the edge, where the implementation returns the linear weights along the edge.
Linear precision
Lemma 2. For every angle and the unit vector : .
Proof. Rotate so that . The left side is . The right side is , and , so the two agree (for both sides are 0).
Theorem 1. .
Proof. Let be the angle of , chosen continuously along the boundary, so that . Because is inside the simple polygon, the boundary winds once around , and . By Lemma 2, . Collect the terms of each : it appears with and with . So .
Theorem 2. If , the coordinates satisfy and .
Proof. The first claim is the normalization. By Theorem 1, , so .
If the cage is convex, or more generally if lies in the kernel of a star-shaped cage, every is in , so every , and : the coordinates are positive. For an arbitrary simple polygon, Hormann and Floater (2006) show that 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 for the rest cage , and for the moved cage with an affine map .
Proof. . The rest cage is the case , .
Theorem 4. The coordinates do not change when the cage and the point are moved together by a similarity with a rotation and .
Proof. The signed angles do not change, and every is multiplied by . So every is divided by , and the normalized do not change.
The boundary cases
If , the implementation returns the unit weight of vertex , so . If is on edge at the fraction from , it returns the weights and , so , and 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
- 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)
- 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)
- 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)
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag K9W6TX Edit this page History Report an error Markdown