dymokomi/luce-cad
Source of release 0.9.5
0.9.5 / src / meshing / surface_normals.lucb
## CAD shading normals come from the support surface, never triangle density.
## Raw per-corner arrays are prepared in face workers and survive hard seams.
## Owned, placed corner normals for one analytic face's contiguous corner range.
pub func face_normals(mesh: MeshBuilder*, first: usize, count: usize, face: BrepFace, s: Surface, tolerance: f64) -> f64[]!:
let result = try new f64[count * 3] --- in memory.heap
errdefer free(result) in memory.heap
var capacity: usize = 16
while capacity < count * 2: capacity *= 2
let keys = try new i64[capacity] --- in memory.heap
defer free(keys) in memory.heap
let normals = try new Vector3[capacity] --- in memory.heap
defer free(normals) in memory.heap
for at in 0..<capacity: keys[at] = -1
var uv = Vector3(0.5, 0.5, 0.0)
for corner in 0..<count:
let id = mesh.corners[first + corner]
var slot = (usize)((id * 2654435761) ^ (id >> 11)) % capacity
while keys[slot] >= 0 and keys[slot] != id: slot = (slot + 1) % capacity
if keys[slot] < 0:
let p = mesh.points[(usize)id]
var n = s.z
if s.kind == surface_spline:
uv = try chart_parameter(s, p, uv, tolerance)
n = try nurbs_normal(s, uv)
if n.length() < 1e-15:
# At a collapsed domain edge use the limiting interior normal.
n = try nurbs_normal(s, Vector3(math.clamp(uv.x, 1e-7, 1.0 - 1e-7), math.clamp(uv.y, 1e-7, 1.0 - 1e-7), 0.0))
elif s.kind == surface_sphere:
n = try p.subtract(s.origin).normalized()
elif s.kind != surface_plane:
let relative = p.subtract(s.origin)
let offset = relative.subtract(s.z.multiply_scalar(relative.dot(s.z)))
if s.kind == surface_cone and offset.length() <= tolerance:
# A cone's apex: the limit of the side's normals, along
# the axis toward the apex.
n = s.z.multiply_scalar(-1.0 if math.tan(s.angle) > 0.0 else 1.0)
else:
let radial = try offset.normalized()
n = try relative.subtract(radial.multiply_scalar(s.radius)).normalized() if s.kind == surface_torus else (try radial.subtract(s.z.multiply_scalar(math.tan(s.angle) if s.kind == surface_cone else 0.0)).normalized())
normals[slot] = try face.placement.transform_normal(n.multiply_scalar(1.0 if face.sense else -1.0))
keys[slot] = id
let n = normals[slot]
result[corner * 3] = n.x
result[corner * 3 + 1] = n.y
result[corner * 3 + 2] = n.z
return result
## Sequential small-model path; full models prepare the same data in face jobs.
pub func model_normals(mesh: MeshBuilder*, faces: const BrepFace[], surfaces: const Surface[], plans: const GridLayout[], tolerance: f64) -> f64[]!:
let result = try new f64[mesh.nc * 3] --- in memory.heap
errdefer free(result) in memory.heap
var polygon: usize = 0
var corner: usize = 0
while polygon < mesh.nf:
let parent = mesh.face_parents[polygon]
let first = corner
while polygon < mesh.nf and mesh.face_parents[polygon] == parent:
corner += (usize)mesh.sizes[polygon]
polygon += 1
let face = faces[(usize)parent]
let plan = plans[(usize)parent]
let cached = plan.applied.length > 0 and plan.applied[0]
let values = plan.normals[0].values if cached else (try face_normals(mesh, first, corner - first, face, surfaces[(usize)face.surface], tolerance))
defer:
if not cached: free(values) in memory.heap
memory.copy(result[first * 3..<corner * 3], values, values.length)
return result