dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / contracts / sketch_modify_contract.lucb
## The sketch's MODIFY tools (sketch/modify.lucb): Trim (an arm past a
## crossing, a middle piece, a circle down to an arc), Extend (a line on to
## the next one), Break (a line in two), Fillet and Chamfer (a square corner,
## then solved: the fillet's arc tangent at its radius), Offset (an L, its
## copies joined), Move/Copy and Scale.
from luce_std import math
from luce_geocore.geocore import invalid
from luce_geocore.core.growing import Growing
from sketch import snap_in, snap_crossing, snap_middle, snap_point, snap_curve, Sketch, new_sketch, solve_sketch, ConstraintKind, EntityKind, trim_curve, extend_curve, break_curve, fillet_corner, chamfer_corner, offset_chain, move_entities, scale_entities, write_sketch, read_sketch, mirror_entities, pattern_round, pattern_along
pub func sketch_modify_contracts() -> !:
try trims()
try extends_and_breaks()
try corners()
try offsets_moves_scales()
try offsets_oriented()
try mirrors_patterns()
try free_form_modify()
try g3_between_lines()
try g3_editor_case()
try snaps()
## Lines crossing like an X at (5, 5); a line crossed twice; a circle cut by
## a line.
func trims() -> !:
var s = new_sketch()
defer s.close()
let a = try s.add_line(try s.add_point(0.0, 0.0), try s.add_point(10.0, 10.0))
let b = try s.add_line(try s.add_point(0.0, 10.0), try s.add_point(10.0, 0.0))
# The arm of `a` past the crossing, picked near (8, 8), goes.
try trim_curve(&s, a, 8.0, 8.0)
let end = s.entity(a).second
try near(s.x(end), 5.0)
try near(s.y(end), 5.0)
if not has_on_curve(&s, end, b): error(invalid, "a trimmed end does not stay on the curve it was cut at")
# A line crossed at x 3 and x 7: its middle goes, two collinear pieces
# stay.
var m = new_sketch()
defer m.close()
let line = try m.add_line(try m.add_point(0.0, 0.0), try m.add_point(10.0, 0.0))
_ = try m.add_line(try m.add_point(3.0, -1.0), try m.add_point(3.0, 1.0))
_ = try m.add_line(try m.add_point(7.0, -1.0), try m.add_point(7.0, 1.0))
let lines_before = count_of(&m, EntityKind.line)
try trim_curve(&m, line, 5.0, 0.0)
if count_of(&m, EntityKind.line) != lines_before + 1: error(invalid, "trimming a middle piece does not leave two pieces")
try near(m.x(m.entity(line).second), 3.0)
# A circle crossed by a line: the arc left runs round the other side.
var c = new_sketch()
defer c.close()
let circle = try c.add_circle(try c.add_point(0.0, 0.0), 5.0)
_ = try c.add_line(try c.add_point(-10.0, 3.0), try c.add_point(10.0, 3.0))
try trim_curve(&c, circle, 0.0, 5.0)
if count_of(&c, EntityKind.circle) != 0 or count_of(&c, EntityKind.arc) != 1: error(invalid, "a trimmed circle is not an arc")
## A line stopping short of an upright: extended to it; then broken where
## another crosses it.
func extends_and_breaks() -> !:
var s = new_sketch()
defer s.close()
let short = try s.add_line(try s.add_point(0.0, 0.0), try s.add_point(4.0, 0.0))
_ = try s.add_line(try s.add_point(10.0, -5.0), try s.add_point(10.0, 5.0))
try extend_curve(&s, short, 3.5, 0.0)
try near(s.x(s.entity(short).second), 10.0)
_ = try s.add_line(try s.add_point(6.0, -5.0), try s.add_point(6.0, 5.0))
let lines = count_of(&s, EntityKind.line)
try break_curve(&s, short, 8.0, 0.0)
if count_of(&s, EntityKind.line) <= lines: error(invalid, "a broken line is not in pieces")
## A square corner at the origin: a fillet of 2 (lines shortened by 2, the
## arc tangent at radius 2 after a solve), and a chamfer of 1.
func corners() -> !:
var s = new_sketch()
defer s.close()
let corner = try s.add_point(0.0, 0.0)
let a = try s.add_line(corner, try s.add_point(10.0, 0.0))
let b = try s.add_line(corner, try s.add_point(0.0, 10.0))
let arc = try fillet_corner(&s, a, b, 2.0)
let solved = try solve_sketch(&s)
if not solved.converged: error(invalid, "a filleted corner does not solve")
try near(s.radius_of(arc), 2.0)
let ea = s.entity(arc)
try near(math.hypot(s.x(ea.third) - s.x(ea.first), s.y(ea.third) - s.y(ea.first)), 2.0)
if count_of(&s, EntityKind.point) != 5: error(invalid, "a fillet left the corner point behind")
# Its lines now end at points made after them: the text reads back.
var text = Growing[u8]()
defer text.close()
try write_sketch(&s, &text)
var back = try read_sketch((str)text.view())
defer back.close()
var again = Growing[u8]()
defer again.close()
try write_sketch(&back, &again)
if (str)text.view() != (str)again.view(): error(invalid, "a filleted sketch does not read back from its text")
var c = new_sketch()
defer c.close()
let k = try c.add_point(0.0, 0.0)
let p = try c.add_line(k, try c.add_point(10.0, 0.0))
let q = try c.add_line(k, try c.add_point(0.0, 10.0))
let cut = try chamfer_corner(&c, p, q, 1.0)
let e = c.entity(cut)
try near(math.hypot(c.x(e.second) - c.x(e.first), c.y(e.second) - c.y(e.first)), math.sqrt(2.0))
## An L of two lines offset 1 inward: its copies meet at (1, 1); a line
## copied 5 up; a circle scaled 2 about its center.
func offsets_moves_scales() -> !:
var s = new_sketch()
defer s.close()
let corner = try s.add_point(0.0, 0.0)
let a = try s.add_line(try s.add_point(0.0, 10.0), corner)
let b = try s.add_line(corner, try s.add_point(10.0, 0.0))
var made = Growing[i64]()
defer made.close()
try offset_chain(&s, [a, b], 1.0, 3.0, 3.0, &made)
if made.count != 2: error(invalid, "an offset L is not two lines")
let joint = s.entity(made.items[0]).second
if joint != s.entity(made.items[1]).first: error(invalid, "an offset L's copies do not share their corner")
try near(s.x(joint), 1.0)
try near(s.y(joint), 1.0)
var copies = Growing[i64]()
defer copies.close()
try move_entities(&s, [b], 0.0, 5.0, true, &copies)
if copies.count != 1: error(invalid, "a copied line is not one line")
try near(s.y(s.entity(copies.items[0]).first), 5.0)
let circle = try s.add_circle(try s.add_point(20.0, 0.0), 3.0)
try scale_entities(&s, [circle], 20.0, 0.0, 2.0)
try near(s.radius_of(circle), 6.0)
## Offsets whatever way the chain's curves run: a square of lines drawn
## every which way, grown 1 outward (still so once solved); a line, an
## arc and a reversed line shrunk 1 inward; a lone circle, its concentric
## copy and no stray points.
func offsets_oriented() -> !:
var s = new_sketch()
defer s.close()
let p0 = try s.add_point(0.0, 0.0)
let p1 = try s.add_point(10.0, 0.0)
let p2 = try s.add_point(10.0, 10.0)
let p3 = try s.add_point(0.0, 10.0)
let square: i64[4] = [try s.add_line(p0, p1), try s.add_line(p2, p1), try s.add_line(p2, p3), try s.add_line(p0, p3)]
var made = Growing[i64]()
defer made.close()
try offset_chain(&s, square[0..], 1.0, 20.0, 5.0, &made)
if made.count != 4: error(invalid, "an offset square is not four lines")
_ = try solve_sketch(&s)
for line in made.view():
let e = s.entity(line)
for p in [e.first, e.second]:
try near(math.abs(s.x(p) - 5.0), 6.0)
try near(math.abs(s.y(p) - 5.0), 6.0)
var t = new_sketch()
defer t.close()
let a0 = try t.add_point(0.0, 0.0)
let a1 = try t.add_point(10.0, 0.0)
let a2 = try t.add_point(10.0, 10.0)
let bottom = try t.add_line(a0, a1)
let round = try t.add_arc(try t.add_point(10.0, 5.0), a1, a2)
let top = try t.add_line(try t.add_point(0.0, 10.0), a2)
made.clear()
try offset_chain(&t, [bottom, round, top], 1.0, 5.0, 5.0, &made)
if made.count != 3 or t.entity(made.items[1]).kind != EntityKind.arc: error(invalid, "an offset line, arc, line is not that")
try near(t.radius_of(made.items[1]), 4.0)
let low = t.entity(made.items[0]).second
try near(t.x(low), 10.0)
try near(t.y(low), 1.0)
let high = t.entity(made.items[2]).first
try near(t.x(high), 10.0)
try near(t.y(high), 9.0)
var u = new_sketch()
defer u.close()
let circle = try u.add_circle(try u.add_point(0.0, 0.0), 3.0)
let points = count_of(&u, EntityKind.point)
made.clear()
try offset_chain(&u, [circle], 1.0, 10.0, 0.0, &made)
if made.count != 1 or count_of(&u, EntityKind.point) != points: error(invalid, "an offset circle is not one concentric circle")
try near(u.radius_of(made.items[0]), 4.0)
func has_on_curve(s: const Sketch*, point: i64, curve: i64) -> bool:
for k in s.constraints.view():
if k.kind == ConstraintKind.on_curve and k.first == point and k.second == curve: return true
return false
func count_of(s: const Sketch*, kind: EntityKind) -> usize:
var n: usize = 0
for e in s.entities.view():
if e.kind == kind: n += 1
return n
func near(value: f64, expected: f64) -> !:
if math.abs(value - expected) > 1e-7 * math.max(1.0, math.abs(expected)):
error(invalid, "a modified sketch is not where it should be")
## A line mirrored about x = 5 (its copy held symmetric), solved after the
## original moves; a point patterned round the origin 4 times; a line
## patterned along 3 times.
func mirrors_patterns() -> !:
var s = new_sketch()
defer s.close()
let axis = try s.add_line(try s.add_point(5.0, -10.0), try s.add_point(5.0, 10.0))
let line = try s.add_line(try s.add_point(1.0, 0.0), try s.add_point(2.0, 3.0))
var made = Growing[i64]()
defer made.close()
try mirror_entities(&s, [line], axis, &made)
if made.count != 1: error(invalid, "a mirrored line is not one line")
let copy = s.entity(made.items[0])
try near(s.x(copy.first), 9.0)
try near(s.x(copy.second), 8.0)
var round = Growing[i64]()
defer round.close()
let p = try s.add_point(3.0, 0.0)
try pattern_round(&s, [p], 0.0, 0.0, 4, math.pi * 0.5, &round)
if round.count != 3: error(invalid, "a circular pattern of four is not three copies")
try near(s.y(round.items[0]), 3.0)
var along = Growing[i64]()
defer along.close()
try pattern_along(&s, [line], 0.0, 5.0, 3, &along)
try near(s.y(s.entity(along.items[1]).first), 10.0)
## A line crossing a spline: Trim on its far piece ends it on the spline;
## an open spline offset 2 is a fit spline 2 away, and a line's offset
## holds its distance as a dimension; a quintic of eight control points
## held G3 to a line at its start and an arc at its end solves, its
## curvature not changing at the arc.
func free_form_modify() -> !:
var s = new_sketch()
defer s.close()
var points = Growing[i64]()
defer points.close()
for (x, y) in [(0.0, 0.0), (10.0, 4.0), (20.0, -4.0), (30.0, 0.0)]:
try points.push(try s.add_point(x, y))
let spline = try s.add_spline(points.view(), true)
let line = try s.add_line(try s.add_point(15.0, -20.0), try s.add_point(15.0, 20.0))
try trim_curve(&s, line, 15.0, 15.0)
let end = s.entity(line).second
if math.abs(s.x(end) - 15.0) > 1e-9 or math.abs(s.y(end)) > 5.0: error(invalid, "a line trimmed at a spline does not end on it")
var made = Growing[i64]()
defer made.close()
try offset_chain(&s, [spline], 2.0, 15.0, 10.0, &made)
if made.count != 1 or s.entity(made.items[0]).kind != EntityKind.spline: error(invalid, "an offset spline is not a spline")
var lines = Growing[i64]()
defer lines.close()
let before = s.constraints.count
try offset_chain(&s, [line], 3.0, 20.0, 0.0, &lines)
var dimensioned = false
for at in before..<s.constraints.count:
let k = s.constraints.items[at]
if k.kind == ConstraintKind.line_distance and k.value == 3.0: dimensioned = true
if not dimensioned: error(invalid, "a line's offset has no distance dimension")
# G3: a line along y = 0 into the spline, an arc out of it.
var g = new_sketch()
defer g.close()
let a0 = try g.add_point(-10.0, 0.0)
let p0 = try g.add_point(0.0, 0.0)
let along = try g.add_line(a0, p0)
_ = try g.constrain(ConstraintKind.fix, along)
let center = try g.add_point(40.0, 10.0)
let q = try g.add_point(40.0, 0.0)
let arc_end = try g.add_point(50.0, 10.0)
let arc = try g.add_arc(center, q, arc_end)
_ = try g.constrain(ConstraintKind.fix, arc)
var cvs = Growing[i64]()
defer cvs.close()
try cvs.push(p0)
for (x, y) in [(5.0, 0.5), (10.0, 0.5), (15.0, 0.5), (25.0, -0.5), (30.0, -0.5), (35.0, -0.5)]:
try cvs.push(try g.add_point(x, y))
try cvs.push(q)
let blend = try g.add_spline(cvs.view(), false)
_ = try g.constrain(ConstraintKind.smooth, blend, along, side = 1.0)
_ = try g.constrain(ConstraintKind.smooth, blend, arc, value = -1.0, side = -1.0)
let solved = try solve_sketch(&g)
if not solved.converged: error(invalid, "a G3 blend does not solve")
# Its first four control points on the line's line.
let e = g.entity(blend)
for k in 0..<(i64)4:
if math.abs(g.y(g.spline_point(e, k))) > 1e-6: error(invalid, "a G3 blend's start is not along its line")
func g3_between_lines() -> !:
var g = new_sketch()
defer g.close()
let a = try g.add_line(try g.add_point(0.0, 0.0), try g.add_point(20.0, 0.0))
let b = try g.add_line(try g.add_point(40.0, 10.0), try g.add_point(60.0, 10.0))
var cvs = Growing[i64]()
defer cvs.close()
try cvs.push(g.entity(a).second)
for (x, y) in [(23.0, 0.0), (26.0, 0.0), (29.0, 0.0), (31.0, 10.0), (34.0, 10.0), (37.0, 10.0)]:
try cvs.push(try g.add_point(x, y))
try cvs.push(g.entity(b).first)
let blend = try g.add_spline(cvs.view(), false)
_ = try g.constrain(ConstraintKind.smooth, blend, a, value = 1.0, side = 1.0)
_ = try g.constrain(ConstraintKind.smooth, blend, b, value = 1.0, side = -1.0)
let solved = try solve_sketch(&g)
if not solved.converged: error(invalid, "a G3 blend between lines does not solve")
func g3_editor_case() -> !:
var g = new_sketch()
defer g.close()
let a = try g.add_line(try g.add_point(100.0, -100.0), try g.add_point(120.0, -100.0))
_ = try g.constrain(ConstraintKind.horizontal, a)
let b = try g.add_line(try g.add_point(140.0, -90.0), try g.add_point(160.0, -90.0))
_ = try g.constrain(ConstraintKind.horizontal, b)
let span = math.hypot(20.0, 10.0)
var cvs = Growing[i64]()
defer cvs.close()
try cvs.push(g.entity(a).second)
for reach in [0.15, 0.3, 0.45]: try cvs.push(try g.add_point(120.0 + span * reach, -100.0))
for reach in [0.45, 0.3, 0.15]: try cvs.push(try g.add_point(140.0 - span * reach, -90.0))
try cvs.push(g.entity(b).first)
let blend = try g.add_spline(cvs.view(), false)
_ = try g.constrain(ConstraintKind.smooth, blend, a, value = 1.0, side = 1.0)
_ = try g.constrain(ConstraintKind.smooth, blend, b, value = 1.0, side = -1.0)
let solved = try solve_sketch(&g)
if not solved.converged: error(invalid, "a G3 blend between level lines does not solve")
## Snapping in a sketch: near two lines' crossing, the crossing (both
## curves named); near a line's middle, the middle; near an end, the point;
## a kind left out of the mask is passed over.
func snaps() -> !:
var s = new_sketch()
defer s.close()
let a = try s.add_line(try s.add_point(0.0, 0.0), try s.add_point(40.0, 30.0))
let b = try s.add_line(try s.add_point(0.0, 30.0), try s.add_point(40.0, 0.0))
var hit = try snap_in(&s, 20.3, 15.2, 1.0, 15, -1)
if hit.kind != snap_crossing or math.abs(hit.x - 20.0) > 1e-9 or math.abs(hit.y - 15.0) > 1e-9 or hit.other < 0: error(invalid, "a snap near a crossing is not the crossing")
hit = try snap_in(&s, 30.2, 22.4, 1.0, 15, -1)
if hit.kind != snap_curve: error(invalid, "a snap on a line away from its points is not on it")
let c = try s.add_line(try s.add_point(0.0, -10.0), try s.add_point(20.0, -10.0))
hit = try snap_in(&s, 10.3, -9.8, 1.0, 15, -1)
if hit.kind != snap_middle or hit.entity != c: error(invalid, "a snap near a line's middle is not its middle")
hit = try snap_in(&s, 0.3, 0.2, 1.0, 15, -1)
if hit.kind != snap_point: error(invalid, "a snap near a line's end is not its point")
hit = try snap_in(&s, 20.3, 15.2, 1.0, 8, -1)
if hit.kind != snap_curve: error(invalid, "a crossing left out still snaps there")
if hit.entity != a and hit.entity != b: error(invalid, "a snap on two lines is on neither")
# A spline made before the line it crosses still crosses it.
var t = new_sketch()
defer t.close()
let p0 = try t.add_point(0.0, -5.0)
let p1 = try t.add_point(5.0, 0.0)
let p2 = try t.add_point(10.0, 5.0)
_ = try t.add_spline([p0, p1, p2], true, degree = 2)
_ = try t.add_line(try t.add_point(0.0, 0.0), try t.add_point(10.0, 0.0))
hit = try snap_in(&t, 5.2, 0.3, 1.0, snap_crossing | snap_curve, -1)
if hit.kind != snap_crossing: error(invalid, "a spline made first does not snap at its crossing")
# A dragged point skips its coincident twin; an arc snaps at its middle.
let q = try t.add_point(30.0, 0.0)
let twin = try t.add_point(30.0, 0.0)
_ = try t.constrain(ConstraintKind.coincident, q, twin)
hit = try snap_in(&t, 30.1, 0.1, 1.0, 1, q)
if hit.kind != 0: error(invalid, "a dragged point snaps to its coincident twin")
let round = try t.add_arc(try t.add_point(60.0, 0.0), try t.add_point(65.0, 0.0), try t.add_point(55.0, 0.0))
hit = try snap_in(&t, 60.1, 4.8, 1.0, 2, -1)
if hit.kind != snap_middle or hit.entity != round or math.abs(hit.y - 5.0) > 1e-9: error(invalid, "a snap near an arc's middle is not its middle")