Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / viewport_batches.luc

## The line batches the viewport draws over the displayed scene: wires (mesh
## and CAD edges), normal guides, CAD patch boundaries, curves and control
## hulls, the context's and, apart, the rest of the scene's around it
## (context_view.luc), which the context display mode greys or hides; the point clouds it draws as Gaussian splats or dots; and the fog
## volumes it ray-marches over all of them. Made on the
## UI thread when the graph cooks synchronously, or taken from the worker's
## result, where only the batches whose content changed are fetched again (a
## CV drag keeps the CAD edges and redraws the hull).
from luce_3d.graphics import WireBatch, FogVolume, GaussianSplats
from luce_geocore.geocore import GeometrySet
from compute_bridge import ComputeService
from overlay_lines import OverlayLines
from check_labels import EdgeLabel
import check_labels

pub class ViewportBatches:
    pub var wire_batch: WireBatch? = none
    pub var normal_batch: WireBatch? = none
    pub var patch_batch: WireBatch? = none
    ## Curves as lines, and their control hulls: one batch per displayed node
    ## without the worker, one for the whole result with it.
    pub let curve_batches: list[WireBatch]
    pub let hull_batches: list[WireBatch]
    ## Check nodes' lines (cad_check.luc), five a node in luce-cad's group
    ## order (passed, near, failed, combs, quills): batch i is group i % 5.
    pub let check_batches: list[WireBatch]
    ## Their edge labels (check_labels.luc).
    pub var check_labels: list[EdgeLabel]
    ## The rest of the scene's lines: wires and CAD edges, patch borders,
    ## curves and hulls (no normal guides: those are the context's).
    pub var around: list[WireBatch]
    ## The displayed fog volumes: the local cook's, or the worker result's
    ## (DisplayPublisher keeps them by key, so each uploads once).
    pub var volumes: list[FogVolume]
    pub var volume_nodes: list[int]
    ## The local cook's fog volumes by content key (FogVolume.key), kept to
    ## the next cook so an unchanged grid with a new look uploads nothing.
    pub var held_volumes: map[int, FogVolume]
    ## The displayed point clouds: the local cook's, or the worker result's
    ## (DisplayPublisher keeps them by key, so each uploads once).
    pub var clouds: list[GaussianSplats]
    ## The node each of `clouds` came from (an Edit node's selection tints
    ## only its own cloud).
    pub var cloud_nodes: list[int]
    ## The local cook's point clouds by content key (GaussianSplats.key), kept
    ## to the next cook so an unchanged cloud uploads nothing.
    pub var held_clouds: map[int, GaussianSplats]
    ## The displayed outputs' whole sets and a key of each (equal keys, the
    ## same set), for the render mode: the local cook's, or the worker's.
    pub var scenes: list[GeometrySet]
    pub var scene_keys: list[int]
    pub var scene_nodes: list[int]
    ## The worker batches shown (ComputeService.batch_key; -1 none): wires,
    ## normals, patches, curves, hulls; around the context (no normals); the
    ## five check groups and the checks' labels.
    var keys: list[int]
    var helper_keys: list[int]
    var helper_lines: list[list[OverlayLines]]
    var glyph_keys: list[int]
    var glyphs: list[list[WireBatch]]

    pub func init(self):
        self.curve_batches = []
        self.hull_batches = []
        self.check_batches = []
        self.check_labels = []
        self.volumes = []
        self.volume_nodes = []
        self.held_volumes = {}
        self.clouds = []
        self.cloud_nodes = []
        self.held_clouds = {}
        self.scenes = []
        self.scene_keys = []
        self.scene_nodes = []
        self.keys = unshown()
        self.around = []
        self.helper_keys = []
        self.helper_lines = []
        self.glyph_keys = []
        self.glyphs = []

    ## Start a synchronous cook: no curves, hulls or checks yet.
    pub func clear_curves(self):
        self.curve_batches.clear()
        self.hull_batches.clear()
        self.check_batches.clear()
        self.check_labels = []

    ## The lines of a synchronous cook (curve batches were added as it went),
    ## and the rest of the scene's (`around`).
    pub func publish(self, wires: OverlayLines, normals: OverlayLines, patches: OverlayLines, hulls: OverlayLines, around: list[WireBatch]) -> unit!:
        self.wire_batch = wires.batch()
        self.normal_batch = normals.batch()
        self.patch_batch = patches.batch()
        self.keys = unshown()
        self.hull_batches.append(hulls.batch())
        self.around = around

    ## The worker's batches, fetching only those that changed; the products
    ## of the nodes `outside` (the rest of the scene) go to `around`.
    pub func adopt(self, service: ComputeService, outside: list[int]) -> unit!:
        if self.changed(service, 0, outside, false):
            self.wire_batch = service.wires(outside)
        if self.changed(service, 1, outside, false):
            self.normal_batch = service.normals(outside)
        if self.changed(service, 2, outside, false):
            self.patch_batch = service.patches(outside)
        if self.changed(service, 3, outside, false):
            self.curve_batches.clear()
            self.curve_batches.append(service.curve_lines(outside))
        if self.changed(service, 4, outside, false):
            self.hull_batches.clear()
            self.hull_batches.append(service.hulls(outside))
        var checks = false
        for which in 5..<11:
            checks = self.changed(service, which, outside, false) or checks
        if checks:
            self.check_batches.clear()
            for group in 0..<5:
                self.check_batches.append(service.check_batch(group, outside))
            let pairs = OverlayLines.make()
            service.check_labels(outside, pairs)
            self.check_labels = check_labels.read(pairs)
        var around = false
        for which in [0, 2, 3, 4]:
            around = self.changed(service, which, outside, true) or around
        if around:
            self.around = [service.wires(outside, true), service.patches(outside, true), service.curve_lines(outside, true), service.hulls(outside, true)]

    ## Whether the worker's batch `which` (of the context, or `around` it)
    ## differs from the one shown (and note the new one).
    func changed(self, service: ComputeService, which: int, outside: list[int], around: bool) -> bool!:
        let key = service.batch_key(which, outside, around)
        # Context 0 to 4, around 5 to 8 (no normals), checks 9 to 13, their
        # labels 14.
        var slot = which + (5 if around else 0) - (1 if around and which > 1 else 0)
        if which >= 5:
            slot = which + 4
        if self.keys[slot] == key:
            return false
        self.keys[slot] = key
        return true

    ## Retained camera/light picking strokes, rebuilt only when outputs change.
    pub func helpers(self, at: int) -> list[OverlayLines]!:
        if self.helper_keys != self.scene_keys:
            self.helper_keys = self.scene_keys
            self.helper_lines = []
            for geometry in self.scenes:
                let lines: list[OverlayLines] = []
                let scene = geometry.scene()
                for index in 0..<scene.count():
                    let helper = OverlayLines.make()
                    helper.add_scene_object(geometry, index)
                    lines.append(helper)
                self.helper_lines.append(lines)
        return self.helper_lines[at]

    ## Scene `at`'s camera and light glyphs to draw, one batch each (a pane
    ## can leave out the camera it looks through), rebuilt only when the
    ## outputs change.
    pub func glyph_batches(self, at: int) -> list[WireBatch]!:
        if self.glyph_keys != self.scene_keys:
            self.glyph_keys = self.scene_keys
            self.glyphs = []
            for index in 0..<self.scenes.length:
                let batches: list[WireBatch] = []
                for helper in self.helpers(index):
                    batches.append(helper.batch())
                self.glyphs.append(batches)
        return self.glyphs[at]

## The worker batch keys of nothing shown yet (changed's slots).
func unshown() -> list[int]:
    let keys: list[int] = []
    for _ in 0..<15:
        keys.append(-1)
    return keys