Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.2

0.9.2 / tests / regressions / parallel_cad_tests.luc

from luce_cad.model import BrepModel, CadModel
from luce_geocore.geocore import Vector3, Mesh

pub func parallel_cad_tests() -> unit!:
    let boundary = BrepModel()
    let normal = Vector3(0.0, 0.0, 1.0)
    let axis = Vector3(1.0, 0.0, 0.0)
    for face in 0..<20:
        let x = float(face) * 3.0
        for point in [Vector3(x, 0.0, 0.0), Vector3(x + 2.0, 0.0, 0.0), Vector3(x + 2.0, 2.0, 0.0), Vector3(x, 2.0, 0.0)]:
            _ = boundary.add_vertex(point)
        for edge in 0..<4:
            _ = boundary.add_line(face * 4 + edge, face * 4 + (edge + 1) % 4)
        let surface = boundary.add_surface(0, Vector3(), normal, axis)
        _ = boundary.add_face(surface, [face * 4 + 1, face * 4 + 2, face * 4 + 3, face * 4 + 4], [4])
    let first = boundary.mesh(4)
    let second = boundary.mesh(4)
    assert(first.face_count() == 20 and second.point_count() == first.point_count())
    for face in 0..<first.face_count():
        for corner in 0..<4:
            assert(first.face_point(face, corner) == second.face_point(face, corner))
    for index in 0..<first.point_count():
        assert(first.point(index).subtract(second.point(index)).length() == 0.0)
    for selected in [3, 19]:
        let planned = boundary.planned_face(selected, 4)
        assert(planned.face_count() == 1 and planned.point_count() == 4)
        let provenance = planned.find_attribute("cad_face", 2)
        assert(provenance >= 0 and planned.attribute_value(provenance, 0, 0) == float(selected))
        for corner in 0..<4:
            assert(planned.point(planned.face_point(0, corner)).subtract(first.point(first.face_point(selected, corner))).length() == 0.0)
    var rejected = false
    _ = boundary.planned_face(20, 4) catch failure:
        rejected = true
    assert(rejected)
    preview_matches(boundary)
    print("PASS deterministic native parallel face meshing and pooled previews")

## The pooled preview holds exactly each face's preview_face mesh, in face
## order, batched; every shared edge outline once; one guide per face.
func preview_matches(boundary: BrepModel) -> unit!:
    let model = CadModel()
    model.attach_brep(boundary)
    let preview = model.preview()
    assert(preview.failed_faces() == 0 and preview.first_error() == "")
    assert(preview.guide_count() == 2 * model.face_count())
    assert(preview.boundary_count() == 2 * model.edge_count())
    var face = 0
    for index in 0..<preview.mesh_count():
        let batch = preview.mesh(index)
        var point_base = 0
        var polygon = 0
        while polygon < batch.face_count():
            let part = model.preview_face(face, 16)
            assert(batch.find_attribute("N", 1) >= 0)
            for local in 0..<part.face_count():
                assert(batch.face_size(polygon + local) == part.face_size(local))
                for corner in 0..<part.face_size(local):
                    let a = batch.point(batch.face_point(polygon + local, corner))
                    let b = part.point(part.face_point(local, corner))
                    assert(a.subtract(b).length() == 0.0)
                    assert(batch.face_point(polygon + local, corner) == point_base + part.face_point(local, corner))
            point_base += part.point_count()
            polygon += part.face_count()
            face += 1
    assert(face == model.face_count())

## Compare with extraction AFTER a full cook: independent previews cannot
## prove that shared stations, stopped trims and display diagonals survived.
pub func planned_matches(model: BrepModel, whole: Mesh, selected: int, divisions: int, spacing: float) -> unit!:
    let planned = model.planned_face(selected, divisions, spacing)
    let parent = whole.find_attribute("cad_face", 2)
    let normals = whole.find_attribute("N", 1)
    let planned_normals = planned.find_attribute("N", 1)
    assert(normals >= 0 and planned_normals >= 0)
    let remove: list[int] = []
    let kept: list[int] = []
    for face in 0..<whole.face_count():
        if int(whole.attribute_value(parent, face, 0)) != selected: remove.append(face)
        else: kept.append(face)
    let reference = whole.without_faces(remove)
    assert(planned.point_count() == reference.point_count())
    assert(planned.face_count() == reference.face_count() and planned.index_count() == reference.index_count())
    for face in 0..<planned.face_count():
        assert(planned.face_size(face) == reference.face_size(face))
        for corner in 0..<planned.face_size(face):
            assert(planned.point(planned.face_point(face, corner)).subtract(reference.point(reference.face_point(face, corner))).length() == 0.0)
            for component in 0..<3:
                assert(planned.attribute_value(planned_normals,planned.face_offset(face)+corner,component) == whole.attribute_value(normals,whole.face_offset(kept[face])+corner,component))
        for name in ["cad_face", "cad_flow_island", "cad_mesher", "cad_trim_grid"]:
            assert(planned.attribute_value(planned.find_attribute(name, 2), face, 0) == reference.attribute_value(reference.find_attribute(name, 2), face, 0))
    for index in 0..<planned.index_count():
        let a = planned.vertex(planned.index(index))
        let b = reference.vertex(reference.index(index))
        assert(a.position.subtract(b.position).length() == 0.0)
        let id = planned.index(index)
        let face = planned.corner_face(id)
        let original = whole.vertex(whole.face_offset(kept[face])+id-planned.face_offset(face))
        assert(a.normal.subtract(original.normal).length() == 0.0)