Algorithm Search⌘K

Algorithmadvanced

Principal axis method

Also called Rao-Ismail-Bedi method.

Five-axis method of Rao, Bedi and coauthors that tilts a toroidal or flat end mill in the minimum curvature plane so its effective radius matches the maximum curvature.

Time
O(m) worst
Space
O(1) extra
Where
m = number of cutter contact points
Properties
deterministiciterative
Origin
N. Rao, Sanjeev Bedi, Ralph O. Buchal, Fathy Ismail, 1996

The principal axis method (PAM) chooses the tool orientation of a flat or toroidal end mill at each cutter contact point in five-axis milling. It tilts the tool axis away from the surface normal in the plane of minimum curvature, by the smallest angle at which the tilted tool, seen along the feed direction, curves as much as the surface does in the plane of maximum curvature. The tilted flat bottom then fits the surface across the feed much more closely than a ball end mill of the same size, so each pass cuts a wider strip and fewer passes are needed for a given scallop height. This is a manufacturing method of mechanical CAD: it decides how a cutter is held against a part. It is not about the placement and routing of a circuit. The tilt comes from one formula in the principal curvatures, and on a concave cylinder the formula is exact: it gives the smallest tilt that does not gouge, and the closest fit.

In plain words

A flat-bottomed cutter held straight up against a curved hollow touches it with its sharp rim and leaves deep ridges. A round-ended cutter fits better, but it cuts only a narrow band at a time.

Tip the flat cutter forward a little, in the direction along which the surface is nearly straight. Seen from the front, its round bottom edge now looks like a flat oval. For a small tip the oval is very flat, and its bottom is a wide, gentle curve.

The method picks the tip at which the bottom of the oval curves exactly as much as the hollow does across. Tip less and the oval digs in at the sides. Tip more and it stands away from the surface and leaves more material. At the right tip the cutter hugs the hollow over a wide band.

The problem

At a cutter contact point pp the surface has the unit normal nn, pointing to the tool, the principal directions emin⁡e_{\min} and emax⁡e_{\max}, and the normal curvatures κmin⁡≤κmax⁡\kappa_{\min} \le \kappa_{\max}, with a positive curvature where the surface bends toward the tool (a concave part). The tool is a toroidal end mill: its center circle, the circle of the centers of the corner arcs, has radius RR and lies at height rr above the tip, where rr is the corner radius. A flat end mill is the case r=0r = 0. The task is to choose the tool axis aa and the tip position cc so that the tool touches pp with normal nn, does not cut into the surface near pp, and leaves as little material as possible across the feed. This is one case of the five-axis tool orientation problem, solved locally at each point.

The key quantity is the effective radius of the tool. Tilt the axis by the angle λ\lambda from nn toward emin⁡e_{\min}, and look along emin⁡e_{\min}. The center circle projects to an ellipse with semi axes RR and Rsin⁡λR\sin\lambda, whose radius of curvature at its lowest point is R2/(Rsin⁡λ)=R/sin⁡λR^2/(R\sin\lambda) = R/\sin\lambda. The corner adds rr, so the tool cuts a profile with the radius

Rcut(λ)=Rsin⁡λ+rR_{\text{cut}}(\lambda) = \frac{R}{\sin\lambda} + r

in the plane of maximum curvature. A profile with a smaller radius than the surface, 1/κmax⁡1/\kappa_{\max}, leaves a small gap. A profile with a larger radius gouges the sides.

The algorithm

  1. PRINCIPAL-AXIS-METHOD(p, n, e_min, κ_max, R, r, λ_min)
  2. 1if κ_max ≤ 0 then ▷ convex or flat across the feed
  3. 2λ ← λ_min
  4. 3else ρ ← 1/κ_max ▷ radius of the surface across the feed
  5. 4if R > ρ − r then fail: no tilt avoids a gouge
  6. 5λ ← max(arcsin(R/(ρ − r)), λ_min) ▷ R/sin λ + r = ρ
  7. 6a ← cos λ · n + sin λ · e_min ▷ tilt in the plane of minimum curvature
  8. 7e ← cos λ · e_min − sin λ · n ▷ in the tip plane, toward the contact
  9. 8c ← p + r n − r a − R e ▷ tip center; the corner touches p
  10. 9return (λ, a, c)

Line L5 solves Rcut(λ)=ρR_{\text{cut}}(\lambda) = \rho. Line L8 puts the lowest corner center at p+rnp + r n, so the corner arc touches pp with normal nn. The tool feeds along emin⁡e_{\min} in the basic method, so the swept cross-section is the effective profile in the plane of nn and emax⁡e_{\max}. The lower bound λmin⁡\lambda_{\min} keeps the center of the flat bottom, where the cutting speed is zero, off the surface, and it is the tilt used on convex parts, where any tilt is free of local gouges.

Example

A toroidal end mill of diameter 20 mm has R=8R = 8 and r=2r = 2. It cuts the inside of a cylinder of radius ρ=50\rho = 50, feeding along the axis. At the bottom, n=(0,0,1)n = (0, 0, 1), emin⁡=(1,0,0)e_{\min} = (1, 0, 0) and κmax⁡=0.02\kappa_{\max} = 0.02.

line value
L3 ρ=50\rho = 50, and R=8≤ρ−r=48R = 8 \le \rho - r = 48
L5 sin⁡λ=8/48=1/6\sin\lambda = 8/48 = 1/6, λ=9.594°\lambda = 9.594°
L6 a=(0.1667,0,0.9860)a = (0.1667, 0, 0.9860)
L7 e=(0.9860,0,−0.1667)e = (0.9860, 0, -0.1667)
L8 c=(−8.2214,0,1.3613)c = (-8.2214, 0, 1.3613)

The effective radius is R/sin⁡λ=48R/\sin\lambda = 48, and with the corner the cut profile has radius 50, equal to the surface. The table below gives the width of the strip across the feed in which the vertical gap between the tool and the surface is at most h=0.01h = 0.01, computed by sampling the swept profile.

tool and tilt strip width for h=0.01h = 0.01
toroidal, λ=9.594°\lambda = 9.594° (PAM) 8.06
toroidal, λ=12°\lambda = 12° 3.98
toroidal, λ=15°\lambda = 15° 2.74
toroidal, λ=20°\lambda = 20° 2.02
ball end mill of radius 10 1.00

A tilt below 9.594°9.594° gouges the sides. For the ball, the effective radius against the concave surface is 1/(1/10−1/50)=12.51/(1/10 - 1/50) = 12.5, and the strip is 22⋅12.5⋅0.01≈1.002\sqrt{2 \cdot 12.5 \cdot 0.01} \approx 1.00 wide. The PAM strip is eight times wider. The reason is the order of contact: at the PAM tilt the clearance grows like the fourth power of the distance from the contact, and at any other tilt it grows like the square.

Correctness

Theorem. Take a concave circular trough of radius ρ\rho, feed along its axis with a fixed tool axis, and a toroidal end mill with 2R+2r<ρ2R + 2r < \rho. The tool placed by lines L6 to L8 at tilt λ\lambda makes no gouge if and only if sin⁡λ≥R/(ρ−r)\sin\lambda \ge R/(\rho - r). At every point of the cutting edge the clearance to the surface grows with λ\lambda, so the tilt of line L5 is the smallest tilt without a gouge and the closest fit at every point of the edge. At that tilt the cut profile has the radius ρ\rho at the contact, and the clearance is of fourth order in the distance from it.

The proof is in the proof file. The distance of a tool point from the axis of the trough decides a gouge, and by the triangle inequality the worst point is a corner center plus rr. For the corner center at angle tt, with u=1−cos⁡tu = 1 - \cos t and b=Rsin⁡λb = R\sin\lambda,

D(t)2−(ρ−r)2=u[ 2(R2−(ρ−r)b)−u(R2−b2)],D(t)^2 - (\rho - r)^2 = u\big[\,2\big(R^2 - (\rho - r) b\big) - u (R^2 - b^2)\big],

which is at most 0 for all tt exactly when (ρ−r)b≥R2(\rho - r) b \ge R^2. The derivative in bb is negative, which gives the monotone clearance. At equality the first term vanishes, and the clearance is u2(R2−b2)/(2(ρ−r))u^2 (R^2 - b^2)/(2(\rho - r)) to leading order.

The model is stated in full: a cylinder, a feed along the zero-curvature direction, and a tool axis that does not turn during the pass. On a general surface the formula only matches the second-order shape of the surface at pp, and nothing is proved about points away from pp. The tests check the theorem by a grid search over tilts that uses a direct 3D test of the placed tool in the trough and no curvature formula, and by exact rational arithmetic at the tilt sin⁡λ=7/25\sin\lambda = 7/25, where the tool touches the trough at one point of its edge and nowhere goes in.

Proof: On a concave cylinder, the PAM tilt is the smallest tilt without a gouge, it gives the least clearance at every point of the cutting edge, and its effective profile has the curvature of the surfacefull

Model. The part is a concave circular trough of radius ρ\rho. In coordinates (x,y,z)(x, y, z) its axis is the line L={y=0,z=ρ}L = \{y = 0, z = \rho\} and its bottom line is {y=0,z=0}\{y = 0, z = 0\}. A point XX of the tool is in the material (a gouge) when dist(X,L)>ρ\mathrm{dist}(X, L) > \rho. The contact point is p=0p = 0, the normal is n=(0,0,1)n = (0, 0, 1), the minimum curvature direction is emin⁡=(1,0,0)e_{\min} = (1, 0, 0) (curvature 0), and the maximum curvature is κmax⁡=1/ρ\kappa_{\max} = 1/\rho in the direction (0,1,0)(0, 1, 0). The tool feeds along xx with a fixed axis.

The tool is a toroidal end mill. Its center circle CC has radius R>0R > 0 and lies at height r≥0r \ge 0 above the tip plane, and the corner radius is rr. With r=0r = 0 it is a flat end mill. The solid tool is the set of points at axial height h≥0h \ge 0 above the tip with a distance from the tool axis of at most R+r2−(r−h)2R + \sqrt{r^2 - (r - h)^2} for h≤rh \le r, and at most R+rR + r for h≥rh \ge r. Only the part of the tool at or below the height ρ\rho of the trough axis counts, as a trough is open above. Write ρ′=ρ−r\rho' = \rho - r and assume 2R+2r<ρ2R + 2r < \rho, so the corner of the tool is below the height of the trough axis.

For a tilt λ∈(0,π/2]\lambda \in (0, \pi/2], write s=sin⁡λs = \sin\lambda, c=cos⁡λc = \cos\lambda, and place the tool by lines L6 to L8: axis a=c n+s emin⁡=(s,0,c)a = c\,n + s\,e_{\min} = (s, 0, c), direction e=c emin⁡−s n=(c,0,−s)e = c\,e_{\min} - s\,n = (c, 0, -s), tip center t0=p+rn−ra−Ret_0 = p + r n - r a - R e.

Claim. Let λ∗=arcsin⁡(R/ρ′)\lambda^* = \arcsin(R/\rho'), which exists when R≤ρ′R \le \rho'.

(a) The tool at tilt λ\lambda makes no gouge if and only if sin⁡λ≥R/ρ′\sin\lambda \ge R/\rho'.

(b) At each point of the center circle, given by its angle tt from ee, the clearance ρ′−dist(q(t),L)\rho' - \mathrm{dist}(q(t), L) grows with λ\lambda. So λ∗\lambda^* is the smallest tilt without a gouge, and of all tilts without a gouge it puts every point of the cutting edge nearest to the surface. With a lower bound λmin⁡\lambda_{\min}, the best tilt is max⁡(λ∗,λmin⁡)\max(\lambda^*, \lambda_{\min}), which line L5 returns.

(c) At λ∗\lambda^* the cross-section of the swept tool has the radius of curvature ρ\rho at the contact point, the same as the trough. The tool touches the trough along the line {y=0,z=0}\{y = 0, z = 0\} only, and the clearance near it is of fourth order in the distance from the contact.

If R>ρ′R > \rho', every tilt makes a gouge, and line L4 reports it.

Step 1: the gouge test reduces to the center circle. The distance from LL does not depend on xx, so the feed motion along xx does not change it. The function X↦dist(X,L)X \mapsto \mathrm{dist}(X, L) is convex and 1-Lipschitz. For q∈Cq \in C and ∣u∣≤r|u| \le r, dist(q+u,L)≤dist(q,L)+r\mathrm{dist}(q + u, L) \le \mathrm{dist}(q, L) + r, with equality when uu is rr times the unit vector from LL to qq at right angles to LL. The ball q+Brq + B_r is in the tool, since the solid torus C⊕BrC \oplus B_r is the corner of the tool together with points inside it. The rest of the tool is the convex hull of the bottom circle (radius RR, height 0) and of CC, which are in C⊕BrC \oplus B_r, and the shank above height rr. A shank point is Y+haY + h a with YY on the disk of radius R+rR + r at height rr and h≥0h \ge 0. As aa points up, dist(Y+ha,L)\mathrm{dist}(Y + h a, L) decreases in hh while the point stays below the axis height. By convexity, the maximum of the distance over the convex hull and over that disk is on their boundary circles, which are in C⊕BrC \oplus B_r. So

max⁡X∈tooldist(X,L)=max⁡q∈Cdist(q,L)+r,\max_{X \in \text{tool}} \mathrm{dist}(X, L) = \max_{q \in C} \mathrm{dist}(q, L) + r,

and the tool makes no gouge if and only if dist(q,L)≤ρ′\mathrm{dist}(q, L) \le \rho' for every q∈Cq \in C.

Step 2: the center circle in coordinates. The vector w=a×e=(0,1,0)w = a \times e = (0, 1, 0) completes the frame of the tool. The center circle is q(t)=t0+ra+R(cos⁡t e+sin⁡t w)q(t) = t_0 + r a + R(\cos t\, e + \sin t\, w), and t0+ra+Re=p+rn=(0,0,r)t_0 + r a + R e = p + r n = (0, 0, r). So

q(t)=(0,0,r)+R(cos⁡t−1) e+Rsin⁡t w,qy=Rsin⁡t,qz=r+Rs(1−cos⁡t).q(t) = (0, 0, r) + R(\cos t - 1)\, e + R \sin t\, w, \qquad q_y = R\sin t, \qquad q_z = r + R s (1 - \cos t).

Seen along xx, the center circle is an ellipse with semi axes RR and b=Rsb = R s, with its lowest point at height rr above pp. Put u=1−cos⁡t∈[0,2]u = 1 - \cos t \in [0, 2], so sin⁡2t=u(2−u)\sin^2 t = u(2 - u). Then

D(t)2=qy2+(ρ−qz)2=R2u(2−u)+(ρ′−bu)2,D(t)^2 = q_y^2 + (\rho - q_z)^2 = R^2 u (2 - u) + (\rho' - b u)^2,
D(t)2−ρ′2=u [ 2(R2−ρ′b)−u(R2−b2) ].(∗)D(t)^2 - \rho'^2 = u\,\big[\,2(R^2 - \rho' b) - u (R^2 - b^2)\,\big]. \qquad (*)

Proof of (a). Note R2−b2=R2(1−s2)≥0R^2 - b^2 = R^2 (1 - s^2) \ge 0 and u≥0u \ge 0. If ρ′b≥R2\rho' b \ge R^2, both terms in the bracket of (∗)(*) are at most 0, so D(t)≤ρ′D(t) \le \rho' for every tt and there is no gouge by Step 1. If ρ′b<R2\rho' b < R^2, then for 0<u<2(R2−ρ′b)/(R2−b2)0 < u < 2(R^2 - \rho' b)/(R^2 - b^2), or for every u∈(0,2]u \in (0, 2] when b=Rb = R, the bracket is positive and D(t)>ρ′D(t) > \rho', which is a gouge. The condition ρ′b≥R2\rho' b \ge R^2 is ρ′Rs≥R2\rho' R s \ge R^2, that is s≥R/ρ′s \ge R/\rho'. If R>ρ′R > \rho' this needs s>1s > 1, so no tilt works.

Proof of (b). Fix tt, so uu is fixed. As λ\lambda grows in (0,π/2](0, \pi/2], b=Rsin⁡λb = R\sin\lambda grows, and

∂∂bD(t)2=−2u(ρ′−bu)≤0,\frac{\partial}{\partial b} D(t)^2 = -2u(\rho' - b u) \le 0,

since bu≤2R<ρ′b u \le 2R < \rho'. So D(t)D(t) does not grow, and the clearance ρ′−D(t)\rho' - D(t) does not shrink, as λ\lambda grows. By (a), the tilts without a gouge are exactly λ≥λ∗\lambda \ge \lambda^*, so the smallest of them, λ∗\lambda^*, gives the least clearance at every tt. With the bound λ≥λmin⁡\lambda \ge \lambda_{\min}, the feasible set is λ≥max⁡(λ∗,λmin⁡)\lambda \ge \max(\lambda^*, \lambda_{\min}), and the same argument picks its smallest element.

Proof of (c). The swept cross-section is the projection of the tool along xx. By Step 1, near the contact its lower boundary is the lower arc of the ellipse grown by a disk of radius rr, that is, the offset of the ellipse at distance rr. An ellipse with semi axes R≥bR \ge b has the radius of curvature R2/bR^2/b at the ends of its short axis, and the offset at distance rr outward adds rr to the radius of curvature. So the cut profile has radius R2/b+r=R/s+rR^2/b + r = R/s + r at the contact. At s=R/ρ′s = R/\rho' this is ρ′+r=ρ\rho' + r = \rho. At s=R/ρ′s = R/\rho', (∗)(*) becomes D(t)2−ρ′2=−u2(R2−b2)D(t)^2 - \rho'^2 = -u^2(R^2 - b^2), which is 0 only at u=0u = 0 when s<1s < 1, so the tool touches along t=0t = 0 only. As u=t2/2+O(t4)u = t^2/2 + O(t^4), the clearance is ρ′−D(t)=u2(R2−b2)/(2ρ′)+O(u3)=t4(R2−b2)/(8ρ′)+O(t6)\rho' - D(t) = u^2(R^2 - b^2)/(2\rho') + O(u^3) = t^4 (R^2 - b^2)/(8\rho') + O(t^6), which is of fourth order. For s>R/ρ′s > R/\rho' the first term of (∗)(*) is 2u(R2−ρ′b)<02u(R^2 - \rho' b) < 0, and the clearance is of second order in tt. ■\blacksquare

Remarks. (1) The claims hold for the trough, where the feed direction has zero curvature and the axis of the tool is fixed along the feed. On a general surface, PAM uses the same formula with ρ=1/κmax⁡\rho = 1/\kappa_{\max} at each contact point. This matches the second-order shape of the surface in the plane of maximum curvature, but it does not prove the absence of a gouge away from the contact point, and a global gouge check is still needed. (2) The tests check (a) and (b) by a grid search over tilts with a direct 3D test of the tool in the trough, and they check (a) and the single touch point of (c) with exact rational arithmetic at the tilt sin⁡λ=7/25\sin\lambda = 7/25.

Complexity

Each contact point costs O(1)O(1) arithmetic after the principal curvatures and directions are known, which for a parametric surface is also O(1)O(1) from the first and second derivatives. A path of mm contact points takes O(m)O(m) time and O(1)O(1) extra space. The real cost of a PAM tool path is elsewhere: in the global gouge and collision checks that the local formula does not cover, and in choosing the paths and the step between them.

Properties

  • Local. The tilt depends only on the curvature at the contact point.
  • Exact on cylinders. On a concave cylinder with the feed along the axis, the tilt is the smallest that does not gouge and gives the closest fit.
  • Wide strips. At the matched tilt the clearance is of fourth order across the feed, so the strip for a given scallop height is much wider than for a ball end mill.
  • Needs concavity across the feed. On convex or flat parts there is nothing to match, and the method falls back to the minimum tilt.

Variants and improvements

  • Inclined tool method. The older inclined tool method tilts a flat or toroidal end mill by a fixed angle in the feed direction. PAM replaces the fixed angle by the curvature-matched one and ties the tilt to the principal directions.
  • Feed direction. Rao, Ismail and Bedi (1997) compared tool paths with different feed directions while the tilt follows the principal axes. Feeding along the minimum curvature direction makes the swept section equal to the effective profile of the theorem.
  • Multi-point machining. Later methods tilt and rotate the tool so that it touches the surface at two points instead of one, which gives a still wider strip on some surfaces.
  • Rolling ball and arc-intersect methods. These methods of the same school choose the axis from a circle that fits the surface, and they target global gouges as well as local ones.
  • Iso-scallop paths. The effective radius gives the scallop height between passes, so PAM can feed an iso-scallop path planner.

When to use it

Use PAM for five-axis finishing of large, gently curved parts that are concave across the feed, such as dies, molds and turbine passages, with a flat or toroidal end mill. Use a ball end mill, or a gouge-checked method such as a multi-point method, where the curvature changes fast or where the part is small compared with the tool.

Pitfalls

  • Matching only at one point. The formula matches the curvature at pp. If the curvature changes within the width of the tool, the tool can gouge at the edge of the strip. Check the result with gouge detection.
  • Global gouges and collisions. The tilted tool and its holder can hit other parts of the surface. PAM does not see this, so an accessibility or collision-free orientation step is still needed.
  • Too wide a tool. If R>1/κmax⁡−rR > 1/\kappa_{\max} - r, no tilt avoids a gouge. Use a smaller tool or a ball end mill there.
  • Umbilic points. Where κmin⁡=κmax⁡\kappa_{\min} = \kappa_{\max}, the principal directions are not defined, and the tilt direction can jump. Smooth the directions or fix the feed direction near such points.
  • Sign of curvature. The formula needs the curvature with the sign that is positive for a concave part, as seen from the tool. With the other sign the method tilts on a convex part and wastes the fit.
  • Feed curvature. The theorem assumes zero curvature along the feed. A curved feed direction also bends the swept volume, and the effective profile changes.

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.

principal_axis_method.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/principal-axis-method/. The authors are listed in prov/WFBXPX.jsonl.

"""Principal axis method (PAM) for five-axis milling with a toroidal end mill.

The tool is a toroidal end mill: a flat bottom disk of radius R and a corner
of radius r. The centers of the corner circles form the center circle C, of
radius R, in the plane at height r above the tool tip. With r = 0 the tool
is a flat end mill.

At a cutter contact point p the surface has the unit normal n (pointing out
of the material, toward the tool), the unit direction e_min of minimum
normal curvature and the maximum normal curvature kappa_max. A curvature is
positive where the surface bends toward the tool (a concave part, such as
the inside of a trough).

PAM tilts the tool axis away from n by the angle lam in the plane of n and
e_min. Seen along e_min, the tilted center circle is an ellipse with semi
axes R and R sin(lam). Its radius of curvature at the lowest point is
R / sin(lam), so the tool cuts a profile of radius R / sin(lam) + r in the
plane of maximum curvature. PAM picks the smallest tilt for which this
radius is not less than 1 / kappa_max:

    sin(lam) = R / (1/kappa_max - r).

Numbers may be float or Fraction. With Fractions, tool_pose_sc takes the sine
and cosine of the tilt and is exact.
"""

import math


def _add(u, v):
    return tuple(a + b for a, b in zip(u, v))


def _scale(k, u):
    return tuple(k * a for a in u)


def _cross(u, v):
    return (u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0])


def pam_tilt(R, r, kappa_max, lam_min=0.0):
    """The PAM tilt angle in radians, from the tool and the maximum curvature."""
    if R <= 0 or r < 0:
        raise ValueError("the tool needs R > 0 and r >= 0")
    if kappa_max <= 0:  # @line L1
        return lam_min  # @line L2
    rho = 1 / kappa_max  # @line L3
    if R > rho - r:  # @line L4
        raise ValueError("no tilt avoids a gouge: the tool is too wide for the curvature")
    return max(math.asin(R / (rho - r)), lam_min)  # @line L5


def tool_pose_sc(p, n, e_min, s, c, R, r):
    """Place the tool for a tilt with sine s and cosine c.

    Returns (axis, tip, e): the unit tool axis, the center of the tool tip
    (the bottom disk) and the unit direction e in the bottom plane from the
    axis to the lowest point of the center circle.
    """
    axis = _add(_scale(c, n), _scale(s, e_min))  # @line L6
    e = _add(_scale(c, e_min), _scale(-s, n))  # @line L7
    tip = _add(_add(p, _scale(r, n)), _add(_scale(-r, axis), _scale(-R, e)))  # @line L8
    return axis, tip, e


def tool_pose(p, n, e_min, lam, R, r):
    """Place the tool for the tilt angle lam (radians)."""
    return tool_pose_sc(p, n, e_min, math.sin(lam), math.cos(lam), R, r)


def principal_axis_method(p, n, e_min, kappa_max, R, r, lam_min=0.0):
    """Run PAM at one contact point. Returns (lam, axis, tip)."""
    lam = pam_tilt(R, r, kappa_max, lam_min)
    axis, tip, _ = tool_pose(p, n, e_min, lam, R, r)
    return lam, axis, tip  # @line L9


def center_circle_point(axis, tip, e, R, r, cos_t, sin_t):
    """The point of the center circle at the angle t from e, given cos t and sin t."""
    w = _cross(axis, e)
    return _add(_add(tip, _scale(r, axis)), _add(_scale(R * cos_t, e), _scale(R * sin_t, w)))
test_principal_axis_method.py 169 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/principal-axis-method/. The authors are listed in prov/WFBXPX.jsonl.

import math
import random
from fractions import Fraction as F

import pytest

from principal_axis_method import (
    center_circle_point,
    pam_tilt,
    principal_axis_method,
    tool_pose,
    tool_pose_sc,
)

# The test part is a concave trough: the inside of a cylinder of radius rho
# whose axis is the line y = 0, z = rho. The contact point is the bottom of
# the trough, p = 0, with the normal n = +z. The minimum curvature direction
# is the axis direction +x (curvature 0) and the maximum curvature is 1/rho,
# across the trough. The tool feeds along +x.
P = (0, 0, 0)
N = (0, 0, 1)
E_MIN = (1, 0, 0)


def axis_distance(q, rho):
    return math.hypot(q[1], q[2] - rho)


def gouge_and_clearance(lam, R, r, rho, samples=360):
    """Direct 3D check of the tool placed with tilt lam in the trough.

    The solid tool is the center circle C grown by a ball of radius r, plus
    parts that are nearer to the trough axis. A tool point is in the material
    when its distance from the trough axis exceeds rho. So the gouge depth is
    max over C of dist(q, axis) + r - rho, and the mean clearance of the
    cutting edge is the mean over C of rho - r - dist(q, axis). No curvature
    formula is used here.
    """
    axis, tip, e = tool_pose(P, N, E_MIN, lam, R, r)
    worst = -math.inf
    total = 0.0
    for i in range(samples):
        t = 2 * math.pi * i / samples
        q = center_circle_point(axis, tip, e, R, r, math.cos(t), math.sin(t))
        d = axis_distance(q, rho)
        worst = max(worst, d + r - rho)
        total += rho - r - d
    return worst, total / samples


def grid_search(R, r, rho, step_deg=0.1, max_deg=30.0, tol=1e-9):
    """The tilt on a grid that keeps the tool closest to the trough without a gouge."""
    best = None
    k = 1
    while k * step_deg <= max_deg:
        lam = math.radians(k * step_deg)
        gouge, clearance = gouge_and_clearance(lam, R, r, rho)
        if gouge <= tol and (best is None or clearance < best[1]):
            best = (lam, clearance)
        k += 1
    return best[0]


def test_tilt_example():
    lam = pam_tilt(8.0, 2.0, 1 / 50.0)
    assert math.isclose(math.sin(lam), 1 / 6)
    assert math.isclose(math.degrees(lam), 9.594068226860461)
    assert math.isclose(8.0 / math.sin(lam) + 2.0, 50.0)


@pytest.mark.parametrize("seed", [None, 1, 2, 3])
def test_matches_grid_search_on_cylinder(seed):
    if seed is None:
        R, r, rho = 8.0, 2.0, 50.0
    else:
        rng = random.Random(seed)
        R = rng.uniform(2.0, 10.0)
        r = rng.choice([0.0, rng.uniform(0.5, 3.0)])
        rho = (R + r) * rng.uniform(2.5, 8.0)
    lam = pam_tilt(R, r, 1 / rho)
    found = grid_search(R, r, rho)
    assert abs(found - lam) < math.radians(0.1) + 1e-12
    gouge, _ = gouge_and_clearance(lam, R, r, rho)
    assert gouge <= 1e-9


def test_smaller_tilt_gouges_larger_tilt_leaves_more():
    R, r, rho = 8.0, 2.0, 50.0
    lam = pam_tilt(R, r, 1 / rho)
    g_small, _ = gouge_and_clearance(lam - math.radians(0.5), R, r, rho)
    g_star, c_star = gouge_and_clearance(lam, R, r, rho)
    g_big, c_big = gouge_and_clearance(lam + math.radians(0.5), R, r, rho)
    assert g_small > 1e-6
    assert g_star <= 1e-9 and g_big <= 1e-9
    assert c_big > c_star


def exact_distance2(s, c, R, r, rho, t):
    """Squared distance from the trough axis of a center circle point, exactly.

    The angle on the circle is given by its rational parameter t:
    cos = (1 - t^2) / (1 + t^2), sin = 2t / (1 + t^2).
    """
    axis, tip, e = tool_pose_sc(P, N, E_MIN, s, c, R, r)
    cos_t = (1 - t * t) / (1 + t * t)
    sin_t = 2 * t / (1 + t * t)
    q = center_circle_point(axis, tip, e, R, r, cos_t, sin_t)
    return q[1] * q[1] + (q[2] - rho) ** 2


def test_exact_touch_at_the_pythagorean_tilt():
    # R = 7, r = 1, rho = 26: sin(lam) = 7 / 25 and cos(lam) = 24 / 25 are rational.
    R, r, rho = F(7), F(1), F(26)
    s, c = F(7, 25), F(24, 25)
    assert s == R / (rho - r)
    limit = (rho - r) ** 2
    ts = [F(k, 20) for k in range(-400, 401)]
    for t in ts:
        d2 = exact_distance2(s, c, R, r, rho, t)
        assert d2 <= limit
        assert (d2 == limit) == (t == 0)


def test_exact_gouge_below_the_tilt():
    # sin = 9 / 41 < 7 / 25: some point of the tool is in the material.
    R, r, rho = F(7), F(1), F(26)
    limit = (rho - r) ** 2
    ts = [F(k, 20) for k in range(-40, 41)]
    for s, c in [(F(9, 41), F(40, 41)), (F(11, 61), F(60, 61))]:
        assert any(exact_distance2(s, c, R, r, rho, t) > limit for t in ts)
    # and a larger tilt, sin = 8 / 17, has no contact except the bottom
    s, c = F(8, 17), F(15, 17)
    for t in ts:
        d2 = exact_distance2(s, c, R, r, rho, t)
        assert d2 <= limit and (d2 == limit) == (t == 0)


def test_pose_touches_p_with_normal_n():
    rng = random.Random(7)
    for _ in range(50):
        R = rng.uniform(1, 10)
        r = rng.uniform(0, 3)
        lam = rng.uniform(0, 1.2)
        axis, tip, e = tool_pose(P, N, E_MIN, lam, R, r)
        assert math.isclose(sum(a * a for a in axis), 1.0)
        assert abs(sum(a * b for a, b in zip(axis, e))) < 1e-12
        q = center_circle_point(axis, tip, e, R, r, 1.0, 0.0)
        # the lowest corner center is r above p along n
        assert all(abs(a - b) < 1e-12 for a, b in zip(q, (0.0, 0.0, r)))


def test_convex_and_flat_use_the_minimum_tilt():
    assert pam_tilt(8.0, 2.0, -1 / 40.0, lam_min=0.05) == 0.05
    assert pam_tilt(8.0, 2.0, 0.0, lam_min=0.05) == 0.05
    # a hard minimum tilt wins over a small matched tilt
    assert pam_tilt(8.0, 2.0, 1 / 1000.0, lam_min=0.1) == 0.1


def test_whole_method_and_errors():
    lam, axis, tip = principal_axis_method(P, N, E_MIN, 1 / 50.0, 8.0, 2.0)
    assert math.isclose(math.sin(lam), 1 / 6)
    assert axis[1] == 0 and axis[0] > 0 and axis[2] > 0
    with pytest.raises(ValueError):
        pam_tilt(8.0, 2.0, 1 / 9.0)  # R = 8 > rho - r = 7
    with pytest.raises(ValueError):
        pam_tilt(0.0, 2.0, 1 / 50.0)

History

The inclined tool method of the 1980s tilted a flat end mill by a fixed angle so that its rim fitted the surface better than a ball. Rao, Bedi and Buchal (1996) described an implementation of the principal axis method, which ties the tilt to the principal curvature directions and matches the effective radius to the maximum curvature, and Rao, Ismail and Bedi (1997) used it to study how the feed direction changes five-axis tool paths, with machining tests on two surfaces. The group at the University of Waterloo later developed the multi-point, rolling ball and arc-intersect methods from the same idea of fitting the tool to the local shape of the part.

References

  1. N. Rao, F. Ismail and S. Bedi, Tool path planning for five-axis machining using the principal axis method, International Journal of Machine Tools and Manufacture 37(7), 1025-1040, 1997, the principal axis method and the comparison of tool paths with different feed directions
  2. N. Rao, S. Bedi and R. Buchal, Implementation of the principal-axis method for machining of complex surfaces, The International Journal of Advanced Manufacturing Technology 11(4), 249-257, 1996, the tilt of the tool in the plane of minimum curvature and the effective radius of the tool in the plane of maximum curvature
  3. Byoung K. Choi and Robert B. Jerard, Sculptured Surface Machining, Theory and Applications, Kluwer Academic Publishers, 1998, the material on tool positioning for five-axis machining with flat and toroidal end mills

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