Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / layout / balance.lucb

## Continue boundary rows through mapped patches without introducing new phases
## on aligned circular bands. Count-only matching is insufficient for fillets.

## What a face's last balance visit saw. A visit reads only the face's rows,
## its edges' cuts (both only ever grow, so equal counts mean equal contents)
## and its mapped state; a visit that changed nothing, repeated on the same
## inputs, again changes nothing, so it is skipped.
pub struct Settled:
    var cuts: usize = 0
    var rows: usize = 0
    var mapped: bool = false
    var clean: bool = false

## Preserve original stations, pair circular phases, then balance logical sides.
## `settled` (one per face, kept across calls by the planner) may start clean.
pub func balance(faces: const BrepFace[], edges: const Curve[], sides: const PatchSides[], cuts: Stations[], metrics: const ArcMetric[], plans: GridLayout[], tolerance: f64, settled: Settled[]) -> !:
    for pass in 0..<32:
        var changed = false
        for face_id in 0..<faces.length:
            let side = sides[face_id]
            let face = faces[face_id]
            let rows = plans[face_id].rows
            let before = changed
            changed = false
            let seen = signature(face,side,cuts,plans[face_id])
            if settled[face_id].clean and seen.cuts == settled[face_id].cuts and seen.rows == settled[face_id].rows and seen.mapped == settled[face_id].mapped:
                changed = before
                continue
            for use in 0..<face.uses.length:
                if not plans[face_id].flow[use]: continue
                let shared = cuts[edge_index(face.uses[use])]
                for at in 0..<shared.count:
                    let added = try rows[use].insert(shared.values[at],same_cut)
                    changed = changed or added
            let mapped = side.valid and not plans[face_id].valid and not plans[face_id].cut_first
            for pair in 0..<(2 if mapped else (usize)0):
                let aligned = try align_sides(face,edges,side,pair,rows,tolerance)
                changed = changed or aligned
            var totals: usize[4]
            for axis in 0..<(4 if mapped else (usize)0):
                totals[axis] = 0
                for segment in 0..<side.lengths[axis]:
                    let use = (side.starts[axis]+segment)%face.uses.length
                    totals[axis] += rows[use].count-1
            for pair in 0..<(2 if mapped else (usize)0):
                let a = totals[pair]
                let b = totals[pair+2]
                if a == b or a > 512 or b > 512: continue
                let axis = pair if a < b else pair+2
                let missing = b-a if a < b else a-b
                for extra in 0..<missing:
                    var largest: f64 = 0.0
                    var selected: usize = 0
                    var interval: usize = 0
                    for segment in 0..<side.lengths[axis]:
                        let use = (side.starts[axis]+segment)%face.uses.length
                        let metric = metrics[edge_index(face.uses[use])]
                        for at in 0..<rows[use].count-1:
                            let gap = metric.at(rows[use].values[at+1])-metric.at(rows[use].values[at])
                            if gap > largest:
                                largest = gap
                                selected = use
                                interval = at
                    if largest <= 1e-14: break
                    let cut = &rows[selected]
                    let metric = metrics[edge_index(face.uses[selected])]
                    let midpoint = metric.parameter((metric.at(cut.values[interval])+metric.at(cut.values[interval+1]))*0.5)
                    let inserted = try cut.insert(midpoint,same_cut)
                    changed = changed or inserted
            # Publishing a row only adds a boundary corner to a stopped neighbor.
            # It becomes an interior row there only across a classified flow seam.
            # Across a hard seam the row first takes a station already there
            # when one is within half its spacing (nest_rows): two families
            # of different counts unioned beat into stations a hair apart.
            for use in 0..<face.uses.length:
                let shared = &cuts[edge_index(face.uses[use])]
                if not plans[face_id].flow[use]: nest_rows(&rows[use], *shared, metrics[edge_index(face.uses[use])])
                for at in 0..<rows[use].count:
                    let added = try shared.insert(rows[use].values[at],same_cut)
                    changed = changed or added
            settled[face_id] = signature(face,side,cuts,plans[face_id])
            settled[face_id].clean = not changed
            changed = before or changed
        if not changed: break

## A face's rows on an edge (`rows`) moved onto stations of the edge's
## shared cuts already there, each to the nearest within `nest_share` of its
## distance to the rows beside it (along the edge's arc length), so that the
## edge's stations nest instead of beating: a row of a 33-row family next
## to a 32-row family's lands a 33rd of their spacing from it, a sliver
## cell on both faces. The rows' ends (the edge's) stay.
func nest_rows(rows: Stations*, shared: Stations, metric: ArcMetric):
    if rows.count < 3 or shared.count < 3: return
    var station: usize = 0
    for at in 1..<rows.count - 1:
        let value = rows.values[at]
        while station + 1 < shared.count and shared.values[station + 1] <= value: station += 1
        let below = shared.values[station]
        let above = shared.values[station + 1] if station + 1 < shared.count else below
        let here = metric.at(value)
        let target = below if math.abs(metric.at(below) - here) <= math.abs(metric.at(above) - here) else above
        let offset = math.abs(metric.at(target) - here)
        if offset <= same_cut: continue
        let before = here - metric.at(rows.values[at - 1])
        let after = metric.at(rows.values[at + 1]) - here
        if offset >= math.min(before, after) * nest_share: continue
        rows.values[at] = target

## How near a station a row moves onto it, as a share of the gap to the
## rows beside it: under half, it stays nearer its own place than theirs.
let nest_share: f64 = 0.5

func signature(face: BrepFace, side: PatchSides, cuts: const Stations[], plan: GridLayout) -> Settled:
    var result = Settled()
    for use in 0..<face.uses.length:
        result.cuts += cuts[edge_index(face.uses[use])].count
        result.rows += plan.rows[use].count
    result.mapped = side.valid and not plan.valid and not plan.cut_first
    return result