Algorithmintro
Barr's global deformations
Also called twist bend taper deformations.
Deform a solid by a transform whose matrix varies along an axis, giving tapering, twisting and bending, and transform normals by the inverse transpose Jacobian.
- Solves
- Shape deformation
- Time
- Θ(N g) all
- Space
- O(1) extra
- Where
- N = the number of points; g = the number of operations in the chain
- Model
- Real RAM
- Properties
- deterministicexactonlineiterative
- Evidence
- Proof sketchCode in 1 languages
Barr's global deformations are three simple maps of space: taper, twist and bend. Each one changes a solid by a transform that varies smoothly along one axis: a scale that depends on the height, a rotation whose angle depends on the height, or a bend of the axis onto a circle. Because each map has a closed form, its Jacobian matrix has one too, and normal vectors move with the cofactor matrix of , which is . Alan Barr (1984) introduced them, and they are still the twist, bend and taper tools of modeling programs. They need no lattice, no handles and no solve; a chain of them builds a horn or a twisted column from a cylinder.
In plain words
Take a straight bar of soft clay. Squeeze the top end thinner than the bottom: that is a taper. Turn the top end while you hold the bottom: that is a twist. Curl the bar around a can: that is a bend. Each of these acts on every point of the bar by a simple rule that depends only on how high the point is. Barr wrote each rule as a formula, and also the formula for how a small arrow on the surface changes. That second formula tells the renderer which way the bent surface faces, so the shading comes out right.
The problem
The input is a point with a normal vector , and a chain of operations, each a smooth map with its Jacobian . The output is the moved point and its new unit normal. Barr's three maps, along the axis for taper and twist and along the axis for bend:
- Taper with a scale : , with and .
- Twist with an angle : , with .
- Bend with the rate , the center and the region : let be clamped to the region, , and . Inside the region, . Outside it, the part beyond the region continues along the tangent: add to the second coordinate and to the third. inside and 1 outside.
The algorithm
is the cofactor matrix of : its entry is times the minor of without row and column .
BARR(p, n, ops)- 1
J ← I - 2
for each operation F in ops, in order do - 3
J ← J_F(p) J - 4
p ← F(p) - 5
m ← cof(J) n - 6
return p, m / |m|
Line L3 evaluates the Jacobian of each operation at the current point, before the point moves, so is the Jacobian of the whole chain by the chain rule. Line L5 moves the normal; for an invertible , , so after line L6 this is the inverse transpose rule, with the sign of kept.
Example
Taper. With , the point on the side of the unit cylinder moves to , and . Its normal becomes , normalized : the tapered side leans in, so its normal tilts up. The naive rule keeps the normal horizontal, which is wrong: the tangent of the tapered side is not perpendicular to it.
Twist. With , the point turns to and its normal to ; points at do not move.
Bend. With , and the region , the point has and moves to , on the circle of radius about . At , : the inner side of the bend is compressed. The point is beyond the region and moves along the tangent at the end of the arc to .
Correctness
By the chain rule, after line L3 has run for the first operations, is the Jacobian of their composition at the original point. A normal at is for two tangent vectors , of the surface; the map sends them to and , and the identity holds for every 3 by 3 matrix. So line L5 gives a vector perpendicular to the moved surface, with the orientation of the moved tangent frame. The determinants of the three maps follow from their Jacobians; for the bend inside the region the block is , with the determinant . The proof derives these and shows that the bend is continuous at the ends of the region.
Proof: The cofactor rule gives the normals of Barr's deformations, and the taper, twist and bend have the stated Jacobiansfull
We use the notation of the page. A surface through has the tangent vectors and , and its normal is a positive multiple of . A smooth map sends the tangent vectors to and , the tangent vectors of the moved surface at .
The chain
Lemma 1. After line L3 and line L4 have run for the operations , is the Jacobian of at the original point , and .
Proof. By induction on . For , and . Line L3 multiplies by at the current , which is the chain rule ; then line L4 moves .
The cofactor rule
Lemma 2. For every 3 by 3 matrix and all vectors , : . If is invertible, .
Proof. For any vector , . Write when is invertible: then . This holds for every , so . The adjugate formula with gives . Both sides of the identity are polynomials in the entries of , and invertible matrices are dense, so it holds for every .
Theorem 1. Line L6 returns a unit normal of the moved surface at the moved point, on the side of , whenever .
Proof. with , so by Lemma 1 and Lemma 2, , which is perpendicular to both moved tangent vectors and on the side of their cross product.
If , is and points to the other side; this is the orientation pitfall of the page.
The three Jacobians
Taper. with . The partial derivatives give the rows , and . The matrix is upper triangular, so .
Twist. with . The derivatives by and give the rotation block , the derivative by gives the third column , and the third row is . Expanding along the third row gives .
Bend inside the region. Here , so and . With and : , , , , and . The determinant is .
Bend outside the region. Here is constant, so is constant, and , gain the terms and . Then , , , : a rotation block, with the determinant 1.
The bend at the ends of the region
Theorem 2. The bend is continuous at and at . Its Jacobian is continuous there only on the plane .
Proof. At , both formulas use , so they have the same , and the outside terms , are 0. So the two sides agree. The column is inside and outside, which agree only if , that is for . The same holds at .
On the line inside the region, , which is the circle of radius about , with the arc length from the center point: the axis bends without stretch.
Complexity
Each operation costs per point, with a 3 by 3 matrix product for line L3. A chain of operations on points costs time. Line L5 and the normalization cost per point. The algorithm needs memory beyond the points, because each point is processed on its own.
Properties
- Normals are exact (proved). The cofactor rule gives a normal of the moved surface for any smooth map, with no approximation.
- Volume (proved). The local volume ratio is : for a taper, 1 for a twist, so a twist keeps the volume, and inside a bend.
- Fold (proved). Inside a bend, points with have : the far side of the bend axis is turned inside out. Keep the object below .
- Bend is only at the region ends (proved). Positions are continuous at and , but the Jacobian jumps there unless , because changes from to 1.
- Order matters. A taper and a twist along the same axis commute, because a scale in commutes with a rotation in ; a bend and a twist do not.
- Smooth, not local. Each map acts on all of space, so it changes the whole object, not one part.
Variants and improvements
- Other profiles. and can be any smooth functions, such as splines, and a taper can scale and differently.
- Local deformations. Barr's paper also covers deformations that act only on part of an object; a bend with a finite region is the simplest case.
- Along a curve. Bending onto a general space curve uses a frame along the curve, such as the Frenet frame or a rotation-minimizing frame, in place of the circle; wires generalize this to deformations controlled by curves.
- Lattices. Free-form deformation gives arbitrary smooth maps at the cost of a lattice of control points.
When to use it
Use Barr's deformations for simple, global shape changes along one axis: a tapered column, a twisted ribbon, a bent tube, a horn from a cylinder. They are cheap and exact, and the normals come out right, so they are a good first modeling tool and a good test of a renderer's normal transform. For changes of a part of an object, or shapes driven by control points, use free-form deformation or cage-based deformation.
Pitfalls
- Normals by . Moving a normal by instead of or tilts it wrongly under any taper or bend.
- Lost orientation. points the wrong way where ; keeps the side of the moved tangent frame.
- Jacobian at the wrong point. In a chain, each must be taken at the point before that operation, as in line L3, not at the original point.
- Bend axis. Points beyond the bend radius, , fold over; tight bends of thick objects need small enough.
- Region ends. The kink in the derivative at and shows as a crease in the shading of a thick object; spread the bend over a larger region or blend the rate smoothly.
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/barr-global-deformations/. The authors are listed in prov/702PFN.jsonl.
"""Global deformations of Barr (1984): taper, twist and bend.
Each operation is a map F from R^3 to R^3 with a closed-form Jacobian J.
Points move by F. Normal vectors move by the cofactor matrix of J,
n' = cof(J) n = det(J) J^-T n, and are then normalized. A chain of
operations is applied left to right; the Jacobians multiply as
J <- J_op(p) J, with J_op taken at the current point p.
Each operation below is a function op(p) that returns (F(p), J(p)).
"""
import math
def mat_mul(a, b):
return [[sum(a[i][k] * b[k][j] for k in range(3)) for j in range(3)] for i in range(3)]
def mat_vec(a, v):
return tuple(sum(a[i][k] * v[k] for k in range(3)) for i in range(3))
def identity():
return [[1.0 if i == j else 0.0 for j in range(3)] for i in range(3)]
def det3(a):
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 cofactor(a):
"""Return the cofactor matrix of a, which equals det(a) a^-T when a is invertible."""
c = [[0.0] * 3 for _ in range(3)]
for i in range(3):
for j in range(3):
r = [x for x in range(3) if x != i]
s = [x for x in range(3) if x != j]
minor = a[r[0]][s[0]] * a[r[1]][s[1]] - a[r[0]][s[1]] * a[r[1]][s[0]]
c[i][j] = minor if (i + j) % 2 == 0 else -minor
return c
def taper(r, dr):
"""Taper along z: (x, y, z) -> (r(z) x, r(z) y, z). dr is the derivative r'."""
def op(p):
x, y, z = p
f, fp = r(z), dr(z)
return (f * x, f * y, z), [[f, 0.0, fp * x], [0.0, f, fp * y], [0.0, 0.0, 1.0]]
return op
def twist(theta, dtheta):
"""Twist about z: rotate the xy plane by the angle theta(z)."""
def op(p):
x, y, z = p
t, tp = theta(z), dtheta(z)
c, s = math.cos(t), math.sin(t)
out = (x * c - y * s, x * s + y * c, z)
jac = [[c, -s, (-x * s - y * c) * tp], [s, c, (x * c - y * s) * tp], [0.0, 0.0, 1.0]]
return out, jac
return op
def bend(k, y0, ymin, ymax):
"""Bend along y in the yz plane with rate k, center y0 and region [ymin, ymax].
Inside the region, the line z = 0 maps onto a circle of radius 1 / k.
Outside it, the object continues along the tangent at the region end.
"""
if k == 0:
raise ValueError("use the identity for k = 0")
if not ymin <= y0 <= ymax:
raise ValueError("the center must lie in the bend region")
def op(p):
x, y, z = p
yh = min(max(y, ymin), ymax)
th = k * (yh - y0)
c, s = math.cos(th), math.sin(th)
yy = -s * (z - 1.0 / k) + y0
zz = c * (z - 1.0 / k) + 1.0 / k
if ymin <= y <= ymax:
jac = [[1.0, 0.0, 0.0], [0.0, c * (1 - k * z), -s], [0.0, s * (1 - k * z), c]]
return (x, yy, zz), jac
d = y - yh
jac = [[1.0, 0.0, 0.0], [0.0, c, -s], [0.0, s, c]]
return (x, yy + c * d, zz + s * d), jac
return op
def deform(p, n, ops):
"""Apply the chain ops to the point p and the normal n. Return (p', n')."""
jac = identity() # @line L1
p = tuple(p)
for op in ops: # @line L2
q, j_op = op(p)
jac = mat_mul(j_op, jac) # @line L3
p = q # @line L4
m = mat_vec(cofactor(jac), n) # @line L5
length = math.sqrt(sum(v * v for v in m))
if length == 0:
raise ValueError("the deformation is singular at this point")
return p, tuple(v / length for v in m) # @line L6
def deform_point(p, ops):
"""Apply the chain ops to a point only."""
for op in ops:
p, _ = op(p)
return p
def jacobian(p, ops):
"""Return the Jacobian of the whole chain at p."""
jac = identity()
for op in ops:
p, j_op = op(p)
jac = mat_mul(j_op, jac)
return jac
test_barr_global_deformations.py 141 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/barr-global-deformations/. The authors are listed in prov/702PFN.jsonl.
import math
import random
from barr_global_deformations import bend, cofactor, deform, deform_point, det3, jacobian, mat_vec, taper, twist
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 close(a, b, eps=1e-9):
return all(abs(x - y) <= eps for x, y in zip(a, b))
def lin_taper():
return taper(lambda z: 1.0 - 0.4 * z, lambda z: -0.4)
def lin_twist():
return twist(lambda z: 0.8 * z, lambda z: 0.8)
def sample_ops():
return [
[lin_taper()],
[lin_twist()],
[bend(0.7, 0.0, -1.0, 1.0)],
[lin_taper(), lin_twist(), bend(0.5, 0.2, -0.5, 0.8)],
[twist(lambda z: math.sin(z), math.cos), taper(lambda z: 1 + 0.2 * z * z, lambda z: 0.4 * z)],
]
def fd_jacobian(p, ops, h=1e-6):
cols = []
for c in range(3):
a, b = list(p), list(p)
a[c] += h
b[c] -= h
fa, fb = deform_point(a, ops), deform_point(b, ops)
cols.append([(fa[r] - fb[r]) / (2 * h) for r in range(3)])
return [[cols[c][r] for c in range(3)] for r in range(3)]
def test_jacobian_matches_finite_differences():
rng = random.Random(21)
for ops in sample_ops():
for _ in range(10):
p = [rng.uniform(-1.5, 1.5) for _ in range(3)]
# Stay away from the bend region ends, where the Jacobian jumps.
if any(abs(p[1] - e) < 1e-3 for e in (-1.0, 1.0, -0.5, 0.8)):
continue
j, f = jacobian(p, ops), fd_jacobian(p, ops)
for r in range(3):
assert close(j[r], f[r], 1e-5)
def test_cofactor_maps_cross_products():
rng = random.Random(22)
for ops in sample_ops():
for _ in range(5):
p = [rng.uniform(-1, 1) for _ in range(3)]
j = jacobian(p, ops)
a = [rng.uniform(-1, 1) for _ in range(3)]
b = [rng.uniform(-1, 1) for _ in range(3)]
assert close(mat_vec(cofactor(j), cross(a, b)), cross(mat_vec(j, a), mat_vec(j, b)), 1e-12)
def test_normal_stays_perpendicular_to_moved_tangents():
rng = random.Random(23)
for ops in sample_ops():
for _ in range(5):
p = [rng.uniform(-1, 1) for _ in range(3)]
n = [rng.uniform(-1, 1) for _ in range(3)]
# Two tangent vectors perpendicular to n.
t1 = cross(n, (1.0, 0.3, -0.2))
t2 = cross(n, t1)
_, n2 = deform(p, n, ops)
j = jacobian(p, ops)
for t in (t1, t2):
jt = mat_vec(j, t)
assert abs(sum(x * y for x, y in zip(jt, n2))) < 1e-12
assert abs(math.sqrt(sum(v * v for v in n2)) - 1) < 1e-12
def test_determinants():
rng = random.Random(24)
for _ in range(10):
p = [rng.uniform(-1, 1) for _ in range(3)]
r = 1.0 - 0.4 * p[2]
assert abs(det3(jacobian(p, [lin_taper()])) - r * r) < 1e-12
assert abs(det3(jacobian(p, [lin_twist()])) - 1) < 1e-12
k = 0.7
inside = -1.0 <= p[1] <= 1.0
want = 1 - k * p[2] if inside else 1.0
assert abs(det3(jacobian(p, [bend(k, 0.0, -1.0, 1.0)])) - want) < 1e-12
p_out = [p[0], 1.5 + abs(p[1]), p[2]]
assert abs(det3(jacobian(p_out, [bend(k, 0.0, -1.0, 1.0)])) - 1) < 1e-12
def test_identity_parameters():
ops = [taper(lambda z: 1.0, lambda z: 0.0), twist(lambda z: 0.0, lambda z: 0.0)]
rng = random.Random(25)
for _ in range(10):
p = tuple(rng.uniform(-2, 2) for _ in range(3))
n = (0.0, 0.6, 0.8)
q, m = deform(p, n, ops)
assert close(q, p) and close(m, n)
def test_bend_maps_axis_onto_circle_and_is_continuous():
k, y0 = 0.5, 0.0
op = [bend(k, y0, -2.0, 2.0)]
for y in (-2.0, -1.0, 0.0, 0.7, 2.0):
q = deform_point((0.0, y, 0.0), op)
# The line z = 0 lands on the circle of radius 1/k about (y0, 1/k).
assert abs(math.hypot(q[1] - y0, q[2] - 1 / k) - 1 / k) < 1e-12
for e in (-2.0, 2.0):
a = deform_point((0.1, e - 1e-9, 0.3), op)
b = deform_point((0.1, e + 1e-9, 0.3), op)
assert math.dist(a, b) < 1e-8
def test_bend_jacobian_jumps_at_region_end_off_axis():
op = [bend(0.5, 0.0, -2.0, 2.0)]
z = 0.4
ja = jacobian((0.0, 2.0, z), op)
jb = jacobian((0.0, 2.0 + 1e-9, z), op)
assert abs(det3(ja) - (1 - 0.5 * z)) < 1e-12
assert abs(det3(jb) - 1) < 1e-12
def test_order_of_operations():
tw = twist(lambda z: 0.8 * z, lambda z: 0.8)
tp = taper(lambda z: 1.0 - 0.4 * z, lambda z: -0.4)
# A z-dependent taper and twist commute; a bend and a twist do not.
p = (0.3, 0.4, 0.9)
assert close(deform_point(p, [tp, tw]), deform_point(p, [tw, tp]), 1e-12)
b = bend(0.6, 0.0, -1.0, 1.0)
assert math.dist(deform_point(p, [b, tw]), deform_point(p, [tw, b])) > 1e-2
History
Barr presented global and local deformations at SIGGRAPH 1984, as hierarchical operations for solid modeling with primitives such as superquadrics. He gave the taper, twist and bend with their Jacobians, and the rule for moving normal vectors, so that deformed implicit and parametric surfaces could be rendered and ray traced. Free-form deformation (Sederberg and Parry 1986) generalized the idea from closed-form maps to lattices of control points.
References
- Alan H. Barr, Global and local deformations of solid primitives, SIGGRAPH 84, Computer Graphics 18(3), 21-30, 1984, the whole paper (tapering, twisting and bending along an axis, the Jacobian matrices of these maps, and the transformation of normal vectors)
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 702PFN Edit this page History Report an error Markdown