Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / chamfer_corner_tests.luc

## Fusion's chamfer Corner Types where three chamfers of 4 meet at a corner
## of a 20 mm cube, against Fusion 2705's own results (the probe of
## 2026-10-12; the cube here is centered, so its corner is (10, 10, 10)
## where Fusion's was (20, 20, 20)):
## - Miter: three planes in a point, 9 faces, 7568.
## - Chamfer: a triangle on the plane through the contact lines' crossings
##   (6, 6, 10), (6, 10, 6), (10, 6, 6); 10 faces; the tip d³/12 more
##   taken, 7562.6667.
## - Blend: the chamfers set back 2 d and one flat hexagon on x + y + z = 28
##   closing the corner, Fusion's corners to its 0.0006; 10 faces, 7456
##   (Fusion 7456.029, its hexagon 0.0006 further in).
## Two distances with Chamfer and Blend corners: closed, the Blend's corner
## one flat face.
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, volume
from luce_std import math

func solid(model: CadModel, what: str) -> float!:
    let mesh = model.tessellate(32)
    manifold(mesh)
    let v = volume(mesh)
    print(f"{what}: {model.face_count()} faces, volume {v}")
    return v

## The corners of the corner's own face (named c/corner[...]).
func corner_face(model: CadModel) -> list[Vector3]!:
    let points: list[Vector3] = []
    for face in 0..<model.face_count():
        if CadModeling.face_name_of(model, face).starts_with("c/corner"):
            for use in 0..<model.face_edge_count(face):
                points.append(model.face_edge_point(face, use, 0.0))
    return points

## Whether `points` are `expected`, in any order.
func same_points(points: list[Vector3], expected: list[Vector3]) -> bool:
    if points.length != expected.length: return false
    for e in expected:
        var found = false
        for p in points:
            found = found or p.subtract(e).length() < 1e-9
        if not found:
            print(f"corner ({e.x}, {e.y}, {e.z}) missing")
            return false
    return true

pub func chamfer_corner_tests() -> unit!:
    let box = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "box")
    # The three edges at the corner (10, 10, 10).
    let edges: list[int] = []
    for face in 0..<box.face_count():
        for use in 0..<box.face_edge_count(face):
            let id = box.face_edge_id(face, use)
            let a = box.face_edge_point(face, use, 0.0)
            let b = box.face_edge_point(face, use, 1.0)
            let at_corner = a.subtract(Vector3(10.0, 10.0, 10.0)).length() < 1e-9 or b.subtract(Vector3(10.0, 10.0, 10.0)).length() < 1e-9
            if at_corner and not (id in edges):
                edges.append(id)
    assert(edges.length == 3)
    let references = CadModeling.edge_references(box, edges)
    let miter = CadModeling.chamfered(box, references, 4.0, "c", corner = 1)
    assert(math.abs(solid(miter, "miter") - 7568.0) < 1e-6 and miter.face_count() == 9)
    let chamfer = CadModeling.chamfered(box, references, 4.0, "c", corner = 0)
    assert(math.abs(solid(chamfer, "chamfer") - (7568.0 - 64.0 / 12.0)) < 1e-6 and chamfer.face_count() == 10)
    assert(math.abs(7568.0 - 64.0 / 12.0 - 7562.6667) < 1e-4)
    assert(same_points(corner_face(chamfer), [Vector3(6.0, 6.0, 10.0), Vector3(6.0, 10.0, 6.0), Vector3(10.0, 6.0, 6.0)]))
    # Blend: Fusion's hexagon (12.0006, 16, 20) ... less 10, at 12 here.
    let blend = CadModeling.chamfered(box, references, 4.0, "c", corner = 2)
    let v_blend = solid(blend, "blend")
    assert(math.abs(v_blend - 7456.0) < 1e-6 and math.abs(v_blend - 7456.029) < 0.03 and blend.face_count() == 10)
    let hexagon = [Vector3(2.0, 6.0, 10.0), Vector3(6.0, 2.0, 10.0), Vector3(10.0, 2.0, 6.0), Vector3(10.0, 6.0, 2.0), Vector3(6.0, 10.0, 2.0), Vector3(2.0, 10.0, 6.0)]
    assert(same_points(corner_face(blend), hexagon))
    for at in 0..<blend.face_count():
        assert(CadModeling.face_frame(blend, CadModeling.face_name_of(blend, at)).found, "every face of an all-flat Blend corner is flat")
    # Two distances: Chamfer and Blend corners closed, the Blend's flat.
    let sided = CadModeling.chamfered_sides(box, references, 2.0, 1.0, 0.0, "c", corner = 0)
    _ = solid(sided, "two distances, chamfer")
    let sided_blend = CadModeling.chamfered_sides(box, references, 2.0, 1.0, 0.0, "c", corner = 2)
    _ = solid(sided_blend, "two distances, blend")
    assert(sided_blend.face_count() == 10 and corner_face(sided_blend).length == 6)
    # Chamfers too large for the corner's plane to stay on the edges: said.
    var said = ""
    _ = CadModeling.chamfered(box, references, 7.0, "c", corner = 2) catch failure:
        said = failure.message
    assert(said.contains("far end"), said)
    print("PASS chamfer corners, as Fusion: three chamfers of 4 at a cube's corner as a Miter (7568, 9 faces), a Chamfer (Fusion's triangle, 7562.667, 10 faces) and a Blend (set back 2 d, Fusion's flat hexagon, 7456, 10 flat faces); two distances with Chamfer and Blend corners; a Blend whose plane passes an edge's end refused")