dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / sketch_tests.luc
## Sketches through Luce (sketch/api.lucb), as the editor uses them: a 40
## by 20 mm plate with a 5 mm hole, drawn roughly, constrained and
## dimensioned until nothing is free, solved, kept as text and read back,
## then extruded 10 mm into a solid of the right volume; a dimension
## changed re-solves; a free corner dragged moves, a fixed one does not.
from luce_cad.sketch import CadSketch
from luce_cad.modeling import CadModeling
from luce_geocore.geocore import Vector3
from checks import manifold, volume
from luce_std import math
## ConstraintKind's order (sketch/entities.lucb).
let horizontal = 2
let vertical = 3
let fix = 8
let distance = 13
let horizontal_distance = 15
let vertical_distance = 16
let radius = 18
func near(a: float, b: float) -> bool:
return math.abs(a - b) <= 1e-6 * math.max(1.0, math.abs(b))
pub func sketch_tests() -> unit!:
let s = CadSketch.parse("")
let a = s.add_point(0.0, 0.0)
let b = s.add_point(38.0, 1.0)
let c = s.add_point(41.0, 22.0)
let d = s.add_point(2.0, 19.0)
let lines = [s.add_line(a, b), s.add_line(b, c), s.add_line(c, d), s.add_line(d, a)]
_ = s.constrain(horizontal, lines[0])
_ = s.constrain(horizontal, lines[2])
_ = s.constrain(vertical, lines[1])
_ = s.constrain(vertical, lines[3])
_ = s.constrain(fix, a)
let width = s.constrain(distance, a, b, value = 40.0)
_ = s.constrain(distance, b, c, value = 20.0)
let center = s.add_point(12.0, 9.0)
let hole = s.add_circle(center, 4.0)
_ = s.constrain(radius, hole, value = 5.0)
_ = s.constrain(horizontal_distance, a, center, value = 10.0)
_ = s.constrain(vertical_distance, a, center, value = 10.0)
assert(s.solve())
assert(near(s.x(c), 40.0) and near(s.y(c), 20.0) and near(s.x(center), 10.0) and near(s.radius(hole), 5.0))
s.analyze()
assert(s.degrees() == 0 and s.fixed(c) and s.fixed(hole) and s.conflicting_count() == 0)
# Kept as text and read back: the same sketch.
let back = CadSketch.parse(s.text())
assert(back.text() == s.text() and back.entity_count() == s.entity_count())
# Extruded 10 mm: the plate less the hole.
let body = CadModeling.extruded(back.curves(Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0)), 10.0, 0.0, "e")
let mesh = body.tessellate(32)
manifold(mesh)
let expected = 40.0 * 20.0 * 10.0 - math.pi * 25.0 * 10.0
assert(math.abs(volume(mesh) - expected) < expected * 0.002)
# A dimension changed: solved again, the plate 50 wide.
s.set_value(width, 50.0)
assert(s.solve() and near(s.x(b), 50.0))
# The width dimension removed: the far side drags; the fixed corner
# does not.
s.unconstrain(width)
assert(s.drag(b, 60.0, 5.0) and near(s.x(b), 60.0) and near(s.y(b), 0.0) and near(s.x(c), 60.0))
assert(s.drag(a, 5.0, 5.0) and near(s.x(a), 0.0) and near(s.y(a), 0.0))
s.analyze()
assert(s.degrees() == 1 and not s.fixed(b))
# Picking: the corner itself, else the line under the point.
assert(s.pick(60.2, 0.1, 1.0) == b and s.pick(30.0, 0.3, 1.0) == lines[0] and s.pick(30.0, 9.0, 1.0) < 0)
boxes(s, a, lines, center, hole)
# Removing a line keeps its corners (other lines use them).
s.remove(lines[0])
assert(s.entity_count() == 9 and s.kind(a) == 0)
print("PASS sketch: a plate drawn roughly, constrained and dimensioned to no freedom, solved, kept as text, extruded to its volume; a dimension changed, a corner dragged, picking and boxes (a window holds what lies wholly inside, a crossing what it touches too), a line removed")
## Boxes on the 60 by 20 plate (corner `a` at the origin; `lines` round it
## from a; the hole of radius 5 at (10, 10)): only what lies entirely
## inside a box is held.
func boxes(s: CadSketch, a: int, lines: list[int], center: int, hole: int) -> unit!:
# Round the corner a: the corner alone, the lines from it only touched.
let corner = [-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0]
assert(held(s, corner, 0) == [a])
assert(held(s, corner, 1) == [a] and held(s, corner, 2).length == 0)
# Inside the hole, touching it nowhere: its center alone.
let inner = [9.0, 9.0, 11.0, 9.0, 11.0, 11.0, 9.0, 11.0]
assert(held(s, inner, 0) == [center])
# A box seen in perspective (a quadrilateral, wound the other way)
# over the hole's right side, and one along the bottom line's middle,
# hold nothing; one round the whole hole holds it and its center.
assert(held(s, [14.0, 8.0, 13.5, 11.0, 16.5, 12.0, 16.0, 7.0], 0).length == 0)
assert(held(s, [20.0, 0.0, 30.0, 0.0, 30.0, -5.0, 20.0, -5.0], 0).length == 0)
let round_hole = [4.0, 16.5, 16.0, 16.0, 15.5, 4.0, 4.5, 4.5]
let whole = held(s, round_hole, 0)
assert(whole.length == 2 and center in whole and hole in whole)
assert(held(s, round_hole, 2) == [hole])
# Round the whole plate: everything.
assert(held(s, [-1.0, -1.0, 61.0, -1.0, 61.0, 21.0, -1.0, 21.0], 0).length == s.entity_count())
assert(held(s, [100.0, 100.0, 110.0, 100.0, 110.0, 110.0], 0).length == 0)
# A crossing box (dragged right to left) holds what it touches too: the
# corner and both lines from it; over the hole's right side, the hole
# alone (its center outside); along the bottom line's middle, that line;
# inside the hole, its center but not the circle round it.
let crossed = crossed_by(s, corner)
assert(crossed.length == 3 and a in crossed and lines[0] in crossed and lines[3] in crossed, f"{crossed}")
assert(crossed_by(s, [14.0, 8.0, 13.5, 11.0, 16.5, 12.0, 16.0, 7.0]) == [hole])
assert(crossed_by(s, [20.0, 1.0, 30.0, 1.0, 30.0, -5.0, 20.0, -5.0]) == [lines[0]])
assert(crossed_by(s, inner) == [center])
func crossed_by(s: CadSketch, corners: list[float]) -> list[int]!:
let result: list[int] = []
for at in 0..<s.select_box(corners, 0, crossing = true):
result.append(s.boxed(at))
return result
func held(s: CadSketch, corners: list[float], only: int) -> list[int]!:
let result: list[int] = []
for at in 0..<s.select_box(corners, only):
result.append(s.boxed(at))
return result