dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / contracts / sketch_contract.lucb
## The sketch solver (sketch/) and its freedom (freedom.lucb): every row's partials against central
## finite differences at scattered configurations, then sketches solved:
## a rectangle held by horizontals, verticals, a fixed corner and two
## dimensions lands exactly; a dimensioned line keeps its length while its
## end is dragged; a line tangent to a circle; an arc's ends stay on its
## circle; points mirrored about a line; and two fixes of one point to
## different places are named as a conflict.
from luce_std import math
from luce_geocore.geocore import invalid
from luce_geocore.core.growing import Growing
from sketch import Sketch, new_sketch, solve_sketch, drag_sketch, freedom_of, write_sketch, read_sketch, ConstraintKind, Row, rows_of, evaluate
pub func sketch_contracts() -> !:
try partials()
try rectangle()
try dragged_line()
try tangent_circle()
try arc_ends()
try mirrored()
try conflicting()
try freedom()
try growing_components()
try round_trip()
try read_checks()
try fits_checks()
## Values read straight from the sketch's parameters.
func read_param(param: i64, context: void*) -> f64:
let s = (const Sketch*)context
return s.params.items[(usize)param]
## Every kind of row: analytic partials match central differences.
func partials() -> !:
var s = new_sketch()
defer s.close()
let a = try s.add_point(0.3, -0.2)
let b = try s.add_point(2.1, 0.7)
let c = try s.add_point(-0.4, 1.9)
let d = try s.add_point(1.7, 2.6)
let p = try s.add_point(0.9, 1.1)
let q = try s.add_point(1.6, -0.8)
let first = try s.add_line(a, b)
let second = try s.add_line(c, d)
let center = try s.add_point(0.5, 0.4)
let circle = try s.add_circle(center, 0.8)
let hub = try s.add_point(2.4, 1.2)
let other = try s.add_circle(hub, 0.5)
let arc = try s.add_arc(center, p, q)
_ = try s.constrain(ConstraintKind.on_curve, p, first)
_ = try s.constrain(ConstraintKind.on_curve, q, circle)
_ = try s.constrain(ConstraintKind.parallel, first, second)
_ = try s.constrain(ConstraintKind.perpendicular, first, second)
_ = try s.constrain(ConstraintKind.angle, first, second, value = 0.6)
_ = try s.constrain(ConstraintKind.equal, first, second)
_ = try s.constrain(ConstraintKind.tangent, first, circle, side = -1.0)
_ = try s.constrain(ConstraintKind.tangent, circle, other, side = 1.0)
_ = try s.constrain(ConstraintKind.tangent, arc, other, side = -1.0)
_ = try s.constrain(ConstraintKind.midpoint, p, second)
_ = try s.constrain(ConstraintKind.collinear, first, second)
_ = try s.constrain(ConstraintKind.symmetric, p, q, first)
_ = try s.constrain(ConstraintKind.distance, p, q, value = 1.3)
_ = try s.constrain(ConstraintKind.line_distance, q, second, value = 0.4, side = -1.0)
_ = try s.constrain(ConstraintKind.horizontal_distance, p, q, value = 0.9)
_ = try s.constrain(ConstraintKind.vertical_distance, p, q, value = 0.2, side = -1.0)
var rows = Growing[Row]()
defer rows.close()
try rows_of(&s, &rows)
if rows.count < 18: error(invalid, "the sketch made too few rows")
let h = 1e-6
for r in rows.view():
let e = evaluate(&r, read_param, (void*)&s, 2.0)
for i in 0..<(usize)8:
let param = r.at[i]
if param < 0: continue
let kept = s.params.items[(usize)param]
s.params.items[(usize)param] = kept + h
let up = evaluate(&r, read_param, (void*)&s, 2.0).residual
s.params.items[(usize)param] = kept - h
let down = evaluate(&r, read_param, (void*)&s, 2.0).residual
s.params.items[(usize)param] = kept
# A parameter read twice by a row has its partials summed.
var analytic = 0.0
for k in 0..<(usize)8:
if r.at[k] == param: analytic += e.partial[k]
if math.abs((up - down) / (2.0 * h) - analytic) > 1e-6 * math.max(1.0, math.abs(analytic)):
error(invalid, "a sketch row's partial is not its residual's slope")
## A 30 by 20 rectangle from a rough one: corner fixed at the origin.
func rectangle() -> !:
var s = new_sketch()
defer s.close()
let a = try s.add_point(0.0, 0.0)
let b = try s.add_point(28.0, 1.5)
let c = try s.add_point(31.0, 22.0)
let d = try s.add_point(-1.0, 19.0)
let bottom = try s.add_line(a, b)
let right = try s.add_line(b, c)
let top = try s.add_line(c, d)
let left = try s.add_line(d, a)
_ = try s.constrain(ConstraintKind.horizontal, bottom)
_ = try s.constrain(ConstraintKind.horizontal, top)
_ = try s.constrain(ConstraintKind.vertical, right)
_ = try s.constrain(ConstraintKind.vertical, left)
_ = try s.constrain(ConstraintKind.fix, a)
_ = try s.constrain(ConstraintKind.distance, a, b, value = 30.0)
_ = try s.constrain(ConstraintKind.distance, b, c, value = 20.0)
let solved = try solve_sketch(&s)
if not solved.converged: error(invalid, "the rectangle did not solve")
try near(s.x(b), 30.0)
try near(s.y(b), 0.0)
try near(s.x(c), 30.0)
try near(s.y(c), 20.0)
try near(s.x(d), 0.0)
try near(s.y(d), 20.0)
## A 10 mm line from a fixed start: its end follows a drag at that length.
func dragged_line() -> !:
var s = new_sketch()
defer s.close()
let a = try s.add_point(0.0, 0.0)
let b = try s.add_point(10.0, 0.0)
_ = try s.add_line(a, b)
_ = try s.constrain(ConstraintKind.fix, a)
_ = try s.constrain(ConstraintKind.distance, a, b, value = 10.0)
# Drag the end toward (6, 6): it lands on the circle, toward the cursor.
let x = s.entity(b).first
let solved = try drag_sketch(&s, [x, x + 1], [6.0, 6.0])
if not solved.converged: error(invalid, "the dragged line did not solve")
try near(math.hypot(s.x(b), s.y(b)), 10.0)
try near(s.x(b) / s.y(b), 1.0)
## A horizontal line tangent to a circle of radius 5 about (0, 0) from
## below: it ends up at y = -5.
func tangent_circle() -> !:
var s = new_sketch()
defer s.close()
let center = try s.add_point(0.0, 0.0)
let circle = try s.add_circle(center, 5.0)
let a = try s.add_point(-8.0, -3.0)
let b = try s.add_point(8.0, -4.0)
let line = try s.add_line(a, b)
_ = try s.constrain(ConstraintKind.fix, circle)
_ = try s.constrain(ConstraintKind.horizontal, line)
# The center is on the line's left (above it), so side +1.
_ = try s.constrain(ConstraintKind.tangent, line, circle, side = 1.0)
let solved = try solve_sketch(&s)
if not solved.converged: error(invalid, "the tangent line did not solve")
try near(s.y(a), -5.0)
try near(s.y(b), -5.0)
## An arc's radius dimension: both ends move onto the new circle.
func arc_ends() -> !:
var s = new_sketch()
defer s.close()
let center = try s.add_point(0.0, 0.0)
let start = try s.add_point(4.0, 0.0)
let end = try s.add_point(0.0, 4.2)
let arc = try s.add_arc(center, start, end)
_ = try s.constrain(ConstraintKind.fix, center)
_ = try s.constrain(ConstraintKind.radius, arc, value = 6.0)
let solved = try solve_sketch(&s)
if not solved.converged: error(invalid, "the arc did not solve")
try near(math.hypot(s.x(start), s.y(start)), 6.0)
try near(math.hypot(s.x(end), s.y(end)), 6.0)
## Point q mirrored about the vertical line x = 2.
func mirrored() -> !:
var s = new_sketch()
defer s.close()
let a = try s.add_point(2.0, 0.0)
let b = try s.add_point(2.0, 5.0)
let axis = try s.add_line(a, b)
let p = try s.add_point(0.0, 1.0)
let q = try s.add_point(3.0, 2.0)
_ = try s.constrain(ConstraintKind.fix, axis)
_ = try s.constrain(ConstraintKind.fix, p)
_ = try s.constrain(ConstraintKind.symmetric, p, q, axis)
let solved = try solve_sketch(&s)
if not solved.converged: error(invalid, "the mirror did not solve")
try near(s.x(q), 4.0)
try near(s.y(q), 1.0)
## A point fixed, then its radius-free twin fixed elsewhere and made
## coincident: the conflict is named.
func conflicting() -> !:
var s = new_sketch()
defer s.close()
let a = try s.add_point(0.0, 0.0)
let b = try s.add_point(5.0, 0.0)
_ = try s.constrain(ConstraintKind.fix, a)
_ = try s.constrain(ConstraintKind.fix, b)
_ = try s.constrain(ConstraintKind.coincident, a, b)
let solved = try solve_sketch(&s)
if solved.conflicts == 0: error(invalid, "two fixed points made coincident are not a conflict")
func near(value: f64, expected: f64) -> !:
if math.abs(value - expected) > 1e-7 * math.max(1.0, math.abs(expected)):
error(invalid, "a solved sketch is not where its constraints put it")
## A rectangle's freedom: fully constrained with its fix and two
## dimensions (no freedom, every entity fixed); one dimension fewer leaves
## one degree; a third dimension that agrees is redundant, one that does
## not conflicts.
func freedom() -> !:
for case in 0..<(i64)4:
var s = new_sketch()
defer s.close()
let a = try s.add_point(0.0, 0.0)
let b = try s.add_point(30.0, 0.0)
let c = try s.add_point(30.0, 20.0)
let d = try s.add_point(0.0, 20.0)
let bottom = try s.add_line(a, b)
let right = try s.add_line(b, c)
let top = try s.add_line(c, d)
let left = try s.add_line(d, a)
_ = try s.constrain(ConstraintKind.horizontal, bottom)
_ = try s.constrain(ConstraintKind.horizontal, top)
_ = try s.constrain(ConstraintKind.vertical, right)
_ = try s.constrain(ConstraintKind.vertical, left)
_ = try s.constrain(ConstraintKind.fix, a)
_ = try s.constrain(ConstraintKind.distance, a, b, value = 30.0)
if case != 1: _ = try s.constrain(ConstraintKind.distance, b, c, value = 20.0)
if case == 2: _ = try s.constrain(ConstraintKind.distance, a, d, value = 20.0)
if case == 3: _ = try s.constrain(ConstraintKind.distance, a, d, value = 25.0)
if case != 3:
let solved = try solve_sketch(&s)
if not solved.converged: error(invalid, "a rectangle for freedom did not solve")
var f = try freedom_of(&s)
defer f.close()
if case == 0 or case == 2:
if f.degrees != 0: error(invalid, "a fully constrained rectangle has freedom left")
for fixed in f.fixed.view():
if not fixed: error(invalid, "a fully constrained rectangle has an entity not fixed")
if case == 1:
if f.degrees != 1: error(invalid, "a rectangle without its height has not one degree of freedom")
if f.fixed.items[(usize)a] == false or f.fixed.items[(usize)c]: error(invalid, "a rectangle without its height has its corners wrongly fixed")
if case == 2 and f.redundant.count == 0: error(invalid, "a repeated rectangle dimension is not redundant")
if case == 3 and f.conflicting.count == 0: error(invalid, "a disagreeing rectangle dimension is not conflicting")
## A sketch written as text and read back is the same sketch: written
## again, the same text.
func round_trip() -> !:
var s = new_sketch()
defer s.close()
let a = try s.add_point(0.1, -2.5)
let b = try s.add_point(1.0 / 3.0, 7.0)
let c = try s.add_point(4.0, 4.0, true)
let line = try s.add_line(a, b)
let circle = try s.add_circle(c, 2.25)
_ = try s.add_arc(c, a, b)
_ = try s.constrain(ConstraintKind.tangent, line, circle, side = -1.0)
_ = try s.constrain(ConstraintKind.distance, a, b, value = 7.5, driven = true)
var first = Growing[u8]()
defer first.close()
try write_sketch(&s, &first)
var back = try read_sketch((str)first.view())
defer back.close()
var second = Growing[u8]()
defer second.close()
try write_sketch(&back, &second)
if (str)first.view() != (str)second.view(): error(invalid, "a sketch read back from its text writes other text")
if back.entities.count != s.entities.count or back.constraints.count != 2 or not back.constraints.items[1].driven: error(invalid, "a sketch read back from its text lost entities or constraints")
## Components solved one after another, a later one with more rows than
## an earlier: the solver's work arrays grow to fit each.
func growing_components() -> !:
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, 1.0))
let b = try s.add_line(try s.add_point(0.0, 5.0), try s.add_point(10.0, 7.0))
_ = try s.constrain(ConstraintKind.parallel, a, b)
var lines = Growing[i64]()
defer lines.close()
for at in 0..<(usize)8:
let y = 20.0 + 3.0 * (f64)at
try lines.push(try s.add_line(try s.add_point(0.0, y), try s.add_point(10.0, y + 0.5 * (f64)at)))
for at in 1..<lines.count:
_ = try s.constrain(ConstraintKind.parallel, lines.items[0], lines.items[at])
_ = try s.constrain(ConstraintKind.distance, s.entity(lines.items[at]).first, s.entity(lines.items[at]).second, value = 10.0)
let solved = try solve_sketch(&s)
if not solved.converged: error(invalid, "a sketch whose second component is larger does not solve")
## A text that says `c` stays a text that says `c` (not a construction
## mark); a cut escape, a negative radius, a rho past 1, a projection of a
## point and a mark past a line's fields are refused.
func read_checks() -> !:
var s = try read_sketch("p 0 0\nt 0 5 - c\n")
defer s.close()
let e = s.entities.items[1]
if e.construction or s.text_content(e) != "c": error(invalid, "a text that says c reads as a construction mark")
try rejects("p 0 0\nt 0 5 - abc%\n", "a sketch text ending in % reads")
try rejects("p 0 0\no 0 -1\n", "a circle with a negative radius reads")
try rejects("p 0 0\np 1 0\np 0 1\nn 0 1 2 1.5\n", "a conic with rho past 1 reads")
try rejects("p 0 0\nj 0 \n", "a projection without a reference reads")
try rejects("p 0 0\nt 0 5 - a\nj 1 edge\n", "a projection of a text reads")
try rejects("p 0 0 c c\n", "a point with two marks reads")
func rejects(text: str, why: str) -> !:
var s = read_sketch(text) catch failure:
_ = failure
return
s.close()
error(invalid, why)
## Curvature takes a control net of degree 2 and 3 points or more, smooth
## one of degree 3 and 4 points; a line's, an arc's and a text's points
## are listed in order.
func fits_checks() -> !:
var s = new_sketch()
defer s.close()
let line = try s.add_line(try s.add_point(-5.0, 0.0), try s.add_point(0.0, 0.0))
let p0 = try s.add_point(0.0, 0.0)
let p1 = try s.add_point(2.0, 0.0)
let p2 = try s.add_point(4.0, 1.0)
let p3 = try s.add_point(6.0, 3.0)
let straight = try s.add_spline([p0, p1, p2], false, degree = 1)
let three = try s.add_spline([p0, p1, p2], false, degree = 3)
let four = try s.add_spline([p0, p1, p2, p3], false, degree = 3)
try refuses(&s, ConstraintKind.curvature, straight, line, "curvature on a degree 1 net is taken")
_ = try s.constrain(ConstraintKind.curvature, three, line, side = 1.0)
try refuses(&s, ConstraintKind.smooth, three, line, "smooth on a net of 3 points is taken")
_ = try s.constrain(ConstraintKind.smooth, four, line, side = 1.0)
var points = Growing[i64]()
defer points.close()
let arc = try s.add_arc(p0, p1, try s.add_point(0.0, 2.0))
try s.points_of(s.entity(arc), &points)
if points.count != 3 or points.items[0] != p0 or points.items[1] != p1: error(invalid, "an arc's points are not its center, start and end")
if s.point_count(s.entity(p0)) != 0: error(invalid, "a point is drawn on points")
func refuses(s: Sketch*, kind: ConstraintKind, first: i64, second: i64, why: str) -> !:
_ = s.constrain(kind, first, second, side = 1.0) catch failure:
_ = failure
return
error(invalid, why)