Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / touching_tests.luc

## Solids touching (the SMLib study's B1 to B6). Two equal pipes crossing at
## right angles meet in two ellipses that cross where the pipes touch:
## both are cut there, at one vertex each, and the union is closed, of
## volume 2 π r² l - 16 r³ / 3. A box and a pipe touching along a line from
## outside (joined), a pipe inside a box touching its wall (subtracted) and
## two pipes side by side (joined) would leave an edge four faces meet at:
## refused, naming it, never a body that meshes open. A ball touching a box
## and two pipes touching at a point stay manifold and are joined. A pipe
## grazing a box's top and bottom from inside is refused only where it
## pinches the box (taken from it). A box laid against a G2-rounded box's
## B-spline strip at its extreme (found exactly, by point-in-solid along the
## strip's bisector) touches it along a line no curve follows: joined,
## refused; the box taken away leaves the rounded box; nothing in common.
## (It failed with "a face piece has no inside to classify": the box's
## plane crossed the rounded box's top and bottom edges twice a hair apart
## at their extreme, and the sliver between was a piece.)
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

func placed(model: CadModel, at: Vector3, turn: Vector3) -> CadModel!:
    return model.transformed(at, turn, Vector3(1.0, 1.0, 1.0))

func pipe(radius: float, length: float, tag: str) -> CadModel!:
    return CadModeling.tagged(CadSolids.cylinder(radius, length), tag)

func refused(a: CadModel, b: CadModel, op: int, what: str) -> unit!:
    var message = ""
    _ = CadModeling.combined(a, b, op) catch failure:
        message = failure.message
        recover a
    if not message.contains("non-manifold"): print(f"{what}: {message}")
    assert(message.contains("non-manifold"))

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 touching_tests() -> unit!:
    let pi = math.pi
    let box = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "a")
    # Equal pipes crossing.
    let crossed = CadModeling.combined(pipe(5.0, 30.0, "p"), placed(pipe(5.0, 30.0, "q"), Vector3(), Vector3(0.0, 0.0, pi / 2.0)), 0)
    assert(crossed.face_count() == 8)
    for d in [16, 32, 64]: closed(crossed.tessellate(d))
    near(volume(crossed.tessellate(32)), 2.0 * pi * 25.0 * 30.0 - 16.0 * 125.0 / 3.0, 0.15, "crossed pipes")
    # Non-manifold answers, refused.
    refused(box, placed(pipe(5.0, 20.0, "c"), Vector3(15.0, 0.0, 0.0), Vector3()), 0, "box and pipe touching outside")
    refused(box, placed(pipe(5.0, 30.0, "c"), Vector3(5.0, 0.0, 0.0), Vector3()), 1, "pipe inside touching the wall")
    refused(pipe(5.0, 20.0, "p"), placed(pipe(5.0, 20.0, "q"), Vector3(10.0, 0.0, 0.0), Vector3()), 0, "pipes side by side")
    # Touching at a point: joined, closed.
    let ball = CadModeling.combined(box, placed(CadModeling.tagged(CadSolids.sphere(5.0), "s"), Vector3(15.0, 0.0, 0.0), Vector3()), 0)
    closed(ball.tessellate(32))
    near(volume(ball.tessellate(32)), 8000.0 + 500.0 * pi / 3.0, 2.0, "ball touching a box")
    let pipes = CadModeling.combined(pipe(5.0, 30.0, "p"), placed(pipe(5.0, 30.0, "q"), Vector3(0.0, 0.0, 10.0), Vector3(0.0, 0.0, pi / 2.0)), 0)
    closed(pipes.tessellate(32))
    near(volume(pipes.tessellate(32)), 2.0 * pi * 25.0 * 30.0, 2.0, "pipes touching at a point")
    # A pipe crossing a box and touching its top and bottom from inside
    # (tangent along two lines in them): joined, intersected and the box
    # taken from the pipe are closed; the pipe taken from the box leaves the
    # box's top and bottom pinched to nothing along the lines, refused. (It
    # was a body that meshed open: the faces either side of each line were
    # merged, so no edge showed four faces.)
    let block = placed(CadModeling.tagged(CadSolids.box(13.0, 10.0, 10.0), "k"), Vector3(3.5, 0.0, 0.0), Vector3())
    let rod = placed(pipe(5.0, 20.0, "r"), Vector3(), Vector3(pi / 2.0, 0.0, 0.0))
    # The pipe inside the box: its section beyond x = -3 (25 acos 0.6 - 12
    # cut off), 10 long.
    let inside = (25.0 * pi - (25.0 * math.acos(0.6) - 12.0)) * 10.0
    for (op, expected, what) in [(0, 1300.0 + 500.0 * pi - inside, "pipe grazing a box, joined"), (2, inside, "pipe grazing a box, intersected")]:
        let made = CadModeling.combined(block, rod, op)
        closed(made.tessellate(32))
        near(volume(made.tessellate(32)), expected, 1.0, what)
    let hollowed = CadModeling.combined(rod, block, 1)
    closed(hollowed.tessellate(32))
    near(volume(hollowed.tessellate(32)), 500.0 * pi - inside, 1.0, "box taken from a grazing pipe")
    refused(block, rod, 1, "pipe grazing a box's top and bottom, taken from it")
    strip_contact()
    print("PASS touching solids: a box against a G2 strip's extreme, at it and a ten-millionth of the size off either way, refused joined, unchanged taken away, empty in common, and pushed in a thousandth closing every way; equal pipes crossing joined at the points they touch (closed, 2πr²l - 16r³/3); touching along a line refused as non-manifold (outside, inside a wall, side by side, a pipe grazing a box's top and bottom taken from it); touching at a point joined; the grazing pipe joined, intersected and hollowed closed")

## A 20 box with its upright edges rounded G2 (r 5), and a 20 x 30 x 20
## box turned 45 degrees about y with its face across the diagonal at the
## strip's extreme, pushed in by `push` (negative: held off).
func strip_contact() -> unit!:
    let rbox = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "r")
    let uprights: list[int] = []
    for face in 0..<rbox.face_count():
        for use in 0..<rbox.face_edge_count(face):
            let a = rbox.face_edge_point(face, use, 0.0)
            let m = rbox.face_edge_point(face, use, 0.5)
            let id = rbox.face_edge_id(face, use)
            if math.abs(a.x - m.x) < 1e-9 and math.abs(a.z - m.z) < 1e-9 and not (id in uprights): uprights.append(id)
    let rounded = CadModeling.filleted(rbox, CadModeling.edge_references(rbox, uprights), 5.0, "g", 2)
    # The strip's point on the bisector x = z, by point-in-solid.
    var low = 0.0
    var high = 20.0
    for k in 0..<80:
        let middle = (low + high) * 0.5
        if CadModeling.inside(rounded, Vector3(middle, 0.0, middle)): low = middle
        else: high = middle
    let reach = low * math.sqrt(2.0)
    let whole = volume(rounded.tessellate(32))
    for push in [0.0, 3e-6, -3e-6, 1e-3]:
        let center = Vector3(1.0, 0.0, 1.0).multiply_scalar((reach - push + 10.0) / math.sqrt(2.0))
        let tool = placed(CadModeling.tagged(CadSolids.box(20.0, 30.0, 20.0), "t"), center, Vector3(0.0, math.pi / 4.0, 0.0))
        if push < 1e-4:
            refused(rounded, tool, 0, f"a box against a G2 strip pushed {push}, joined")
            let taken = CadModeling.combined(rounded, tool, 1)
            closed(taken.tessellate(32))
            near(volume(taken.tessellate(32)), whole, 1e-6, f"a box against a G2 strip pushed {push}, taken away")
            assert(CadModeling.combined(rounded, tool, 2).face_count() == 0)
            continue
        let joined = CadModeling.combined(rounded, tool, 0)
        let taken = CadModeling.combined(rounded, tool, 1)
        let common = CadModeling.combined(rounded, tool, 2)
        for made in [joined, taken, common]: closed(made.tessellate(32))
        near(volume(joined.tessellate(32)) + volume(common.tessellate(32)), whole + 12000.0, 0.01, "a box pushed into a G2 strip: joined and in common")
        near(volume(taken.tessellate(32)) + volume(common.tessellate(32)), whole, 0.01, "a box pushed into a G2 strip: taken away and in common")