Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / curvature_tests.luc

## Principal curvature attributes from the exact supports: spheres, cylinders
## (analytic and rational NURBS), holes, planes, scaled placements.
from luce_cad.model import BrepModel, CadModel
from luce_geocore.geocore import Vector3, Mesh
from luce_std import math

func close_to(actual: float, expected: float, tolerance: float = 0.000001):
    if math.abs(actual - expected) > tolerance: print(f"curvature: actual={actual}, expected={expected}")
    assert(math.abs(actual - expected) <= tolerance)

## Every corner of CAD face `face` (all faces when negative) has k1 and k2.
func expect(mesh: Mesh, face: int, k1: float, k2: float, tolerance: float = 0.000001) -> unit!:
    let a = mesh.find_attribute("curvature.k1", 1)
    let b = mesh.find_attribute("curvature.k2", 1)
    let owner = mesh.find_attribute("cad_face", 2)
    assert(a >= 0 and b >= 0 and owner >= 0)
    var checked = 0
    for polygon in 0..<mesh.face_count():
        if face >= 0 and int(mesh.attribute_value(owner, polygon)) != face: continue
        for at in 0..<mesh.face_size(polygon):
            let corner = mesh.face_offset(polygon) + at
            close_to(mesh.attribute_value(a, corner), k1, tolerance)
            close_to(mesh.attribute_value(b, corner), k2, tolerance)
            checked += 1
    assert(checked > 0)

func sphere_cap(radius: float) -> BrepModel!:
    let z = Vector3(0.0, 0.0, 1.0)
    let x = Vector3(1.0, 0.0, 0.0)
    let sphere = BrepModel()
    _ = sphere.add_vertex(Vector3(radius * 0.8660254037844386, 0.0, radius * 0.5))
    _ = sphere.add_circle(0, 0, Vector3(0.0, 0.0, radius * 0.5), z, x, radius * 0.8660254037844386)
    _ = sphere.add_surface(4, Vector3(), z, x, radius)
    _ = sphere.add_face(0, [1], [1])
    return sphere

pub func curvature_tests() -> unit!:
    let z = Vector3(0.0, 0.0, 1.0)
    let x = Vector3(1.0, 0.0, 0.0)
    # Spheres: H = 1/r and K = 1/r^2 at every corner, including the pole.
    for radius in [1.0, 2.0, 0.25]:
        let cap = sphere_cap(radius).mesh(8, curvature = true)
        expect(cap, 0, 1.0 / radius, 1.0 / radius)
        let mean = cap.find_attribute("curvature.k1", 1)
        let second = cap.find_attribute("curvature.k2", 1)
        for corner in 0..<cap.vertex_count():
            let k1 = cap.attribute_value(mean, corner)
            let k2 = cap.attribute_value(second, corner)
            close_to((k1 + k2) * 0.5, 1.0 / radius)
            close_to(k1 * k2, 1.0 / (radius * radius))
    # Off by default: no analysis columns unless asked for.
    assert(sphere_cap(1.0).mesh(8).find_attribute("curvature.k1", 1) < 0)
    # Hollow cylinder: the convex wall k1 = 1/r, k2 = 0; the bore, whose
    # normals face the axis, bends toward them: k1 = 0, k2 = -1/r. Caps flat.
    let b = BrepModel()
    for p in [Vector3(2.0, 0.0, 0.0), Vector3(2.0, 0.0, 3.0), Vector3(1.0, 0.0, 0.0), Vector3(1.0, 0.0, 3.0)]:
        _ = b.add_vertex(p)
    _ = b.add_circle(0, 0, Vector3(), z, x, 2.0)
    _ = b.add_circle(1, 1, Vector3(0.0, 0.0, 3.0), z, x, 2.0)
    _ = b.add_line(0, 1)
    _ = b.add_circle(2, 2, Vector3(), z, x, 1.0)
    _ = b.add_circle(3, 3, Vector3(0.0, 0.0, 3.0), z, x, 1.0)
    _ = b.add_line(2, 3)
    let outside = b.add_surface(1, Vector3(), z, x, 2.0)
    let inside = b.add_surface(1, Vector3(), z, x, 1.0)
    let bottom = b.add_surface(0, Vector3(), z, x)
    let top = b.add_surface(0, Vector3(0.0, 0.0, 3.0), z, x)
    _ = b.add_face(outside, [1, 3, -2, -3], [4])
    _ = b.add_face(inside, [4, 6, -5, -6], [4], false)
    _ = b.add_face(bottom, [1, -4], [1, 1], false)
    _ = b.add_face(top, [2, -5], [1, 1])
    let tube = b.mesh(8, curvature = true, directions = true)
    expect(tube, 0, 0.5, 0.0)
    expect(tube, 1, 0.0, -1.0)
    expect(tube, 2, 0.0, 0.0)
    expect(tube, 3, 0.0, 0.0)
    # The wall bends around the axis: d1 is horizontal, d2 runs along it.
    let d1 = tube.find_attribute("curvature.d1", 1)
    let d2 = tube.find_attribute("curvature.d2", 1)
    let owner = tube.find_attribute("cad_face", 2)
    for polygon in 0..<tube.face_count():
        if int(tube.attribute_value(owner, polygon)) != 0: continue
        let corner = tube.face_offset(polygon)
        close_to(tube.attribute_value(d1, corner, 2), 0.0)
        close_to(math.abs(tube.attribute_value(d2, corner, 2)), 1.0)
    # A uniform placement scale divides curvature; a nonuniform one is refused.
    let model = CadModel()
    model.attach_brep(sphere_cap(1.0))
    expect(model.transformed(Vector3(1.0, 2.0, 3.0), Vector3(0.3, 0.2, 0.1), Vector3(2.0, 2.0, 2.0)).tessellate(8, curvature = true), 0, 0.5, 0.5)
    var refused = false
    _ = model.transformed(Vector3(), Vector3(), Vector3(1.0, 2.0, 1.0)).tessellate(8, curvature = true) catch failure:
        refused = true
    assert(refused)
    # Rational NURBS quarter cylinder, r = 3: exact second derivatives.
    let r = 3.0
    let w = 0.7071067811865476
    let arc = BrepModel()
    for p in [Vector3(r, 0.0, 0.0), Vector3(r, 0.0, 2.0), Vector3(0.0, r, 0.0), Vector3(0.0, r, 2.0)]:
        _ = arc.add_vertex(p)
    _ = arc.add_circle(0, 2, Vector3(), z, x, r)
    _ = arc.add_line(2, 3)
    _ = arc.add_circle(1, 3, Vector3(0.0, 0.0, 2.0), z, x, r)
    _ = arc.add_line(1, 0)
    let net = [Vector3(r, 0.0, 0.0), Vector3(r, 0.0, 2.0), Vector3(r, r, 0.0), Vector3(r, r, 2.0), Vector3(0.0, r, 0.0), Vector3(0.0, r, 2.0)]
    let spline = arc.add_spline_surface(net, [1.0, 1.0, w, w, 1.0, 1.0], 3, 2, 2, 1, [0.0, 0.0, 0.0, 1.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0])
    _ = arc.add_face(spline, [1, 2, -3, 4], [4])
    expect(arc.mesh(8, curvature = true), 0, 1.0 / r, 0.0, 0.00001)
    print("PASS principal curvature: spheres, cylinders, bores, planes, NURBS, placements")