dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / src / modeling / cap_blend.lucb
## Blends round a flat face's tangent loops, built directly (no booleans):
## the edges where a flat face (the cap) meets walls ruled away from it.
## Planes, cylinders and cones about the cap's normal, and surfaces swept
## straight along one way (B-spline sweeps, an earlier fillet's strips, a
## cylinder along a slanted upright), as an extrusion's sides (straight or
## tapered) or an earlier fillet's strips are. The top of a box whose
## uprights were filleted is such a loop: lines and the fillets' sections,
## tangent where they meet; so is a tapered one's.
##
## The blend is a 2D section (a circle's arc, a G2/G3 B-spline, or a
## chamfer's line) carried along the edge in the frame of the way into the
## cap and the wall's ruling (cap_rows.lucb). As OpenCASCADE and Parasolid
## do for a rolling ball along a curve, the strip over a curved edge or a
## slanted wall is approximated: each row of its section's control points
## is fitted along the edge, so its rows on the cap and the wall are those
## faces' new boundaries. Lines and arcs with walls that keep one corner
## stay exact: a line's rows are lines, an arc's arcs; square ones are a
## cylinder or plane along a line, a torus or cone about an arc.
##
## The cap's loop moves onto the contacts on it, each wall's edge onto its
## contact (the wall's ruling at the corner moved along), and each joint's
## seam (the line between two walls, along their common ruling) is
## shortened; joints get one section edge both strips share. Every vertex
## of a picked edge is such a joint, or an open end: the loop goes on
## tangentially into an edge that is not picked. There the blend runs out
## as Fusion 360's does (cap_runout.lucb): carried on past the end and
## trimmed by the faces it runs into; where the face going on does not turn
## away from it, the strip stops in the section's plane, a flat end face
## fills the corner between the section and the sharp edge going on, and
## the seam is split at the wall's contact. Or the loop turns a sharp
## outside corner there into a flat end wall square to the edge (a plate's
## side): the strip ends flush in that wall, which loses the corner beyond
## the section (Fusion's result, its edge on the cap cut at the contact).
## Anything else is declined (blend.lucb's booleans).
## A picked edge of a cap loop: its cap and wall faces, the side of its
## cap normal its cap lies on (`sign`: the way into the cap is `sign`
## (n x the edge's carrier tangent)), the cap's normal toward the wall
## (`into_wall`), whether the edge is convex, how its wall runs away from
## it (`ruling`: cap_rows.lucb; `s0`, `s1` a plane's at its ends, `sweep`
## a sweep's way), and what it becomes: its contact edges on the cap
## (`outer`) and the wall (`inner`), and its strip's surface and sense.
struct CapSide:
var edge: usize
var cap: usize
var wall: usize
var sign: f64
var into_wall: Vector3
var convex: bool
var ruling: i64
var s0: Vector3
var s1: Vector3
var sweep: Vector3
var outer: i64
var inner: i64
var surface: i64
var sense: bool
## The builder: the body `w`, the result `out` (begun as a copy of the
## body, so its points, edges and surfaces keep their numbers), the blend
## (`reach`: a fillet's radius, a chamfer's size; `weight`: a G1 section's
## middle weight, the chain's corner's; a chamfer's `chamfer` sides, and
## whether the cap takes their first distance: `cap_first`), and for each body edge its picked
## side (-1), whether it is a seam, and its new seam (-1, a split seam's
## upper part in `uppers`), for
## each body vertex its contacts on the cap and wall, its section (-1),
## whether it is an open end (the edge going on there: `continues`), its
## run-out (`runout_of`, into `runouts`; -1: a flat end) and a flat end's
## line from its cap contact to it; whether it is a corner end (`cornered`:
## the loop turns into an end wall there), and each edge going on from a
## corner end cut at the cap contact (`corner_cuts`, by body edge).
struct Cap:
var w: const Shape*
var out: Shape
var reach: f64
var law: const RadiusLaw*?
## The law as measured along the balls' path (spine_law), when one is
## given: `law` then points here (else it stays empty).
var spine: RadiusLaw
var round: bool
var continuity: i64
var weight: f64
var chamfer: const ChamferSides*?
var cap_first: bool
## Fusion's Tangency weight of a G2 or G3 section (fillet_section).
var blend_shape: BlendShape
## A G1 variable fillet at weight 1 round a closed loop: the ball rolled
## (cap_rolled.lucb), its sections degree 5.
var rolled: bool
var balls: SampledBalls
var sides: Growing[CapSide]
var side_of: Growing[i64]
var is_seam: Growing[bool]
var seams: Growing[i64]
var uppers: Growing[i64]
var on_cap: Growing[i64]
var on_wall: Growing[i64]
var sections: Growing[i64]
var open: Growing[bool]
var continues: Growing[i64]
var end_lines: Growing[i64]
var runouts: Growing[Runout]
var runout_of: Growing[i64]
var cornered: Growing[bool]
var corner_cuts: Growing[i64]
var name: Growing[u8]
func close():
self.out.close()
self.spine.close()
self.sides.close()
self.side_of.close()
self.is_seam.close()
self.seams.close()
self.uppers.close()
self.on_cap.close()
self.on_wall.close()
self.sections.close()
self.open.close()
self.continues.close()
self.end_lines.close()
self.runouts.close()
self.runout_of.close()
self.cornered.close()
self.corner_cuts.close()
self.name.close()
self.balls.close()
func at(id: i64) -> Vector3:
return self.out.points.items[(usize)id]
## A straight out edge from point `a` to `b`.
func line(a: i64, b: i64) -> i64!:
let pa = self.at(a)
let span = self.at(b).subtract(pa)
let length = span.length()
if length <= self.w.tolerance: error(invalid, "a blend would make an edge of no length")
let c = try self.out.add_carrier(Carrier(0, pa, span.multiply_scalar(1.0 / length), Vector3(), 0.0, 0.0, -1))
return try self.out.add_edge(ShapeEdge(c, 0.0, length, a, b))
## A B-spline out edge over its whole domain, from point `a` to `b`:
## from its first control point to its last (`forward`), or back.
func spline(a: i64, b: i64, net: const Vector3[], weights: const f64[], knots: const f64[], degree: i64, forward: bool) -> i64!:
let index = try self.out.store_spline(net, weights, knots, degree, Matrix4())
let c = try self.out.add_carrier(Carrier(2, Vector3(), Vector3(), Vector3(), 0.0, 0.0, index))
let low = knots[(usize)degree]
let high = knots[net.length]
return try self.out.add_edge(ShapeEdge(c, low if forward else high, high if forward else low, a, b))
## Edges `chosen` of `w` blended round their caps (see the file's header):
## filleted to `size` (`continuity` 1 to 3) when `round`, its radius
## changing along the chain by `law` when given (`size` then its largest),
## else chamfered (by `sides` when given: chamfers.lucb); none when they are
## not all such loops (the booleans then try).
pub func cap_blend(w: const Shape*, chosen: const usize[], size: f64, round: bool, continuity: i64, tag: str, sides: const ChamferSides*? = none, law: const RadiusLaw*? = none, blend_shape: BlendShape = plain_blend) -> interop.Reference[CadModel]?!:
var made = try new_cap(w, size, round, continuity)
defer made.close()
let b = &made
b.blend_shape = blend_shape
b.law = law
if not try cap_eligible(b, chosen): return none
try chamfer_first(b, sides)
try spine_law(b)
try chain_weight(b)
b.rolled = rolled_cap(b)
if b.rolled: try cap_balls(b)
try cap_room(b)
try cap_contacts(b)
for k in 0..<b.sides.count: try cap_strip(b, k)
try cap_sections(b)
return try cap_result(b, tag)
func new_cap(w: const Shape*, size: f64, round: bool, continuity: i64) -> Cap!:
var out = empty_shape(w.tolerance)
errdefer out.close()
for p in w.points.view(): try out.points.push(p)
for c in w.carriers.view(): try out.carriers.push(c)
for edge in w.edges.view(): try out.edges.push(edge)
for sp in w.splines.view(): try out.splines.push(sp)
for p in w.spline_points.view(): try out.spline_points.push(p)
for x in w.spline_weights.view(): try out.spline_weights.push(x)
for k in w.spline_knots.view(): try out.spline_knots.push(k)
for s in w.surfaces.view():
try out.surfaces.push(try Surface(s.kind, s.origin, s.x, s.z, s.radius, s.angle, s.points, s.weights, s.ku, s.kv, s.nu, s.nv, s.du, s.dv))
var made = Cap(w, out, size, none, empty_law(), round, continuity, math.sqrt(0.5), none, true, plain_blend, false, SampledBalls(Growing[Vector3](), Growing[f64](), 1.0), Growing[CapSide](), Growing[i64](), Growing[bool](), Growing[i64](), Growing[i64](), Growing[i64](), Growing[i64](),
Growing[i64](), Growing[bool](), Growing[i64](), Growing[i64](), Growing[Runout](), Growing[i64](), Growing[bool](), Growing[i64](), Growing[u8]())
errdefer made.close()
try made.side_of.fill(w.edges.count, -1)
try made.is_seam.fill(w.edges.count, false)
try made.seams.fill(w.edges.count, -1)
try made.uppers.fill(w.edges.count, -1)
try made.on_cap.fill(w.points.count, -1)
try made.on_wall.fill(w.points.count, -1)
try made.sections.fill(w.points.count, -1)
try made.open.fill(w.points.count, false)
try made.continues.fill(w.points.count, -1)
try made.end_lines.fill(w.points.count, -1)
try made.runout_of.fill(w.points.count, -1)
try made.cornered.fill(w.points.count, false)
try made.corner_cuts.fill(w.edges.count, -1)
return made
## Fusion measures a variable radius along a chain by its balls' path (the
## sections' centers), not the edge: a chain's strips run on that path's
## length (probe 8 of 2026-10-12: a plate's line, arc and line ran 10.0416,
## 11.024 and 10.0416 long, the law 2 to 4 straight in it), so round a
## convex arc, where the path is shorter, the radius changes more slowly
## than along the edge. The law is tabled again so: each chain edge's table
## the length of its sections' centers under the law as it stands, a few
## times over, the centers moving with the radius. On a straight edge the
## path is straight and the law unchanged. A closed loop's law stays on its
## edge: Fusion's loop laws were read back as radii along the edge
## (fusion_roll_tests), which says nothing of its own measure there.
func spine_law(b: Cap*) -> !:
let given = b.law else return
if not b.round or given.closed: return
b.spine = try given.copied()
b.law = &b.spine
var row = Growing[f64]()
defer row.close()
var rows = Growing[f64]()
defer rows.close()
for pass in 0..<6:
rows.clear()
for side in b.sides.view():
let edge = b.w.edges.items[side.edge]
var previous = Vector3()
var run = 0.0
for k in 0..<law_samples + 1:
let t = edge.t0 + (edge.t1 - edge.t0) * (f64)k / (f64)law_samples
let (u, _) = try into_cap(b, &side, t)
let (_, c) = try corner_at(b, &side, t)
let center = (try b.w.at(edge.carrier, t)).add(u.multiply_scalar(c)).add(side.into_wall.multiply_scalar(try reach_at(b, &side, t)))
if k > 0: run += center.subtract(previous).length()
previous = center
try rows.push(run)
for k in 0..<b.sides.count:
try b.spine.retable(b.sides.items[k].edge, rows.view()[k * (law_samples + 1)..<(k + 1) * (law_samples + 1)])
## A chamfer's `sides` on the builder, and which face of the loops takes
## their first distance: the same for every side (a section is shared at
## each joint), or an error.
func chamfer_first(b: Cap*, sides: const ChamferSides*?) -> !:
b.chamfer = sides
if b.round or sides == none: return
for k in 0..<b.sides.count:
let side = b.sides.items[k]
let on_cap = first_face(sides, side.edge, side.cap)
if k == 0: b.cap_first = on_cap
elif on_cap != b.cap_first: error(invalid, "this chamfer's first distance would change faces along its loop: flip it, or chamfer the loop's edges apart")
## Whether the loop's fillet rolls a ball (Fusion's G1 at weight 1 along a
## law that changes): a closed loop (no open end).
func rolled_cap(b: const Cap*) -> bool:
let law = b.law else return false
if not b.round or b.continuity != 1 or not b.blend_shape.plain(): return false
var changes = false
for r in law.radii.view():
if r != law.radii.items[0]: changes = true
if not changes: return false
for open in b.open.view():
if open: return false
return true
## The G1 section's middle weight, sin(θ/2) for the chain's corner of
## cosine λ (√½ square): the corner at the first side's start (a slanted
## sweep's corner changes along it; its sections are then near-circles).
func chain_weight(b: Cap*) -> !:
let side = b.sides.items[0]
let (lambda, _) = try corner_at(b, &side, b.w.edges.items[side.edge].t0)
b.weight = math.sqrt((1.0 - lambda) * 0.5)
## The way into side `side`'s cap at its carrier's parameter `t`, and how
## it turns (its derivative by `t`).
func into_cap(b: const Cap*, side: const CapSide*, t: f64) -> (Vector3, Vector3)!:
let w = b.w
let n = w.surfaces.items[(usize)w.faces.items[side.cap].surface].z
let (_, d1, d2) = try w.derivatives(w.edges.items[side.edge].carrier, t)
let crosswise = n.cross(d1)
let turning = n.cross(d2)
let length = crosswise.length()
if length <= 1e-300: error(invalid, "a blend's edge stops along it")
let u = crosswise.multiply_scalar(side.sign / length)
let du = turning.multiply_scalar(1.0 / length).subtract(crosswise.multiply_scalar(crosswise.dot(turning) / (length * length * length)))
return (u, du.multiply_scalar(side.sign))
## Each vertex's contacts on the cap (its contact distance into the cap)
## and the wall (that far along the ruling), the open ends' run-outs, each
## seam shortened to its wall contact (a joint's, a run-out's), or split
## there (a flat end's: its upper part meets the end face).
func cap_contacts(b: Cap*) -> !:
let w = b.w
for side in b.sides.view():
let edge = w.edges.items[side.edge]
for end in 0..<(usize)2:
let v = edge.start if end == 0 else edge.end
if b.on_cap.items[(usize)v] >= 0: continue
let t = edge.t0 if end == 0 else edge.t1
let (u, _) = try into_cap(b, &side, t)
let (lambda, c) = try corner_at(b, &side, t)
let s = try ruling_at(b, &side, t)
let p = w.points.items[(usize)v]
b.on_cap.items[(usize)v] = try b.out.add_point(p.add(u.multiply_scalar(c)))
b.on_wall.items[(usize)v] = try b.out.add_point(p.add(s.multiply_scalar(try wall_distance(b, lambda, try reach_at(b, &side, t)))))
try cap_runouts(b)
try corner_cuts(b)
for v in 0..<w.points.count:
if flat_end(b, v): b.end_lines.items[v] = try b.line(b.on_cap.items[v], (i64)v)
for g in 0..<w.edges.count:
let seam = w.edges.items[g]
if not b.is_seam.items[g]: continue
let low = b.on_wall.items[(usize)seam.start]
let high = b.on_wall.items[(usize)seam.end]
if low < 0 and high < 0: continue
let a = low if low >= 0 else seam.start
let z = high if high >= 0 else seam.end
b.seams.items[g] = try b.line(a, z)
# At an open end the part above the contact stays, the way the seam
# runs.
if low >= 0 and flat_end(b, (usize)seam.start): b.uppers.items[g] = try b.line(seam.start, low)
if high >= 0 and flat_end(b, (usize)seam.end): b.uppers.items[g] = try b.line(high, seam.end)
## Whether vertex `v` is an open end that stops flat (no run-out).
func flat_end(b: const Cap*, v: usize) -> bool:
return b.open.items[v] and b.runout_of.items[v] < 0 and not b.cornered.items[v]
## Each edge going on from a corner end, on the cap and its end wall, cut
## at the end's cap contact (which lies on it: the wall is square to the
## blended edge), from both ends when both are corner ends.
func corner_cuts(b: Cap*) -> !:
let w = b.w
for v in 0..<w.points.count:
if not b.cornered.items[v]: continue
let g = (usize)b.continues.items[v]
if b.corner_cuts.items[g] >= 0: continue
let edge = w.edges.items[g]
# Its corner ends' contacts must leave it some length.
let both = b.cornered.items[(usize)edge.start] and b.cornered.items[(usize)edge.end] and b.continues.items[(usize)edge.start] == (i64)g and b.continues.items[(usize)edge.end] == (i64)g
let length = w.points.items[(usize)edge.end].subtract(w.points.items[(usize)edge.start]).length()
let reach = b.at(b.on_cap.items[v]).subtract(w.points.items[v]).length()
if reach * (2.0 if both else 1.0) >= length - w.tolerance * 10.0: try too_large(b.round, b.reach, b.reach * length / (reach * (2.0 if both else 1.0)), "the edge its chain ends against")
var t0 = edge.t0
var t1 = edge.t1
var start = edge.start
var end = edge.end
for k in 0..<(usize)2:
let at = edge.start if k == 0 else edge.end
if not b.cornered.items[(usize)at] or b.continues.items[(usize)at] != (i64)g: continue
let touch = b.on_cap.items[(usize)at]
let t = try nearest_on(w, edge.carrier, b.at(touch), edge.t0, edge.t1)
if (try w.at(edge.carrier, t)).subtract(b.at(touch)).length() > w.tolerance * 10.0: error(invalid, "a blend's contact is off the edge its chain ends against")
if k == 0:
t0 = t
start = touch
else:
t1 = t
end = touch
b.corner_cuts.items[g] = try b.out.add_edge(ShapeEdge(edge.carrier, t0, t1, start, end))
## The largest blend the loops take, an error when the blend is not
## smaller: each contact on the cap must stay short of where the edge's
## offset folds (its radius of curvature toward the cap), and each seam
## between walls must keep some length (its whole length, or half when
## blends shorten it at both ends).
func cap_room(b: const Cap*) -> !:
let w = b.w
var most = 1.0e300
var what = "this curved edge"
# The contacts are the radius times the corner's factor (1 when square).
var factor = 1.0
for side in b.sides.view():
let edge = w.edges.items[side.edge]
for k in 0..<(usize)5:
let t = edge.t0 + (edge.t1 - edge.t0) * (f64)k / 4.0
let (lambda, c) = try corner_at(b, &side, t)
let r = try reach_at(b, &side, t)
factor = math.max(factor, math.max(c, try wall_distance(b, lambda, r)) / r)
most = math.min(most, try offset_room(b, &side))
var ends = Growing[i64]()
defer ends.close()
try ends.fill(w.points.count, 0)
for side in b.sides.view():
let edge = w.edges.items[side.edge]
ends.items[(usize)edge.start] = 1
ends.items[(usize)edge.end] = 1
for g in 0..<w.edges.count:
if not b.is_seam.items[g]: continue
let seam = w.edges.items[g]
let shortened = ends.items[(usize)seam.start] + ends.items[(usize)seam.end]
if shortened == 0: continue
let height = w.points.items[(usize)seam.end].subtract(w.points.items[(usize)seam.start]).length() / (f64)shortened
if height < most:
most = height
what = "the height of its walls"
if b.reach * factor >= most: try too_large(b.round, b.reach, most / factor, what)
## An error: a blend of `size` is too large for `what`; it must be less
## than `most`.
func too_large(round: bool, size: f64, most: f64, what: str) -> !:
var buffer: u8[256]
let text = try strings.format(buffer, f"a fillet radius of {rounded_text(size)} is too large for {what}: it must be less than {rounded_text(most)}") if round else try strings.format(buffer, f"a chamfer of {rounded_text(size)} is too large for {what}: it must be less than {rounded_text(most)}")
let owned = try interop.copy_text(text)
if let owner = owned.owner: _ = ownership.pool_object(owner)
error(invalid, owned.value)
## How far side `side`'s cap contact may go in before its offset of the
## edge folds back (where the edge curves toward the cap: its radius of
## curvature there); very large when it never does.
func offset_room(b: const Cap*, side: const CapSide*) -> f64!:
let edge = b.w.edges.items[side.edge]
var least = 1.0e300
for k in 0..<(usize)65:
let t = edge.t0 + (edge.t1 - edge.t0) * (f64)k / 64.0
let (_, d1, _) = try b.w.derivatives(edge.carrier, t)
let (_, du) = try into_cap(b, side, t)
let turning = du.dot(d1)
if turning < 0.0: least = math.min(least, d1.dot(d1) / -turning)
return least
## Whether side `side` keeps one corner all along with its wall's ruling
## square to the edge (no skew): a line between planes whose seams are
## parallel and square to it, an arc on a cylinder or cone about the cap's
## normal. Its strip is then a cylinder, plane, torus or cone.
func regular_side(b: const Cap*, side: const CapSide*) -> bool!:
if b.law != none: return false
let w = b.w
let edge = w.edges.items[side.edge]
let kind = w.carriers.items[(usize)edge.carrier].kind
if kind == curve_circle: return side.ruling == ruling_cone
if kind != curve_line or side.ruling != ruling_between: return false
let along = w.carriers.items[(usize)edge.carrier].x
return side.s0.subtract(side.s1).length() <= 1e-12 * side.s0.length() and math.abs(side.s0.dot(along)) <= 1e-12 * side.s0.length()
## Side `k`'s contact edges and strip surface.
func cap_strip(b: Cap*, k: usize) -> !:
let side = b.sides.items[k]
let w = b.w
let edge = w.edges.items[side.edge]
let carrier = w.carriers.items[(usize)edge.carrier]
let cap_start = b.on_cap.items[(usize)edge.start]
let cap_end = b.on_cap.items[(usize)edge.end]
let wall_start = b.on_wall.items[(usize)edge.start]
let wall_end = b.on_wall.items[(usize)edge.end]
let corner = w.points.items[(usize)edge.start]
let (u0, _) = try into_cap(b, &side, edge.t0)
var empty_points: Vector3[]
var empty_numbers: f64[]
# A corner of cosine λ: the cap's contact c from it, the wall's its leg
# along the ruling (μ of it along the normal); a fillet's center r off
# the cap.
let (lambda, c) = try corner_at(b, &side, edge.t0)
let mu = math.sqrt(1.0 - lambda * lambda)
let regular = try regular_side(b, &side)
if carrier.kind == curve_line and regular:
b.sides.items[k].outer = try b.line(cap_start, cap_end)
b.sides.items[k].inner = try b.line(wall_start, wall_end)
let along = carrier.x.multiply_scalar(1.0 if edge.t1 > edge.t0 else -1.0)
if b.round and b.continuity == 1 and b.blend_shape.plain():
let axis = corner.add(u0.multiply_scalar(c)).add(side.into_wall.multiply_scalar(b.reach))
try b.out.surfaces.push(try Surface(1, axis, try b.at(cap_start).subtract(axis).normalized(), along, b.reach, 0.0, empty_points, empty_numbers, empty_numbers, empty_numbers, 0, 0, 0, 0))
return try finish_strip(b, k)
if not b.round:
let normal = try along.cross(b.at(wall_start).subtract(b.at(cap_start))).normalized()
try b.out.surfaces.push(try Surface(0, b.at(cap_start), along, normal, 0.0, 0.0, empty_points, empty_numbers, empty_numbers, empty_numbers, 0, 0, 0, 0))
return try finish_strip(b, k)
elif carrier.kind == curve_circle and regular:
# Concentric arcs: the cap's contact a circle of the radius moved c,
# the wall's moved c λ and lowered c μ.
let radial = w.points.items[(usize)edge.start].subtract(carrier.origin)
let outward = 1.0 if u0.dot(radial) > 0.0 else -1.0
let radius = carrier.a + c * outward
let c_cap = try b.out.add_carrier(Carrier(1, carrier.origin, carrier.x, carrier.y, radius, radius, -1))
b.sides.items[k].outer = try b.out.add_edge(ShapeEdge(c_cap, edge.t0, edge.t1, cap_start, cap_end))
let down = try wall_distance(b, lambda, b.reach)
let wall_radius = carrier.a + down * lambda * outward
let lowered = carrier.origin.add(side.into_wall.multiply_scalar(down * mu))
let c_wall = try b.out.add_carrier(Carrier(1, lowered, carrier.x, carrier.y, wall_radius, wall_radius, -1))
b.sides.items[k].inner = try b.out.add_edge(ShapeEdge(c_wall, edge.t0, edge.t1, wall_start, wall_end))
if b.round and b.continuity == 1 and b.blend_shape.plain():
# A torus about the arc's axis, its tube's center the ball's path.
let center = carrier.origin.add(side.into_wall.multiply_scalar(b.reach))
try b.out.surfaces.push(try Surface(5, center, carrier.x, carrier.x.cross(carrier.y), radius, b.reach, empty_points, empty_numbers, empty_numbers, empty_numbers, 0, 0, 0, 0))
return try finish_strip(b, k)
if not b.round:
# A cone through both contact circles, opening its own way.
let opening = wall_radius - radius
let axis = side.into_wall if opening > 0.0 else side.into_wall.multiply_scalar(-1.0)
let origin = carrier.origin if opening > 0.0 else lowered
let start_radius = radius if opening > 0.0 else wall_radius
try b.out.surfaces.push(try Surface(2, origin, carrier.x, axis, start_radius, math.atan2(math.abs(opening), down * mu), empty_points, empty_numbers, empty_numbers, empty_numbers, 0, 0, 0, 0))
return try finish_strip(b, k)
# The section swept along the edge: rows of control points, each exact
# (a line's, an arc's) or fitted, sharing their knots.
var a = Growing[f64]()
defer a.close()
var z = Growing[f64]()
defer z.close()
var ku = Growing[f64]()
defer ku.close()
var wu = Growing[f64]()
defer wu.close()
let du = try section_at(b, lambda, b.reach, &a, &z, &ku, &wu)
var net = Growing[Vector3]()
defer net.close()
var kv = Growing[f64]()
defer kv.close()
var wv = Growing[f64]()
defer wv.close()
let dv = try along_rows(b, &side, a.count, &net, &kv, &wv)
let nv = wv.count
let nu = a.count
if b.sides.items[k].outer < 0:
if carrier.kind == curve_line and side.ruling == ruling_between and b.law == none:
# Exact lines between the contacts.
b.sides.items[k].outer = try b.line(cap_start, cap_end)
b.sides.items[k].inner = try b.line(wall_start, wall_end)
else:
# The first and last rows: an arc's run from its start over
# [0, 1], a fitted one on the carrier's parameter.
let forward = edge.t1 > edge.t0 or (carrier.kind == curve_circle and side.ruling == ruling_cone)
b.sides.items[k].outer = try b.spline(cap_start, cap_end, net.view()[..<nv], wv.view(), kv.view(), dv, forward)
b.sides.items[k].inner = try b.spline(wall_start, wall_end, net.view()[(nu - 1) * nv..<nu * nv], wv.view(), kv.view(), dv, forward)
var weights = Growing[f64]()
defer weights.close()
for i in 0..<nu:
for j in 0..<nv: try weights.push(wu.items[i] * wv.items[j])
try b.out.surfaces.push(try Surface(3, Vector3(), Vector3(), Vector3(), 0.0, 0.0, net.view(), weights.view(), ku.view(), kv.view(), (i64)nu, (i64)nv, du, dv))
try finish_strip(b, k)
## Side `k`'s strip is the surface just made: its sense, outward (away
## from the material) at the section's middle halfway along.
func finish_strip(b: Cap*, k: usize) -> !:
let side = b.sides.items[k]
let edge = b.w.edges.items[side.edge]
let surface = (i64)b.out.surfaces.count - 1
b.sides.items[k].surface = surface
let t = (edge.t0 + edge.t1) * 0.5
let (u, _) = try into_cap(b, &side, t)
let s = try ruling_at(b, &side, t)
let tangent = try (try b.w.tangent(edge.carrier, t)).normalized()
let spread = try s.subtract(tangent.multiply_scalar(s.dot(tangent))).normalized()
let bisector = try u.add(spread).normalized()
let outward = bisector.multiply_scalar(-1.0 if side.convex else 1.0)
let corner = try b.w.at(edge.carrier, t)
# A point of the strip there: the circle's middle (G1), the chamfer's,
# or near the B-spline's (its normal by parameters, so near is enough).
let surf = &b.out.surfaces.items[(usize)surface]
var normal = Vector3()
if surf.kind == surface_spline:
let u_mid = (surf.ku[(usize)surf.du] + surf.ku[(usize)surf.nu]) * 0.5
let v_low = surf.kv[(usize)surf.dv]
let v_high = surf.kv[(usize)surf.nv]
# Halfway along the rows is halfway along the edge (a B-spline's
# rows run on its parameter, a line's and an arc's over [0, 1]).
normal = try surface_normal_checked(surf.points, surf.weights, surf.nu, surf.nv, surf.du, surf.dv, surf.ku, surf.kv, u_mid, (v_low + v_high) * 0.5)
else:
# The arc's middle, on the bisector from its center to the corner;
# the chamfer's, between its contacts.
let (lambda, c) = try corner_at(b, &side, t)
let center = corner.add(u.multiply_scalar(c)).add(side.into_wall.multiply_scalar(b.reach))
let middle = center.add((try corner.subtract(center).normalized()).multiply_scalar(b.reach)) if b.round else corner.add(u.multiply_scalar(c * 0.5)).add(s.multiply_scalar(try wall_distance(b, lambda, b.reach) * 0.5))
normal = try normal_at(surf, middle, b.w.tolerance * 100.0)
b.sides.items[k].sense = normal.dot(outward) > 0.0
## Each vertex's section, from its cap contact to its wall contact, shared
## by the strips meeting there (or ending: an open end's): a chamfer's
## line, a circle's arc (G1: an exact circle at a square corner, else the
## rational arc), or the G2/G3 B-spline.
func cap_sections(b: Cap*) -> !:
let w = b.w
var a = Growing[f64]()
defer a.close()
var z = Growing[f64]()
defer z.close()
var ku = Growing[f64]()
defer ku.close()
var wu = Growing[f64]()
defer wu.close()
var net = Growing[Vector3]()
defer net.close()
for side in b.sides.view():
let edge = w.edges.items[side.edge]
for end in 0..<(usize)2:
let v = (usize)(edge.start if end == 0 else edge.end)
if b.sections.items[v] >= 0: continue
let t = edge.t0 if end == 0 else edge.t1
let (u, _) = try into_cap(b, &side, t)
let s = try ruling_at(b, &side, t)
let (lambda, _) = try corner_at(b, &side, t)
let corner = w.points.items[v]
let head = b.on_cap.items[v]
let tail = b.on_wall.items[v]
if not b.round:
b.sections.items[v] = try b.line(head, tail)
continue
let tangent = try (try w.tangent(edge.carrier, t)).normalized()
let r = try reach_at(b, &side, t)
if b.rolled:
# The rolled section there (cap_rolled.lucb), degree 5.
try cap_rolled_section(b, &side, t, &net)
let degree = try section_at(b, 0.0, r, &a, &z, &ku, &wu)
b.sections.items[v] = try b.spline(head, tail, net.view(), wu.view(), ku.view(), degree, true)
continue
if b.continuity == 1 and b.blend_shape.plain() and math.abs(s.dot(tangent)) <= 1e-12 * s.length():
# A circle's arc square to the edge, from the cap's contact
# round to the wall's: π - θ (under a Tangency weight the
# conic's spline, below).
let (_, c) = try corner_at(b, &side, t)
let center = corner.add(u.multiply_scalar(c)).add(side.into_wall.multiply_scalar(r))
let x = try b.at(head).subtract(center).normalized()
let to_tail = b.at(tail).subtract(center)
let y = try to_tail.subtract(x.multiply_scalar(to_tail.dot(x))).normalized()
let carrier = try b.out.add_carrier(Carrier(1, center, x, y, r, r, -1))
b.sections.items[v] = try b.out.add_edge(ShapeEdge(carrier, 0.0, math.pi - math.acos(lambda), head, tail))
continue
let degree = try section_at(b, lambda, r, &a, &z, &ku, &wu)
net.clear()
for i in 0..<a.count: try net.push(corner.add(u.multiply_scalar(a.items[i])).add(s.multiply_scalar(z.items[i])))
b.sections.items[v] = try b.spline(head, tail, net.view(), wu.view(), ku.view(), degree, true)