Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / tests / editor / tool_parity_tests.luc

## Node and step parity (POLY-MODELING.md ยง3.5, 1): every registry tool of
## Edit Mesh, run on the same selection of the same grid, gives the same
## mesh and output selection as a step of an Edit Mesh node, as a verb
## node after a plain node, and through its own cook; a Select tool's
## selection is its node's cook selection.
from workspace import Workspace
from luce_geocore.geocore import Selection, Mesh, Vector3, Verbs
from view_projection import project
from geometry_data import GeometryData
from tool_record import Tool
from tools import ToolRun
from edit_steps import add_tool_node
import modeling_commands
import placing_commands
import node_commands
import selection_commands
import tool_prompts
import tool_registry
from edit_types import point_level, edge_level, face_level

## The level a tool is checked at (faces, else edges, else points, as its
## first pick takes them) and the selection there, on the 4 by 4 grid
## over -1..1 (point z*5+x, face z*4+x): two interior faces, a row of
## interior edges from boundary to boundary, or the four points of face 5.
func pick_of(tool: Tool) -> (int, str, int):
    if tool.offered(face_level):
        return (face_level, "5 6", 2)
    if tool.offered(edge_level):
        return (edge_level, "p5-6-7-8-9", 4)
    return (point_level, "6 7 12 11", 0)

## A workspace showing the grid, with an Edit Mesh after it when `edit`,
## and `tool`'s selection made.
func picked(tool: Tool, edit: bool) -> Workspace!:
    let w = Workspace()
    node_commands.delete_selected(w)
    node_commands.add_node(w, "Grid", 0.0, 0.0)
    if edit:
        node_commands.add_node(w, "Edit Mesh", 0.0, 100.0)
    let (level, expression, domain) = pick_of(tool)
    selection_commands.set_mode(w, level)
    w.selection = Selection.of(w.geometry else trap("grid"), expression, domain)
    return w

## A knife stroke down the screen through face 5's middle.
func stroke(w: Workspace) -> unit!:
    let middle = project(w, (w.geometry else trap("grid")).face_center(5), 800.0, 600.0)
    modeling_commands.bisect(w, middle[0] + 1.0, middle[1] - 40.0, middle[0] - 1.0, middle[1] + 40.0, 800.0, 600.0)

## A Knife stroke across face 5 seen from above: two points on the grid.
func knife_cut(w: Workspace, tool: Tool) -> unit!:
    let middle = (w.geometry else trap("grid")).face_center(5)
    placing_commands.start_placing(w, tool.label, "knife")
    placing_commands.add_corner(w, middle.add(Vector3(-0.5, 0.0, 0.1)), Vector3(0.0, -1.0, 0.0))
    placing_commands.add_corner(w, middle.add(Vector3(0.5, 0.0, 0.1)), Vector3(0.0, -1.0, 0.0))
    placing_commands.finish_placing(w)

## `tool` on its selection: as a step (`edit`) or a node. A Select tool
## changes the selection in both; its node is checked apart (selected). A
## hover tool (Loop Cut) runs on the selection with its starting numbers, as
## Repeat runs it.
func ran(tool: Tool, edit: bool) -> Workspace!:
    let w = picked(tool, edit)
    if tool.gesture == "stroke":
        stroke(w)
    elif tool.gesture == "knife":
        knife_cut(w, tool)
    elif tool.gesture == "hover":
        modeling_commands.model(w, tool.label, tool.starting())
    else:
        modeling_commands.model(w, tool.label)
    # A tool asking for a second pick (Bridge given one loop) takes none.
    if w.prompt != none:
        tool_prompts.accept_prompt(w)
    return w

## A Select tool as a node after the grid: its cook selection becomes the
## viewport's.
func selected(tool: Tool) -> Workspace!:
    let w = picked(tool, false)
    let mesh = w.geometry else trap("grid")
    let chosen = w.current_selection() else trap("selection")
    add_tool_node(w, tool.label, ToolRun(tool.name, tool.starting()), chosen.expression(mesh), mesh)
    assert(w.network.require(w.network.display).kind == tool.name)
    return w

## Whether two results agree: counts, bounds and the output selection.
func same(a: Workspace, b: Workspace) -> bool!:
    let x = a.geometry else return false
    let y = b.geometry else return false
    return same_mesh(x, y) and a.selected_count() == b.selected_count()

func same_mesh(x: Mesh, y: Mesh) -> bool:
    if x.point_count() != y.point_count() or x.face_count() != y.face_count():
        return false
    let p = x.bounds()
    let q = y.bounds()
    for (u, v) in [(p.minimum.x, q.minimum.x), (p.minimum.y, q.minimum.y), (p.minimum.z, q.minimum.z), (p.maximum.x, q.maximum.x), (p.maximum.y, q.maximum.y), (p.maximum.z, q.maximum.z)]:
        if u - v > 0.000001 or v - u > 0.000001:
            return false
    return true

## A primitive: a step of an Edit Mesh with nothing wired gives what its
## node gives, and so does its cook on nothing.
func primitive_parity(tool: Tool) -> unit!:
    let step = Workspace()
    node_commands.delete_selected(step)
    node_commands.add_node(step, "Edit Mesh", 0.0, 0.0)
    modeling_commands.create(step, tool.label)
    let node = Workspace()
    node_commands.delete_selected(node)
    node_commands.add_node(node, tool.name, 0.0, 0.0)
    let made = step.geometry else trap(tool.label)
    assert(same_mesh(made, node.geometry else trap(tool.label)), tool.label)
    let edit = step.selected() else trap("edit")
    let cooked = tool.cook(edit.edits[0].invocation(), GeometryData(mesh = Mesh.empty()), "")
    assert(same_mesh(cooked.operand() else trap(tool.label), made), tool.label)

pub func tool_parity_tests() -> unit!:
    for tool in tool_registry.of_kind("Edit Mesh"):
        # Primitives start from nothing; Poly Pen's steps and the verbs of a
        # reference surface are checked with theirs (poly_pen_tests,
        # retopology_tests).
        if tool.inputs == 0:
            primitive_parity(tool)
            continue
        if tool.gesture == "pen" or Verbs.second_required(Verbs.index_of(tool.name)):
            continue
        let step = ran(tool, true)
        let node = selected(tool) if tool.section == "Select" else ran(tool, false)
        if tool.section != "Select":
            assert((step.selected() else trap("edit")).edits.length == 1)
            assert(node.network.require(node.network.display).kind == tool.name, tool.label)
        if not same(step, node):
            print(f"FAIL {tool.label}: the step and the node differ")
            assert(false)
        # The tool's own cook on the step's invocation gives the step's mesh.
        if tool.section != "Select" and tool.gesture in ["", "hover"]:
            let edit = step.selected() else trap("edit")
            let input = picked(tool, false).geometry else trap("grid")
            let cooked = tool.cook(edit.edits[0].invocation(), GeometryData(mesh = input), "")
            assert(same_mesh(cooked.operand() else trap(tool.label), step.geometry else trap("step")), tool.label)
    print("PASS tool parity: every Edit Mesh tool gives the same mesh and selection as a step, as a node and through its cook, from the node's starting values; every primitive as a step on nothing, as a node and through its cook")