Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / fusion_chamfer_tests.luc

## Chamfers checked against Fusion 2705 itself (the probe of 2026-10-12,
## luce_work/cad/fusion/probe5): a ball of 10 cut flat at its middle, 4
## above it and 4 below it (a dome over z = c; here y = c, y up), its rim
## chamfered Equal distance 3, Two distances 2 and 3 (flipped and not) and
## Distance and angle 3 at 30° (flipped and not). Each distance is a chord
## from the rim on the sphere and runs along the plane; the first distance
## goes on the sphere (the faces tie: by name), as Fusion's; Distance and
## angle leaves its first face at the angle from that face's own tangent
## at its contact (on the sphere, not the rim's tangent). Fusion's circles
## where the chamfer meets the plane and the sphere are ours to 1e-5, its
## volume is the dome less the triangle and the sphere's segment beyond the
## chord turned about the axis (Pappus) to 1e-4, and our mesh's within
## its error.
## A D-shaft (probe 6: a circle of 10 cut flat at y = 5, 20 long), its
## flat's edge with the cylinder chamfered Equal distance 2, Two distances
## 1 and 2 and Distance and angle 2 at 30°, each way: Fusion's contact
## lines (the first distance on the cylinder, a chord) and its volumes, the
## triangle and the circle's segment beyond the chord taken along it.
from luce_cad.modeling import CadModeling
from luce_cad.sketch import CadSketch
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_geocore.geocore import Vector3
from checks import closed, volume
from luce_std import math

pub func fusion_chamfer_tests() -> unit!:
    dome_chamfers()
    shaft_chamfers()
    print("PASS Fusion chamfers: domes cut at 0, 4 and -4 chamfered at their rims, a D-shaft's flat edge, Equal distance, Two distances and Distance and angle each way: Fusion's contacts, and its volumes")

## The ball of 10 above y = `cut`.
func dome_at(cut: float) -> CadModel!:
    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, cut - 15.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    return CadModeling.combined(ball, below, 1)

## Fusion's cases: the cut, the chamfer (0 equal, 1 two distances, 2
## distance and angle), its distance, second distance or angle (degrees),
## flipped; its plane circle's radius, its sphere circle's radius and
## height, its volume.
struct Case:
    var cut: float
    var kind: int
    var distance: float
    var second: float
    var flip: bool
    var plane_radius: float
    var sphere_radius: float
    var sphere_height: float
    var volume: float

func cases() -> list[Case]:
    return [Case(0.0, 0, 3.0, 0.0, false, 7.0, 9.55000000001, 2.96605798996, 1833.02110734),
            Case(0.0, 1, 2.0, 3.0, false, 7.0, 9.8, 1.98997487421, 1922.68200482),
            Case(0.0, 1, 2.0, 3.0, true, 8.0, 9.55000000001, 2.96605798996, 1909.27460203),
            Case(0.0, 2, 3.0, 30.0, false, 8.87905927387, 9.55000000001, 2.96605798996, 1981.43652881),
            Case(0.0, 2, 3.0, 30.0, true, 7.0, 9.86249036979, 1.65265958555, 1952.5456777),
            Case(4.0, 0, 3.0, 0.0, false, 6.16515138991, 7.56629638139, 6.53843705093, 710.222266368),
            Case(4.0, 1, 2.0, 3.0, false, 6.16515138991, 8.18585841243, 5.74384209843, 772.294475889),
            Case(4.0, 1, 2.0, 3.0, true, 7.16515138991, 7.56629638139, 6.53843705093, 765.770547934),
            Case(4.0, 2, 3.0, 30.0, false, 8.05200007545, 7.56629638139, 6.53843705093, 819.481640879),
            Case(4.0, 2, 3.0, 30.0, true, 6.16515138991, 8.46318576169, 5.32677076315, 804.038098854),
            Case(-4.0, 0, 3.0, 0.0, false, 6.16515138991, 9.93914277336, -1.10156294908, 3040.25675141),
            Case(-4.0, 1, 2.0, 3.0, false, 6.16515138991, 9.7778383118, -2.09615790157, 3129.84991195),
            Case(-4.0, 1, 2.0, 3.0, true, 7.16515138991, 9.93914277336, -1.10156294908, 3110.88501448),
            Case(-4.0, 2, 3.0, 30.0, false, 7.80813394339, 9.93914277336, -1.10156294908, 3159.5042066),
            Case(-4.0, 2, 3.0, 30.0, true, 6.16515138991, 9.81976990788, -1.89000501486, 3111.90606178)]

func dome_chamfers() -> unit!:
    for c in cases():
        let dome = dome_at(c.cut)
        let rim_radius = math.sqrt(100.0 - c.cut * c.cut)
        let on_rim = func (a: Vector3, b: Vector3) -> bool:
            return math.abs(a.y - c.cut) < 1e-9 and math.abs(b.y - c.cut) < 1e-9 and math.abs(math.hypot(a.x, a.z) - rim_radius) < 1e-6
        let rim = CadModeling.edge_references(dome, edges_where(dome, on_rim))
        var made = dome
        if c.kind == 0: made = CadModeling.chamfered(dome, rim, c.distance, "c")
        elif c.kind == 1: made = CadModeling.chamfered_sides(dome, rim, c.distance, c.second, 0.0, "c", true, c.flip)
        else: made = CadModeling.chamfered_sides(dome, rim, c.distance, 0.0, c.second * math.pi / 180.0, "c", true, c.flip)
        let what = f"cut {c.cut} kind {c.kind} flip {c.flip}"
        assert(made.face_count() == dome.face_count() + 1, what)
        assert(has_circle(made, c.plane_radius, c.cut), f"{what}: no plane circle of {c.plane_radius}")
        assert(has_circle(made, c.sphere_radius, c.sphere_height), f"{what}: no sphere circle of {c.sphere_radius} at {c.sphere_height}")
        # Fusion's volume: the dome less what turns about the axis, the
        # triangle rim, plane contact, sphere contact and the sphere's
        # segment beyond its chord from the rim.
        let h = 10.0 - c.cut
        let whole = math.pi * h * h * (30.0 - h) / 3.0
        let triangle = 0.5 * math.abs((c.plane_radius - rim_radius) * (c.sphere_height - c.cut))
        let triangle_at = (rim_radius + c.plane_radius + c.sphere_radius) / 3.0
        let chord = math.hypot(c.sphere_radius - rim_radius, c.sphere_height - c.cut)
        let phi = 2.0 * math.asin(chord / 20.0)
        let segment = 50.0 * (phi - math.sin(phi))
        let middle = Vector3((rim_radius + c.sphere_radius) * 0.5, (c.cut + c.sphere_height) * 0.5, 0.0)
        let segment_at = 40.0 * math.pow(math.sin(phi * 0.5), 3.0) / (3.0 * (phi - math.sin(phi))) * middle.x / middle.length()
        let taken = 2.0 * math.pi * (triangle * triangle_at + segment * segment_at)
        if math.abs(whole - taken - c.volume) > 1e-4: print(f"{what}: Pappus {whole - taken}, Fusion {c.volume}")
        assert(math.abs(whole - taken - c.volume) <= 1e-4)
        let mesh = made.tessellate(64)
        closed(mesh)
        let got = volume(dome.tessellate(64)) - volume(mesh)
        if math.abs(got - taken) > taken * 0.002: print(f"{what}: taken {got}, expected {taken}")
        assert(math.abs(got - taken) <= taken * 0.002)

## 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

## 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

## The D-shaft's cases: the chamfer (as Case's), distance, second or
## angle, flipped; Fusion's contacts on the plane (x at y = 5) and on the
## cylinder, its volume.
func shaft_chamfers() -> 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 on_edge = func (p: Vector3, q: Vector3) -> bool:
        return math.abs(p.x - x) < 1e-6 and math.abs(p.y - 5.0) < 1e-6 and math.abs(q.x - x) < 1e-6 and math.abs(q.y - 5.0) < 1e-6
    let edge = CadModeling.edge_references(shaft, edges_where(shaft, on_edge))
    let whole = 2000.0 * (math.pi - (2.0 * math.pi / 3.0 - math.sqrt(0.75)) * 0.5)
    let before = volume(shaft.tessellate(64))
    let fusion = [(0, 2.0, 0.0, false, 6.660254037844392, 9.482036394194093, 3.1766312060386688, 5017.010877789808),
                  (1, 1.0, 2.0, false, 6.660254037844396, 9.116327376541125, 4.110057805399678, 5036.849972844474),
                  (1, 1.0, 2.0, true, 7.660254037844393, 9.482036394194093, 3.1766312060386688, 5035.24456572942),
                  (2, 2.0, 30.0, false, 7.419522346897383, 9.482036394194093, 3.1766312060386688, 5030.855139199517),
                  (2, 2.0, 30.0, true, 6.660254037844394, 9.39702750813219, 3.419923100218324, 5022.328872980917)]
    for (kind, d, second, flip, plane_x, round_x, round_y, expected) in fusion:
        var made = shaft
        if kind == 0: made = CadModeling.chamfered(shaft, edge, d, "c")
        elif kind == 1: made = CadModeling.chamfered_sides(shaft, edge, d, second, 0.0, "c", false, flip)
        else: made = CadModeling.chamfered_sides(shaft, edge, d, 0.0, second * math.pi / 180.0, "c", false, flip)
        let what = f"D-shaft kind {kind} flip {flip}"
        assert(has_line(made, plane_x, 5.0) and has_line(made, round_x, round_y), what)
        # Taken: the triangle edge, plane contact, cylinder contact and
        # the circle's segment beyond the chord to the cylinder's contact.
        let triangle = 0.5 * math.abs((plane_x - x) * (round_y - 5.0))
        let chord = math.hypot(round_x - x, round_y - 5.0)
        let phi = 2.0 * math.asin(chord / 20.0)
        let taken = 20.0 * (triangle + 50.0 * (phi - math.sin(phi)))
        if math.abs(whole - taken - expected) > 1e-4: print(f"{what}: {whole - taken}, Fusion {expected}")
        assert(math.abs(whole - taken - expected) <= 1e-4)
        let mesh = made.tessellate(64)
        closed(mesh)
        let got = before - volume(mesh)
        if math.abs(got - taken) > taken * 0.002: print(f"{what}: taken {got}, expected {taken}")
        assert(math.abs(got - taken) <= taken * 0.002)

## Whether some edge of `model` is a line along z through (x, y).
func has_line(model: CadModel, x: float, y: float) -> bool!:
    for face in 0..<model.face_count():
        for use in 0..<model.face_edge_count(face):
            let p = model.face_edge_point(face, use, 0.0)
            let q = model.face_edge_point(face, use, 1.0)
            if math.abs(p.x - x) < 1e-6 and math.abs(p.y - y) < 1e-6 and math.abs(q.x - x) < 1e-6 and math.abs(q.y - y) < 1e-6: return true
    return false