Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / checks.luc

## Shared assertions for the CAD regressions.
from luce_geocore.geocore import Mesh, Vector3
from luce_std import math

pub func near(a: float, b: float):
    if math.abs(a - b) >= 0.000001: print(f"near failed: actual={a}, expected={b}")
    assert(math.abs(a - b) < 0.000001)

## Every edge is used by exactly two faces in opposite directions, and every
## display triangle faces the same way as its corner normals.
pub func manifold(mesh: Mesh) -> unit!:
    closed(mesh)
    for triangle in 0..<mesh.index_count() // 3:
        let a = mesh.vertex(mesh.index(triangle * 3))
        let b = mesh.vertex(mesh.index(triangle * 3 + 1))
        let c = mesh.vertex(mesh.index(triangle * 3 + 2))
        if b.position.subtract(a.position).cross(c.position.subtract(a.position)).dot(a.normal) <= 0.0:
            print(f"invalid display triangle {triangle}: {a.position.x},{a.position.y},{a.position.z}; {b.position.x},{b.position.y},{b.position.z}; {c.position.x},{c.position.y},{c.position.z}; normal {a.normal.x},{a.normal.y},{a.normal.z}")
            assert(false)

## Every edge is used by exactly two faces in opposite directions.
pub func closed(mesh: Mesh) -> unit!:
    let counts: list[int] = []
    let directions: list[int] = []
    for edge in 0..<mesh.edge_count():
        counts.append(0)
        directions.append(0)
    for face in 0..<mesh.face_count():
        for at in 0..<mesh.face_size(face):
            let edge = mesh.face_edge(face,at)
            let a = mesh.edge_point(edge,0)
            let b = mesh.edge_point(edge,1)
            let p = mesh.face_point(face,at)
            let q = mesh.face_point(face,(at+1)%mesh.face_size(face))
            assert((p == a and q == b) or (p == b and q == a))
            counts[edge] += 1
            directions[edge] += 1 if p == a else -1
    for edge in 0..<mesh.edge_count(): assert(counts[edge] == 2 and directions[edge] == 0)

## The volume a closed triangle mesh encloses (signed: outward faces
## positive), by the divergence theorem.
## Which of `mesh`'s points lie on a CAD face's trim: on an edge used once
## or by two different CAD faces.
pub func trim_points(mesh: Mesh) -> list[bool]!:
    let parent = mesh.find_attribute("cad_face", 2)
    let owner: list[float] = []
    let shared: list[bool] = []
    for edge in 0..<mesh.edge_count():
        owner.append(-1.0)
        shared.append(false)
    let uses: list[int] = []
    for edge in 0..<mesh.edge_count(): uses.append(0)
    for face in 0..<mesh.face_count():
        let cad = mesh.attribute_value(parent, face, 0)
        for at in 0..<mesh.face_size(face):
            let edge = mesh.face_edge(face, at)
            uses[edge] += 1
            if owner[edge] >= 0.0 and owner[edge] != cad: shared[edge] = true
            owner[edge] = cad
    let result: list[bool] = []
    for point in 0..<mesh.point_count(): result.append(false)
    for edge in 0..<mesh.edge_count():
        if uses[edge] == 1 or shared[edge]:
            result[mesh.edge_point(edge, 0)] = true
            result[mesh.edge_point(edge, 1)] = true
    return result

## Whether the side a b runs along a grid line through `a` (x or y fixed,
## or along z with x and y fixed, as `vertical` picks), or, from a trim
## point (`trim`), within 45 degrees of one: the trim point may stand for a
## grid line's crossing a hair from it (absorb_crossing), and the side be
## short where a line passes close to the trim.
pub func along_grid(a: Vector3, b: Vector3, trim: bool, vertical: bool) -> bool:
    let first = math.abs(a.z - b.z) if vertical else math.abs(a.x - b.x)
    let second = math.abs(a.x - b.x) + math.abs(a.y - b.y) if vertical else math.abs(a.y - b.y)
    if first < 0.000001 or second < 0.000001: return true
    return trim and math.min(first, second) < math.max(first, second)

pub func volume(mesh: Mesh) -> float!:
    var total = 0.0
    for triangle in 0..<mesh.index_count() // 3:
        let a = mesh.vertex(mesh.index(triangle * 3)).position
        let b = mesh.vertex(mesh.index(triangle * 3 + 1)).position
        let c = mesh.vertex(mesh.index(triangle * 3 + 2)).position
        total += a.dot(b.cross(c)) / 6.0
    return total

## One CAD face's display triangles: how many, the largest angle (in
## degrees), the largest aspect (the longest side over the height onto it)
## and the slivers (aspect over 10, or an angle over 150 degrees).
pub struct FaceQuality:
    pub var triangles: int
    pub var angle: float
    pub var aspect: float
    pub var slivers: int

## The quality of each CAD face's triangles in `mesh` (by `cad_face`).
pub func face_quality(mesh: Mesh, faces: int) -> list[FaceQuality]!:
    let parent = mesh.find_attribute("cad_face", 2)
    let result: list[FaceQuality] = []
    for face in 0..<faces: result.append(FaceQuality(0, 0.0, 0.0, 0))
    for triangle in 0..<mesh.index_count() // 3:
        let face = int(mesh.attribute_value(parent, mesh.triangle_face(triangle), 0))
        let a = mesh.vertex(mesh.index(triangle * 3)).position
        let b = mesh.vertex(mesh.index(triangle * 3 + 1)).position
        let c = mesh.vertex(mesh.index(triangle * 3 + 2)).position
        let ab = b.subtract(a).length()
        let bc = c.subtract(b).length()
        let ca = a.subtract(c).length()
        let longest = math.max(ab, math.max(bc, ca))
        let area = b.subtract(a).cross(c.subtract(a)).length()
        let aspect = longest * longest / math.max(area, 1e-300)
        # The largest angle faces the longest side.
        var cosine = 1.0
        if longest == ab: cosine = a.subtract(c).dot(b.subtract(c)) / math.max(ca * bc, 1e-300)
        elif longest == bc: cosine = b.subtract(a).dot(c.subtract(a)) / math.max(ab * ca, 1e-300)
        else: cosine = a.subtract(b).dot(c.subtract(b)) / math.max(ab * bc, 1e-300)
        let angle = math.acos(math.clamp(cosine, -1.0, 1.0)) * 180.0 / math.pi
        result[face].triangles += 1
        result[face].angle = math.max(result[face].angle, angle)
        result[face].aspect = math.max(result[face].aspect, aspect)
        if aspect > 10.0 or angle > 150.0: result[face].slivers += 1
    return result