Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / src / local_cook.luc

## Cooking what the viewport draws on the UI thread, as tests and scripts do
## before a worker runs (the worker's path is compute_worker.luc): each view
## item's scene objects (context_view.luc) go into `objects` and its lines
## into the viewport batches; the displayed node's result comes back for
## picking.
from luce_3d.graphics import Group, Matrix4, CurveLines, WireBatch
from luce_geocore.geocore import Mesh
from network import Network
from node import Node
from geometry_data import GeometryData
from overlay_lines import OverlayLines
import check_labels
import cad_nodes
from scene_projection import append_scene, append_placed, append_overlays, append_patch_boundaries, append_cad_hulls, append_fog, append_clouds
from viewport_batches import ViewportBatches
from render_geometry import render_geometry
from context_view import view_items
import context_display
from slice_view import Slicer

pub class LocalCook:
    ## The node picked on (Network.pick_node) and its result, when it cooked.
    pub var node: Node? = none
    pub var data: GeometryData? = none
    ## The first failure, or how many CAD patches could not be meshed.
    pub var notice: str = ""
    ## The meshes on screen and the node each came from (object picking).
    pub var nodes: list[int]
    pub var meshes: list[Mesh]
    ## The meshes of the rest of the scene around the context, placed: they
    ## hide what lies behind them, but are not picked on.
    pub var around: list[Mesh]
    ## Every mesh drawn around the context (All or Greyed), placed, and the
    ## node each came from: a snap target among them is landed on.
    pub var outside: list[Mesh]
    pub var outside_nodes: list[int]

    pub func init(self):
        self.nodes = []
        self.meshes = []
        self.around = []
        self.outside = []
        self.outside_nodes = []

## The result of the nearest node upstream of node `id` that cooked.
func nearest_cooked(graph: Network, id: int) -> GeometryData?!:
    for upstream in graph.upstream_chain(id):
        let data = graph.evaluate_data(upstream) catch failure:
            continue
        return data
    return none

## Cook what `graph`'s viewport draws (the context, then the rest of the
## root) into `objects` and `batches`, meshes prepared for display `mode`.
## The render mode's sets are the root's objects, whatever the context. The
## rest of the scene around the context draws as `context_mode` says
## (context_display.luc), its lines apart. While `dragging`, a node that
## fails shows its last good result.
pub func cook_outputs(graph: Network, objects: Group, batches: ViewportBatches, mode: int, slicer: Slicer, context_mode: int = 1, dragging: bool = false) -> LocalCook!:
    let result = LocalCook()
    objects.clear()
    batches.clear_curves()
    batches.volumes.clear()
    batches.volume_nodes.clear()
    let held = batches.held_volumes
    batches.held_volumes = {}
    batches.clouds.clear()
    batches.cloud_nodes.clear()
    let held_clouds = batches.held_clouds
    batches.held_clouds = {}
    batches.scenes = []
    batches.scene_keys = []
    batches.scene_nodes = []
    for node in graph.outputs(-1):
        let data = graph.evaluate_data(node.id) catch failure:
            result.notice = failure.message
            continue
        # A node's evaluations count its cooks, so the key changes with its
        # result.
        batches.scene_nodes.append(node.id)
        batches.scenes.append(render_geometry(data, graph.gesture, graph.cad_quality))
        batches.scene_keys.append((node.id, node.evaluations, graph.gesture, graph.cad_quality).hash())
    var analytic = 0
    let wires = OverlayLines.make()
    let normals = OverlayLines.make()
    let patches = OverlayLines.make()
    let hulls = OverlayLines.make()
    # The rest of the scene's lines: wires and CAD edges, patch borders, hulls.
    let around_wires = OverlayLines.make()
    let around_patches = OverlayLines.make()
    let around_hulls = OverlayLines.make()
    let around: list[WireBatch] = []
    for item in view_items(graph):
        let node = item.node
        let matrix = item.matrix
        # A failed node shows, while a drag makes it fail, its last good
        # result (Fusion's last good preview); else the nearest node upstream
        # that cooked (what it started from); not picked on: the view never
        # goes blank.
        var fallback = false
        var kept = false
        var shown: GeometryData? = none
        shown = graph.evaluate_data(node.id) catch failure:
            if item.outside:
                recover none
            result.notice = failure.message
            fallback = true
            kept = dragging and node.last_good != none
            recover node.last_good if kept else nearest_cooked(graph, node.id)
        let cooked = shown else continue
        # A sliced sketch shows its body cut by its plane.
        let data = slicer.shown(graph, node, cooked)
        if fallback:
            result.notice = f"{result.notice} (the viewport {"keeps its last good result" if kept else "shows its input"})"
        if node.id == graph.pick_node() and not item.outside and not fallback:
            result.node = node
            result.data = cooked
        let branch = Group()
        if item.outside:
            let fade = context_display.fade if context_mode == context_display.greyed else 0.0
            if context_mode != context_display.hidden:
                analytic += append_placed(data, matrix, branch, fade = fade) catch failure:
                    continue
        else:
            analytic += append_scene(data, branch, mode = mode) catch failure:
                result.notice = failure.message
                continue
        objects.add(branch)
        # The rest of the scene is not picked on: tools act on the context.
        if not item.outside:
            append_pick_meshes(data, Matrix4(), node.id, result)
        elif context_mode != context_display.hidden:
            let placed = LocalCook()
            append_pick_meshes(data, matrix, node.id, placed)
            for at in 0..<placed.meshes.length:
                result.outside.append(placed.meshes[at])
                result.outside_nodes.append(placed.nodes[at])
                # Drawn as it is, the rest hides what lies behind it.
                if context_mode == context_display.all:
                    result.around.append(placed.meshes[at])
        # Overlays are display only: one that cannot be drawn says so and
        # leaves the cook (and the edit that made it) standing.
        append_overlays(data, matrix, around_wires if item.outside else wires, OverlayLines.make() if item.outside else normals) catch failure:
            result.notice = f"{node.name}: guides not drawn: {failure.message}"
        append_cad_hulls(data, matrix, around_hulls if item.outside else hulls, modeled = node.id == graph.cv_nets) catch failure:
            result.notice = f"{node.name}: hulls not drawn: {failure.message}"
        if CurveLines.count(data.geometry) > 0:
            let curves = CurveLines.batch(data.geometry, matrix = matrix)
            # A CAD sketch's curves show no control polygons, as Fusion's.
            let hulled = cad_nodes.shows_curve_hulls(node.kind)
            if item.outside:
                around.append(curves)
                if hulled:
                    around.append(CurveLines.batch(data.geometry, true, matrix))
            else:
                batches.curve_batches.append(curves)
                if hulled:
                    batches.hull_batches.append(CurveLines.batch(data.geometry, true, matrix))
        # CAD curves (luce-cad's objects): lines; their hulls go with the
        # CV nets (append_cad_hulls).
        if data.has_curves():
            let lines = OverlayLines.make()
            lines.add_cad_curves(data.geometry, matrix)
            if item.outside:
                around.append(lines.batch())
            else:
                batches.curve_batches.append(lines.batch())
        append_patch_boundaries(data, matrix, around_patches if item.outside else patches)
        # A Check node's lines (cad_check.luc), one batch per group, and its
        # edge labels.
        if let check = data.check:
            if not item.outside:
                for group in 0..<5:
                    let lines = OverlayLines.make()
                    lines.add_check(check, group, matrix)
                    batches.check_batches.append(lines.batch())
                let pairs = OverlayLines.make()
                pairs.add_check_labels(check, matrix)
                for label in check_labels.read(pairs): batches.check_labels.append(label)
        append_fog(data, matrix, batches.volumes, held, batches.held_volumes) catch failure:
            result.notice = f"{node.name}: fog not drawn: {failure.message}"
        append_clouds(data, matrix, batches.clouds, held_clouds, batches.held_clouds) catch failure:
            result.notice = f"{node.name}: points not drawn: {failure.message}"
        while batches.volume_nodes.length < batches.volumes.length:
            batches.volume_nodes.append(node.id)
        while batches.cloud_nodes.length < batches.clouds.length:
            batches.cloud_nodes.append(node.id)
    # A tool prompt picks on its input, which is not shown.
    if result.node == none:
        if let node = graph.find(graph.picking):
            result.data = graph.evaluate_data(node.id) catch failure:
                result.notice = failure.message
                recover none
            if result.data != none:
                result.node = node
    if analytic > 0 and result.notice == "":
        result.notice = f"CAD preview: {analytic} patches could not be meshed; boundaries remain visible"
    around.append(around_wires.batch())
    around.append(around_patches.batch())
    around.append(around_hulls.batch())
    batches.publish(wires, normals, patches, hulls, around)
    return result

## Match the worker's world-space picking meshes, including CAD and SDF.
func append_pick_meshes(data: GeometryData, matrix: Matrix4, node: int, result: LocalCook) -> unit!:
    let meshes: list[Mesh] = []
    if let mesh = data.mesh: meshes.append(mesh)
    if let field = data.field_preview(): meshes.append(field)
    if data.has_cad():
        for mesh in data.preview().meshes: meshes.append(mesh)
    for mesh in meshes:
        if mesh.face_count() == 0: continue
        result.nodes.append(node)
        result.meshes.append(mesh.placed(matrix))
    for at in 0..<data.instance_count():
        if data.instance_visible(at):
            append_pick_meshes(data.instance(at), matrix.multiply(data.instance_matrix(at)), node, result)