Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.1

0.9.1 / src / tests / planar_cut_contract.lucb

## Planar map rejection and trim-ribbon agglomeration are separate contracts.
from luce_std import math
from luce_geocore.geocore import Vector3, MeshBuilder, invalid
from brep_types import Curve, Surface, BrepFace
from layout_types import Stations
from planar_preflight import can_map_plane
from clipped_slivers import coalesce

pub func planar_cut_contract() -> !:
    for scale in [0.01, 1.0, 100.0]:
        for angle in [0.0, 0.37]:
            for swapped in [false, true]:
                for reverse in [false, true]:
                    for concave in [false, true]: try fitted(scale, angle, swapped, reverse, concave)
    for scale in [0.001, 1.0, 1000.0]:
        for reverse in [false, true]:
            for ratio in [0.02, 0.2, 0.5]:
                for boundary in [false, true]: try ribbon(scale, reverse, ratio, boundary)

func fitted(scale: f64, angle: f64, swapped: bool, reverse: bool, concave: bool) -> !:
    let basis = [Vector3(), Vector3(4.0, 0.0, 0.0), Vector3(1.0, 1.0, 0.0) if concave else Vector3(4.0, 4.0, 0.0), Vector3(0.0, 4.0, 0.0)]
    var points = [Vector3(), Vector3(), Vector3(), Vector3()]
    let origin = Vector3(20.0, -10.0, 0.0).multiply_scalar(scale)
    for at in 0..<(usize)4:
        let p = basis[at]
        points[at] = origin.add(Vector3(p.x * math.cos(angle) - p.y * math.sin(angle), p.x * math.sin(angle) + p.y * math.cos(angle), 0.0).multiply_scalar(scale))
    var empty: f64[]
    var controls: Vector3[]
    let edges = [try line(points, 0), try line(points, 1), try line(points, 2), try line(points, 3)]
    defer:
        for edge in edges: edge.close()
    let s = try Surface(0, origin, Vector3(0.0, 1.0, 0.0) if swapped else Vector3(1.0, 0.0, 0.0), Vector3(0.0, 0.0, 1.0), 0.0, 0.0, controls, empty, empty, empty, 0, 0, 0, 0)
    defer s.close()
    let face = try BrepFace(0, [-4, -3, -2, -1] if reverse else [1, 2, 3, 4], [4], not reverse)
    defer face.close()
    var rows = [Stations(), Stations(), Stations(), Stations()]
    defer:
        for row in rows: row.close()
    for at in 0..<(usize)4:
        try rows[at].open(9)
        for step in 0..<9: _ = try rows[at].insert((f64)step / 8.0, 1e-12)
    if (try can_map_plane(points, edges, face, s, rows, scale * 1e-6, 8)) == concave:
        error(invalid, "planar preflight must reject folded maps and preserve valid fitted quads")
    for row in rows:
        if row.count != 9: error(invalid, "map preflight changed an existing boundary phase")
        for at in 0..<row.count:
            if row.values[at] != (f64)at / 8.0: error(invalid, "map preflight relocated a boundary station")

func line(points: const Vector3[], at: usize) -> Curve!:
    var empty: f64[]
    return try Curve(0, (i64)at, (i64)((at + 1) % 4), Vector3(), Vector3(), Vector3(), 0.0, [points[at], points[(at + 1) % 4]], empty, empty, 0, true)

func ribbon(scale: f64, reverse: bool, ratio: f64, boundary: bool) -> !:
    let tolerance = scale * 1e-6
    let width = tolerance * ratio
    var source = try MeshBuilder(precise = true)
    defer source.close()
    let points = [Vector3(), Vector3(scale, 0.0, 0.0), Vector3(0.0, width, 0.0), Vector3(scale, width, 0.0), Vector3(0.0, scale * 0.5, 0.0), Vector3(scale, scale * 0.5, 0.0)]
    for p in points: _ = try source.point(p)
    let ids = [0, 1, 3, 2, 2, 3, 5, 4]
    for face in 0..<(usize)2:
        try source.face(4)
        for at in 0..<(usize)4: try source.corner(ids[face * 4 + (3 - at if reverse else at)])
    let original = try source.finish()
    defer original.release()
    let result = try coalesce(original, 2 if boundary else 0, tolerance)
    defer result.release()
    let mesh = result.value()
    if mesh.face_count() != (1 if boundary and ratio < 0.05 else 2):
        error(invalid, "only sub-tolerance boundary ribbons may coalesce")
    if mesh.point_count() != 6: error(invalid, "ribbon coalescing removed a canonical point")
    for at in 0..<6:
        if (try mesh.point(at)).subtract(points[(usize)at]).length() != 0.0:
            error(invalid, "ribbon coalescing moved a point")
    var found = false
    for edge in 0..<(try mesh.edge_count()):
        let a = try mesh.edge_point(edge, 0)
        let b = try mesh.edge_point(edge, 1)
        found = found or (a == 0 and b == 1) or (a == 1 and b == 0)
    if not found: error(invalid, "ribbon coalescing removed the real trim edge")
    var area: f64 = 0.0
    for triangle in 0..<mesh.index_count() // 3:
        var p = [Vector3(), Vector3(), Vector3()]
        for at in 0..<(usize)3:
            let index = mesh.index(triangle * 3 + (i64)at)
            defer index.release()
            p[at] = try mesh.point(try mesh.corner_point(try index.get()))
        let turn = p[1].subtract(p[0]).cross(p[2].subtract(p[0])).z
        if (turn >= 0.0) == reverse or turn == 0.0:
            error(invalid, "ribbon coalescing inverted a display triangle")
        area += turn * 0.5
    if math.abs(area / (scale * scale) - (-0.5 if reverse else 0.5)) > 1e-10:
        error(invalid, "ribbon coalescing changed trimmed area")