Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.1

0.9.1 / tests / brep_cache_tests.luc

## The B-rep's columns and caches: the cv domain, O(1) copies with moved
## control vertices, the kept tessellation a Tessellate node places, the
## per-face cache that re-meshes only what a move changed, the overlay, and
## the geometry-file codec, and control vertices edited as a point cloud.
from luce_cad.model import BrepModel, CadModel
from luce_cad.cad_geometry import CadGeometry
from luce_geocore.geocore import Vector3, GeometryFile, Verbs
from checks import near, manifold
from luce_geocore.geocore import Mesh

## A closed solid: a raised B-spline dome (its border rows flat, so its
## lines stay on it) and a planar cap under it.
func solid() -> CadModel!:
    let b = BrepModel()
    let knots = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]
    let points: list[Vector3] = []
    for u in 0..<3:
        for v in 0..<3:
            points.append(Vector3(float(u), float(v), 4.0 if u == 1 and v == 1 else 0.0))
    for p in [points[0], points[6], points[8], points[2]]: _ = b.add_vertex(p)
    for edge in 0..<4: _ = b.add_line(edge, (edge + 1) % 4)
    let dome = b.add_spline_surface(points, [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], 3, 3, 2, 2, knots, knots)
    _ = b.add_face(dome, [1, 2, 3, 4], [4])
    let plane = b.add_surface(0, Vector3(), Vector3(0.0, 0.0, 1.0), Vector3(1.0, 0.0, 0.0))
    _ = b.add_face(plane, [-4, -3, -2, -1], [4], false)
    let model = CadModel()
    model.attach_brep(b)
    return model

pub func brep_cache_tests() -> unit!:
    let model = solid()
    assert(model.cv_count() == 9)
    near(model.cv_position(4).z, 4.0)
    let first = model.tessellate(8)
    manifold(first)
    # The same settings give the kept mesh: the same columns.
    let again = model.tessellate(8)
    assert(again.points_id() == first.points_id())
    # A Tessellate node places the kept mesh (curvature on: as the viewport shows it).
    let shown = model.tessellate(8, 0.0, none, model.supports_curvature())
    let placed = CadGeometry.tessellated(CadGeometry.of_model(model), 8, 0.0)
    assert((placed.mesh() else trap("no mesh")).points_id() == shown.points_id())
    # Moving the dome's middle cv: a copy; the original keeps its net.
    let moved = model.with_cvs_moved([4], Vector3(0.0, 0.0, 1.0))
    near(model.cv_position(4).z, 4.0)
    near(moved.cv_position(4).z, 5.0)
    _ = model.reused_faces()
    let raised = moved.tessellate(8)
    manifold(raised)
    assert(raised.bounds().maximum.z > first.bounds().maximum.z + 0.1)
    # Only the dome was meshed again: the cap came from the cache.
    assert(moved.reused_faces() == 1)
    # The overlay: 4 straight edges, the dome's 3x3 net (12 lines), 2 guides.
    let lines = model.overlay(first)
    assert(lines.boundary_count() == 8 and lines.hull_count() == 24 and lines.guide_count() == 4)
    near(lines.hull_point(0).z, 0.0)
    # A geometry file gives back the same B-rep: control vertices bit for
    # bit, the same tessellation.
    _ = CadGeometry.register_codec()
    _ = GeometryFile.save(CadGeometry.of_model(moved), "brep_codec.prism")
    let loaded = GeometryFile.load("brep_codec.prism")
    assert(CadGeometry.model_count(loaded) == 1)
    let back = CadGeometry.model(loaded, 0)
    assert(back.face_count() == moved.face_count() and back.cv_count() == moved.cv_count())
    for cv in 0..<moved.cv_count():
        let p = moved.cv_position(cv)
        let q = back.cv_position(cv)
        assert(p.x == q.x and p.y == q.y and p.z == q.z)
    assert(back.tessellate(8).topology_hash() == raised.topology_hash())
    cv_cloud_edits(model, first)
    print("PASS B-rep columns: cv domain, O(1) moved copies, kept tessellation placed by Tessellate, per-face cache reuse, overlay, file codec and cv cloud edits")

## The cvs as points: a point verb moves some, and only those go back. A
## boundary cv pulls the dome's trims off it: the tessellation still
## succeeds (re-projected trims), and a fold leaves the face out, counted.
func cv_cloud_edits(model: CadModel, first: Mesh) -> unit!:
    let parts = CadGeometry.of_model(model)
    let cloud = CadGeometry.cv_cloud(parts)
    assert(cloud.point_count() == 9 and cloud.face_count() == 0 and CadGeometry.is_cv_cloud(cloud) and not CadGeometry.is_cv_cloud(first))
    near(cloud.point(4).z, 4.0)
    let moved = Verbs.run("Transform Components", cloud, "4", 1, [0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0]).mesh()
    let edited = CadGeometry.model(CadGeometry.with_cv_cloud(parts, cloud, moved), 0)
    near(edited.cv_position(4).z, 3.0)
    for cv in [0, 1, 2, 3, 5, 6, 7, 8]:
        let a = model.cv_position(cv)
        let b = edited.cv_position(cv)
        assert(a.x == b.x and a.y == b.y and a.z == b.z)
    let lowered = edited.tessellate(8)
    manifold(lowered)
    assert(CadGeometry.dropped_faces(lowered) == 0)
    # A drag's quick preview: the kept mesh with the dome's own points on
    # the moved surface, the shared points (edges) where they were.
    let quick = model.with_cvs_moved([4], Vector3(0.0, 0.0, 1.0)).tessellate(8, 0.0, none, false, false, true)
    manifold(quick)
    assert(quick.face_count() == first.face_count() and quick.point_count() == first.point_count())
    var higher = 0
    for point in 0..<first.point_count():
        let before = first.point(point)
        let after = quick.point(point)
        if after.z > before.z + 0.000001: higher += 1
        if before.z == 0.0 and (before.x == 0.0 or before.x == 2.0 or before.y == 0.0 or before.y == 2.0): assert(after.z == before.z)
    assert(higher > 0)
    # A corner cv raised: the dome's edges no longer lie on it.
    let raised = edited.with_cvs_moved([0], Vector3(0.0, 0.0, 1.5))
    let pulled = raised.tessellate(8)
    assert(CadGeometry.dropped_faces(pulled) == 0)
    manifold(pulled)
    # Folded far over the cap: the dome is left out, the model still meshes.
    let folded = edited.with_cvs_moved([0, 1, 3], Vector3(3.0, 3.0, -6.0)).tessellate(8)
    assert(folded.face_count() > 0 and CadGeometry.dropped_faces(folded) == 1)