Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / fusion_probe_tests.luc

## Cases run in Fusion 2705 itself (the probe of 2026-10-12) and here:
## - A dome's rim (a ball of 10 cut at its middle) filleted 2 (Fusion
##   succeeds: a torus between the plane's circle of √60 and the sphere's
##   of √93.75 at 2.5): at G1 and G2,
##   Tangency weights 0.5, 1 and 2, the volumes Fusion gave (probe of
##   2026-10-12); G3 closes too.
## - Fusion's Tangency weight on a straight edge filleted 4 (its sections
##   fusion_section_contract checks): a 20 box's edge and prisms whose edge
##   is 120° and 60°, G1 and G2 at weights 0.5, 1 and 2, Fusion's volumes.
## - A D-shaft (a circle of 10 cut flat at 5, 20 long), the flat meeting
##   the cylinder along it filleted 2 at G1 and G2 (G2's section matching
##   the cylinder's curvature, as Fusion's) at weights 0.5, 1 and 2:
##   Fusion's volumes (G1 under a weight the conic, touching the cylinder
##   only tangentially). G3 (not in Fusion) matches the cylinder's
##   curvature and its change: measured G3 at both contacts, on the dome's
##   rim too; weighted 2 its section would cross the cylinder: refused.
## - A variable radius (2 to 6, straight) along a 20 box's edge: G2 at
##   weights 0.5, 1 and 2 and G1 under any other weight (0.5, 0.9, 0.99,
##   0.999, 1.001, 1.01, 1.1, 2) scale the section with the radius, and G1
##   at exactly 1 rolls a ball (Fusion's cone, probe 5): Fusion's volumes,
##   and the cone on edges of 120° and 60°.
## - Seams: Fusion's periodic faces have none (probe 7: a cylinder has two
##   edges, its circles; a sphere and a torus none). Ours stay in the B-rep
##   but are not drawn: a dome's rim strip shows only its contact circles,
##   a cylinder its rims, a sphere and a torus nothing.
## - A fillet's open end where the face going on does not turn away (a
##   stadium plate, one arc's top edge alone, r 3): Fusion fails there
##   (ASM_BL_END_TOO_CMPLX) at G1 and G2; here it ends flat (a deliberate
##   superset), the half turn of the section taken exactly (Pappus) at G1,
##   less at G2.
from luce_cad.modeling import CadModeling
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_cad.sketch import CadSketch
from luce_geocore.geocore import Vector3
from luce_cad.measure import CadMeasure
from checks import closed, volume
from luce_std import math

pub func fusion_probe_tests() -> unit!:
    dome_fillet()
    weighted_edges()
    flat_shaft()
    variable_weights()
    open_end()
    seams_hidden()
    boss_corner()
    print("PASS Fusion probe cases: a boss's upright and its two foot edges in one fillet (Fusion's 1817.27080, 15 faces); a dome's rim filleted 2 (Fusion's torus circles and volumes at G1 and G2, weights 0.5, 1, 2; G3 measured G3); Tangency weights on a box's edge and 120° and 60° prisms' (Fusion's volumes); a D-shaft's flat edge at G1 and G2 under weights (Fusion's volumes), G3 measured G3; variable radius under weights, a ball rolled at G1 weight 1 (Fusion's cones at 90°, 120°, 60°); a stadium arc's top edge filleted alone at G1 and G2, ending flat where Fusion fails; seams not drawn (a dome's rim strip, a cylinder, a sphere, a torus)")

## The volume of `model`'s mesh at 32 and 64 divisions, extrapolated (its
## error falls as the square of the division).
func refined(model: CadModel) -> float!:
    let coarse = volume(model.tessellate(32))
    let mesh = model.tessellate(64)
    closed(mesh)
    let fine = volume(mesh)
    return fine + (fine - coarse) / 3.0

## Fusion's volumes taken away from a dome of 10 by its rim filleted 2,
## G1 then G2 at weights 0.5, 1 and 2 (the dome 2000 π / 3).
func dome_fillet() -> unit!:
    let ball = CadModeling.tagged(CadSolids.sphere(10.0), "ball")
    let below = CadModeling.tagged(CadSolids.box(30.0, 30.0, 30.0), "cut").transformed(Vector3(0.0, -15.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let dome = CadModeling.combined(ball, below, 1)
    let rim_edges = CadModeling.edge_references(dome, edges_where(dome, rim))
    let whole = refined(dome)
    let fusion = [1986.038249, 2020.562509, 2056.411298, 2000.042229, 2032.746816, 2079.880153]
    var at = 0
    for continuity in [1, 2]:
        for weight in [0.5, 1.0, 2.0]:
            let made = CadModeling.filleted(dome, rim_edges, 2.0, "f", continuity, tangency = weight)
            assert(made.face_count() == 3)
            # The contacts: the plane's circle of √60, the sphere's of
            # √93.75 at 2.5 (10/8 of the ball's center at √60, 2).
            assert(has_circle(made, math.sqrt(60.0), 0.0) and has_circle(made, math.sqrt(93.75), 2.5))
            let taken = whole - refined(made)
            let expected = 2000.0 * math.pi / 3.0 - fusion[at]
            if math.abs(taken - expected) > 0.01: print(f"dome G{continuity} weight {weight}: taken {taken}, Fusion {expected}")
            assert(math.abs(taken - expected) <= 0.01)
            at += 1
    let g3 = CadModeling.filleted(dome, rim_edges, 2.0, "f", 3)
    closed(g3.tessellate(32))
    contacts_g3(g3, "dome")

## A prism 20 deep whose top edge at (20, 20) is `angle` degrees between
## its top and its side.
func prism(angle: float) -> CadModel!:
    let t = angle * math.pi / 180.0
    let s = CadSketch.parse("")
    let corners = [Vector3(0.0, 0.0, 0.0), Vector3(20.0, 0.0, 0.0), Vector3(20.0, 20.0, 0.0), Vector3(0.0, 20.0, 0.0)] if angle == 90.0 else [Vector3(-20.0, 0.0, 0.0), Vector3(20.0 - math.cos(t) * 20.0 / math.sin(t), 0.0, 0.0), Vector3(20.0, 20.0, 0.0), Vector3(-20.0, 20.0, 0.0)]
    let ids: list[int] = []
    for p in corners:
        ids.append(s.add_point(p.x, p.y))
    for k in 0..<4:
        _ = s.add_line(ids[k], ids[(k + 1) % 4])
    return CadModeling.extruded_sides(s.curves(Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0)), 20.0, 0.0, "p")

## The reference of `model`'s edge along z through (x, y).
func edge_along(model: CadModel, x: float, y: float) -> str!:
    let along = func (a: Vector3, b: Vector3) -> bool:
        return math.abs(a.x - x) < 1e-6 and math.abs(a.y - y) < 1e-6 and math.abs(b.x - x) < 1e-6 and math.abs(b.y - y) < 1e-6
    let found = edges_where(model, along)
    assert(found.length == 1)
    return CadModeling.edge_references(model, found)

## Fusion's volumes for the edge at (20, 20) filleted 4 at G1 and G2,
## weights 0.5, 1 and 2, the edge 90°, 120° and 60°.
func weighted_edges() -> unit!:
    let fusion = [7900.730747, 7931.327412, 7960.567923, 7917.722074, 7946.16264, 7986.770673,
                  18283.220536, 18292.200599, 18300.075207, 18290.408837, 18297.650774, 18307.482233,
                  13398.10065, 13471.445881, 13552.034859, 13418.569641, 13492.711559, 13608.637652]
    var at = 0
    for angle in [90.0, 120.0, 60.0]:
        let p = prism(angle)
        let edge = edge_along(p, 20.0, 20.0)
        for continuity in [1, 2]:
            for weight in [0.5, 1.0, 2.0]:
                let v = refined(CadModeling.filleted(p, edge, 4.0, "f", continuity, false, tangency = weight))
                if math.abs(v - fusion[at]) > 0.002: print(f"{angle}° G{continuity} weight {weight}: {v}, Fusion {fusion[at]}")
                assert(math.abs(v - fusion[at]) <= 0.002)
                at += 1

## A circle of 10 cut flat at y = 5, 20 long: the flat meets the cylinder
## along (±√75, 5).
func flat_shaft() -> unit!:
    let s = CadSketch.parse("")
    let x = math.sqrt(75.0)
    let a = s.add_point(-x, 5.0)
    let b = s.add_point(x, 5.0)
    _ = s.add_line(a, b)
    _ = s.add_arc(s.add_point(0.0, 0.0), a, b)
    let shaft = CadModeling.extruded_sides(s.curves(Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0)), 20.0, 0.0, "s")
    let edge = edge_along(shaft, x, 5.0)
    let whole = refined(shaft)
    let exact = 2000.0 * (math.pi - (2.0 * math.pi / 3.0 - math.sqrt(0.75)) * 0.5)
    # G1 then G2 at weights 0.5, 1 and 2 (G1 at 1 the circle).
    let fusion = [5045.918856, 5049.190782, 5052.113922, 5048.28947, 5050.892648, 5054.231887]
    var at = 0
    for continuity in [1, 2]:
        for weight in [0.5, 1.0, 2.0]:
            let taken = whole - refined(CadModeling.filleted(shaft, edge, 2.0, "f", continuity, false, tangency = weight))
            let expected = exact - fusion[at]
            if math.abs(taken - expected) > 0.003: print(f"D-shaft G{continuity} weight {weight}: taken {taken}, Fusion {expected}")
            assert(math.abs(taken - expected) <= 0.003)
            at += 1
    # G3 at 0.5 and 1: its curvature and the curvature's change are the
    # cylinder's at the contact; at 2 its section would cross it.
    for weight in [0.5, 1.0]:
        let g3 = CadModeling.filleted(shaft, edge, 2.0, "f", 3, false, tangency = weight)
        closed(g3.tessellate(32))
        contacts_g3(g3, "D-shaft")
    var said = ""
    _ = CadModeling.filleted(shaft, edge, 2.0, "f", 3, false, tangency = 2.0) catch failure:
        said = failure.message
    assert(said.contains("crosses the cylinder"), said)

## Both contact edges of a G3 fillet in `model` measured G3: the faces'
## curvatures and their rates of change across the edge alike.
func contacts_g3(model: CadModel, what: str) -> unit!:
    let check = CadMeasure.check(model, level = 3)
    var smooth = 0
    for index in 0..<check.edge_count():
        let edge = check.edge(index)
        if edge.angle >= 1.0: continue
        smooth += 1
        assert(edge.level == 3, f"{what}: a G3 fillet's contact measured G{edge.level}: {check.summary()}")
        for station in edge.first..<edge.first + edge.count:
            let s = check.station(station)
            assert(math.abs(s.curvature1 - s.curvature2) < 1e-9 and math.abs(s.rate1 - s.rate2) < 1e-9, f"{what}: curvatures {s.curvature1}, {s.curvature2}, rates {s.rate1}, {s.rate2}")
    assert(smooth == 2, f"{what}: {smooth} smooth edges")

## The ids of `model`'s edges whose start and middle `keep` takes.
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):
            let id = model.face_edge_id(face, use)
            if keep(model.face_edge_point(face, use, 0.0), model.face_edge_point(face, use, 0.5)) and not (id in found):
                found.append(id)
    return found

## Whether some edge of `model` is a circle of `radius` about the y axis at
## height `y` (its start, middle and end on it).
func has_circle(model: CadModel, radius: float, y: float) -> bool!:
    for face in 0..<model.face_count():
        for use in 0..<model.face_edge_count(face):
            var on = true
            for t in [0.0, 0.5, 1.0]:
                let p = model.face_edge_point(face, use, t)
                on = on and math.abs(p.y - y) < 1e-5 and math.abs(math.hypot(p.x, p.z) - radius) < 1e-5
            if on: return true
    return false

func rim(a: Vector3, b: Vector3) -> bool:
    return math.abs(a.y) < 1e-9 and math.abs(b.y) < 1e-9 and math.abs(math.hypot(a.x, a.z) - 10.0) < 1e-6

## The edge at (20, 20) of 90°, 120° and 60° prisms filleted 2 to 6
## straight along it: Fusion's volumes (probes 3 and 5). G2 at weights 0.5,
## 1, 2 and G1 under weights near 1 but not 1 (Fusion scales the section
## there: 0.999 and 1.001 lie either side of the cone); G1 at exactly 1 the
## ball rolled, a cone, at each angle.
func variable_weights() -> unit!:
    let cases = [(90.0, 2, 0.5, 7910.865581), (90.0, 2, 1.0, 7941.676193), (90.0, 2, 2.0, 7985.66823), (90.0, 1, 0.5, 7892.458309), (90.0, 1, 2.0, 7957.281917),
                 (90.0, 1, 0.9, 7920.42197), (90.0, 1, 0.99, 7925.111222), (90.0, 1, 0.999, 7925.555585), (90.0, 1, 1.001, 7925.653756), (90.0, 1, 1.01, 7926.092956), (90.0, 1, 1.1, 7930.26468),
                 (90.0, 1, 1.0, 7926.566651), (120.0, 1, 1.0, 18290.858623), (60.0, 1, 1.0, 13460.328324)]
    for (angle, continuity, weight, expected) in cases:
        let p = prism(angle)
        let edge = edge_along(p, 20.0, 20.0)
        let v = refined(CadModeling.variable_filleted(p, edge, [0.0, 1.0], [2.0, 6.0], "v", false, continuity, false, weight))
        if math.abs(v - expected) > 0.002: print(f"variable {angle}° G{continuity} weight {weight}: {v}, expected {expected}")
        assert(math.abs(v - expected) <= 0.002)

## A stadium plate 10 high: lines (0, ±10) to (30, ±10), half circles of
## 10 about (0, 0) and (30, 0); y up.
func stadium() -> CadModel!:
    let s = CadSketch.parse("")
    let a = s.add_point(0.0, -10.0)
    let b = s.add_point(30.0, -10.0)
    let c = s.add_point(30.0, 10.0)
    let d = s.add_point(0.0, 10.0)
    _ = s.add_line(a, b)
    _ = s.add_arc(s.add_point(30.0, 0.0), b, c)
    _ = s.add_line(c, d)
    _ = s.add_arc(s.add_point(0.0, 0.0), d, a)
    return CadModeling.extruded_sides(s.curves(Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 0.0, -1.0)), 10.0, 0.0, "s")

func far_arc_top(a: Vector3, b: Vector3) -> bool:
    return math.abs(a.y - 10.0) < 1e-9 and math.abs(b.y - 10.0) < 1e-9 and b.x > 30.0 + 1e-6

func open_end() -> unit!:
    let plate = stadium()
    let before = volume(plate.tessellate(64))
    let edges = edges_where(plate, far_arc_top)
    assert(edges.length == 1)
    let references = CadModeling.edge_references(plate, edges)
    # G1: the corner's region r²(1 - π/4), its centroid 0.2234 r in from
    # both faces, turned half round at 10 less that.
    let area = 9.0 * (1.0 - math.pi / 4.0)
    let inward = 3.0 * (10.0 - 3.0 * math.pi) / (3.0 * (4.0 - math.pi))
    let half_turn = math.pi * (10.0 - inward) * area
    var g1_taken = 0.0
    for continuity in [1, 2]:
        let made = CadModeling.filleted(plate, references, 3.0, "f", continuity, false)
        let mesh = made.tessellate(64)
        closed(mesh)
        var ends = 0
        for face in 0..<made.face_count():
            if CadModeling.face_name_of(made, face).starts_with("f/end["): ends += 1
        assert(ends == 2, f"G{continuity}: {ends} flat ends")
        let taken = before - volume(mesh)
        print(f"stadium arc G{continuity}: {taken} taken, half turn {half_turn}")
        if continuity == 1:
            assert(math.abs(taken - half_turn) < half_turn * 0.002)
            g1_taken = taken
        else:
            assert(taken > 0.0 and taken < g1_taken)

## Seams are not drawn (overlay and preview boundaries).
func seams_hidden() -> unit!:
    let ball = CadModeling.tagged(CadSolids.sphere(10.0), "ball")
    let below = CadModeling.tagged(CadSolids.box(30.0, 30.0, 30.0), "cut").transformed(Vector3(0.0, -15.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let dome = CadModeling.combined(ball, below, 1)
    let made = CadModeling.filleted(dome, CadModeling.edge_references(dome, edges_where(dome, rim)), 2.0, "f")
    let on_circles = func (p: Vector3) -> bool:
        let radial = math.hypot(p.x, p.z)
        return (math.abs(p.y) < 1e-6 and math.abs(radial - math.sqrt(60.0)) < 1e-6) or (math.abs(p.y - 2.5) < 1e-6 and math.abs(radial - math.sqrt(93.75)) < 1e-6)
    let lines = made.overlay(made.tessellate(16))
    assert(lines.boundary_count() > 0)
    for k in 0..<lines.boundary_count():
        assert(on_circles(lines.boundary(k)), f"a drawn edge off the contacts: {lines.boundary(k)}")
    let preview = made.preview()
    for k in 0..<preview.boundary_count():
        assert(on_circles(preview.boundary(k)), f"a previewed edge off the contacts: {preview.boundary(k)}")
    # A cylinder: its rims only; a sphere and a torus: nothing.
    let cylinder = CadSolids.cylinder(2.0, 4.0)
    let rims = cylinder.overlay(cylinder.tessellate(16))
    for k in 0..<rims.boundary_count():
        assert(math.abs(math.abs(rims.boundary(k).y) - 2.0) < 1e-9, f"a cylinder's seam drawn: {rims.boundary(k)}")
    for solid in [CadSolids.sphere(2.0), CadSolids.torus(3.0, 1.0)]:
        assert(solid.overlay(solid.tessellate(16)).boundary_count() == 0)

## Probe 13: a 20 x 4 x 20 plate with a 6 cube boss on it, the boss's
## upright and its two foot edges filleted 1 in one feature: Fusion's
## 1817.2707995 and 15 faces (the lone convex edge first, its foot run round).
func boss_corner() -> unit!:
    let plate = CadModeling.tagged(CadSolids.box(20.0, 4.0, 20.0), "p")
    let boss = CadModeling.moved_body(CadModeling.tagged(CadSolids.box(6.0, 6.0, 6.0), "b"), Vector3(0.0, 5.0, 0.0))
    let body = CadModeling.combined(plate, boss, 0)
    let found: list[int] = []
    for face in 0..<body.face_count():
        for use in 0..<body.face_edge_count(face):
            let a = body.face_edge_point(face, use, 0.0)
            let b = body.face_edge_point(face, use, 1.0)
            let id = body.face_edge_id(face, use)
            let upright = a.x > 2.99 and b.x > 2.99 and a.z > 2.99 and b.z > 2.99 and a.y > 1.99 and b.y > 1.99
            let foot = math.abs(a.y - 2.0) < 1e-9 and math.abs(b.y - 2.0) < 1e-9 and ((a.x > 2.99 and b.x > 2.99) or (a.z > 2.99 and b.z > 2.99)) and math.abs(a.x) < 3.01 and math.abs(b.x) < 3.01 and math.abs(a.z) < 3.01 and math.abs(b.z) < 3.01
            if (upright or foot) and not (id in found): found.append(id)
    assert(found.length == 3)
    let made = CadModeling.filleted(body, CadModeling.edge_references(body, found), 1.0, "f", 1, false)
    assert(made.face_count() == 15, f"{made.face_count()} faces")
    let v = refined(made)
    assert(math.abs(v - 1817.2707995) < 1e-3, f"boss corner: {v}")