Algorithmadvanced
Wires deformation
Also called wire deformer.
Deform a surface by moving space curves, where each point follows the nearest point of a wire with a falloff in distance.
- Solves
- Shape deformation
- Time
- Θ(N w V) allΘ(V) per_point_per_wire
- Space
- O(w) extra
- Where
- N = the number of points; V = the number of vertices of one wire polyline; w = the number of wires
- Model
- Real RAM
- Properties
- deterministicexactonlineiterative
- Evidence
- Proof sketchCode in 1 languages
Wires deformation moves the points of an object with space curves. Each wire is a pair of curves with the same parameter: the reference curve , which says where the wire was when the object was bound to it, and the wire curve , which the user moves. A point near finds its closest point and then follows at the same parameter: it is scaled about , turned with the tangent, and moved by , each by an amount that falls from 1 on the curve to 0 at the radius of influence. Karan Singh and Eugene Fiume (1998) introduced the method for modeling and animation; creases, lips and brows follow curves better than lattices. This page uses polylines for and ; smooth curves such as NURBS work in the same way with a numerical closest point.
In plain words
Lay a piece of string on a soft surface, and draw a ring around the string to say how far its pull reaches. Now move the string. A point of the surface right under the string goes with it all the way. A point a little away from the string goes part of the way, and a point outside the ring does not move at all. To know which part of the string pulls it, each point looks for the closest place on the string in its starting position. If the string turns, the points near it turn with it, and if you ask for it, they also spread away from the string or pull in toward it.
The problem
The input is a point and a wire: the reference polyline and the wire polyline with the same number of vertices, the radius , the scale , and the falloff with and for . The parameter with means the point at the fraction of segment , so and correspond. The output is the moved point. The implementation uses for ; it has and , so the move is smooth at the curve and at the radius. Any falloff with the properties above can be passed in.
The algorithm
and are the unit tangents of the segments that hold .
WIRE(P, R, W, r, s, f)- 1
u ← the parameter of the point of R closest to P - 2
F ← f(|P - R(u)| / r) - 3
if F = 0 then return P - 4
Q ← R(u) + (1 + (s - 1)F)(P - R(u)) - 5
a ← R'(u) × W'(u) / |R'(u) × W'(u)|; θ ← the angle from R'(u) to W'(u) - 6
Q ← R(u) + rot(a, Fθ)(Q - R(u)) - 7
return Q + F(W(u) - R(u))
Line L1 is a closest point search; on a polyline it projects onto each segment and keeps the nearest, and on a smooth curve it is a Newton iteration such as Newton point inversion. Line L4 scales the offset from the curve: with points spread from the wire, with they gather to it, and with nothing changes. Lines L5 and L6 turn the offset by the fraction of the turn of the tangent, with Rodrigues' formula for rot; if the tangents are parallel the step does nothing. Line L7 adds the fraction of the move of the curve point. For several wires, the implementation computes the move of each wire alone and returns , so with the moves are averaged and with a large the largest move wins.
Example
Let be the straight polyline from to , the same polyline moved by , and , . The point has the closest point at the distance , so . The tangents agree, so lines L5 and L6 do nothing, and line L7 gives . With , line L4 first moves the offset to , and the result is . The point is farther than and does not move.
For the turn, let go from to and from to , a turn of 90 degrees about . With a very large , , and the point turns with the tangent to , while lies on the axis of the turn and stays.
Correctness
The method is a construction, so correctness means that it has the properties a user expects of it. These follow from the lines of the algorithm, and the proof gives them in full. If and , line L4 keeps , line L6 turns by 0, and line L7 adds 0, so every point stays. A point at the distance or more has and stays, by line L3. A point on has and the offset 0, so it goes to : the object follows the wire exactly along the curve. The turn at line L6 keeps the length of the offset, so with the distance of the moved point from is the distance of from . As , , and every step tends to the identity, so the move is continuous across the boundary of the region of influence.
Proof: Wires deformation is the identity at rest, moves only points near the reference curve, interpolates the wire, and is continuous at the radius of influencefull
We use the notation of the page. Write for the closest point of line L1, , and for the result of the algorithm. Let be the factor of line L4 and the turn rot of line L6, or the identity if the tangents are parallel. Then, for ,
The closest point
Lemma 1. On a segment from to with , the point closest to is with the value of clamped to .
Proof. is a quadratic in with the leading coefficient and its minimum at the unclamped value. On a convex quadratic has its minimum at the clamped value.
Taking the nearest of the segment points gives a closest point of the polyline, so is the distance from to .
Four properties
Theorem 1 (identity at rest). If and , then for every .
Proof. If , line L3 returns . Otherwise , the two tangents are the same, so is not defined and is the identity, and . So .
Theorem 2 (locality). If , then .
Proof. , so , and line L3 returns .
Theorem 3 (interpolation). If lies on , then .
Proof. , so and . The offset is 0, so , and .
Theorem 4 (continuity at the radius). Let with and , and let be continuous with . Then .
Proof. The distance to is continuous in , so . Then and , with , so uniformly. The offsets are bounded by and the moves are bounded by the largest vertex move, because is piecewise linear in . So , and .
The proof does not need to converge, so it holds even where the closest point is not unique.
The size of the offset
Theorem 5. For , . In particular, with the moved point keeps its distance from the moved curve point .
Proof. is a rotation, so , and because and .
Where the map jumps
Let have two closest points and with , and let and the tangents be unchanged. Points close to on either side of the set of such points bind to or to , and their moves tend to and , which differ when . So the map is discontinuous there, which is the medial jump of the Pitfalls. The same argument with the turn in place of the move applies to a point whose closest point is an inner vertex of a polyline, where and change. Away from these sets, and the tangents depend continuously on , and so does .
Complexity
Line L1 on a polyline with vertices projects onto segments, in time; lines L2 to L7 take . So points and wires cost time. The closest parameter depends only on , so it can be computed once per point and wire and kept while moves, which makes each later frame . A spatial grid over the segments of makes line L1 much faster for long wires.
Properties
- Identity at rest (proved). with moves no point.
- Local (proved). Only points within the distance of move.
- Interpolates the wire (proved). A point on goes to the matching point of .
- Continuous at the radius (proved). The move tends to 0 at the boundary of the region of influence.
- Not continuous everywhere (proved). Where a point has two closest points on , as on the inside of a tight bend, jumps, and so can the move. A polyline also has a jump in its tangent at each inner vertex, so the turn of line L6 jumps for points whose closest point is that vertex.
- Independent of the representation. Like FFD, wires move points, so they work on meshes, surfaces and point clouds.
Variants and improvements
- Smooth curves. Use spline curves such as B-splines for and , with a smooth tangent, to remove the jump of the turn at the vertices.
- Several wires. The blend of moves above with the exponent is one choice; other blends weight each wire by its falloff.
- Holding a region still. In the blend above, a wire that does not move has the weight 0 and has no effect. To keep the upper lip still while the lower lip moves, the blend must give a fixed wire weight, for example by weighting each wire by its falloff .
- Falloff and scale along the curve. , and the falloff can vary with , to make a crease sharp at one end and soft at the other.
- Frames. The turn at line L6 uses only the change of the tangent; a full frame along the curve, such as a rotation-minimizing frame, also controls the twist about the tangent.
When to use it
Use wires when the shape change follows a curve on the object: a lip, a brow, a wrinkle, a seam, a tail. A curve is a natural handle for an artist, and it acts only near itself, so many wires can shape one face. For broad changes of a volume, use a lattice (FFD) or a cage; for a skeleton that bends limbs, use linear blend skinning.
Pitfalls
- Medial jumps. A point that lies about as far from two parts of as from each, inside a bend, can switch its closest point between frames and jump. Keep smaller than the radius of curvature of .
- Rebinding. The binding must use , not ; finding the closest point on the moved wire changes and makes the result depend on the path of the edit.
- Opposite tangents. A turn of 180 degrees has no defined axis; the implementation raises an error there.
- Parameter mismatch. and must share the parameter, so they need the same number of vertices; a wire that is resampled after binding moves points along the curve.
- Falloff with a kink. A falloff with , such as , makes a visible crease at the radius.
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/wire-deformation/. The authors are listed in prov/TTFYST.jsonl.
"""Wires deformation in the spirit of Singh and Fiume (1998).
A wire is a pair of curves: the reference curve R and the wire curve W,
with the same parameter. Here both are polylines with the same number of
vertices, and the parameter u = i + t means the point at the fraction t of
segment i. A point P near R is bound to its closest point R(u). It then
moves with W: it is scaled about R(u), rotated by the turn from the tangent
R'(u) to the tangent W'(u), and translated by W(u) - R(u). The amount of
each step is the falloff F = f(|P - R(u)| / r), so points at distance r or
more from R do not move.
The falloff f(x) = (1 - x^2)^2 for x < 1, and 0 otherwise, is one smooth
choice: f(0) = 1, f(1) = 0 and f'(1) = 0. Any decreasing f with these
properties can be passed in.
"""
import math
def sub(a, b):
return tuple(x - y for x, y in zip(a, b))
def add(a, b):
return tuple(x + y for x, y in zip(a, b))
def scale(a, c):
return tuple(c * x for x in a)
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 norm(a):
return math.sqrt(dot(a, a))
def default_falloff(x):
"""Return (1 - x^2)^2 for 0 <= x < 1 and 0 for x >= 1."""
if x >= 1:
return 0.0
return (1 - x * x) ** 2
def closest_parameter(poly, p):
"""Return (u, distance) of the point of the polyline closest to p.
The search is brute force over all segments; ties keep the first segment.
"""
best_u, best_d = 0.0, math.inf
for i in range(len(poly) - 1):
a, b = poly[i], poly[i + 1]
d = sub(b, a)
dd = dot(d, d)
t = 0.0 if dd == 0 else min(1.0, max(0.0, dot(sub(p, a), d) / dd))
dist = norm(sub(p, add(a, scale(d, t))))
if dist < best_d:
best_u, best_d = i + t, dist
return best_u, best_d
def point_at(poly, u):
"""Return the polyline point with the parameter u."""
i = min(int(math.floor(u)), len(poly) - 2)
t = u - i
return add(scale(poly[i], 1 - t), scale(poly[i + 1], t))
def tangent_at(poly, u):
"""Return the unit direction of the segment that holds the parameter u."""
i = min(int(math.floor(u)), len(poly) - 2)
d = sub(poly[i + 1], poly[i])
return scale(d, 1 / norm(d))
def rotate(v, axis, angle):
"""Rotate v about the unit axis by angle (Rodrigues' formula)."""
c, s = math.cos(angle), math.sin(angle)
return add(add(scale(v, c), scale(cross(axis, v), s)), scale(axis, dot(axis, v) * (1 - c)))
def deform(p, ref, wire, radius, s=1.0, falloff=default_falloff):
"""Move the point p by one wire. Return the new point."""
if len(ref) != len(wire) or len(ref) < 2:
raise ValueError("R and W need the same number of vertices, at least 2")
p = tuple(p)
u, dist = closest_parameter(ref, p) # @line L1
f = falloff(dist / radius) # @line L2
if f == 0: # @line L3
return p
c = point_at(ref, u)
q = add(c, scale(sub(p, c), 1 + (s - 1) * f)) # @line L4
tr, tw = tangent_at(ref, u), tangent_at(wire, u)
axis = cross(tr, tw) # @line L5
sin_t, cos_t = norm(axis), dot(tr, tw)
if sin_t > 1e-15:
theta = math.atan2(sin_t, cos_t)
q = add(c, rotate(sub(q, c), scale(axis, 1 / sin_t), theta * f)) # @line L6
elif cos_t < 0:
raise ValueError("the tangents are opposite; the rotation axis is not defined")
return add(q, scale(sub(point_at(wire, u), c), f)) # @line L7
def deform_many(p, wires, m=1.0):
"""Blend several wires: p + sum d_i |d_i|^m / sum |d_i|^m.
Each item of wires is a tuple (ref, wire, radius, s). With m = 0 this
is the plain average of the moves; a large m lets the largest move win.
"""
p = tuple(p)
moves = [sub(deform(p, *w), p) for w in wires]
weights = [norm(d) ** m if norm(d) > 0 else 0.0 for d in moves]
total = sum(weights)
if total == 0:
return p
out = p
for d, w in zip(moves, weights):
out = add(out, scale(d, w / total))
return out
test_wire_deformation.py 105 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/wire-deformation/. The authors are listed in prov/TTFYST.jsonl.
import math
import random
from wire_deformation import closest_parameter, default_falloff, deform, deform_many, point_at
def close(a, b, eps=1e-12):
return all(abs(x - y) <= eps for x, y in zip(a, b))
REF = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (2.0, 0.0, 0.0), (3.0, 0.0, 0.0)]
def test_wire_equal_to_reference_is_identity():
rng = random.Random(31)
ref = [(rng.uniform(-1, 1), rng.uniform(-1, 1), rng.uniform(-1, 1)) for _ in range(6)]
for _ in range(30):
p = tuple(rng.uniform(-1.5, 1.5) for _ in range(3))
assert close(deform(p, ref, ref, 0.8), p)
def test_far_points_do_not_move():
wire = [(x, y + 1.0, z) for x, y, z in REF]
rng = random.Random(32)
for _ in range(30):
p = (rng.uniform(-1, 4), rng.uniform(-3, 3), rng.uniform(-3, 3))
_, d = closest_parameter(REF, p)
if d >= 0.5:
assert deform(p, REF, wire, 0.5, s=2.0) == p
def test_point_on_reference_follows_the_wire():
wire = [(0.0, 0.0, 0.0), (1.0, 0.5, 0.0), (2.0, 0.5, 0.5), (3.0, 1.0, 0.0)]
for u in (0.0, 0.3, 1.0, 1.75, 2.5, 3.0):
p = point_at(REF, u)
assert close(deform(p, REF, wire, 0.7, s=1.5), point_at(wire, u), 1e-12)
def test_translated_straight_wire_moves_points_by_falloff_times_offset():
t = (0.0, 0.25, -0.5)
wire = [tuple(a + b for a, b in zip(v, t)) for v in REF]
rng = random.Random(33)
for _ in range(30):
p = (rng.uniform(0.2, 2.8), rng.uniform(-1, 1), rng.uniform(-1, 1))
_, d = closest_parameter(REF, p)
f = default_falloff(d / 1.2)
q = deform(p, REF, wire, 1.2)
assert close(q, tuple(a + f * b for a, b in zip(p, t)), 1e-12)
def test_worked_example():
# A point at distance 0.5 from a straight wire of radius 1: F = (1 - 0.25)^2 = 0.5625.
wire = [(x, 1.0, 0.0) for x, _, _ in REF]
q = deform((1.5, 0.5, 0.0), REF, wire, 1.0)
assert close(q, (1.5, 0.5 + 0.5625, 0.0))
# With s = 2 the offset 0.5 from R grows to 0.5 (1 + 0.5625) first.
q2 = deform((1.5, 0.5, 0.0), REF, wire, 1.0, s=2.0)
assert close(q2, (1.5, 0.5 * 1.5625 + 0.5625, 0.0))
def test_closest_point_matches_dense_sampling():
rng = random.Random(34)
ref = [(rng.uniform(-1, 1), rng.uniform(-1, 1), rng.uniform(-1, 1)) for _ in range(5)]
for _ in range(20):
p = tuple(rng.uniform(-1.5, 1.5) for _ in range(3))
u, d = closest_parameter(ref, p)
best = min(math.dist(p, point_at(ref, k / 1000)) for k in range(4001))
assert d <= best + 1e-12
assert best - d < 2e-3
assert abs(math.dist(p, point_at(ref, u)) - d) < 1e-12
def test_falloff_shape():
assert default_falloff(0.0) == 1.0
assert default_falloff(1.0) == 0.0
h = 1e-6
assert abs((default_falloff(1.0) - default_falloff(1.0 - h)) / h) < 1e-5
xs = [k / 100 for k in range(101)]
vals = [default_falloff(x) for x in xs]
assert all(a >= b for a, b in zip(vals, vals[1:]))
def test_rotation_turns_offsets_with_the_tangent():
# Turn a straight wire by 90 degrees about its first vertex in the xy plane.
ref = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0)]
wire = [(0.0, 0.0, 0.0), (0.0, 1.0, 0.0)]
# A point at u = 0 with offset (0, 0, 0.2): the axis is z, so it does not turn.
q = deform((0.0, 0.0, 0.2), ref, wire, 10.0)
assert close(q, (0.0, 0.0, 0.2), 1e-12)
# With a big radius F is near 1 and an offset along y turns towards -x.
q = deform((0.0, 0.2, 0.0), ref, wire, 1e6)
assert close(q, (-0.2, 0.0, 0.0), 1e-9)
def test_many_wires():
w1 = [(x, 1.0, 0.0) for x, _, _ in REF]
p = (1.5, 0.5, 0.0)
single = deform(p, REF, w1, 1.0)
assert close(deform_many(p, [(REF, w1, 1.0, 1.0)]), single)
# Two equal wires give the same result as one.
assert close(deform_many(p, [(REF, w1, 1.0, 1.0), (REF, w1, 1.0, 1.0)], m=2.0), single)
# A wire that does not move adds a zero move with weight 0.
assert close(deform_many(p, [(REF, w1, 1.0, 1.0), (REF, REF, 1.0, 1.0)], m=1.0), single)
History
Singh and Fiume presented wires at SIGGRAPH 1998 as a geometric deformation technique driven by space curves, with a radius of influence, a scale and a falloff for each wire. Earlier space deformations, such as Barr's closed-form maps and lattice FFD, used no curve as the handle. Wire deformers of this kind are now common in animation software.
References
- Karan Singh and Eugene Fiume, Wires: a geometric deformation technique, SIGGRAPH 98, 405-414, 1998, the whole paper (the reference curve and the wire curve, the radius of influence, the scale and the falloff, and the use of several wires)
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 TTFYST Edit this page History Report an error Markdown