Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / tests / editor / edit_cad_nets_tests.luc

## Edit CAD's CVs level, surfacing Stage 1 (edit_cad_nets.luc): on a NURBS
## dome, Move CVs steps move the middle CV along its normal alone (NUV) and
## carry neighbors by Rows falloff (Preceding 1: a neighbor by the
## exponent's weight, a corner by its square); in a workspace, a box's face
## converted to a B-spline and raised to degree 2 gives CVs; the right-click
## and S menus offer the structure tools on picked CVs; the gizmo in NUV
## mode moves along the frame fixed at its press as one Move CVs step, with
## a ghost of the hull; a second click takes a hull row; Insert Knot and
## Rebuild (Explicit Control: multi-span quintic, then a single span of
## degree 7) make one undo each and say what they did; one falloff
## control (Alias's) at the CVs level; NUV's frame the surface's; Step
## size, Drag strength and locks; an arrow-key nudge; Insert Knot placed
## by a click on its green isoparm.
from workspace import Workspace
from node import Node
from app import Luce3D
from geometry_data import cad_data
from edit_operations import EditStep
from cad_display_tests import dome_model
from luce_cad.cad_geometry import CadGeometry
from luce_cad.nurbs import CadNurbs
import tool_run
import edit_cad
import edit_cad_nets
import marking_items
import modeling_commands
import node_commands
import selection_commands
import component_moves
import node_catalog
from knot_place import KnotPlacer
import view_projection as gm
from luce_3d.graphics import Vector3
from luce_std import math

pub func edit_cad_nets_tests() -> unit!:
    steps_on_a_dome()
    one_falloff()
    in_the_workspace()
    print("PASS Edit CAD CVs: NUV and Rows falloff steps, NUV on the surface frame, one falloff control with step, strength and locks, a converted face's CVs, structure tools in the right-click and S menus, the NUV gizmo's fixed frame and ghost, hull rows, an arrow nudge, Insert Knot placed by a click and Rebuild (multi-span quintic, single span degree 7) as one undo each")

func steps_on_a_dome() -> unit!:
    let kind = edit_cad.kind()
    let dome = cad_data(dome_model())
    let cloud = kind.pick(dome, 1) else trap("cloud")
    # NUV: 1.5 along the middle CV's normal (up), nothing else moves.
    let lifted = CadGeometry.model(kind.run(EditStep(edit_cad_nets.move_verb, "4", 1, [1.0, 1.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 3.0], cloud.topology_hash()), 0, dome, "#1", none).geometry, 0)
    assert(math.abs(lifted.cv_position(4).z - 5.5) < 1e-6 and math.abs(lifted.cv_position(4).x - 1.0) < 1e-6)
    for cv in [0, 1, 3, 8]:
        assert(lifted.cv_position(cv).z == 0.0)
    # Rows falloff, one row each way, exponent 1: an edge neighbor half, a
    # corner a quarter.
    let rows = CadGeometry.model(kind.run(EditStep(edit_cad_nets.move_verb, "4", 1, [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 3.0], cloud.topology_hash()), 0, dome, "#1", none).geometry, 0)
    assert(math.abs(rows.cv_position(1).z - 0.5) < 1e-6 and math.abs(rows.cv_position(0).z - 0.25) < 1e-6 and math.abs(rows.cv_position(4).z - 5.0) < 1e-6)
    # NUV's gizmo stands in each CV's surface frame (Alias's), not its
    # hull's: on the dome's side CV they differ.
    let model = CadGeometry.model(dome.geometry, 0)
    let axes = edit_cad_nets.gizmo_axes(1, cloud, 1, []) else trap("no NUV axes")
    for which in 0..<3:
        assert(axes[which].subtract(CadNurbs.cv_frame(model, 1, which)).length() < 1e-6, f"axis {which}")
    assert(axes[0].subtract(CadNurbs.cloud_hull_frame(cloud, 1, 0)).length() > 1e-3)

## Edit CAD's parameters at the CVs level: one falloff (Alias's), its own
## rows for each kind, Symmetry kept (it mirrors CV moves), Keep selection
## gone; Step size, Drag strength and the locks shape a move.
func one_falloff() -> unit!:
    let rows = node_catalog.fields("Edit CAD")
    var labels: list[str] = []
    for row in rows:
        labels.append(row.name)
    for label in ["CV move", "Falloff", "Soft radius", "Radius curve", "Rows U", "Falloff exponent", "Symmetry", "Step size", "Drag strength", "Lock X", "Lock Y", "Lock Z"]:
        assert(label in labels, f"no {label}: {labels}")
    var falloffs = 0
    for label in labels:
        if label == "Falloff": falloffs += 1
    assert(falloffs == 1 and not ("Keep selection" in labels) and not ("CV falloff" in labels), f"{labels}")
    for row in rows:
        if row.name == "Soft radius":
            assert(row.hide_when == "{ falloff != 0 }")
    let values: list[float] = []
    for _ in 0..<32:
        values.append(0.0)
    values[edit_cad_nets.strength_slot] = 0.5
    values[edit_cad_nets.step_slot] = 0.25
    values[edit_cad_nets.lock_y_slot] = 1.0
    let made = edit_cad_nets.adjusted(values, Vector3(1.1, 3.0, -0.9))
    assert(made.subtract(Vector3(0.5, 0.0, -0.5)).length() < 1e-12, f"{made}")
    values[2] = 1.0
    assert(edit_cad_nets.move_numbers(values, Vector3())[10] == 1.0)

## The cloud point of the displayed model's CV (u, v) on its surface.
func cv_at(w: Workspace, u: int, v: int) -> int!:
    let cloud = w.geometry else trap("no cloud")
    let us = cloud.find_attribute("cad_cv_u", 0)
    let vs = cloud.find_attribute("cad_cv_v", 0)
    for i in 0..<cloud.point_count():
        if int(cloud.attribute_value(us, i)) == u and int(cloud.attribute_value(vs, i)) == v: return i
    trap("no such CV")

func in_the_workspace() -> unit!:
    let editor = Luce3D()
    let w = editor.workspace
    node_commands.delete_selected(w)
    node_commands.add_node(w, "CAD", 0.0, 0.0)
    node_commands.enter_group(w, w.network.selected)
    node_commands.add_node(w, "Edit CAD", 0.0, 0.0)
    let node = w.selected() else trap("node")
    modeling_commands.create(w, "Box")
    # A face converted, then raised to degree 2 each way: a 3 by 3 net.
    selection_commands.set_mode(w, 3)
    selection_commands.choose_component(w, 0, false)
    tool_run.run(w, edit_cad_nets.convert_verb)
    assert(node.edits[-1].verb == edit_cad_nets.convert_verb and not node.warning.contains("could not"), node.warning)
    let converted = CadGeometry.model(w.network.evaluate_data(node.id).geometry, 0)
    var spline = -1
    for face in 0..<converted.face_count():
        if converted.face_surface_kind(face) == 3: spline = converted.face_surface_id(face)
    assert(spline >= 0)
    selection_commands.choose_component(w, 0, false)
    tool_run.run(w, edit_cad_nets.raise_verb)
    let raised = CadGeometry.model(w.network.evaluate_data(node.id).geometry, 0)
    assert(CadNurbs.structure(raised, spline).nu == 3 and CadNurbs.structure(raised, spline).nv == 3, node.warning)
    # The CVs level: the middle CV picked; the menus offer the tools.
    selection_commands.set_mode(w, 1)
    let middle = cv_at(w, 1, 1)
    selection_commands.choose_component(w, middle, false)
    assert(w.selected_count() == 1)
    var offered: list[str] = []
    editor.panels.refresh()
    for command in editor.panels.viewport_menu.entries():
        offered.append(command.text())
    for name in [edit_cad_nets.insert_verb, edit_cad_nets.remove_verb, edit_cad_nets.raise_verb, edit_cad_nets.rebuild_verb]:
        assert(name in offered, f"right-click lacks {name}: {offered}")
    var marked: list[str] = []
    for item in marking_items.modify_items(w, editor.panels.actions, no_add, editor.panels.view.sketch.editor):
        marked.append(item.name)
    assert(edit_cad_nets.rebuild_verb in marked and edit_cad_nets.insert_verb in marked, f"S menu: {marked}")
    # NUV: the gizmo's axes the CV's frame, fixed at the press; one drag
    # one Move CVs step, the hull's ghost kept meanwhile.
    node_commands.set_parameter(w, edit_cad_nets.move_mode_slot, 1.0)
    let frame = component_moves.cv_axes(w) else trap("no NUV frame")
    let before = CadGeometry.model(w.network.evaluate_data(node.id).geometry, 0)
    let steps = node.edits.length
    w.begin("Move gizmo")
    w.cv_frame = frame
    w.ghost_hulls = w.batches.hull_batches.copy()
    component_moves.preview_move(w, frame[0].multiply_scalar(0.3))
    component_moves.preview_move(w, frame[0].multiply_scalar(0.7))
    w.commit()
    w.cv_frame = none
    w.ghost_hulls = []
    assert(shown(w).edits.length == steps + 1 and shown(w).edits[-1].verb == edit_cad_nets.move_verb)
    let after = CadGeometry.model(w.network.evaluate_data(shown(w).id).geometry, 0)
    let first = CadNurbs.first_cv(after, spline)
    let moved = after.cv_position(first + 4).subtract(before.cv_position(first + 4))
    assert(moved.subtract(frame[0].multiply_scalar(0.7)).length() < 1e-6, f"moved {moved.x} {moved.y} {moved.z}")
    assert(after.cv_position(first).subtract(before.cv_position(first)).length() == 0.0)
    # A second click on the CV takes its row (3 CVs), a third the other.
    selection_commands.choose_component(w, cv_at(w, 1, 1), false, row = 0)
    assert(w.selected_count() == 3)
    selection_commands.choose_component(w, cv_at(w, 1, 1), false, row = 2)
    assert(w.selected_count() == 9)
    # Arrow keys with the Move tool: a nudge of a step (the node's Step
    # size) along the gizmo's first axis, one undo.
    selection_commands.choose_component(w, cv_at(w, 1, 1), false)
    node_commands.set_parameter(w, edit_cad_nets.step_slot, 0.125)
    modeling_commands.choose_tool(w, "Move")
    assert(component_moves.nudges(w))
    let axis = component_moves.cv_axes(w) else trap("no axes")
    let held = CadGeometry.model(w.network.evaluate_data(shown(w).id).geometry, 0)
    component_moves.nudge(w, 0, 1.0)
    let nudged = CadGeometry.model(w.network.evaluate_data(shown(w).id).geometry, 0)
    assert(nudged.cv_position(first + 4).subtract(held.cv_position(first + 4)).subtract(axis[0].multiply_scalar(0.125)).length() < 1e-6)
    w.undo()
    modeling_commands.choose_tool(w, "Select")
    # Insert Knot: one undo, a row more, said in the tool bar's line.
    selection_commands.choose_component(w, cv_at(w, 1, 1), false)
    tool_run.run(w, edit_cad_nets.insert_verb)
    let inserted = CadGeometry.model(w.network.evaluate_data(shown(w).id).geometry, 0)
    assert(CadNurbs.structure(inserted, spline).nu == 4)
    assert(shown(w).summary.starts_with("Inserted 1 knot"), shown(w).summary)
    # Alias's click: the isoparm under the pointer, a click puts the knot
    # there (the surface told to the editor by the step's cook).
    assert(edit_cad_nets.probe_words(shown(w).sketch_state) != "", shown(w).sketch_state)
    let placer = KnotPlacer(w)
    assert(placer.active())
    let target = CadNurbs.point_at(inserted, spline, 0.3, 0.6)
    let at = gm.project(w, target, 900.0, 600.0)
    assert(placer.click(at[0], at[1], 900.0, 600.0))
    assert(math.abs(shown(w).edits[-1].numbers[edit_cad_nets.fraction_slot] - 0.3) < 1e-3, f"{shown(w).edits[-1].numbers}")
    w.undo()
    w.undo()
    assert(CadNurbs.structure(CadGeometry.model(w.network.evaluate_data(shown(w).id).geometry, 0), spline).nu == 3)
    # Rebuild: Explicit Control's multi-span quintic (4 spans, held G2: 9
    # CVs a direction), then a single span of degree 7 (8).
    selection_commands.choose_component(w, cv_at(w, 1, 1), false)
    tool_run.run(w, edit_cad_nets.rebuild_verb)
    let quintic = CadNurbs.structure(CadGeometry.model(w.network.evaluate_data(shown(w).id).geometry, 0), spline)
    assert(quintic.du == 5 and quintic.nu == 9 and quintic.nv == 9, f"{quintic.du} {quintic.nu} {shown(w).warning}")
    assert(shown(w).summary.contains("moved at most"), shown(w).summary)
    modeling_commands.set_last_step(w, edit_cad_nets.preset_slot, 1.0, 0.0, 2.0)
    let single = CadNurbs.structure(CadGeometry.model(w.network.evaluate_data(shown(w).id).geometry, 0), spline)
    assert(single.du == 7 and single.nu == 8 and single.spans_u == 1)

## The Edit node as it is now (a gizmo drag's previews replace the graph).
func shown(w: Workspace) -> Node!:
    return w.selected() else trap("no Edit node")

func no_add(kind: str) -> unit!:
    _ = kind