dymokomi/luced-3d
Source of release 0.11.4
0.11.4 / tests / editor / cache_tests.luc
## Node caching: content stamps, the worker's result store, hashed topology
## guards and incremental Edit replay on a ~500k-face mesh.
from workspace import Workspace
from network import Network
from geometry_tests import verb
from stamps import stamp_graph
from edit_operations import move_step
from compute_request import RequestWriter
from project_graph import write_graph, read_graph
from luce_prism.prism import Document
from luce_geocore.geocore import Mesh
from luce_3d.graphics import Vector3
from node_evaluation import SourceCache
import time
import node_commands
import selection_commands
import component_moves
func settle(workspace: Workspace) -> unit!:
let session = workspace.compute else trap("compute")
let deadline = time.now() + 60000000000
while session.busy and time.now() < deadline:
_ = workspace.poll_compute()
assert(not session.busy)
pub func cache_tests() -> unit!:
stamp_rules()
group_stamps()
hashed_guards()
store_hits()
source_cache()
large_edit_replay()
pinned_undo()
print("PASS stamps ignore names/flags/Edit values, result store hits on undo, hashed guards, small deltas and one-recipe Edit replay")
## Rename, placement, visibility, output flags and an Edit node's own values are
## not cook inputs; a parameter is, and undo restores the earlier stamp.
func stamp_rules() -> unit!:
let w = Workspace()
node_commands.add_node(w, "Edit Mesh", 0.0, 100.0)
stamp_graph(w.network)
let cube = w.network.require(1).stamp
let edit = w.network.require(2).stamp
node_commands.select_node(w, 1)
node_commands.rename(w, "Renamed cube")
node_commands.move_node(w, 1, 300.0, 20.0)
node_commands.toggle_visible(w)
node_commands.set_output(w, 1, 1)
node_commands.select_node(w, 2)
node_commands.set_parameter(w, 0, 0.9)
stamp_graph(w.network)
assert(w.network.require(1).stamp == cube and w.network.require(2).stamp == edit)
node_commands.select_node(w, 1)
node_commands.set_parameter(w, 0, 3.0)
stamp_graph(w.network)
assert(w.network.require(1).stamp != cube and w.network.require(2).stamp != edit)
w.undo()
stamp_graph(w.network)
assert(w.network.require(1).stamp == cube and w.network.require(2).stamp == edit)
## Visibility and output flags reach only the parent Group's stamp.
func group_stamps() -> unit!:
let graph = Network(false)
let group = graph.add("Group", 0.0, 0.0)
graph.scope = group.id
let child = graph.add("Cube", 0.0, 0.0)
let other = graph.add("Sphere", 0.0, 60.0)
stamp_graph(graph)
let before = group.stamp
let own = child.stamp
child.visible = false
stamp_graph(graph)
assert(group.stamp != before and child.stamp == own)
child.visible = true
child.name = "Renamed"
stamp_graph(graph)
assert(group.stamp == before)
other.output = -1
stamp_graph(graph)
assert(group.stamp != before and child.stamp == own)
## The guard is the mesh's 64-bit connectivity hash: transforms pass, other
## connectivity fails, projects store one integer and old arrays load unguarded.
func hashed_guards() -> unit!:
let mesh = Mesh.cube(2.0)
let edit = move_step("0 1", 1, Vector3(0.0, 0.5, 0.0), mesh.topology_hash())
assert(edit.topology == mesh.topology_hash() and edit.topology != 0)
_ = edit.evaluate(mesh.transformed(Vector3(1.0, 0.0, 0.0), Vector3(), Vector3(2.0, 2.0, 2.0)))
var rejected = false
_ = edit.evaluate(verb("PolyExtrude", mesh, "2", 3, [0.5])) catch failure: rejected = true
assert(rejected)
let graph = Network()
let node = graph.add("Edit Mesh", 0.0, 90.0)
graph.connect(1, node.id, 0)
node.edits.append(edit)
# A worker request resends a body only when its node's stamp changed.
stamp_graph(graph)
let writer = RequestWriter()
let full = writer.encode(graph, 3)
let delta = writer.encode(graph, 3)
assert(full.contains("edit_count") and not delta.contains("edit_count") and delta.byte_count < full.byte_count)
let doc = Document()
write_graph(doc, graph)
let loaded = read_graph(doc).require(node.id).edits[0]
assert(loaded.topology == edit.topology and loaded.group == "0 1" and loaded.key == edit.key)
## Undo, redo, renames and visibility round trips hit the worker's store and
## the UI's held meshes instead of recooking or recopying.
func store_hits() -> unit!:
let w = Workspace()
w.enable_compute()
settle(w)
node_commands.add_node(w, "Subdivide", 0.0, 100.0)
settle(w)
let cooks = (w.selected() else trap("Subdivide")).evaluations
let faces = (w.geometry else trap("subdivided")).face_count()
node_commands.set_parameter(w, 0, 2.0)
settle(w)
assert((w.selected() else trap("Subdivide")).evaluations == cooks + 1)
assert((w.geometry else trap("level 2")).face_count() == faces * 4)
w.undo()
settle(w)
assert((w.selected() else trap("Subdivide")).evaluations == cooks + 1)
assert((w.geometry else trap("undone")).face_count() == faces)
w.redo()
settle(w)
assert((w.selected() else trap("Subdivide")).evaluations == cooks + 1)
let adopted = w.publisher.adopted
node_commands.rename(w, "Renamed")
node_commands.toggle_visible(w)
settle(w)
node_commands.toggle_visible(w)
settle(w)
node_commands.select_node(w, 1)
settle(w)
node_commands.select_node(w, 2)
settle(w)
assert((w.selected() else trap("Subdivide")).evaluations == cooks + 1)
assert(w.publisher.adopted == adopted and w.geometry != none)
(w.compute else trap("compute")).close()
## Reloading an unchanged file (a new stamp) reuses its parse and tessellation.
func source_cache() -> unit!:
let cache = SourceCache()
let first = cache.load("tests/fixtures/assembly.step", 0.0)
let again = cache.load("tests/fixtures/assembly.step", 0.0)
assert(again is first and cache.hits == 1)
_ = cache.load("tests/fixtures/assembly.step", 0.001)
assert(cache.hits == 1)
let tessellated = cache.tessellated(first, 8, 0.0)
assert(cache.tessellated(again, 8, 0.0) is tessellated and cache.hits == 2)
assert(not (cache.tessellated(first, 12, 0.0) is tessellated))
## A ~500k-face mesh: requests stay small, a new recipe or a gizmo step
## applies one recipe to the stored prefix, and undo applies none.
func large_edit_replay() -> unit!:
let w = Workspace()
w.enable_compute()
settle(w)
node_commands.delete_selected(w)
node_commands.add_node(w, "File", 0.0, 0.0)
node_commands.set_text(w, 0, "tests/fixtures/grid500k.obj")
settle(w)
node_commands.add_node(w, "Edit Mesh", 0.0, 100.0)
settle(w)
let session = w.compute else trap("compute")
assert(w.editing() and (w.geometry else trap("grid")).face_count() > 500000)
for face in 0..<20:
selection_commands.choose_component(w, face * 9973, true)
component_moves.move_selection(w, Vector3(0.0, 0.1, 0.0))
settle(w)
assert(w.notice == "" and session.recipes == 1 and session.request_size < 16384)
# A second move of the same components merges into the first step (D19).
component_moves.move_selection(w, Vector3(0.0, 0.1, 0.0))
settle(w)
assert(w.notice == "" and session.recipes == 1 and session.request_size < 16384)
assert((w.selected() else trap("Edit Mesh")).edits.length == 1)
w.undo()
settle(w)
assert(session.recipes == 0)
w.begin("Move components")
component_moves.preview_move(w, Vector3(0.0, 0.2, 0.0))
settle(w)
assert(session.recipes == 1 and session.request_size < 16384)
component_moves.preview_move(w, Vector3(0.0, 0.3, 0.0))
settle(w)
assert(session.recipes == 1)
w.commit()
assert((w.selected() else trap("Edit Mesh")).edits.length == 1)
session.close()
## Undo inside the pinned window (the last eight steps) replays nothing:
## every stored prefix is kept while the Edit node is shown (D18).
func pinned_undo() -> unit!:
let w = Workspace()
w.enable_compute()
settle(w)
node_commands.add_node(w, "Edit Mesh", 0.0, 100.0)
settle(w)
let session = w.compute else trap("compute")
selection_commands.set_mode(w, 1)
for step in 0..<10:
# Alternate groups so the moves do not merge.
selection_commands.choose_component(w, step % 8, false)
component_moves.move_selection(w, Vector3(0.0, 0.05, 0.0))
settle(w)
assert(session.recipes == 1)
assert((w.selected() else trap("Edit Mesh")).edits.length == 10)
for undo in 0..<16:
if (w.selected() else trap("Edit Mesh")).edits.length <= 2: break
w.undo()
settle(w)
assert(session.recipes == 0)
session.close()