dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / contracts / layout_contract.lucb
## Unexported low-level layout contracts, independent of external STEP fixtures.
import memory
from luce_geocore.geocore import Vector3, invalid
from geometry import Surface, Curve, support_normal, surface_point
from luce_std import math
from cutcell_contract import cutcell_contract
from clipped_spacing_contract import clipped_spacing_contract
from edge_sampling_contract import edge_sampling_contract
from intersection_contract import intersection_contract
from flow_admission_contract import flow_admission_contract
from layout_policy_contract import layout_policy_contract
from trim_alignment_contract import trim_alignment_contract
from layout_envelope_contract import layout_envelope_contract
from layout_pitch_contract import layout_pitch_contract
from mapped_extrusion_contract import mapped_extrusion_contract
from planar_cut_contract import planar_cut_contract
from normal_scale_contract import normal_scale_contract
from display_error_contract import display_error_contract
from cut_sliver_contract import cut_sliver_contract
from analytic_chart_contract import analytic_chart_contract
from adaptive_cells_contract import adaptive_cells_contract
from face_jobs_contract import face_jobs_contract
from circular_endpoint_contract import circular_endpoint_contract
from layout_feasibility_contract import layout_feasibility_contract
from sphere_chart_contract import sphere_chart_contract
from instances_contract import instances_contract
from layout import refine_cells, fill_plane, Stations, check_grid_size, chart_identities, rail_axis
from meshing import redistribute_grid
## Both boundary-to-first-row gaps are included; the boundary itself is fixed.
pub func interior_spacing_contract() -> !:
let s = try Surface(3, Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 0.0, 1.0), 0.0, 0.0,
[Vector3(), Vector3(0.0, 1.0, 0.0), Vector3(1.0, 0.0, 0.0), Vector3(1.0, 1.0, 0.0)],
[1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], 2, 2, 1, 1)
defer s.close()
# Identity is topological, never a proximity weld; a nonperiodic support
# cannot identify opposite rails just because their parameter difference is 1.
let chart = try chart_identities(s, [Vector3(), Vector3(), Vector3()], [7, 7, 8], 1e-6)
defer free(chart) in memory.heap
if chart[0] != chart[1] or chart[0] == chart[2]: error(invalid, "trim chart identity must preserve explicit CAD topology")
var rejected = false
let bad = chart_identities(s, [Vector3(), Vector3(1.0, 0.0, 0.0)], [7, 7], 1e-6) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
rejected = true
recover chart[..<0]
if bad.length > 0: free(bad) in memory.heap
if not rejected: error(invalid, "open support rails are not a periodic seam")
let grid = try new Vector3[25] --- in memory.heap
defer free(grid) in memory.heap
for both_axes in [false, true]:
for row in 0..<(usize)5:
for col in 0..<(usize)5:
let boundary = row == 0 or row == 4 or col == 0 or col == 4
grid[row * 5 + col] = Vector3((f64)col / 4.0 if boundary else 0.3 + (f64)col * 0.1,
(f64)row / 4.0 if boundary or not both_axes else 0.3 + (f64)row * 0.1, 0.0)
try redistribute_grid(grid, 4, 4, s, 1e-6)
for row in 0..<(usize)5:
for col in 0..<(usize)5:
let boundary = row == 0 or row == 4 or col == 0 or col == 4
# Coupled axes converge iteratively within the bounded sweeps;
# boundary values and the single-axis affine case stay exact.
let limit = 1e-10 if boundary or not both_axes else 0.01
if grid[row * 5 + col].subtract(Vector3((f64)col / 4.0, (f64)row / 4.0, 0.0)).length() > limit:
error(invalid, "interior spacing must include both end intervals and preserve every trim vertex")
for axis in 0..<(usize)2:
for line in 1..<(usize)4:
var low: f64 = 1e100
var high: f64 = 0.0
let stride = 1 if axis == 0 else (usize)5
let start = line * 5 if axis == 0 else line
for at in 0..<(usize)4:
let gap = grid[start + (at + 1) * stride].subtract(grid[start + at * stride]).length()
low = math.min(low, gap)
high = math.max(high, gap)
if high > low * 1.05: error(invalid, "both interior row families must have evenly spaced end gaps")
pub func layout_contracts() -> !:
try sphere_chart_contract()
try instances_contract()
try layout_feasibility_contract()
try circular_endpoint_contract()
try face_jobs_contract()
try adaptive_cells_contract()
try interior_spacing_contract()
try diagonal_error_contract()
try endpoint_roundoff_contract()
try analytic_normal_contract()
try metric_rail_contract()
try cutcell_contract()
try clipped_spacing_contract()
try edge_sampling_contract()
try anisotropic_budget_contract()
try intersection_contract()
try endpoint_flow_contract()
try flow_admission_contract()
try layout_policy_contract()
try trim_alignment_contract()
try layout_envelope_contract()
try layout_pitch_contract()
try mapped_extrusion_contract()
try planar_cut_contract()
try normal_scale_contract()
try display_error_contract()
try cut_sliver_contract()
try analytic_chart_contract()
func endpoint_flow_contract() -> !:
for swapped in [false, true]:
for coupled in [false, true]:
var u = Stations()
var v = Stations()
try u.open(128)
defer u.close()
try v.open(128)
defer v.close()
for value in [-0.1, 0.0, 10.0, 10.1]:
if swapped: _ = try v.insert(value, 1e-12)
else: _ = try u.insert(value, 1e-12)
for value in [-0.005, 0.0, 0.5, 0.505]:
if swapped: _ = try u.insert(value, 1e-12)
else: _ = try v.insert(value, 1e-12)
if coupled:
if swapped: _ = try u.insert(0.25, 1e-12)
else: _ = try v.insert(0.25, 1e-12)
let high = Vector3(0.5, 10.0, 0.0) if swapped else Vector3(10.0, 0.5, 0.0)
let independent = try fill_plane(&u, &v, Vector3(), high, 16, 1e-12)
if independent == coupled: error(invalid, "interior stations must determine planar phase ownership")
let across = u if swapped else v
let along = v if swapped else u
if along.count < 16: error(invalid, "long planar supports must retain requested longitudinal density")
if coupled:
if across.count < 16: error(invalid, "actual inherited interior rows must retain coupled support density")
var found = false
for value in across.values[..<across.count]: found = found or value == 0.25
if not found: error(invalid, "planar support fill moved an inherited row")
elif across.count != 4:
error(invalid, "trim endpoints and exterior guards are not interior flow constraints")
func anisotropic_budget_contract() -> !:
for swap in [false, true]:
try check_grid_size(63 if swap else 2049, 2049 if swap else 63)
for pair in [[2049, 64], [363, 363], [2050, 2]]:
var rejected = false
check_grid_size((usize)pair[1 if swap else 0], (usize)pair[0 if swap else 1]) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
rejected = true
recover ()
if not rejected: error(invalid, "anisotropic support grids must retain total and axis budgets")
func metric_rail_contract() -> !:
var empty_points: Vector3[]
var empty_numbers: f64[]
let small = try Surface(0, Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 0.0, 1.0), 0.0, 0.0, empty_points, empty_numbers, empty_numbers, empty_numbers, 0, 0, 0, 0)
defer small.close()
let noisy = [Vector3(), Vector3(0.5, 2e-7, 0.0), Vector3(1.0, 0.0, 0.0)]
if try rail_axis(small, noisy, 1e-7, 1e-6) != 0: error(invalid, "physical rail uncertainty must not create a second station family")
let large = try Surface(3, Vector3(), Vector3(), Vector3(), 0.0, 0.0,
[Vector3(), Vector3(0.0, 1000000.0, 0.0), Vector3(1.0, 0.0, 0.0), Vector3(1.0, 1000000.0, 0.0)],
[1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], 2, 2, 1, 1)
defer large.close()
for delta in [2e-7, 5e-8]:
if try rail_axis(large, [Vector3(), Vector3(0.5, delta, 0.0), Vector3(1.0, 0.0, 0.0)], 1e-7, 1e-6) != -1:
error(invalid, "a tiny UV wobble can exceed physical CAD tolerance on a stretched chart")
if try rail_axis(small, [Vector3(), Vector3(0.5, 0.1, 0.0), Vector3(1.0, 0.0, 0.0)], 1e-7, 1.0) != -1:
error(invalid, "oblique trims are not rails even under a loose physical tolerance")
func analytic_normal_contract() -> !:
var empty_points: Vector3[]
var empty_numbers: f64[]
for kind in [0, 1, 2, 4, 5]:
let s = try Surface(kind, Vector3(2.0, 3.0, 4.0), Vector3(0.0, 1.0, 0.0), Vector3(1.0, 0.0, 0.0), 2.0, 0.2, empty_points, empty_numbers, empty_numbers, empty_numbers, 0, 0, 0, 0)
defer s.close()
for uv in [Vector3(0.3, 0.2, 0.0), Vector3(-2.1, -0.4, 0.0)]:
let du = (try surface_point(s, uv.add(Vector3(1e-5, 0.0, 0.0)))).subtract(try surface_point(s, uv.subtract(Vector3(1e-5, 0.0, 0.0))))
let dv = (try surface_point(s, uv.add(Vector3(0.0, 1e-5, 0.0)))).subtract(try surface_point(s, uv.subtract(Vector3(0.0, 1e-5, 0.0))))
let expected = try du.cross(dv).normalized()
if expected.subtract(try support_normal(s, uv)).length() > 1e-8:
error(invalid, "analytic chart normals must agree with support derivatives")
## Floating endpoint reconstruction must clamp tiny overshoots, while a real
## interval crossing the seam of a closed spline must still wrap normally.
func endpoint_roundoff_contract() -> !:
for reversed in [false, true]:
let edge = try Curve(2, 0, 1, Vector3(), Vector3(), Vector3(), 0.0,
[Vector3(), Vector3(1.0, 0.0, 0.0)], [1.0, 1.0], [0.0, 0.0, 0.3, 0.3], 1, not reversed,
0.2 if reversed else 0.1, -0.20000000000000004 if reversed else 0.2)
defer edge.close()
let p = try edge.at(1.0)
if math.abs(p.x - (0.0 if reversed else 1.0)) > 1e-12:
error(invalid, "open spline endpoint wrapped across its knot domain")
let closed = try Curve(2, 0, 1, Vector3(), Vector3(), Vector3(), 0.0,
[Vector3(), Vector3(1.0, 0.0, 0.0), Vector3()], [1.0, 1.0, 1.0], [0.0, 0.0, 0.5, 1.0, 1.0], 1, true, 0.75, 0.5)
defer closed.close()
if math.abs((try closed.at(1.0)).x - 0.5) > 1e-12:
error(invalid, "a real periodic crossing must still wrap")
## A bilinear saddle has straight isocurves, but its display triangles are not
## on the support. Planning must catch this before shared cuts are frozen.
func diagonal_error_contract() -> !:
let s = try Surface(3, Vector3(), Vector3(), Vector3(), 0.0, 0.0,
[Vector3(), Vector3(0.0, 1.0, 0.0), Vector3(1.0, 0.0, 0.0), Vector3(1.0, 1.0, 1.0)],
[1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], 2, 2, 1, 1)
defer s.close()
var u = Stations()
var v = Stations()
try u.open(257)
defer u.close()
try v.open(257)
defer v.close()
for t in [0.0, 1.0]:
_ = try u.insert(t, 1e-9)
_ = try v.insert(t, 1e-9)
try refine_cells(&u, &v, s, 8, 1e-6, 1e-9)
if u.count < 9 or v.count < 9: error(invalid, "mixed curvature needs complete support rows")