Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / apex_tests.luc

## Fillets meeting at a vertex of four edges (the SMLib study's item 7, its
## F8). A square pyramid (base 20, height 15) with every edge rounded 1 is
## built whole (rounded.lucb): its volume is the Steiner formula's for the
## pyramid shrunk by 1 and grown back by a ball, 1933.19692 (SMLib's, its
## corner a G1 approximation: 1933.1271). Its four edges at the apex alone
## rounded 1 meet in a ball (corners.lucb), which touches all four faces
## there; closed and converging. A rectangular pyramid's apex has no ball
## touching its four faces (it was built broken, its shrunk apex taken as
## one point by least squares, then refused): its fillets now stop at their
## setbacks and an eight-sided patch of eight quads closes the hole
## (setback.lucb, patch_fit.lucb), G1 and G2. Its four apex edges alone and
## every edge: closed at 32 and 64 divisions, every edge between two faces
## G1 to 0.5 degrees (the G2 corner G2 too). All are built directly
## (local_corners.lucb), quickly and with lean meshes (lean_corners).
## Every edge at G2 under Fusion's default corner makes the base's corners
## three-sided (three_sided.lucb): G1 there, the base not being square; the
## Setback corner type keeps G2 on every edge.
from luce_cad.modeling import CadModeling
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_cad.measure import CadMeasure
from luce_geocore.geocore import Vector3
from checks import closed, volume
from luce_std import math
from luce_std import clock

## A pyramid over a 20 by `depth` base at y = 0, its apex at y = 15: a box
## less four blocks beyond its sides.
func pyramid(depth: float) -> CadModel!:
    let half = depth * 0.5
    var made = CadModeling.tagged(CadSolids.box(20.0, 15.0, depth), "py").transformed(Vector3(0.0, 7.5, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
    let alpha = math.atan2(10.0, 15.0)
    let beta = math.atan2(half, 15.0)
    let cuts = [(Vector3(10.0, 0.0, 0.0), Vector3(math.cos(alpha), math.sin(alpha), 0.0), Vector3(0.0, 0.0, alpha)),
                (Vector3(-10.0, 0.0, 0.0), Vector3(-math.cos(alpha), math.sin(alpha), 0.0), Vector3(0.0, 0.0, -alpha)),
                (Vector3(0.0, 0.0, half), Vector3(0.0, math.sin(beta), math.cos(beta)), Vector3(-beta, 0.0, 0.0)),
                (Vector3(0.0, 0.0, -half), Vector3(0.0, math.sin(beta), -math.cos(beta)), Vector3(beta, 0.0, 0.0))]
    var k = 0
    for (at, out, turn) in cuts:
        let block = CadModeling.tagged(CadSolids.box(60.0, 60.0, 60.0), f"c{k}").transformed(at.add(out.multiply_scalar(30.0)), turn, Vector3(1.0, 1.0, 1.0))
        made = CadModeling.combined(made, block, 1)
        k += 1
    return made

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, 1.0)) and not (id in found):
                found.append(id)
    return found

func near(value: float, expected: float, within: float, what: str) -> unit!:
    if math.abs(value - expected) > within: print(f"{what}: {value} against {expected}")
    assert(math.abs(value - expected) <= within)

pub func apex_tests() -> unit!:
    let square = pyramid(20.0)
    near(volume(square.tessellate(16)), 2000.0, 1e-6, "pyramid")
    # Every edge: the Steiner volume.
    let rounded = CadModeling.filleted(square, "", 1.0, "f", 1)
    let coarse = volume(rounded.tessellate(32))
    let mesh = rounded.tessellate(64)
    closed(mesh)
    near(volume(mesh) + (volume(mesh) - coarse) / 3.0, 1933.19692, 0.01, "pyramid rounded whole")
    # The four edges at the apex: a ball there.
    let apex = func (a: Vector3, b: Vector3) -> bool:
        return a.y > 14.0 or b.y > 14.0
    let tipped = CadModeling.filleted(square, CadModeling.edge_references(square, edges_where(square, apex)), 1.0, "t", 1)
    var spheres = 0
    for face in 0..<tipped.face_count():
        if tipped.face_surface_kind(face) == 4: spheres += 1
    assert(spheres == 1)
    var previous = 0.0
    for d in [16, 32, 64]:
        let m = tipped.tessellate(d)
        closed(m)
        if d == 64: near(volume(m), previous, 0.003, "apex rounded, converging")
        previous = volume(m)
    assert(previous < 2000.0 - 7.0 and previous > 2000.0 - 9.0)
    # A rectangular base: no ball touches the apex's four faces.
    let oblong = pyramid(12.0)
    let oblong_apex = CadModeling.edge_references(oblong, edges_where(oblong, apex))
    for continuity in [1, 2]:
        for (what, references) in [("apex", oblong_apex), ("every edge", "")]:
            let made = CadModeling.filleted(oblong, references, 1.0, "o", continuity)
            let at32 = made.tessellate(32)
            let at64 = made.tessellate(64)
            closed(at32)
            closed(at64)
            near(volume(at64), volume(at32), 0.02, f"rectangular pyramid, {what} G{continuity}, converging")
            assert(volume(at64) < 1200.0 - 1.0)
            # Every edge G2: its base corners are three-sided (Fusion's
            # Rolling ball), not square, so G1 there; G2 under Setback.
            let level = 1 if continuity == 2 and references == "" else continuity
            let check = CadMeasure.check(made, "", level, 8, 0.0001, 0.5, 0.5, 1.0, false, 8, false, 1.0, false)
            if check.passed() != check.edge_count(): print(f"rectangular pyramid, {what} G{continuity}: {check.summary()}")
            assert(check.passed() == check.edge_count())
    let setback = CadModeling.filleted(oblong, "", 1.0, "o", 2, setback = true)
    closed(setback.tessellate(32))
    let held = CadMeasure.check(setback, "", 2, 8, 0.0001, 0.5, 0.5, 1.0, false, 8, false, 1.0, false)
    if held.passed() != held.edge_count(): print(f"rectangular pyramid, every edge G2, Setback: {held.summary()}")
    assert(held.passed() == held.edge_count())
    lean_corners(oblong, oblong_apex)
    print("PASS four edges at a vertex: a square pyramid rounded whole (the Steiner volume), its apex's four edges met by one ball (closed, converging), a rectangular pyramid's apex closed by an eight-sided setback patch, G1 and G2 (alone and with every edge), built directly in under 300 ms with no face over 20,000 triangles at 32 divisions")

## The rectangular pyramid's fillets are built directly (local_blend.lucb),
## its corners of four edges too: each in well under 300 ms (2 s by
## booleans; SMLib 30 ms), and their meshes lean: at 32 divisions no face
## over 20,000 triangles (the booleans' strips had 54,000 each), the apex
## G1 under 120,000 in all and every edge G2 under 300,000.
func lean_corners(oblong: CadModel, apex: str) -> unit!:
    for (references, continuity, most) in [(apex, 1, 120000), ("", 1, 200000), (apex, 2, 200000), ("", 2, 300000)]:
        let start = clock.now()
        let made = CadModeling.filleted(oblong, references, 1.0, "o", continuity)
        let took = float(start.elapsed().milliseconds())
        if took > 300.0: print(f"rectangular pyramid G{continuity} ({references}): {took} ms")
        assert(took < 300.0)
        let mesh = made.tessellate(32)
        let parent = mesh.find_attribute("cad_face", 2)
        let counts: list[int] = []
        for face in 0..<made.face_count(): counts.append(0)
        for triangle in 0..<mesh.index_count() // 3:
            counts[int(mesh.attribute_value(parent, mesh.triangle_face(triangle), 0))] += 1
        var largest = 0
        for count in counts:
            if count > largest: largest = count
        if largest > 20000 or mesh.index_count() // 3 > most: print(f"rectangular pyramid G{continuity}: {mesh.index_count() // 3} triangles, a face of {largest}")
        assert(largest <= 20000 and mesh.index_count() // 3 <= most)