Algorithm Search⌘K

Algorithmcore

Iso-parametric toolpath

Also called isoparametric machining.

Machines a parametric surface along curves of constant u or v, simple to compute but uneven in spacing where the parameterization is distorted.

Time
O(N) worst
Space
O(1) extra
Where
N = number of passes
Properties
deterministicexactiterative
Origin
Gary C. Loney, Tulga M. Ozsoy, 1987

An iso-parametric toolpath machines a parametric surface S(u,v)S(u, v) by moving the tool along the curves where one parameter is constant, v=v0,v1,…v = v_0, v_1, \dots, back and forth. The curves come directly from the surface equation, so the path is easy to compute and lies on the surface exactly. The cost is uneven spacing: equal steps in vv give equal steps on the surface only where the parameterization is even, and elsewhere the passes crowd together or spread apart. This is a manufacturing method of mechanical CAD: placement and routing here mean where a cutter goes and how it moves, not the placement and routing of a circuit. The code builds the path on a bilinear patch, where every iso-curve is straight and the largest gap between passes has an exact formula.

In plain words

A surface in a CAD model is made like a sheet of graph paper that has been stretched and bent. The grid lines of the paper are still there on the bent sheet. An iso-parametric path just traces these grid lines, one after the other, back and forth.

This is simple, because the grid lines are known. The problem is that the sheet was stretched more in some places than in others. Where it was stretched a lot, the grid lines are far apart, and the tool leaves ridges. Where it was squeezed, the lines are close, and the tool cuts the same place many times.

To be safe, the gap must be small enough at the widest place. Then the path is denser than needed everywhere else.

The problem

The input is a surface patch S:[0,1]2→R3S : [0, 1]^2 \to \mathbb{R}^3 and a path interval ww, the largest allowed distance between neighbouring passes. For a ball end mill of radius rr on a nearly flat surface, the interval ww that gives a scallop height hh is about 8rh\sqrt{8rh}. The output is a cutter contact path on the surface, which a CAM system turns into cutter location data by offsetting along the surface normal.

The gap between the passes vjv_j and vj+1v_{j+1} at the parameter uu is about (vj+1−vj) ∣Sv(u,vj)∣(v_{j+1} - v_j)\,|S_v(u, v_j)|, where SvS_v is the partial derivative. The length ∣Sv∣|S_v| is the stretch of the parameterization across the passes, and it comes from the first fundamental form. The passes are vj=j/Nv_j = j/N, with NN chosen so that the largest gap is at most ww.

On a bilinear patch S(u,v)=(1−u)(1−v)P00+u(1−v)P10+(1−u)vP01+uvP11S(u, v) = (1-u)(1-v)P_{00} + u(1-v)P_{10} + (1-u)vP_{01} + uvP_{11}, the surface is linear in vv for fixed uu, so the gap is exactly ∣Sv(u)∣/N|S_v(u)|/N with Sv(u)=(1−u)(P01−P00)+u(P11−P10)S_v(u) = (1-u)(P_{01} - P_{00}) + u(P_{11} - P_{10}). Every iso-curve of a bilinear patch is a straight line, which makes it a doubly ruled surface.

The algorithm

  1. ISO-PARAMETRIC(S, w)
  2. 1M ← max(|S_v(0)|², |S_v(1)|²) ▷ the largest squared gap factor is at an end
  3. 2N ← the smallest positive integer with M ≤ N² w²
  4. 3for j ← 0 to N do
  5. 4C_j ← the curve u ↦ S(u, j/N), forward for even j and backward for odd j
  6. 5append C_j, joined to the previous pass along the boundary curve u = 0 or u = 1
  7. 6return the path

Line L1 uses the fact that ∣Sv(u)∣2|S_v(u)|^2 is a convex quadratic in uu on a bilinear patch, so its largest value is at u=0u = 0 or u=1u = 1. Line L2 compares rationals, so NN is exact. For a tensor-product B-spline or NURBS surface, line L1 bounds ∣Sv∣|S_v| over the patch, and line L4 also steps along each curve with a chord tolerance, since the curves are not straight.

Example

Take the flat trapezoid with P00=(0,0,0)P_{00} = (0, 0, 0), P10=(4,0,0)P_{10} = (4, 0, 0), P01=(0,1,0)P_{01} = (0, 1, 0) and P11=(4,4,0)P_{11} = (4, 4, 0), and w=1w = 1. Then Sv(0)=(0,1,0)S_v(0) = (0, 1, 0) and Sv(1)=(0,4,0)S_v(1) = (0, 4, 0), so M=16M = 16 and N=4N = 4.

step pass from to gap to the next pass at u=0u = 0 and u=1u = 1
L4, L5 v=0v = 0 (0,0,0)(0, 0, 0) (4,0,0)(4, 0, 0) 14\tfrac14 and 11
L4, L5 v=14v = \tfrac14 (4,1,0)(4, 1, 0) (0,14,0)(0, \tfrac14, 0) 14\tfrac14 and 11
L4, L5 v=12v = \tfrac12 (0,12,0)(0, \tfrac12, 0) (4,2,0)(4, 2, 0) 14\tfrac14 and 11
L4, L5 v=34v = \tfrac34 (4,3,0)(4, 3, 0) (0,34,0)(0, \tfrac34, 0) 14\tfrac14 and 11
L4, L5 v=1v = 1 (0,1,0)(0, 1, 0) (4,4,0)(4, 4, 0)

The passes fan out: they are ww apart at the long side and w/4w/4 apart at the short side. The path has length about 24.4324.43, with 21.9321.93 on the passes and 2.52.5 on the links. Any path that keeps every point of this region within w/2=0.5w/2 = 0.5 has length at least (10−π/4)/1≈9.2(10 - \pi/4)/1 \approx 9.2, because a path of length LL widened by 0.50.5 covers at most L+π/4L + \pi/4 of area, and the trapezoid has area 10.

Correctness

Theorem. On a bilinear patch, NN is the smallest number of passes with all gaps at most ww, the path lies on the patch, and every point of the patch is within w/2w/2 of the path.

For fixed uu the patch is linear in vv, so the points S(u,v)S(u, v) with vj≤v≤vj+1v_j \le v \le v_{j+1} form a straight segment on the patch of length ∣Sv(u)∣/N|S_v(u)|/N between two passes. The function ∣Sv(u)∣2|S_v(u)|^2 is the squared norm of an affine map of uu, so it is convex and largest at an end, and line L2 makes every such segment at most ww long. A point on a segment of length at most ww is within w/2w/2 of one of its ends. The full proof is in the proof file.

The bound is about the distance between passes. The scallop height that a tool leaves also depends on the tool and the curvature, see scallop height.

Proof: On a bilinear patch, the iso-parametric passes chosen from the end gaps keep every surface point within half the path interval of the pathfull

Setting. S(u,v)=(1−u)(1−v)P00+u(1−v)P10+(1−u)vP01+uvP11S(u, v) = (1-u)(1-v)P_{00} + u(1-v)P_{10} + (1-u)vP_{01} + uvP_{11} on [0,1]2[0, 1]^2, with corners in Q3\mathbb{Q}^3, and w>0w > 0 is rational. Let a=P01−P00a = P_{01} - P_{00}, b=P11−P10b = P_{11} - P_{10} and B(u)=(1−u)a+ubB(u) = (1-u)a + ub. Let NN be the value of line L2 and vj=j/Nv_j = j/N. Pass jj is the segment from S(0,vj)S(0, v_j) to S(1,vj)S(1, v_j), and Γ\Gamma is the union of the passes and the links.

Claim. (a) Line L2 stops, and NN is the smallest positive integer with ∣B(u)∣/N≤w|B(u)|/N \le w for all u∈[0,1]u \in [0, 1]. (b) Pass jj is the iso-parametric curve {S(u,vj):0≤u≤1}\{S(u, v_j) : 0 \le u \le 1\}, and the links lie on the patch. (c) Every point of the patch lies within distance w/2w/2 of Γ\Gamma, along a straight segment that lies on the patch.

Step 1 (the patch is linear in each parameter). Expanding the formula gives S(u,v)=A(u)+vB(u)S(u, v) = A(u) + vB(u) with A(u)=(1−u)P00+uP10A(u) = (1-u)P_{00} + uP_{10}, and in the same way S(u,v)=A′(v)+uB′(v)S(u, v) = A'(v) + u B'(v) with A′(v)=(1−v)P00+vP01A'(v) = (1-v)P_{00} + vP_{01} and B′(v)=(1−v)(P10−P00)+v(P11−P01)B'(v) = (1-v)(P_{10} - P_{00}) + v(P_{11} - P_{01}). So for fixed vv, u↦S(u,v)u \mapsto S(u, v) is the affine map from S(0,v)S(0, v) to S(1,v)S(1, v), and for fixed uu, v↦S(u,v)v \mapsto S(u, v) is affine with derivative B(u)B(u). This proves the first part of (b). The link from pass jj to pass j+1j + 1 joins S(e,vj)S(e, v_j) to S(e,vj+1)S(e, v_{j+1}) for e∈{0,1}e \in \{0, 1\}, and by the same fact it is the curve v↦S(e,v)v \mapsto S(e, v) on [vj,vj+1][v_j, v_{j+1}], which lies on the patch.

Step 2 (the largest gap is at an end). The function g(u)=∣B(u)∣2g(u) = |B(u)|^2 is the square of the norm of an affine map, so it is convex: for u=(1−λ)u0+λu1u = (1 - \lambda)u_0 + \lambda u_1, B(u)=(1−λ)B(u0)+λB(u1)B(u) = (1 - \lambda)B(u_0) + \lambda B(u_1), and the convexity of x↦∣x∣2x \mapsto |x|^2 gives g(u)≤(1−λ)g(u0)+λg(u1)g(u) \le (1-\lambda) g(u_0) + \lambda g(u_1). With u0=0u_0 = 0 and u1=1u_1 = 1, g(u)≤max⁡(g(0),g(1))=Mg(u) \le \max(g(0), g(1)) = M, the value of line L1.

Step 3 (proof of (a)). The test of line L2 is M>n2w2M > n^2 w^2, and n2w2n^2 w^2 grows without bound as nn grows, so the loop stops at the smallest n≥1n \ge 1 with M≤n2w2M \le n^2 w^2. By Step 2, max⁡u∣B(u)∣/n≤w\max_u |B(u)|/n \le w holds if and only if M≤n2w2M \le n^2 w^2. So NN is the smallest positive integer with that property.

Step 4 (proof of (c)). Let (u,v)∈[0,1]2(u, v) \in [0, 1]^2 and choose jj with vj≤v≤vj+1v_j \le v \le v_{j+1}. By Step 1 the points S(u,v′)S(u, v') for v′∈[vj,vj+1]v' \in [v_j, v_{j+1}] form a straight segment on the patch from q0=S(u,vj)q_0 = S(u, v_j) to q1=S(u,vj+1)q_1 = S(u, v_{j+1}), of length ∣B(u)∣(vj+1−vj)=∣B(u)∣/N≤w|B(u)|(v_{j+1} - v_j) = |B(u)|/N \le w by (a). The point S(u,v)S(u, v) lies on this segment, so it is within w/2w/2 of q0q_0 or of q1q_1, along the segment. By (b), q0q_0 lies on pass jj and q1q_1 lies on pass j+1j + 1. ■\blacksquare

Remark. The bound of (c) holds for every uu, but the gap ∣B(u)∣/N|B(u)|/N is ww only where ∣B(u)∣|B(u)| is largest. Where ∣B(u)∣|B(u)| is small, the passes are closer than needed, which is the unevenness of iso-parametric paths. On a curved surface the gap between two iso-curves is only Δv ∣Sv∣\Delta v\,|S_v| to first order, and this proof does not apply.

Complexity

Line L2 counts up to NN, and lines L3 to L5 write 2(N+1)2(N + 1) points, so the time is O(N)O(N) with N=⌈M/w⌉N = \lceil \sqrt{M}/w \rceil. Integer square roots would give NN in time polynomial in the bit size, but the count is cheap next to the path. The extra space is O(1)O(1) apart from the output. On a curved patch, each pass also costs one point per forward step, and the number of steps grows with the curvature and shrinks with the chord tolerance.

Properties

  • Exact passes. The passes are curves of the surface model, with no intersection to solve and no projection.
  • Patch by patch. Each patch of a model gets its own passes. The passes of neighbouring patches do not line up, and the boundaries between patches need extra care.
  • Uneven spacing. The gap varies with ∣Sv∣|S_v|, so the path is longer than needed wherever ∣Sv∣|S_v| is below its largest value.
  • Conservative. With NN set by the largest gap, no gap exceeds ww.

Variants and improvements

  • Adaptive iso-curves. Elber and Cohen (1994) add passes only where two neighbouring passes are too far apart, so the spacing becomes more even.
  • Iso-planar paths. Cutting the surface with parallel planes gives passes that run across patch boundaries. See iso-planar toolpath.
  • Iso-scallop paths. Placing each pass from the last one so that the scallop height is constant removes the waste of uneven passes. See iso-scallop toolpath.
  • Reparameterization. An arc-length parametrization of the boundary curves makes the passes more even at the boundaries, but not in general inside the patch.

When to use it

Use an iso-parametric path for a single patch whose parameterization is close to even, such as a gently bent sheet, and for finishing along the flow of a surface. Avoid it on trimmed patches, on patches that are very distorted, and on models with many small patches, where an iso-planar or iso-scallop path is shorter and more even.

Pitfalls

  • Trimmed surfaces. A trimmed patch keeps only part of its parameter square. The iso-curves must be clipped to the trim curves in the parameter plane, or the tool cuts outside the face.
  • Degenerate patches. At a pole, where a whole boundary of the parameter square maps to one point, all passes meet, and the tool cuts the same point many times.
  • First-order gap only. On a curved surface, Δv ∣Sv∣\Delta v\,|S_v| is only an estimate of the gap. The true gap can be larger where ∣Sv∣|S_v| changes fast.
  • Contact path, not tool path. The iso-curves give cutter contact points. The tool center is offset from them, and on concave parts with small radius the offset can gouge the part.

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.

iso_parametric_toolpath.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/iso-parametric-toolpath/. The authors are listed in prov/SQ9VDF.jsonl.

"""Iso-parametric toolpath on a bilinear patch, with exact arithmetic.

The patch is S(u, v) = (1-u)(1-v) P00 + u(1-v) P10 + (1-u) v P01 + u v P11
for 0 <= u, v <= 1. The passes are the iso-v curves v = j / N. On a bilinear
patch every iso-parametric curve is a straight segment, so each pass is
exact and needs no forward step.

For a fixed u the patch is linear in v, with S_v(u) = (1-u)(P01 - P00) +
u(P11 - P10). The gap between the passes v_j and v_j+1 at u is therefore
exactly |S_v(u)| / N. Its square is a convex quadratic in u, so the gap is
largest at u = 0 or u = 1, and N is the smallest integer with
max(|S_v(0)|, |S_v(1)|) <= N w.

The path is a list of contact points on the surface (the CC path). The
cutter location points come from it by an offset along the surface normal.
"""

from fractions import Fraction

Point = tuple[Fraction, Fraction, Fraction]


def _pt(p) -> Point:
    if len(p) != 3:
        raise ValueError("a corner needs 3 coordinates")
    return tuple(Fraction(c) for c in p)


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 lerp(a, b, t):
    return tuple(x + t * (y - x) for x, y in zip(a, b))


class BilinearPatch:
    def __init__(self, p00, p10, p01, p11):
        self.p00, self.p10, self.p01, self.p11 = map(_pt, (p00, p10, p01, p11))

    def at(self, u, v) -> Point:
        u, v = Fraction(u), Fraction(v)
        return lerp(lerp(self.p00, self.p10, u), lerp(self.p01, self.p11, u), v)

    def s_v(self, u) -> Point:
        """The derivative in v, which does not depend on v."""
        return lerp(sub(self.p01, self.p00), sub(self.p11, self.p10), Fraction(u))


def pass_count(patch: BilinearPatch, w) -> int:
    """The smallest N such that the gap between neighbouring iso-v passes is at most w."""
    w = Fraction(w)
    if w <= 0:
        raise ValueError("the path interval must be positive")
    m = max(dot(patch.s_v(0), patch.s_v(0)), dot(patch.s_v(1), patch.s_v(1)))  # @line L1
    n = 1
    while m > n * n * w * w:  # @line L2
        n += 1
    return n


def iso_parametric(patch: BilinearPatch, w) -> list[Point]:
    """The zigzag CC path: passes v = j / N, linked along the boundary curves u = 0 and u = 1."""
    n = pass_count(patch, w)
    path = []
    for j in range(n + 1):  # @line L3
        v = Fraction(j, n)
        a, b = patch.at(0, v), patch.at(1, v)
        # The pass is the straight segment from a to b, run back and forth.
        seg = [a, b] if j % 2 == 0 else [b, a]  # @line L4
        # Moving from the last point of one pass to the first point of the next
        # runs along the boundary curve u = 0 or u = 1, which is straight too.
        path.extend(seg)  # @line L5
    return path  # @line L6


def path_segments(path):
    return list(zip(path, path[1:]))
test_iso_parametric_toolpath.py 100 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/iso-parametric-toolpath/. The authors are listed in prov/SQ9VDF.jsonl.

import math
import random
from fractions import Fraction as F

import pytest

from iso_parametric_toolpath import BilinearPatch, dot, iso_parametric, pass_count, path_segments, sub

TRAPEZOID = BilinearPatch((0, 0, 0), (4, 0, 0), (0, 1, 0), (4, 4, 0))


def dist2(p, a, b):
    """Exact squared distance from p to the segment ab in space."""
    d = sub(b, a)
    dd = dot(d, d)
    t = 0 if dd == 0 else min(F(1), max(F(0), dot(sub(p, a), d) / dd))
    q = tuple(x + t * y - z for x, y, z in zip(a, d, p))
    return dot(q, q)


def random_patch(rng):
    return BilinearPatch(*[tuple(rng.randrange(-10, 11) for _ in range(3)) for _ in range(4)])


def test_trapezoid_example():
    assert pass_count(TRAPEZOID, 1) == 4
    path = iso_parametric(TRAPEZOID, 1)
    assert path[:4] == [(0, 0, 0), (4, 0, 0), (4, 1, 0), (0, F(1, 4), 0)]
    assert len(path) == 10 and path[-2:] == [(0, 1, 0), (4, 4, 0)]
    gap0 = TRAPEZOID.at(0, F(1, 4))[1] - TRAPEZOID.at(0, 0)[1]
    gap1 = TRAPEZOID.at(1, F(1, 4))[1] - TRAPEZOID.at(1, 0)[1]
    assert (gap0, gap1) == (F(1, 4), 1)
    length = sum(math.dist(a, b) for a, b in path_segments(path))
    assert abs(length - 24.43) < 0.01


def test_gap_formula_and_its_maximum():
    rng = random.Random(1)
    for _ in range(40):
        patch = random_patch(rng)
        w = F(rng.randrange(1, 13), 2)
        n = pass_count(patch, w)
        ends = max(dot(patch.s_v(0), patch.s_v(0)), dot(patch.s_v(1), patch.s_v(1)))
        for _ in range(10):
            u = F(rng.randrange(0, 101), 100)
            j = rng.randrange(0, n)
            g = sub(patch.at(u, F(j + 1, n)), patch.at(u, F(j, n)))
            sv = patch.s_v(u)
            assert dot(g, g) * n * n == dot(sv, sv)
            assert dot(sv, sv) <= ends
            assert dot(g, g) <= w * w


def test_pass_count_is_minimal():
    rng = random.Random(2)
    for _ in range(60):
        patch = random_patch(rng)
        w = F(rng.randrange(1, 13), 2)
        n = pass_count(patch, w)
        if n > 1:
            worst = max(dot(g, g) for g in (sub(patch.at(u, F(1, n - 1)), patch.at(u, 0)) for u in (0, 1)))
            assert worst > w * w


def test_every_surface_point_is_near_the_path():
    """Brute force: the exact distance from sample points of the patch to the path is at most w/2."""
    rng = random.Random(3)
    for _ in range(25):
        patch = random_patch(rng)
        w = F(rng.randrange(2, 13), 2)
        segs = path_segments(iso_parametric(patch, w))
        samples = [(F(i, 6), F(j, 6)) for i in range(7) for j in range(7)]
        samples += [(F(rng.randrange(0, 1001), 1000), F(rng.randrange(0, 1001), 1000)) for _ in range(30)]
        for u, v in samples:
            p = patch.at(u, v)
            assert min(dist2(p, a, b) for a, b in segs) <= w * w / 4


def test_path_lies_on_the_patch():
    rng = random.Random(4)
    for _ in range(20):
        patch = random_patch(rng)
        path = iso_parametric(patch, F(rng.randrange(1, 9)))
        n = len(path) // 2 - 1
        for (a, b), j in zip(zip(path[::2], path[1::2]), range(n + 1)):
            v = F(j, n)
            for t in (F(0), F(1, 3), F(1)):
                q = tuple(x + t * (y - x) for x, y in zip(a, b))
                u = t if j % 2 == 0 else 1 - t
                assert q == patch.at(u, v)


def test_bad_input():
    with pytest.raises(ValueError):
        pass_count(TRAPEZOID, 0)
    with pytest.raises(ValueError):
        BilinearPatch((0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0))

History

Iso-parametric paths were the first tool paths for sculptured surfaces, because they come straight from the surface equations of Bezier and B-spline patches. Loney and Ozsoy (1987) described iso-parametric machining of free-form patches. Elber and Cohen (1994) made the passes adaptive, and Choi and Jerard (1998) compare it with the iso-planar and iso-scallop methods in their book.

References

  1. Byoung K. Choi and Robert B. Jerard, Sculptured Surface Machining, Theory and Applications, Kluwer Academic Publishers, 1998, chapter 6, introduction to tool-path generation methods, the part on iso-parametric machining
  2. Gary C. Loney and Tulga M. Ozsoy, NC machining of free form surfaces, Computer-Aided Design 19(2), 85-90, 1987, the tool paths along the iso-parametric curves of a surface patch, and the choice of the step between them
  3. Gershon Elber and Elaine Cohen, Toolpath generation for freeform surface models, Computer-Aided Design 26(6), 490-496, 1994, adaptive iso-curves, which add iso-parametric passes only where the gap between the passes is too large

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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • 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
  • a scene preset for the example