dymokomi/luce-cad
Source of release 0.9.2
0.9.2 / tests / contracts / trim_seam_contract.lucb
## A retraced internal seam is legal only with explicit matching identities.
## Its segments survive lattice insertion, diagonal flips and quad pairing.
from luce_std import math
from luce_geocore.geocore import Vector3, Mesh, invalid
from polygons import TrimPolygon
pub func trim_seam_contract() -> !:
for scale in [0.000001,1.0,1000.0]:
for axis in 0..<3:
for reverse in [false,true]:
for start in 0..<(usize)9:
try check(scale,axis,reverse,start)
func check(scale: f64, axis: i64, reverse: bool, start: usize) -> !:
let source = [Vector3(),Vector3(1.0,0.0,0.0),Vector3(1.0,1.0,0.0),Vector3(0.0,1.0,0.0),Vector3(0.0,0.6,0.0),Vector3(0.3,0.5,0.0),Vector3(0.5,0.5,0.0),Vector3(0.3,0.5,0.0),Vector3(0.0,0.6,0.0)]
let original_ids = [0,1,2,3,4,5,6,5,4]
var points = source
var ids = original_ids
let n = 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 index = (start+(8-at if reverse else at))%9
let p = source[index].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)
ids[at] = original_ids[index]
for spacing in [0.0,0.13]:
let result = try TrimPolygon.quadrangulate_seams(points,[9],n,ids,spacing*scale,spacing*scale)
defer result.release()
let mesh = result.value()
for at in 0..<9:
if (try mesh.point(at)) != points[(usize)at]: error(invalid,"seam meshing moved a canonical point")
var boundary: i64 = 0
var seams: 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..<(try mesh.face_size(face)):
if (try mesh.face_edge(face,corner)) == edge: uses += 1
if uses != 1 and uses != 2: error(invalid,"seam mesh has a nonmanifold edge")
let ai = ids[(usize)a] if a < 9 else -1
let bi = ids[(usize)b] if b < 9 else -1
if (ai == 4 and bi == 5) or (ai == 5 and bi == 4) or (ai == 5 and bi == 6) or (ai == 6 and bi == 5):
if uses != 2: error(invalid,"retraced seam became a boundary")
seams += 1
if uses == 1:
if ai < 0 or bi < 0 or not ((ai+1)%5 == bi or (bi+1)%5 == ai): error(invalid,"seam mesh changed the outer boundary")
boundary += 1
if boundary != 5 or seams != 2: error(invalid,"meshing removed or split an authored seam")
var area: f64 = 0.0
for face in 0..<mesh.face_count():
let a = try mesh.point(try mesh.face_point(face,0))
for at in 1..<(try mesh.face_size(face))-1:
let b = try mesh.point(try mesh.face_point(face,at))
let c = try mesh.point(try mesh.face_point(face,at+1))
let cross = b.subtract(a).cross(c.subtract(a)).dot(n)
if cross <= 0.0: error(invalid,"seam mesh reversed a polygon")
area += cross*0.5
if math.abs(area-scale*scale) > scale*scale*1e-12: error(invalid,"seam mesh changed domain area")
# Mere coincidence must not authorize an overlapping constraint.
var wrong = ids
for at in 0..<wrong.length: wrong[at] = (i64)at
var rejected = false
reject(points,n,wrong) catch failure:
if failure.code != invalid: error(failure.code,failure.message)
rejected = true
recover ()
if not rejected: error(invalid,"unidentified overlapping constraints were accepted")
func reject(points: const Vector3[], n: Vector3, ids: const i64[]) -> !:
let result = try TrimPolygon.triangulate_seams(points,[9],n,ids)
result.release()