Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / blend_cap_tests.luc

## Parasolid's caps by faces or sheets (Stage 8): a fillet stopping where it
## meets another body's face (or the body's own), the edge sharp beyond.
## A 20 cube's top front edge rounded 2, capped by the plane x = 0 (a small
## box's face): half the edge's spandrel prism, 10 r² (1 - π/4) taken away,
## the same flipped (the other half); capped by a plane at 45° to the edge,
## the prism cut slanted: the spandrel's area times the length to the plane
## at its centroid (r (5/6 - π/4) / (1 - π/4) below the edge); G2 capped at
## x = 0 half of the whole G2 edge's.
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 closed, volume
from luce_std import math

pub func blend_cap_tests() -> unit!:
    let cube = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "c")
    let edge = top_front(cube)
    let r = 2.0
    let spandrel = r * r * (1.0 - math.pi / 4.0)
    # A box whose +x face lies on x = 0.
    let square = CadModeling.moved_body(CadModeling.tagged(CadSolids.box(4.0, 4.0, 4.0), "k"), Vector3(-2.0, 10.0, 10.0))
    let square_face = face_facing(square, Vector3(1.0, 0.0, 0.0))
    let half = CadModeling.capped_fillet(cube, edge, r, "f", square, square_face)
    near(extrapolated(half), 8000.0 - 10.0 * spandrel, 1e-3, "capped at x = 0")
    assert(names_with(half, "/cap").length > 0, "the cap face is named")
    let other = CadModeling.capped_fillet(cube, edge, r, "f", square, square_face, true)
    near(extrapolated(other), 8000.0 - 10.0 * spandrel, 1e-3, "capped at x = 0, flipped")
    # A plane at 45°, its normal (1, 0, 1) / √2, through (0, 10, 10).
    let turned = CadModeling.moved_body(CadModeling.tagged(CadSolids.box(4.0, 4.0, 4.0), "s"), Vector3(), Vector3(), Vector3(0.0, 1.0, 0.0), math.pi / 4.0)
    let n = Vector3(1.0, 0.0, 1.0).multiply_scalar(1.0 / math.sqrt(2.0))
    var slanted_face = face_facing(turned, n)
    let at = CadModeling.face_point(turned, slanted_face)
    let slant = CadModeling.moved_body(turned, n.multiply_scalar(n.dot(Vector3(0.0, 10.0, 10.0)) - n.dot(at.point)))
    slanted_face = face_facing(slant, n)
    let d = n.dot(Vector3(0.0, 10.0, 10.0))
    let below = r * (5.0 / 6.0 - math.pi / 4.0) / (1.0 - math.pi / 4.0)
    let slanted = CadModeling.capped_fillet(cube, edge, r, "f", slant, slanted_face)
    near(extrapolated(slanted), 8000.0 - spandrel * ((d - n.z * (10.0 - below)) / n.x + 10.0), 2e-3, "capped at 45°")
    # G2: half the whole edge's.
    let whole = CadModeling.filleted(cube, edge, r, "f", 2)
    let g2 = CadModeling.capped_fillet(cube, edge, r, "f", square, square_face, false, 2)
    near(extrapolated(g2), 8000.0 - (8000.0 - extrapolated(whole)) * 0.5, 2e-3, "G2 capped at x = 0")
    print("PASS blend caps: a cube's top edge rounded 2 capped by a face at x = 0 (half the spandrel prism, either side) and by a plane at 45° (the prism cut at its centroid), G2 capped half the whole edge's")

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)

func extrapolated(made: CadModel) -> float!:
    let coarse = volume(made.tessellate(32))
    let mesh = made.tessellate(64)
    closed(mesh)
    return volume(mesh) + (volume(mesh) - coarse) / 3.0

func top_front(model: CadModel) -> str!:
    let found: list[int] = []
    for face in 0..<model.face_count():
        for use in 0..<model.face_edge_count(face):
            let a = model.face_edge_point(face, use, 0.0)
            let b = model.face_edge_point(face, use, 1.0)
            let id = model.face_edge_id(face, use)
            if math.abs(a.y - 10.0) < 1e-9 and math.abs(b.y - 10.0) < 1e-9 and math.abs(a.z - 10.0) < 1e-9 and math.abs(b.z - 10.0) < 1e-9 and not (id in found):
                found.append(id)
    return CadModeling.edge_references(model, found)

func face_facing(model: CadModel, normal: Vector3) -> str!:
    for f in 0..<model.face_count():
        let name = CadModeling.face_name_of(model, f)
        let at = CadModeling.face_point(model, name)
        if at.found and at.normal.dot(normal) > 0.999: return name
    assert(false, "no face facing that way")
    return ""

func names_with(model: CadModel, part: str) -> list[str]!:
    let found: list[str] = []
    for f in 0..<model.face_count():
        let name = CadModeling.face_name_of(model, f)
        if name.contains(part): found.append(name)
    return found