Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.4

0.9.4 / src / modeling / radius_law.lucb

## A variable radius fillet's radius along its tangent chain (Fusion's
## Variable radius: radii at positions 0 to 1 along the chain, its start and
## end among them; Fusion's API measures a position as the fraction of the
## curve's length). The chain is the picked edges in order: it starts at an
## open end (the first named edge's chain followed back), or, a closed loop,
## at the first named edge's start. Each edge's arc length is tabled once, so
## the radius at any parameter of any chain edge is a lookup.
##
## Between points the radius is a cubic (Hermite) on each piece, its slope
## at each point either Fusion's (law_slopes, the default: straight between
## two radii, a natural cubic spline through more, held flat at an extreme
## and along equal radii) or nil (smooth: held a moment at every point,
## Inventor's Smooth radius transition). A closed loop's law is Fusion's
## too: its radii at mid points only, round the loop (the start and end
## radii are not used; one mid radius holds all round). Blends that take a
## law (cap_blend.lucb, curved_blend.lucb) ask it for the radius at each
## section.

## Samples of each edge's arc length table.
let law_samples: usize = 64

## The radius at `s` (0 to 1) of points `positions` (ascending, 0 and 1
## among them) with `radii` and `slopes` (per unit of `s`) there.
func radius_at(positions: const f64[], radii: const f64[], slopes: const f64[], s: f64) -> f64:
    var k: usize = 0
    while k + 2 < positions.length and s > positions[k + 1]: k += 1
    return hermite(positions, radii, slopes, k, math.clamp(s, positions[k], positions[k + 1]))

## Piece `k`'s cubic (from point k to k + 1) at `s`, carried on outside it.
func hermite(positions: const f64[], radii: const f64[], slopes: const f64[], k: usize, s: f64) -> f64:
    let h = positions[k + 1] - positions[k]
    if h <= 0.0: return radii[k]
    let u = (s - positions[k]) / h
    let u2 = u * u
    let u3 = u2 * u
    return (2.0 * u3 - 3.0 * u2 + 1.0) * radii[k] + (u3 - 2.0 * u2 + u) * h * slopes[k] + (3.0 * u2 - 2.0 * u3) * radii[k + 1] + (u3 - u2) * h * slopes[k + 1]

## The slopes at `positions` (rising) of Fusion's law through `radii` onto
## `slopes` (all nil when `smooth`). Fusion 2705 read back (probes 5 to 7
## of 2026-10-12, luce_work/cad/fusion/FINDINGS.md): two radii run straight
## (and points in a line); otherwise the law is a natural cubic spline, to
## 0.003 where that spline keeps to the radii's shape. Where it would not,
## Fusion keeps the shape: flat at a point that is an extreme or beside an
## equal radius (equal radii hold between them), so the runs between such
## points are natural splines held flat at those ends. Those runs whose
## spline would still turn back (a steep piece beside a shallow one) are
## limited as Fritsch and Carlson's monotone cubic is. Fusion's own pieces
## there (into a flat run from an open end, steep beside shallow) are not
## cubics and are not decoded: ours differ by up to 0.4 (MODELING.md,
## Variable radius); the spline's pieces, the extremes and flat runs are
## Fusion's.
func law_slopes(positions: const f64[], radii: const f64[], smooth: bool, slopes: Growing[f64]*) -> !:
    let n = positions.length
    slopes.clear()
    try slopes.fill(n, 0.0)
    if smooth or n < 2: return
    # Held flat: interior extremes and points beside an equal radius.
    var held = Growing[bool]()
    defer held.close()
    try held.fill(n, false)
    for i in 1..<n - 1:
        if (radii[i] - radii[i - 1]) * (radii[i + 1] - radii[i]) <= 0.0: held.items[i] = true
    # Each run between held points (or the ends) a natural spline, held
    # flat at its held ends: the slope form, tridiagonal.
    var a: usize = 0
    while a < n - 1:
        var b = a + 1
        while b < n - 1 and not held.items[b]: b += 1
        try run_slopes(positions, radii, a, b, held.items[a], held.items[b], slopes)
        a = b
    # Fritsch-Carlson: a piece's slopes kept to its chord's sign and within
    # the circle of 3 in the chord's units, so it never turns back.
    for k in 0..<n - 1:
        let d = (radii[k + 1] - radii[k]) / (positions[k + 1] - positions[k])
        if d == 0.0:
            slopes.items[k] = 0.0
            slopes.items[k + 1] = 0.0
            continue
        var alpha = slopes.items[k] / d
        var beta = slopes.items[k + 1] / d
        if alpha < 0.0: alpha = 0.0
        if beta < 0.0: beta = 0.0
        let q = alpha * alpha + beta * beta
        if q > 9.0:
            let tau = 3.0 / math.sqrt(q)
            alpha *= tau
            beta *= tau
        slopes.items[k] = alpha * d
        slopes.items[k + 1] = beta * d

## The natural spline's slopes through points `a` to `b`, nil at an end
## that is `held` (else natural: no curvature there), onto `slopes`.
func run_slopes(positions: const f64[], radii: const f64[], a: usize, b: usize, held_a: bool, held_b: bool, slopes: Growing[f64]*) -> !:
    let count = b - a + 1
    var below = Growing[f64]()
    defer below.close()
    var diagonal = Growing[f64]()
    defer diagonal.close()
    var above = Growing[f64]()
    defer above.close()
    var sides = Growing[f64]()
    defer sides.close()
    for j in 0..<count:
        let i = a + j
        var l = 0.0
        var m = 1.0
        var u = 0.0
        var r = 0.0
        let pinned = (j == 0 and held_a) or (j == count - 1 and held_b)
        if pinned:
            m = 1.0
        elif j == 0:
            let h = positions[i + 1] - positions[i]
            m = 2.0 / h
            u = 1.0 / h
            r = 3.0 * (radii[i + 1] - radii[i]) / (h * h)
        elif j == count - 1:
            let h = positions[i] - positions[i - 1]
            l = 1.0 / h
            m = 2.0 / h
            r = 3.0 * (radii[i] - radii[i - 1]) / (h * h)
        else:
            let h0 = positions[i] - positions[i - 1]
            let h1 = positions[i + 1] - positions[i]
            l = 1.0 / h0
            m = 2.0 * (1.0 / h0 + 1.0 / h1)
            u = 1.0 / h1
            r = 3.0 * ((radii[i] - radii[i - 1]) / (h0 * h0) + (radii[i + 1] - radii[i]) / (h1 * h1))
        try below.push(l)
        try diagonal.push(m)
        try above.push(u)
        try sides.push(r)
    # Thomas: forward, then back.
    for j in 1..<count:
        let f = below.items[j] / diagonal.items[j - 1]
        diagonal.items[j] -= f * above.items[j - 1]
        sides.items[j] -= f * sides.items[j - 1]
    slopes.items[b] = sides.items[count - 1] / diagonal.items[count - 1]
    var j = count - 1
    while j > 0:
        j -= 1
        slopes.items[a + j] = (sides.items[j] - above.items[j] * slopes.items[a + j + 1]) / diagonal.items[j]

## A radius law over a chain of a shape's edges (see the header).
struct RadiusLaw:
    var positions: Growing[f64]
    var radii: Growing[f64]
    ## The law's slope at each point (law_slopes).
    var slopes: Growing[f64]
    var smooth: bool
    ## Per shape edge: its place in the chain (-1: not on it), whether the
    ## chain runs its way (t0 to t1), the chain's length before it, and its
    ## arc length table (law_samples + 1 values from t0).
    var place: Growing[i64]
    var forward: Growing[bool]
    var before: Growing[f64]
    var lengths: Growing[f64]
    var total: f64
    var closed: bool

    func close():
        self.positions.close()
        self.radii.close()
        self.slopes.close()
        self.smooth = false
        self.place.close()
        self.forward.close()
        self.before.close()
        self.lengths.close()

    ## A copy, its tables its own.
    func copied() -> RadiusLaw!:
        var made = RadiusLaw(Growing[f64](), Growing[f64](), Growing[f64](), self.smooth, Growing[i64](), Growing[bool](), Growing[f64](), Growing[f64](), self.total, self.closed)
        errdefer made.close()
        try made.positions.assign(self.positions.view())
        try made.radii.assign(self.radii.view())
        try made.slopes.assign(self.slopes.view())
        try made.place.assign(self.place.view())
        try made.forward.assign(self.forward.view())
        try made.before.assign(self.before.view())
        try made.lengths.assign(self.lengths.view())
        return made

    ## Edge `e`'s length table replaced by `row` (law_samples + 1 lengths
    ## from its t0), then each chain edge's length before it and the
    ## chain's total again, in the chain's order.
    func retable(e: usize, row: const f64[]) -> !:
        if row.length != law_samples + 1: error(invalid, "a radius law's table has law_samples + 1 lengths")
        let first = e * (law_samples + 1)
        for k in 0..<row.length: self.lengths.items[first + k] = row[k]
        var count: i64 = 0
        for place in self.place.view():
            if place >= 0: count += 1
        self.total = 0.0
        for at in 0..<count:
            for g in 0..<self.place.count:
                if self.place.items[g] != at: continue
                self.before.items[g] = self.total
                self.total += self.lengths.items[g * (law_samples + 1) + law_samples]

    ## The largest radius.
    func largest() -> f64:
        var most = 0.0
        for r in self.radii.view(): most = math.max(most, r)
        return most

    ## The radius at fraction `s` of the chain.
    func at_fraction(s: f64) -> f64:
        return radius_at(self.positions.view(), self.radii.view(), self.slopes.view(), s)

    ## The fraction of the chain at edge `e`'s carrier parameter `t`.
    func fraction(w: const Shape*, e: usize, t: f64) -> f64!:
        let k = self.place.items[e]
        if k < 0: error(invalid, "a variable radius was asked off its chain")
        let edge = w.edges.items[e]
        let span = edge.t1 - edge.t0
        let f = math.clamp((t - edge.t0) / span, 0.0, 1.0) if span != 0.0 else 0.0
        let at = f * (f64)law_samples
        let low = (usize)math.min(math.floor(at), (f64)(law_samples - 1))
        let row = e * (law_samples + 1)
        let table = &self.lengths
        let run = table.items[row + low] + (table.items[row + low + 1] - table.items[row + low]) * (at - (f64)low)
        let own = table.items[row + law_samples]
        let into = run if self.forward.items[e] else own - run
        return math.clamp((self.before.items[e] + into) / self.total, 0.0, 1.0)

    ## Edge `e`'s carrier parameters (between `low` and `high`) where the
    ## law's points fall, onto `out`, rising: where its cubics meet, only
    ## their slopes alike (bisection on the chain's fraction).
    func points_on(w: const Shape*, e: usize, low: f64, high: f64, out: Growing[f64]*) -> !:
        out.clear()
        if self.place.items[e] < 0: return
        let f_low = try self.fraction(w, e, low)
        let f_high = try self.fraction(w, e, high)
        let lo = math.min(f_low, f_high)
        let hi = math.max(f_low, f_high)
        for k in 0..<self.positions.count:
            let f = self.positions.items[k]
            if f <= lo + 1e-9 or f >= hi - 1e-9: continue
            var a = low
            var b = high
            for _ in 0..<80:
                let m = (a + b) * 0.5
                if ((try self.fraction(w, e, m)) < f) == (f_low < f_high): a = m
                else: b = m
            try out.push((a + b) * 0.5)
        # Rising in the parameter.
        if out.count > 1 and out.items[0] > out.items[out.count - 1]:
            var i: usize = 0
            var j = out.count - 1
            while i < j:
                let keep = out.items[i]
                out.items[i] = out.items[j]
                out.items[j] = keep
                i += 1
                j -= 1

    ## The chain's edge and carrier parameter `s` along it (its length from
    ## the chain's start; a closed loop's wraps round): the arc length table
    ## read backward (linear between its samples, as `fraction` reads it).
    ## (-1, 0) off the chain.
    func at_length(w: const Shape*, s: f64) -> (i64, f64):
        var at = s
        if self.closed: at = s - math.floor(s / self.total) * self.total
        for e in 0..<self.place.count:
            if self.place.items[e] < 0: continue
            let edge = w.edges.items[e]
            let row = e * (law_samples + 1)
            let own = self.lengths.items[row + law_samples]
            let before = self.before.items[e]
            if at < before or at > before + own: continue
            let into = at - before
            let run = into if self.forward.items[e] else own - into
            var j: usize = 0
            while j + 1 < law_samples and self.lengths.items[row + j + 1] < run: j += 1
            let a = self.lengths.items[row + j]
            let gap = self.lengths.items[row + j + 1] - a
            let f = ((f64)j + ((run - a) / gap if gap > 0.0 else 0.0)) / (f64)law_samples
            return ((i64)e, edge.t0 + (edge.t1 - edge.t0) * f)
        return (-1, 0.0)

    ## The radius at edge `e`'s carrier parameter `t`.
    func at_edge(w: const Shape*, e: usize, t: f64) -> f64!:
        return self.at_fraction(try self.fraction(w, e, t))

## A law with nothing in it (a holder until a law is copied in).
func empty_law() -> RadiusLaw:
    return RadiusLaw(Growing[f64](), Growing[f64](), Growing[f64](), false, Growing[i64](), Growing[bool](), Growing[f64](), Growing[f64](), 0.0, false)

## The law of radii `radii` at `positions` (0 to 1, rising; the ends are
## added from the nearest radius when missing) along edges `chosen` of `w`,
## which must make one chain; `first` the edge it starts from (the first
## named).
func chain_law(w: const Shape*, chosen: const usize[], first: usize, positions: const f64[], radii: const f64[], smooth: bool) -> RadiusLaw!:
    var law = RadiusLaw(Growing[f64](), Growing[f64](), Growing[f64](), smooth, Growing[i64](), Growing[bool](), Growing[f64](), Growing[f64](), 0.0, false)
    errdefer law.close()
    try law_points(&law, positions, radii)
    try law.place.fill(w.edges.count, -1)
    try law.forward.fill(w.edges.count, true)
    try law.before.fill(w.edges.count, 0.0)
    try law.lengths.fill(w.edges.count * (law_samples + 1), 0.0)
    var picked = Growing[bool]()
    defer picked.close()
    try picked.fill(w.edges.count, false)
    for e in chosen: picked.items[e] = true
    # Back from the first edge's start to an open end (or round to itself).
    var e = first
    var v = w.edges.items[first].start
    law.closed = w.edges.items[first].start == w.edges.items[first].end
    var steps: usize = 0
    while not law.closed and steps < chosen.length:
        let previous = try next_on_chain(w, &picked, e, v)
        if previous < 0: break
        if (usize)previous == first:
            law.closed = true
            break
        let p = w.edges.items[(usize)previous]
        v = p.start if p.end == v else p.end
        e = (usize)previous
        steps += 1
    if law.closed:
        e = first
        v = w.edges.items[first].start
    # Forward from there, each edge's length tabled.
    var count: usize = 0
    while true:
        let edge = w.edges.items[e]
        law.place.items[e] = (i64)count
        law.forward.items[e] = edge.start == v
        law.before.items[e] = law.total
        law.total += try table_edge(w, e, &law.lengths)
        count += 1
        v = edge.end if edge.start == v else edge.start
        let next = try next_on_chain(w, &picked, e, v)
        if next < 0 or law.place.items[(usize)next] >= 0: break
        e = (usize)next
    if count != chosen.length: error(invalid, "a variable radius runs along one tangent chain: pick one edge or one chain")
    if law.total <= w.tolerance: error(invalid, "a variable radius's chain has no length")
    if law.closed: try loop_law(&law, positions, radii)
    else: try law_slopes(law.positions.view(), law.radii.view(), smooth, &law.slopes)
    return law

## A closed loop's law, as Fusion's: through its mid points (positions
## strictly inside 0 to 1) round the loop, the start and end radii not used
## (with no mid point, the start radius all round); the points three times
## over (a turn back, this turn, a turn on) give this turn's slopes, and the
## law's value and slope at 0 close it at both ends.
func loop_law(law: RadiusLaw*, positions: const f64[], radii: const f64[]) -> !:
    var at = Growing[f64]()
    defer at.close()
    var sizes = Growing[f64]()
    defer sizes.close()
    for turn in 0..<(usize)3:
        for k in 0..<positions.length:
            if positions[k] <= 0.0 or positions[k] >= 1.0: continue
            try at.push(positions[k] + (f64)turn - 1.0)
            try sizes.push(radii[k])
    let mids = at.count // 3
    law.positions.clear()
    law.radii.clear()
    law.slopes.clear()
    if mids == 0:
        try law.positions.push(0.0)
        try law.positions.push(1.0)
        try law.radii.push(radii[0])
        try law.radii.push(radii[0])
        try law.slopes.fill(2, 0.0)
        return
    var slopes = Growing[f64]()
    defer slopes.close()
    try law_slopes(at.view(), sizes.view(), law.smooth, &slopes)
    let start = radius_at(at.view(), sizes.view(), slopes.view(), 0.0)
    # The slope at 0, of the piece holding it.
    var k: usize = 0
    while at.items[k + 1] < 0.0: k += 1
    let h = 1e-6
    let rate = (hermite(at.view(), sizes.view(), slopes.view(), k, h) - hermite(at.view(), sizes.view(), slopes.view(), k, -h)) / (2.0 * h)
    try law.positions.push(0.0)
    try law.radii.push(start)
    try law.slopes.push(rate)
    for j in mids..<2 * mids:
        try law.positions.push(at.items[j])
        try law.radii.push(sizes.items[j])
        try law.slopes.push(slopes.items[j])
    try law.positions.push(1.0)
    try law.radii.push(start)
    try law.slopes.push(rate)

## The positions and radii checked, the chain's ends added.
func law_points(law: RadiusLaw*, positions: const f64[], radii: const f64[]) -> !:
    if positions.length != radii.length or positions.length == 0: error(invalid, "a variable radius needs a radius at each position")
    for k in 0..<positions.length:
        if not math.is_finite(positions[k]) or positions[k] < 0.0 or positions[k] > 1.0: error(invalid, "a variable radius's positions run from 0 to 1 along the edge")
        if not math.is_finite(radii[k]) or radii[k] <= 0.0: error(invalid, "a variable radius's radii are positive")
        if k > 0 and positions[k] <= positions[k - 1]: error(invalid, "a variable radius's positions must rise along the edge")
    if positions[0] > 0.0:
        try law.positions.push(0.0)
        try law.radii.push(radii[0])
    for k in 0..<positions.length:
        try law.positions.push(positions[k])
        try law.radii.push(radii[k])
    if positions[positions.length - 1] < 1.0:
        try law.positions.push(1.0)
        try law.radii.push(radii[radii.length - 1])

## The picked edge other than `e` at vertex `v` (-1: none, an open end).
func next_on_chain(w: const Shape*, picked: const Growing[bool]*, e: usize, v: i64) -> i64!:
    for g in 0..<w.edges.count:
        if g == e or not picked.items[g]: continue
        let other = w.edges.items[g]
        if other.start == v or other.end == v: return (i64)g
    return -1

## Edge `e`'s arc length from t0 at law_samples + 1 even steps of its
## parameter into its rows of `tables` (chords of a fine polyline); its
## whole length.
func table_edge(w: const Shape*, e: usize, tables: Growing[f64]*) -> f64!:
    let edge = w.edges.items[e]
    let row = e * (law_samples + 1)
    var previous = try w.at(edge.carrier, edge.t0)
    tables.items[row] = 0.0
    var run = 0.0
    for k in 1..<law_samples + 1:
        let here = try w.at(edge.carrier, edge.t0 + (edge.t1 - edge.t0) * (f64)k / (f64)law_samples)
        run += here.subtract(previous).length()
        tables.items[row + k] = run
        previous = here
    return run