Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.3

0.11.3 / src / sketch_frames.luc

## A Sketch node's input body and frame for the editor on the UI thread
## (sketch_node.luc): an origin plane's frame at once; a Face plane's, and
## the body that projected curves follow, need its input, cooked here on a
## copy of the graph and kept until the input changes.
from workspace import Workspace
from network import Node
from geometry_data import GeometryData
from sketch_node import SketchFrame, node_frame, face_frame, face_plane

pub class FrameCache:
    var key: str = ""
    var kept: SketchFrame? = none
    ## The input body as last cooked, and its node and stamp.
    var body_key: str = ""
    var body_data: GeometryData? = none

    ## Sketch node `node`'s input as cooked now (none without one), and a
    ## key that changes when it does.
    pub func body(self, w: Workspace, node: Node) -> GeometryData?!:
        let found = w.network.find(node.inputs[0])
        if found == none:
            self.body_key = ""
            self.body_data = none
            return none
        let input = found else return none
        # Stamps are current: every change rebuilds, which stamps the
        # graph, so this runs no graph walk per event.
        let key = f"{input.id}|{input.stamp}"
        if key != self.body_key:
            self.body_key = key
            self.body_data = w.network.copy().evaluate_data(input.id) catch failure:
                recover none
        return self.body_data

    pub func body_stamp(self) -> str:
        return self.body_key

    ## Sketch node `node`'s frame in `w` (an origin plane at once).
    pub func frame(self, w: Workspace, node: Node) -> SketchFrame!:
        if int(node.values[0]) != face_plane:
            return node_frame(node.values)
        let data = self.body(w, node) else return node_frame([1.0, node.values[1]])
        let key = f"{self.body_key}|{node.texts[1]}|{node.values[1]}"
        if key == self.key:
            if let f = self.kept:
                return f
        let f = face_frame(node.values, node.texts, data) catch failure:
            recover node_frame([1.0, node.values[1]])
        self.key = key
        self.kept = f
        return f