dymokomi/luce-geocore
Source of release 0.7.9
0.7.9 / src / verbs / extrude.lucb
## PolyExtrude and Inset, after Houdini's PolyExtrude and Blender's Extrude
## and Inset. A face group extrudes as regions (connected faces move
## together, walls along the region's boundary only) or as individual faces;
## its fronts move `distance` along their region's average normal (Blender's
## Extrude Region), their points' normals (Houdini's), each face's own
## normal (Individual faces) or a vector, and `inset` inward across the
## region's boundary; the walls get `divisions` rows, and the fronts turn
## by `twist` and scale by `taper` about their region's center, the rows
## following. With Manifold, a wall that would lie in the plane of the face
## beside it merges into that face, so a face pushed into a box cuts its
## sides instead of doubling them (Blender's Extrude Manifold).
##
## An edge group extrudes its edges into quads: outward in the face plane
## for boundary edges, along the faces' normals otherwise (or along the
## vector). A point group grows spikes (Maya's Extrude on vertices): each
## point opens into a ring of points along its edges and a tip `distance`
## out along its normal (extrude_spikes.lucb).
##
## Around a boundary point, each run of region faces between two boundary
## edges (a sector) gets its own front point, so regions touching at a point
## stay apart; a whole closed surface has no boundary and moves out along its
## points' normals (extrude_fronts.lucb). The fronts keep their face ids and
## are the output selection (an edge group selects its front edges, a point
## group its tips). Front corners moved inward blend their face's corners,
## so UVs follow the inset (extrude_walls.lucb).
let extrude_parms: ParmSpec[10] = [
ParmSpec("Distance", ParmKind.number, 0.2, -100000.0, 100000.0, 1, "", "How far the fronts move.", 2.0),
ParmSpec("Inset", ParmKind.number, 0.0, -100000.0, 100000.0, 1, "", "How far the fronts' boundaries move inward across the faces; for points, how far along each edge a spike's base sits (0: a quarter of the edge).", 1.0),
ParmSpec("Divisions", ParmKind.integer, 1.0, 1.0, 64.0, 1, "", "Rows of faces along each wall."),
ParmSpec("Individual faces", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "Extrude each face on its own, along its own normal, instead of each connected region."),
ParmSpec("Clamp inset", ParmKind.toggle, 1.0, 0.0, 1.0, 1, "", "Keep each inset within half of the boundary edges beside it.", more = true),
ParmSpec("Direction", ParmKind.menu, 0.0, 0.0, 2.0, 1, "Region normal|Point normals|Vector", "Which way the fronts move: along each region's average normal, along each point's normal, or along Vector."),
ParmSpec("Vector", ParmKind.vector, 0.0, -100000.0, 100000.0, 3, "", "The way the fronts move when Direction is Vector (its length does not matter; zero moves along the region's normal).", more = true),
ParmSpec("Twist", ParmKind.number, 0.0, -36000.0, 36000.0, 1, "", "Degrees each front turns about its region's center, the wall rows turning with it.", 180.0, more = true),
ParmSpec("Taper", ParmKind.number, 1.0, 0.0, 100000.0, 1, "", "How each front scales about its region's center (1 keeps its size), the wall rows following.", 2.0, more = true),
ParmSpec("Manifold", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "Merge each wall that would lie in the plane of the face beside it into that face, so faces pushed into a solid cut it cleanly.", more = true)]
let extrude_picks: PickSpec[1] = [PickSpec("Faces, edges or points", "faces edges points")]
## Edges and points extrude as they are; other groups become faces.
let extrude_also: Domain[2] = [Domain.edge, Domain.point]
func extrude_verb() -> Verb:
return Verb("PolyExtrude", "Modeling", "Extrude faces (as regions or one by one), edges or points", Domain.face, true, extrude_parms, true, cook_extrude, false, extrude_also,
label = "Extrude", section = "Modify", family = "Extrude", icon = "extrude", picks = extrude_picks,
drag = 0, drag_mode = "normal", follows = true, wheel = 2, axis = 6, axis_choice = 5)
let inset_parms: ParmSpec[9] = [
ParmSpec("Thickness", ParmKind.number, 0.1, 0.0, 100000.0, 1, "", "How far the inner faces' boundaries move inward.", 1.0),
ParmSpec("Depth", ParmKind.number, 0.0, -100000.0, 100000.0, 1, "", "How far the inner faces move along their normals.", 1.0),
ParmSpec("Individual faces", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "Inset each face on its own instead of each connected region."),
ParmSpec("Clamp", ParmKind.toggle, 1.0, 0.0, 1.0, 1, "", "Keep each inset within half of the boundary edges beside it.", more = true),
ParmSpec("Outset", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "Move the rim outward into the faces around the group, which keeps its size and stays selected."),
ParmSpec("Even", ParmKind.toggle, 1.0, 0.0, 1.0, 1, "", "Keep the rim's width at corners: a point goes further where the boundary turns."),
ParmSpec("Relative", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "Scale the thickness at each point by the mean length of the boundary edges there.", more = true),
ParmSpec("Edge rail", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "Slide each boundary point along the edge between the two group faces beside it, where there is one.", more = true),
ParmSpec("Boundary", ParmKind.toggle, 1.0, 0.0, 1.0, 1, "", "Inset along the mesh's open edges too; off, the rim slides along them.", more = true)]
func inset_verb() -> Verb:
return Verb("Inset", "Modeling", "Inset faces by a distance, as regions or one by one", Domain.face, true, inset_parms, true, cook_inset,
section = "Modify", family = "Extrude", icon = "inset", picks = pick_faces, drag = 0)
## One extrusion's settings. Inset is an extrusion by its depth along the
## points' normals, its rim moved by its thickness.
struct Extrusion:
let distance: f64
let inset: f64
let divisions: usize
let individual: bool
let clamp: bool
## How fronts move: 0 along their region's normal, 1 along their
## points' normals, 2 along `vector`.
let direction: i64 = 1
let vector: Vector3 = Vector3()
## Degrees about the region's axis, and the front's scale.
let twist: f64 = 0.0
let taper: f64 = 1.0
let manifold: bool = false
## The rim: width kept at corners, scaled by edge lengths, slid along
## the region's inner edges, and inset along the mesh's open edges.
let even: bool = true
let relative: bool = false
let rail: bool = false
let boundary: bool = true
func cook_extrude(input: const VerbInput*, output: VerbOutput*) -> !:
let distance = input.parms.at(0)
let inset = input.parms.at(1)
# Numbers by component: Vector takes 6..8, Twist 9, Taper 10.
let vector = Vector3(input.parms.at(6), input.parms.at(7), input.parms.at(8))
let twist = input.parms.at(9)
let taper = input.parms.at(10)
if not math.is_finite(distance) or not math.is_finite(inset) or not math.is_finite(twist) or not math.is_finite(taper) or not vector.is_finite() or taper < 0.0:
error(errors.invalid, "Extrude distance, inset, vector, twist and taper must be finite, the taper not negative")
let divisions = (usize)math.max(1.0, math.min(64.0, input.parms.at(2)))
let direction = (i64)math.max(0.0, math.min(2.0, input.parms.at(5)))
let settings = Extrusion(distance, inset, divisions, input.parms.at(3) != 0.0, input.parms.at(4) != 0.0, direction, vector, twist, taper, input.parms.at(11) != 0.0)
if input.domain == Domain.edge: return try extrude_edges(input, output, settings)
if input.domain == Domain.point: return try extrude_points(input, output, settings)
try extrude_faces(input, output, settings, try drag_key(input, settings))
func cook_inset(input: const VerbInput*, output: VerbOutput*) -> !:
let thickness = input.parms.at(0)
let depth = input.parms.at(1)
if not math.is_finite(thickness) or not math.is_finite(depth) or thickness < 0.0: error(errors.invalid, "Inset thickness must be finite and not negative")
let outset = input.parms.at(4) != 0.0
let settings = Extrusion(depth, thickness, 1, input.parms.at(2) != 0.0, input.parms.at(3) != 0.0, even = input.parms.at(5) != 0.0, relative = input.parms.at(6) != 0.0,
rail = input.parms.at(7) != 0.0, boundary = input.parms.at(8) != 0.0 and not outset)
if not outset: return try extrude_faces(input, output, settings, 0)
# Outset: the faces around the group inset away from it (along the
# mesh's open edges they slide), and the group stays selected.
let faces = (usize)input.mesh.face_count()
var rest = try BitsBuilder.zeros(faces)
defer rest.close()
for face in 0..<faces:
if not input.group.get(face): rest.set(face)
let around = VerbInput(input.mesh, rest.view(), input.parms, input.symmetry, input.domain)
try extrude_faces(&around, output, settings, 0)
if not output.built: return
output.selection.close()
output.selection = try BitsBuilder.zeros(output.builder.face_count())
for face in 0..<faces:
if input.group.get(face): output.selection.set(face)
## Face extrusion: the regions, their fronts, then the faces. With a drag
## key (see drag_key) a frame whose last frame was built from the same key
## only places the fronts and wall rows again; a built one keeps their
## points for the next frame.
func extrude_faces(input: const VerbInput*, output: VerbOutput*, settings: Extrusion, key: u64) -> !:
let mesh = input.mesh
if not input.group.any():
output.warning = "No faces in the group to extrude; the input passes through"
return
let topology = try Topology.of(mesh)
var regions = try regions_of(mesh, &topology, input.group, settings)
defer regions.close()
var fronts = Fronts()
defer fronts.close()
try place_fronts(mesh, &topology, input.group, settings, ®ions, &fronts)
if key != 0:
if let last = input.over:
if try placed_again(input, output, last, key, &fronts, ®ions, settings): return
try build_faces(input, output, &topology, &fronts, ®ions, settings)
if key != 0 and output.trail_points.count == fronts.count() * settings.divisions: output.trail_key = key
## What a face extrusion's topology and corner values follow, as one key:
## the input's points and faces, the group, and every setting but those
## that only place the fronts (distance, direction, vector, twist, taper).
## 0 (no key) for Manifold, whose merges follow the positions, and for a
## zero distance, whose walls collapse.
func drag_key(input: const VerbInput*, settings: Extrusion) -> u64!:
if settings.manifold or settings.distance == 0.0: return 0
var key = FullKey()
defer key.close()
try key.text("PolyExtrude faces")
try key.word(mesh_positions_id(input.mesh))
try key.word(input.mesh.corners_id())
try key.word(input.mesh.offsets_id())
try key.word((u64)input.symmetry)
for word in input.group.words: try key.word(word)
for at in 0..<input.parms.numbers.length:
if at == 0 or (at >= 5 and at <= 10): continue
try key.word(memory.read[u64]((void*)&input.parms.numbers[at]))
let hash = key.hash()
return hash if hash != 0 else 1
## The last frame `last` with every front and wall row placed again for
## `settings`, when it was built from `key` (its trail lists those points in
## the order build_faces makes them); false when it was not.
func placed_again(input: const VerbInput*, output: VerbOutput*, last: const Mesh*, key: u64, fronts: const Fronts*, regions: const Regions*, settings: Extrusion) -> bool!:
let points = trail_from(last, key) else return false
let count = fronts.count()
let rows = settings.divisions
if points.length != count * rows: return false
let mesh = input.mesh
var positions = mesh_positions(last)
errdefer positions.release()
let out = try positions.writable()
let origin = last.position_origin()
# Fronts first (the last row), then rows 1..<rows, as build_faces made them;
# a row point no wall used (an inner front's) was dropped.
output.moved.clear()
for (at, point) in points.indexed():
if point == dropped_point: continue
let row = rows if at < count else 1 + (at - count) // count
let front = at % count
out[(usize)point] = Float3.of(front_place(mesh, fronts, regions, settings, front, (f64)row / (f64)rows).subtract(origin))
try output.moved.push(point)
output.positions.release()
output.positions = positions
try output.trail_points.assign(points)
output.over_key = key
# The selection is the group's faces, as a build selects them.
output.selection.close()
output.selection = try BitsBuilder.zeros((usize)last.face_count())
let words = output.selection.words()
memory.copy(words[..<input.group.words.length], input.group.words, input.group.words.length)
output.selection_domain = Domain.face
return true
## The group's regions: faces joined across edges that do not stop a region
## (each face alone for individual faces), with the way each region's fronts
## move (zero: along their points' own normals), the axis they turn about
## for twist and taper, and the region's center.
struct Regions:
var of_face: Growing[i32]
var directions: Growing[Vector3]
var axes: Growing[Vector3]
var centers: Growing[Vector3]
func init():
self.of_face = Growing[i32]()
self.directions = Growing[Vector3]()
self.axes = Growing[Vector3]()
self.centers = Growing[Vector3]()
func close():
self.of_face.close()
self.directions.close()
self.axes.close()
self.centers.close()
func regions_of(mesh: const Mesh*, topology: const Topology*, group: Bits, settings: Extrusion) -> Regions!:
var regions = Regions()
errdefer regions.close()
# Only the group's faces are read (a region grows across group faces).
var listed = try group_faces(group)
defer listed.close()
try regions.of_face.reserve(topology.faces())
regions.of_face.count = topology.faces()
for face in listed.view(): regions.of_face.items[(usize)face] = -1
var queue = Growing[i32]()
defer queue.close()
for listed_seed in listed.view():
let seed = (usize)listed_seed
if regions.of_face.items[seed] >= 0: continue
let region = (i32)regions.directions.count
regions.of_face.items[seed] = region
queue.clear()
try queue.push((i32)seed)
var at: usize = 0
var area = Vector3()
var magnitude = 0.0
var weighted = Vector3()
while at < queue.count:
let face = (usize)queue.items[at]
at += 1
let vector = face_area_vector(mesh, topology, face)
area = area.add(vector)
magnitude += vector.length()
weighted = weighted.add(face_centroid(mesh, topology, face).multiply_scalar(vector.length()))
for corner in (usize)topology.offsets[face]..<(usize)topology.offsets[face + 1]:
if stops(topology, group, corner, settings.individual): continue
let across = (usize)topology.corner_faces[(usize)topology.twin(corner)]
if regions.of_face.items[across] >= 0: continue
regions.of_face.items[across] = region
try queue.push((i32)across)
# A region whose faces cancel out (a closed shell) faces no way.
let normal = area.multiply_scalar(1.0 / area.length()) if area.length() > magnitude * 0.000001 and area.length() > 0.0 else Vector3()
var direction = Vector3()
if settings.direction == 2 and settings.vector.length() > 0.0: direction = settings.vector.multiply_scalar(1.0 / settings.vector.length())
elif settings.individual or settings.direction != 1: direction = normal
try regions.directions.push(direction)
try regions.axes.push(direction if direction.length() > 0.0 else normal)
try regions.centers.push(weighted.multiply_scalar(1.0 / magnitude) if magnitude > 0.0 else face_centroid(mesh, topology, seed))
return regions
## Face `face`'s mean corner position (world).
func face_centroid(mesh: const Mesh*, topology: const Topology*, face: usize) -> Vector3:
let start = (usize)topology.offsets[face]
let end = (usize)topology.offsets[face + 1]
var sum = Vector3()
for corner in start..<end: sum = sum.add(point_of(mesh, topology.corners[corner]))
return sum.multiply_scalar(1.0 / (f64)(end - start))
## Whether corner `corner`'s edge stops a region: the other side is not a
## group face (or there is no single other side), or faces go one by one.
func stops(topology: const Topology*, group: Bits, corner: usize, individual: bool) -> bool:
if individual: return true
let edge = (usize)topology.corner_edges[corner]
let first = (usize)topology.edge_corner_offsets[edge]
if (usize)topology.edge_corner_offsets[edge + 1] - first != 2: return true
let across = topology.twin(corner)
return across < 0 or not group.get((usize)topology.corner_faces[(usize)across]) or topology.corner_faces[(usize)across] == topology.corner_faces[corner]
## Whether corner `corner`'s edge gets a wall (the rim insets across it): a
## region's stop, but not the mesh's own open edge when `boundary` is off.
func walls(topology: const Topology*, group: Bits, corner: usize, settings: Extrusion) -> bool:
if not stops(topology, group, corner, settings.individual): return false
if settings.boundary: return true
let edge = (usize)topology.corner_edges[corner]
return (usize)topology.edge_corner_offsets[edge + 1] - (usize)topology.edge_corner_offsets[edge] != 1
func point_of(mesh: const Mesh*, point: i32) -> Vector3:
let exact = precise_span(mesh)
if exact.length > 0: return exact[(usize)point]
return mesh.point_span()[(usize)point].vector().add(mesh.position_origin())
## The unit vector from `a` to `b` turned a quarter inward in a face with
## normal `normal` (to the left of a counterclockwise edge).
func inward(normal: Vector3, a: Vector3, b: Vector3) -> Vector3:
let across = normal.cross(b.subtract(a))
let length = across.length()
return across.multiply_scalar(1.0 / length) if length > 0.0 else Vector3()
## Edge extrusion: each group edge becomes `divisions` quads from the edge
## to its copy moved `distance` along its direction (outward in the face's
## plane for a boundary edge, the faces' normals otherwise, or the Vector),
## with the moved copies shared where group edges meet. The front edges are
## selected.
func extrude_edges(input: const VerbInput*, output: VerbOutput*, settings: Extrusion) -> !:
let mesh = input.mesh
if not input.group.any() or settings.distance == 0.0:
output.warning = "No edges in the group to extrude (or a zero distance); the input passes through"
return
let topology = try Topology.of(mesh)
let normals = mesh.face_normal_span()
let sums = try new Vector3[topology.points] --- in memory.heap
defer free(sums) in memory.heap
let front = try new i32[topology.points] --- in memory.heap
defer free(front) in memory.heap
for at in 0..<topology.points:
sums[at] = Vector3()
front[at] = -1
let along_vector = settings.direction == 2 and settings.vector.length() > 0.0
for edge in 0..<topology.edges():
if not input.group.get(edge): continue
let direction = settings.vector if along_vector else edge_direction(mesh, &topology, normals, edge)
for side in 0..<(usize)2:
let point = (usize)topology.edge_points[edge * 2 + side]
sums[point] = direction if along_vector else sums[point].add(direction)
var builder = &output.builder
try builder.begin(mesh)
try builder.copy_points(mesh)
output.built = true
try builder.copy_faces(mesh, 0, topology.faces())
let rows = settings.divisions
# Rows 1..rows of each group point: consecutive new points.
for point in 0..<topology.points:
let length = sums[point].length()
if length == 0.0 and not touches_group(&topology, input.group, point): continue
let direction = sums[point].multiply_scalar(1.0 / length) if length > 0.0 else Vector3()
front[point] = (i32)builder.point_count()
let start = point_of(mesh, (i32)point)
for row in 1..<rows + 1:
_ = try builder.point(start.add(direction.multiply_scalar(settings.distance * (f64)row / (f64)rows)), (i32)point)
for edge in 0..<topology.edges():
if not input.group.get(edge): continue
# Face the way the edge's first face does: it runs a→b there, so the
# quads run b→a along the edge.
let corner = (usize)topology.edge_corners[(usize)topology.edge_corner_offsets[edge]]
let next = topology.next(corner)
let a = topology.corners[corner]
let b = topology.corners[next]
for row in 0..<rows:
try builder.face(topology.corner_faces[corner])
try builder.corner(edge_row(front, b, row), (i32)next)
try builder.corner(edge_row(front, a, row), (i32)corner)
try builder.corner(edge_row(front, a, row + 1), (i32)corner)
try builder.corner(edge_row(front, b, row + 1), (i32)next)
try builder.end_face()
try builder.select_edge(edge_row(front, a, rows), edge_row(front, b, rows))
func edge_row(front: const i32[], point: i32, row: usize) -> i32:
if row == 0: return point
return front[(usize)point] + (i32)row - 1
func touches_group(topology: const Topology*, group: Bits, point: usize) -> bool:
for slot in (usize)topology.point_edge_offsets[point]..<(usize)topology.point_edge_offsets[point + 1]:
if group.get((usize)topology.point_edges[slot]): return true
return false
## An edge's extrusion direction: outward in its face's plane on a boundary,
## else its faces' summed normals.
func edge_direction(mesh: const Mesh*, topology: const Topology*, normals: const Float3[], edge: usize) -> Vector3:
let first = (usize)topology.edge_corner_offsets[edge]
let count = (usize)topology.edge_corner_offsets[edge + 1] - first
let corner = (usize)topology.edge_corners[first]
if count == 1:
let a = point_of(mesh, topology.corners[corner])
let b = point_of(mesh, topology.corners[topology.next(corner)])
return inward(normals[(usize)topology.corner_faces[corner]].vector(), a, b).multiply_scalar(-1.0)
var sum = Vector3()
for at in first..<first + count:
sum = sum.add(normals[(usize)topology.corner_faces[(usize)topology.edge_corners[at]]].vector())
let length = sum.length()
return sum.multiply_scalar(1.0 / length) if length > 0.0 else Vector3()