Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / src / layout / density.lucb

## Directional grid support from physical-space chord error, not uniform UV size.

func density_point(s: Surface, low: Vector3, high: Vector3, t: f64, other: f64, axis: usize) -> Vector3!:
    return try surface_point(s, Vector3(low.x + (high.x - low.x) * (t if axis == 0 else other), low.y + (high.y - low.y) * (other if axis == 0 else t), 0.0))

## Preserve inherited seam phase; only fill gaps that exceed required spacing.
pub func fill(stations: Stations*, required: usize, epsilon: f64) -> !:
    let low = stations.values[0]
    let high = stations.values[stations.count - 1]
    let spacing = (high - low) / (f64)required
    var at: usize = 0
    while at + 1 < stations.count:
        let a = stations.values[at]
        let b = stations.values[at + 1]
        let count = (usize)math.max(1.0, math.ceil((b - a) / spacing - 1e-6))
        for step in 1..<count: _ = try stations.insert(a + (b - a) * (f64)step / (f64)count, epsilon)
        at += count

## Endpoint-only smooth joins impose no interior phase. A long planar strip
## may use physical pitch unless a rail actually contributed interior stations.
## Compact domains retain their existing initial resolution policy. Returns
## whether the chart owns an independent phase for later flow admission.
pub func fill_plane(u: Stations*, v: Stations*, trim_low: Vector3, trim_high: Vector3, required: usize, epsilon: f64) -> bool!:
    let x = u.values[u.count - 1] - u.values[0]
    let y = v.values[v.count - 1] - v.values[0]
    let longest = math.max(x, y)
    let independent = longest > 4.0 * math.min(x, y) and not interior(*u, trim_low.x, trim_high.x, epsilon) and not interior(*v, trim_low.y, trim_high.y, epsilon)
    let nu = (usize)math.max(1.0, math.ceil((f64)required * x / longest)) if independent else required
    let nv = (usize)math.max(1.0, math.ceil((f64)required * y / longest)) if independent else required
    try fill(u, nu, epsilon)
    try fill(v, nv, epsilon)
    return independent

func interior(stations: Stations, low: f64, high: f64, epsilon: f64) -> bool:
    for value in stations.values[..<stations.count]:
        if value > low + epsilon and value < high - epsilon: return true
    return false

## Insert complete rows only where directional deviation needs them. This
## resolves narrow knot spans without doubling an entire patch's resolution.
pub func refine_axis(stations: Stations*, s: Surface, low: Vector3, high: Vector3, axis: usize, divisions: i64, tolerance: f64, edge_size: f64, epsilon: f64) -> !:
    var at: usize = 0
    let start = low.x if axis == 0 else low.y
    let span = (high.x - low.x) if axis == 0 else (high.y - low.y)
    var targets: f64[5]
    for cross in 0..<(usize)5:
        let other = (f64)cross / 4.0
        let first = try density_point(s, low, high, 0.0, other, axis)
        let last = try density_point(s, low, high, 1.0, other, axis)
        var extent = last.subtract(first).length()
        # Endpoint chords understate the scale of closed AND nearly closed
        # isocurves (including analytic charts with exterior guard cells).
        # Use sampled extent before refining against the deflection budget.
        for sample in 1..<8:
            extent = math.max(extent, (try density_point(s, low, high, (f64)sample / 8.0, other, axis)).subtract(first).length())
        targets[cross] = math.max(tolerance * 0.5, extent / ((f64)divisions * (f64)divisions * 32.0))
    while at + 1 < stations.count:
        let a = stations.values[at]
        let b = stations.values[at + 1]
        # The worst chord sag over its target, and whether an edge is longer
        # than the edge size.
        var worst: f64 = 0.0
        var long = false
        for cross in 0..<(usize)5:
            let other = (f64)cross / 4.0
            let pa = try density_point(s, low, high, (a - start) / span, other, axis)
            let pb = try density_point(s, low, high, (b - start) / span, other, axis)
            let delta = pb.subtract(pa)
            if edge_size > 0.0 and delta.length() > edge_size: long = true
            for sample in 1..<(usize)4:
                let t = (a + (b - a) * (f64)sample / 4.0 - start) / span
                let p = try density_point(s, low, high, t, other, axis)
                let projection = math.clamp(p.subtract(pa).dot(delta) / delta.dot(delta), 0.0, 1.0) if delta.dot(delta) > 1e-30 else 0.0
                worst = math.max(worst, p.subtract(pa.add(delta.multiply_scalar(projection))).length() / targets[cross])
        if worst <= 1.0 and not long:
            at += 1
            continue
        # As many even pieces as the sag needs (it falls as the square of
        # the piece), not halves until it fits: halving overshoots up to
        # twice the count, past the station budget on long cylinders.
        let pieces = (usize)math.max(2.0, math.ceil(math.sqrt(worst * 2.0)))
        # Curvature is a hard requirement: bypass optional row-phase spacing.
        let separation = stations.separation
        stations.separation = 0.0
        defer stations.separation = separation
        for piece in 1..<pieces:
            let added = try stations.insert(a + (b - a) * (f64)piece / (f64)pieces, epsilon)
            if not added: error(invalid, "curvature cannot be resolved within parameter precision")

## Move each grid line that passes near a trim sample (within `fraction`
## of its spacing to the next line) onto it, unless the line already runs
## through a sample (a rail's, which it keeps). A line missing a sample by a
## little crosses the trim beside it: the crossing becomes a station next to
## the sample, a needle on both faces sharing the edge.
pub func snap_lines(stations: Stations*, chains: const UVChain[], axis: usize, fraction: f64) -> !:
    if stations.count < 3: return
    let locked = try new bool[stations.count] --- in memory.heap
    defer free(locked) in memory.heap
    for at in 0..<locked.length: locked[at] = false
    let exact = (stations.values[stations.count - 1] - stations.values[0]) * 1e-12
    for pass in 0..<(usize)2:
        for chain in chains:
            for p in chain.points:
                let value = p.x if axis == 0 else p.y
                var low: usize = 0
                var high = stations.count - 1
                while high - low > 1:
                    let mid = (low + high) // 2
                    if stations.values[mid] <= value: low = mid
                    else: high = mid
                let line = low if math.abs(stations.values[low] - value) <= math.abs(stations.values[high] - value) else high
                let distance = math.abs(stations.values[line] - value)
                if pass == 0:
                    if distance <= exact: locked[line] = true
                    continue
                # The domain's ends stay where they are.
                if locked[line] or line == 0 or line == stations.count - 1: continue
                let gap = math.min(stations.values[line] - stations.values[line - 1], stations.values[line + 1] - stations.values[line])
                if distance < gap * fraction:
                    stations.values[line] = value
                    locked[line] = true