dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / contracts / edge_sampling_contract.lucb
## Sampling count refinement keeps the same geometric and angular criteria.
from luce_std import math
import memory
from luce_geocore.geocore import Vector3, invalid
from geometry import Curve, BrepFace
from layout import curve_intervals, compact_seams, close_metrics, measure_curves
pub func edge_sampling_contract() -> !:
try seam_ownership()
var nonbinary: usize = 0
# Rational arcs of a quarter turn and of 60 degrees: sampled as the arc
# itself would be (4 divisions a turn), within the same distance and
# angular criteria.
for sweep in [0.5 * math.pi, math.pi / 3.0]:
for radius in [0.2,2.0]:
for tolerance in [1e-6,0.01]:
for phase in [0.0,1.3,2.7]:
for reverse in [false,true]:
let axis = Vector3(math.cos(phase),math.sin(phase),0.0)
let other = Vector3(-math.sin(phase),math.cos(phase),0.0)
let origin = Vector3(3.0,-2.0,1.0)
let half = sweep*0.5
let middle = axis.multiply_scalar(math.cos(half)).add(other.multiply_scalar(math.sin(half))).multiply_scalar(radius/math.cos(half))
let points = [origin.add(axis.multiply_scalar(radius)),origin.add(middle),origin.add(axis.multiply_scalar(math.cos(sweep)).add(other.multiply_scalar(math.sin(sweep))).multiply_scalar(radius))]
let edge = try Curve(2,0,1,Vector3(),Vector3(),Vector3(),0.0,points,[1.0,math.cos(half),1.0],[0.0,0.0,0.0,1.0,1.0,1.0],2,true,1.0 if reverse else 0.0,-1.0 if reverse else 1.0)
defer edge.close()
let metrics = try measure_curves([edge])
defer close_metrics(metrics)
let arc = (usize)math.max(2.0, math.ceil(64.0*sweep/(2.0*math.pi)-1e-9))
let shared = try curve_intervals(edge,16,tolerance,&metrics[0])
if shared & (shared - 1) != 0: error(invalid,"shared-edge stations must retain their dyadic phase")
if shared > 2 * arc: error(invalid,"a spline arc must be sampled about as its arc is")
let count = try curve_intervals(edge,16,tolerance,&metrics[0],true)
# Its arc's count, give or take one where the arc's own
# turn per interval is exactly the limit.
if count > shared or (tolerance < 1e-3 and (count + 1 < arc or count > arc + 1)): error(invalid,"a compact spline arc must take about its arc's count")
if count & (count - 1) != 0: nonbinary += 1
var length: f64 = 0.0
var previous = try edge.at(0.0)
for at in 1..<33:
let p = try edge.at((f64)at/32.0)
length += p.subtract(previous).length()
previous = p
let cosine = math.cos(0.75*2.0*math.pi/64.0)
for interval in 0..<count:
let a = metrics[0].station((f64)interval/(f64)count)
let b = metrics[0].station((f64)(interval+1)/(f64)count)
let pa = try edge.at(a)
let pb = try edge.at(b)
let span = pb.subtract(pa)
# The chord limit, or an arc's sag turning a quarter
# turn's sixteenth over the interval.
let limit = math.max(math.max(tolerance,length/4096.0), span.length()*math.pi/256.0)
# Denser independent probes than the production sampler.
for sample in 1..<64:
let p = try edge.at(a+(b-a)*(f64)sample/64.0)
let t = math.clamp(p.subtract(pa).dot(span)/span.dot(span),0.0,1.0)
if p.subtract(pa.add(span.multiply_scalar(t))).length() > limit*(1.0+1e-6):
error(invalid,"count refinement exceeded the distance criterion")
if math.abs(p.subtract(origin).length()-radius) > 1e-10: error(invalid,"sampling changed exact circular support")
var samples = [Vector3(),Vector3(),Vector3(),Vector3(),Vector3()]
for at in 0..<(usize)5: samples[at] = try edge.at(a+(b-a)*(f64)at/4.0)
for first in 0..<(usize)4:
for second in first+1..<(usize)4:
let u = samples[first+1].subtract(samples[first])
let v = samples[second+1].subtract(samples[second])
if u.dot(v) < cosine*u.length()*v.length(): error(invalid,"count refinement exceeded the angular criterion")
if nonbinary == 0: error(invalid,"sampling regression did not exercise a non-power-of-two result")
# A true C0 kink cannot necessarily meet the turn target under a uniform
# count. Retain the existing bounded fallback, never loop or import-fail.
let kink = try Curve(2,0,1,Vector3(),Vector3(),Vector3(),0.0,[Vector3(),Vector3(0.4,0.0,0.0),Vector3(0.4,1.0,0.0)],[1.0,1.0,1.0],[0.0,0.0,0.3,1.0,1.0],1,true,0.0,1.0)
defer kink.close()
let metrics = try measure_curves([kink])
defer close_metrics(metrics)
if try curve_intervals(kink,16,1e-6,&metrics[0],true) != 64: error(invalid,"C0 sampling must retain the bounded legacy fallback")
func seam_ownership() -> !:
let a = try BrepFace(0,[1,2,-1,3],[4],true)
defer a.close()
let b = try BrepFace(0,[-4,-2,4,5],[4],false)
defer b.close()
let c = try BrepFace(0,[6,7,-6,8,9],[5],true)
defer c.close()
let d = try BrepFace(0,[10,11,-10,12],[4],true)
defer d.close()
let e = try BrepFace(0,[10,13,14],[3],true)
defer e.close()
let adjacent = try BrepFace(0,[16,-16,17,18],[4],true)
defer adjacent.close()
let same_sense = try BrepFace(0,[19,20,19,21],[4],true)
defer same_sense.close()
let flags = try compact_seams([a,b,c,d,e,adjacent,same_sense],21)
defer free(flags) in memory.heap
for edge in 0..<flags.length:
if flags[edge] != (edge == 0 or edge == 3):
error(invalid,"only opposite repeated seams confined to one rectangular band may compact")