dymokomi/luce-cad
Source of release 0.9.5
0.9.5 / src / modeling / full_round.lucb
## Fusion's Full Round Fillet: a center face rounded away whole, the round
## tangent to it and to the side faces on each side. For a flat center face
## between sides along it, the round is the circle tangent to all three
## faces in the section across them: it is the fillet, of that one radius,
## of both the center face's edges with the sides, the two fillets touching
## the center face at the same line, so the face goes and the two strips
## are one surface.
##
## The center face's edges with the sides run alike: parallel lines (a
## rib's top, the sides flat, tapered, or curved along it: cylinders whose
## axes run with the edges) or concentric arcs (a curved rib: the sides
## cylinders or cones about the center face's normal). Every section across
## them is then the same, and the circle is solved in one: its center X and
## radius r with each face's outward signed distance at X equal to -r,
## Newton from the circle tangent to the faces' tangent planes at the edges
## (Cramer's rule on n·X + r = h for each), which is the answer when they
## are flat.
## The sides, when not named, are the center face's neighbors across its
## longest pair of such edges. G2 and G3 rounds are two halves meeting
## where the center face was, joined into one face (strip_join.lucb), as
## Fusion's full round is one face.
## How far past a rib's ends a curved rib's round reaches: this share of
## its span plus this many radians (at most a quarter of what is left of
## the turn).
let rib_margin_share: f64 = 0.05
let rib_margin_turn: f64 = 0.01
## A rib end's plane whose cos-sin amplitude is below this never crosses
## the round.
let least_rib_amplitude: f64 = 1e-12
## The inscribed circle: the determinant below which no circle touches all
## three faces, and its Newton solve's residual and central-difference step
## as shares of the scale.
let inscribed_singular: f64 = 1e-12
let inscribed_residual_share: f64 = 1e-14
let inscribed_difference_share: f64 = 1e-7
## `model` with face `center` rounded fully between faces `side1` and
## `side2` (empty: found as above), of `continuity` (1 G1, 2 G2, 3 G3), the
## round named for feature `tag`.
func full_rounded(model: const CadModel*, center: str, side1: str, side2: str, tag: str, continuity: i64) -> interop.Reference[CadModel]!:
var w = try shape_of(model)
defer w.close()
let c = try named_face(&w, center)
if w.surfaces.items[(usize)w.faces.items[c].surface].kind != surface_plane: error(invalid, "a full round takes a flat center face")
var sides: usize[2] = [0, 0]
if side1 != "" and side2 != "":
sides[0] = try named_face(&w, side1)
sides[1] = try named_face(&w, side2)
else:
sides = try round_sides(&w, c)
# The edges between the center and each side: alike, one per side.
var edges = Growing[i64]()
defer edges.close()
var first: i64 = -1
for use in w.face_uses(c):
let e = edge_index(use)
let (fa, fb) = try edge_faces(&w, e)
let other = fb if fa == c else fa
if other != sides[0] and other != sides[1]: continue
if first >= 0 and not try alike(&w, (usize)first, e): error(invalid, "a full round's center face meets its sides along parallel lines or concentric arcs")
if first < 0: first = (i64)e
try edges.push((i64)e)
if edges.count < 2: error(invalid, "the center face does not meet both sides")
let (radius, center_point) = try inscribed_circle(&w, c, sides, (usize)first)
let second = (usize)edges.items[1] if edges.items[0] == first else (usize)edges.items[0]
let rounded = try round_body(model, &w, c, sides, edges.view(), (usize)first, second, radius, center_point, continuity, tag)
if continuity == 1: return rounded
# G2 and G3: two halves meeting where the center face was, made one
# face (strip_join.lucb), as Fusion's full round is.
defer rounded.release()
var name_a = Growing[u8]()
defer name_a.close()
var name_b = Growing[u8]()
defer name_b.close()
try blend_name(&w, (usize)first, tag, &name_a)
try blend_name(&w, second, tag, &name_b)
var both = Growing[u8]()
defer both.close()
try round_name(name_b.view(), name_a.view(), &both)
let joined = try seam_joined(rounded.value(), (str)name_a.view(), (str)name_b.view(), (str)both.view())
if let one = joined: return one
error(invalid, "a full round's halves did not join into one face")
## The full round of `model` (`w` its shape) over face `c` between
## `sides`, their edges with it `edges` (`first` and `other` one each side),
## of radius `radius` about `center`: G2 and G3 as two halves.
func round_body(model: const CadModel*, w: const Shape*, c: usize, sides: const usize[2], edges: const i64[], first: usize, other: usize, radius: f64, center: Vector3, continuity: i64, tag: str) -> interop.Reference[CadModel]!:
if w.carriers.items[(usize)w.edges.items[first].carrier].kind != curve_circle:
var text = Growing[u8]()
defer text.close()
try references_of(w, edges, &text)
return try blended(model, (str)text.view(), radius, true, tag, continuity, false)
# Arcs (a curved rib): the slab over the round's chord taken away and
# the cap under the round added back (round_part), each turned past the
# rib's ends and trimmed by them.
var section = try rib_section(w, c, first, other, radius, center)
var trims = Growing[usize]()
defer trims.close()
try rib_ends(w, c, sides, §ion, &trims)
let slab = try round_part(w, first, other, radius, §ion, continuity, tag, 0)
defer slab.release()
let slab_trimmed = try ends_trimmed(slab.value(), w, trims.view(), §ion)
defer slab_trimmed.release()
let cut = try combine(model, slab_trimmed.value(), op_subtract)
defer cut.release()
let cap = try round_part(w, first, other, radius, §ion, continuity, tag, 1)
defer cap.release()
let cap_trimmed = try ends_trimmed(cap.value(), w, trims.view(), §ion)
defer cap_trimmed.release()
return try combine(cut.value(), cap_trimmed.value(), op_union)
## A curved rib's full round in the section through its arc's middle `p`
## (along the arc `along`, the center face's normal `up`, across to the
## other arc `width`): the sides' outward normals, the round's center, top
## and side contacts, the slab's corners above the center face; the arcs'
## axis (`origin`, `axis` turning the way along), and the turn from `low`
## to `high` about it that the round's tools sweep (rib_ends sets it past
## the rib's ends).
struct RibSection:
var p: Vector3
var along: Vector3
var up: Vector3
var width: Vector3
var na: Vector3
var nb: Vector3
var center: Vector3
var mid: Vector3
var contact_a: Vector3
var contact_b: Vector3
var out_a: Vector3
var out_b: Vector3
var above_a: Vector3
var above_b: Vector3
var origin: Vector3
var axis: Vector3
var low: f64
var high: f64
## The slab's corners: the chord's end past side a's contact, up beside
## side a, across above, down beside side b, the chord's other end.
func corners() -> Vector3[4]:
return [self.out_a, self.above_a, self.above_b, self.out_b]
## The section of a full round of radius `radius` about `center` over
## center face `c` between its arcs `ea` and `eb` (see RibSection), turned
## over the arc's own span.
func rib_section(w: const Shape*, c: usize, ea: usize, eb: usize, radius: f64, center: Vector3) -> RibSection!:
let edge = w.edges.items[ea]
let carrier = w.carriers.items[(usize)edge.carrier]
let p = try w.edge_at(ea, 0.5)
let along = try along_edge(w, ea)
let side_a = try other_side(w, c, ea)
let side_b = try other_side(w, c, eb)
let q = try section_foot(w, c, side_b, p, along)
let up = try w.face_normal(c, p)
# Each side's outward normal at its contact (the same along its line in
# the section: a cylinder's or cone's about the axis).
let na = try w.face_normal(side_a, p)
let nb = try w.face_normal(side_b, q)
# The chord on past each contact by the radius, out of the body, then
# straight up (along the axis: walls of cylinders about it) to the
# radius above the center face. The slab crosses the sides along the
# contacts and lies on none of them (a slab face on a side's surface,
# or its edge along one, leaves the booleans coincident pieces to
# part), and leaning ends meet its walls in ellipses, not in curves to
# trace.
let contact_a = center.add(na.multiply_scalar(radius))
let contact_b = center.add(nb.multiply_scalar(radius))
let chord = try contact_a.subtract(contact_b).normalized()
let out_a = contact_a.add(chord.multiply_scalar(radius))
let out_b = contact_b.subtract(chord.multiply_scalar(radius))
let above_a = out_a.add(up.multiply_scalar(p.subtract(out_a).dot(up) + radius))
let above_b = out_b.add(up.multiply_scalar(p.subtract(out_b).dot(up) + radius))
var axis = carrier.x.cross(carrier.y)
if axis.cross(p.subtract(carrier.origin)).dot(along) < 0.0: axis = axis.multiply_scalar(-1.0)
let sweep = math.abs(edge.t1 - edge.t0)
return RibSection(p, along, up, try q.subtract(p).normalized(), na, nb, center, center.add(up.multiply_scalar(radius)),
contact_a, contact_b, out_a, out_b, above_a, above_b,
carrier.origin, axis, -sweep * 0.5, sweep * 0.5)
## A curved rib's ends, the faces at the center face's other edges: each
## must be flat. An end through the arcs' axis (square to the rib) ends the
## round's tools flush, at the arcs' own end. Past another (leaning, as an
## extrusion's drafted ends do) the section's turn reaches at every slab
## corner, by a margin, and the end goes into `trims`: the tools are
## trimmed by its plane (ends_trimmed).
func rib_ends(w: const Shape*, c: usize, sides: const usize[2], section: RibSection*, trims: Growing[usize]*) -> !:
var low = section.low
var high = section.high
var leaning_low = false
var leaning_high = false
for use in w.face_uses(c):
let g = edge_index(use)
let (fa, fb) = try edge_faces(w, g)
let f = fb if fa == c else fa
if f == sides[0] or f == sides[1]: continue
let s = &w.surfaces.items[(usize)w.faces.items[f].surface]
if s.kind != surface_plane: error(invalid, "a curved rib's full round needs its ends flat")
let through = math.abs(s.z.dot(section.axis)) < parallel_slack and math.abs(section.origin.subtract(s.origin).dot(s.z)) <= w.tolerance * 10.0
if through: continue
try trims.push(f)
# The end's side (the turn to its edge), then each corner's turn to
# its plane on that side.
let toward = turn_about(section, try w.edge_at(g, 0.5))
let n = try w.face_normal(f, s.origin)
if toward < 0.0 and not leaning_low:
low = 0.0
leaning_low = true
if toward >= 0.0 and not leaning_high:
high = 0.0
leaning_high = true
for corner in section.corners():
let turn = try plane_turn(section, corner, s.origin, n, toward)
if toward < 0.0:
low = math.min(low, turn)
else:
high = math.max(high, turn)
if trims.count == 0: return
if low >= 0.0 or high <= 0.0: error(invalid, "a curved rib's full round needs an end on each side of the round")
let span = high - low
let margin = math.min(rib_margin_share * span + rib_margin_turn, (2.0 * math.pi - span) * 0.25)
if not (margin > 0.0): error(invalid, "a curved rib's ends overlap round its axis: no full round fits")
section.low = (low - margin) if leaning_low else low
section.high = (high + margin) if leaning_high else high
## The turn about the section's axis from its middle `p` to point `x`.
func turn_about(section: const RibSection*, x: Vector3) -> f64:
let a = section.axis
let from_p = section.p.subtract(section.origin)
let from_x = x.subtract(section.origin)
let u = from_p.subtract(a.multiply_scalar(from_p.dot(a)))
let v = from_x.subtract(a.multiply_scalar(from_x.dot(a)))
return math.atan2(a.dot(u.cross(v)), u.dot(v))
## The turn about the section's axis that takes point `x` onto the plane
## through `o` facing `n`, the one nearest turn `toward`: a cos t + b sin t
## = c on the circle `x` runs round.
func plane_turn(section: const RibSection*, x: Vector3, o: Vector3, n: Vector3, toward: f64) -> f64!:
let a = section.axis
let rel = x.subtract(section.origin)
let level = a.multiply_scalar(rel.dot(a))
let radial = rel.subtract(level)
let cos_part = n.dot(radial)
let sin_part = n.dot(a.cross(radial))
let c = n.dot(o.subtract(section.origin.add(level)))
let amplitude = math.sqrt(cos_part * cos_part + sin_part * sin_part)
if amplitude <= least_rib_amplitude or math.abs(c) > amplitude: error(invalid, "a curved rib's end does not cross its round: no full round fits")
let phase = math.atan2(sin_part, cos_part)
let spread = math.acos(c / amplitude)
var best = 0.0
var nearest = 1.0e300
for t in [phase + spread, phase - spread]:
# The same turn within a half turn of `toward`.
var turn = t
while turn - toward > math.pi: turn -= 2.0 * math.pi
while toward - turn > math.pi: turn += 2.0 * math.pi
if math.abs(turn - toward) < nearest:
nearest = math.abs(turn - toward)
best = turn
return best
## `tool` (a round's slab or cap, turned past the rib's ends `ends`) with
## what lies past each end plane taken away: a box on the plane's outer side
## over the tool's end, no larger than it, so a rib turned more than half
## round keeps the tool's far part.
func ends_trimmed(tool: const CadModel*, w: const Shape*, ends: const usize[], section: const RibSection*) -> interop.Reference[CadModel]!:
if ends.length == 0: return try copy_model(tool)
var current = try copy_model(tool)
errdefer current.release()
let corners = section.corners()
var diameter = 0.0
for x in corners:
for y in corners: diameter = math.max(diameter, x.subtract(y).length())
for f in ends:
let s = &w.surfaces.items[(usize)w.faces.items[f].surface]
let n = try w.face_normal(f, s.origin)
# The tool's end past this plane: its corners at the turn there.
let toward = section.low if (try plane_turn(section, section.p, s.origin, n, 0.0)) < 0.0 else section.high
var depth = 0.0
var middle = Vector3()
for x in corners:
let tip = turned_point(x, section.origin, section.axis, toward)
depth = math.max(depth, n.dot(tip.subtract(s.origin)))
middle = middle.add(tip.multiply_scalar(0.25))
if not (depth > 0.0): error(invalid, "a curved rib's full round did not reach its end")
let side = diameter * 2.0 + depth * 4.0
let foot = middle.subtract(n.multiply_scalar(n.dot(middle.subtract(s.origin))))
let x = try across(n)
let box = try CadSolids.box(side, side, side)
defer box.release()
let placed = try box.value().placed(Matrix4(x, n.cross(x), n, foot.add(n.multiply_scalar(side * 0.5))))
defer placed.release()
let less = try combine(current.value(), placed.value(), op_subtract)
current.release()
current = less
return current
## A full round of a curved rib (radius `radius`; its section `section`)
## between its arcs `ea` and `eb` (concentric, the sides cylinders or cones
## about their axis), turned about the axis along the arcs over the
## section's turn: `part` 0 the slab over the chord between the side
## contacts (the chord on past them, walls beside the sides and a line
## above the center face), taken away; 1 the cap between the chord and the
## round, added back; both trimmed by the rib's ends after (ends_trimmed).
## The round touches the center face's plane along a circle, so one tool of
## the region over it would touch the body there, which the booleans cannot
## part; the slab and the cap meet the body only on their own faces. The
## round is a torus (G1; one face, named for both edges' blends) or, G2 and
## G3, two strips of revolution meeting where the center face was (joined
## after).
func round_part(w: const Shape*, ea: usize, eb: usize, radius: f64, section: const RibSection*, continuity: i64, tag: str, part: i64) -> interop.Reference[CadModel]!:
let carrier = w.carriers.items[(usize)w.edges.items[ea].carrier]
let a = section.axis
let o = section.origin
let back = section.low
let sweep = section.high - section.low
let along = section.along
let width = section.width
let center = section.center
let mid = section.mid
let contact_a = section.contact_a
let contact_b = section.contact_b
let na = section.na
let nb = section.nb
# The profile built where the tool starts.
let way = turned_direction(along, a, back)
let flat = turned_direction(width, a, back)
var profile = Profiles(turned_point(section.p, o, a, back), way, flat, way.cross(flat), Growing[ProfileEdge](), Growing[usize](), Growing[usize](), Growing[usize](),
Growing[ProfileSpline](), Growing[Vector3](), Growing[f64](), Growing[f64]())
defer profile.close()
try profile.loop_offsets.push(0)
let ta = turned_point(contact_a, o, a, back)
let tb = turned_point(contact_b, o, a, back)
let tm = turned_point(mid, o, a, back)
let tc = turned_point(center, o, a, back)
if part == 0:
# The slab: up beside side a, across above, down beside side b,
# the chord (on past both contacts) back.
let corners: Vector3[4] = [section.above_a, section.above_b, section.out_b, section.out_a]
var previous = turned_point(section.out_a, o, a, back)
for point in corners:
let next = turned_point(point, o, a, back)
try profile.edges.push(line_edge(previous, next))
previous = next
try profile.loop_offsets.push(profile.edges.count)
try single_region(&profile)
let slab = try BrepModel.held(w.tolerances)
defer slab.release()
try revolve_into(slab.value(), &profile, o, a, (carrier.a + radius) * turned_tool_share, "", sweep)
for face in 0..<slab.value().face_count():
try slab.value().set_face_name(face, "")
return try CadModel.of_brep(slab)
# The cap: the round first (faces 0, and 1 for G2/G3), from side b's
# contact over the top to side a's, then the chord back.
if continuity == 1:
let leaving = tb.subtract(tc)
let arriving = ta.subtract(tc)
let toward_top = tm.subtract(tc)
# Signed about the way along, the long way when the top is not on
# the short one.
var turn = math.atan2(way.dot(leaving.cross(arriving)), leaving.dot(arriving))
let to_top = math.atan2(way.dot(leaving.cross(toward_top)), leaving.dot(toward_top))
if (to_top > 0.0) != (turn > 0.0) or math.abs(to_top) > math.abs(turn): turn = turn - math.pi * 2.0 * (1.0 if turn > 0.0 else -1.0)
try profile.edges.push(ProfileEdge(1, tb, ta, tc, radius, turn, -1))
else:
for half in 0..<(usize)2:
let head = tb if half == 0 else tm
let tail = tm if half == 0 else ta
# Each half a fillet's section between its contacts, its legs
# meeting where the side's and the center face's tangents do.
let side_point = contact_b if half == 0 else contact_a
let side_normal = nb if half == 0 else na
let leg = turned_point(try lines_meet(side_point, along.cross(side_normal), mid, width), o, a, back)
let first_point = profile.spline_points.count
let knot_first = profile.spline_knots.count
try fillet_section(head, tail, leg, continuity, &profile.spline_points, &profile.spline_knots)
let count = profile.spline_points.count - first_point
while profile.spline_weights.count < profile.spline_points.count: try profile.spline_weights.push(1.0)
try profile.splines.push(ProfileSpline(first_point, count, knot_first, 5))
try profile.edges.push(ProfileEdge(2, head, tail, Vector3(), 0.0, 0.0, (i64)profile.splines.count - 1))
# The chord back: the cap meets the cut body on it.
try profile.edges.push(line_edge(ta, tb))
try profile.loop_offsets.push(profile.edges.count)
try single_region(&profile)
let solid = try BrepModel.held(w.tolerances)
defer solid.release()
try revolve_into(solid.value(), &profile, o, a, (carrier.a + radius) * turned_tool_share, "", sweep)
# The round's faces named for the edges' blends.
var name_a = Growing[u8]()
defer name_a.close()
var name_b = Growing[u8]()
defer name_b.close()
try blend_name(w, ea, tag, &name_a)
try blend_name(w, eb, tag, &name_b)
let b = solid.value()
for face in 0..<b.face_count():
try b.set_face_name(face, "")
if continuity == 1:
var both = Growing[u8]()
defer both.close()
try round_name(name_b.view(), name_a.view(), &both)
try b.set_face_name(0, (str)both.view())
else:
try b.set_face_name(0, (str)name_b.view())
try b.set_face_name(1, (str)name_a.view())
return try CadModel.of_brep(solid)
## The name of a full round's one face, named for both edges' blends
## (`a`, then `b`, as aliases), into `out`.
func round_name(a: const u8[], b: const u8[], out: Growing[u8]*) -> !:
for byte in a: try out.push(byte)
try out.push(124)
for byte in b: try out.push(byte)
## The side of center face `c` across edge `e`.
func other_side(w: const Shape*, c: usize, e: usize) -> usize!:
let (fa, fb) = try edge_faces(w, e)
return fb if fa == c else fa
## Whether edges `a` and `b` run alike: parallel lines, or arcs about one
## axis.
func alike(w: const Shape*, a: usize, b: usize) -> bool!:
let ca = w.carriers.items[(usize)w.edges.items[a].carrier]
let cb = w.carriers.items[(usize)w.edges.items[b].carrier]
if ca.kind == curve_line and cb.kind == curve_line: return math.abs(math.abs(ca.x.dot(cb.x)) - 1.0) <= parallel_slack
if ca.kind != curve_circle or cb.kind != curve_circle: return false
let axis_a = ca.x.cross(ca.y)
let axis_b = cb.x.cross(cb.y)
if math.abs(math.abs(axis_a.dot(axis_b)) - 1.0) > parallel_slack: return false
let offset = cb.origin.subtract(ca.origin)
return offset.subtract(axis_a.multiply_scalar(offset.dot(axis_a))).length() <= w.tolerance * 10.0
## The circle tangent to faces `c` and `sides` inside the body, in the
## section square to edge `e` at its middle (see the header): its radius
## and center.
func inscribed_circle(w: const Shape*, c: usize, sides: const usize[2], e: usize) -> (f64, Vector3)!:
let edge = w.edges.items[e]
let p = try w.edge_at(e, 0.5)
let along = try (try w.tangent(edge.carrier, (edge.t0 + edge.t1) * 0.5)).normalized()
var faces: usize[3] = [c, sides[0], sides[1]]
# The faces' tangent planes where the section meets them: at the
# middle of the center's edge with each (the center's own at p).
var feet: Vector3[3] = [p, p, p]
for k in 1..<(usize)3:
feet[k] = try section_foot(w, c, faces[k], p, along)
let u = try across(along)
let v = along.cross(u)
# Each tangent plane in the section: n . x = h (outward n); the center X
# is r in from each: nu X.u + nv X.v + r = h.
var m: f64[9]
var b: f64[3]
for k in 0..<(usize)3:
let n = try w.face_normal(faces[k], feet[k])
if math.abs(n.dot(along)) > parallel_slack_loose: error(invalid, "a full round's faces must run along its edges")
m[k * 3] = n.dot(u)
m[k * 3 + 1] = n.dot(v)
m[k * 3 + 2] = 1.0
b[k] = n.dot(feet[k].subtract(p))
let det = determinant(m)
if math.abs(det) < inscribed_singular: error(invalid, "no circle touches the center face and both sides: they must close round the rib")
var mx: f64[9]
var my: f64[9]
var mr: f64[9]
for k in 0..<(usize)9:
mx[k] = m[k]
my[k] = m[k]
mr[k] = m[k]
for k in 0..<(usize)3:
mx[k * 3] = b[k]
my[k * 3 + 1] = b[k]
mr[k * 3 + 2] = b[k]
var x = p.add(u.multiply_scalar(determinant(mx) / det)).add(v.multiply_scalar(determinant(my) / det))
var radius = determinant(mr) / det
if not (radius > 0.0): error(invalid, "the sides open away from the center face: no full round fits")
# Refined on the faces themselves (curved sides): Newton on the three
# signed distances and the section's plane, to the last digits (the
# two strips must meet exactly: a hair apart, the booleans see a
# sliver of the center face).
let scale = math.max(radius, w.tolerance)
var rows: f64[16]
var values: f64[4]
var base: f64[4]
for _ in 0..<60:
try round_gaps(w, faces[..<3], x, radius, p, along, values[..<4])
var worst = 0.0
for k in 0..<(usize)4: worst = math.max(worst, math.abs(values[k]))
if worst <= scale * inscribed_residual_share: break
let h = scale * inscribed_difference_share
let steps: Vector3[3] = [Vector3(h, 0.0, 0.0), Vector3(0.0, h, 0.0), Vector3(0.0, 0.0, h)]
for k in 0..<(usize)4: base[k] = values[k]
for col in 0..<(usize)3:
try round_gaps(w, faces[..<3], x.add(steps[col]), radius, p, along, values[..<4])
for row in 0..<(usize)4: rows[row * 4 + col] = (values[row] - base[row]) / h
for row in 0..<(usize)4:
rows[row * 4 + 3] = 1.0 if row < 3 else 0.0
values[row] = base[row]
if not solve_small(rows[..<16], values[..<4], 4): error(invalid, "no circle touches the center face and both sides: they must close round the rib")
x = x.subtract(Vector3(values[0], values[1], values[2]))
radius -= values[3]
if not (radius > 0.0): error(invalid, "the sides open away from the center face: no full round fits")
try round_gaps(w, faces[..<3], x, radius, p, along, values[..<4])
for k in 0..<(usize)3:
if math.abs(values[k]) > w.tolerance: error(invalid, "no circle touches the center face and both sides: they must close round the rib")
return (radius, x)
## The full round's equations at center `x` and radius `radius`: each
## face's outward signed distance plus the radius, and `x`'s offset from
## the section's plane (through `p`, square to `along`).
func round_gaps(w: const Shape*, faces: const usize[], x: Vector3, radius: f64, p: Vector3, along: Vector3, out: f64[]) -> !:
for k in 0..<(usize)3:
let face = w.faces.items[faces[k]]
let (gap, _) = try signed_gap(&w.surfaces.items[(usize)face.surface], x, w.tolerance)
out[k] = (gap if face.sense else -gap) + radius
out[3] = x.subtract(p).dot(along)
## Where the section through `p` square to `along` meets the edge between
## faces `c` and `side` (the side's foot).
func section_foot(w: const Shape*, c: usize, side: usize, p: Vector3, along: Vector3) -> Vector3!:
for use in w.face_uses(c):
let e = edge_index(use)
let (fa, fb) = try edge_faces(w, e)
if fa != side and fb != side: continue
let edge = w.edges.items[e]
var low = math.min(edge.t0, edge.t1)
var high = math.max(edge.t0, edge.t1)
# Bisection on the side of the section's plane.
var f_low = (try w.at(edge.carrier, low)).subtract(p).dot(along)
let f_high = (try w.at(edge.carrier, high)).subtract(p).dot(along)
if f_low * f_high > 0.0: return try w.at(edge.carrier, low if math.abs(f_low) < math.abs(f_high) else high)
for _ in 0..<80:
let middle = (low + high) * 0.5
let f = (try w.at(edge.carrier, middle)).subtract(p).dot(along)
if (f < 0.0) == (f_low < 0.0):
low = middle
f_low = f
else:
high = middle
return try w.at(edge.carrier, (low + high) * 0.5)
error(invalid, "the center face does not meet both sides")
func determinant(m: const f64[9]) -> f64:
return m[0] * (m[4] * m[8] - m[5] * m[7]) - m[1] * (m[3] * m[8] - m[5] * m[6]) + m[2] * (m[3] * m[7] - m[4] * m[6])
## The faces across the center face `c`'s longest pair of edges that run
## alike (parallel lines, concentric arcs: a rib's sides).
func round_sides(w: const Shape*, c: usize) -> usize[2]!:
var best = 0.0
var found: usize[2] = [0, 0]
var any = false
let uses = w.face_uses(c)
for i in 0..<uses.length:
let a = edge_index(uses[i])
let kind_a = w.carriers.items[(usize)w.edges.items[a].carrier].kind
if kind_a != curve_line and kind_a != curve_circle: continue
let length_a = try edge_length(w, a)
for j in i + 1..<uses.length:
let b = edge_index(uses[j])
if not try alike(w, a, b): continue
let length_b = try edge_length(w, b)
let (fa, fb) = try edge_faces(w, a)
let (ga, gb) = try edge_faces(w, b)
let side_a = fb if fa == c else fa
let side_b = gb if ga == c else ga
if side_a == side_b: continue
if length_a + length_b > best:
best = length_a + length_b
found = [side_a, side_b]
any = true
if not any: error(invalid, "the center face has no pair of parallel or concentric edges: pick its side faces")
return found
## Edge `e`'s length (a fine polyline's).
func edge_length(w: const Shape*, e: usize) -> f64!:
var total = 0.0
var previous = try w.edge_at(e, 0.0)
for k in 1..<33:
let here = try w.edge_at(e, (f64)k / 32.0)
total += here.subtract(previous).length()
previous = here
return total