Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / fillet_tests.luc

## Fillets meeting at corners, checked by volume: two meeting where a third
## edge stays sharp (a miter), three rounded by a ball (a sphere corner), and
## every edge of a box by reference against the box rounded whole; a box
## cut by a slanted plane, whose corners are not square, both ways.
from luce_cad.modeling import CadModeling
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_geocore.geocore import Vector3
from checks import manifold, closed, volume
from luce_std import math

## The straight edges of `model` that `keep` takes (by their ends).
func edges_where(model: CadModel, keep: func(Vector3, Vector3) -> bool) -> list[int]!:
    let found: list[int] = []
    for face in 0..<model.face_count():
        for use in 0..<model.face_edge_count(face):
            if not model.face_edge_linear(face, use):
                continue
            let id = model.face_edge_id(face, use)
            if keep(model.face_edge_point(face, use, 0.0), model.face_edge_point(face, use, 1.0)) and not (id in found):
                found.append(id)
    return found

## `model` with `edges` filleted to `r`: `faces` faces, closed, of volume
## `expected` within `tolerance` (unchecked when negative). Square corners
## also face their display triangles out (manifold); the sphere patches of
## slanted corners leave slivers too thin to say.
func filleted(model: CadModel, edges: list[int], r: float, faces: int, expected: float, tolerance: float, square: bool) -> float!:
    let made = CadModeling.filleted(model, CadModeling.edge_references(model, edges), r, "#f")
    if made.face_count() != faces: print(f"faces {made.face_count()}, expected {faces}")
    assert(made.face_count() == faces)
    let mesh = made.tessellate(32)
    if square:
        manifold(mesh)
    else:
        closed(mesh)
    let v = volume(mesh)
    if tolerance >= 0.0 and math.abs(v - expected) > tolerance: print(f"volume {v}, expected {expected}")
    assert(tolerance < 0.0 or math.abs(v - expected) <= tolerance)
    return v

func top(a: Vector3, b: Vector3) -> bool:
    return a.y > 0.99 and b.y > 0.99

func top_or_upright(a: Vector3, b: Vector3) -> bool:
    return (a.y > 0.99 and b.y > 0.99) or math.abs(a.y - b.y) > 0.5

func any(a: Vector3, b: Vector3) -> bool:
    return true

## Edges with an end at `corner`.
func at(corner: Vector3) -> func(Vector3, Vector3) -> bool:
    return func(a: Vector3, b: Vector3) -> bool:
        return a.subtract(corner).length() < 1e-6 or b.subtract(corner).length() < 1e-6

pub func fillet_tests() -> unit!:
    let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#b")
    let r = 0.3
    # The corner region a fillet takes from a square edge, per length.
    let a = r * r * (1.0 - math.pi * 0.25)
    # Two top edges meeting at a corner whose upright edge stays sharp: a
    # miter, where the regions both take overlap by r^3 (5/3 - pi/2).
    let two = edges_where(box, at(Vector3(1.0, 1.0, 1.0)))
    let pair = edges_where(box, func(p: Vector3, q: Vector3) -> bool:
        return top(p, q) and (p.subtract(Vector3(1.0, 1.0, 1.0)).length() < 1e-6 or q.subtract(Vector3(1.0, 1.0, 1.0)).length() < 1e-6))
    assert(two.length == 3 and pair.length == 2)
    _ = filleted(box, pair, r, 8, 8.0 - 4.0 * a + r * r * r * (5.0 / 3.0 - math.pi * 0.5), 0.001, true)
    # The three at that corner: a ball rounds it. Each edge reaches the
    # ball's plane (2 - r long), and the corner cube loses all but an eighth
    # of the ball.
    let corner = CadModeling.filleted(box, CadModeling.edge_references(box, two), r, "#f")
    var corners = 0
    for face in 0..<corner.face_count():
        if corner.face_name(face).starts_with("#f/corner["):
            corners += 1
    assert(corners == 1)
    _ = filleted(box, two, r, 10, 8.0 - 3.0 * (2.0 - r) * a - r * r * r * (1.0 - math.pi / 6.0), 0.001, true)
    # The top ring and the uprights: four balls at the top, the uprights
    # running out past the bottom.
    _ = filleted(box, edges_where(box, top_or_upright), r, 18, 8.0 - 4.0 * (2.0 - 2.0 * r) * a - 4.0 * (2.0 - r) * a - 4.0 * r * r * r * (1.0 - math.pi / 6.0), 0.001, true)
    # Every edge by reference: the rounded box, as the box rounded whole.
    let shrunk = 2.0 - 2.0 * r
    let rounded = shrunk * shrunk * shrunk + 6.0 * shrunk * shrunk * r + 3.0 * math.pi * r * r * shrunk + 4.0 / 3.0 * math.pi * r * r * r
    _ = filleted(box, edges_where(box, any), r, 26, rounded, 0.001, true)
    # Its corners mesh on a barycentric grid (mesher 7): near-equilateral
    # triangles between their arcs' samples, no pole.
    let corner_mesh = CadModeling.filleted(box, "", r, "#w").tessellate(16)
    let mesher = corner_mesh.find_attribute("cad_mesher", 2)
    var gridded = 0
    for polygon in 0..<corner_mesh.face_count():
        if int(corner_mesh.attribute_value(mesher, polygon)) == 7:
            gridded += 1
    assert(gridded > 0 and gridded < 8 * 600)
    # A box cut by a slanted plane (corners of 104 and 76 degrees): every
    # edge by reference and the body rounded whole agree; a slanted corner's
    # three edges.
    let cutter = CadModeling.tagged(CadSolids.box(4.0, 4.0, 4.0), "#c").transformed(Vector3(2.6, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0)).transformed(Vector3(), Vector3(0.0, 0.25, 0.0), Vector3(1.0, 1.0, 1.0))
    let slanted = CadModeling.combined(box, cutter, 1)
    assert(slanted.face_count() == 6)
    let whole = CadModeling.filleted(slanted, "", 0.2, "#w")
    let whole_mesh = whole.tessellate(32)
    manifold(whole_mesh)
    let by_reference = filleted(slanted, edges_where(slanted, any), 0.2, 26, volume(whole_mesh), 0.0005, false)
    assert(by_reference < volume(slanted.tessellate(16)))
    var slant_corner = Vector3(-10.0, 0.0, 0.0)
    for face in 0..<slanted.face_count():
        for use in 0..<slanted.face_edge_count(face):
            let p = slanted.face_edge_point(face, use, 0.0)
            if p.y > 0.99 and p.z > 0.99 and p.x > slant_corner.x:
                slant_corner = p
    _ = filleted(slanted, edges_where(slanted, at(slant_corner)), 0.2, 10, 0.0, -1.0, false)
    inside_corners()
    pocket_corners()
    chains()
    mixed()
    too_large()
    cone_rims()
    flats()
    smooth()
    print("PASS fillets: two meeting in a miter, three rounded by a ball, the top ring with uprights, every edge by reference as the box rounded whole; a slanted box's corners; inside corners (an L's edge filleted and chamfered, ending square or slanted, beside an outside fillet; a boss's rim); G2 and G3 fillets outside and inside, alone, apart, mitered, on a hole's rim, and three at a corner (Fusion's Rolling ball corner, three quads; Setback corners: a box's, every edge, a pocket's, slanted); a pocket's inside corners, mitered and balled; tangent chains around a slot's rim and an earlier fillet; inside and outside fillets meeting (a boss on a plate, an L's inside corner); sizes too large, said with the size that fits; a cone's rims; a D-shaft's flat meeting its cylinder; a fillet kept between radii (its corners fitted once)")

func blend(model: CadModel, edges: list[int], r: float, round: bool, faces: int, expected: float, tolerance: float) -> unit!:
    let refs = CadModeling.edge_references(model, edges)
    let made = CadModeling.filleted(model, refs, r, "#f") if round else CadModeling.chamfered(model, refs, r, "#f")
    assert(made.face_count() == faces)
    let mesh = made.tessellate(32)
    manifold(mesh)
    let v = volume(mesh)
    if tolerance >= 0.0 and math.abs(v - expected) > tolerance: print(f"volume {v}, expected {expected}")
    assert(tolerance < 0.0 or math.abs(v - expected) <= tolerance)

## The L's inside edge: along z at x = 0, y = 0.
func inner(a: Vector3, b: Vector3) -> bool:
    return math.abs(a.x) < 1e-6 and math.abs(a.y) < 1e-6 and math.abs(b.x) < 1e-6 and math.abs(b.y) < 1e-6

func inner_or_wall_top(a: Vector3, b: Vector3) -> bool:
    return inner(a, b) or (math.abs(a.x + 1.0) < 1e-6 and math.abs(b.x + 1.0) < 1e-6 and a.y > 0.99 and b.y > 0.99)

func inner_or_slab_front(a: Vector3, b: Vector3) -> bool:
    return inner(a, b) or (math.abs(a.z - 1.0) < 1e-6 and math.abs(b.z - 1.0) < 1e-6 and math.abs(a.y) < 1e-6 and math.abs(b.y) < 1e-6 and a.x > -1e-6 and b.x > -1e-6)

func boss_rim(a: Vector3, b: Vector3) -> bool:
    return math.abs(a.y - 0.5) < 1e-6

## Edges whose carriers are not lines that `keep` takes.
func round_edges_where(model: CadModel, keep: func(Vector3, Vector3) -> bool) -> list[int]!:
    let found: list[int] = []
    for face in 0..<model.face_count():
        for use in 0..<model.face_edge_count(face):
            if model.face_edge_linear(face, use):
                continue
            let id = model.face_edge_id(face, use)
            if keep(model.face_edge_point(face, use, 0.0), model.face_edge_point(face, use, 1.0)) and not (id in found):
                found.append(id)
    return found

## Inside corners take material: an L (a slab and a wall joined) filleted
## and chamfered along its inside edge, the corner region added; the same
## edge ending on a slanted face; beside an outside fillet (one adds what
## the other takes); a boss's rim on a plate, by Pappus; one meeting an
## outside fillet where the corner's third edge stays sharp (a Y corner).
func inside_corners() -> unit!:
    let slab = CadModeling.tagged(CadSolids.box(2.0, 1.0, 2.0), "#s").transformed(Vector3(0.0, -0.5, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let wall = CadModeling.tagged(CadSolids.box(1.0, 2.0, 2.0), "#w").transformed(Vector3(-0.5, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let l = CadModeling.combined(slab, wall, 0)
    let r = 0.3
    let a = r * r * (1.0 - math.pi * 0.25)
    let edge = edges_where(l, inner)
    assert(edge.length == 1)
    blend(l, edge, r, true, 9, 6.0 + 2.0 * a, 0.001)
    blend(l, edge, r, false, 9, 6.0 + r * r, 0.001)
    blend(l, edges_where(l, inner_or_wall_top), r, true, 10, 6.0, 0.001)
    # With the slab's front edge (the wall's front edge sharp): a Y corner,
    # the front edge's fillet run on to the wall, the inside one's section
    # rolled round its end (y_blend_tests): the strips and the torus's
    # spandrel, r³ times the unit one's 0.2616735.
    blend(l, edges_where(l, inner_or_slab_front), r, true, 11, 6.0 + (1.0 - r) * a + r * r * r * 0.2616735, 0.001)
    # Its +z end cut at a slant: the fillet ends on the slanted face.
    let cutter = CadModeling.tagged(CadSolids.box(6.0, 6.0, 4.0), "#k").transformed(Vector3(0.0, 0.0, 2.8), Vector3(), Vector3(1.0, 1.0, 1.0)).transformed(Vector3(), Vector3(0.0, 0.3, 0.0), Vector3(1.0, 1.0, 1.0))
    let cut = CadModeling.combined(l, cutter, 1)
    let before = volume(cut.tessellate(16))
    blend(cut, edges_where(cut, inner), r, true, 10, before + a * 1.8, 0.01)
    # A boss of radius 0.5 on a plate: the corner region's centroid is
    # `reach` out from the boss.
    let plate = CadModeling.tagged(CadSolids.box(3.0, 1.0, 3.0), "#p")
    let boss = CadModeling.tagged(CadSolids.cylinder(0.5, 1.0), "#c").transformed(Vector3(0.0, 1.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let bossed = CadModeling.combined(plate, boss, 0)
    let reach = r * (10.0 - 3.0 * math.pi) / (3.0 * (4.0 - math.pi))
    blend(bossed, round_edges_where(bossed, boss_rim), r, true, 9, volume(bossed.tessellate(32)) + 2.0 * math.pi * (0.5 + reach) * a, 0.001)

## G2 and G3 fillets: degree 5 cross-sections between the G1 fillet's
## contact points. For a square corner of setback r they take r^2 times
## these areas (the curves' control points at fractions 0, 0.3, 0.6 and,
## over three spans, 0, 0.1, 0.2, 0.6 of the way to the corner, from each
## contact point).
func smooth() -> unit!:
    let g2 = 0.16824175009718
    let g3 = 0.1641116415388808
    let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#b")
    let r = 0.4
    let upright = edges_where(box, func(a: Vector3, b: Vector3) -> bool:
        return a.x > 0.99 and b.x > 0.99 and a.z > 0.99 and b.z > 0.99)
    smoothed(box, upright, r, 2, 7, 8.0 - 2.0 * r * r * g2)
    smoothed(box, upright, r, 3, 7, 8.0 - 2.0 * r * r * g3)
    let apart = edges_where(box, func(a: Vector3, b: Vector3) -> bool:
        return a.y > 0.99 and b.y > 0.99 and ((a.z > 0.99 and b.z > 0.99) or (a.z < -0.99 and b.z < -0.99)))
    smoothed(box, apart, r, 2, 8, 8.0 - 4.0 * r * r * g2)
    # Inside an L.
    let slab = CadModeling.tagged(CadSolids.box(2.0, 1.0, 2.0), "#s").transformed(Vector3(0.0, -0.5, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let wall = CadModeling.tagged(CadSolids.box(1.0, 2.0, 2.0), "#w").transformed(Vector3(-0.5, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let l = CadModeling.combined(slab, wall, 0)
    smoothed(l, edges_where(l, inner), 0.3, 2, 9, 6.0 + 2.0 * 0.09 * g2)
    smoothed(l, edges_where(l, inner), 0.3, 3, 9, 6.0 + 2.0 * 0.09 * g3)
    # Two meeting where a third edge stays sharp: a miter, their curve an
    # affine copy of the profile in the bisecting plane. The regions overlap
    # by r^3 times the integral of the profile curve's height squared:
    # 0.0667460 (G2), 0.0643052 (G3) (0.0958703 for a circle).
    let pair = edges_where(box, func(a: Vector3, b: Vector3) -> bool:
        return a.y > 0.99 and b.y > 0.99 and (a.subtract(Vector3(1.0, 1.0, 1.0)).length() < 1e-6 or b.subtract(Vector3(1.0, 1.0, 1.0)).length() < 1e-6))
    smoothed(box, pair, r, 2, 8, 8.0 - 4.0 * r * r * g2 + 0.0667460 * r * r * r)
    smoothed(box, pair, r, 3, 8, 8.0 - 4.0 * r * r * g3 + 0.0643052 * r * r * r)
    let ring = edges_where(box, top)
    let mitered = CadModeling.filleted(box, CadModeling.edge_references(box, ring), r, "#f", 2)
    assert(mitered.face_count() == 10)
    closed(mitered.tessellate(32))
    setback_corners()
    fillet_plans()
    # A hole's rim (radius 0.5, r 0.2): one strip round it (a loop round the
    # top face: cap_blend.lucb), by Pappus, the regions' centroids 0.19836 r
    # (G2) and 0.19592 r (G3) out from the hole.
    let plate = CadModeling.tagged(CadSolids.box(3.0, 1.0, 3.0), "#p")
    let drilled = CadModeling.combined(plate, CadModeling.tagged(CadSolids.cylinder(0.5, 2.0), "#h"), 1)
    let rim = round_edges_where(drilled, top_rim)
    let before = volume(drilled.tessellate(32))
    let rr = 0.2
    smoothed(drilled, rim, rr, 2, 8, before - 2.0 * math.pi * (0.5 + 0.19836347 * rr) * g2 * rr * rr)
    smoothed(drilled, rim, rr, 3, 8, before - 2.0 * math.pi * (0.5 + 0.19591901 * rr) * g3 * rr * rr)

func smoothed(model: CadModel, edges: list[int], r: float, continuity: int, faces: int, expected: float, setback: bool = false) -> unit!:
    let made = CadModeling.filleted(model, CadModeling.edge_references(model, edges), r, "#f", continuity, setback = setback)
    assert(made.face_count() == faces)
    let mesh = made.tessellate(32)
    # A G3 blend meets its faces so flatly that a display sliver there may
    # face either way: closed, then.
    if continuity == 2:
        manifold(mesh)
    else:
        closed(mesh)
    let v = volume(mesh)
    if math.abs(v - expected) > 0.0002: print(f"volume {v}, expected {expected}")
    assert(math.abs(v - expected) <= 0.0002)

## The pocket's floor edges (y = 0, inside |x|, |z| <= 1).
func pocket_floor(a: Vector3, b: Vector3) -> bool:
    return math.abs(a.y) < 1e-6 and math.abs(b.y) < 1e-6 and math.abs(a.x) < 1.0001 and math.abs(a.z) < 1.0001 and math.abs(b.x) < 1.0001 and math.abs(b.z) < 1.0001

## Those and its walls' upright edges.
func pocket_floor_and_walls(a: Vector3, b: Vector3) -> bool:
    let upright = math.abs(a.x - b.x) < 1e-9 and math.abs(a.z - b.z) < 1e-9 and math.abs(math.abs(a.x) - 1.0) < 1e-4 and math.abs(math.abs(a.z) - 1.0) < 1e-4 and a.y > -1e-6 and b.y > -1e-6
    return pocket_floor(a, b) or upright

## A 2 x 1 x 2 pocket in a block: its floor edges filleted meet in miters
## (the regions added overlap by r^3 (5/3 - pi/2) at each corner, as
## outside miters do); with its walls' edges too, balls round the corners,
## material added around them.
func pocket_corners() -> unit!:
    let block = CadModeling.tagged(CadSolids.box(4.0, 2.0, 4.0), "#k")
    let cut = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#c").transformed(Vector3(0.0, 1.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let pocket = CadModeling.combined(block, cut, 1)
    let r = 0.3
    let a = r * r * (1.0 - math.pi * 0.25)
    blend(pocket, edges_where(pocket, pocket_floor), r, true, 15, 28.0 + 8.0 * a - 4.0 * r * r * r * (5.0 / 3.0 - math.pi * 0.5), 0.001)
    blend(pocket, edges_where(pocket, pocket_floor_and_walls), r, true, 23, 28.0 + 4.0 * (2.0 - 2.0 * r) * a + 4.0 * (1.0 - r) * a + 4.0 * r * r * r * (1.0 - math.pi / 6.0), 0.001)

## The first straight edge `keep` takes.
func first_edge(model: CadModel, keep: func(Vector3, Vector3) -> bool) -> int!:
    for face in 0..<model.face_count():
        for use in 0..<model.face_edge_count(face):
            if model.face_edge_linear(face, use) and keep(model.face_edge_point(face, use, 0.0), model.face_edge_point(face, use, 1.0)):
                return model.face_edge_id(face, use)
    assert(false)
    return -1

func slot_top(a: Vector3, b: Vector3) -> bool:
    return a.y > 0.49 and b.y > 0.49 and math.abs(a.z - 0.5) < 1e-6 and math.abs(b.z - 0.5) < 1e-6

func top_front(a: Vector3, b: Vector3) -> bool:
    return a.y > 0.99 and b.y > 0.99 and a.z > 0.99 and b.z > 0.99

func front_right(a: Vector3, b: Vector3) -> bool:
    return a.x > 0.99 and b.x > 0.99 and a.z > 0.99 and b.z > 0.99

## One edge picked brings its tangent chain: a rounded slot's rim (two
## lines, two half circles: their tools part-way tori and cones, ending
## square where they meet), filleted and chamfered, by Pappus; a box's top
## front edge after its front right upright was rounded, the chain running
## around the earlier fillet's arc to the side.
func chains() -> unit!:
    let plate = CadModeling.tagged(CadSolids.box(6.0, 1.0, 4.0), "#p")
    let middle = CadModeling.tagged(CadSolids.box(2.0, 2.0, 1.0), "#m")
    let end0 = CadModeling.tagged(CadSolids.cylinder(0.5, 2.0), "#e0").transformed(Vector3(-1.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let end1 = CadModeling.tagged(CadSolids.cylinder(0.5, 2.0), "#e1").transformed(Vector3(1.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let slotted = CadModeling.combined(plate, CadModeling.combined(CadModeling.combined(middle, end0, 0), end1, 0), 1)
    let before = volume(slotted.tessellate(32))
    let r = 0.2
    let a = r * r * (1.0 - math.pi * 0.25)
    let reach = r * (10.0 - 3.0 * math.pi) / (3.0 * (4.0 - math.pi))
    let edge = [first_edge(slotted, slot_top)]
    blend(slotted, edge, r, true, 14, before - 4.0 * a - 2.0 * math.pi * (0.5 + reach) * a, 0.001)
    blend(slotted, edge, r, false, 14, before - 2.0 * r * r - 2.0 * math.pi * (0.5 + r / 3.0) * r * r * 0.5, 0.001)
    # Unchained: that line alone, its fillet ending in a wall at each end
    # where the rim curves on.
    let alone = CadModeling.filleted(slotted, CadModeling.edge_references(slotted, edge), r, "#f", 1, false)
    assert(alone.face_count() == 13)
    closed(alone.tessellate(16))
    let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#b")
    let rounded = CadModeling.filleted(box, CadModeling.edge_references(box, [first_edge(box, front_right)]), 0.5, "#r")
    let around = CadModeling.filleted(rounded, CadModeling.edge_references(rounded, [first_edge(rounded, top_front)]), r, "#f")
    assert(around.face_count() == 10)
    let mesh = around.tessellate(32)
    # Closed; its bottom face may mesh a sliver (a tessellation matter).
    closed(mesh)
    let expected = volume(rounded.tessellate(64)) - 2.0 * 1.5 * a - 0.5 * math.pi * (0.5 - reach) * a
    assert(math.abs(volume(mesh) - expected) < 0.001)

## A unit boss's uprights and its base edges on the plate.
func boss_edges(a: Vector3, b: Vector3) -> bool:
    let upright = math.abs(a.x - b.x) < 1e-9 and math.abs(a.z - b.z) < 1e-9 and math.abs(math.abs(a.x) - 0.5) < 1e-6 and math.abs(math.abs(a.z) - 0.5) < 1e-6 and a.y > 0.49 and b.y > 0.49
    let base = math.abs(a.y - 0.5) < 1e-6 and math.abs(b.y - 0.5) < 1e-6 and math.abs(a.x) < 0.5001 and math.abs(b.x) < 0.5001 and math.abs(a.z) < 0.5001 and math.abs(b.z) < 0.5001
    return upright or base

## An L's inside upright (x = z = 0), and the top edges reaching it.
func l_corner(a: Vector3, b: Vector3) -> bool:
    let upright = math.abs(a.x) < 1e-6 and math.abs(a.z) < 1e-6 and math.abs(b.x) < 1e-6 and math.abs(b.z) < 1e-6
    let corner = Vector3(0.0, 1.0, 0.0)
    let on_top = a.y > 0.999 and b.y > 0.999 and (a.subtract(corner).length() < 1e-6 or b.subtract(corner).length() < 1e-6)
    return upright or on_top

## Inside and outside fillets meeting at corners, by order. A boss on a
## plate (an outside upright, two inside base edges at each corner): the
## uprights first, then the base runs around them (a quarter torus each,
## its region's centroid r + reach from the upright's axis). An L's inside
## corner (two outside top edges, the inside upright): the upright first,
## then the top edges run around it.
func mixed() -> unit!:
    let r = 0.2
    let a = r * r * (1.0 - math.pi * 0.25)
    let reach = r * (10.0 - 3.0 * math.pi) / (3.0 * (4.0 - math.pi))
    let plate = CadModeling.tagged(CadSolids.box(4.0, 1.0, 4.0), "#p")
    let boss = CadModeling.tagged(CadSolids.box(1.0, 1.0, 1.0), "#s").transformed(Vector3(0.0, 1.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let bossed = CadModeling.combined(plate, boss, 0)
    let edges = edges_where(bossed, boss_edges)
    assert(edges.length == 8)
    let made = CadModeling.filleted(bossed, CadModeling.edge_references(bossed, edges), r, "#f")
    let mesh = made.tessellate(32)
    closed(mesh)
    assert(math.abs(volume(mesh) - (17.0 - 4.0 * a + 4.0 * (1.0 - 2.0 * r) * a + 2.0 * math.pi * (r + reach) * a)) < 0.0005)
    let a1 = CadModeling.tagged(CadSolids.box(2.0, 1.0, 1.0), "#a").transformed(Vector3(0.0, 0.5, -0.5), Vector3(), Vector3(1.0, 1.0, 1.0))
    let a2 = CadModeling.tagged(CadSolids.box(1.0, 1.0, 1.0), "#c").transformed(Vector3(-0.5, 0.5, 0.5), Vector3(), Vector3(1.0, 1.0, 1.0))
    let l = CadModeling.combined(a1, a2, 0)
    let corner_edges = edges_where(l, l_corner)
    assert(corner_edges.length == 3)
    let rounded = CadModeling.filleted(l, CadModeling.edge_references(l, corner_edges), r, "#f")
    let lm = rounded.tessellate(32)
    closed(lm)
    assert(math.abs(volume(lm) - (3.0 + a - 2.0 * (1.0 - r) * a - 0.5 * math.pi * (r + reach) * a)) < 0.0005)

## Whether `make` fails saying `size` must be less than `most`.
func refused_over(message: str, most: str) -> bool:
    return message.contains("too large") and message.contains(f"less than {most}")

## Blends too large for their faces or edges say so, with the size they
## must stay under: a chamfer on an edge of a box one high (the face beside
## it is one wide), a hole's rim in a plate one thick, every edge of that
## box (whole), and its twelve by reference (the uprights between corners).
## A fillet on that edge runs off the narrow face over its convex far edge
## and is trimmed by the next face (overflow_tests): built, closed.
func too_large() -> unit!:
    let box = CadModeling.tagged(CadSolids.box(2.0, 1.0, 3.0), "#b")
    let edge = CadModeling.edge_references(box, [0])
    let over = CadModeling.filleted(box, edge, 1.5, "#f")
    closed(over.tessellate(16))
    assert(volume(over.tessellate(16)) < 6.0)
    var said = ""
    _ = CadModeling.chamfered(box, edge, 1.2, "#c") catch failure:
        said = failure.message
    assert(refused_over(said, "1.0"))
    said = ""
    _ = CadModeling.filleted(box, "", 0.6, "#f") catch failure:
        said = failure.message
    assert(refused_over(said, "0.5"))
    said = ""
    _ = CadModeling.filleted(box, CadModeling.edge_references(box, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), 0.6, "#f") catch failure:
        said = failure.message
    assert(refused_over(said, "0.5"))
    let plate = CadModeling.tagged(CadSolids.box(3.0, 1.0, 3.0), "#p")
    let drilled = CadModeling.combined(plate, CadModeling.tagged(CadSolids.cylinder(0.5, 2.0), "#h"), 1)
    said = ""
    _ = CadModeling.filleted(drilled, CadModeling.edge_references(drilled, round_edges_where(drilled, top_rim)), 1.5, "#f") catch failure:
        said = failure.message
    assert(refused_over(said, "1.0"))

func top_rim(a: Vector3, b: Vector3) -> bool:
    return a.y > 0.49 and b.y > 0.49

## A frustum's rims (radius 2 at the base, 1 at the top, 2 high): filleted
## and chamfered, closed with the blend a fourth face. The base chamfer by
## Pappus: its triangle (0.2 in along the base, 0.2 up the side, whose
## line runs (-1, 2) / sqrt 5) turned about the axis.
func cone_rims() -> unit!:
    let cone = CadModeling.tagged(CadSolids.cone(2.0, 2.0, 1.0), "#k")
    let before = volume(cone.tessellate(32))
    let base = round_edges_where(cone, func(a: Vector3, b: Vector3) -> bool:
        return a.y < 0.0)
    let upper = round_edges_where(cone, func(a: Vector3, b: Vector3) -> bool:
        return a.y > 0.0)
    let d = 0.2
    let up = d / math.sqrt(5.0)
    let area = 0.5 * d * 2.0 * up
    let centroid = (2.0 + (2.0 - d) + (2.0 - up)) / 3.0
    blend(cone, base, d, false, 4, before - 2.0 * math.pi * centroid * area, 0.002)
    blend(cone, base, d, true, 4, -1.0, -1.0)
    blend(cone, upper, d, false, 4, -1.0, -1.0)
    blend(cone, upper, d, true, 4, -1.0, -1.0)

## A D-shaft (radius 1, 2 long, flat at x = 0.6): the flat's edges with the
## cylinder filleted (a cylinder along them, its center 0.2 in from the
## flat and 0.8 from the axis), one alone, and chamfered. The fillet's
## section, between the corner (0.6, 0.8), the contacts and the shaft's own
## circle, is 0.0018792 in area.
func flats() -> unit!:
    let shaft = CadModeling.tagged(CadSolids.cylinder(1.0, 2.0), "#d")
    let cut = CadModeling.tagged(CadSolids.box(2.0, 4.0, 4.0), "#c").transformed(Vector3(1.6, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let d = CadModeling.combined(shaft, cut, 1)
    let before = volume(d.tessellate(32))
    let edges = edges_where(d, func(a: Vector3, b: Vector3) -> bool:
        return math.abs(a.x - 0.6) < 1e-6 and math.abs(a.y - b.y) > 1.0)
    assert(edges.length == 2)
    blend(d, edges, 0.2, true, 6, before - 2.0 * 2.0 * 0.0018792, 0.001)
    blend(d, [edges[0]], 0.2, true, 5, before - 2.0 * 0.0018792, 0.001)
    blend(d, edges, 0.2, false, 6, -1.0, -1.0)

## Three G2 or G3 fillets meeting at a corner: each stops at its setback
## (2 r at a square corner, Fusion's) and six quads fill the hole between
## them (setback.lucb). The corner tool takes r^3 times 1.4290392 (G2) or
## 1.1015857 (G3), from their meshes at 32 and 64 divisions, extrapolated.
pub func setback_corners() -> unit!:
    let g2 = 0.16824175009718
    let g3 = 0.1641116415388808
    let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#b")
    let r = 0.4
    let corner = edges_where(box, at(Vector3(1.0, 1.0, 1.0)))
    # Fusion's Rolling ball corner of three G2 fillets: the strips stop r
    # from the corner, three quads between their end sections (Fusion's 20
    # cube rounded 4: 7845.406289, scaled here).
    smoothed(box, corner, r, 2, 12, 8.0 - (8000.0 - 7845.406289) * r * r * r / 64.0)
    # (G3, ours alone: the fit's own corner, 0.38420 r^3 beyond the strips.)
    smoothed(box, corner, r, 3, 12, 8.0 - 3.0 * r * r * (2.0 - r) * g3 - 0.38420 * r * r * r)
    # The Setback corner type: the strips stop 2 r back, six quads, as full
    # as Fusion's (its 20 cube rounded 4: 7816.965281, a corner of
    # 1.3457 r^3 beyond the strips; ours 1.3436, 0.13 more volume there;
    # G3 ours alone).
    smoothed(box, corner, r, 2, 15, 8.0 - 3.0 * r * r * (2.0 - 2.0 * r) * g2 - 1.3436 * r * r * r, true)
    smoothed(box, corner, r, 3, 15, 8.0 - 3.0 * r * r * (2.0 - 2.0 * r) * g3 - 1.0223 * r * r * r, true)
    # Every edge: twelve fillets between eight setback corners.
    let s = 0.3
    smoothed(box, edges_where(box, any), s, 2, 66, 8.0 - 12.0 * s * s * (2.0 - 4.0 * s) * g2 - 8.0 * 1.3436 * s * s * s, true)
    # A pocket's inside corners: material added, the same regions.
    let block = CadModeling.tagged(CadSolids.box(4.0, 2.0, 4.0), "#k")
    let cut = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#c").transformed(Vector3(0.0, 1.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let pocket = CadModeling.combined(block, cut, 1)
    smoothed(pocket, edges_where(pocket, pocket_floor_and_walls), s, 2, 43, 28.0 + s * s * g2 * (4.0 * (2.0 - 4.0 * s) + 4.0 * (1.0 - 2.0 * s)) + 4.0 * 1.3436 * s * s * s, true)
    # A slanted box's corner (faces at 104 and 76 degrees): the fit's own
    # volume is unknown, but it lies between the ball corner's body and
    # the body with the G2 fillets alone, Rolling ball (three quads) and
    # Setback (six) alike.
    let cutter = CadModeling.tagged(CadSolids.box(4.0, 4.0, 4.0), "#c").transformed(Vector3(2.6, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0)).transformed(Vector3(), Vector3(0.0, 0.25, 0.0), Vector3(1.0, 1.0, 1.0))
    let slanted = CadModeling.combined(box, cutter, 1)
    for corner_at in [Vector3(1.0, 1.0, 1.0), Vector3(1.0, 1.0, -1.0)]:
        var slant_corner = Vector3(-10.0, 0.0, 0.0)
        for face in 0..<slanted.face_count():
            for use in 0..<slanted.face_edge_count(face):
                let p = slanted.face_edge_point(face, use, 0.0)
                if p.y > 0.99 and p.z * corner_at.z > 0.99 and p.x > slant_corner.x:
                    slant_corner = p
        let three = CadModeling.edge_references(slanted, edges_where(slanted, at(slant_corner)))
        for setback in [false, true]:
            let smooth_corner = CadModeling.filleted(slanted, three, 0.2, "#f", 2, setback = setback)
            assert(smooth_corner.face_count() == (15 if setback else 12))
            # Closed: a G2 contact line on a slanted face can leave a display
            # sliver facing either way.
            let mesh = smooth_corner.tessellate(32)
            closed(mesh)
            let v = volume(mesh)
            assert(v < volume(slanted.tessellate(32)) - 0.001 and v > volume(CadModeling.filleted(slanted, three, 0.2, "#f").tessellate(32)) - 0.05)

## A fillet kept between radii (CadModeling.fillet_plan): each radius it
## gives is the fillet made afresh (its corner patches fitted once, at
## radius 1, then scaled), growing and shrinking; it is for its own body,
## references and continuity only.
func fillet_plans() -> unit!:
    let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#b")
    let every = CadModeling.edge_references(box, edges_where(box, any))
    for continuity in [1, 2, 3]:
        let plan = CadModeling.fillet_plan(box, every, continuity, "#f")
        for r in [0.3, 0.1, 0.45, 0.3]:
            let kept = plan.filleted(r)
            let fresh = CadModeling.filleted(box, every, r, "#f", continuity)
            assert(kept.face_count() == fresh.face_count())
            # Scaled rather than fitted at this radius: the same within
            # rounding (2e-8 at most here; a mesh's own error is 1e-4).
            assert(math.abs(volume(kept.tessellate(16)) - volume(fresh.tessellate(16))) < 1e-6)
        assert(plan.made_for(box, every, continuity, "#f", true))
        assert(not plan.made_for(box, every, continuity % 3 + 1, "#f", true))
        assert(not plan.made_for(CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#b"), every, continuity, "#f", true))
    # Too large, said as a fresh fillet says it.
    let plan = CadModeling.fillet_plan(box, every, 2, "#f")
    var refused = false
    _ = plan.filleted(1.5) catch failure:
        refused = failure.message.contains("must be less than")
        recover box
    assert(refused)