Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / cap_loop_tests.luc

## Blends round a flat face's tangent loop (cap_blend.lucb): a box whose
## four uprights were filleted (G1, G2, G3), its top loop (lines and the
## uprights' sections, tangent where they meet) then filleted at G1, G2
## and G3 and chamfered, from one picked edge (a line or a curve: the
## tangent chain brings the rest), the bottom loop and both at once; a
## line alone (Tangent chain off) running out round the uprights as
## Fusion's does, a curve alone ending in flat faces (the sides it goes on
## into do not turn away).
## Each result is closed, its display triangles facing out, of the volume
## the swept section takes: along a closed convex loop of length P the
## section (area A, centroid x̄ in from both faces) sweeps A (P - 2π x̄),
## the loop's curvature integrating to one turn whatever its shape.
from luce_cad.modeling import CadModeling
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_geocore.geocore import Vector3, Mesh
from checks import closed, volume
from luce_std import math

## The ids of `model`'s edges whose start and middle `keep` takes.
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, 0.5)) and not (id in found):
                found.append(id)
    return found

## How long `model`'s edges `edges` are together (fine polylines).
func length_of(model: CadModel, edges: list[int]) -> float!:
    var total = 0.0
    let done: 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 not (id in edges) or id in done:
                continue
            done.append(id)
            var previous = model.face_edge_point(face, use, 0.0)
            for k in 1..<2001:
                let p = model.face_edge_point(face, use, float(k) / 2000.0)
                total += p.subtract(previous).length()
                previous = p
    return total

func top(a: Vector3, b: Vector3) -> bool:
    return a.y > 0.99 and b.y > 0.99

func bottom(a: Vector3, b: Vector3) -> bool:
    return a.y < -0.99 and b.y < -0.99

func upright(a: Vector3, b: Vector3) -> bool:
    return math.abs(a.x - b.x) < 1e-9 and math.abs(a.z - b.z) < 1e-9

## The section's area and centroid (in from each face) of a blend of `r`:
## `continuity` 0 a chamfer.
func section(continuity: int, r: float) -> (float, float):
    if continuity == 0:
        return (r * r * 0.5, r / 3.0)
    if continuity == 1:
        return (r * r * (1.0 - math.pi * 0.25), r * (10.0 - 3.0 * math.pi) / (12.0 - 3.0 * math.pi))
    if continuity == 2:
        return (0.16824175009718 * r * r, 0.19836347 * r)
    return (0.1641116415388808 * r * r, 0.19591901 * r)

## Every display triangle faces the way its corner normals do, but for
## slivers of no area (a G3 contact is so flat that the untouched faces
## beside it may mesh one: fillet_tests' note).
func outward(mesh: Mesh) -> unit!:
    for triangle in 0..<mesh.index_count() // 3:
        let a = mesh.vertex(mesh.index(triangle * 3))
        let b = mesh.vertex(mesh.index(triangle * 3 + 1))
        let c = mesh.vertex(mesh.index(triangle * 3 + 2))
        let n = b.position.subtract(a.position).cross(c.position.subtract(a.position))
        if n.dot(a.normal) <= 0.0 and n.length() > 1e-6: print(f"triangle {triangle} faces in: {a.position.x},{a.position.y},{a.position.z}")
        assert(n.dot(a.normal) > 0.0 or n.length() <= 1e-6)

## `model`'s edges `picked` blended to `r` (`continuity` 0: chamfered):
## `faces` faces, closed and facing out (outward), `removed` less than before.
func checked(model: CadModel, picked: list[int], r: float, continuity: int, faces: int, removed: float) -> unit!:
    let refs = CadModeling.edge_references(model, picked)
    let made = CadModeling.chamfered(model, refs, r, "#g") if continuity == 0 else CadModeling.filleted(model, refs, r, "#g", continuity)
    if made.face_count() != faces: print(f"faces {made.face_count()}, expected {faces}")
    assert(made.face_count() == faces)
    let mesh = made.tessellate(64)
    closed(mesh)
    outward(mesh)
    let expected = volume(model.tessellate(64)) - removed
    let v = volume(mesh)
    if math.abs(v - expected) > 0.0003: print(f"volume {v}, expected {expected} (G{continuity})")
    assert(math.abs(v - expected) <= 0.0003)

## What one run-out of a chamfer (`continuity` 0) or G1 fillet of `r`
## takes past a line's end into a G1 upright of `radius`: the section
## carried on, the upright's cylinder receding δ(s) = R - √(R² - s²) from
## the line's wall s past the end, until δ reaches r. Each slice takes the
## corner's part farther than δ from the wall.
func run_out(continuity: int, radius: float, r: float) -> float:
    let most = math.sqrt(radius * radius - (radius - r) * (radius - r))
    var total = 0.0
    let n = 400
    for k in 0..<n:
        let s = most * (float(k) + 0.5) / float(n)
        let d = radius - math.sqrt(radius * radius - s * s)
        var slice = (r - d) * (r - d) * 0.5
        if continuity == 1:
            slice = 0.0
            for j in 0..<n:
                let x = d + (r - d) * (float(j) + 0.5) / float(n)
                slice += (r - math.sqrt(r * r - (r - x) * (r - x))) * (r - d) / float(n)
        total += slice * most / float(n)
    return total

pub func cap_loop_tests() -> unit!:
    let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#b")
    let r = 0.2
    for first in 1..<4:
        let rounded = CadModeling.filleted(box, CadModeling.edge_references(box, edges_where(box, upright)), 0.4, "#f", first)
        let loop = edges_where(rounded, top)
        let floor = edges_where(rounded, bottom)
        assert(loop.length == 8 and floor.length == 8)
        let perimeter = length_of(rounded, loop)
        # A line of the loop and a curve of it each bring the whole loop.
        var line = -1
        var curve = -1
        for face in 0..<rounded.face_count():
            for use in 0..<rounded.face_edge_count(face):
                let id = rounded.face_edge_id(face, use)
                if not (id in loop):
                    continue
                if rounded.face_edge_linear(face, use):
                    line = id
                else:
                    curve = id
        assert(line >= 0 and curve >= 0)
        for continuity in 0..<4:
            let (area, centroid) = section(continuity, r)
            let swept = area * (perimeter - 2.0 * math.pi * centroid)
            checked(rounded, [line], r, continuity, 18, swept)
            if continuity == first:
                checked(rounded, [curve], r, continuity, 18, swept)
                checked(rounded, [floor[0]], r, continuity, 18, swept)
                checked(rounded, [line, floor[0]], r, continuity, 26, 2.0 * swept)
        # Too large for the curves (their least radius of curvature), and a
        # line alone without its chain: said, not built.
        var message = ""
        _ = CadModeling.filleted(rounded, CadModeling.edge_references(rounded, [line]), 0.45, "#g", 2) catch failure:
            message = failure.message
        assert(message.starts_with("a fillet radius of 0.45 is too large for this curved edge: it must be less than "))
        # Without Tangent chain, a line alone: its blend runs out at each
        # end, carried on past it and trimmed by the upright it runs into
        # (a chamfer's and G1's strip running on in its plane or cylinder,
        # G2's and G3's with a piece of their own). It takes the section
        # along the line's 2 - 2 x 0.4, and more past each end.
        for continuity in 0..<4:
            let (area, _) = section(continuity, r)
            let refs = CadModeling.edge_references(rounded, [line])
            let alone = CadModeling.chamfered(rounded, refs, r, "#g", false) if continuity == 0 else CadModeling.filleted(rounded, refs, r, "#g", continuity, false)
            assert(alone.face_count() == (11 if continuity < 2 else 13))
            let mesh = alone.tessellate(64)
            closed(mesh)
            outward(mesh)
            let removed = volume(rounded.tessellate(64)) - volume(mesh)
            assert(removed > area * 1.2 + 0.001)
            # Round G1 uprights, a chamfer's and G1's run-outs by their
            # slices.
            if first == 1 and continuity < 2:
                let expected = area * 1.2 + 2.0 * run_out(continuity, 0.4, r)
                if math.abs(removed - expected) > 0.0001: print(f"alone removed {removed}, expected {expected} (G{continuity})")
                assert(math.abs(removed - expected) <= 0.0001)
        # A curve alone (an upright's section on the top): open along its
        # B-spline wall, a flat face at each end.
        let curve_alone = CadModeling.filleted(rounded, CadModeling.edge_references(rounded, [curve]), r, "#g", first, false)
        assert(curve_alone.face_count() == 13)
        let curve_mesh = curve_alone.tessellate(64)
        closed(curve_mesh)
        outward(curve_mesh)
        # Kept between radii (FilletPlan): the same body for each radius.
        let plan = CadModeling.fillet_plan(rounded, CadModeling.edge_references(rounded, [curve]), 3, "#g")
        for size in [0.1, 0.2, 0.3]:
            assert(plan.filleted(size).face_count() == 18)
    print("PASS cap loops: a box's top and bottom loops after its uprights were filleted (G1, G2, G3) filleted G1, G2, G3 and chamfered, from a line or a curve, one loop or both; closed, outward, of the swept volume; too large said; a line alone running out round the uprights (its volume past the ends), a curve alone ending in flat faces")