Algorithmcore
Free-form deformation
Also called Sederberg-Parry deformation, lattice deformation.
Embed an object in a trivariate Bezier lattice and move its points with the lattice control points, so that any shape deforms smoothly.
- Solves
- Shape deformation
- Time
- Θ(N l m n) allΘ(l m n) per_point
- Space
- Θ(l m n) extra
- Where
- N = the number of points to deform; l = the lattice degree along S; m = the lattice degree along T; n = the lattice degree along U
- Model
- Real RAM
- Properties
- deterministicexactonlineiterative
- Evidence
- Proof sketchCode in 1 languages
Free-form deformation (FFD) bends space, and with it any object that lies in that space. The object sits inside a box with a grid of control points, the lattice. Each point of the object gets its coordinates in the box once, and its new position is a trivariate Bernstein polynomial of those coordinates with the moved control points as coefficients. Thomas Sederberg and Scott Parry (1986) introduced the method. It works for polygon meshes, parametric surfaces and point clouds alike, because it moves points and does not care what they belong to.
In plain words
Put a soft object inside a block of clear jelly. Grab the jelly at a few marked points and pull them. The jelly bends, and the object bends with it, smoothly and without tears. FFD is that jelly. The marked points are the control points of a grid that fills a box around the object. At the start each point of the object is told where it sits in the box, as three numbers between 0 and 1. When the control points move, each object point takes a weighted mix of all the control points, with weights that depend only on those three numbers. If no control point moves, every point stays where it is.
The problem
The input is a lattice and a set of points. The lattice is a parallelepiped with the corner and the edge vectors , and , and the degrees , and . It has the control points , which start at the regular grid
and are then moved by the user. The output is the new position of each point . Points inside the lattice should move smoothly with the control points, and the map should be the identity while the lattice is at rest.
The algorithm
is the Bernstein polynomial of degree .
FFD(X, X_0, S, T, U, P)- 1
s ← (T × U)·(X - X_0) / ((T × U)·S) - 2
t ← (S × U)·(X - X_0) / ((S × U)·T) - 3
u ← (S × T)·(X - X_0) / ((S × T)·U) - 4
if s, t or u is outside [0, 1] then return X - 5
X' ← 0 - 6
for i ← 0..l, j ← 0..m, k ← 0..n do X' ← X' + B_i^l(s) B_j^m(t) B_k^n(u) P_ijk - 7
return X'
Lines L1 to L3 compute the local coordinates, the unique with . Each is a dot product with a fixed vector, so they can be computed once per point and kept while the user edits the lattice. Line L4 leaves the points outside the lattice where they are. Line L6 evaluates the Bezier volume; the de Casteljau algorithm applied along each of the three directions gives the same result.
Example
Take the unit cube as the lattice, with , so it has 27 control points on the grid with spacing .
Move only the center control point from to .
Its weight at the center of the cube is , so the center point moves up by of , to .
At the weight is , and the point moves to .
The corners of the cube do not move, because every Bernstein weight of an inner control point is 0 there.
The Jacobian at is : the region below the moved control point is stretched up by in volume, and the region above it is compressed.
The implementation reproduces these numbers exactly with fractions.Fraction.
Correctness
The Bernstein polynomials satisfy and . With the control points at rest, line L6 gives , which is by lines L1 to L3. So the rest lattice gives the identity, exactly in rational arithmetic. The same two identities show that an affine map applied to every control point gives for every point inside the lattice. The Jacobian of the map is , where the rows of the second factor are the three vectors of lines L1 to L3; the volume of a deformed region is , a statement Sederberg and Parry use to control the volume. The proof gives these arguments and the continuity across the lattice boundary.
Proof: Free-form deformation is the identity at rest, reproduces affine maps, and is continuous across a face whose control points are at restfull
We use the notation of the page. The lattice has the corner , the edge vectors , , with , and the degrees . The map is and for a point inside the lattice.
The local coordinates
Lemma 1. For every point , lines L1 to L3 return the unique with .
Proof. , , are linearly independent, so the coefficients exist and are unique. Write and take the dot product with . The terms in and vanish, because is orthogonal to both, so . The divisor is the triple product , and this is line L1. Lines L2 and L3 follow in the same way with and .
The Bernstein identities
Lemma 2. For every and every real : and .
Proof. The first sum is by the binomial theorem. For the second, for , so the sum is .
Identity and affine invariance
Theorem 1. If every control point is at rest, , then for every point inside the lattice.
Proof. Write . By the first identity of Lemma 2 applied three times, , so the terms give . The terms give , and in the same way the and terms give and . So by Lemma 1.
The proof uses only ring operations on the inputs, so it holds exactly in rational arithmetic, and the implementation checks it with fractions.Fraction.
Theorem 2. Let be an affine map. If every control point is moved to , then for every point inside the lattice.
Proof. , by Theorem 1 for the rest points and by .
The Jacobian and the volume
Lemma 3. Inside the lattice, , where the columns of are , , and the rows of are the vectors , and . The derivative of a Bernstein polynomial is , with .
Proof. is a linear function of with the matrix by Lemma 1, so the chain rule gives . The derivative formula follows from the product rule and , .
By the change of variables formula, the deformed image of a region inside the lattice, if the map is one to one on , has the volume . If somewhere, the map turns the orientation there, and the object is folded.
Continuity across the lattice boundary
Theorem 3. Take the face of the lattice. If the control points are at rest, then is continuous across this face: the points just inside and just outside map to nearby points. If all the control points with are at rest, all derivatives of of order at most agree on both sides of the face.
Proof. At , for and , so . If these points are at rest, this is by Lemma 2, which is the identity, the map outside. For the derivatives, the -th derivative in of at is 0 for , so and all its derivatives in and depend only on the control points with . If those with are at rest, every mixed derivative of order at most at the face equals that of the rest map, which is the identity by Theorem 1. Outside the lattice the map is the identity, so all derivatives of order at most match.
If a control point on the face moves, the map jumps at the face, and the object tears there; the implementation leaves points outside the lattice in place, so it shows this jump.
Complexity
Lines L1 to L4 take time. The Bernstein values take time, and line L6 takes time, so one point costs and points cost . The lattice holds control points. The weights of each point can be stored once, which makes each later edit a sparse matrix product, but the dense weights cost memory.
Properties
- Identity at rest (proved). The rest lattice gives , by the linear precision of the Bernstein basis.
- Affine invariance (proved). Moving the lattice by an affine map moves the object by the same map.
- Smooth inside (proved). The map is a polynomial of degree in inside the lattice, so it is there.
- Boundary continuity (proved). The map is continuous across a face of the lattice if the control points on that face are at rest, and if the outer layers of control points at that face are at rest.
- Not interpolating. A point at a control point does not follow it: only the corners of the lattice are interpolated.
- No volume guarantee. The volume changes by , which can be negative: a lattice folded too far turns the object inside out.
Variants and improvements
- Other lattice shapes. Extended free-form deformation (Coquillart 1990) joins Bezier volumes of other shapes, such as cylinders, and finds the local coordinates by Newton iteration.
- B-spline lattices. A B-spline volume instead of a Bezier volume makes each control point act only on nearby cells, so large lattices stay local and cheap.
- Direct manipulation. The user drags a point of the object, and a least squares solve finds the control point moves that put it there.
- Cages. Cage-based deformation replaces the box by a coarse mesh around the object and the Bernstein weights by generalized barycentric coordinates.
- Simple global maps. Barr's global deformations taper, twist and bend with a closed form and no lattice.
When to use it
Use FFD to shape an object with a few handles when its own representation is detailed, mixed, or not editable, such as a dense mesh or a point cloud. It is the right tool for broad, smooth changes of shape: bulges, bends, squashes. For a shape that should follow a coarse mesh around it, prefer cage-based deformation, whose cage can fit the object closely. For a deformation along a curve, such as a wrinkle or a lip, use wires. For deformations that should keep the local detail of a surface rigid, use a surface method such as Laplacian surface editing or as-rigid-as-possible deformation.
Pitfalls
- Points outside the lattice. They do not move, so a lattice that cuts through an object tears it unless the boundary layers of control points stay at rest.
- Folding. A control point moved past its neighbours can make and turn part of the object inside out; check the sign of if this matters.
- Normals. Transform normals with the inverse transpose of , not with , as for Barr's deformations.
- Polygon meshes. FFD moves vertices only, so large curvature on large triangles shows as facets; refine the mesh first.
- Degree. A high degree makes each control point act on the whole lattice; for local control, raise the number of cells with a spline lattice, not the degree.
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.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/free-form-deformation/. The authors are listed in prov/5QACBN.jsonl.
"""Free-form deformation of Sederberg and Parry (1986).
A lattice is a parallelepiped with the corner X0 and the edge vectors S, T
and U. A point X inside it has the local coordinates (s, t, u) in [0, 1]^3
with X = X0 + s S + t T + u U. The lattice holds (l + 1)(m + 1)(n + 1)
control points P[i][j][k]. The deformed point is the trivariate Bernstein
polynomial
X' = sum_ijk B_i^l(s) B_j^m(t) B_k^n(u) P[i][j][k],
with B_i^l(s) = C(l, i) (1 - s)^(l - i) s^i. In the undeformed lattice,
P[i][j][k] = X0 + (i / l) S + (j / m) T + (k / n) U, and X' = X.
All arithmetic works with int, float or fractions.Fraction. With Fraction
inputs the result is exact.
"""
from fractions import Fraction
from math import comb
def _sub(a, b):
return tuple(x - y for x, y in zip(a, b))
def _dot(a, b):
return sum(x * y for x, y in zip(a, b))
def _cross(a, b):
return (
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
)
def bernstein(deg, x):
"""Return the list [B_0^deg(x), ..., B_deg^deg(x)]."""
return [comb(deg, i) * (1 - x) ** (deg - i) * x ** i for i in range(deg + 1)]
def bernstein_derivative(deg, x):
"""Return the derivatives d/dx B_i^deg(x) for i = 0..deg."""
if deg == 0:
return [0 * x]
low = bernstein(deg - 1, x)
out = []
for i in range(deg + 1):
a = low[i - 1] if i >= 1 else 0
b = low[i] if i <= deg - 1 else 0
out.append(deg * (a - b))
return out
class Lattice:
"""A parallelepiped lattice of degree (l, m, n) with movable control points."""
def __init__(self, x0, s_axis, t_axis, u_axis, l, m, n):
self.x0 = tuple(x0)
self.axes = (tuple(s_axis), tuple(t_axis), tuple(u_axis))
self.deg = (l, m, n)
s, t, u = self.axes
# Each local coordinate is a dot product with a fixed covector:
# s = (T x U).(X - X0) / ((T x U).S), and the same for t and u.
self.covectors = []
for a, b, c in ((t, u, s), (s, u, t), (s, t, u)):
w = _cross(a, b)
den = _dot(w, c)
if den == 0:
raise ValueError("the lattice axes are linearly dependent")
self.covectors.append(tuple(_div(x, den) for x in w))
self.points = self.rest_points()
def rest_points(self):
"""Return the control points of the undeformed lattice."""
l, m, n = self.deg
s, t, u = self.axes
pts = []
for i in range(l + 1):
plane = []
for j in range(m + 1):
row = []
for k in range(n + 1):
row.append(tuple(
self.x0[d] + _frac(i, l) * s[d] + _frac(j, m) * t[d] + _frac(k, n) * u[d]
for d in range(3)
))
plane.append(row)
pts.append(plane)
return pts
def local(self, x):
"""Return the local coordinates (s, t, u) of the point x."""
r = _sub(x, self.x0)
return tuple(_dot(c, r) for c in self.covectors)
def deform(self, x):
"""Return the deformed position of x. Points outside the lattice do not move."""
r = _sub(x, self.x0)
s = _dot(self.covectors[0], r) # @line L1
t = _dot(self.covectors[1], r) # @line L2
u = _dot(self.covectors[2], r) # @line L3
if not (0 <= s <= 1 and 0 <= t <= 1 and 0 <= u <= 1): # @line L4
return tuple(x)
return self.evaluate(s, t, u)
def evaluate(self, s, t, u):
"""Return sum_ijk B_i^l(s) B_j^m(t) B_k^n(u) P[i][j][k]."""
l, m, n = self.deg
bs, bt, bu = bernstein(l, s), bernstein(m, t), bernstein(n, u)
out = [0 * s, 0 * s, 0 * s] # @line L5
for i in range(l + 1):
for j in range(m + 1):
wij = bs[i] * bt[j]
for k in range(n + 1): # @line L6
w = wij * bu[k]
p = self.points[i][j][k]
out[0] += w * p[0]
out[1] += w * p[1]
out[2] += w * p[2]
return tuple(out) # @line L7
def jacobian(self, x):
"""Return the 3 by 3 matrix dX'/dX at a point x inside the lattice.
By the chain rule, dX'/dX = (dX'/d(s, t, u)) (d(s, t, u)/dX), and the
second factor has the three covectors as its rows.
"""
l, m, n = self.deg
s, t, u = self.local(x)
bs, bt, bu = bernstein(l, s), bernstein(m, t), bernstein(n, u)
ds, dt, du = bernstein_derivative(l, s), bernstein_derivative(m, t), bernstein_derivative(n, u)
cols = [[0 * s] * 3 for _ in range(3)]
for i in range(l + 1):
for j in range(m + 1):
for k in range(n + 1):
p = self.points[i][j][k]
ws = (ds[i] * bt[j] * bu[k], bs[i] * dt[j] * bu[k], bs[i] * bt[j] * du[k])
for a in range(3):
for d in range(3):
cols[a][d] += ws[a] * p[d]
# cols[a] is the derivative by the local coordinate a.
return [[sum(cols[a][r] * self.covectors[a][c] for a in range(3)) for c in range(3)] for r in range(3)]
def _frac(i, n):
"""Return i / n as a Fraction; a degree of 0 gives 0."""
if n == 0:
return Fraction(0)
return Fraction(i, n)
def _div(x, d):
"""Return x / d, as an exact Fraction when both are int."""
if isinstance(x, int) and isinstance(d, int):
return Fraction(x, d)
return x / d
def det3(a):
"""Return the determinant of a 3 by 3 matrix."""
return (
a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1])
- a[0][1] * (a[1][0] * a[2][2] - a[1][2] * a[2][0])
+ a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0])
)
def de_casteljau_volume(points, s, t, u):
"""Evaluate the same trivariate Bezier volume by repeated linear interpolation.
This is an independent second method, used by the tests.
"""
def lerp_list(pts, x):
while len(pts) > 1:
pts = [tuple((1 - x) * a + x * b for a, b in zip(p, q)) for p, q in zip(pts, pts[1:])]
return pts[0]
rows = []
for plane in points:
cols = [lerp_list(list(row), u) for row in plane]
rows.append(lerp_list(cols, t))
return lerp_list(rows, s)
test_free_form_deformation.py 121 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/free-form-deformation/. The authors are listed in prov/5QACBN.jsonl.
import random
from fractions import Fraction as F
from free_form_deformation import Lattice, bernstein, de_casteljau_volume, det3
def unit_lattice(l=2, m=2, n=2):
return Lattice((0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), l, m, n)
def skew_lattice(l, m, n):
return Lattice((1, -2, 3), (2, 1, 0), (F(1, 2), 3, 1), (0, 1, 4), l, m, n)
def rand_point_inside(lat, rng):
s, t, u = (F(rng.randint(0, 64), 64) for _ in range(3))
a, b, c = lat.axes
return tuple(lat.x0[d] + s * a[d] + t * b[d] + u * c[d] for d in range(3))
def test_bernstein_partition_of_unity():
for deg in range(6):
for x in (F(0), F(1, 3), F(5, 7), F(1)):
assert sum(bernstein(deg, x)) == 1
def test_local_coordinates_are_inverse_of_embedding():
lat = skew_lattice(3, 2, 1)
rng = random.Random(1)
for _ in range(20):
s, t, u = (F(rng.randint(-10, 10), 7) for _ in range(3))
a, b, c = lat.axes
x = tuple(lat.x0[d] + s * a[d] + t * b[d] + u * c[d] for d in range(3))
assert lat.local(x) == (s, t, u)
def test_rest_lattice_is_exact_identity():
rng = random.Random(2)
for deg in ((1, 1, 1), (2, 2, 2), (3, 2, 1), (4, 1, 3)):
lat = skew_lattice(*deg)
for _ in range(10):
x = rand_point_inside(lat, rng)
assert lat.deform(x) == x
def test_affine_motion_of_lattice_is_reproduced_exactly():
rng = random.Random(3)
a = [[F(rng.randint(-5, 5), 3) for _ in range(3)] for _ in range(3)]
b = [F(rng.randint(-5, 5), 2) for _ in range(3)]
def aff(p):
return tuple(sum(a[r][c] * p[c] for c in range(3)) + b[r] for r in range(3))
lat = skew_lattice(3, 2, 2)
lat.points = [[[aff(p) for p in row] for row in plane] for plane in lat.points]
for _ in range(10):
x = rand_point_inside(lat, rng)
assert lat.deform(x) == aff(x)
def test_matches_de_casteljau():
rng = random.Random(4)
lat = unit_lattice(3, 2, 4)
lat.points = [[[tuple(F(rng.randint(-20, 20), 5) for _ in range(3)) for _ in row] for row in plane] for plane in lat.points]
for _ in range(10):
s, t, u = (F(rng.randint(0, 30), 30) for _ in range(3))
assert lat.evaluate(s, t, u) == de_casteljau_volume(lat.points, s, t, u)
def test_worked_example_center_point():
lat = unit_lattice(2, 2, 2)
lat.points[1][1][1] = (F(1, 2), F(1, 2), F(1))
y = lat.deform((F(1, 2), F(1, 2), F(1, 2)))
assert y == (F(1, 2), F(1, 2), F(9, 16))
assert all(isinstance(v, F) for v in y)
assert lat.deform((F(1, 4), F(1, 4), F(1, 4))) == (F(1, 4), F(1, 4), F(283, 1024))
# Below the center the lattice is stretched along z: det J = 9/8 at (1/2, 1/2, 1/4).
assert det3(lat.jacobian((F(1, 2), F(1, 2), F(1, 4)))) == F(9, 8)
# The corner points of the lattice do not move.
assert lat.deform((0, 0, 0)) == (0, 0, 0)
assert lat.deform((1, 1, 1)) == (1, 1, 1)
def test_point_outside_does_not_move():
lat = unit_lattice()
lat.points[1][1][1] = (F(1, 2), F(1, 2), F(3))
for x in ((F(-1, 10), F(1, 2), F(1, 2)), (2, 0, 0), (F(1, 2), F(1, 2), F(11, 10))):
assert lat.deform(x) == x
def test_jacobian_matches_finite_differences():
rng = random.Random(5)
lat = skew_lattice(2, 3, 2)
lat.points = [[[tuple(float(v) + rng.uniform(-0.3, 0.3) for v in p) for p in row] for row in plane] for plane in lat.points]
h = 1e-6
for _ in range(5):
x = tuple(float(v) for v in rand_point_inside(lat, rng))
s, t, u = lat.local(x)
if not all(0.01 < c < 0.99 for c in (s, t, u)):
continue
j = lat.jacobian(x)
for c in range(3):
xp = list(x)
xm = list(x)
xp[c] += h
xm[c] -= h
fp, fm = lat.deform(xp), lat.deform(xm)
for r in range(3):
assert abs((fp[r] - fm[r]) / (2 * h) - j[r][c]) < 1e-6
def test_rest_jacobian_is_identity_and_affine_det_is_volume_ratio():
lat = skew_lattice(2, 2, 2)
x = rand_point_inside(lat, random.Random(6))
j = lat.jacobian(x)
assert j == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
scale = (2, 3, F(1, 2))
lat.points = [[[tuple(scale[d] * p[d] for d in range(3)) for p in row] for row in plane] for plane in lat.points]
assert det3(lat.jacobian(x)) == 3
History
Sederberg and Parry presented FFD at SIGGRAPH 1986 as a tool for solid modeling. They took the lattice from Bezier volumes and showed that the method keeps the algebraic form of the object (a deformed polynomial surface stays polynomial), and that the volume change is the Jacobian determinant. Barr's global deformations of 1984 were the earlier closed-form space deformations. Coquillart (1990) extended the lattice to other shapes, and later work moved from lattices to cages and to surface-based methods.
References
- Thomas W. Sederberg and Scott R. Parry, Free-form deformation of solid geometric models, SIGGRAPH 86, Computer Graphics 20(4), 151-160, 1986, the whole paper (the parallelepiped lattice, the local coordinates, the trivariate Bernstein polynomial, the volume change and the continuity across the lattice boundary)
- Sabine Coquillart, Extended free-form deformation: a sculpturing tool for 3D geometric modeling, SIGGRAPH 90, Computer Graphics 24(4), 187-196, 1990, the whole paper (lattices that are not parallelepipeds)
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 | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 5QACBN Edit this page History Report an error Markdown