Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / layout / mapped_extrusion.lucb

## One physical row phase for a mapped affine extrusion (or cylinder), its
## ends one edge each or several (a setback corner's halves), so its rows
## along it follow its pitch across: a single row of cells from end to end
## connects stations of the two ends that need not face each other, and
## its long triangles cut across the surface. Late clipped neighbors
## may contribute incompatible phases on its two straight rails. Preserve those
## canonical vertices as trim corners instead of bending the entire strip.

## Runs after global row reconciliation, before shared vertices are allocated.
## Only line rails, matching extrusion coverage and proven affine supports
## qualify. Curved row families and all original shared cuts remain untouched.
pub func regularize_extrusions(faces: const BrepFace[], surfaces: const Surface[], edges: const Curve[], sides: const PatchSides[], metrics: const ArcMetric[], cuts: Stations[], plans: GridLayout[], tolerance: f64) -> !:
    for at in 0..<faces.length:
        let face = faces[at]
        let side = sides[at]
        let plan = &plans[at]
        if plan.valid or not side.valid: continue
        let s = surfaces[(usize)face.surface]
        for axis in 0..<(usize)2:
            # The way the support runs straight: an affine extrusion's, or a
            # cylinder's axis (its chart's v).
            var translation = Vector3()
            if s.kind == surface_cylinder:
                if axis != 1: continue
                translation = s.z
            else:
                if not affine_axis(s, (i64)axis): continue
                let stride = (usize)s.nv if axis == 0 else 1
                let along = (usize)(s.nu if axis == 0 else s.nv)
                translation = s.points[(along - 1) * stride].subtract(s.points[0])
            for pair in 0..<(usize)2:
                if side.lengths[pair] != 1 or side.lengths[pair + 2] != 1: continue
                let a = side.starts[pair]
                let b = side.starts[pair + 2]
                let ai = edge_index(face.uses[a])
                let bi = edge_index(face.uses[b])
                if edges[ai].kind != curve_line or edges[bi].kind != curve_line: continue
                if not rails(edges[ai], edges[bi], translation, tolerance): continue
                # The ends (each one edge, or a setback corner's halves).
                let cross_count = row_count(face, side, (pair + 1) % 4, plan)
                # A single transverse span is valid on flat/low-curvature
                # profiles too. Its two boundary rails still define physical
                # pitch; it must not disable correction of a crowded phase.
                if cross_count < 1 or cross_count != row_count(face, side, (pair + 3) % 4, plan): continue
                # A cylinder, or ends of several edges: rows only where the
                # ends' samples do not face each other along the way.
                if s.kind == surface_cylinder or side.lengths[(pair + 1) % 4] > 1 or side.lengths[(pair + 3) % 4] > 1:
                    if try ends_face(face, side, (pair + 1) % 4, (pair + 3) % 4, plan, edges, translation, tolerance): continue
                let cross_length = math.min(side_length(face, side, (pair + 1) % 4, metrics), side_length(face, side, (pair + 3) % 4, metrics))
                let length = metrics[ai].at(1.0)
                let pitch = cross_length / (f64)cross_count
                if pitch <= vanishing: continue
                let existing = math.max((f64)(plan.rows[a].count - 1), (f64)(plan.rows[b].count - 1))
                let needed = math.max(existing, math.ceil(length / (pitch * row_stretch) - count_slack))
                if not math.is_finite(needed) or needed > 1024.0: continue
                let count = (usize)needed
                if (count + 1) * (cross_count + 1) > 131072: continue
                # A good existing phase retains its continuation contract.
                if not crowded(plan.rows[a]) and not crowded(plan.rows[b]) and needed <= existing: continue
                # Conservative atomic budget preflight includes every old cut.
                if cuts[ai].count + count - 1 > cuts[ai].limit or cuts[bi].count + count - 1 > cuts[bi].limit: continue
                for use in [a, b]:
                    let index = edge_index(face.uses[use])
                    let row = &plan.rows[use]
                    row.count = 0
                    for step in 0..<count + 1:
                        let t = (f64)step / (f64)count
                        _ = try row.insert(t, same_station)
                        _ = try cuts[index].insert(t, same_cut)

## Whether the row samples of sides `a` and `b` (opposite, run the other
## way round the face) pair off along `translation`: each of a's against
## b's from its far end, their difference along it within the tolerance.
func ends_face(face: BrepFace, side: PatchSides, a: usize, b: usize, plan: const GridLayout*, edges: const Curve[], translation: Vector3, tolerance: f64) -> bool!:
    var first = Growing[Vector3]()
    defer first.close()
    var second = Growing[Vector3]()
    defer second.close()
    try side_points(face, side, a, plan, edges, &first)
    try side_points(face, side, b, plan, edges, &second)
    if first.count != second.count: return false
    let way = try translation.normalized()
    for at in 0..<first.count:
        let gap = second.items[second.count - 1 - at].subtract(first.items[at])
        if gap.subtract(way.multiply_scalar(gap.dot(way))).length() > tolerance * 10.0: return false
    return true

## Side `at`'s row samples in the face's loop order onto `out` (each
## joint between its edges once).
func side_points(face: BrepFace, side: PatchSides, at: usize, plan: const GridLayout*, edges: const Curve[], out: Growing[Vector3]*) -> !:
    for segment in 0..<side.lengths[at]:
        let use = (side.starts[at] + segment) % face.uses.length
        let row = plan.rows[use]
        let edge = edges[edge_index(face.uses[use])]
        let forward = face.uses[use] > 0
        for k in 0..<row.count:
            if segment > 0 and k == 0: continue
            let value = row.values[k if forward else row.count - 1 - k]
            try out.push(try edge.at(value))

## The intervals of side `side`'s rows (over its edges).
func row_count(face: BrepFace, side: PatchSides, at: usize, plan: const GridLayout*) -> usize:
    var count: usize = 0
    for segment in 0..<side.lengths[at]: count += plan.rows[(side.starts[at] + segment) % face.uses.length].count - 1
    return count

## The length of side `side` (over its edges).
func side_length(face: BrepFace, sides: PatchSides, side: usize, metrics: const ArcMetric[]) -> f64:
    var length = 0.0
    for at in 0..<sides.lengths[side]: length += metrics[edge_index(face.uses[(sides.starts[side] + at) % face.uses.length])].at(1.0)
    return length

func crowded(row: Stations) -> bool:
    if row.count < 3: return false
    var small: f64 = 1.0
    var large: f64 = 0.0
    for at in 0..<row.count - 1:
        let gap = row.values[at + 1] - row.values[at]
        small = math.min(small, gap)
        large = math.max(large, gap)
    return large > small * 1.5

func rails(a: Curve, b: Curve, translation: Vector3, tolerance: f64) -> bool:
    let delta = a.points[1].subtract(a.points[0])
    let other = b.points[1].subtract(b.points[0])
    let length = delta.length()
    let span = translation.length()
    if length <= vanishing or span <= vanishing: return false
    # Independently fitted STEP edges can disagree within import uncertainty.
    # Use that model's tolerance for classification only, capped relative to
    # rail length so a loose File setting cannot turn oblique rails into flow.
    # Never move an endpoint, change the curve, or enlarge projection tolerance.
    # One part per million is only a layout-direction cap, not a new geometric
    # tolerance. Independently fitted rails still have to agree within the
    # File setting; clearly oblique rails remain ineligible even with a loose
    # import. Coverage below is measured on the exact support translation.
    let epsilon = math.max(length * 1e-10, math.min(tolerance, length * 1e-6))
    if delta.cross(translation).length() > span * epsilon: return false
    let same = delta.dot(other) > 0.0
    if (other if same else other.multiply_scalar(-1.0)).subtract(delta).length() > epsilon: return false
    # No slanted or shifted ends: both rails cover the same extrusion interval.
    let offset = b.points[0 if same else 1].subtract(a.points[0])
    return math.abs(offset.dot(translation)) <= span * epsilon