Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / profile_tests.luc

## Profiles from crossing curves (modeling/arrangement.lucb), as Fusion's:
## two overlapping circles make three faces, joined by default into one
## solid, or the lens alone when chosen; a line between a rectangle's
## sides splits it in two (either side chosen by a click in it); a line
## loose inside it splits nothing; a line right across splits it too, its
## ends outside dropped. Each checked by its extruded solid's faces and
## volume.
from luce_cad.modeling import CadModeling, CadProfileMap
from luce_cad.sketch import CadSketch
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_cad.cad_edits import CadEdits
from luce_cad.cad_geometry import CadGeometry
from luce_geocore.geocore import Vector3, GeometrySet, Sketches
from checks import manifold, volume
from luce_std import math

## `model` closed, of `faces` faces and `expected` volume (within 0.3%).
func solid(model: CadModel, faces: int, expected: float) -> unit!:
    if model.face_count() != faces: print(f"faces {model.face_count()}, expected {faces}")
    assert(model.face_count() == faces)
    let mesh = model.tessellate(32)
    manifold(mesh)
    let v = volume(mesh)
    if math.abs(v - expected) > expected * 0.003: print(f"volume {v}, expected {expected}")
    assert(math.abs(v - expected) <= expected * 0.003)

## A sketch's curves on the XY plane.
func curves(s: CadSketch) -> GeometrySet!:
    return s.curves(Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0))

## A 40 x 20 rectangle from the origin, with a line from `a` to `b`.
func crossed(a: list[float], b: list[float]) -> GeometrySet!:
    let s = CadSketch.parse("")
    let corners = [s.add_point(0.0, 0.0), s.add_point(40.0, 0.0), s.add_point(40.0, 20.0), s.add_point(0.0, 20.0)]
    for at in 0..<4:
        _ = s.add_line(corners[at], corners[(at + 1) % 4])
    _ = s.add_line(s.add_point(a[0], a[1]), s.add_point(b[0], b[1]))
    return curves(s)

pub func profile_tests() -> unit!:
    spline_profiles()
    fills_and_anchors()
    let h = 5.0
    # Two circles of 10 overlapping by 8: a lens and two crescents.
    let s = CadSketch.parse("")
    _ = s.add_circle(s.add_point(0.0, 0.0), 10.0)
    _ = s.add_circle(s.add_point(12.0, 0.0), 10.0)
    let pair = curves(s)
    assert(CadModeling.profile_keys(pair).split(";").length == 3)
    let lens = 2.0 * 100.0 * math.acos(0.6) - 6.0 * math.sqrt(400.0 - 144.0)
    # By default all three, joined: one solid, the union.
    solid(CadModeling.extruded(pair, h, 0.0, "e"), 4, (2.0 * math.pi * 100.0 - lens) * h)
    # The lens alone, picked where it is.
    let picked = CadModeling.profile_at(pair, Vector3(6.0, 0.0, 0.0))
    assert(picked != "" and CadModeling.profile_at(pair, Vector3(40.0, 0.0, 0.0)) == "")
    solid(CadModeling.extruded(pair, h, 0.0, "e", picked), 4, lens * h)
    # A crescent: its key differs from the lens's (a circle run the other way).
    let crescent = CadModeling.profile_at(pair, Vector3(-5.0, 0.0, 0.0))
    assert(crescent != picked)
    # Laid out once (CadProfileMap): the same keys under the same points.
    let laid = CadProfileMap.of(pair)
    assert(laid.key_at(Vector3(6.0, 0.0, 0.0)) == picked and laid.key_at(Vector3(-5.0, 0.0, 0.0)) == crescent and laid.key_at(Vector3(40.0, 0.0, 0.0)) == "")
    # Its outline to draw: one closed curve.
    assert(Sketches.curve_count(CadModeling.profile_outlines(pair, crescent)) == 1)
    # A line from the bottom to the top at x 15: two faces; by default the
    # rectangle whole, chosen the left part.
    let split = crossed([15.0, 0.0], [15.0, 20.0])
    assert(CadModeling.profile_keys(split).split(";").length == 2)
    solid(CadModeling.extruded(split, h, 0.0, "e"), 6, 40.0 * 20.0 * h)
    solid(CadModeling.extruded(split, h, 0.0, "e", CadModeling.profile_at(split, Vector3(5.0, 10.0, 0.0))), 6, 15.0 * 20.0 * h)
    # A line loose inside splits nothing.
    let loose = crossed([10.0, 10.0], [20.0, 10.0])
    assert(CadModeling.profile_keys(loose).split(";").length == 1)
    solid(CadModeling.extruded(loose, h, 0.0, "e"), 6, 40.0 * 20.0 * h)
    # A line right across, its ends outside: two faces, the ends dropped.
    let across = crossed([-5.0, 8.0], [45.0, 8.0])
    assert(CadModeling.profile_keys(across).split(";").length == 2)
    solid(CadModeling.extruded(across, h, 0.0, "e", CadModeling.profile_at(across, Vector3(20.0, 4.0, 0.0))), 6, 40.0 * 8.0 * h)
    # Every face of a 20 mm box gives a sketch plane on it, its normal out
    # of the box.
    let box = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "#b")
    for face in 0..<box.face_count():
        let frame = CadModeling.face_frame(box, CadModeling.face_name_of(box, face))
        assert(frame.found)
        let normal = frame.u.cross(frame.v)
        assert(math.abs(frame.origin.dot(normal) - 10.0) < 1e-9 and math.abs(normal.length() - 1.0) < 1e-9)
    assert(not CadModeling.face_frame(box, "#nothing/f0").found)
    # A named body's pick mesh names each polygon's face.
    let pick = CadEdits.pick_mesh(CadGeometry.of_model(box))
    let names = pick.find_attribute("cad_face_name", 2)
    assert(names >= 0 and CadModeling.face_frame(box, pick.attribute_text(names, 0)).found)
    print("PASS profiles: sketch planes on a box's faces, crossing circles (joined, the lens, a crescent's own key), a rectangle split by a line between its sides, a loose line, a line right across")

## A parabola's arch (a quadratic B-spline on (0, 0), (1, 2), (2, 0),
## closed by its chord: area 4/3) extruded and booleaned with a box (B-spline
## faces met by planes: their union and intersection hold both volumes),
## and, raised off the axis, turned about it.
func spline_profiles() -> unit!:
    let s = CadSketch.parse("")
    let a = s.add_point(0.0, 0.0)
    let b = s.add_point(2.0, 0.0)
    _ = s.add_spline([a, s.add_point(1.0, 2.0), b], false, 2)
    _ = s.add_line(b, a)
    let arch = curves(s)
    let prism = CadModeling.extruded(arch, 1.0, 0.0, "e")
    solid(prism, 4, 4.0 / 3.0)
    # Its floor crosses the arch near the top (lines along the sweep).
    let box = CadSolids.box(0.8, 1.0, 2.0).transformed(Vector3(1.0, 1.4, 0.5), Vector3(), Vector3(1.0, 1.0, 1.0))
    let both = CadModeling.combined(prism, box, 0)
    let shared = CadModeling.combined(prism, box, 2)
    let together = volume(both.tessellate(32)) + volume(shared.tessellate(32))
    manifold(both.tessellate(32))
    manifold(shared.tessellate(32))
    if math.abs(together - (4.0 / 3.0 + 1.6)) > 0.01: print(f"union + intersection {together}, parts {4.0 / 3.0 + 1.6}")
    assert(math.abs(together - (4.0 / 3.0 + 1.6)) <= 0.01)
    # Raised half a unit off the axis: the arch turned (its chord a cylinder)
    # holds 2 pi 0.9 (4 / 3), its centroid 2/5 of the way up (Pappus).
    let r = CadSketch.parse("")
    let c = r.add_point(0.0, 0.5)
    let d = r.add_point(2.0, 0.5)
    _ = r.add_spline([c, r.add_point(1.0, 2.5), d], false, 2)
    _ = r.add_line(d, c)
    solid(CadModeling.revolved(curves(r), 0, "r"), 4, 2.0 * math.pi * 0.9 * 4.0 / 3.0)

## A 40 x 20 plate with a hole of radius 5 at (10, 10): two profiles, the
## ring and the disc. Their fills cover their areas; the plate's anchor
## stands on the ring (not in the hole), the default islands' too; inside
## tells a point in a box from one outside it.
func fills_and_anchors() -> unit!:
    let s = CadSketch.parse("")
    let corners = [s.add_point(0.0, 0.0), s.add_point(40.0, 0.0), s.add_point(40.0, 20.0), s.add_point(0.0, 20.0)]
    for at in 0..<4:
        _ = s.add_line(corners[at], corners[(at + 1) % 4])
    _ = s.add_circle(s.add_point(10.0, 10.0), 5.0)
    let plate = curves(s)
    let keys = CadModeling.profile_keys(plate).split(";")
    assert(keys.length == 2)
    let disc = CadModeling.profile_at(plate, Vector3(10.0, 10.0, 0.0))
    let ring = CadModeling.profile_at(plate, Vector3(30.0, 10.0, 0.0))
    assert(disc != "" and ring != "" and disc != ring)
    let hole = math.pi * 25.0
    for (key, expected) in [(ring, 800.0 - hole), (disc, hole), ("", 800.0)]:
        let a = CadModeling.profile_anchor(plate, key)
        let fill = CadModeling.profile_fills(plate, key)
        assert(fill.face_count() > 0)
        var area = 0.0
        for face in 0..<fill.face_count():
            let p = fill.point(fill.face_point(face, 0))
            let q = fill.point(fill.face_point(face, 1))
            let r = fill.point(fill.face_point(face, 2))
            area += q.subtract(p).cross(r.subtract(p)).length() * 0.5
            assert(math.abs(p.z) < 1e-9)
        # The outline follows arcs every 10 degrees: within 1%.
        assert(math.abs(area - expected) < expected * 0.01, f"fill {area}, expected {expected}")
        if key != "":
            assert(a.found and math.abs(a.area - expected) < expected * 0.01 and a.normal.z > 0.99)
            assert(CadModeling.profile_at(plate, a.point) == key, f"anchor {a.point.x}, {a.point.y}")
    let islands = CadModeling.profile_anchor(plate, "")
    assert(islands.found and CadModeling.profile_at(plate, islands.point) == ring)
    # A sketch without a closed profile: no fill, no anchor.
    let open = CadSketch.parse("")
    _ = open.add_line(open.add_point(0.0, 0.0), open.add_point(10.0, 0.0))
    assert(CadModeling.profile_fills(curves(open)).face_count() == 0 and not CadModeling.profile_anchor(curves(open)).found)
    let box = CadSolids.box(2.0, 2.0, 2.0)
    assert(CadModeling.inside(box, Vector3(0.3, 0.2, -0.4)) and not CadModeling.inside(box, Vector3(1.5, 0.0, 0.0)))