Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / contracts / meet_contract.lucb

## Where two analytic surfaces meet (modeling/intersect.lucb), case by case
## beyond what whole booleans reach: two spheres and a plane through a
## sphere meet in one circle; one cone or one torus twice is the same
## surface; a cone and a cylinder, and a torus and a sphere, about one
## axis meet in circles; cylinders crossing at an angle have no closed
## form (traced). Every curve lies on both surfaces.
from luce_std import math
from luce_geocore.geocore import Vector3, invalid
from luce_geocore.core.growing import Growing
from geometry import Surface, surface_distance, surface_plane, surface_cylinder, surface_cone, surface_sphere, surface_torus, curve_circle
from modeling import Carrier, meet, meet_curves, meet_same, meet_march

pub func meet_contracts() -> !:
    let z = Vector3(0.0, 0.0, 1.0)
    let x = Vector3(1.0, 0.0, 0.0)
    try circles(surface_torus, Vector3(), x, z, 2.0, 0.5, surface_sphere, Vector3(), x, z, 2.0, 0.0, 2, "a torus and a sphere about one axis met in the wrong curves")
    try circles(surface_sphere, Vector3(), x, z, 1.0, 0.0, surface_sphere, Vector3(1.0, 0.0, 0.0), x, z, 1.0, 0.0, 1, "two spheres met in the wrong curves")
    try circles(surface_plane, Vector3(0.0, 0.0, 0.5), x, z, 0.0, 0.0, surface_sphere, Vector3(), x, z, 1.0, 0.0, 1, "a plane and a sphere met in the wrong curves")
    # A cone widening by a half per unit, cut by a cylinder of 1.5: at 1.
    try circles(surface_cone, Vector3(), x, z, 1.0, math.atan(0.5), surface_cylinder, Vector3(0.0, 0.0, -3.0), x, z, 1.5, 0.0, 1, "a cone and a cylinder about one axis met in the wrong curves")
    # A torus (2, 0.5) and a sphere of 2 about its center: two circles.
    try same(surface_cone, 1.0, math.atan(0.5))
    try same(surface_torus, 2.0, 0.5)
    # Cylinders crossing at an angle.
    var a = try analytic(surface_cylinder, Vector3(), x, z, 1.0, 0.0)
    defer a.close()
    var b = try analytic(surface_cylinder, Vector3(), z, x, 0.5, 0.0)
    defer b.close()
    var out = Growing[Carrier]()
    defer out.close()
    if try meet(&a, &b, 1e-9, &out) != meet_march: error(invalid, "crossing cylinders were not left to tracing")

func analytic(kind: i64, origin: Vector3, x: Vector3, z: Vector3, radius: f64, angle: f64) -> Surface!:
    let no_points: Vector3[1] = [Vector3()]
    let no_numbers: f64[1] = [0.0]
    return try Surface(kind, origin, x, z, radius, angle, no_points[..<0], no_numbers[..<0], no_numbers[..<0], no_numbers[..<0], 0, 0, 0, 0)

## Two surfaces meeting in `count` circles, each on both.
func circles(ka: i64, oa: Vector3, xa: Vector3, za: Vector3, ra: f64, aa: f64, kb: i64, ob: Vector3, xb: Vector3, zb: Vector3, rb: f64, ab: f64, count: usize, what: str) -> !:
    var a = try analytic(ka, oa, xa, za, ra, aa)
    defer a.close()
    var b = try analytic(kb, ob, xb, zb, rb, ab)
    defer b.close()
    var out = Growing[Carrier]()
    defer out.close()
    if try meet(&a, &b, 1e-9, &out) != meet_curves or out.count != count: error(invalid, what)
    for c in out.view():
        if c.kind != curve_circle: error(invalid, "surfaces met in something other than circles")
        for k in 0..<8:
            let t = 2.0 * math.pi * (f64)k / 8.0
            let p = c.origin.add(c.x.multiply_scalar(c.a * math.cos(t))).add(c.y.multiply_scalar(c.a * math.sin(t)))
            if surface_distance(&a, p) > 1e-9 or surface_distance(&b, p) > 1e-9: error(invalid, "a meeting circle leaves a surface")

## A cone or torus met by itself: the same surface.
func same(kind: i64, radius: f64, angle: f64) -> !:
    var a = try analytic(kind, Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 0.0, 1.0), radius, angle)
    defer a.close()
    var b = try analytic(kind, Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 0.0, 1.0), radius, angle)
    defer b.close()
    var out = Growing[Carrier]()
    defer out.close()
    if try meet(&a, &b, 1e-9, &out) != meet_same: error(invalid, "one surface twice was not the same surface")