Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.1

0.9.1 / src / patch_validation.lucb

## Validate precisely the polygons a face job will return, before committing it.
import memory
from luce_geocore.geocore import MeshBuilder, Matrix4, invalid
from brep_types import Surface, BrepFace
from surface_normals import face_normals

## Shared global points are deliberately not copied: only this patch's corners
## participate in triangulation. A rejected strategy can safely rewind scratch.
pub func validate(mesh: MeshBuilder*, first_face: usize, first_corner: usize) -> !:
    var normals: f64[]
    try check(mesh,first_face,first_corner,normals)

func check(mesh: MeshBuilder*, first_face: usize, first_corner: usize, normals: const f64[]) -> !:
    var checked = try MeshBuilder(precise = true)
    defer checked.close()
    var corner = first_corner
    for size in mesh.sizes[first_face..<mesh.nf]:
        try checked.face(size)
        for at in 0..<size:
            let id = try checked.point(mesh.points[(usize)mesh.corners[corner]])
            try checked.corner(id)
            corner += 1
    let result = try checked.finish()
    defer result.release()
    if normals.length == 0: return
    let geometry = result.value()
    for triangle in 0..<(usize)geometry.index_count()//3:
        let ia = geometry.index((i64)(triangle*3))
        defer ia.release()
        let ib = geometry.index((i64)(triangle*3+1))
        defer ib.release()
        let ic = geometry.index((i64)(triangle*3+2))
        defer ic.release()
        let a = (usize)(try ia.get())
        let b = (usize)(try ib.get())
        let c = (usize)(try ic.get())
        let cross = checked.points[b].subtract(checked.points[a]).cross(checked.points[c].subtract(checked.points[a]))
        for id in [a,b,c]:
            if cross.x*normals[id*3]+cross.y*normals[id*3+1]+cross.z*normals[id*3+2] <= cross.length()*1e-10:
                error(invalid,"conformed boundary polygon has a folded support triangle")

## A curved clipped n-gon must also triangulate with its support orientation.
## Pure geometric polygon validity alone can accept a perpendicular sliver.
pub func validate_conformed(mesh: MeshBuilder*, first_face: usize, first_corner: usize, face: BrepFace, surface: Surface, tolerance: f64) -> !:
    var unplaced = face
    # Test in the support's local frame; assembly placement happens afterwards.
    unplaced.placement = Matrix4()
    let normals = try face_normals(mesh,first_corner,mesh.nc-first_corner,unplaced,surface,tolerance)
    defer free(normals) in memory.heap
    try check(mesh,first_face,first_corner,normals)