Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.2

0.9.2 / tests / regressions / spacing_tests.luc

## Extra rows belong on long physical spans, not short split STEP coedges.
from luce_cad.model import BrepModel
from luce_geocore.geocore import Vector3
from checks import near
from luce_std import math

pub func spacing_tests() -> unit!:
    let b = BrepModel()
    let points = [Vector3(),Vector3(0.01,0.0,0.0),Vector3(0.99,0.0,0.0),Vector3(1.0,0.0,0.0),Vector3(1.0,1.0,0.0),Vector3(0.0,1.0,0.0)]
    for p in points: _ = b.add_vertex(p)
    for at in 0..<4: _ = b.add_line(at,at+1)
    let quadratic = [0.0,0.0,0.0,1.0,1.0,1.0]
    _ = b.add_spline_edge(4,5,[points[4],Vector3(0.5,1.0,1.0),points[5]],[1.0,1.0,1.0],2,quadratic,true)
    _ = b.add_line(5,0)
    let controls = [points[0],points[5],Vector3(0.5,0.0,0.0),Vector3(0.5,1.0,1.0),points[3],points[4]]
    _ = b.add_spline_surface(controls,[1.0,1.0,1.0,1.0,1.0,1.0],3,2,2,1,quadratic,[0.0,0.0,1.0,1.0])
    _ = b.add_face(0,[1,2,3,4,5,6],[6])
    let mesh = b.mesh(8)
    assert(mesh.face_count() >= 16)
    for face in 0..<mesh.face_count(): assert(mesh.face_size(face) == 4)
    var tiny_interior = 0
    var middle = 0
    for at in 0..<mesh.point_count():
        let p = mesh.point(at)
        near(p.z,2.0*p.x*(1.0-p.x)*p.y)
        if math.abs(p.y) > 0.000001: continue
        if (p.x > 0.000001 and p.x < 0.009999) or (p.x > 0.990001 and p.x < 0.999999): tiny_interior += 1
        if p.x > 0.01 and p.x < 0.99: middle += 1
    assert(tiny_interior == 0 and middle >= 12)
    var longest = 0.0
    var shortest = 1.0
    for edge in 0..<mesh.edge_count():
        let a = mesh.point(mesh.edge_point(edge,0))
        let c = mesh.point(mesh.edge_point(edge,1))
        if math.abs(a.y)+math.abs(c.y) > 0.000001: continue
        let length = c.subtract(a).length()
        longest = math.max(longest,length)
        shortest = math.min(shortest,length)
    assert(longest/shortest < 8.0)
    parameter_spacing()
    tangent_trim()
    affine_strip()
    notched_extrusion()
    print("PASS physical-span allocation on unequal compound sides, no tiny-edge row bunching and exact support")

func parameter_spacing() -> unit!:
    let b = BrepModel()
    let p = [Vector3(),Vector3(1.0,0.0,0.0),Vector3(1.0,1.0,0.0),Vector3(0.0,1.0,0.0)]
    for point in p: _ = b.add_vertex(point)
    _ = b.add_spline_edge(0,1,[p[0],Vector3(0.02,0.0,0.0),Vector3(0.05,0.0,0.0),p[1]],[1.0,1.0,1.0,1.0],3,[0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0],true)
    for at in 1..<4: _ = b.add_line(at,(at+1)%4)
    _ = b.add_surface(0,Vector3(),Vector3(0.0,0.0,1.0),Vector3(1.0,0.0,0.0))
    _ = b.add_face(0,[1,2,3,4],[4])
    let mesh = b.mesh(8,0.1)
    var minimum = 1.0
    var maximum = 0.0
    for edge in 0..<mesh.edge_count():
        let a = mesh.point(mesh.edge_point(edge,0))
        let c = mesh.point(mesh.edge_point(edge,1))
        if math.abs(a.y)+math.abs(c.y) > 0.000001: continue
        let length = c.subtract(a).length()
        minimum = math.min(minimum,length)
        maximum = math.max(maximum,length)
    assert(maximum/minimum < 1.02)

func tangent_trim() -> unit!:
    let b = BrepModel(0.01)
    let p = [Vector3(1.0,0.0,0.0),Vector3(),Vector3(0.0,1.0,0.0)]
    for point in p: _ = b.add_vertex(point)
    _ = b.add_line(0,1)
    _ = b.add_line(1,2)
    _ = b.add_spline_edge(2,0,[p[2],Vector3(0.5,0.0,0.0),Vector3(0.75,0.0,0.0),p[0]],[1.0,1.0,1.0,1.0],3,[0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0],true)
    _ = b.add_surface(0,Vector3(),Vector3(0.0,0.0,1.0),Vector3(1.0,0.0,0.0))
    _ = b.add_face(0,[1,2,3],[3],false)
    let mesh = b.mesh(16)
    let clipped = mesh.find_attribute("cad_trim_grid",2)
    for face in 0..<mesh.face_count(): assert(mesh.attribute_value(clipped,face,0) == 1.0)

## Degree-elevated straight extrusion: shape needs longitudinal pitch even
## though curvature needs none. Exercise production planning, not just helpers.
func affine_strip() -> unit!:
    for swapped in [false,true]:
        let b = BrepModel()
        let p = [Vector3(),Vector3(1.0,0.0,0.0),Vector3(1.0,20.0,0.0),Vector3(0.0,20.0,0.0)]
        for point in p: _ = b.add_vertex(point)
        let quadratic = [0.0,0.0,0.0,1.0,1.0,1.0]
        let cubic = [0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0]
        _ = b.add_spline_edge(0,1,[p[0],Vector3(0.5,0.0,0.3),p[1]],[1.0,1.0,1.0],2,quadratic,true)
        _ = b.add_line(1,2)
        _ = b.add_spline_edge(2,3,[p[2],Vector3(0.5,20.0,0.3),p[3]],[1.0,1.0,1.0],2,quadratic,true)
        _ = b.add_line(3,0)
        let points: list[Vector3] = []
        let weights: list[float] = []
        for a in 0..<(4 if swapped else 3):
            for c in 0..<(3 if swapped else 4):
                let u = c if swapped else a
                let v = a if swapped else c
                points.append(Vector3(float(u)*0.5,20.0*float(v)/3.0,0.3 if u == 1 else 0.0))
                weights.append(1.0)
        _ = b.add_spline_surface(points,weights,4 if swapped else 3,3 if swapped else 4,
            3 if swapped else 2,2 if swapped else 3,cubic if swapped else quadratic,quadratic if swapped else cubic)
        _ = b.add_face(0,[1,2,3,4],[4],not swapped)
        let mesh = b.mesh(8)
        assert(mesh.face_count() > 32)
        for at in 0..<mesh.point_count():
            let q = mesh.point(at)
            near(q.z,0.6*q.x*(1.0-q.x))
        for face in 0..<mesh.face_count():
            assert(mesh.face_size(face) == 4)
            var low = 1e100
            var high = 0.0
            for corner in 0..<4:
                let a = mesh.point(mesh.face_point(face,corner))
                let c = mesh.point(mesh.face_point(face,(corner+1)%4))
                let length = a.subtract(c).length()
                low = math.min(low,length)
                high = math.max(high,length)
                assert(math.abs(a.x-c.x) < 1e-8 or math.abs(a.y-c.y) < 1e-8)
            assert(high/low < 18.0)

## A notch has eight logical corners, so mapped planning is unavailable.
## Its Boolean support grid still needs physical pitch along the straight axis.
func notched_extrusion() -> unit!:
    for swapped in [false,true]:
        for reversed in [false,true]:
            let b = BrepModel()
            let uv = [Vector3(),Vector3(1.0,0.0,0.0),Vector3(1.0,20.0,0.0),Vector3(0.0,20.0,0.0),
                Vector3(0.0,11.0,0.0),Vector3(0.2,11.0,0.0),Vector3(0.2,9.0,0.0),Vector3(0.0,9.0,0.0)]
            let p: list[Vector3] = []
            for q in uv:
                p.append(Vector3(q.x,q.y,0.6*q.x*(1.0-q.x)))
                _ = b.add_vertex(p[-1])
            let quadratic = [0.0,0.0,0.0,1.0,1.0,1.0]
            let linear = [0.0,0.0,1.0,1.0]
            for at in 0..<p.length:
                let next = (at+1)%p.length
                let a = p[at]
                let c = p[next]
                if a.x == c.x:
                    _ = b.add_line(at,next)
                else:
                    let dx = c.x-a.x
                    let middle = Vector3((a.x+c.x)*0.5,a.y,(a.z+c.z)*0.5+0.3*dx*dx)
                    _ = b.add_spline_edge(at,next,[a,middle,c],[1.0,1.0,1.0],2,quadratic,true)
            let controls: list[Vector3] = []
            for a in 0..<(2 if swapped else 3):
                for c in 0..<(3 if swapped else 2):
                    let u = c if swapped else a
                    let v = a if swapped else c
                    controls.append(Vector3(float(u)*0.5,float(v)*20.0,0.3 if u == 1 else 0.0))
            _ = b.add_spline_surface(controls,[1.0,1.0,1.0,1.0,1.0,1.0],2 if swapped else 3,3 if swapped else 2,
                1 if swapped else 2,2 if swapped else 1,linear if swapped else quadratic,quadratic if swapped else linear)
            _ = b.add_face(0,[-8,-7,-6,-5,-4,-3,-2,-1] if reversed else [1,2,3,4,5,6,7,8],[8],swapped == reversed)
            let mesh = b.mesh(8)
            let method = mesh.find_attribute("cad_mesher",2)
            var quads = 0
            for at in 0..<mesh.point_count():
                let q = mesh.point(at)
                near(q.z,0.6*q.x*(1.0-q.x))
                assert(not (q.x < 0.2-1e-8 and q.y > 9.0+1e-8 and q.y < 11.0-1e-8))
            for face in 0..<mesh.face_count():
                assert(mesh.attribute_value(method,face,0) == 2.0)
                if mesh.face_size(face) != 4: continue
                quads += 1
                for corner in 0..<4:
                    let a = mesh.point(mesh.face_point(face,corner))
                    let c = mesh.point(mesh.face_point(face,(corner+1)%4))
                    assert(math.abs(a.x-c.x) < 1e-8 or math.abs(a.y-c.y) < 1e-8)
                    assert(math.abs(a.y-c.y) < 2.5)
            assert(quads >= 40)