dymokomi/luce-cad
Source of release 0.9.2
0.9.2 / src / meshing / clipped_cells.lucb
## Lifted cut cells retain their polygon boundary and their UV triangulation.
## Display diagonals are not mesh edges and must not change quad-flow valence.
pub func append_cells(lifted: MeshBuilder*, part: Mesh*, forward: bool, normals: const Vector3[], failed: i64*? = none) -> !:
var triangle: i64 = 0
# Display triangles of one face, sized by the largest (any size).
var largest: i64 = 3
for face in 0..<part.face_count():
let size = try part.face_size(face)
if size > largest: largest = size
let indices = try new i64[(usize)(largest - 2) * 3] --- in memory.heap
defer free(indices) in memory.heap
let alternate = try new i64[indices.length] --- in memory.heap
defer free(alternate) in memory.heap
for face in 0..<part.face_count():
let count = try part.face_size(face)
let offset = try part.face_offset(face)
try lifted.face(count)
for at in 0..<count:
try lifted.corner(try part.face_point(face, at if forward else count - 1 - at))
for t in 0..<count - 2:
for at in 0..<3:
let index = part.index(triangle + t * 3 + (at if forward else 2 - at))
defer index.release()
let corner_index = (try index.get()) - offset
indices[(usize)(t * 3 + at)] = corner_index if forward else count - 1 - corner_index
let chosen = indices[..<(usize)(count - 2) * 3]
let other = alternate[..<chosen.length]
var bad = folds(lifted, chosen, normals)
if bad > 0:
try repair(lifted, part, face, forward, chosen, normals)
bad = folds(lifted, chosen, normals)
if bad > 0:
var valid = true
lifted.triangulate_last(other) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
valid = false
if valid and folds(lifted, other, normals) < bad:
for at in 0..<chosen.length: chosen[at] = other[at]
if folds(lifted, chosen, normals) > 0 and not (try solve(lifted, part, face, forward, chosen, normals)):
if let destination = failed: *destination = face
error(invalid, "trimmed cell has no consistently oriented display triangulation")
try lifted.set_last_display(chosen)
triangle += (count - 2) * 3