dymokomi/luce-cad
Source of release 0.9.5
0.9.5 / src / meshing / display_error.lucb
## Check retained display polygons against their support chart.
## Chart and lifted point IDs correspond; face corners can use either winding.
## `trim` holds the chart points the first trim points truly have, where
## the chart moved one onto a grid line (absorb_crossing): the probes run
## through those, as the display triangles join the trim's own points.
pub func validate_display_error(mesh: const Mesh*, chart: const Mesh*, surface: Surface, tolerance: f64, divisions: Density, trim: const Vector3[]? = none) -> !:
if surface.kind == surface_plane: return
# A display triangle may stray as a chord turning the step angle does
# (its longest side's turn_sag), or the surface's loosest target.
let target = try loosest_sag(surface, divisions, tolerance)
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(chart, trim, ids[0])
let b = try chart_point(chart, trim, ids[1])
let c = try chart_point(chart, trim, ids[2])
let pa = try mesh.point(ids[0])
let pb = try mesh.point(ids[1])
let pc = try mesh.point(ids[2])
let longest = math.max(pb.subtract(pa).length(), math.max(pc.subtract(pb).length(), pa.subtract(pc).length()))
let deviation = math.max(target, longest * turn_sag(divisions))
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 surface_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
func chart_point(chart: const Mesh*, trim: const Vector3[]?, id: i64) -> Vector3!:
if let points = trim:
if id < (i64)points.length: return points[(usize)id]
return try chart.point(id)