dymokomi/luce-cad
Source of release 0.9.2
0.9.2 / tests / regressions / cad_tests.luc
from luce_cad.model import BrepModel, CadModel
from luce_geocore.geocore import Vector3
from checks import near, manifold
pub func cad_tests() -> unit!:
let z = Vector3(0.0, 0.0, 1.0)
let x = Vector3(1.0, 0.0, 0.0)
let outer = [Vector3(), Vector3(8.0, 0.0, 0.0), Vector3(8.0, 8.0, 0.0), Vector3(0.0, 8.0, 0.0)]
let plane = CadModel()
plane.add_planar(outer)
assert(plane.tessellate(4).face_count() == 1)
let grid = plane.tessellate(4, 2.0)
assert(grid.point_count() > 4 and grid.face_count() > 4)
# Hollow cylinder: circles are shared by wall and cap, seam appears twice.
let b = BrepModel()
for p in [Vector3(2.0, 0.0, 0.0), Vector3(2.0, 0.0, 3.0), Vector3(1.0, 0.0, 0.0), Vector3(1.0, 0.0, 3.0)]:
_ = b.add_vertex(p)
_ = b.add_circle(0, 0, Vector3(), z, x, 2.0)
_ = b.add_circle(1, 1, Vector3(0.0, 0.0, 3.0), z, x, 2.0)
_ = b.add_line(0, 1)
_ = b.add_circle(2, 2, Vector3(), z, x, 1.0)
_ = b.add_circle(3, 3, Vector3(0.0, 0.0, 3.0), z, x, 1.0)
_ = b.add_line(2, 3)
let outside = b.add_surface(1, Vector3(), z, x, 2.0)
let inside = b.add_surface(1, Vector3(), z, x, 1.0)
let bottom = b.add_surface(0, Vector3(), z, x)
let top = b.add_surface(0, Vector3(0.0, 0.0, 3.0), z, x)
_ = b.add_face(outside, [1, 3, -2, -3], [4])
_ = b.add_face(inside, [4, 6, -5, -6], [4], false)
_ = b.add_face(bottom, [1, -4], [1, 1], false)
_ = b.add_face(top, [2, -5], [1, 1])
let model = CadModel()
model.attach_brep(b)
let tube = model.tessellate(4)
assert(model.face_count() == 4 and model.edge_count() == 6)
# Shared ring samples are retained; complex caps now gain interior support.
assert(tube.point_count() > 64)
manifold(tube)
assert(tube.ray_face(Vector3(0.0, 0.0, 5.0), Vector3(0.0, 0.0, -1.0)) == -1)
near(tube.ray_distance(Vector3(1.5, 0.0, 5.0), Vector3(0.0, 0.0, -1.0)), 2.0)
let moved = model.transformed(Vector3(4.0, 0.0, 0.0), Vector3(), Vector3(-2.0, 1.0, 1.0)).tessellate(4)
manifold(moved)
near(moved.bounds().maximum.x, 8.0)
near(tube.bounds().maximum.x, 2.0)
let dense = model.tessellate(4, 0.75)
assert(dense.point_count() > tube.point_count())
manifold(dense)
# A ruled tensor-product patch shares its original four corner IDs.
let ruled = BrepModel()
let net = [Vector3(), Vector3(0.0, 0.0, 2.0), Vector3(2.0, 0.0, 0.0), Vector3(2.0, 0.0, 2.0)]
for p in net:
_ = ruled.add_vertex(p)
_ = ruled.add_line(0, 2)
_ = ruled.add_line(2, 3)
_ = ruled.add_line(3, 1)
_ = ruled.add_line(1, 0)
let surface = ruled.add_spline_surface(net, [1.0, 1.0, 1.0, 1.0], 2, 2, 1, 1, [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0])
_ = ruled.add_face(surface, [1, 2, 3, 4], [4])
assert(ruled.mesh(4).face_count() == 1 and ruled.mesh(4).face_size(0) == 4)
let sphere = BrepModel()
_ = sphere.add_vertex(Vector3(0.8660254037844386, 0.0, 0.5))
_ = sphere.add_circle(0, 0, Vector3(0.0, 0.0, 0.5), z, x, 0.8660254037844386)
_ = sphere.add_surface(4, Vector3(), z, x, 1.0)
_ = sphere.add_face(0, [1], [1])
near(sphere.preview_face(0, 4).ray_distance(Vector3(0.0, 0.0, 2.0), Vector3(0.0, 0.0, -1.0)), 1.0)
let torus = BrepModel()
_ = torus.add_vertex(Vector3(4.0, 0.0, 0.0))
_ = torus.add_vertex(Vector3(3.0, 0.0, 1.0))
_ = torus.add_circle(0, 0, Vector3(), z, x, 4.0)
_ = torus.add_circle(1, 1, Vector3(0.0, 0.0, 1.0), z, x, 3.0)
_ = torus.add_circle(0, 1, Vector3(3.0, 0.0, 0.0), Vector3(0.0, -1.0, 0.0), x, 1.0)
_ = torus.add_surface(5, Vector3(), z, x, 3.0, 1.0)
_ = torus.add_face(0, [1, 3, -2, -3], [4])
let torus_preview = torus.preview_face(0, 4)
near(torus_preview.bounds().maximum.x, 4.0)
near(torus_preview.bounds().maximum.z, 1.0)
let ellipse = BrepModel()
_ = ellipse.add_vertex(Vector3(2.0, 0.0, 0.0))
_ = ellipse.add_ellipse(0, 0, Vector3(), z, x, 2.0, 1.0)
_ = ellipse.add_surface(0, Vector3(), z, x)
_ = ellipse.add_face(0, [1], [1])
near(ellipse.preview_face(0, 4).bounds().maximum.y, 1.0)
# Topological endpoints may trim a proper subinterval of a spline curve.
let partial = BrepModel()
for p in [Vector3(), Vector3(2.0, 0.0, 0.0), Vector3(2.0, 2.0, 0.0), Vector3(0.0, 2.0, 0.0)]:
_ = partial.add_vertex(p)
_ = partial.add_spline_edge(0, 1, [Vector3(-2.0, 0.0, 0.0), Vector3(4.0, 0.0, 0.0)], [1.0, 1.0], 1, [0.0, 0.0, 1.0, 1.0], true)
for edge in 1..<4: _ = partial.add_line(edge, (edge + 1) % 4)
_ = partial.add_surface(0, Vector3(), z, x)
_ = partial.add_face(0, [1, 2, 3, 4], [4])
near(partial.face_edge_point(0, 0, 0.5).x, 1.0)
near(partial.mesh(4).bounds().maximum.x, 2.0)
print("PASS CAD stitching, trims, edge-size grids, normals and reflected transforms")