dymokomi/luced-3d
Source of release 0.11.9
0.11.9 / 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, one batch a product: a category that changed fetches only the
## products it did not hold (a CV drag keeps the CAD edges and redraws the
## hull of the one body moved).
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:
## Wires (mesh and CAD edges), normal guides and CAD patch boundaries:
## one batch a product with the worker, one a cook without it.
pub let wire_batches: list[WireBatch]
pub let normal_batches: list[WireBatch]
pub let patch_batches: list[WireBatch]
## Curves as lines, and their control hulls: one batch per displayed node
## without the worker, one a product 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]]
## The worker products' batches by product key, a map per overlay of the
## context (0 to 4) and around it (5 to 9): kept while a result still
## has the product.
var made: list[map[int, WireBatch]]
pub func init(self):
self.wire_batches = []
self.normal_batches = []
self.patch_batches = []
self.made = []
for slot in 0..<10:
self.made.append({})
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_batches.clear()
self.wire_batches.append(wires.batch())
self.normal_batches.clear()
self.normal_batches.append(normals.batch())
self.patch_batches.clear()
if patches.count() > 0:
self.patch_batches.append(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_batches.clear()
self.lines(service, 0, outside, false, self.wire_batches)
if self.changed(service, 1, outside, false):
self.normal_batches.clear()
self.lines(service, 1, outside, false, self.normal_batches)
if self.changed(service, 2, outside, false):
self.patch_batches.clear()
self.lines(service, 2, outside, false, self.patch_batches)
if self.changed(service, 3, outside, false):
self.curve_batches.clear()
self.lines(service, 3, outside, false, self.curve_batches)
if self.changed(service, 4, outside, false):
self.hull_batches.clear()
self.lines(service, 4, outside, false, self.hull_batches)
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:
let lines: list[WireBatch] = []
for which in [0, 2, 3, 4]:
self.lines(service, which, outside, true, lines)
self.around = lines
## Overlay `which`'s batches appended to `into`: one a product, those the
## last ones held taken again, the rest made now; the others go.
func lines(self, service: ComputeService, which: int, outside: list[int], around: bool, into: list[WireBatch]) -> unit!:
let slot = which + (5 if around else 0)
let kept = self.made[slot]
let fresh: map[int, WireBatch] = {}
for at in 0..<service.batch_count(which, outside, around):
let product = service.batch_product(which, outside, around, at)
let batch = kept[product] else service.product_lines(which, product)
fresh[product] = batch
into.append(batch)
self.made[slot] = fresh
## 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