Algorithm Search⌘K

Algorithmresearch

Constant engagement toolpath

Also called adaptive clearing, engagement angle control.

Stori and Wright's and later toolpaths that bound the angle of tool engagement with the material by curved paths and medial axis spirals, for high-speed roughing.

Time
O(m (n + k)) worst
Space
O(m) extra
Where
k = number of pixels in the box around one move; m = number of tool moves; n = number of samples on the front half circle
Properties
deterministiciterative
Origin
J. A. Stori, P. K. Wright, 2000

A constant engagement toolpath is a roughing path for pocket machining along which the angle of the tool circle that is in contact with the material stays near a chosen value. The engagement angle sets the load on the tool and the chip thickness. An ordinary contour-parallel path keeps the distance between passes constant, so the engagement jumps at inner corners and grows on curves that bend toward the material, and the feed must be set for the worst case. A constant engagement path changes the distance between passes instead, so the tool can run at a high feed everywhere. Stori and Wright gave the first construction in 2000, for convex shapes. Commercial systems call the idea adaptive clearing. The version here grows the path step by step: it keeps a pixel model of the stock and, at each step, turns the tool until the measured engagement equals the target.

In plain words

A cutter that moves through a block of metal touches the uncut metal along part of its edge. The size of that part is the engagement. A big engagement is a heavy load, and a small one wastes time.

If the cutter goes round a pocket in rings that are always the same distance apart, the load is not steady. In a corner or on a bend the cutter suddenly bites much more.

So the method does not fix the distance. It keeps a picture of the metal that is left. Before each small move, it looks at how much of the cutter edge would touch metal in each direction it could go. Then it steers toward the metal or away from it until the touch is just right, makes the move, and erases the metal it cut from the picture.

The problem

The tool is a disk of radius RR that moves in the plane. At the center cc with the feed direction ψ\psi, the front half circle is the set of points c+R(cos⁡t,sin⁡t)c + R(\cos t, \sin t) with ∣t−ψ∣≤90°|t - \psi| \le 90°. The engagement angle θ(c,ψ)\theta(c, \psi) is the measure of the part of the front half circle that lies in the remaining material. Given the initial material, a start point, a start heading and a target θ∗\theta^*, the task is to produce a sequence of tool moves along which θ\theta stays close to θ∗\theta^* and the material is removed.

Two closed forms set the scale. For a straight cut that leaves a wall at the radial depth aa inside the tool,

θ=arccos⁡(1−a/R).\theta = \arccos(1 - a/R).

For a tool on a circle of radius ρ\rho that cuts into material outside the radius RbR_b about the same center,

cos⁡θ=Rb2−ρ2−R22ρR.\cos\theta = \frac{R_b^2 - \rho^2 - R^2}{2\rho R}.

So for a fixed stepover ss, with Rb=ρ−s+RR_b = \rho - s + R, the engagement on a circle is larger than on a straight line, and it grows as ρ\rho falls. To keep θ=θ∗\theta = \theta^*, the stepover must be

s=ρ+R−ρ2+R2+2ρRcos⁡θ∗,s = \rho + R - \sqrt{\rho^2 + R^2 + 2\rho R\cos\theta^*},

which is smaller on tight curves. Both formulas are proved in the proof file.

The algorithm

The generator below moves the tool by a fixed step σ\sigma. The material lies on the side side of the path, +1+1 for the left and −1-1 for the right. Let E(φ)E(\varphi) be the engagement, sampled at nn points of the front half circle in the pixel stock, at the point c+σ(cos⁡φ,sin⁡φ)c + \sigma(\cos\varphi, \sin\varphi) with the heading φ\varphi.

  1. CONSTANT-ENGAGEMENT-PATH(stock, c, ψ, R, θ*, σ, Δ, m, side)
  2. 1repeat m times
  3. 2E(φ) ← sampled engagement at c + σ(cos φ, sin φ) with heading φ
  4. 3toward ← ψ + side·Δ; away ← ψ − side·Δ ▷ the turn limits
  5. 4if E(away) ≥ θ* then φ ← away ▷ too much material even when turning away
  6. 5else if E(toward) ≤ θ* then φ ← toward ▷ too little even when turning in
  7. 6else bisect φ between toward and away 12 times so that E crosses θ*
  8. 7remove the capsule of radius R around the segment c to c + σ(cos φ, sin φ) from the stock
  9. 8append (c + σ(cos φ, sin φ), φ, E(φ)) to the path; c, ψ ← that point, φ
  10. 9return the path

Turning toward the material raises the engagement, and turning away lowers it. Line L6 keeps a bracket ℓ,h\ell, h with E(ℓ)>θ∗≥E(h)E(\ell) > \theta^* \ge E(h). The stock is a grid of pixels of side dd. A pixel is cut when its center is within RR of the move segment, so the stock always equals the initial material minus the swept capsules, at pixel resolution.

Example

The tool has R=5R = 5, and the target is 60°60°. On a straight wall, 60°60° needs the depth a=R(1−cos⁡60°)=2.5a = R(1 - \cos 60°) = 2.5. On a circle of radius ρ=10\rho = 10, the same angle needs the stepover s=15−175=1.771s = 15 - \sqrt{175} = 1.771, and the stepover 2.5 gives arccos⁡(0.3125)=71.79°\arccos(0.3125) = 71.79° instead. The table compares the pixel engagement, with d=0.2d = 0.2 and n=60n = 60 samples of 3°3°, with the closed form for some circles.

ρ\rho ss pixel closed form straight cut with the same ss
8 1 48°48° 46.57°46.57° 36.87°36.87°
10 2 63°63° 63.90°63.90° 53.13°53.13°
15 2 60°60° 60.44°60.44° 53.13°53.13°
25 3 72°72° 71.58°71.58° 66.42°66.42°

In the test run, the stock is the square [−26,26]2[-26, 26]^2 with a round hole of radius 8 cut in the middle, and the tool starts at (3,0)(3, 0), heading up, with the material on its right. The step is σ=0.5\sigma = 0.5 and the turn limit is Δ=15°\Delta = 15°. In 600 steps the tool spirals out from the radius 5.64 to 13.33, and from step 100 on the engagement stays between 60°60° and 63°63°. An Archimedean spiral with the fixed pitch 2.5, the straight-cut stepover for 60°60°, cut to the same radius in the same stock, has engagements from 63°63° to 78°78° after the same warm-up. Started on a straight wall at the depth 1, the generator turns into the material and settles at the depth 2.5.

Correctness

The proof file proves the straight and circular closed forms and the stepover formula. For the generator it proves what a single step does. The stock invariant holds: after each step the pixel stock is exactly the initial material minus the pixels within RR of the segments cut so far. The new heading is within Δ\Delta of the old one. When the target lies between E(away)E(\text{away}) and E(toward)E(\text{toward}), the bisection returns a heading inside an interval of width 2Δ/2122\Delta/2^{12} on which the sampled engagement crosses the target. The generator stops after mm steps.

What is proved and what is measured. The step is proved, but the path as a whole is not. No result here says that the engagement stays near the target over many steps, that the path leaves no islands of material, or that it clears the pocket. The tests measure these things. The generator holds 60°60° to 63°63° after a warm-up, which is within one sample of the target. It does more than twice as well as a fixed-pitch spiral. The pixel engagement is within two samples of an engagement computed with exact rational arithmetic from the true union of the cut capsules. It is within one sample plus 2°2° of the closed forms on straight and circular cuts.

Proof: The engagement angle has closed forms for straight and circular cuts, and each step of the generator keeps the pixel stock exact and picks a heading at which the engagement crosses the targetfull

Setting. The tool is a disk of radius RR with center cc, moving in the unit direction uu. Let u⊥u^\perp be uu turned by +90°+90°. The front half circle is {c+R(cos⁡t u+sin⁡t u⊥):∣t∣≤90°}\{c + R(\cos t\, u + \sin t\, u^\perp) : |t| \le 90°\}. The engagement angle θ(c,u)\theta(c, u) is the measure of the set of t∈[−90°,90°]t \in [-90°, 90°] whose point is in material. The code samples nn values of tt, at the centers of nn equal arcs, so it returns the measure of the sampled set, 180°180° times the fraction of samples in material.

Claim 1 (straight cut). Let the material be the half-plane above a line parallel to uu, at the distance a∈[0,2R]a \in [0, 2R] inside the tool, measured from the side of the tool farthest into the material: in coordinates with u=(1,0)u = (1, 0) and c=(0,a−R)c = (0, a - R), the material is {y≥0}\{y \ge 0\}. Then θ=arccos⁡(1−a/R)\theta = \arccos(1 - a/R).

Proof. The point at tt has height a−R+Rsin⁡ta - R + R\sin t. It is in material when sin⁡t≥1−a/R\sin t \ge 1 - a/R. On [−90°,90°][-90°, 90°], sin⁡\sin is increasing, so the set is [arcsin⁡(1−a/R),90°][\arcsin(1 - a/R), 90°], of measure 90°−arcsin⁡(1−a/R)=arccos⁡(1−a/R)90° - \arcsin(1 - a/R) = \arccos(1 - a/R). □\square

Claim 2 (circular cut). Let the tool center move on the circle of radius ρ\rho about OO, counterclockwise, so uu is the tangent, and let nn be the outward unit normal at cc. Let the material ahead of the tool be the set of points at distance more than RbR_b from OO, with ρ−R<Rb<ρ+R\rho - R < R_b < \rho + R. Then

cos⁡θ=Rb2−ρ2−R22ρR.\cos\theta = \frac{R_b^2 - \rho^2 - R^2}{2\rho R}.

Proof. Write a point of the tool circle as c+R(cos⁡φ n+sin⁡φ u)c + R(\cos\varphi\, n + \sin\varphi\, u), with φ\varphi measured from nn toward uu. Its squared distance from OO is ρ2+R2+2ρRcos⁡φ\rho^2 + R^2 + 2\rho R\cos\varphi, since c=ρnc = \rho n and n⊥un \perp u. So it lies beyond RbR_b exactly when cos⁡φ>cos⁡α0\cos\varphi > \cos\alpha_0, with cos⁡α0\cos\alpha_0 the right side above, that is when ∣φ∣<α0|\varphi| < \alpha_0. The front half is uu-side: sin⁡φ≥0\sin\varphi \ge 0, that is φ∈[0°,180°]\varphi \in [0°, 180°]. On it, the material set is [0,α0)[0, \alpha_0), of measure α0\alpha_0. Points with φ<0\varphi < 0 lie behind the tool; in a steady pass they were already removed by earlier positions of the same pass, and in any case they are not in the front half. □\square

Claim 3 (constant engagement stepover on a circle). If the previous pass left the boundary at Rb=ρ−s+RR_b = \rho - s + R, the engagement equals θ∗\theta^* exactly when

s=ρ+R−ρ2+R2+2ρRcos⁡θ∗.s = \rho + R - \sqrt{\rho^2 + R^2 + 2\rho R\cos\theta^*}.

Proof. Claim 2 with θ=θ∗\theta = \theta^* gives Rb2=ρ2+R2+2ρRcos⁡θ∗R_b^2 = \rho^2 + R^2 + 2\rho R\cos\theta^*, and Rb>0R_b > 0, so RbR_b is the positive root. Solve Rb=ρ−s+RR_b = \rho - s + R for ss. □\square

As ρ→∞\rho \to \infty this tends to R(1−cos⁡θ∗)R(1 - \cos\theta^*), the stepover of Claim 1, and for finite ρ\rho it is smaller, because ρ2+R2+2ρRcos⁡θ∗>(ρ+Rcos⁡θ∗)2\rho^2 + R^2 + 2\rho R\cos\theta^* > (\rho + R\cos\theta^*)^2 when sin⁡θ∗≠0\sin\theta^* \ne 0.

Claim 4 (one step of the generator). Assume that before a step the stock holds exactly the pixels whose centers are in the initial material and at distance more than RR from every segment cut so far. Then:

(a) after line L7 the same holds with the new segment added;

(b) the new heading φ\varphi lies within the allowed turn of the old heading ψ\psi;

(c) let E(φ)E(\varphi) be the sampled engagement at c+σ(cos⁡φ,sin⁡φ)c + \sigma(\cos\varphi, \sin\varphi) with heading φ\varphi. If E(away)<θ∗<E(toward)E(\text{away}) < \theta^* < E(\text{toward}), the bisection of line L6 ends with two headings ℓ\ell and hh, ∣ℓ−h∣=2Δ/212|\ell - h| = 2\Delta / 2^{12}, with E(ℓ)>θ∗≥E(h)E(\ell) > \theta^* \ge E(h), and returns their midpoint. In the other cases it returns the end of the allowed range that is nearest to the target, as lines L4 and L5 say.

Proof. (a) The function cut_capsule visits every pixel whose center can be within RR of the segment (a box around it), computes the exact nearest point of the segment to the center by clamping the projection parameter to [0,1][0, 1], and clears the pixel when the distance is at most RR. No other pixel changes. (b) Lines L3 to L6 only return toward, away, or a point between them, which are ψ±Δ\psi \pm \Delta. (c) The bisection keeps the invariant E(ℓ)>θ∗≥E(h)E(\ell) > \theta^* \ge E(h): it holds at the start by the case condition, and each step replaces ℓ\ell or hh by the midpoint according to the side of θ∗\theta^* on which E(mid)E(\text{mid}) falls. After 12 halvings the interval has length 2Δ/2122\Delta/2^{12}. □\square

Termination. Each step evaluates EE at most 15 times, each evaluation looks at nn samples, and each cut visits a finite box of pixels. So the generator stops after its given number of steps.

What is proved and what is measured. Claims 1 to 3 are exact statements about the engagement of a disk. Claim 4 says what one step does: the stock stays exact for the pixel model, and the heading is a point where the sampled engagement crosses the target. If the engagement grows as the heading turns toward the material, which holds for the smooth boundaries met in the tests, this crossing is where the engagement equals the target up to the sample step of 180°/n180°/n. Nothing here proves that a whole path keeps the engagement near the target, avoids leaving islands of material, or finishes the region. Those are measured: in the tests the path holds 60°60° to 63°63° after a warm-up, the pixel engagement agrees with an exact stock (Fractions, union of capsules) to within two samples, and it matches Claims 1 and 2 to within one sample plus 2°2°.

Complexity

One evaluation of EE looks at nn points, each in O(1)O(1) time. Each step evaluates EE at most 15 times and cuts one capsule, which visits the kk pixels in a box around the segment, about (2R+σ)(2R)/d2(2R + \sigma)(2R)/d^2 of them. So mm steps take O(m(n+k))O(m(n + k)) time. The stock takes the space of its pixel grid, which the caller provides, and the path takes O(m)O(m) space. In practice the stock is the main cost. A finer grid gives a more accurate engagement and costs quadratically more per cut.

Properties

  • Steady load. The engagement stays close to the target wherever the turn limit allows the path to follow the material.
  • Variable stepover. The distance between passes is not constant. It shrinks on curves that bend toward the material and in corners.
  • Stock driven. The path depends on the actual remaining material, so it handles rest material and partly cut regions.
  • Local and greedy. Each step looks only at the next move. Nothing in the method plans the whole pocket.

Variants and improvements

  • Offset construction. Stori and Wright built each pass as a curve at the stepover of the stepover formula from the previous one, for convex shapes, with no stock model.
  • Medial axis spirals. Elber, Cohen and Drake grow a spiral out from the medial axis of the pocket, so that the passes change slowly and the engagement does not jump. Fermat spiral and Voronoi paths are related.
  • Stock models. A pixel grid is the simplest stock. A z-map, a polygon model of the stock boundary, or a distance transform gives the engagement faster or more accurately.
  • Feed adjustment. Instead of changing the path, a planner can keep the path and change the feed to keep the chip thickness constant. This is a case of feedrate scheduling.
  • Trochoidal moves. Where the stepover would have to be very small, for example in a narrow slot, many systems switch to trochoidal milling.

When to use it

Use a constant engagement path for roughing of pockets and open regions in hard materials at high speed, where tool load decides the tool life, and the machine can follow smooth curves at a high feed. In a soft material at a low speed, a simple contour-parallel or zigzag path is shorter and often faster.

Difference from trochoidal milling. Trochoidal milling is a fixed geometric pattern: a circle of loops that moves forward along a slot, with the same shape everywhere. It bounds the engagement by making the loops small, and the engagement still varies within each loop, from zero on the return to its highest value as the tool comes round to the front. A constant engagement path is computed from the stock, so its shape changes with the material, and it aims to keep the engagement at one value along the whole path.

Pitfalls

  • Islands of material. A greedy path can leave thin islands or uncut regions between passes. A real system needs a final check of the stock and clean-up moves.
  • Pixel noise. The sampled engagement moves in steps of 180°/n180°/n, and a coarse grid shifts the boundary by up to a pixel. Choose dd much smaller than the stepover.
  • The turn limit. A small Δ\Delta keeps the path smooth but cannot follow sharp corners, so the engagement rises there. A large Δ\Delta gives a path that zigzags, which the machine cannot follow at full feed.
  • Engagement is not the only load. The axial depth, the chip thinning at small angles and the machine dynamics also matter. A constant angle does not mean a constant force.
  • Air moves. Where there is no material, the engagement is zero, and the generator turns toward the material at the turn limit. It needs a rule to link to the next region.

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.

constant_engagement_toolpath.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/constant-engagement-toolpath/. The authors are listed in prov/X9MBE5.jsonl.

"""A constant engagement toolpath, generated step by step from a pixel stock.

The stock is a grid of square pixels of side d; a pixel holds material until
the tool removes it. The tool is a disk of radius R. Its engagement angle at
the center c, moving in the direction psi, is the angle of the front half of
the tool circle (the points within 90 degrees of the feed direction) that
lies in material. It is sampled at n points of the front half circle.

For a straight cut with radial depth a, the engagement is arccos(1 - a/R).
For a tool on a circle of radius rho, cutting into material outside the
circle of radius Rb around the same center, it is
arccos((Rb^2 - rho^2 - R^2) / (2 rho R)).

The generator moves the tool by a step s at a time. At each step it chooses
the new heading within max_turn of the old one so that the engagement at
the new position equals the target, by bisection, then removes the swept
capsule from the stock. The material lies on the side `side` of the path:
+1 for the left, -1 for the right.
"""

import math


class Stock:
    """A pixel stock on [x0, x0 + nx d] x [y0, y0 + ny d]."""

    def __init__(self, x0, y0, d, nx, ny, material):
        self.x0, self.y0, self.d, self.nx, self.ny = x0, y0, d, nx, ny
        self.cells = bytearray(nx * ny)
        for j in range(ny):
            y = y0 + (j + 0.5) * d
            for i in range(nx):
                if material(x0 + (i + 0.5) * d, y):
                    self.cells[j * nx + i] = 1

    def has(self, x, y):
        i = math.floor((x - self.x0) / self.d)
        j = math.floor((y - self.y0) / self.d)
        if 0 <= i < self.nx and 0 <= j < self.ny:
            return self.cells[j * self.nx + i] == 1
        return False

    def cut_capsule(self, a, b, r):
        """Remove the pixels whose centers are within r of the segment from a to b."""
        d = self.d
        i0 = max(0, math.floor((min(a[0], b[0]) - r - self.x0) / d))
        i1 = min(self.nx - 1, math.floor((max(a[0], b[0]) + r - self.x0) / d))
        j0 = max(0, math.floor((min(a[1], b[1]) - r - self.y0) / d))
        j1 = min(self.ny - 1, math.floor((max(a[1], b[1]) + r - self.y0) / d))
        ex, ey = b[0] - a[0], b[1] - a[1]
        ee = ex * ex + ey * ey
        rr = r * r
        for j in range(j0, j1 + 1):
            y = self.y0 + (j + 0.5) * d
            row = j * self.nx
            for i in range(i0, i1 + 1):
                x = self.x0 + (i + 0.5) * d
                px, py = x - a[0], y - a[1]
                t = 0.0 if ee == 0 else min(1.0, max(0.0, (px * ex + py * ey) / ee))
                qx, qy = px - t * ex, py - t * ey
                if qx * qx + qy * qy <= rr:
                    self.cells[row + i] = 0


def front_points(c, psi, R, n=60):
    """The n sample points of the front half of the tool circle, at the centers of n equal arcs."""
    pts = []
    for k in range(n):
        t = psi - math.pi / 2 + math.pi * (k + 0.5) / n
        pts.append((c[0] + R * math.cos(t), c[1] + R * math.sin(t)))
    return pts


def engagement(stock, c, psi, R, n=60):
    """The engagement angle in degrees, from n samples of the front half circle."""
    return 180.0 * sum(1 for x, y in front_points(c, psi, R, n) if stock.has(x, y)) / n


def straight_engagement(a, R):
    """Closed form for a straight cut of radial depth a, 0 <= a <= 2R, in degrees."""
    return math.degrees(math.acos(1 - a / R))


def circle_engagement(rho, Rb, R):
    """Closed form for a tool on a circle of radius rho with material outside radius Rb."""
    c = (Rb * Rb - rho * rho - R * R) / (2 * rho * R)
    return math.degrees(math.acos(max(-1.0, min(1.0, c))))


def constant_engagement_path(stock, c, psi, R, target, step, max_turn, n_steps, side=-1, n=60):
    """Generate n_steps tool moves that hold the engagement at target (degrees).

    Returns a list of (center, heading, engagement) after each move. The
    stock is cut as the tool moves.
    """
    path = []

    def E(phi):  # @line L2
        p = (c[0] + step * math.cos(phi), c[1] + step * math.sin(phi))
        return engagement(stock, p, phi, R, n)

    for _ in range(n_steps):  # @line L1
        toward, away = psi + side * max_turn, psi - side * max_turn  # @line L3
        if E(away) >= target:  # @line L4
            phi = away
        elif E(toward) <= target:  # @line L5
            phi = toward
        else:  # @line L6
            lo, hi = toward, away
            for _ in range(12):
                mid = (lo + hi) / 2
                if E(mid) > target:
                    lo = mid
                else:
                    hi = mid
            phi = (lo + hi) / 2
        e = E(phi)
        nc = (c[0] + step * math.cos(phi), c[1] + step * math.sin(phi))
        stock.cut_capsule(c, nc, R)  # @line L7
        path.append((nc, phi, e))  # @line L8
        c, psi = nc, phi
    return path  # @line L9


def follow_path(stock, centers, R, n=60):
    """Cut along a given list of centers and record the engagement at each one."""
    out = []
    for a, b in zip(centers, centers[1:]):
        psi = math.atan2(b[1] - a[1], b[0] - a[0])
        out.append(engagement(stock, b, psi, R, n))
        stock.cut_capsule(a, b, R)
    return out
test_constant_engagement_toolpath.py 137 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/constant-engagement-toolpath/. The authors are listed in prov/X9MBE5.jsonl.

import math
import random
from fractions import Fraction as F

import pytest

from constant_engagement_toolpath import (
    Stock,
    circle_engagement,
    constant_engagement_path,
    engagement,
    follow_path,
    front_points,
    straight_engagement,
)

R = 5.0
D = 0.2
N = 60
STEP = 180.0 / N  # the angle of one sample


def square_stock(L, material):
    return Stock(-L, -L, D, int(2 * L / D), int(2 * L / D), material)


@pytest.mark.parametrize("a", [0.5, 1.0, 2.0, 2.5, 4.0, 5.0, 7.5, 9.5])
def test_straight_cut_matches_closed_form(a):
    # material above y = 0; the tool center is at y = a - R, moving along +x
    stock = square_stock(20.0, lambda x, y: y >= 0)
    e = engagement(stock, (0.3, a - R), 0.0, R, N)
    assert abs(e - straight_engagement(a, R)) <= STEP + 2.0


@pytest.mark.parametrize("rho,s", [(8.0, 1.0), (10.0, 2.0), (15.0, 2.0), (25.0, 3.0), (10.0, 4.0)])
def test_circular_cut_matches_closed_form(rho, s):
    Rb = rho - s + R
    stock = square_stock(40.0, lambda x, y: x * x + y * y > Rb * Rb)
    e = engagement(stock, (rho, 0.0), math.pi / 2, R, N)
    assert abs(e - circle_engagement(rho, Rb, R)) <= STEP + 2.0


def test_closed_form_numbers():
    assert math.isclose(straight_engagement(2.0, 5.0), 53.13010235415598)
    assert math.isclose(circle_engagement(10.0, 13.0, 5.0), math.degrees(math.acos(0.44)))
    assert abs(circle_engagement(10.0, 13.0, 5.0) - 63.90) < 0.005
    # the stepover that gives 53.13 degrees on a circle of radius 10
    s = 10 + 5 - math.sqrt(100 + 25 + 2 * 10 * 5 * 0.6)
    assert math.isclose(s, 1.3985, abs_tol=1e-4)
    assert math.isclose(circle_engagement(10.0, 10.0 - s + 5.0, 5.0), 53.13010235415598)


HOLE = 8.0
L = 26.0


def spiral_run(n_steps):
    stock = square_stock(L, lambda x, y: x * x + y * y > HOLE * HOLE)
    start = (HOLE - R, 0.0)
    path = constant_engagement_path(stock, start, math.pi / 2, R, 60.0, 0.5, math.radians(15), n_steps, side=-1, n=N)
    return start, path


def exact_engagement(start, path, k):
    """Engagement at path[k] from the exact stock: the square minus the hole minus
    the capsules cut before step k, tested with Fractions on the same sample points."""
    centers = [start] + [c for c, _, _ in path]
    caps = [(centers[i], centers[i + 1]) for i in range(k)]  # cut before reaching path[k]
    c, psi, _ = path[k]
    rr = F(R) ** 2
    hole2 = F(HOLE) ** 2
    count = 0
    for x, y in front_points(c, psi, R, N):
        fx, fy = F(x), F(y)
        if not (-L <= fx <= L and -L <= fy <= L and fx * fx + fy * fy > hole2):
            continue
        cut = False
        for a, b in caps:
            # a float prefilter with a wide margin, then the exact test
            ex, ey = b[0] - a[0], b[1] - a[1]
            t = max(0.0, min(1.0, ((x - a[0]) * ex + (y - a[1]) * ey) / (ex * ex + ey * ey)))
            if math.hypot(x - a[0] - t * ex, y - a[1] - t * ey) > R + 1e-6:
                continue
            ax, ay, bx, by = F(a[0]), F(a[1]), F(b[0]), F(b[1])
            ex, ey = bx - ax, by - ay
            t = ((fx - ax) * ex + (fy - ay) * ey) / (ex * ex + ey * ey)
            t = min(F(1), max(F(0), t))
            qx, qy = fx - ax - t * ex, fy - ay - t * ey
            if qx * qx + qy * qy <= rr:
                cut = True
                break
        if not cut:
            count += 1
    return 180.0 * count / N


def test_pixel_engagement_matches_exact_stock():
    start, path = spiral_run(300)
    rng = random.Random(5)
    for k in [40, 120] + rng.sample(range(150, 300), 3):
        exact = exact_engagement(start, path, k)
        assert abs(path[k][2] - exact) <= 2 * STEP


def test_generator_holds_the_target_and_beats_constant_stepover():
    _, path = spiral_run(600)
    dev_gen = max(abs(e - 60.0) for _, _, e in path[100:])
    assert dev_gen <= STEP + 1.0
    # the path really spirals out
    r_end = math.hypot(*path[-1][0])
    assert r_end > HOLE + 3
    # an Archimedean spiral with the straight-cut stepover for 60 degrees
    stock = square_stock(L, lambda x, y: x * x + y * y > HOLE * HOLE)
    s0 = R * (1 - math.cos(math.radians(60)))
    centers, phi, r = [], 0.0, HOLE - R
    while r < r_end:
        centers.append((r * math.cos(phi), r * math.sin(phi)))
        phi += 0.5 / r
        r += s0 * (0.5 / r) / (2 * math.pi)
    es = follow_path(stock, centers, R, N)
    dev_spiral = max(abs(e - 60.0) for e in es[100:])
    assert dev_spiral >= 9.0
    assert dev_spiral > 2 * dev_gen


def test_generator_settles_on_a_straight_wall():
    # Material above y = 0; the tool starts with depth 1 and must find the
    # depth R (1 - cos 60) = 2.5, where the straight engagement is 60 degrees.
    stock = Stock(-5.0, -10.0, D, 400, 100, lambda x, y: y >= 0)
    path = constant_engagement_path(stock, (0.0, 1.0 - R), 0.0, R, 60.0, 0.5, math.radians(10), 120, side=1, n=N)
    ys = [c[1] for c, _, _ in path[60:]]
    depth = sum(ys) / len(ys) + R
    assert abs(depth - 2.5) <= 0.3
    assert all(abs(e - 60.0) <= STEP + 1.0 for _, _, e in path[60:])

History

High-speed machining in the 1990s made tool load a main concern of roughing. Stori and Wright (2000) defined the constant engagement problem for pockets and gave an offset construction for convex shapes. Elber, Cohen and Drake (2005) used the medial axis to build spirals whose passes change slowly. Through the 2000s CAM systems added roughing strategies that compute the path from a model of the remaining stock and keep the engagement or the chip thickness within bounds. They are sold under names such as adaptive clearing.

References

  1. J. A. Stori and P. K. Wright, Constant engagement tool path generation for convex geometries, Journal of Manufacturing Systems 19(3), 172-184, 2000, the engagement angle of a tool on a curved path and the construction of offsets that keep it constant
  2. Gershon Elber, Elaine Cohen and Sam Drake, MATHSM, medial axis transform toward high speed machining of pockets, Computer-Aided Design 37(2), 241-250, 2005, the spiral paths built on the medial axis that limit the change of engagement in a pocket
  3. Byoung K. Choi and Robert B. Jerard, Sculptured Surface Machining, Theory and Applications, Kluwer Academic Publishers, 1998, the material on pocket machining and the stock models used for simulation

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