Algorithm Search⌘K

Algorithmadvanced

Inverse tool offset

Also called ITO method.

Computes cutter locations on a grid that do not gouge the sampled part, as the highest inverted tool placed at each sample, after Kishinami, Kondo and Saito.

Time
O(n k) worst
Space
O(n) extra
Where
k = number of grid points in the tool footprint; n = number of grid points
Properties
deterministicexactiterative
Origin
Takeshi Kishinami, Tsukasa Kondo, Katsumasa Saito, 1988

The inverse tool offset (ITO) method computes the cutter location of a tool that moves straight down onto a part, at every point of a grid. It does not ask where the tool touches the part. Instead it turns the tool upside down, puts this inverted tool at every sample point of the part, and keeps the highest of all these shapes at each grid point. The upper envelope is the surface on which the tool tip can move without cutting into any sample, which is the cutter location surface of three-axis machining. The method works the same for every part, however many patches, holes or steep walls it has, because it only needs heights. 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 computes the envelope on a height grid by scattering, as a z-buffer does, and it checks it against a direct maximum over all samples.

In plain words

Press a ball down onto a bumpy surface until it rests on the bumps. The height of the lowest point of the ball is where the tool tip may go. Doing this at every grid point one by one is slow, because at each point the ball has to be compared with all the bumps under it.

Turn the question around. Each bump pushes the ball up when the ball is near. If you hang an upside-down ball under each bump, the height that a bump forces on the tip at a nearby point is the height of that hanging ball there. So hang one upside-down ball under every bump and take, at each point, the highest one.

The result is a smooth surface over the part. The tool tip runs along it and never pushes into a bump.

The problem

The part is a height field ff sampled on a grid with spacing dd. The tool points down, and its shape is a function T(o)≥0T(o) \ge 0 on a disk of offsets oo: the height of the tool surface above its tip at the horizontal offset oo. A ball end mill of radius rr has T(o)=r−r2−∣o∣2T(o) = r - \sqrt{r^2 - |o|^2}, and a flat end mill has T=0T = 0 on its disk. The tool tip at grid point pp must be at least as high as

Z(p)=max⁡q∈N(p)(f(q)−T(q−p)),Z(p) = \max_{q \in N(p)} \big(f(q) - T(q - p)\big),

where N(p)N(p) is the set of samples under the tool. This is the lowest tip height that leaves every sample at or under the tool surface. For a ball end mill on a smooth part, the tool center is at height Z+rZ + r, and the surface of centers is the offset surface of the part, cut at the points where the offset folds.

In terms of mathematical morphology, ZZ is the grayscale dilation of ff by the structuring function −T-T.

The algorithm

  1. INVERSE-TOOL-OFFSET(f, T)
  2. 1Z[p] ← −∞ for every grid point p
  3. 2for each sample q of the part do
  4. 3for each offset o in the footprint of the tool do
  5. 4p ← q + o; skip p if it is off the grid
  6. 5Z[p] ← max(Z[p], f(q) − T(o)) ▷ the inverted tool at q, as in a z-buffer
  7. 6return Z

The value f(q)−T(o)f(q) - T(o) is the height of the inverted tool, hung at the sample qq, above the point q+oq + o. For a symmetric tool, T(o)=T(−o)T(o) = T(-o), so this is the same as f(q)−T(q−p)f(q) - T(q - p) in the formula for Z(p)Z(p). The inverted tool is computed once, as a small table of heights over the footprint.

Example

Take a 5×55 \times 5 grid with spacing d=0.5d = 0.5, a flat floor at height 0, and one pin of height 1 at the middle sample. The tool is a ball end mill with r=1r = 1. Its footprint has the 13 offsets with ∣o∣≤1|o| \le 1, and TT is 0 at the center, 1−0.75=0.13401 - \sqrt{0.75} = 0.1340 at distance 0.5, 1−0.5=0.29291 - \sqrt{0.5} = 0.2929 at distance 0.707 and 1 at distance 1.

grid point on the middle row x=−1x = -1 x=−0.5x = -0.5 x=0x = 0 x=0.5x = 0.5 x=1x = 1
highest inverted tool floor 0, pin 1−1=01 - 1 = 0 pin 1−0.13401 - 0.1340 pin 11 pin 1−0.13401 - 0.1340 floor 0, pin 0
ZZ 0 0.8660 1 0.8660 0

The tip rides over the pin on a dome of radius 1, which is the inverted ball hung under the pin. At the diagonal neighbour (0.5,0.5)(0.5, 0.5), Z=0.7071Z = 0.7071. Elsewhere the floor wins and Z=0Z = 0.

Correctness

Theorem. INVERSE-TOOL-OFFSET returns, at each grid point pp, Z[p]=max⁡q∈N(p)(f(q)−T(q−p))Z[p] = \max_{q \in N(p)} (f(q) - T(q - p)). With the tip at Z[p]Z[p], the tool is at or above every sample under it and touches at least one, and every lower tip height leaves a sample above the tool.

The scatter loop sends the value f(q)−T(o)f(q) - T(o) to the point q+oq + o. For a fixed pp, the pairs that reach it are exactly the samples qq in its footprint, by the symmetry of the footprint, with the same values as in the formula, and the maximum does not depend on the order. The sample that attains the maximum touches the tool, and any lower tip cuts into it. The full proof is in the proof file.

The claim is about the samples only. The tests compare the scatter with a direct maximum over all samples for exact equality, with Fractions for the flat tool, and they check the ball on the tilted plane z=mxz = mx against the continuous answer mx+r(1+m2−1)mx + r(\sqrt{1 + m^2} - 1). The grid value is never above it, and in the tests it is below it by at most 0.0056 for d=0.25d = 0.25 and r=2r = 2, which is a measurement.

Proof: The scattered inverse tool gives the lowest tip height at each grid point that leaves every sample under the tool, with contact at one samplefull

Setting. The grid points are G={0,…,nx−1}×{0,…,ny−1}G = \{0, \dots, n_x - 1\} \times \{0, \dots, n_y - 1\} and f:G→Rf : G \to \mathbb{R} gives the sampled heights of the part. The tool is a finite footprint O⊂Z2O \subset \mathbb{Z}^2 with (0,0)∈O(0, 0) \in O, symmetric (o∈Oo \in O implies −o∈O-o \in O), and a height function T:O→R≥0T : O \to \mathbb{R}_{\ge 0} with T(−o)=T(o)T(-o) = T(o) and T(0,0)=0T(0, 0) = 0. The tool with its tip at height zz above grid point pp has its surface at height z+T(q−p)z + T(q - p) above the grid point qq, for q−p∈Oq - p \in O. For p∈Gp \in G let N(p)={q∈G:q−p∈O}N(p) = \{q \in G : q - p \in O\}, the samples under the tool. Let ZZ be the array that INVERSE-TOOL-OFFSET returns.

Claim. For every p∈Gp \in G:

(a) Z[p]=max⁡q∈N(p)(f(q)−T(q−p))Z[p] = \max_{q \in N(p)} \big(f(q) - T(q - p)\big);

(b) with the tip at Z[p]Z[p], the tool surface is at or above every sample in N(p)N(p), and it touches at least one of them;

(c) Z[p]Z[p] is the lowest tip height with property (b): every lower tip height puts some sample of N(p)N(p) above the tool surface.

The loops stop after nxny∣O∣n_x n_y |O| passes of line L5.

Termination. Lines L2 and L3 run over the finite sets GG and OO, so line L5 runs at most nxny∣O∣n_x n_y |O| times.

Proof of (a). Line L5 at sample qq and offset oo with p=q+o∈Gp = q + o \in G compares the value f(q)−T(o)f(q) - T(o) into Z[p]Z[p]. Fix pp. The pairs (q,o)(q, o) that reach pp are those with q=p−o∈Gq = p - o \in G and o∈Oo \in O, which by the symmetry of OO are exactly the q∈N(p)q \in N(p), with o=p−qo = p - q. Their values are f(q)−T(p−q)=f(q)−T(q−p)f(q) - T(p - q) = f(q) - T(q - p) by the symmetry of TT. Line L1 starts Z[p]Z[p] empty, and line L5 keeps the largest value seen, so after the loops Z[p]Z[p] is the maximum of these values. The set N(p)N(p) holds pp itself, since (0,0)∈O(0, 0) \in O, so the maximum is over a nonempty set.

Proof of (b). For q∈N(p)q \in N(p), (a) gives Z[p]≥f(q)−T(q−p)Z[p] \ge f(q) - T(q - p), so Z[p]+T(q−p)≥f(q)Z[p] + T(q - p) \ge f(q): the tool surface above qq is at or above the sample. For a q∗q^* that attains the maximum, equality holds, so the tool touches q∗q^*.

Proof of (c). Let z<Z[p]z < Z[p]. Then z+T(q∗−p)<Z[p]+T(q∗−p)=f(q∗)z + T(q^* - p) < Z[p] + T(q^* - p) = f(q^*), so the sample q∗q^* lies above the tool surface. ■\blacksquare

Remarks. (1) Claim (a) says that ZZ is the grayscale dilation of ff by the structuring function −T-T on OO, so INVERSE-TOOL-OFFSET is a morphological dilation of the height field. (2) The claims are about the samples. Between grid points the part can rise above the tool, for example a thin rib that lies between samples, so the method is gouge-free only at the samples. (3) Each value is computed by the same expression f(q)−T(o)f(q) - T(o) in the scatter loop and in a direct maximum, so with floats the two give equal results, and the tests compare them for equality.

Complexity

The footprint of a tool of radius rr has k≈πr2/d2k \approx \pi r^2/d^2 grid points. Lines L2 to L5 run nknk times for nn samples, so the time is O(nk)O(nk), and ZZ takes O(n)O(n) space. Halving the grid spacing multiplies nn by 4 and kk by 4, so the time grows by 16. Each step is a table lookup, a subtraction and a comparison, which suits graphics hardware: drawing the inverted tool at every sample with a depth test is a z-buffer rendering.

Properties

  • Robust. The method needs only heights, so gaps, overlaps and bad patches of the model do not stop it.
  • Gouge-free at the samples. No sample lies above the tool surface. Between samples the part can rise above the tool.
  • Any tool shape. Ball, flat, bull-nose and tapered tools all work by their height table TT.
  • Order-free. The maximum is the same in any order of the samples, so the loop can run in parallel.
  • Three-axis only. The tool axis is fixed and vertical.

Variants and improvements

  • Rendering on graphics hardware. Drawing the inverted tool as a mesh at every sample with a depth buffer computes ZZ at the speed of the graphics card.
  • Gather form. At each grid point, take the maximum over the samples in the footprint. This gives the same values and suits a parallel machine.
  • Paths from the surface. Tool paths are read from ZZ along grid rows, which gives iso-planar passes, or along level sets of ZZ, which gives waterline passes.
  • Verification. The same grid is used to simulate the cut and check it, as in the z-map model and in NC verification.

When to use it

Use the inverse tool offset for three-axis machining of complex parts given as meshes or as many patches, such as dies and molds, where robustness matters more than exact geometry. Use an exact method on the surface model, with gouge detection, when the part has thin features or when a fine grid is too costly.

Pitfalls

  • Features between samples. A rib thinner than the grid spacing can fall between samples and get cut. The grid spacing must be smaller than the smallest feature.
  • Grid error. The grid value is below the continuous answer by an amount that grows with the spacing, so a finished surface can show small cuts below the true part. In the tests on a tilted plane the error was below 0.0060.006 for spacing 0.25. The error depends on the spacing and the slope, so choose the spacing from the tolerance of the part.
  • Edges of the grid. Near the edge of the grid, the footprint holds fewer samples, and the tool can drop over the edge. Pad the grid with the stock height or with the part outside the machining area.
  • Memory. A fine grid over a large part needs much memory, and the time grows with the fourth power of the inverse spacing.

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.

inverse_tool_offset.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/inverse-tool-offset/. The authors are listed in prov/RAY63F.jsonl.

"""Inverse tool offset (ITO) on a height grid.

The part is a height field sampled on a square grid: f[i][j] is the height
of the part at (i d, j d), with grid spacing d. The tool points straight
down. Its shape is a function T(o) >= 0 for offsets o in its footprint: the
height of the tool surface above its tip at the horizontal offset o. A ball
end mill of radius r has T(o) = r - sqrt(r^2 - |o|^2), and a flat end mill
has T = 0 on its disk.

The cutter location of the tip at the grid point p is the lowest height at
which the tool touches no sample from above:

    Z[p] = max over samples q in the footprint of p of  f[q] - T(q - p).

The ITO method computes Z by scattering: it puts the tool upside down at each
sample q, so that its surface is the height f[q] - T(o) at p = q + o, and
keeps the highest value at each grid point, like a z-buffer. For a symmetric
tool, T(q - p) = T(p - q), so the scatter and the gather give the same Z.

Heights may be int, float or Fraction. With Fractions and a flat tool, every
step is exact.
"""

import math


def footprint(r, d):
    """The grid offsets (a, b) of the tool disk of radius r on a grid of spacing d."""
    if r <= 0 or d <= 0:
        raise ValueError("the radius and the spacing must be positive")
    k = int(r // d)
    return [(a, b) for a in range(-k, k + 1) for b in range(-k, k + 1) if (a * a + b * b) * d * d <= r * r]


def ball_tool(r, d):
    """The inverted ball end mill: offset -> height of the tool surface above the tip."""
    return {(a, b): r - math.sqrt(r * r - (a * a + b * b) * d * d) for a, b in footprint(r, d)}


def flat_tool(r, d):
    """The flat end mill: height 0 on the whole disk."""
    return {o: 0 for o in footprint(r, d)}


def inverse_tool_offset(f, tool):
    """Cutter location heights Z of the tool tip at every grid point, by scattering.

    A grid point whose footprint holds no sample keeps the value None, which
    cannot happen when the footprint holds the offset (0, 0).
    """
    nx, ny = len(f), len(f[0])
    z = [[None] * ny for _ in range(nx)]  # @line L1
    for i in range(nx):  # @line L2
        for j in range(ny):
            h = f[i][j]
            for (a, b), t in tool.items():  # @line L3
                p, q = i + a, j + b  # @line L4
                if 0 <= p < nx and 0 <= q < ny:
                    v = h - t
                    if z[p][q] is None or v > z[p][q]:  # @line L5
                        z[p][q] = v
    return z  # @line L6
test_inverse_tool_offset.py 104 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/inverse-tool-offset/. The authors are listed in prov/RAY63F.jsonl.

import math
import random
from fractions import Fraction as F

import pytest

from inverse_tool_offset import ball_tool, flat_tool, footprint, inverse_tool_offset


def gather(f, tool):
    """Brute force: at each grid point, the maximum over all samples of the part of
    f[q] - T(q - p), taken only where q - p is in the footprint."""
    nx, ny = len(f), len(f[0])
    z = [[None] * ny for _ in range(nx)]
    for p in range(nx):
        for q in range(ny):
            for i in range(nx):
                for j in range(ny):
                    o = (i - p, j - q)
                    if o in tool:
                        v = f[i][j] - tool[o]
                        if z[p][q] is None or v > z[p][q]:
                            z[p][q] = v
    return z


def terrain(rng, nx, ny):
    return [[rng.uniform(-1.0, 1.0) for _ in range(ny)] for _ in range(nx)]


def test_pin_example():
    f = [[0.0] * 5 for _ in range(5)]
    f[2][2] = 1.0
    z = inverse_tool_offset(f, ball_tool(1.0, 0.5))
    row = [round(v, 6) for v in z[2]]
    assert row == [0.0, 0.866025, 1.0, 0.866025, 0.0]
    assert round(z[1][1], 6) == 0.707107
    assert len(footprint(1.0, 0.5)) == 13


def test_scatter_equals_gather_exactly():
    rng = random.Random(1)
    for _ in range(4):
        nx, ny = rng.randrange(8, 14), rng.randrange(8, 14)
        f = terrain(rng, nx, ny)
        tool = ball_tool(rng.choice([0.5, 1.0, 1.5]), 0.25)
        assert inverse_tool_offset(f, tool) == gather(f, tool)


def test_flat_tool_is_exact_with_fractions():
    rng = random.Random(2)
    for _ in range(4):
        nx, ny = rng.randrange(6, 11), rng.randrange(6, 11)
        f = [[F(rng.randrange(-50, 51), 7) for _ in range(ny)] for _ in range(nx)]
        tool = flat_tool(F(3, 2), F(1, 2))
        z = inverse_tool_offset(f, tool)
        assert z == gather(f, tool)
        for p in range(nx):
            for q in range(ny):
                disk = [f[i][j] for i in range(nx) for j in range(ny) if (i - p) ** 2 + (j - q) ** 2 <= 9]
                assert z[p][q] == max(disk)


def test_no_gouge_at_samples_and_contact():
    rng = random.Random(3)
    f = terrain(rng, 16, 16)
    tool = ball_tool(1.0, 0.25)
    z = inverse_tool_offset(f, tool)
    for p in range(16):
        for q in range(16):
            touch = False
            for (a, b), t in tool.items():
                i, j = p + a, q + b
                if 0 <= i < 16 and 0 <= j < 16:
                    assert z[p][q] + t >= f[i][j]
                    touch = touch or z[p][q] + t == f[i][j]
            assert touch


def test_ball_on_a_tilted_plane():
    """The grid result is at most the continuous offset, and close to it (measured)."""
    for m, d in ((0.5, 0.25), (1.0, 0.25), (2.0, 0.125)):
        r = 2.0
        n = int(12 / d)
        f = [[m * i * d for _ in range(n)] for i in range(n)]
        z = inverse_tool_offset(f, ball_tool(r, d))
        k = int(r / d) + 1
        worst = 0.0
        for i in range(k, n - k):
            exact = m * i * d + r * (math.sqrt(1 + m * m) - 1)
            for j in range(k, n - k):
                assert z[i][j] <= exact + 1e-12
                worst = max(worst, exact - z[i][j])
        assert worst <= m * d * d


def test_bad_input():
    with pytest.raises(ValueError):
        footprint(0, 1)
    with pytest.raises(ValueError):
        ball_tool(1.0, 0.0)

History

Kishinami, Kondo and Saito developed the inverse offset method in Japan in the late 1980s, and Kondo, Kishinami and Saito (1988) described a machining system based on it. The method spread with the z-map model and graphics hardware, and Choi and Jerard (1998) present it as a standard way to compute cutter location data for sculptured surfaces.

References

  1. Tsukasa Kondo, Takeshi Kishinami and Katsumasa Saito, Machining system based on inverse offset method, Journal of the Japan Society for Precision Engineering 54(5), 971-976, 1988, the inverse offset of the tool over the part and the cutter location surface built from it
  2. Byoung K. Choi and Robert B. Jerard, Sculptured Surface Machining, Theory and Applications, Kluwer Academic Publishers, 1998, the chapters on the computation of cutter location data for sculptured surfaces, where the inverse tool offset method is presented

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
  • at least three sources
  • a scene preset for the example