Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / meshing / clipped_slivers.lucb

## Optional cut-cell cleanup. An almost coincident grid/trim line can leave a
## sub-tolerance ribbon. Merge the interior side, never the canonical CAD trim.

## Coalesce only sub-tolerance, boundary-adjacent slivers with a safe larger
## neighbor. Original point IDs/positions survive; the caller still validates
## lifted display triangles. Work stops after 128 optional dissolves.
## Curved charts provide lifted positions: UV width is not a physical length.
pub func coalesce(source: interop.Reference[Mesh], boundary: usize, tolerance: f64, positions: const Vector3[]? = none) -> interop.Reference[Mesh]!:
    if let points = positions:
        if points.length != (usize)source.value().point_count(): error(invalid, "cut-cell metric must cover every chart point")
    # Keep the no-edit path cheap. Only allocate local topology after the first
    # accepted candidate; subsequent edits never rebuild an entire patch mesh.
    var first: i64 = -1
    for face in 0..<source.value().face_count():
        if not (try sliver(source.value(), face, boundary, tolerance, positions)): continue
        first = try merge_edge(source.value(), face, positions)
        if first >= 0: break
    if first < 0: return source.clone()
    var mesh = try DissolveWorkspace(source.value(), 128)
    defer mesh.close()
    try mesh.dissolve(first)
    # One entry per side: merges stay within 256 corners (choose_edge), but a
    # source face may be larger.
    var largest: i64 = 256
    for face in 0..<source.value().face_count():
        let size = try source.value().face_size(face)
        if size > largest: largest = size
    let neighbors = try new i64[(usize)largest] --- in memory.heap
    defer free(neighbors) in memory.heap
    for pass in 1..<128:
        var edge: i64 = -1
        for face in 0..<mesh.face_slots():
            if not mesh.active(face) or not (try sliver(&mesh, face, boundary, tolerance, positions)): continue
            let count = (usize)(try mesh.face_size(face))
            for side in 0..<count: neighbors[side] = try mesh.neighbor(face, (i64)side)
            edge = try choose_edge(&mesh, face, neighbors[..<count], positions)
            if edge >= 0: break
        if edge < 0: break
        try mesh.dissolve(edge)
    return try mesh.finish()

func sliver[M: PolygonTopology](mesh: const M*, face: i64, boundary: usize, tolerance: f64, positions: const Vector3[]?) -> bool!:
    let count = try mesh.face_size(face)
    let origin = try point_position(mesh, try mesh.face_point(face, 0), positions)
    var area = Vector3()
    var longest: f64 = 0.0
    var rail_origin = Vector3()
    var rail = Vector3()
    var boundary_points: usize = 0
    for at in 0..<count:
        let id = try mesh.face_point(face, at)
        if (usize)id < boundary: boundary_points += 1
        let a = (try point_position(mesh, id, positions)).subtract(origin)
        let b = (try point_position(mesh, try mesh.face_point(face, (at + 1) % count), positions)).subtract(origin)
        area = area.add(a.cross(b))
        let length = b.subtract(a).length()
        if length > longest:
            longest = length
            rail_origin = a
            rail = b.subtract(a)
    if boundary_points < 2 or longest <= 0.0: return false
    let measure = area.length() * 0.5
    if measure <= longest * tolerance * 0.05: return true
    # A severely skewed triangle/quad narrower than import uncertainty can
    # also terminate as a trim polygon. Do not erase healthy narrow quad rows
    # or judge n-gons by their deliberately collinear boundary corners.
    if count > 4 or measure > longest * tolerance or longest * longest <= measure * 40.0: return false
    # Area/length is only a quick estimate; every lifted corner must actually
    # lie in a tolerance-width strip about the longest side, including on a
    # warped chart whose area-vector components can cancel.
    let direction = rail.multiply_scalar(1.0 / longest)
    for at in 0..<count:
        let p = (try point_position(mesh, try mesh.face_point(face, at), positions)).subtract(origin).subtract(rail_origin)
        if p.cross(direction).length() > tolerance: return false
    for at in 0..<count:
        let p = try point_position(mesh, try mesh.face_point(face, at), positions)
        let a = (try point_position(mesh, try mesh.face_point(face, (at + count - 1) % count), positions)).subtract(p)
        let b = (try point_position(mesh, try mesh.face_point(face, (at + 1) % count), positions)).subtract(p)
        let product = a.length() * b.length()
        if product > 0.0 and a.cross(b).length() < product * 0.1: return true
    return false

func point_position[M: PolygonTopology](mesh: const M*, id: i64, positions: const Vector3[]?) -> Vector3!:
    if let points = positions: return points[(usize)id]
    return try mesh.point(id)