Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.2

0.11.2 / src / display_publisher.luc

## Adopts worker results by content key. A key the UI already holds keeps its
## mesh, scene object and renderer cache, so re-publishing an unchanged stamp
## (a rename, a selection, a visibility toggle elsewhere) rebuilds nothing. A
## fog volume's key is its grids' content: a new look (Volume Visualization)
## only sets the held volume's look, uploading nothing. A point cloud's key is
## its columns' (GaussianSplats.key): held, it uploads once.
## A few keys that just left the scene stay held, so toggling a node back on
## or undoing a display change needs no new copy either.
## Until the first result arrives the scene objects and line batches are the
## local cook's (made before the worker started); the first result replaces
## them whatever it holds, even nothing but fog.
from luce_geocore.geocore import Mesh
from luce_3d import graphics as three
from luce_3d.graphics import Group, Vector3, FogVolume, GaussianSplats
from compute_bridge import ComputeService
from network import Network
from geometry_data import GeometryData
from scene_projection import display_mesh

pub class DisplayPublisher:
    let objects: Group
    let held: map[int, Mesh]
    ## Fog volumes by key, kept like meshes so each uploads once.
    let held_volumes: map[int, FogVolume]
    ## The fog volumes of the displayed result.
    pub var volumes: list[FogVolume]
    ## Point clouds (Gaussian splats or dots) by key, kept like meshes so each
    ## uploads once, and those of the displayed result.
    let held_clouds: map[int, GaussianSplats]
    pub var clouds: list[GaussianSplats]
    ## The node each of `clouds` came from.
    pub var cloud_nodes: list[int]
    let shown: map[int, three.Mesh]
    var shown_keys: list[int]
    var product_keys: list[int]
    ## Every key the worker may re-publish without copying: this result's
    ## products, then recently dropped keys, newest first.
    pub var kept: list[int]
    pub var geometry: Mesh? = none
    pub let bounds: list[Vector3]
    ## The meshes on screen and the node each came from (object picking).
    pub var nodes: list[int]
    pub var meshes: list[Mesh]
    ## Meshes adopted as new copies over the session (tests and benchmarks).
    pub var adopted: int = 0
    ## Whether a result has been shown: before it, `objects` holds the local
    ## cook's meshes, which no key of ours names.
    var showing: bool = false

    pub func init(self, objects: Group):
        self.objects = objects
        self.held = {}
        self.held_volumes = {}
        self.volumes = []
        self.held_clouds = {}
        self.clouds = []
        self.cloud_nodes = []
        self.shown = {}
        self.shown_keys = []
        self.product_keys = []
        self.kept = []
        self.bounds = []
        self.nodes = []
        self.meshes = []

    ## Adopt the completed result. Returns whether its overlay batches differ
    ## from the previous result's, i.e. whether the caller must fetch them.
    pub func adopt(self, service: ComputeService, network: Network) -> bool!:
        let displayed: list[int] = []
        let products: list[int] = []
        self.nodes = []
        self.meshes = []
        self.volumes = []
        self.clouds = []
        self.cloud_nodes = []
        var fresh = false
        self.geometry = none
        for index in 0..<service.mesh_count():
            let key = service.mesh_key(index)
            let node = service.mesh_node(index)
            products.append(key)
            if service.volume_fresh(index):
                self.held_volumes[key] = service.take_volume(index)
                fresh = true
            if service.volume_product(index):
                if let volume = self.held_volumes[key]:
                    service.style_volume(index, volume)
                    self.volumes.append(volume)
                continue
            if service.cloud_fresh(index):
                self.held_clouds[key] = service.take_cloud(index)
                fresh = true
            if service.cloud_product(index):
                if let cloud = self.held_clouds[key]:
                    self.clouds.append(cloud)
                    self.cloud_nodes.append(node)
                continue
            if service.mesh_fresh(index):
                self.held[key] = service.take_mesh(index)
                self.adopted += 1
                fresh = true
            let mesh = self.held[key] else continue
            if service.mesh_display(index):
                displayed.append(key)
                self.nodes.append(node)
                self.meshes.append(mesh)
            if service.mesh_inspect(index):
                if let selected = network.find(node):
                    selected.cache = GeometryData(mesh)
                if node == network.pick_node():
                    self.geometry = mesh
        let first = not self.showing
        if first or displayed != self.shown_keys:
            self.show(displayed)
        self.retain(products)
        let changed = first or fresh or products != self.product_keys
        self.product_keys = products
        return changed

    ## The scene objects of the displayed keys: a key still on screen keeps its
    ## object, a newly shown one gets one.
    func show(self, displayed: list[int]) -> unit!:
        self.objects.clear()
        self.bounds.clear()
        let shown: map[int, three.Mesh] = {}
        for key in displayed:
            let mesh = self.held[key] else continue
            if let existing = self.shown[key]:
                shown[key] = existing
            else:
                shown[key] = display_mesh(mesh)
            self.objects.add(shown[key] else continue)
            self.bounds.append(mesh.bounds().minimum)
            self.bounds.append(mesh.bounds().maximum)
        self.shown.clear()
        for key in shown.keys():
            self.shown[key] = shown[key] else continue
        self.shown_keys = displayed
        self.showing = true

    ## Keep this result's keys plus the six most recent dropped ones.
    func retain(self, products: list[int]) -> unit:
        let kept = products.copy()
        var extra = 0
        for key in self.kept:
            if extra == 6:
                break
            if not (key in kept):
                kept.append(key)
                extra += 1
        let meshes: map[int, Mesh] = {}
        let volumes: map[int, FogVolume] = {}
        let clouds: map[int, GaussianSplats] = {}
        for key in kept:
            if let mesh = self.held[key]:
                meshes[key] = mesh
            if let volume = self.held_volumes[key]:
                volumes[key] = volume
            if let cloud = self.held_clouds[key]:
                clouds[key] = cloud
        self.held.clear()
        for key in meshes.keys():
            self.held[key] = meshes[key] else continue
        self.held_volumes.clear()
        for key in volumes.keys():
            self.held_volumes[key] = volumes[key] else continue
        self.held_clouds.clear()
        for key in clouds.keys():
            self.held_clouds[key] = clouds[key] else continue
        self.kept = kept