Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / src / layout / rows.lucb

## Face-owned interior rows, distinct from the shared boundary discretization.

## Establish each patch's own curvature requirement before sharing any rows.
## Hard seams share cuts later, but cannot inflate a neighbor's interior grid.
pub func initialize_rows(edges: const Curve[], surfaces: const Surface[], faces: const BrepFace[], sides: const PatchSides[], baseline: const usize[], seams: const SeamFlow[], metrics: const ArcMetric[], plans: GridLayout[], divisions: i64, tolerance: f64) -> !:
    # Each face resets its own edges' counts from the baseline and writes only
    # its own plan, so faces run on the pool with private count scratch.
    var errors = try FaceErrors(faces.length)
    defer errors.close()
    var job = RowJob(edges = edges, surfaces = surfaces, faces = faces, sides = sides, baseline = baseline, seams = seams, metrics = metrics, plans = plans, divisions = divisions, tolerance = tolerance, errors = &errors)
    try parallel.for_each(faces.length, (void*)&job, row_item)
    try errors.raise()

struct RowJob:
    let edges: const Curve[]
    let surfaces: const Surface[]
    let faces: const BrepFace[]
    let sides: const PatchSides[]
    let baseline: const usize[]
    let seams: const SeamFlow[]
    let metrics: const ArcMetric[]
    let plans: GridLayout[]
    let divisions: i64
    let tolerance: f64
    let errors: FaceErrors*

func row_item(raw: void*, at: usize, worker: usize):
    let job = (RowJob*)raw
    let counts = new usize[job.edges.length] --- in memory.heap catch failure:
        job.errors.record(at, failure.code, failure.message)
        return
    defer free(counts) in memory.heap
    face_rows(job, at, counts) catch failure:
        job.errors.record(at, failure.code, failure.message)

func face_rows(job: RowJob*, at: usize, counts: usize[]) -> !:
    let face = job.faces[at]
    let plan = &job.plans[at]
    let side = job.sides[at]
    let metrics = job.metrics
    plan.flow = try new bool[face.uses.length] --- in memory.heap
    for use in 0..<face.uses.length: plan.flow[use] = job.seams[edge_index(face.uses[use])].aligned
    for use in face.uses:
        let edge = edge_index(use)
        counts[edge] = job.baseline[edge]
    patch_density(face, job.surfaces[(usize)face.surface], job.edges, counts, metrics, job.divisions, job.tolerance) catch failure:
        if failure.code != invalid: error(failure.code, failure.message)
        recover ()
    if side.valid:
        for pair in 0..<(usize)2:
            let a = side_count(face, side, pair, counts)
            let b = side_count(face, side, pair + 2, counts)
            if a == b: continue
            let axis = pair if a < b else pair + 2
            raise_side(face, side, axis, counts, metrics, b if a < b else a)
    plan.method = try new i64[1] --- in memory.heap
    plan.method[0] = 0
    plan.rows = try new Stations[face.uses.length] --- in memory.heap
    for use in 0..<plan.rows.length: plan.rows[use] = Stations()
    plan.row_ids = try new RowIds[face.uses.length] --- in memory.heap
    for use in 0..<plan.row_ids.length: plan.row_ids[use] = RowIds()
    for use in 0..<face.uses.length:
        let index = edge_index(face.uses[use])
        try plan.rows[use].open(edge_stations)
        for step in 0..<counts[index] + 1:
            _ = try plan.rows[use].insert(metrics[index].station((f64)step / (f64)counts[index]), 1e-12)

## Freeze local row parameters as canonical shared point IDs, never new points.
pub func bind(faces: const BrepFace[], cuts: const Stations[], samples: const Samples[], plans: GridLayout[]) -> !:
    for face in 0..<faces.length:
        for use in 0..<faces[face].uses.length:
            let index = edge_index(faces[face].uses[use])
            let row = plans[face].rows[use]
            let ids = try new i64[row.count] --- in memory.heap
            plans[face].row_ids[use].values = ids
            var station: usize = 0
            for at in 0..<row.count:
                while station + 1 < cuts[index].count and cuts[index].values[station] < row.values[at] - 1e-9: station += 1
                if math.abs(cuts[index].values[station] - row.values[at]) > 1e-9:
                    error(invalid, "shared boundary lost a face row station")
                ids[at] = samples[index].ids[station]