dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / shadow_face_tests.luc
## Fusion's Project of a whole face and of a body, from a cast carried as
## text (sketch/shadow_faces.lucb), and its sketch Slice (CadModeling
## .sliced): a box's top face found under a point (over its bottom),
## projected as one closed loop of linked lines; the box's outline seen
## from above, the same loop once; the box sliced through its middle,
## capped, half its volume left behind the plane. Silhouettes, as Fusion's
## Project brings them: a cylinder seen from its side outlined by its two
## silhouette lines and its rims seen edge-on (lines), one loop, followed
## when the cylinder grows; a ball outlined by its silhouette's half circles.
from luce_cad.sketch import CadSketch, CadProjection, CadShadows
from luce_cad.modeling import CadModeling
from luce_cad.solids import CadSolids
from luce_geocore.geocore import Vector3
from checks import manifold, volume
from luce_std import math
pub func shadow_face_tests() -> unit!:
let box = CadModeling.tagged(CadSolids.box(4.0, 2.0, 2.0), "box")
# The plane y = 0 seen from above: x along u, -z along v.
let u = Vector3(1.0, 0.0, 0.0)
let v = Vector3(0.0, 0.0, -1.0)
let shadows = CadShadows.parse(CadShadows.text_of(CadProjection.of(box, Vector3(0.0, 0.0, 0.0), u, v)))
assert(shadows.face_count() == 6)
let top = shadows.face_at(0.5, 0.2, 1.0)
let bottom = shadows.face_at(0.5, 0.2, -1.0)
assert(top >= 0 and bottom >= 0 and top != bottom)
let frame = CadModeling.face_frame(box, shadows.face_name(top))
assert(frame.found and frame.u.cross(frame.v).y > 0.99)
assert(shadows.face_at(3.0, 0.0, 1.0) < 0)
# The face: four linked lines sharing their corners, remembered.
let s = CadSketch.parse("")
assert(s.project_shadow_face(shadows, top) == 4)
assert(s.entity_count() == 8 and s.link_count() == 4 and s.projects_face(shadows.face_name(top)))
# Again: nothing new.
assert(s.project_shadow_face(shadows, top) == 0)
# The outline from above: the top's four edges (the bottom's cast the
# same shadows), one loop.
let o = CadSketch.parse("")
assert(o.project_shadow_outline(shadows, 1.0) == 4, f"{o.entity_count()}")
assert(o.entity_count() == 8)
var low = 1.0e9
var high = -1.0e9
for at in 0..<o.entity_count():
if o.kind(at) == 0:
low = math.min(low, o.x(at))
high = math.max(high, o.x(at))
assert(math.abs(low + 2.0) < 1e-6 and math.abs(high - 2.0) < 1e-6)
# Sliced at y = 0.3: what is below, capped (a face named for the
# slice), 4 x 2 x 1.3.
let sliced = CadModeling.sliced(box, Vector3(0.0, 0.3, 0.0), Vector3(0.0, 1.0, 0.0), "slice")
let mesh = sliced.tessellate(16)
manifold(mesh)
assert(math.abs(volume(mesh) - 10.4) < 1e-5, f"{volume(mesh)}")
var capped = false
for f in 0..<sliced.face_count():
capped = capped or CadModeling.face_name_of(sliced, f).starts_with("slice")
assert(capped)
# Wholly behind: the box itself; wholly in front: refused.
assert(CadModeling.sliced(box, Vector3(0.0, 5.0, 0.0), Vector3(0.0, 1.0, 0.0), "slice").face_count() == 6)
var refused = false
_ = CadModeling.sliced(box, Vector3(0.0, -5.0, 0.0), Vector3(0.0, 1.0, 0.0), "slice") catch failure:
refused = true
recover box
assert(refused)
silhouettes()
print("PASS Project of a face and a body, and Slice: a box's top found over its bottom and projected as one linked loop, its outline once, and the box sliced and capped with half its volume behind the plane; a cylinder's side-on outline by its silhouettes and edge-on rims, relinked as it grows; a ball's silhouette half circles")
## A cylinder (radius 1, 2 tall along y) seen from its side (the plane
## z = 0): its outline a rectangle, the silhouettes x = ±1 and the rims
## y = ±1, four linked lines in one loop; grown to radius 1.5 and
## relinked, the silhouettes follow. A ball of radius 1 (two half balls)
## seen across its equator: its silhouette, two half circles of radius 1.
func silhouettes() -> unit!:
let u = Vector3(1.0, 0.0, 0.0)
let v = Vector3(0.0, 1.0, 0.0)
let pin = CadModeling.tagged(CadSolids.cylinder(1.0, 2.0), "pin")
let shadows = CadShadows.parse(CadShadows.text_of(CadProjection.of(pin, Vector3(), u, v)))
let s = CadSketch.parse("")
assert(s.project_shadow_outline(shadows, 1.0) == 4, f"{s.entity_count()}")
assert(s.link_count() == 4 and s.entity_count() == 8)
box_of(s, 1.0, 1.0)
let wider = CadModeling.tagged(CadSolids.cylinder(1.5, 2.0), "pin")
assert(s.relink(wider, Vector3(), u, v) == 0)
box_of(s, 1.5, 1.0)
let ball = CadModeling.tagged(CadSolids.sphere(1.0), "ball")
let round = CadSketch.parse("")
assert(round.project_shadow_outline(CadShadows.parse(CadShadows.text_of(CadProjection.of(ball, Vector3(), u, v))), 1.0) == 2)
var arcs = 0
for at in 0..<round.entity_count():
if round.kind(at) == 3:
arcs += 1
assert(math.abs(round.radius(at) - 1.0) < 1e-6)
assert(arcs == 2)
## The sketch's points span x from -`x` to `x` and y from -`y` to `y`, each
## at a corner.
func box_of(s: CadSketch, x: float, y: float) -> unit!:
for at in 0..<s.entity_count():
if s.kind(at) == 0:
assert(math.abs(math.abs(s.x(at)) - x) < 1e-6 and math.abs(math.abs(s.y(at)) - y) < 1e-6, f"{s.x(at)}, {s.y(at)}")