Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / vertex_blend_tests.luc

## Corners where blends of both kinds meet, and Fusion's Setback corners,
## against Fusion 2705 (probes 14 and 15 of 2026-10-13; its solids exported
## as STEP and measured here):
## - three edges, one of a kind: a boss's upright and both its foot edges
##   (the upright lone), an L's inside upright and both its top edges (the
##   upright lone, inside): the pair's section rolled round the lone one's
##   end, Fusion's rolling ball, built directly (y_blend.lucb). G1 is the
##   torus Fusion's two passes leave (1817.27080, 6000.13449, as before);
##   G2 the swept section, Fusion's own (the boss 1816.998776, its solid);
## - Fusion's Setback corner type (setback.lucb): each fillet stops 2 r from
##   the corner (Fusion's), the curves on the faces Fusion's circles of r
##   (3 r beyond a crossing behind the corner), six quads filling the hole,
##   G1 throughout; at a mixed corner a vertex blend, the same patch.
##   Fusion's corner is fuller inside than the fair fill (its patch a
##   transfinite hexagon's fit): with Fusion's fullness (0, the default:
##   1.27 at G1, 1.1 at G2) within 0.4 of its volumes, the cube's corner at
##   G1 and G2, the boss's and the L's vertex blends (the fair fill, a
##   fullness of 1, takes 14 more from the cube at G1);
## - the corner's controls: Edge shape (the face curves' rho) and fullness
##   move the corner monotonically.
from luce_cad.modeling import CadModeling
from luce_cad.model import CadModel
from luce_cad.measure import CadMeasure
from luce_cad.solids import CadSolids
from luce_geocore.geocore import Vector3
from checks import closed, volume
from luce_std import math

pub func vertex_blend_tests() -> unit!:
    rolled_mixed()
    setback_corner()
    vertex_blends()
    corner_controls()
    three_sided()
    print("PASS vertex blends: Fusion's Rolling ball corner of three G2 fillets (three quads between their end sections r back, its volumes at radii 4 and 2 within 0.01, G2 all round); a boss's upright and feet, an L's inside upright and top edges rolled round the lone edge directly (Fusion's G1 tori, its G2 swept sections); Fusion's Setback corners 2 r back with its circles on the faces and its fullness, convex and mixed (vertex blends), within 0.4 of its volumes, G1 throughout; Edge shape and fullness move a corner monotonically")

## The plate with a 6 cube boss (probe 13's).
func boss() -> CadModel!:
    let plate = CadModeling.tagged(CadSolids.box(20.0, 4.0, 20.0), "p")
    let b = CadModeling.moved_body(CadModeling.tagged(CadSolids.box(6.0, 6.0, 6.0), "b"), Vector3(0.0, 5.0, 0.0))
    return CadModeling.combined(plate, b, 0)

## The 20 cube less a 10 x 10 quarter along y.
func ell() -> CadModel!:
    let cube = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "c")
    let notch = CadModeling.moved_body(CadModeling.tagged(CadSolids.box(10.0, 22.0, 10.0), "n"), Vector3(5.0, 0.0, 5.0))
    return CadModeling.combined(cube, notch, 1)

func edges_in(body: CadModel, lo: Vector3, hi: Vector3) -> list[int]!:
    let found: list[int] = []
    for face in 0..<body.face_count():
        for use in 0..<body.face_edge_count(face):
            let id = body.face_edge_id(face, use)
            var inside = true
            for p in [body.face_edge_point(face, use, 0.0), body.face_edge_point(face, use, 1.0)]:
                if p.x < lo.x - 1e-6 or p.y < lo.y - 1e-6 or p.z < lo.z - 1e-6 or p.x > hi.x + 1e-6 or p.y > hi.y + 1e-6 or p.z > hi.z + 1e-6: inside = false
            if inside and not (id in found): found.append(id)
    return found

## The boss's upright and both foot edges round its (3, 2, 3) corner.
func boss_three(body: CadModel) -> str!:
    let three: list[int] = []
    for e in edges_in(body, Vector3(3.0, 2.0, 3.0), Vector3(3.0, 8.0, 3.0)): three.append(e)
    for e in edges_in(body, Vector3(3.0, 2.0, -3.0), Vector3(3.0, 2.0, 3.0)): three.append(e)
    for e in edges_in(body, Vector3(-3.0, 2.0, 3.0), Vector3(3.0, 2.0, 3.0)): three.append(e)
    assert(three.length == 3)
    return CadModeling.edge_references(body, three)

## The L's inside upright and both top edges at its (0, 10, 0) corner.
func ell_three(l: CadModel) -> str!:
    let three: list[int] = []
    for e in edges_in(l, Vector3(0.0, -10.0, 0.0), Vector3(0.0, 10.0, 0.0)): three.append(e)
    for e in edges_in(l, Vector3(0.0, 10.0, 0.0), Vector3(10.0, 10.0, 0.0)): three.append(e)
    for e in edges_in(l, Vector3(0.0, 10.0, 0.0), Vector3(0.0, 10.0, 10.0)): three.append(e)
    assert(three.length == 3)
    return CadModeling.edge_references(l, three)

## The volume of `model`'s mesh at 32 and 64 divisions, extrapolated.
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

## `made` has `faces` faces, its volume within `within` of `expected`, and
## every smooth edge at `level`.
func solid(made: CadModel, faces: int, expected: float, within: float, level: int, label: str) -> unit!:
    assert(made.face_count() == faces, f"{label}: {made.face_count()} faces")
    let v = refined(made)
    assert(math.abs(v - expected) < within, f"{label}: {v} against {expected}")
    let check = CadMeasure.check(made, "", level, 12, 0.001, 0.01, 0.5, 1.0, false, 48, false, 1.0, false)
    for k in 0..<check.edge_count():
        assert(check.edge(k).level >= level, f"{label}: an edge below G{level}: {check.summary()}")

func rolled_mixed() -> unit!:
    let body = boss()
    let refs = boss_three(body)
    solid(CadModeling.filleted(body, refs, 1.0, "f", 1, false), 15, 1817.2707995, 1e-3, 1, "boss G1")
    solid(CadModeling.filleted(body, refs, 1.0, "f", 2, false), 15, 1816.998776, 2e-3, 2, "boss G2")
    let l = ell()
    let lrefs = ell_three(l)
    solid(CadModeling.filleted(l, lrefs, 2.0, "f", 1, false), 12, 6000.1344921, 1e-3, 1, "L G1")
    # Fusion's L at G2 measures 6000.0599 as its solid here and 6000.1321
    # by its own reckoning: its faces' areas are ours within 5e-4.
    solid(CadModeling.filleted(l, lrefs, 2.0, "f", 2, false), 12, 6000.0919, 0.05, 2, "L G2")

## A 20 cube's corner (10, 10, 10), its three edges filleted 4 with the
## Setback corner type: strips 12 long (Fusion's), G1 throughout.
func setback_corner() -> unit!:
    let cube = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "c")
    let corner = edges_in(cube, Vector3(10.0, -10.0, 10.0), Vector3(10.0, 10.0, 10.0))
    for e in edges_in(cube, Vector3(-10.0, 10.0, 10.0), Vector3(10.0, 10.0, 10.0)): corner.append(e)
    for e in edges_in(cube, Vector3(10.0, 10.0, -10.0), Vector3(10.0, 10.0, 10.0)): corner.append(e)
    let refs = CadModeling.edge_references(cube, corner)
    let made = CadModeling.filleted(cube, refs, 4.0, "f", 1, false, true)
    solid(made, 15, 7777.8918, 0.4, 1, "cube setback G1")
    for f in 0..<made.face_count():
        if not made.face_name(f).contains("blend["):
            continue
        # Each strip from -10 to 2 along its edge: its contacts' least
        # coordinate along it is -10, its greatest 2 (8 from the corner).
        var high = -100.0
        for use in 0..<made.face_edge_count(f):
            for k in 0..<9:
                let p = made.face_edge_point(f, use, float(k) / 8.0)
                high = math.max(high, math.min(p.x, math.min(p.y, p.z)))
        assert(math.abs(high - 2.0) < 1e-6, f"a setback strip stops at {high}, not 2")
    solid(CadModeling.filleted(cube, refs, 4.0, "f", 1, false, true, corner_fullness = 1.0), 15, 7763.545, 0.01, 1, "cube setback G1, the fair fill")
    solid(CadModeling.filleted(cube, refs, 4.0, "f", 2, false, true), 15, 7816.8526, 0.4, 2, "cube setback G2")

## Mixed corners under the Setback corner type: vertex blends (Fusion's
## 1817.0823 and 6001.6447 as its solids).
func vertex_blends() -> unit!:
    let body = boss()
    solid(CadModeling.filleted(body, boss_three(body), 1.0, "f", 1, false, true), 20, 1817.0823, 0.1, 1, "boss vertex blend")
    let l = ell()
    solid(CadModeling.filleted(l, ell_three(l), 2.0, "f", 1, false, true), 17, 6001.6447, 0.4, 1, "L vertex blend")

## Edge shape and fullness on the cube's G1 setback corner: sharper face
## curves (a higher rho) and a fuller patch each keep more of the corner.
func corner_controls() -> unit!:
    let cube = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "c")
    let corner = edges_in(cube, Vector3(10.0, -10.0, 10.0), Vector3(10.0, 10.0, 10.0))
    for e in edges_in(cube, Vector3(-10.0, 10.0, 10.0), Vector3(10.0, 10.0, 10.0)): corner.append(e)
    for e in edges_in(cube, Vector3(10.0, 10.0, -10.0), Vector3(10.0, 10.0, 10.0)): corner.append(e)
    let refs = CadModeling.edge_references(cube, corner)
    var last = 0.0
    for shape in [0.35, 0.5, 0.65]:
        let v = refined(CadModeling.filleted(cube, refs, 4.0, "f", 1, false, true, corner_shape = shape))
        assert(v > last, f"edge shape {shape}: {v} after {last}")
        last = v
    last = 0.0
    for fullness in [0.8, 1.0, 1.2]:
        let v = refined(CadModeling.filleted(cube, refs, 4.0, "f", 1, false, true, corner_fullness = fullness))
        assert(v > last, f"fullness {fullness}: {v} after {last}")
        last = v
    var refused = false
    _ = CadModeling.filleted(cube, refs, 4.0, "f", 1, false, true, corner_shape = 1.0) catch failure:
        refused = failure.message.contains("edge shape")
    assert(refused)

## Fusion's default (Rolling ball) corner of three G2 fillets (probes 14
## and 17): each strip stops r from the corner, a three-sided patch between
## their end sections, 12 faces; Fusion's solids 7845.406289 and 7960.487036
## for a 20 cube's corner rounded 4 and 2 (ours three quads, its fullness
## Fusion's). G2 on every edge.
func three_sided() -> unit!:
    let cube = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "c")
    let corner = edges_in(cube, Vector3(10.0, -10.0, 10.0), Vector3(10.0, 10.0, 10.0))
    for e in edges_in(cube, Vector3(-10.0, 10.0, 10.0), Vector3(10.0, 10.0, 10.0)): corner.append(e)
    for e in edges_in(cube, Vector3(10.0, 10.0, -10.0), Vector3(10.0, 10.0, 10.0)): corner.append(e)
    let refs = CadModeling.edge_references(cube, corner)
    solid(CadModeling.filleted(cube, refs, 4.0, "f", 2, false), 12, 7845.406289, 0.01, 2, "cube G2 rolling ball, r 4")
    solid(CadModeling.filleted(cube, refs, 2.0, "f", 2, false), 12, 7960.487036, 0.01, 2, "cube G2 rolling ball, r 2")