dymokomi/luce-cad
Source of release 0.9.1
0.9.1 / src / display_error.lucb
## Check retained display polygons against their support chart.
## Chart and lifted point IDs correspond; face corners can use either winding.
from luce_std import math
from luce_geocore.geocore import Vector3, Mesh, invalid, squared_triangle_distance
from brep_types import Surface
from mapped_patch import point
pub func validate_display_error(mesh: const Mesh*, chart: const Mesh*, surface: Surface, tolerance: f64, divisions: i64) -> !:
if surface.kind == 0: return
let extent = mesh.bounds().maximum.subtract(mesh.bounds().minimum).length()
let deviation = math.max(tolerance,extent/((f64)divisions*(f64)divisions*16.0))
for triangle in 0..<mesh.index_count()//3:
var ids: i64[3]
for at in 0..<3:
let index = mesh.index(triangle*3+at)
defer index.release()
ids[(usize)at] = try mesh.corner_point(try index.get())
let a = try chart.point(ids[0])
let b = try chart.point(ids[1])
let c = try chart.point(ids[2])
let pa = try mesh.point(ids[0])
let pb = try mesh.point(ids[1])
let pc = try mesh.point(ids[2])
for probe in [a.add(b).add(c).multiply_scalar(1.0/3.0),a.add(b).multiply_scalar(0.5),b.add(c).multiply_scalar(0.5),c.add(a).multiply_scalar(0.5)]:
let p = try point(surface,probe)
if squared_triangle_distance(p,pa,pb,pc) <= deviation*deviation: continue
# UV barycentrics do not survive a non-affine NURBS parameterization.
# Even a flat surface can place this sample across a display
# diagonal. Test the complete cell, not an unrelated nearby patch.
let index = mesh.index(triangle*3)
defer index.release()
let face = try mesh.corner_face(try index.get())
if not (try within_cell(mesh,face,p,deviation*deviation)):
error(invalid,"display polygon exceeds surface deviation")
func within_cell(mesh: const Mesh*, face: i64, sample: Vector3, maximum2: f64) -> bool!:
let offset = ((try mesh.face_offset(face))-face*2)*3
for triangle in 0..<(try mesh.face_size(face))-2:
var corners = [Vector3(),Vector3(),Vector3()]
for at in 0..<(usize)3:
let index = mesh.index(offset+triangle*3+(i64)at)
defer index.release()
corners[at] = try mesh.point(try mesh.corner_point(try index.get()))
if squared_triangle_distance(sample,corners[0],corners[1],corners[2]) <= maximum2: return true
return false