Algorithm Search⌘K

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.

Time
Θ(N g) all
Space
O(1) extra
Where
N = the number of points; g = the number of operations in the chain
Properties
deterministicexactonlineiterative

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 JJ has one too, and normal vectors move with the cofactor matrix of JJ, which is det⁡(J)J−T\det(J)J^{-\mathsf T}. 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 p=(x,y,z)p = (x, y, z) with a normal vector nn, and a chain of operations, each a smooth map F:R3→R3F: \mathbb{R}^3 \to \mathbb{R}^3 with its Jacobian JFJ_F. The output is the moved point and its new unit normal. Barr's three maps, along the zz axis for taper and twist and along the yy axis for bend:

  • Taper with a scale r(z)r(z): F(x,y,z)=(r(z)x,r(z)y,z)F(x, y, z) = (r(z)x, r(z)y, z), with J=(r0r′x0rr′y001)J = \begin{pmatrix} r & 0 & r'x \\ 0 & r & r'y \\ 0 & 0 & 1 \end{pmatrix} and det⁡J=r2\det J = r^2.
  • Twist with an angle θ(z)\theta(z): F=(xcos⁡θ−ysin⁡θ,  xsin⁡θ+ycos⁡θ,  z)F = (x\cos\theta - y\sin\theta,\; x\sin\theta + y\cos\theta,\; z), with det⁡J=1\det J = 1.
  • Bend with the rate kk, the center y0y_0 and the region [ymin⁡,ymax⁡][y_{\min}, y_{\max}]: let y^\hat y be yy clamped to the region, θ=k(y^−y0)\theta = k(\hat y - y_0), C=cos⁡θC = \cos\theta and S=sin⁡θS = \sin\theta. Inside the region, F=(x,  −S(z−1/k)+y0,  C(z−1/k)+1/k)F = (x,\; -S(z - 1/k) + y_0,\; C(z - 1/k) + 1/k). Outside it, the part beyond the region continues along the tangent: add C(y−y^)C(y - \hat y) to the second coordinate and S(y−y^)S(y - \hat y) to the third. det⁡J=1−kz\det J = 1 - kz inside and 1 outside.

The algorithm

cof⁡(J)\operatorname{cof}(J) is the cofactor matrix of JJ: its entry (i,j)(i, j) is (−1)i+j(-1)^{i+j} times the minor of JJ without row ii and column jj.

  1. BARR(p, n, ops)
  2. 1J ← I
  3. 2for each operation F in ops, in order do
  4. 3J ← J_F(p) J
  5. 4p ← F(p)
  6. 5m ← cof(J) n
  7. 6return p, m / |m|

Line L3 evaluates the Jacobian of each operation at the current point, before the point moves, so JJ is the Jacobian of the whole chain by the chain rule. Line L5 moves the normal; for an invertible JJ, cof⁡(J)=det⁡(J)J−T\operatorname{cof}(J) = \det(J)J^{-\mathsf T}, so after line L6 this is the inverse transpose rule, with the sign of det⁡J\det J kept.

Example

Taper. With r(z)=1−0.4zr(z) = 1 - 0.4z, the point (1,0,1)(1, 0, 1) on the side of the unit cylinder moves to (0.6,0,1)(0.6, 0, 1), and det⁡J=0.36\det J = 0.36. Its normal (1,0,0)(1, 0, 0) becomes cof⁡(J)(1,0,0)∝(1,0,0.4)\operatorname{cof}(J)(1, 0, 0) \propto (1, 0, 0.4), normalized (0.9285,0,0.3714)(0.9285, 0, 0.3714): the tapered side leans in, so its normal tilts up. The naive rule Jn=(0.6,0,0)Jn = (0.6, 0, 0) keeps the normal horizontal, which is wrong: the tangent (−0.4,0,1)(-0.4, 0, 1) of the tapered side is not perpendicular to it.

Twist. With θ(z)=π2z\theta(z) = \frac{\pi}{2}z, the point (1,0,1)(1, 0, 1) turns to (0,1,1)(0, 1, 1) and its normal (1,0,0)(1, 0, 0) to (0,1,0)(0, 1, 0); points at z=0z = 0 do not move.

Bend. With k=0.5k = 0.5, y0=0y_0 = 0 and the region [−2,2][-2, 2], the point (0,1,0)(0, 1, 0) has θ=0.5\theta = 0.5 and moves to (0,2sin⁡0.5,2−2cos⁡0.5)=(0,0.9589,0.2448)(0, 2\sin 0.5, 2 - 2\cos 0.5) = (0, 0.9589, 0.2448), on the circle of radius 1/k=21/k = 2 about (y0,1/k)=(0,2)(y_0, 1/k) = (0, 2). At (0,1,0.5)(0, 1, 0.5), det⁡J=1−0.5⋅0.5=0.75\det J = 1 - 0.5 \cdot 0.5 = 0.75: the inner side of the bend is compressed. The point (0,3,0)(0, 3, 0) is beyond the region and moves along the tangent at the end of the arc to (0,2.2232,1.7609)(0, 2.2232, 1.7609).

Correctness

By the chain rule, after line L3 has run for the first ii operations, JJ is the Jacobian of their composition at the original point. A normal at pp is a×ba \times b for two tangent vectors aa, bb of the surface; the map sends them to JaJa and JbJb, and the identity (Ja)×(Jb)=cof⁡(J)(a×b)(Ja) \times (Jb) = \operatorname{cof}(J)(a \times b) 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 y,zy, z block is (C(1−kz)−SS(1−kz)C)\begin{pmatrix} C(1 - kz) & -S \\ S(1 - kz) & C \end{pmatrix}, with the determinant 1−kz1 - kz. 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 pp has the tangent vectors aa and bb, and its normal is a positive multiple of a×ba \times b. A smooth map FF sends the tangent vectors to JF(p)aJ_F(p)a and JF(p)bJ_F(p)b, the tangent vectors of the moved surface at F(p)F(p).

The chain

Lemma 1. After line L3 and line L4 have run for the operations F1,…,FiF_1, \dots, F_i, JJ is the Jacobian of Fi∘⋯∘F1F_i \circ \dots \circ F_1 at the original point p0p_0, and p=Fi(⋯F1(p0))p = F_i(\cdots F_1(p_0)).

Proof. By induction on ii. For i=0i = 0, J=IJ = I and p=p0p = p_0. Line L3 multiplies by JFi+1J_{F_{i+1}} at the current p=Fi(⋯F1(p0))p = F_i(\cdots F_1(p_0)), which is the chain rule D(Fi+1∘G)(p0)=DFi+1(G(p0)) DG(p0)D(F_{i+1} \circ G)(p_0) = DF_{i+1}(G(p_0))\,DG(p_0); then line L4 moves pp. □\square

The cofactor rule

Lemma 2. For every 3 by 3 matrix JJ and all vectors aa, bb: (Ja)×(Jb)=cof⁡(J)(a×b)(Ja) \times (Jb) = \operatorname{cof}(J)(a \times b). If JJ is invertible, cof⁡(J)=det⁡(J)J−T\operatorname{cof}(J) = \det(J)J^{-\mathsf T}.

Proof. For any vector cc, c⋅((Ja)×(Jb))=det⁡[c,Ja,Jb]c \cdot ((Ja) \times (Jb)) = \det[c, Ja, Jb]. Write c=Jdc = Jd when JJ is invertible: then det⁡[Jd,Ja,Jb]=det⁡(J)det⁡[d,a,b]=det⁡(J) d⋅(a×b)=det⁡(J) (J−1c)⋅(a×b)=c⋅det⁡(J)J−T(a×b)\det[Jd, Ja, Jb] = \det(J)\det[d, a, b] = \det(J)\,d \cdot (a \times b) = \det(J)\,(J^{-1}c) \cdot (a \times b) = c \cdot \det(J)J^{-\mathsf T}(a \times b). This holds for every cc, so (Ja)×(Jb)=det⁡(J)J−T(a×b)(Ja) \times (Jb) = \det(J)J^{-\mathsf T}(a \times b). The adjugate formula J−1=adj⁡(J)/det⁡JJ^{-1} = \operatorname{adj}(J)/\det J with adj⁡(J)=cof⁡(J)T\operatorname{adj}(J) = \operatorname{cof}(J)^{\mathsf T} gives det⁡(J)J−T=cof⁡(J)\det(J)J^{-\mathsf T} = \operatorname{cof}(J). Both sides of the identity are polynomials in the entries of JJ, and invertible matrices are dense, so it holds for every JJ. □\square

Theorem 1. Line L6 returns a unit normal of the moved surface at the moved point, on the side of (Ja)×(Jb)(Ja) \times (Jb), whenever cof⁡(J)n≠0\operatorname{cof}(J)n \ne 0.

Proof. n=λ(a×b)n = \lambda(a \times b) with λ>0\lambda > 0, so by Lemma 1 and Lemma 2, m=λ(Ja)×(Jb)m = \lambda(Ja) \times (Jb), which is perpendicular to both moved tangent vectors and on the side of their cross product. □\square

If det⁡J<0\det J < 0, J−TnJ^{-\mathsf T}n is −m/∣det⁡J∣-m/|\det J| and points to the other side; this is the orientation pitfall of the page.

The three Jacobians

Taper. F=(rx,ry,z)F = (rx, ry, z) with r=r(z)r = r(z). The partial derivatives give the rows (r,0,r′x)(r, 0, r'x), (0,r,r′y)(0, r, r'y) and (0,0,1)(0, 0, 1). The matrix is upper triangular, so det⁡J=r2\det J = r^2.

Twist. F=(xcos⁡θ−ysin⁡θ,xsin⁡θ+ycos⁡θ,z)F = (x\cos\theta - y\sin\theta, x\sin\theta + y\cos\theta, z) with θ=θ(z)\theta = \theta(z). The derivatives by xx and yy give the rotation block (cos⁡θ−sin⁡θsin⁡θcos⁡θ)\begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix}, the derivative by zz gives the third column θ′(−xsin⁡θ−ycos⁡θ,  xcos⁡θ−ysin⁡θ,  1)\theta'(-x\sin\theta - y\cos\theta,\; x\cos\theta - y\sin\theta,\; 1), and the third row is (0,0,1)(0, 0, 1). Expanding along the third row gives det⁡J=cos⁡2θ+sin⁡2θ=1\det J = \cos^2\theta + \sin^2\theta = 1.

Bend inside the region. Here y^=y\hat y = y, so θ=k(y−y0)\theta = k(y - y_0) and dθ/dy=kd\theta/dy = k. With Y=−S(z−1/k)+y0Y = -S(z - 1/k) + y_0 and Z=C(z−1/k)+1/kZ = C(z - 1/k) + 1/k: ∂Y/∂y=−Ck(z−1/k)=C(1−kz)\partial Y/\partial y = -Ck(z - 1/k) = C(1 - kz), ∂Y/∂z=−S\partial Y/\partial z = -S, ∂Z/∂y=−Sk(z−1/k)=S(1−kz)\partial Z/\partial y = -Sk(z - 1/k) = S(1 - kz), ∂Z/∂z=C\partial Z/\partial z = C, and X=xX = x. The determinant is C2(1−kz)+S2(1−kz)=1−kzC^2(1 - kz) + S^2(1 - kz) = 1 - kz.

Bend outside the region. Here y^\hat y is constant, so θ\theta is constant, and YY, ZZ gain the terms C(y−y^)C(y - \hat y) and S(y−y^)S(y - \hat y). Then ∂Y/∂y=C\partial Y/\partial y = C, ∂Z/∂y=S\partial Z/\partial y = S, ∂Y/∂z=−S\partial Y/\partial z = -S, ∂Z/∂z=C\partial Z/\partial z = C: a rotation block, with the determinant 1.

The bend at the ends of the region

Theorem 2. The bend is continuous at y=ymax⁡y = y_{\max} and at y=ymin⁡y = y_{\min}. Its Jacobian is continuous there only on the plane z=0z = 0.

Proof. At y=ymax⁡y = y_{\max}, both formulas use y^=ymax⁡\hat y = y_{\max}, so they have the same θ\theta, and the outside terms C(y−y^)C(y - \hat y), S(y−y^)S(y - \hat y) are 0. So the two sides agree. The ∂/∂y\partial/\partial y column is (C(1−kz),S(1−kz))(C(1 - kz), S(1 - kz)) inside and (C,S)(C, S) outside, which agree only if kz=0kz = 0, that is z=0z = 0 for k≠0k \ne 0. The same holds at ymin⁡y_{\min}. □\square

On the line z=0z = 0 inside the region, F=(x,S/k+y0,(1−C)/k)F = (x, S/k + y_0, (1 - C)/k), which is the circle of radius 1/k1/k about (y0,1/k)(y_0, 1/k), with the arc length ∣y−y0∣|y - y_0| from the center point: the axis bends without stretch.

Complexity

Each operation costs O(1)O(1) per point, with a 3 by 3 matrix product for line L3. A chain of gg operations on NN points costs Θ(Ng)\Theta(Ng) time. Line L5 and the normalization cost O(1)O(1) per point. The algorithm needs O(1)O(1) 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 det⁡J\det J: r(z)2r(z)^2 for a taper, 1 for a twist, so a twist keeps the volume, and 1−kz1 - kz inside a bend.
  • Fold (proved). Inside a bend, points with z>1/kz > 1/k have det⁡J<0\det J < 0: the far side of the bend axis is turned inside out. Keep the object below z=1/kz = 1/k.
  • Bend is only C0C^0 at the region ends (proved). Positions are continuous at ymin⁡y_{\min} and ymax⁡y_{\max}, but the Jacobian jumps there unless z=0z = 0, because det⁡J\det J changes from 1−kz1 - kz to 1.
  • Order matters. A taper and a twist along the same axis commute, because a scale in xyxy commutes with a rotation in xyxy; 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. r(z)r(z) and θ(z)\theta(z) can be any smooth functions, such as splines, and a taper can scale xx and yy 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 JJ. Moving a normal by JJ instead of cof⁡(J)\operatorname{cof}(J) or J−TJ^{-\mathsf T} tilts it wrongly under any taper or bend.
  • Lost orientation. J−TnJ^{-\mathsf T}n points the wrong way where det⁡J<0\det J < 0; cof⁡(J)n\operatorname{cof}(J)n keeps the side of the moved tangent frame.
  • Jacobian at the wrong point. In a chain, each JFJ_F 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, z>1/kz > 1/k, fold over; tight bends of thick objects need kk small enough.
  • Region ends. The kink in the derivative at ymin⁡y_{\min} and ymax⁡y_{\max} shows as a crease in the shading of a thick object; spread the bend over a larger region or blend the rate kk 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.

barr_global_deformations.pyI0RawGitHub
# 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

  1. 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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.all
  • a full proof of space.extra
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
  • History with a source
  • at least three sources
  • a scene preset for the example