dymokomi/luce-cad
Source of release 0.9.2
0.9.2 / 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 quadrangulated, refined where the surface curves, 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.
var u_length: f64 = 0.0
var v_length: f64 = 0.0
for at in 0..<16:
let a = (f64)at / 16.0
let b = (f64)(at + 1) / 16.0
u_length += (try evaluate(surface, Vector3(b, 0.5, 0.0))).subtract(try evaluate(surface, Vector3(a, 0.5, 0.0))).length()
v_length += (try evaluate(surface, Vector3(0.5, b, 0.0))).subtract(try evaluate(surface, Vector3(0.5, a, 0.0))).length()
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 initial = try TrimPolygon.quadrangulate_seams(uv, sizes, Vector3(0.0, 0.0, 1.0), ids, spacing, vertical)
defer initial.release()
let chord = math.max(tolerance * 4.0, math.min(u_length, v_length) / ((f64)divisions * (f64)divisions * 4.0))
let constraints = try TrimConstraints.build(uv, ids, sizes)
defer constraints.close()
let part = try refine_uv(initial, surface, u_length, v_length, chord, 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))])