dymokomi/luce-cad
Source of release 0.9.2
0.9.2 / tests / regressions / model_api_tests.luc
## CadModel's own API: a subset keeps the chosen B-rep faces and the
## placement; a mirrored placement keeps a solid closed and outward with
## its volume; face paths come through from the B-rep; tessellation and the
## pooled preview report progress up to 1.
from luce_cad.model import CadModel, BrepModel
from luce_cad.solids import CadSolids
from luce_geocore.geocore import Vector3, Matrix4
from checks import near, manifold, volume
pub func model_api_tests() -> unit!:
let box = CadSolids.box(2.0, 1.0, 3.0)
# Three faces of six, placed as the box was.
let moved = box.transformed(Vector3(5.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let part = moved.subset([0, 2, 4])
assert(part.face_count() == 3)
assert(part.tessellate(8).bounds().minimum.x >= 4.0 - 1e-9)
# Mirrored in x: still closed, outward, the same volume.
let mirrored = box.placed(Matrix4.compose(Vector3(), Vector3(), Vector3(-1.0, 1.0, 1.0)))
let mesh = mirrored.tessellate(8)
manifold(mesh)
near(volume(mesh), 6.0)
# A face's path, set on the B-rep.
let b = BrepModel()
let corners = [Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(1.0, 1.0, 0.0), Vector3(0.0, 1.0, 0.0)]
for p in corners: _ = b.add_vertex(p)
for at in 0..<4: _ = b.add_line(at, (at + 1) % 4)
_ = b.add_surface(0, Vector3(), Vector3(0.0, 0.0, 1.0), Vector3(1.0, 0.0, 0.0))
_ = b.add_face(0, [1, 2, 3, 4], [4])
b.set_face_path(0, "/parts/square")
let square = CadModel()
square.attach_brep(b)
assert(square.face_path(0) == "/parts/square")
# Progress up to 1, never backwards.
var last = 0.0
var calls = 0
let report = func(fraction: float) -> unit!:
assert(fraction >= last - 1e-12 and fraction <= 1.0 + 1e-12)
last = fraction
calls += 1
_ = CadSolids.sphere(1.0).tessellate(16, 0.0, report)
assert(calls > 0)
near(last, 1.0)
last = 0.0
_ = box.preview(report)
near(last, 1.0)
print("PASS model API: subsets, mirrored placement, face paths, progress")