dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / contracts / ear_remainder_contract.lucb
## Nearly straight sampled constraints must not be stranded as an unmeshable
## residual polygon. The original coordinates and every boundary edge survive.
from luce_std import math
import memory
from luce_geocore.geocore import Vector3, invalid
from polygons import TrimPolygon
pub func ear_remainder_contract() -> !:
var seeded: i64 = 0
for scale in [0.000001, 1.0, 1000.0]:
for axis in 0..<3:
for reverse in [false, true]:
for noise in [1e-17, 3e-16, 1e-15]:
for start in 0..<(usize)24:
if try rectangle(scale, axis, reverse, start, noise): seeded += 1
if seeded == 0: error(invalid, "noisy trim tests did not exercise interior seed fallback")
func rectangle(scale: f64, axis: i64, reverse: bool, start: usize, noise: f64) -> bool!:
let input = try new Vector3[24] --- in memory.heap
defer free(input) in memory.heap
for at in 0..<(usize)12:
input[at] = Vector3((f64)at / 11.0, 0.0, 0.0)
# Deterministic nonmonotone, binary64-sized perturbations mimic a
# projected straight CAD edge without making adjacent points equal.
let offset = (f64)((i64)(at * 7 % 11) - 5) * noise
input[12 + at] = Vector3(1.0 - (f64)at / 11.0, 0.2 + offset, 0.0)
let points = try new Vector3[24] --- in memory.heap
defer free(points) in memory.heap
let normal = Vector3(1.0, 0.0, 0.0) if axis == 0 else (Vector3(0.0, 1.0, 0.0) if axis == 1 else Vector3(0.0, 0.0, 1.0))
for at in 0..<points.length:
let p = input[(start + (23 - at if reverse else at)) % 24].multiply_scalar(scale)
points[at] = Vector3(0.0, p.x, p.y) if axis == 0 else (Vector3(p.y, 0.0, p.x) if axis == 1 else p)
let result = try TrimPolygon.triangulate(points, [24], normal)
defer result.release()
let mesh = result.value()
if mesh.point_count() < 24 or mesh.point_count() > 25 or mesh.face_count() != (22 if mesh.point_count() == 24 else 24): error(invalid, "noisy straight trim lost vertices or triangles")
for at in 0..<24:
if (try mesh.point(at)) != points[(usize)at]: error(invalid, "noisy straight trim moved a boundary vertex")
var boundary: i64 = 0
for edge in 0..<(try mesh.edge_count()):
let a = try mesh.edge_point(edge, 0)
let b = try mesh.edge_point(edge, 1)
var uses: i64 = 0
for face in 0..<mesh.face_count():
for corner in 0..<3:
let x = try mesh.face_point(face, corner)
let y = try mesh.face_point(face, (corner + 1) % 3)
if (x == a and y == b) or (x == b and y == a): uses += 1
if uses == 2: continue
if uses != 1 or ((a + 1) % 24 != b and (b + 1) % 24 != a): error(invalid, "noisy trim introduced a missing or internal boundary")
boundary += 1
if boundary != 24: error(invalid, "noisy trim did not preserve the complete boundary")
var area: f64 = 0.0
for face in 0..<mesh.face_count():
let a = try mesh.point(try mesh.face_point(face, 0))
let b = try mesh.point(try mesh.face_point(face, 1))
let c = try mesh.point(try mesh.face_point(face, 2))
let cross = b.subtract(a).cross(c.subtract(a)).dot(normal)
if cross <= 0.0: error(invalid, "noisy trim reversed a triangle")
area += cross * 0.5
if math.abs(area - 0.2 * scale * scale) > scale * scale * 1e-13: error(invalid, "noisy trim changed domain area")
return mesh.point_count() == 25