Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.1

0.9.1 / src / tests / face_jobs_contract.lucb

## Face jobs leave out the faces no mesher follows, noting each in source
## order, and mesh the rest. Every face failing fails the model with the first
## in source order. A failure that is not the face's own (running out of
## memory) stops new work but joins earlier in-flight faces, and the first
## such face in source order is reported whatever the completion order.
import memory
import sync
import thread
from luce_geocore.geocore import Vector3, MeshBuilder, invalid
from brep_types import BrepFace, Surface, Curve, Samples
from layout_types import GridLayout
from face_jobs import mesh_faces, job_threads, FaceNotes
from luce_geocore.core import parallel

var lock: sync.Mutex
var entered: usize = 0
var visits: usize[64]
## Faces in flight together: four, or fewer on a small pool (CI runners).
var wide: usize = 4

pub func face_jobs_contract() -> !:
    var empty: f64[]
    var points: Vector3[]
    var edges: Curve[]
    var samples: Samples[]
    let surface = try Surface(0, Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 0.0, 1.0), 0.0, 0.0, points, empty, empty, empty, 0, 0, 0, 0)
    defer surface.close()
    let surfaces = [surface]
    let faces = try new BrepFace[64] --- in memory.heap
    defer free(faces) in memory.heap
    var created: usize = 0
    defer close_faces(faces[..<created])
    let plans = try new GridLayout[64] --- in memory.heap
    defer free(plans) in memory.heap
    for at in 0..<faces.length:
        faces[at] = try BrepFace(0, [(i64)at], [1], true)
        created += 1
        plans[at] = GridLayout()
    # Faces 1 and 5 fail; the other 62 each mesh one triangle.
    for repeat in 0..<4:
        var builder = try MeshBuilder(precise = true)
        defer builder.close()
        var notes = FaceNotes()
        defer notes.close()
        let result = try mesh_faces(&builder, edges, surfaces, faces, samples, plans, 4, 1e-6, 0.0, none, skip_some, &notes)
        defer free(result) in memory.heap
        if builder.nf != 62 or notes.skipped != 2 or notes.dropped != 0: error(invalid, "face jobs did not leave out exactly the failed faces")
        if (str)notes.lines.view() != "Skipped CAD face 1: bad face\nSkipped CAD face 5: bad face\n": error(invalid, "face jobs lost source-order skip notes")
    # Every face failing fails the model with the first.
    var builder = try MeshBuilder(precise = true)
    defer builder.close()
    var notes = FaceNotes()
    defer notes.close()
    var every = ""
    let none_meshed = mesh_faces(&builder, edges, surfaces, faces, samples, plans, 4, 1e-6, 0.0, none, fail_all, &notes) catch failure:
        every = failure.message
        recover empty
    defer free(none_meshed) in memory.heap
    if every != "CAD face 0: bad face": error(invalid, "face jobs accepted a model whose every face failed")
    wide = job_threads() if job_threads() < 4 else 4
    for repeat in 0..<16:
        entered = 0
        for at in 0..<visits.length: visits[at] = 0
        var stopped = try MeshBuilder(precise = true)
        defer stopped.close()
        var stop_notes = FaceNotes()
        defer stop_notes.close()
        var failed = false
        let result = mesh_faces(&stopped, edges, surfaces, faces, samples, plans, 4, 1e-6, 0.0, none, fail_ordered, &stop_notes) catch failure:
            if failure.code != memory.exhausted or failure.message != "CAD face 1: earlier failure": error(invalid, "face jobs lost source-order failure reporting")
            failed = true
            recover empty
        defer free(result) in memory.heap
        if not failed: error(invalid, "face jobs accepted a model that ran out of memory")
        # Every pool worker may hold a face when the first failure lands; each
        # finishing worker may take at most a few more before it sees the stop.
        if entered < 2 or entered > parallel.width() + 3: error(invalid, "face jobs kept scheduling after failure")
        for at in 0..<wide:
            if visits[at] != 1: error(invalid, "face jobs did not join the earlier in-flight work")
        if stopped.nf != 0 or stopped.nc != 0 or stopped.np != 0: error(invalid, "failed face jobs assembled a partial result")

## One triangle on the plane, or a failure for faces 1 and 5.
func skip_some(mesh: MeshBuilder*, face: BrepFace, surface: Surface, edges: const Curve[], samples: const Samples[], plan: GridLayout, divisions: i64, tolerance: f64, edge_size: f64) -> !:
    let index = (usize)face.uses[0]
    if index == 1 or index == 5: error(invalid, "bad face")
    let x = (f64)index * 2.0
    let a = try mesh.point(Vector3(x, 0.0, 0.0))
    let b = try mesh.point(Vector3(x + 1.0, 0.0, 0.0))
    let c = try mesh.point(Vector3(x, 1.0, 0.0))
    try mesh.face(3)
    for id in [a, b, c]: try mesh.corner(id)

func fail_all(mesh: MeshBuilder*, face: BrepFace, surface: Surface, edges: const Curve[], samples: const Samples[], plan: GridLayout, divisions: i64, tolerance: f64, edge_size: f64) -> !:
    error(invalid, "bad face")

func fail_ordered(mesh: MeshBuilder*, face: BrepFace, surface: Surface, edges: const Curve[], samples: const Samples[], plan: GridLayout, divisions: i64, tolerance: f64, edge_size: f64) -> !:
    let index = (usize)face.uses[0]
    lock.lock()
    entered += 1
    visits[index] += 1
    lock.unlock()
    # Synchronize the first callbacks so both failing faces are in flight.
    while true:
        lock.lock()
        let ready = entered >= wide
        lock.unlock()
        if ready: break
        thread.sleep(1)
    if index == 1:
        thread.sleep(5)
        error(memory.exhausted, "earlier failure")
    if index >= 3: error(memory.exhausted, "later failure")

func close_faces(faces: const BrepFace[]):
    for face in faces: face.close()