Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / src / stamps.luc

## Content stamps: a node's stamp names its cooked result. It covers what
## compute reads (kind, bypass, parameters, texts, Edit recipes, the file
## epoch, every input's stamp and the object paths it hides), never names,
## positions, display flags, selection or the Edit tool amount. Flags affect
## only what a level yields: a container's stamp lists the stamps of what it
## yields (Network.outputs), and a Render node's those of the root's objects.
## An Import node's lists its source's stamp and the steps that place it. A
## node path row's text is left out for what it reaches (node_paths.luc).
from network import Network
from node import Node
from cache_node import reads_file, cache_stamp, frozen_stamp
from edit_kinds import is_edit
import node_catalog
import script_cook
import node_paths

## Stamp every node. Computed on the UI thread before each request; cheap,
## since Edit recipes carry their own precomputed content keys.
pub func stamp_graph(graph: Network) -> unit!:
    let done: map[int, int] = {}
    for node in graph.nodes:
        node.stamp = stamp_of(graph, node, done, 0)

func stamp_of(graph: Network, node: Node, done: map[int, int], depth: int) -> int!:
    if let known = done[node.id]:
        return known
    if depth > 256:
        trap("node stamps exceed the graph depth budget")
    let inputs: list[int] = []
    for input in node.inputs:
        if let upstream = graph.find(input):
            inputs.append(stamp_of(graph, upstream, done, depth + 1))
        else:
            inputs.append(0)
    let children: list[int] = []
    if node.kind == "Input":
        let source = graph.find(graph.context_input(node))
        if let upstream = source:
            inputs.append(stamp_of(graph, upstream, done, depth + 1))
        else:
            inputs.append(0)
    if node_catalog.collects_children(node.kind):
        for child in graph.outputs(node.id):
            children.append(stamp_of(graph, child, done, depth + 1))
    # Render renders the root's flagged objects.
    if node.kind == "Render":
        for child in graph.outputs(-1):
            children.append(stamp_of(graph, child, done, depth + 1))
    # An Import reads its source, placed (a source that reads the Import
    # fails its cook; it is not followed, so stamping ends).
    for source in graph.import_sources(node):
        if not graph.depends_on(source.id, node.id):
            children.append(stamp_of(graph, source, done, depth + 1))
            let steps: list[float] = graph.import_steps(node, source) catch failure:
                recover [-1.0]
            children.append(nonzero(steps.hash()))
    # A Material reads every node inside it.
    if node.kind == "Material":
        for child in graph.children(node.id):
            children.append(nonzero((stamp_of(graph, child, done, depth + 1), child.enabled).hash()))
    var stamp = 0
    if node.frozen != "":
        stamp = frozen_stamp(node.frozen)
    elif is_edit(node.kind) and node.enabled and holds(graph, node):
        # A holding Edit node names its held result; untouched, its input.
        stamp = nonzero((node.kind, "held", node.held_key, node.held_failure).hash()) if node.held_key != 0 else nonzero((node.kind, false, inputs).hash())
        # An action the worker runs (edit_held.luc) is a drag's frame apart
        # from its release: a frame's result stays on the worker, and the
        # released cook must put it where the UI takes it (HeldStore), not
        # find the frame's under the same stamp and leave the node waiting.
        if node.held_base != 0 and graph.gesture:
            stamp = nonzero((stamp, "gesture").hash())
    elif is_edit(node.kind) and node.enabled:
        # An unconnected node starts from its project's tolerances (the
        # worker's stored prefixes are keyed alike: edit_base).
        stamp = prefix_stamp(node, edit_base(graph, inputs[0]), node.edits.length)
        # A drag's results apart: its last step failing fails the node, let
        # go it is a warning (edit_types.run_step).
        if graph.gesture:
            stamp = nonzero((stamp, "gesture").hash())
    elif is_edit(node.kind):
        # A bypassed Edit passes its input through; its own values are UI state.
        stamp = nonzero((node.kind, false, inputs).hash())
    elif node.kind == "Cache":
        stamp = stamp_cache(node, inputs)
    else:
        # A node path is a name: what it reaches is in the stamp instead
        # (node_paths.resolution), and an Import's sources in `children`.
        let texts = path_free(graph, node)
        stamp = nonzero((node.kind, node.enabled, node.values, texts, inputs, node.sources, children, node.source_revision, node.guard, graph.direct_children(node)).hash())
    # A Script node reads its file parameters' files: an edited file recooks it.
    if node.kind == script_cook.kind_name and node.frozen == "":
        stamp = nonzero((stamp, script_cook.file_stamps(node.texts, graph.folder)).hash())
    # A result that follows the timeline is a new result each frame.
    if node.time_dependent and node.frozen == "":
        stamp = nonzero((stamp, graph.frame, graph.fps).hash())
    # Bodies are built under the project's tolerances: new ones, new
    # results. (An Edit node's are in its base already; a Cache's or a
    # frozen node's result is its file's, named by the file.)
    if not (is_edit(node.kind) and node.enabled) and node.kind != "Cache" and node.frozen == "":
        stamp = nonzero((stamp, tolerance_key(graph)).hash())
    # Hidden objects leave the result after the cook (an Edit node's recipe
    # prefixes keep the whole result).
    if node.hidden.length > 0:
        stamp = nonzero((stamp, node.hidden).hash())
    done[node.id] = stamp
    return stamp

## The project's CAD tolerances as a stamp part.
pub func tolerance_key(graph: Network) -> int:
    let t = graph.tolerances
    return nonzero((t.positional, t.fit, t.tangent, t.curvature).hash())

## What an Edit node's recipe prefixes are keyed from: its input's stamp, or
## (unconnected) the project's tolerances.
pub func edit_base(graph: Network, input: int) -> int:
    return input if input != 0 else tolerance_key(graph)

## A Cache node's stamp: from its path and input while it writes (Load from
## disk off, or bypassed); from its file while it reads, so upstream edits
## stop there and downstream results survive the switch (cache_node.luc).
func stamp_cache(node: Node, inputs: list[int]) -> int!:
    let written = nonzero(("Cache", node.enabled, node.texts[0].trim(), inputs, node.source_revision).hash())
    return cache_stamp(reads_file(node.kind, node.enabled, node.values), node.texts[0], node.source_revision, written)

## An enabled Edit node's result after its first `count` recipes. The full
## count is the node's own stamp, so the state before a new recipe (or a
## gizmo drag's preview recipe) is already stored under count - 1.
pub func prefix_stamp(node: Node, input: int, count: int) -> int:
    let recipes: list[int] = []
    for at in 0..<count:
        recipes.append(node.edits[at].key)
    return nonzero((node.kind, input, recipes).hash())

## Zero means "never cooked" on both sides of the worker boundary.
## `node`'s texts with each node path row's text left out for what it
## reaches (node_paths.resolution): a rename the paths followed keeps the
## stamp, a path that comes to name another node changes it.
func path_free(graph: Network, node: Node) -> list[str]:
    if not node_paths.holds_paths(node.kind):
        return node.texts
    let texts = node.texts.copy()
    for each in node_paths.rows(node.kind):
        texts[each.slot] = ""
    texts.append(str(node_paths.resolution(graph.nodes, node)))
    return texts

func nonzero(value: int) -> int:
    return value if value != 0 else 1

## Whether `node`'s Edit kind holds its result (edit_held.luc).
func holds(graph: Network, node: Node) -> bool:
    let kind = graph.kinds.of(node.kind) else return false
    return kind.holds