dymokomi/luce-geocore
Source of release 0.7.9
0.7.9 / src / verbs / bevel.lucb
## PolyBevel: edges (or points) cut away and replaced by strips and corner
## patches, after Blender's bevel (its bmesh bevel tool, read for the
## algorithm and described here in our own words) and Houdini's PolyBevel.
##
## Each beveled edge becomes a strip of `segments` faces whose sides lie
## Width into the faces beside it; each point at the end of a beveled edge
## (a bevel vertex) becomes a ring of boundary points, one between each pair
## of beveled edges around it (more at miters), filled by a patch. The faces
## around a bevel vertex are rebuilt with the ring's points in place of the
## vertex. With Affect Vertices, each point is cut off instead: a boundary
## point slid along each of its edges, the ring filled the same way.
##
## The pieces, in the order a run builds them:
##
## - bevel_setup.lucb: which edges or points bevel (the group, the Angle or
## Weight limit, the `bevel_weight` attributes), refusing non-manifold
## ones; each bevel vertex's edges in turn around it (its halves) and how
## far each half's strip side sits into the faces (the Width Type).
## - bevel_meet.lucb: where two offset lines meet, or meet on an edge
## between them (Loop slide), and slides along edges.
## - bevel_boundary.lucb: each vertex's ring of boundary points, its miters,
## and the kind of patch that fills it.
## - bevel_widths.lucb: Clamp overlap (the widest bevel before strips
## cross) and the even-width pass along chains of loop-slid points.
## - bevel_profile.lucb: the profile between two boundary points: a
## superellipse sampled by equal chords, or the Custom profile ramp.
## - bevel_grid.lucb and bevel_corners.lucb: the Grid Fill patch (a coarse
## grid refined by Catmull-Clark, Sabin's rule at its center), the cube
## corner as an octant of a superellipsoid, pipes, square profiles.
## - bevel_patches.lucb: the vertex meshes: a polygon, a fan, Cutoff's
## faces, two strips welded, the grid's rings.
## - bevel_emit.lucb: the output: rebuilt faces, strips and patches, each
## corner's values interpolated inside the face it lies in.
## - bevel_marks.lucb: edge values carried to the strip sides, Mark seams
## and sharp, Harden normals, Face strength and Material.
let bevel_parms: ParmSpec[20] = [
ParmSpec("Width", ParmKind.number, 0.1, 0.0, 100000.0, 1, "", "How far the bevel reaches; Width Type says how it is measured (Percent reads it as a percentage).", 1.0),
ParmSpec("Segments", ParmKind.integer, 1.0, 1.0, 64.0, 1, "", "Faces across each strip."),
ParmSpec("Shape", ParmKind.number, 0.5, 0.0, 1.0, 1, "", "The profile across a strip: 0.5 round, 0.25 a straight chamfer, 1 the square corner, 0 hollowed in to the opposite corner."),
ParmSpec("Width Type", ParmKind.menu, 0.0, 0.0, 4.0, 1, "Offset|Width|Depth|Percent|Absolute",
"Offset: from the edge to the strip's sides across the faces. Width: the strip's own width. Depth: from the edge straight in to the strip. Percent: a share of the edges beside. Absolute: along the edges beside."),
ParmSpec("Affect", ParmKind.menu, 0.0, 0.0, 2.0, 1, "Auto|Edges|Vertices", "Bevel edges, or cut off points; Auto cuts off points for a group of points and bevels edges otherwise."),
ParmSpec("Clamp overlap", ParmKind.toggle, 1.0, 0.0, 1.0, 1, "", "Keep the width below where strips and patches would cross.", more = true),
ParmSpec("Loop slide", ParmKind.toggle, 1.0, 0.0, 1.0, 1, "", "Slide the strip's sides along an unbeveled edge between two beveled ones, keeping edge loops straight.", more = true),
ParmSpec("Profile Type", ParmKind.menu, 0.0, 0.0, 1.0, 1, "Superellipse|Custom", "The profile's shape: the superellipse Shape sets, or the Custom profile ramp.", more = true),
ParmSpec("Miter Outer", ParmKind.menu, 0.0, 0.0, 2.0, 1, "Sharp|Patch|Arc", "Where three or more beveled edges meet round an outside corner over 180°: a sharp miter, a patch, or an arc.", more = true),
ParmSpec("Miter Inner", ParmKind.menu, 0.0, 0.0, 1.0, 1, "Sharp|Arc", "Where beveled edges meet inside a corner under 180°: a sharp miter or an arc, spread apart by Spread.", more = true),
ParmSpec("Spread", ParmKind.number, 0.1, 0.0, 100000.0, 1, "", "How far an inner arc miter's points spread along their edges.", 1.0, more = true),
ParmSpec("Intersection", ParmKind.menu, 0.0, 0.0, 1.0, 1, "Grid Fill|Cutoff", "How a corner where three or more beveled edges meet fills: a grid of faces following the profiles, or a face cutting off each strip's end.", more = true),
ParmSpec("Harden normals", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "Give the bevel's corners normals that blend into the faces beside it (writes N on corners).", more = true),
ParmSpec("Mark seams", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "Carry a beveled edge's seam across the corner patches to the next beveled seam.", more = true),
ParmSpec("Mark sharp", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "Carry a beveled edge's sharp mark across the corner patches to the next sharp one.", more = true),
ParmSpec("Face strength", ParmKind.menu, 0.0, 0.0, 3.0, 1, "None|New|Affected|All",
"Write face_strength for weighted normals: 1 weak on corner patches, 2 medium on strips, 3 strong on the faces kept; New the bevel's faces, Affected also the rebuilt faces, All every face.", more = true),
ParmSpec("Material", ParmKind.integer, -1.0, -1.0, 1000.0, 1, "", "The new faces' material: -1 keeps the material of the faces beside them, 0 and up the mesh's material of that number (in order of first use).", more = true),
ParmSpec("Limit", ParmKind.menu, 0.0, 0.0, 2.0, 1, "None|Angle|Weight", "Bevel only the group's edges whose faces meet at more than Angle, or whose bevel_weight is above 0 (the weight also scales the width).", more = true),
ParmSpec("Angle", ParmKind.number, 30.0, 0.0, 180.0, 1, "", "With Limit Angle: the least angle between an edge's face normals, in degrees.", more = true),
ParmSpec("Custom profile", ParmKind.ramp, 0.0, 0.0, 1.0, 27, "",
"With Profile Type Custom: the profile across a strip, its depth from the straight chamfer (0) to the square corner (1) at each place across (0 to 1).", more = true, initials = default_custom_profile)]
## The Custom profile's default: a round profile (the circle's depth at its
## middle is sqrt(2) - 1), smooth.
let default_custom_profile: f64[12] = [1.0, 1.0, 3.0, 0.0, 0.0, 2.0, 0.5, 0.41421356237309515, 2.0, 1.0, 0.0, 2.0]
## Blender's modal keys while Bevel's drag lasts: A, P and S choose what the
## pointer sets (the width, the shape, the segments), M, V, O, I, Z and N
## cycle the menus, C, H, U, K and L flip the toggles.
let bevel_keys: KeySpec[14] = [
KeySpec("A", 0, "drag", "length"),
KeySpec("P", 2, "drag", "amount"),
KeySpec("S", 1, "drag", "amount"),
KeySpec("M", 3, "cycle"),
KeySpec("V", 4, "cycle"),
KeySpec("O", 8, "cycle"),
KeySpec("I", 9, "cycle"),
KeySpec("Z", 7, "cycle"),
KeySpec("N", 11, "cycle"),
KeySpec("C", 5, "toggle"),
KeySpec("H", 12, "toggle"),
KeySpec("U", 13, "toggle"),
KeySpec("K", 14, "toggle"),
KeySpec("L", 6, "toggle")]
let bevel_picks: PickSpec[1] = [PickSpec("Edges or points", "edges points")]
## Points cut off as they are; other groups become edges.
let bevel_also: Domain[1] = [Domain.point]
func bevel_verb() -> Verb:
return Verb("PolyBevel", "Modeling", "Bevel the group's edges into strips with corner patches, or cut off its points", Domain.edge, true, bevel_parms, true, cook_bevel, false, bevel_also,
label = "Bevel", section = "Modify", family = "Bevel", icon = "bevel", picks = bevel_picks, drag = 0, wheel = 1, keys = bevel_keys)
## Where the Custom profile's numbers start among the run's numbers.
let custom_profile_number: usize = 19
## Width types.
let width_offset: i64 = 0
let width_width: i64 = 1
let width_depth: i64 = 2
let width_percent: i64 = 3
let width_absolute: i64 = 4
## Miter kinds (Miter Inner's Arc is `miter_arc` too).
let miter_sharp: i64 = 0
let miter_patch: i64 = 1
let miter_arc: i64 = 2
## Limits.
let limit_angle: i64 = 1
let limit_weight: i64 = 2
## One run's settings, read from its numbers.
struct BevelSettings:
let width: f64
let segments: usize
let shape: f64
## The superellipse exponent the shape gives (see bevel_profile.lucb).
let exponent: f64
let width_type: i64
let vertices: bool
let clamp: bool
let loop_slide: bool
let custom: bool
let miter_outer: i64
let miter_inner: i64
let spread: f64
let cutoff: bool
let harden: bool
let mark_seam: bool
let mark_sharp: bool
let strength: i64
let material: i64
let limit: i64
## Angle in radians.
let angle: f64
## The Custom profile's ramp numbers, its size exactly.
let curve: const f64[]
## Whether widths are evened along loop-slid chains afterwards.
let even_widths: bool
func bevel_settings(input: const VerbInput*) -> BevelSettings!:
let width = input.parms.at(0)
if not math.is_finite(width) or width < 0.0: error(errors.invalid, "Bevel width must be finite and not negative")
let segments = (usize)math.max(1.0, math.min(64.0, math.round(input.parms.at(1))))
let shape = math.max(0.0, math.min(1.0, input.parms.at(2)))
let width_type = (i64)math.max(0.0, math.min(4.0, math.round(input.parms.at(3))))
let affect = (i64)math.round(input.parms.at(4))
let vertices = affect == 2 or (affect == 0 and input.domain == Domain.point)
let custom = input.parms.at(7) == 1.0
let cutoff = input.parms.at(11) == 1.0
var outer = (i64)math.max(0.0, math.min(2.0, math.round(input.parms.at(8))))
var inner = miter_arc if input.parms.at(9) == 1.0 else miter_sharp
# A cut-off corner has no use for special miters.
if cutoff:
outer = miter_sharp
inner = miter_sharp
let spread = input.parms.at(10)
if not math.is_finite(spread) or spread < 0.0: error(errors.invalid, "Bevel spread must be finite and not negative")
let angle = input.parms.at(18)
if not math.is_finite(angle): error(errors.invalid, "Bevel angle must be finite")
let curve = try custom_curve(input.parms.numbers, custom)
let limit = (i64)math.max(0.0, math.min(2.0, math.round(input.parms.at(17))))
let strength = (i64)math.max(0.0, math.min(3.0, math.round(input.parms.at(15))))
return BevelSettings(width, segments, shape, superellipse_exponent(shape), width_type, vertices, input.parms.at(5) != 0.0, input.parms.at(6) != 0.0, custom, outer, inner, spread,
cutoff, input.parms.at(12) != 0.0, input.parms.at(13) != 0.0, input.parms.at(14) != 0.0, strength,
(i64)math.round(input.parms.at(16)), limit, angle * math.pi / 180.0, curve,
not vertices and width_type != width_percent and width_type != width_absolute)
## The Custom profile's ramp among `numbers`, cut to its size; checked when
## it is used.
func custom_curve(numbers: const f64[], custom: bool) -> const f64[]!:
let first = custom_profile_number
if numbers.length < first + 3:
if custom: error(errors.invalid, "Bevel's custom profile is missing")
return numbers[..<0]
let points = numbers[first + 2]
if points < 1.0 or points > 8.0 or points != math.floor(points) or first + 3 + (usize)points * 3 > numbers.length:
if custom: error(errors.invalid, "Bevel's custom profile needs 1 to 8 points")
return numbers[..<0]
let curve = numbers[first..<first + 3 + (usize)points * 3]
if custom:
ramp.check(curve) catch failure:
error(errors.invalid, "Bevel's custom profile is not a valid ramp")
if ramp.channels(curve) != 1: error(errors.invalid, "Bevel's custom profile is a float ramp")
return curve
func cook_bevel(input: const VerbInput*, output: VerbOutput*) -> !:
let settings = try bevel_settings(input)
var work = try Bevel.start(input.mesh, settings)
defer work.close()
try choose_beveled(&work, input)
if work.vertices.count == 0 or settings.width == 0.0:
output.warning = "Nothing in the group bevels (or the width is 0); the input passes through"
if work.skipped_boundary > 0: output.warning = "The group's edges are all on the boundary (one face) and do not bevel; the input passes through"
return
try build_spacing(&work)
if settings.clamp: try limit_widths(&work)
try reserve_rings(&work)
try build_rings(&work)
if settings.even_widths: try even_widths(&work)
if settings.custom: orient_profiles(&work)
try build_vertex_meshes(&work)
try emit(&work, output)