dymokomi/luce-geocore
Source of release 0.7.9
0.7.9 / src / verbs / bevel_corners.lucb
## Special vertex meshes of the Grid Fill patch.
##
## - A cube-like corner (three beveled edges, equal widths, faces meeting
## near square): the patch is an octant of a superellipsoid (a sphere for
## the round shape) built on the unit cube's corner and mapped onto the
## corner's three boundary points and the vertex. The square and hollow
## shapes have exact grids of their own.
## - A pipe (two beveled edges in line, faces along them): the grid's
## interior points snap to the pipe's profile across it.
## - The square shape with three or more beveled edges: offset lines
## parallel to each edge, meeting along center lines.
## Whether vertex `slot` is a cube-like corner: -1 not, 0 near enough for
## the octant patch, 1 exactly three edges, all beveled.
func corner_test(work: const Bevel*, slot: i32) -> i64:
if work.settings.vertices or work.settings.custom: return -1
let vertex = work.vertices.items[(usize)slot]
if vertex.bounds != 3: return 0
let width = work.halves.items[(usize)vertex.first_half].offset_left
var total = 0.0
var flat: u32 = 0
for at in vertex.first_half..<vertex.first_half + vertex.halves:
let angle = signed_face_angle(work, (i32)at)
let size = math.abs(angle)
if size <= math.pi * 0.25: flat += 1
elif size >= math.pi * 0.75: return -1
if work.halves.items[at].beveled and math.abs(work.halves.items[at].offset_left - width) > 0.00001: return -1
total += angle
if flat != vertex.halves - 3: return -1
let off = math.abs(math.abs(total) - 1.5 * math.pi)
if (work.settings.exponent == exponent_square and off > math.pi / 16.0) or off > math.pi * 0.25: return -1
if vertex.halves != 3 or vertex.selected != 3: return 0
return 1
## The angle between the faces either side of `half`'s edge (between their
## normals), negative where the edge is concave; 0 on a boundary.
func signed_face_angle(work: const Bevel*, half: i32) -> f64:
let item = work.halves.items[(usize)half]
if item.before < 0 or item.after < 0: return 0.0
let angle = angle_between(normal_of(work, item.before), normal_of(work, item.after))
let topology = &work.topology
var center = Vector3()
let first = (usize)topology.offsets[(usize)item.after]
let end = (usize)topology.offsets[(usize)item.after + 1]
for corner in first..<end: center = center.add(input_point(work, topology.corners[corner]))
center = center.multiply_scalar(1.0 / (f64)(end - first))
let rise = center.subtract(vertex_at(work, item.slot)).dot(normal_of(work, item.before))
return -angle if rise > tiny else angle
## The cube-like corner's patch: the octant mapped onto vertex `slot`.
func corner_grid(work: const Bevel*, slot: i32) -> Grid!:
let vertex = work.vertices.items[(usize)slot]
let a = work.bounds.items[(usize)vertex.first_bound].position
let b = work.bounds.items[(usize)vertex.first_bound + 1].position
let c = work.bounds.items[(usize)vertex.first_bound + 2].position
let d = vertex_at(work, slot)
# The affine map taking the unit axes to a, b, c and (1, 1, 1) to d.
let origin = a.add(b).add(c).subtract(d).multiply_scalar(0.5)
let x = a.subtract(origin)
let y = b.subtract(origin)
let z = c.subtract(origin)
var grid = try octant_grid(work)
for at in 0..<grid.points.count:
let p = grid.points.items[at]
grid.points.items[at] = origin.add(x.multiply_scalar(p.x)).add(y.multiply_scalar(p.y)).add(z.multiply_scalar(p.z))
for i in 0..<(usize)3: grid.profiles.items[i] = work.bounds.items[(usize)vertex.first_bound + i].profile
return grid
## The unit cube's corner (1, 1, 1) beveled: boundary points on the axes,
## quarter superellipses between them, the patch an octant of the
## superellipsoid |x|^r + |y|^r + |z|^r = 1 with the run's segments.
func octant_grid(work: const Bevel*) -> Grid!:
let segments = work.settings.segments
let r = work.settings.exponent
if r == exponent_square: return try square_octant(segments)
if r == exponent_hollow: return try hollow_octant(segments)
var coarse = try new_grid(3, 2)
for i in 0..<(usize)3:
let start = axis_point(i)
let end = axis_point((i + 1) % 3)
var profile = blank_profile()
profile.exponent = r
profile.start = start
profile.end = end
profile.middle = start.add(end)
profile.plane_point = start
profile.plane_normal = start.cross(end)
profile.projection = profile.plane_normal
coarse.profiles.items[i] = profile
coarse.put(i, 0, 0, start)
coarse.put(i, 0, 1, profile_point(work, &profile, 1, 2))
# The center on the superellipsoid's diagonal.
let m = math.pow(3.0, -1.0 / r)
coarse.put(0, 1, 1, Vector3(m, m, m))
copy_equivalents(&coarse)
var grid = try refined(work, coarse)
for at in 0..<grid.points.count: grid.points.items[at] = onto_superellipsoid(grid.points.items[at], r, false)
return grid
func axis_point(axis: usize) -> Vector3:
return Vector3(1.0 if axis == 0 else 0.0, 1.0 if axis == 1 else 0.0, 1.0 if axis == 2 else 0.0)
## `p` (in the positive octant) moved radially onto |x|^r + |y|^r + |z|^r =
## 1; the square shapes (only in 2D) snap to the nearer leg, or with
## `midline` to the corner.
func onto_superellipsoid(p: Vector3, r: f64, midline: bool) -> Vector3:
if r == exponent_circle: return unit_of(p)
let a = math.max(0.0, p.x)
let b = math.max(0.0, p.y)
let c = math.max(0.0, p.z)
if r == exponent_square or r == exponent_hollow:
var x = math.min(1.0, a)
var y = math.min(1.0, b)
if r == exponent_square:
if 1.0 - x < 1.0 - y:
x = 1.0
if midline: y = 1.0
else:
y = 1.0
if midline: x = 1.0
else:
if x < y:
x = 0.0
if midline: y = 0.0
else:
y = 0.0
if midline: x = 0.0
return Vector3(x, y, 0.0)
let inverse = 1.0 / r
if a == 0.0:
if b == 0.0: return Vector3(0.0, 0.0, math.pow(c, inverse))
let y = math.pow(1.0 / (1.0 + math.pow(c / b, r)), inverse)
return Vector3(0.0, y, c * y / b)
let x = math.pow(1.0 / (1.0 + math.pow(b / a, r) + math.pow(c / a, r)), inverse)
return Vector3(x, b * x / a, c * x / a)
## The square shape's cube corner: each sector a flat quarter of the face
## it lies in.
func square_octant(segments: usize) -> Grid!:
let half = segments // 2
var grid = try new_grid(3, segments)
for i in 0..<(usize)3:
grid.profiles.items[i] = blank_profile()
for j in 0..<half + 1:
for k in 0..<half + 1:
if not is_canonical(segments, i, j, k): continue
var p = axis_point(i)
p = p.with_coordinate((i + 1) % 3, (f64)k * 2.0 / (f64)segments)
p = p.with_coordinate((i + 2) % 3, (f64)j * 2.0 / (f64)segments)
grid.put(i, j, k, p)
copy_equivalents(&grid)
return grid
## The hollow shape's cube corner: only its boundary (the profiles run
## along the axes into the opposite corner and meet there).
func hollow_octant(segments: usize) -> Grid!:
let half = segments // 2
let step = 2.0 / (2.0 * (f64)half + math.sqrt(2.0)) if segments % 2 == 1 else 2.0 / (f64)segments
var grid = try new_grid(3, segments)
for i in 0..<(usize)3:
grid.profiles.items[i] = blank_profile()
for k in 0..<half + 1:
grid.put(i, 0, k, Vector3().with_coordinate(i, 1.0 - (f64)k * step))
grid.put(i, 0, segments - k, Vector3().with_coordinate((i + 1) % 3, 1.0 - (f64)k * step))
return grid
## A pipe: two beveled edges in line (with one or two more beveled edges
## off it), every face along the pipe. Returns the boundary point whose
## beveled edge starts the pipe, or -1.
func pipe_test(work: const Bevel*, slot: i32) -> i32:
let vertex = work.vertices.items[(usize)slot]
if vertex.bounds < 3 or vertex.bounds > 4 or vertex.selected < 3 or vertex.selected > 4: return -1
let here = vertex_at(work, slot)
var found: i32 = -1
var along = Vector3()
for at in vertex.first_bound..<vertex.first_bound + vertex.bounds:
let one = (i32)at
let two = next_bound(work, one)
let three = next_bound(work, two)
let first = work.bounds.items[(usize)one].bevel
let third = work.bounds.items[(usize)three].bevel
if first < 0 or work.bounds.items[(usize)two].bevel < 0 or third < 0: continue
let dir1 = unit_of(here.subtract(input_point(work, work.halves.items[(usize)first].other)))
let dir3 = unit_of(input_point(work, work.halves.items[(usize)third].other).subtract(here))
if angle_between(dir1, dir3) < tiny_angle:
found = one
along = dir1
break
if found < 0: return -1
for at in vertex.first_half..<vertex.first_half + vertex.halves:
let after = work.halves.items[at].after
if after >= 0 and math.abs(along.dot(normal_of(work, after))) > tiny_big: return -1
return found
## The pipe's patch: the Grid Fill patch with its interior points snapped
## to the pipe's profile in the plane across the pipe through each (a
## Custom profile's shape is carried across instead).
func pipe_grid(work: const Bevel*, slot: i32, pipe: i32) -> Grid!:
var grid = try grid_patch(work, slot)
let vertex = work.vertices.items[(usize)slot]
let count = (usize)vertex.bounds
let segments = work.settings.segments
let half = segments // 2
let first_pipe = (usize)(pipe - (i32)vertex.first_bound)
let second_pipe = (usize)(next_bound(work, next_bound(work, pipe)) - (i32)vertex.first_bound)
let even = segments % 2 == 0
for i in 0..<count:
for j in 1..<half + 1:
for k in 0..<half + 1:
if not is_canonical(segments, i, j, k): continue
if work.settings.custom:
var a = Vector3()
var b = Vector3()
var f = 0.0
if i == first_pipe or i == second_pipe:
if count == 3 and i == first_pipe:
let ring = j if j > k else k
b = grid.at(i, 0, ring)
a = canonical_at(&grid, i, ring, 0)
f = (math.min((f64)j, (f64)k) if k < j else 2.0 * (f64)ring - (f64)j) / (2.0 * (f64)ring)
else:
a = grid.at(i, 0, k)
b = grid.at(second_pipe if i == first_pipe else first_pipe, 0, segments - k)
f = (f64)j / (f64)segments
else:
a = canonical_at(&grid, i, j, 0)
b = canonical_at(&grid, i, j, segments)
f = (f64)k / (f64)segments
grid.put(i, j, k, lerp(a, b, f))
else:
let midline = even and k == half and ((i == 0 and j == half) or i == first_pipe or i == second_pipe)
grid.put(i, j, k, onto_pipe(work, pipe, midline, grid.at(i, j, k)))
copy_equivalents(&grid)
return grid
## `p` moved onto the pipe's profile, in the plane across the pipe through
## `p`.
func onto_pipe(work: const Bevel*, pipe: i32, midline: bool, p: Vector3) -> Vector3:
let profile = work.bounds.items[(usize)pipe].profile
if profile.start.subtract(profile.end).length() < 0.0000001: return profile.start
let half = work.halves.items[(usize)work.bounds.items[(usize)pipe].bevel]
let along = unit_of(vertex_at(work, half.slot).subtract(input_point(work, half.other)))
let start = onto_plane(profile.start, p, along)
let end = onto_plane(profile.end, p, along)
let middle = onto_plane(profile.middle, p, along)
var flat = profile
flat.start = start
flat.end = end
flat.middle = middle
let map = square_map(&flat) else return nearest_on_segment(p, start, end)
# Into the unit square (z across it), onto the superellipse, and back.
let z_axis = unit_of(map.2.cross(map.1))
let local = solve3(map.1, map.2, z_axis, p.subtract(map.0)) else return nearest_on_segment(p, start, end)
let snapped = onto_superellipsoid(local, profile.exponent, midline)
return map.0.add(map.1.multiply_scalar(snapped.x)).add(map.2.multiply_scalar(snapped.y)).add(z_axis.multiply_scalar(snapped.z))
## The coordinates of `v` in the basis a, b, c (Cramer's rule), if any.
func solve3(a: Vector3, b: Vector3, c: Vector3, v: Vector3) -> Vector3?:
let det = a.dot(b.cross(c))
if math.abs(det) < 0.000000000001: return none
return Vector3(v.dot(b.cross(c)) / det, a.dot(v.cross(c)) / det, a.dot(b.cross(v)) / det)
## The square shape with three or more beveled edges (an even count of
## segments): lines parallel to each beveled edge's sides, spaced across
## the half strips, meeting along a center line in each sector from the
## sector's edge to the vertex.
func square_grid(work: const Bevel*, slot: i32) -> Grid!:
let vertex = work.vertices.items[(usize)slot]
let count = (usize)vertex.bounds
let segments = work.settings.segments
let half = segments // 2
let odd = segments % 2 == 1
var step = 1.0 / (f64)half
let here = vertex_at(work, slot)
var grid = try new_grid(count, segments)
errdefer grid.close()
var centers = Growing[Vector3]()
defer centers.close()
try centers.fill(count * (half + 1), Vector3())
var placed = Growing[u8]()
defer placed.close()
try placed.fill(count, 0)
var i: usize = 0
while i < count:
let bound_index = (i32)vertex.first_bound + (i32)i
let bound = work.bounds.items[(usize)bound_index]
grid.profiles.items[i] = bound.profile
var kind: i64 = 0
if bound.first >= 0 and bound.last >= 0: kind = angle_kind(work, bound.first, bound.last)
if bound.patch_start:
centers.items[i * (half + 1)] = midpoint(bound.position, work.bounds.items[(usize)next_bound(work, bound_index)].position)
placed.items[i] = 1
i += 1
if i < count:
let middle = work.bounds.items[(usize)vertex.first_bound + i]
centers.items[i * (half + 1)] = midpoint(middle.position, work.bounds.items[(usize)next_bound(work, (i32)vertex.first_bound + (i32)i)].position)
placed.items[i] = 1
i += 2
continue
if bound.arc_start:
centers.items[i * (half + 1)] = bound.profile.middle
placed.items[i] = 1
i += 2
continue
if kind == -1:
let first_point = input_point(work, work.halves.items[(usize)bound.first].other)
let last_point = input_point(work, work.halves.items[(usize)bound.last].other)
let dir1 = here.subtract(first_point)
let dir2 = here.subtract(last_point)
let (k1, on_first, _) = line_meet(here, first_point, bound.position, bound.position.add(dir2))
let (k2, on_last, _) = line_meet(here, last_point, bound.position, bound.position.add(dir1))
let previous = (i + count - 1) % count
if k2 != 0:
if placed.items[i] == 1: centers.items[i * (half + 1)] = closer_point(centers.items[i * (half + 1)], on_last, here)
else: centers.items[i * (half + 1)] = on_last
placed.items[i] = 1
if k1 != 0:
if placed.items[previous] == 1: centers.items[previous * (half + 1)] = closer_point(centers.items[previous * (half + 1)], on_first, here)
else: centers.items[previous * (half + 1)] = on_first
placed.items[previous] = 1
i += 1
# Center lines still unset start on the edge after their sector.
for at in 0..<count:
if placed.items[at] == 1: continue
let bound_index = (i32)vertex.first_bound + (i32)at
let bound = work.bounds.items[(usize)bound_index]
let following = work.bounds.items[(usize)next_bound(work, bound_index)]
let edge = following.first
if edge >= 0:
let edge_far = input_point(work, work.halves.items[(usize)edge].other)
let before_arc = work.bounds.items[(usize)previous_bound(work, bound_index)].arc_start
if before_arc and following.arc_start:
let (meets, meet, _) = line_meet(here, edge_far, bound.position, following.position)
if meets != 0:
centers.items[at * (half + 1)] = meet
placed.items[at] = 1
else:
let from_point = bound.position if before_arc else following.position
centers.items[at * (half + 1)] = nearest_on_segment(from_point, here, edge_far)
placed.items[at] = 1
if placed.items[at] == 0: centers.items[at * (half + 1)] = midpoint(bound.position, following.position)
# Each center line runs from its start toward the vertex.
for at in 0..<count:
if odd:
let bound = work.bounds.items[(usize)vertex.first_bound + at]
let following = work.bounds.items[(usize)next_bound(work, (i32)vertex.first_bound + (i32)at)]
let angle = 0.5 * corner_angle(bound.position, centers.items[at * (half + 1)], following.position)
let reach = math.min(0.8, 0.5 / math.sin(angle)) if angle > small_angle else 0.8
step = 1.0 / ((f64)half + reach)
let start = centers.items[at * (half + 1)]
for j in 1..<half + 1: centers.items[at * (half + 1) + j] = lerp(start, here, (f64)j * step)
# The sectors' edges, then their interiors where the lines cross.
for at in 0..<count:
let bound = work.bounds.items[(usize)vertex.first_bound + at]
let previous_line = ((at + count - 1) % count) * (half + 1)
for j in 0..<half + (1 if odd else 0):
grid.put(at, j, 0, lerp(bound.position, centers.items[previous_line], (f64)j * step))
for k in 1..<half + 1:
grid.put(at, 0, k, lerp(bound.position, centers.items[at * (half + 1)], (f64)k * step))
if not odd: grid.put(0, half, half, here)
copy_equivalents(&grid)
for at in 0..<count:
let previous_line = ((at + count - 1) % count) * (half + 1)
for j in 1..<half + (1 if odd else 0):
for k in 1..<half + 1:
let (meets, meet_a, meet_b) = line_meet(grid.at(at, 0, k), centers.items[previous_line + k], grid.at(at, j, 0), centers.items[at * (half + 1) + j])
var position = meet_a
if meets == 0: position = lerp(grid.at(at, 0, k), centers.items[previous_line + k], (f64)j * step)
elif meets == 2: position = midpoint(meet_a, meet_b)
grid.put(at, j, k, position)
copy_equivalents(&grid)
return grid
## Whichever of `a` and `b` is nearer `point`.
func closer_point(a: Vector3, b: Vector3, point: Vector3) -> Vector3:
return a if a.subtract(point).length() <= b.subtract(point).length() else b