Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / tests / cache_node_tests.luc

## The Cache node: writes its input to a .prism file, and with Load from
## disk reads it back without cooking upstream; its stamp follows the file,
## so downstream results survive the switch. The file is a prism crate that
## luce-prism opens as a document.
from network import Network
from stamps import stamp_graph
from luce_geocore.geocore import GeometryFile
from luce_cad.cad_geometry import CadGeometry
from luce_std import files, paths
from luce_prism.prism import Document
import node_catalog

pub func cache_node_tests() -> unit!:
    assert("Cache" in node_catalog.kinds() and node_catalog.category("Cache") == "File I/O")
    let graph = Network(false)
    let sphere = graph.add("Sphere", 0.0, 0.0)
    let cache = graph.add("Cache", 0.0, 60.0)
    cache.texts[0] = "cache.prism"
    let moved = graph.add("Transform", 0.0, 120.0)
    graph.connect(sphere.id, cache.id, 0)
    graph.connect(cache.id, moved.id, 0)
    stamp_graph(graph)
    let written = graph.evaluate(moved.id)
    assert(GeometryFile.key("cache.prism") == cache.stamp and cache.stamp != 0)
    let downstream = moved.stamp
    # Load from disk: the same stamps, so every downstream result is kept.
    cache.values[0] = 1.0
    stamp_graph(graph)
    assert(cache.stamp == GeometryFile.key("cache.prism") and moved.stamp == downstream)
    # Upstream edits stop at the cache while it reads.
    sphere.values[0] = 3.0
    stamp_graph(graph)
    assert(moved.stamp == downstream)
    for node in graph.nodes:
        node.dirty = true
    let loaded = graph.evaluate(moved.id)
    assert(loaded.point_count() == written.point_count() and loaded.bounds().maximum.x == written.bounds().maximum.x)
    # Writing again after the upstream edit gives a new stamp and file.
    cache.values[0] = 0.0
    stamp_graph(graph)
    assert(moved.stamp != downstream)
    for node in graph.nodes:
        node.dirty = true
    let bigger = graph.evaluate(moved.id)
    assert(bigger.bounds().maximum.x > written.bounds().maximum.x and GeometryFile.key("cache.prism") == cache.stamp)
    # luce-prism reads the crate: the mesh element and its point column.
    let document = Document.load("cache.prism")
    let points = document.get("/geometry/mesh", "P")
    assert(points.rank() == 2 and points.count() == bigger.point_count() * 3)
    let key = document.get("/geometry", "key")
    assert(key.rank() == 0 and key.number_at() == float(cache.stamp))
    cad_round_trip("tests/fixtures/assembly.step", "cad_cache.prism")
    # The camera model, when this machine has it (the bench's default file).
    let camera = paths.join(files.home_directory(), "Desktop/camera.step")
    let kind = files.metadata(camera).kind catch failure:
        return
    cad_round_trip(camera, "camera_cache.prism")

## File (STEP) -> Cache writes the analytic models; switched to reading, the
## Cache node gives back the same models (control vertices bit for bit, the
## same tessellation) without cooking upstream: the File node now names a
## missing file.
func cad_round_trip(step: str, file: str) -> unit!:
    let graph = Network(false)
    let source = graph.add("File", 0.0, 0.0)
    source.texts[0] = step
    let cache = graph.add("Cache", 0.0, 60.0)
    cache.texts[0] = file
    graph.connect(source.id, cache.id, 0)
    stamp_graph(graph)
    let written = graph.evaluate_data(cache.id)
    assert(CadGeometry.model_count(written.geometry) > 0)
    cache.values[0] = 1.0
    source.texts[0] = "missing.step"
    stamp_graph(graph)
    for node in graph.nodes:
        node.dirty = true
    let loaded = graph.evaluate_data(cache.id)
    assert(CadGeometry.model_count(loaded.geometry) == CadGeometry.model_count(written.geometry))
    for index in 0..<CadGeometry.model_count(written.geometry):
        let a = CadGeometry.model(written.geometry, index)
        let b = CadGeometry.model(loaded.geometry, index)
        assert(a.face_count() == b.face_count() and a.cv_count() == b.cv_count())
        for cv in 0..<a.cv_count():
            let p = a.cv_position(cv)
            let q = b.cv_position(cv)
            assert(p.x == q.x and p.y == q.y and p.z == q.z)
        assert(a.tessellate(8).topology_hash() == b.tessellate(8).topology_hash())
    print(f"PASS Cache round trip of {step}: {CadGeometry.model_count(loaded.geometry)} models, no upstream cook")