Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / layout / patch_density.lucb

## Surface-interior sampling requirements for one face's independent row grid.
## A curved interior with straight trims must not collapse to one planar quad.

## Raise opposite edge counts from sampled interior curvature, never lower them.
pub func patch_density(face: BrepFace, surface: Surface, edges: const Curve[], counts: usize[], metrics: const ArcMetric[], divisions: Density, tolerance: f64) -> !:
    if surface.kind != surface_spline: return
    let sides = try patch_sides(face, edges)
    if not sides.valid: return
    let boundary = try new Vector3[36] --- in memory.heap
    defer free(boundary) in memory.heap
    var seed = Vector3(0.5, 0.5, 0.0)
    for side in 0..<(usize)4:
        for at in 0..<(usize)9:
            let t = (f64)at / 8.0
            seed = try chart_parameter(surface, try side_position(face, sides, side, t, edges, counts, metrics), seed, tolerance)
            boundary[side * 9 + at] = seed
    # An interval may turn the step angle (a quarter turn over the steps):
    # each pair of its quarters by at most three quarters of it.
    let step = math.pi * 0.5 / (f64)divisions.steps
    let chord = math.max(tolerance, divisions.within(flat_sag))
    let most = (usize)(divisions.steps * 4)
    var counts_of: usize[2] = [1, 1]
    for axis in 0..<(usize)2:
        var count: usize = 1
        while count < most:
            # How many times finer the worst interval must be: its turn and
            # its sag per unit length (an arc's turn) fall as the interval
            # does, its sag against the chord limit as its square.
            var worst = 0.0
            for cross in 0..<(usize)5:
                let other = ((f64)cross + 0.5) / 5.0
                for interval in 0..<count:
                    var p = [Vector3(), Vector3(), Vector3(), Vector3(), Vector3()]
                    for sample in 0..<(usize)5:
                        p[sample] = try sample_cross(surface, boundary, ((f64)interval + (f64)sample / 4.0) / (f64)count, other, axis)
                    let span = p[4].subtract(p[0])
                    var along = 0.0
                    for sample in 0..<(usize)4: along += p[sample + 1].subtract(p[sample]).length()
                    var deflection: f64 = 0.0
                    for sample in 1..<(usize)4:
                        let t = math.clamp(p[sample].subtract(p[0]).dot(span) / span.dot(span), 0.0, 1.0) if span.dot(span) > degenerate else 0.0
                        deflection = math.max(deflection, p[sample].subtract(p[0].add(span.multiply_scalar(t))).length())
                    # Within an arc's sag turning the step (turn_sag) or the
                    # chord limit, whichever allows more.
                    let turning = along * turn_sag(divisions)
                    if deflection > math.max(chord, turning): worst = math.max(worst, math.min(deflection / turning, math.sqrt(deflection / chord)))
                    if deflection > tolerance:
                        for a in 0..<(usize)4:
                            for b in a + 1..<(usize)4:
                                let da = p[a + 1].subtract(p[a])
                                let db = p[b + 1].subtract(p[b])
                                let turn = math.acos(math.clamp(da.dot(db) / (da.length() * db.length()), -1.0, 1.0))
                                if turn > step * 0.75: worst = math.max(worst, turn / (step * 0.75))
            if worst <= 1.0: break
            # An even count, so a symmetric patch's crest (a dome's top, a
            # fillet's middle) is a sample.
            let needed = math.max((f64)(count + 1), math.ceil((f64)count * worst))
            count = (usize)math.min((f64)most, math.ceil(needed * 0.5) * 2.0)
        counts_of[axis] = count
    # Along a straight (or barely turning) direction the angle asks for no
    # rows: as many as keep the cells no more than row_stretch times
    # longer than the other direction's steps.
    var lengths: f64[2] = [0.0, 0.0]
    for axis in 0..<(usize)2:
        for cross in 0..<(usize)5:
            let other = ((f64)cross + 0.5) / 5.0
            var previous = try sample_cross(surface, boundary, 0.0, other, axis)
            var length = 0.0
            for at in 1..<(usize)17:
                let p = try sample_cross(surface, boundary, (f64)at / 16.0, other, axis)
                length += p.subtract(previous).length()
                previous = p
            lengths[axis] = math.max(lengths[axis], length)
    for axis in 0..<(usize)2:
        # The steps across as the sides already have them, if finer.
        let across = math.max((f64)counts_of[1 - axis], math.max((f64)side_count(face, sides, 1 - axis, counts), (f64)side_count(face, sides, 3 - axis, counts)))
        let spacing = lengths[1 - axis] / across
        var count = counts_of[axis]
        if spacing > 0.0: count = (usize)math.min((f64)most, math.max((f64)count, math.ceil(lengths[axis] / (spacing * row_stretch) - count_slack)))
        for side in [axis, axis + 2]:
            raise_side(face, sides, side, counts, metrics, count)

func side_position(face: BrepFace, sides: PatchSides, side: usize, fraction: f64, edges: const Curve[], counts: const usize[], metrics: const ArcMetric[]) -> Vector3!:
    var position = fraction * (f64)side_count(face, sides, side, counts)
    for segment in 0..<sides.lengths[side]:
        let use = face.uses[(sides.starts[side] + segment) % face.uses.length]
        let count = (f64)counts[edge_index(use)]
        if position <= count or segment + 1 == sides.lengths[side]:
            let t = math.clamp(position / count, 0.0, 1.0)
            return try edges[edge_index(use)].at(metrics[edge_index(use)].station(t if use > 0 else 1.0 - t))
        position -= count
    return Vector3()

func interpolate(boundary: const Vector3[], side: usize, fraction: f64) -> Vector3:
    let scaled = math.clamp(fraction, 0.0, 1.0) * 8.0
    let low = (usize)math.min(7.0, math.floor(scaled))
    let t = scaled - (f64)low
    return boundary[side * 9 + low].multiply_scalar(1.0 - t).add(boundary[side * 9 + low + 1].multiply_scalar(t))

func sample_cross(s: Surface, boundary: const Vector3[], along: f64, other: f64, axis: usize) -> Vector3!:
    let u = along if axis == 0 else other
    let v = other if axis == 0 else along
    let a = boundary[0]
    let b = boundary[8]
    let c = boundary[18]
    let d = boundary[27]
    let bilinear = a.multiply_scalar((1.0 - u) * (1.0 - v)).add(b.multiply_scalar(u * (1.0 - v))).add(c.multiply_scalar(u * v)).add(d.multiply_scalar((1.0 - u) * v))
    let uv = interpolate(boundary, 0, u).multiply_scalar(1.0 - v).add(interpolate(boundary, 2, 1.0 - u).multiply_scalar(v)).add(interpolate(boundary, 3, 1.0 - v).multiply_scalar(1.0 - u)).add(interpolate(boundary, 1, v).multiply_scalar(u)).subtract(bilinear)
    return try surface_point(s, uv)