Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / contracts / basis_contract.lucb

## B-spline bases and their derivatives (basis_derivatives, The NURBS
## Book's A2.3): finite at a knot repeated past the degree (a zero knot
## difference divides to 0, not NaN), a partition of unity (derivatives
## summing to zero), and matching differences of the bases away from knots.
from luce_std import math
from luce_geocore.geocore import invalid
from geometry import basis_derivatives

pub func basis_contracts() -> !:
    # Degree 2, an inner knot of full multiplicity (a break in the curve).
    let knots: f64[9] = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0]
    let parameters: f64[5] = [0.0, 0.4, 1.0, 1.7, 2.0]
    for t in parameters:
        var ders: f64[36]
        _ = basis_derivatives(knots[..<9], 6, 2, t, &ders)
        for k in 0..<(usize)3:
            var sum = 0.0
            for j in 0..<(usize)3:
                if not math.is_finite(ders[k * 12 + j]): error(invalid, "a basis derivative at a repeated knot is not finite")
                sum += ders[k * 12 + j]
            if math.abs(sum - (1.0 if k == 0 else 0.0)) > 1e-9: error(invalid, "bases and their derivatives do not sum as a partition of unity")
    # Away from knots: the derivatives are the bases' differences.
    let h = 1e-5
    let t = 0.4
    var at: f64[36]
    var before: f64[36]
    var after: f64[36]
    let first = basis_derivatives(knots[..<9], 6, 2, t, &at)
    if basis_derivatives(knots[..<9], 6, 2, t - h, &before) != first or basis_derivatives(knots[..<9], 6, 2, t + h, &after) != first: error(invalid, "nearby parameters left their knot span")
    for j in 0..<(usize)3:
        if math.abs((after[j] - before[j]) / (2.0 * h) - at[12 + j]) > 1e-6: error(invalid, "a basis's first derivative is off its difference")
        if math.abs((after[12 + j] - before[12 + j]) / (2.0 * h) - at[24 + j]) > 1e-4: error(invalid, "a basis's second derivative is off its difference")