dymokomi/luced-3d
Source of release 0.11.9
0.11.9 / tools / preview_held.luc
## Captures of Edit Mesh holding its result, for preview.py
## (docs/research/POLY-MODELING.md, "Edit nodes hold their result"):
## held_boxes: two boxes made inside one Edit Mesh, the second moved into
## the first and picked at the Object level.
## held_boolean: the same two boxes both picked and joined by Boolean
## (Union): one piece, its faces cut where the boxes crossed.
## held_subtract: the same, Subtract: the moved box cut out of the first.
## held_cad: a box and a cylinder made inside one Edit CAD, the cylinder
## moved beside the box.
## held_sdf: a sphere and a box made inside one Edit SDF, the box moved
## over the sphere's right half and set to Subtract: that half cut
## away, the box (picked) drawn through.
## held_drag: 1,000 faces of a 1M-face grid in an Edit Mesh dragged up a
## little each frame in the real window; prints the work a move takes
## and the frame intervals (request to redraw).
## held_heavy: two 1M-face spheres in an Edit Mesh, the second cut out of
## the first by Boolean (Subtract) in the real window; prints the frame intervals while it runs and
## when its result showed (with --background the worker runs it and the
## UI keeps drawing; without, the UI thread runs it).
## held_heavy_running: the same, captured a few frames after the
## command, while the worker runs it: the Edit Mesh node computing,
## both spheres still shown.
from app import Luce3D
from luce_3d.graphics import Vector3
from luce_geocore.geocore import Selection
from edit_types import face_level, object_level
import node_commands
import modeling_commands
import selection_commands
import component_moves
import preview_support
import edit_held
import time
pub func scenes() -> list[str]:
return ["held_boxes", "held_boolean", "held_subtract", "held_cad", "held_sdf", "held_drag", "held_heavy", "held_heavy_running"]
## The frames a scene runs before its capture (held_drag drags over them).
pub func frames(scene: str) -> int:
if scene == "held_heavy":
return 70
if scene == "held_heavy_running":
return 7
return 130 if scene == "held_drag" else 5
pub func setup(editor: Luce3D, scene: str) -> unit!:
let w = editor.workspace
node_commands.delete_selected(w)
if scene == "held_cad" or scene == "held_sdf":
let cad = scene == "held_cad"
node_commands.add_node(w, "CAD" if cad else "SDF", 40.0, 40.0)
node_commands.enter_group(w, w.network.selected)
node_commands.add_node(w, "Edit CAD" if cad else "Edit SDF", 40.0, 40.0)
preview_support.settle(w)
modeling_commands.create(w, "Box" if cad else "Sphere")
modeling_commands.create(w, "Cylinder" if cad else "Box")
component_moves.move_selection(w, Vector3(1.6, 0.8, -1.4) if cad else Vector3(1.0, -1.0, 0.0))
if cad:
selection_commands.select_none(w)
else:
modeling_commands.model(w, "Subtract")
preview_support.settle(w)
return
if scene.starts_with("held_heavy"):
# Two UV spheres of 999,698 faces, overlapping, in an Edit Mesh;
# both picked at the Object level.
node_commands.add_node(w, "Sphere", 40.0, 40.0)
let first = w.network.require(w.network.selected)
node_commands.add_node(w, "Sphere", 200.0, 40.0)
let second = w.network.require(w.network.selected)
for sphere in [first, second]:
sphere.values[1] = 1414.0
sphere.values[2] = 707.0
second.values[3] = 0.7
second.values[4] = 0.3
node_commands.add_node(w, "Merge", 40.0, 140.0)
let merge = w.network.require(w.network.selected)
w.network.connect(first.id, merge.id, 0)
w.network.connect(second.id, merge.id, 1)
for node in [first, second, merge]:
w.network.invalidate(node.id)
w.rebuild()
preview_support.settle(w)
node_commands.add_node(w, "Edit Mesh", 40.0, 240.0)
preview_support.settle(w)
let mesh = w.geometry else trap("spheres")
selection_commands.set_mode(w, object_level)
selection_commands.choose_component(w, 0, false)
selection_commands.choose_component(w, mesh.face_count() - 1, true)
preview_support.settle(w)
return
if scene != "held_drag":
node_commands.add_node(w, "Edit Mesh", 40.0, 40.0)
preview_support.settle(w)
modeling_commands.create(w, "Box")
modeling_commands.create(w, "Box")
component_moves.move_selection(w, Vector3(1.2, 0.6, -0.8))
preview_support.settle(w)
selection_commands.set_mode(w, object_level)
if scene == "held_boxes":
# The moved box picked.
selection_commands.choose_component(w, 6, false)
preview_support.settle(w)
return
selection_commands.choose_component(w, 0, false)
selection_commands.choose_component(w, 6, true)
modeling_commands.model(w, "Boolean", [1.0] if scene == "held_subtract" else [0.0])
selection_commands.select_none(w)
preview_support.settle(w)
return
# held_drag: the grid, 1024 × 1024, in an Edit Mesh; a band of 1,000
# faces across its middle picked.
node_commands.add_node(w, "Grid", 40.0, 40.0)
let grid = w.network.require(w.network.selected)
grid.values[2] = 1024.0
grid.values[3] = 1024.0
w.network.invalidate(grid.id)
w.rebuild()
preview_support.settle(w)
node_commands.add_node(w, "Edit Mesh", 40.0, 140.0)
preview_support.settle(w)
selection_commands.set_mode(w, face_level)
w.selection = Selection.of(w.geometry else trap("grid"), "409600-410599", 2)
preview_support.settle(w)
## What held_drag measures over its frames.
pub class DragTimes:
pub var last_frame: int = 0
pub var work: float = 0.0
pub var worst_work: float = 0.0
pub var gaps: float = 0.0
pub var worst_gap: float = 0.0
pub var moves: int = 0
pub func act(editor: Luce3D, scene: str, frame: int, last: int, t: DragTimes) -> unit!:
if scene.starts_with("held_heavy"):
heavy(editor, scene, frame, last, t)
return
if scene != "held_drag":
return
let w = editor.workspace
let now = time.now()
if frame == 4:
w.begin("Move gizmo")
elif frame > 4 and frame < last - 2:
if t.moves > 0:
let gap = float(now - t.last_frame) / 1000000.0
t.gaps += gap
if gap > t.worst_gap: t.worst_gap = gap
let started = time.now()
component_moves.preview_move(w, Vector3(0.0, 0.002 * float(frame - 4), 0.0))
editor.panels.view.layout().invalidate()
let spent = float(time.now() - started) / 1000000.0
t.work += spent
if spent > t.worst_work: t.worst_work = spent
t.moves += 1
elif frame == last - 2:
w.commit()
print(f"Held drag in the window: {t.moves} moves of 1,000 faces of a 1M-face grid; the move {t.work / float(t.moves):.2f} ms mean, {t.worst_work:.2f} ms worst; frame to frame {t.gaps / float(t.moves - 1):.2f} ms mean, {t.worst_gap:.2f} ms worst")
t.last_frame = now
## held_heavy's Boolean, at frame 4.
func heavy(editor: Luce3D, scene: str, frame: int, last: int, t: DragTimes) -> unit!:
if frame != 4 or t.moves != 0:
return
# The capture asks for a frame every 4 ms, as a turning camera would
# (the editor itself draws only when something changed): how often
# frames come is how free the UI thread is.
_ = editor.app.every(0.004, func () -> unit!: editor.panels.view.layout().invalidate())
let started = time.now()
modeling_commands.model(editor.workspace, "Boolean", [1.0])
t.work = float(time.now() - started) / 1000000.0
t.gaps = float(started)
t.last_frame = time.now()
t.moves = 1
## held_heavy's every frame, the capture's own waiting for the worker
## included: after the Boolean, the intervals between frames (each redraws
## the viewport) until its result shows.
pub func watch(editor: Luce3D, scene: str, t: DragTimes) -> unit!:
if not scene.starts_with("held_heavy") or t.moves <= 0:
return
let w = editor.workspace
let now = time.now()
let gap = float(now - t.last_frame) / 1000000.0
t.last_frame = now
if gap > t.worst_gap: t.worst_gap = gap
t.moves += 1
let node = w.selected() else trap("edit")
let shown = w.geometry else trap("shown")
if not edit_held.waiting(node) and shown.face_count() != 1999396 and node.edits.length > 0:
print(f"Held heavy action in the window ({"worker" if w.compute != none else "UI thread"}): the Boolean's command took {t.work:.1f} ms; its result showed {(float(now) - t.gaps) / 1000000.0:.1f} ms after it; the longest frame interval meanwhile {t.worst_gap:.1f} ms over {t.moves - 1} frames")
t.moves = -1