Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / tests / editor / sketch_curve_tests.luc

## The Sketch editor's curves past lines and circles, drawn by clicks as a
## user would on the ground plane: a fit point spline (Enter ends it), a
## control point spline (its last point again ends it), an ellipse, a
## conic, the circle touching a triangle's three sides, a tangent arc off a
## line's end, an overall slot, an arc slot and a blend curve between two lines (as
## curved as each: Curvature), the whole sketch reading back from its text.
from app import Luce3D
from luce_ui.ui import Event
from luce_window.input import EventKind, Key
from luce_cad.sketch import CadSketch
from sketch_kinds import arc_kind, circle_kind, conic_kind, curvature, ellipse_kind, offset, line_kind, point_kind, smooth, spline_kind, text_kind
from luce_cad.cad_geometry import CadGeometry
from luce_3d.graphics import Vector3
from view_projection import project
import node_commands
from luce_std import math
from snapping import grid_step
from sketch_support import click, key, near, sketch_of, last_of

## The line starting at (`x`, `y`), or -1.
func line_from(s: CadSketch, x: float, y: float) -> int:
    for at in 0..<s.entity_count():
        if s.kind(at) == line_kind and near(s.x(s.part(at, 0)), x) and near(s.y(s.part(at, 0)), y):
            return at
    return -1

pub func sketch_curve_tests() -> unit!:
    let editor = Luce3D()
    editor.app.layout(1600.0, 1000.0)
    let w = editor.workspace
    # Clicks land where the tests put them: no grid snapping (one check
    # below turns it on).
    w.snaps.flags[1] = false
    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, "Sketch", 0.0, 0.0)
    let id = w.network.selected
    node_commands.display_selected(w)
    w.view.yaw = 0.0
    w.view.pitch = 1.5
    w.view.distance = 320.0
    w.view.target = Vector3(70.0, 0.0, 30.0)
    w.view.update_camera()
    editor.app.layout(1600.0, 1000.0)
    let e = editor.panels.view.sketch.editor
    # A 3-point rectangle: a side's ends, then how wide; then Select again
    # (a tool is used once).
    e.choose("3-Point Rectangle")
    for (x, y) in [(0.0, 60.0), (20.0, 60.0), (10.0, 70.0)]:
        click(editor, x, y)
    var s = sketch_of(editor, id)
    var sides = 0
    for at in 0..<s.entity_count():
        # (The first corner snapped onto the y axis: not a side.)
        if s.kind(at) == line_kind and s.origin_part(at) < 0:
            sides += 1
    assert(sides == 4 and last_of(s, arc_kind) < 0 and e.tool == "Select" and w.last_sketch_tool == "3-Point Rectangle")
    # A dragged corner snaps onto a point near it (the layer drags use too)
    # and, let go there, is held on it.
    e.choose("Point")
    click(editor, 40.0, 80.0)
    s = sketch_of(editor, id)
    let target = s.entity_count() - 1
    let view = editor.panels.view
    let bounds = view.layout().bounds()
    let from_at = project(w, e.frame.at(20.0, 70.0), bounds.width, bounds.height)
    let to_at = project(w, e.frame.at(40.4, 79.7), bounds.width, bounds.height)
    for (kind, at) in [(EventKind.pointer_moved, from_at), (EventKind.pointer_down, from_at), (EventKind.pointer_moved, to_at), (EventKind.pointer_up, to_at)]:
        editor.app.dispatch(Event(kind = kind, x = bounds.x + at[0], y = bounds.y + at[1], button = 0))
    s = sketch_of(editor, id)
    var landed = false
    for at in 0..<s.entity_count():
        if at != target and s.kind(at) == point_kind and near(s.x(at), s.x(target)) and near(s.y(at), s.y(target)) and math.hypot(s.x(at) - 40.0, s.y(at) - 80.0) < 0.05:
            landed = true
    assert(landed)
    # Grid snapping on: a point clicked off the grid lands on it, and the
    # mark says so.
    w.snaps.flags[1] = true
    e.choose("Point")
    click(editor, 90.3, 52.9)
    s = sketch_of(editor, id)
    let step = grid_step(w.view.distance)
    let gridded = s.entity_count() - 1
    assert(near(s.x(gridded) / step, math.round(s.x(gridded) / step)) and near(s.y(gridded) / step, math.round(s.y(gridded) / step)))
    w.snaps.flags[1] = false
    # A fit point spline through four points, ended by Enter.
    e.choose("Fit Point Spline")
    for (x, y) in [(0.0, 0.0), (10.0, 10.0), (20.0, 0.0), (30.0, 10.0)]:
        click(editor, x, y)
    key(editor, Key.enter)
    s = sketch_of(editor, id)
    let fit = last_of(s, spline_kind)
    assert(fit >= 0 and s.part_count(fit) == 4 and s.fit(fit))
    # A control point spline, ended by its last point again.
    e.choose("Control Point Spline")
    for (x, y) in [(0.0, 30.0), (10.0, 40.0), (20.0, 30.0), (30.0, 40.0), (40.0, 30.0), (50.0, 40.0), (50.0, 40.0)]:
        click(editor, x, y)
    s = sketch_of(editor, id)
    let control = last_of(s, spline_kind)
    assert(control != fit and s.part_count(control) == 6 and not s.fit(control))
    # An ellipse: center, major axis' end, 8 wide.
    e.choose("Ellipse")
    for (x, y) in [(70.0, 0.0), (90.0, 0.0), (70.0, 8.0)]:
        click(editor, x, y)
    s = sketch_of(editor, id)
    let ellipse = last_of(s, ellipse_kind)
    assert(ellipse >= 0 and near(s.radius(ellipse), 8.0))
    # A conic: start, end, apex.
    e.choose("Conic Curve")
    for (x, y) in [(100.0, 0.0), (120.0, 0.0), (110.0, 15.0)]:
        click(editor, x, y)
    s = sketch_of(editor, id)
    assert(last_of(s, conic_kind) >= 0 and near(s.radius(last_of(s, conic_kind)), 0.5))
    # A triangle, then the circle touching its three sides: its inradius,
    # 600 / (20 + 36.0555…).
    e.choose("Line")
    for (x, y) in [(0.0, -60.0), (40.0, -60.0), (20.0, -30.0), (0.0, -60.0)]:
        click(editor, x, y)
    key(editor, Key.escape)
    e.choose("3-Tangent Circle")
    for (x, y) in [(13.0, -60.0), (33.0, -49.5), (7.0, -49.5)]:
        click(editor, x, y)
    s = sketch_of(editor, id)
    let inner = last_of(s, circle_kind)
    assert(inner >= 0 and near(s.radius(inner), 600.0 / (20.0 + math.sqrt(1300.0))))
    # A tangent arc off a line's end: a quarter circle of 10 about (80, -50).
    e.choose("Line")
    click(editor, 60.0, -60.0)
    click(editor, 80.0, -60.0)
    key(editor, Key.escape)
    e.choose("Tangent Arc")
    click(editor, 80.0, -60.0)
    click(editor, 90.0, -50.0)
    s = sketch_of(editor, id)
    let arc = last_of(s, arc_kind)
    let center = s.part(arc, 0)
    assert(arc >= 0 and near(s.radius(arc), 10.0) and near(s.x(center), 80.0) and near(s.y(center), -50.0))
    # An overall slot 40 long and 10 wide: half circles of 5.
    e.choose("Overall Slot")
    for (x, y) in [(100.0, -60.0), (140.0, -60.0), (120.0, -55.0)]:
        click(editor, x, y)
    s = sketch_of(editor, id)
    assert(near(s.radius(last_of(s, arc_kind)), 5.0))
    # A quarter arc slot about (100, -110), its centerline 20 out, 10 wide:
    # arcs of 25 and 15, its end caps 5.
    e.choose("Center Point Arc Slot")
    for (x, y) in [(100.0, -110.0), (120.0, -110.0), (100.0, -90.0), (100.0, -85.0)]:
        click(editor, x, y)
    s = sketch_of(editor, id)
    let cap = last_of(s, arc_kind)
    assert(near(s.radius(cap), 5.0) and near(s.radius(cap - 2), 15.0) and near(s.radius(cap - 3), 25.0))
    # A blend curve between two lines' ends: a quintic held as curved as
    # each (two Curvature constraints).
    e.choose("Line")
    click(editor, 0.0, -100.0)
    click(editor, 20.0, -100.0)
    key(editor, Key.escape)
    click(editor, 40.0, -90.0)
    click(editor, 60.0, -90.0)
    key(editor, Key.escape)
    let before = sketch_of(editor, id).constraint_count()
    e.choose("Blend Curve")
    click(editor, 12.0, -100.0)
    click(editor, 48.0, -90.0)
    s = sketch_of(editor, id)
    let blend = last_of(s, spline_kind)
    assert(blend != control and s.part_count(blend) == 6)
    var curvatures = 0
    for at in before..<s.constraint_count():
        if s.constraint_kind(at) == curvature:
            curvatures += 1
    assert(curvatures == 2)
    # Two lines crossing like an X at (160, -30): Trim on one's arm past
    # the crossing takes that arm, the line ending at the crossing.
    e.choose("Line")
    click(editor, 150.0, -40.0)
    click(editor, 170.0, -20.0)
    key(editor, Key.escape)
    click(editor, 150.0, -20.0)
    click(editor, 170.0, -40.0)
    key(editor, Key.escape)
    s = sketch_of(editor, id)
    let crossed = line_from(s, 150.0, -40.0)
    e.choose("Trim")
    click(editor, 166.0, -24.0)
    s = sketch_of(editor, id)
    let cut_end = s.part(crossed, 1)
    assert(near(s.x(cut_end), 160.0) and near(s.y(cut_end), -30.0))
    # Offset that line, its distance typed: a copy 4 away, the 4 kept as
    # one Offset relation (its one dimension).
    let lines_before = s.entity_count()
    e.choose("Offset")
    click(editor, 155.0, -35.0)
    for c in [52]:
        editor.app.dispatch(Event(kind = EventKind.text_input, codepoint = c))
    key(editor, Key.enter)
    s = sketch_of(editor, id)
    var offset_dimension = false
    for at in 0..<s.constraint_count():
        if s.constraint_kind(at) == offset and near(s.constraint_value(at), 4.0):
            offset_dimension = true
    assert(s.entity_count() > lines_before and offset_dimension)
    # A G3 blend from the arc slot's inner arc (its start, at (115, -110))
    # to a line: eight control points, held G3 at both ends, and solving.
    e.choose("Line")
    click(editor, 140.0, -90.0)
    click(editor, 160.0, -90.0)
    key(editor, Key.escape)
    let smooth_before = sketch_of(editor, id).constraint_count()
    e.choose("Blend Curve G3")
    click(editor, 112.0, -100.0)
    click(editor, 148.0, -90.0)
    s = sketch_of(editor, id)
    let g3 = last_of(s, spline_kind)
    var smooths = 0
    for at in smooth_before..<s.constraint_count():
        if s.constraint_kind(at) == smooth:
            smooths += 1
    assert(CadSketch.parse(s.text()).solve())
    assert(s.part_count(g3) == 8 and smooths == 2)
    # Text: its baseline's start, how tall, then what it says.
    e.choose("Text")
    click(editor, 130.0, 0.0)
    click(editor, 130.0, 8.0)
    assert(editor.panels.sketch_text.layout().is_visible())
    for c in [67, 65, 68]:
        editor.app.dispatch(Event(kind = EventKind.text_input, codepoint = c))
    key(editor, Key.enter)
    assert(not editor.panels.sketch_text.layout().is_visible())
    s = sketch_of(editor, id)
    let text = last_of(s, text_kind)
    assert(text >= 0 and near(s.radius(text), 8.0) and s.text_content(text) == "CAD")
    # It all reads back from its text.
    assert(CadSketch.parse(s.text()).text() == s.text())
    lettered_body(editor)
    projected_edge(editor)
    editor.close()
    print("PASS Sketch curves: fit and control point splines, ellipse, conic, a triangle's 3-tangent circle, tangent arc, overall and arc slots, blend curve with curvature, trim of crossing lines, typed offset, G3 blend, text placed and extruded, a box's edge projected and following it")

## An "I" alone in a sketch, extruded: a solid of its outline.
func lettered_body(editor: Luce3D) -> unit!:
    let w = editor.workspace
    let s = CadSketch.parse("")
    _ = s.add_text(s.add_point(0.0, 0.0), 10.0, "I")
    node_commands.add_node(w, "Sketch", 300.0, 0.0)
    node_commands.set_text(w, 0, s.text())
    node_commands.add_node(w, "Extrude", 300.0, 120.0)
    let body = CadGeometry.model(w.network.evaluate_data(w.network.selected).geometry, 0)
    let box = body.tessellate(8).bounds()
    assert(body.face_count() >= 6 and near(box.maximum.y - box.minimum.y, 10.0) and box.maximum.z - box.minimum.z > 9.0)

## Project: a 20 mm box into a Sketch on the ground, 30 up; its side at
## x 10 clicked brings in a fixed line linked to that edge; the box made
## 30 wide, the line follows to x 15.
func projected_edge(editor: Luce3D) -> unit!:
    let w = editor.workspace
    node_commands.add_node(w, "Solid Box", 600.0, 0.0)
    let box = w.network.selected
    node_commands.add_node(w, "Sketch", 600.0, 120.0)
    let id = w.network.selected
    node_commands.set_parameter(w, 1, 30.0)
    node_commands.display_selected(w)
    w.view.yaw = 0.0
    w.view.pitch = 1.5
    w.view.distance = 120.0
    w.view.target = Vector3(0.0, 30.0, 0.0)
    w.view.update_camera()
    editor.app.layout(1600.0, 1000.0)
    let e = editor.panels.view.sketch.editor
    e.sync()
    e.choose("Project")
    click(editor, 10.0, 3.0)
    var s = sketch_of(editor, id)
    assert(s.link_count() == 1 and s.kind(0) == point_kind)
    let line = last_of(s, line_kind)
    assert(line >= 0 and near(math.abs(s.x(s.part(line, 0))), 10.0))
    node_commands.select_node(w, box)
    node_commands.set_parameter(w, 0, 30.0)
    let bounds = w.network.evaluate_data(id).geometry.bounds()
    assert(near(bounds.maximum.x, 15.0))