Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / solid_tests.luc

## Analytic solids (CadSolids): closed B-reps whose tessellations pass the
## closed-manifold audit, with outward normals, at their sizes, and with
## every point on the analytic surface it belongs to.
from luce_cad.solids import CadSolids
from luce_cad.model import CadModel
from luce_geocore.geocore import Mesh, Vector3
from checks import near, manifold, volume
from luce_std import math

## Closed and consistently wound, with every display triangle outward (the
## mesh's volume, by the divergence theorem, is positive).
func closed(model: CadModel, faces: int) -> Mesh!:
    assert(model.face_count() == faces)
    let mesh = model.tessellate(16)
    manifold(mesh)
    assert(volume(mesh) > 0.0)
    return mesh

pub func solid_tests() -> unit!:
    let box = closed(CadSolids.box(2.0, 1.0, 3.0), 6)
    near(box.bounds().maximum.x, 1.0)
    near(box.bounds().minimum.z, -1.5)
    near(volume(box), 6.0)
    let cylinder = closed(CadSolids.cylinder(1.0, 2.0), 3)
    near(cylinder.bounds().maximum.y, 1.0)
    assert(math.abs(volume(cylinder) - 2.0 * math.pi) < 0.05)
    let frustum = closed(CadSolids.cone(1.0, 2.0, 0.5), 3)
    assert(math.abs(volume(frustum) - math.pi * 2.0 * (1.0 + 0.5 + 0.25) / 3.0) < 0.05)
    let cone = closed(CadSolids.cone(1.0, 2.0), 2)
    assert(math.abs(volume(cone) - math.pi * 2.0 / 3.0) < 0.05)
    let sphere = closed(CadSolids.sphere(1.5), 2)
    for point in 0..<sphere.point_count():
        assert(math.abs(sphere.point(point).length() - 1.5) < 0.0001)
    assert(math.abs(volume(sphere) - 4.0 / 3.0 * math.pi * 3.375) < 0.1)
    let torus = closed(CadSolids.torus(1.0, 0.3), 1)
    for point in 0..<torus.point_count():
        let p = torus.point(point)
        let ring = Vector3(p.x, 0.0, p.z)
        assert(math.abs(p.subtract(ring.normalized()).length() - 0.3) < 0.0001)
    assert(math.abs(volume(torus) - 2.0 * math.pi * math.pi * 0.09) < 0.02)
    # Invalid dimensions are errors, not broken solids.
    var rejected = false
    _ = CadSolids.torus(1.0, 1.5) catch failure:
        rejected = true
    assert(rejected)
    print("PASS analytic solids: box, cylinder, cone, frustum, sphere and torus are closed, outward and on their surfaces")