dymokomi/luce-cad
Source of release 0.9.4
0.9.4 / src / meshing / surface_trim.lucb
## A trimmed B-spline face meshed in its normalized chart, stretched to
## its lengths along u and v so the lattice is square on the surface: the
## loops triangulated with a lattice, refined where the surface curves or
## a triangle is badly shaped on it (chart_refine), paired into quads,
## lifted.
pub func mesh_trim(mesh: MeshBuilder*, ids: const i64[], sizes: const i64[], surface: Surface, sense: bool, divisions: i64, edge_size: f64, tolerance: f64) -> !:
# Approximate metric scaling avoids a square UV lattice on long thin strips.
let lengths = try spline_lengths(surface)
let u_length = lengths.x
let v_length = lengths.y
if u_length <= 0.000000001 or v_length <= 0.000000001:
error(invalid, "degenerate NURBS parameter metric")
let uv = try new Vector3[ids.length] --- in memory.heap
defer free(uv) in memory.heap
var seed = Vector3(0.5, 0.5, 0.0)
for at in 0..<ids.length:
seed = try project(surface, mesh.points[(usize)ids[at]], seed, tolerance)
# Inversion noise along exact domain edges creates microscopic UV ears.
# Snap parameters only near an endpoint, and only after checking the
# actual surface residual. Original shared 3D boundary points stay intact.
let snapped = Vector3(0.0 if seed.x < 0.0000001 else (1.0 if seed.x > 0.9999999 else seed.x),
0.0 if seed.y < 0.0000001 else (1.0 if seed.y > 0.9999999 else seed.y), 0.0)
if (try evaluate(surface, snapped)).subtract(mesh.points[(usize)ids[at]]).length() <= tolerance:
seed = snapped
uv[at] = Vector3(seed.x * u_length, seed.y * v_length, 0.0)
var spacing = u_length / (f64)divisions
var vertical = v_length / (f64)divisions
if edge_size > 0.0:
spacing = math.min(spacing, edge_size)
vertical = math.min(vertical, edge_size)
let normal = Vector3(0.0, 0.0, 1.0)
let triangles = try TrimPolygon.triangulate_seams(uv, sizes, normal, ids)
defer triangles.release()
let chord = spline_target(lengths, divisions, tolerance)
let constraints = try TrimConstraints.build(uv, ids, sizes)
defer constraints.close()
let gridded = try insert_grid(triangles, normal, spacing, vertical, constraints)
defer gridded.release()
let improved = try improve(gridded, normal, ids, constraints)
defer improved.release()
var lift = ChartLift(surface = surface, uscale = u_length, vscale = v_length)
let refined = try chart_refine(improved, &lift, chord, ids, constraints)
defer refined.release()
let part = try recombine(refined, normal, constraints)
defer part.release()
let mapped = try new i64[(usize)part.value().point_count()] --- in memory.heap
defer free(mapped) in memory.heap
for at in 0..<mapped.length:
let point = try part.value().point((i64)at)
mapped[at] = ids[at] if at < ids.length else (try mesh.point(try evaluate(surface, Vector3(point.x / u_length, point.y / v_length, 0.0))))
for face in 0..<part.value().face_count():
let count = try part.value().face_size(face)
try mesh.face(count)
for at in 0..<count:
try mesh.corner(mapped[(usize)(try part.value().face_point(face, at if sense else count - 1 - at))])