Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / layout / rails.lucb

## Parameter rails are layout decisions, not a change to the exact CAD trim.
## A fitted STEP support may invert an exact circle to a numerically wavering
## UV line. A dimensionless UV epsilon alone must not create a second row family.

pub func iso_axis(uv: const Vector3[], epsilon: f64) -> i64:
    let low = bounds(uv,false)
    let high = bounds(uv,true)
    if high.y-low.y < epsilon: return 0
    if high.x-low.x < epsilon: return 1
    return -1

## Admit only a nearly constant coordinate whose physical displacement to a
## constant rail fits the model tolerance at every station. This neither snaps
## UVs nor moves canonical 3D points, and oblique trims remain unclassified.
pub func rail_axis(s: Surface, uv: const Vector3[], epsilon: f64, tolerance: f64) -> i64!:
    let low = bounds(uv,false)
    let high = bounds(uv,true)
    for axis in 0..<2:
        let along = high.x-low.x if axis == 0 else high.y-low.y
        let across = high.y-low.y if axis == 0 else high.x-low.x
        if across > math.max(epsilon,along*1e-4) or along <= epsilon: continue
        if across == 0.0: return axis
        let constant = (low.y+high.y)*0.5 if axis == 0 else (low.x+high.x)*0.5
        var valid = true
        for p in uv:
            let candidate = Vector3(p.x,constant,0.0) if axis == 0 else Vector3(constant,p.y,0.0)
            if (try surface_point(s,candidate)).subtract(try surface_point(s,p)).length() > tolerance:
                valid = false
                break
        if valid: return axis
    return -1

pub func bounds(uv: const Vector3[], high: bool) -> Vector3:
    var result = uv[0]
    for p in uv:
        result = Vector3(math.max(result.x,p.x),math.max(result.y,p.y),0.0) if high else Vector3(math.min(result.x,p.x),math.min(result.y,p.y),0.0)
    return result