Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.4

0.9.4 / src / modeling / variable_fillet.lucb

## Fusion's Variable radius fillet: radii at points along a tangent chain
## of edges (positions 0 to 1 along it), Fusion's law between them or held
## at each (radius_law.lucb), G1, G2 or G3, under Fusion's Tangency weight
## (fillet_section, arc_section) as a constant fillet's.
##
## The chain is built as a constant fillet's would be, the radius asked of
## the law at each section: loops round a flat face and their open ends'
## run-outs (cap_blend.lucb: a box's top past rounded uprights, an earlier
## fillet's edges, a hole's rim, a chain ending where an unpicked tangent
## edge goes on), loops between two curved faces (curved_blend.lucb). Their
## rows are fitted along the chain, within the tolerance.
##
## One straight edge between two flat faces is exact: the rolling ball's
## section swept with its radius changing. In each section square to the
## edge every control point is the edge's point plus r(s) times a fixed
## vector (G1: the rational quadratic arc from contact to contact whose
## middle control point is the edge's own point, weighted sin(θ/2) for
## faces θ apart, times the Tangency weight (a conic), the contacts
## r cot(θ/2) along each face; G2 and G3: the fillet's B-spline on those
## contacts), so with r(s) a piecewise cubic the
## face is exactly a B-spline, cubic along (a knot thrice at each point:
## each piece a Bezier). The tool is the region between the edge and that
## face, taken away (an inside edge: added) by the booleans. Its net keeps
## to one side of each face's plane, touching it only along the contact
## row, so it meets the body only along curves both carry (kept_apart). At
## an end square to the edge it ends flush. At another flat end face,
## leaning either way (a slanted end, a wall a convex edge runs into, an
## inside edge's end), each row of the net stops where it meets the end
## face's plane: its first or last piece is the same polynomial from there,
## so the rows keep their knots, the end's section lies in that plane, and
## no boolean has to cross the strip there (an exact cylinder when the
## radius holds).

## `model` with the edges `reference` names (with their tangent chains when
## `chain`) filleted with `radii` at `positions` along the chain (0 its
## start, 1 its end; the ends are added from the nearest radius when
## missing), Fusion's law between them or `smooth`, of `continuity`, the blend
## named for feature `tag`, under Tangency weight `blend_shape`.
func variable_filleted_edge(model: const CadModel*, reference: str, positions: const f64[], radii: const f64[], smooth: bool, tag: str, continuity: i64 = 1, chain: bool = true, blend_shape: BlendShape = plain_blend) -> interop.Reference[CadModel]!:
    if continuity < 1 or continuity > 3: error(invalid, "a fillet's continuity is G1, G2 or G3")
    try check_shape(blend_shape)
    var w = try shape_of(model)
    defer w.close()
    var chosen = Growing[usize]()
    defer chosen.close()
    try resolve_edges(&w, reference, &chosen)
    if chosen.count == 0: error(invalid, "a variable radius fillet takes an edge")
    for e in chosen.view():
        if not try blendable(&w, e): error(invalid, "only straight edges between flat faces, rims, edges round a flat face whose walls are ruled away from it, and loops between two curved faces can be blended yet")
    let first = chosen.items[0]
    if chain: try tangent_chains(&w, &chosen)
    var law = try chain_law(&w, chosen.view(), first, positions, radii, smooth)
    defer law.close()
    let largest = law.largest()
    for e in chosen.view(): try check_fits(&w, e, largest, true)
    if let made = try cap_blend(&w, chosen.view(), largest, true, continuity, tag, none, &law, blend_shape): return made
    if let made = try curved_blend(&w, chosen.view(), largest, true, continuity, tag, none, &law, blend_shape): return made
    let e = chosen.items[0]
    let (fa, fb) = try edge_faces(&w, e)
    if chosen.count != 1 or w.carriers.items[(usize)w.edges.items[e].carrier].kind != curve_line or not flat_pair(&w, fa, fb):
        error(invalid, "a variable radius fillet takes one straight edge between flat faces, a tangent chain round a flat face, or a loop between two curved faces")
    return try variable_straight(model, &w, e, &law, continuity, tag, blend_shape)

## One straight edge `e` between flat faces filleted by `law`, of
## `continuity` (see the header).
func variable_straight(model: const CadModel*, w: const Shape*, e: usize, law: const RadiusLaw*, continuity: i64, tag: str, blend_shape: BlendShape) -> interop.Reference[CadModel]!:
    let edge = w.edges.items[e]
    let (fa, fb) = try edge_faces(w, e)
    let start = try w.edge_at(e, 0.0)
    let end = try w.edge_at(e, 1.0)
    let length = end.subtract(start).length()
    let along = try end.subtract(start).normalized()
    let ua = try into_face(w, fa, e, along)
    let ub = try into_face(w, fb, e, along)
    let theta = math.acos(math.clamp(ua.dot(ub), -1.0, 1.0))
    let reach = 1.0 / math.tan(theta * 0.5)
    let weight = math.sin(theta * 0.5)
    let convex_edge = try convex(w, e)
    # How each end goes: flush at a face square to the edge; at another
    # flat face, leaning either way, each row of the net stops in its plane
    # (no boolean then crosses the strip there: a G2 strip's crossing with
    # a slanted face, marched, could not be split cleanly).
    var ends = EndPlanes(along.multiply_scalar(-1.0), 0.0, false, along, 0.0, false)
    for k in 0..<(usize)2:
        let v = edge.start if k == 0 else edge.end
        let point = start if k == 0 else end
        if ends_square(w, v, fa, fb, along): continue
        let (n, h) = end_plane(w, point, fa, fb) catch failure:
            _ = failure
            error(invalid, "a variable radius fillet's edge must end at a flat face")
        if k == 0:
            ends.n0 = n
            ends.h0 = h
            ends.slanted0 = true
        else:
            ends.n1 = n
            ends.h1 = h
            ends.slanted1 = true
    var name = Growing[u8]()
    defer name.close()
    try blend_name(w, e, tag, &name)
    let tool = try straight_law_tool(w, e, law, continuity, blend_shape, (str)name.view(), start, end, &ends)
    defer tool.release()
    return try combine(model, tool.value(), op_subtract if convex_edge else op_union)

## The tool of straight edge `e` between flat faces filleted by `law` from
## `start` to `end` (the edge's ends, or where corners stop it: the law then
## spans that stretch), of `continuity` under `blend_shape`, its blend face
## named `name`, ending as `ends` says (see the header). At G1 (or a
## setback corner's G1, `g1_spline`) under the weight of 1 it is the rolled
## balls' envelope, as Fusion's: at a corner its law is held flat, its end
## section the corner's (rolled_ends gives the corner patch that section).
func straight_law_tool(w: const Shape*, e: usize, law: const RadiusLaw*, continuity: i64, blend_shape: BlendShape, name: str, start: Vector3, end: Vector3, ends: const EndPlanes*) -> interop.Reference[CadModel]!:
    let (fa, fb) = try edge_faces(w, e)
    let length = end.subtract(start).length()
    let along = try end.subtract(start).normalized()
    let ua = try into_face(w, fa, e, along)
    let ub = try into_face(w, fb, e, along)
    let theta = math.acos(math.clamp(ua.dot(ub), -1.0, 1.0))
    let reach = 1.0 / math.tan(theta * 0.5)
    let weight = math.sin(theta * 0.5)
    let convex_edge = try convex(w, e)
    var kept_at = Growing[f64]()
    defer kept_at.close()
    var kept_sizes = Growing[f64]()
    defer kept_sizes.close()
    var kept_slopes = Growing[f64]()
    defer kept_slopes.close()
    try keep_law(law, &kept_at, &kept_sizes, &kept_slopes)
    let straight = straight_law(kept_at.view(), kept_sizes.view(), kept_slopes.view())
    # Fusion rolls a ball at G1 under the weight of 1 (exactly; 0.999 and
    # 1.001 scale the section): one straight piece of the law makes a cone.
    var cone = -1.0
    var shape = blend_shape
    if (continuity == 1 or continuity == g1_spline) and blend_shape.plain() and straight:
        cone = rolling_weight(theta, (kept_sizes.items[1] - kept_sizes.items[0]) / (length * (kept_at.items[1] - kept_at.items[0])))
        # Its section as a setback corner's patch takes it (degree 5,
        # rational): the circle's arc with the cone's middle weight.
        if continuity == g1_spline: shape = weighted_shape(cone / math.sin(theta * 0.5))
    let n0 = ends.n0.multiply_scalar(1.0 if ends.n0.dot(along) > 0.0 else -1.0) if ends.slanted0 else along
    let n1 = ends.n1.multiply_scalar(1.0 if ends.n1.dot(along) > 0.0 else -1.0) if ends.slanted1 else along
    var rolled = Rolled(start, along, length, ua, ub, theta, try ua.add(ub).normalized().multiply_scalar(1.0 / math.sin(theta * 0.5)), kept_at.view(), kept_sizes.view(), kept_slopes.view(), n0, n1)
    # Any other law: the balls' envelope (rolled_ball.lucb), unless it
    # folds, where Fusion scales the section instead.
    if (continuity == 1 or continuity == g1_spline) and blend_shape.plain() and not straight and not rolled_folds(&rolled):
        var net = Growing[Vector3]()
        defer net.close()
        var weights = Growing[f64]()
        defer weights.close()
        var knots = Growing[f64]()
        defer knots.close()
        let along_degree = rolled_net(&rolled, w.tolerances.fit, &net, &weights, &knots) catch failure:
            if failure.code != invalid: error(failure.code, failure.message)
            error(invalid, "a G1 variable fillet (Tangency weight 1) changes its radius too steeply here to roll its ball: spread its points, or give it another weight")
        var section_knots = Growing[f64]()
        defer section_knots.close()
        for _ in 0..<(usize)6: try section_knots.push(0.0)
        for _ in 0..<(usize)6: try section_knots.push(1.0)
        # The net row-major across (6 rows, each along).
        let count = net.count // 6
        let rolled_tool = try strip_solid(w.tolerances, start, end, ua, ub, face_outward(w, fa), face_outward(w, fb), net.view(), weights.view(), 6, count, 5, along_degree, section_knots.view(), knots.view(), length + reach * law.largest() * 4.0, name, convex_edge, ends)
        return rolled_tool
    if (ends.slanted0 or ends.slanted1) and cone < 0.0:
        # A slanted end: the sweep trimmed by it (tilted_sweep.lucb).
        var section = Growing[Vector3]()
        defer section.close()
        var section_weights = Growing[f64]()
        defer section_weights.close()
        var section_knots = Growing[f64]()
        defer section_knots.close()
        if continuity == 1: try arc_section(ua.multiply_scalar(reach), ub.multiply_scalar(reach), Vector3(), &section, &section_weights, &section_knots, blend_shape)
        else:
            try fillet_section(ua.multiply_scalar(reach), ub.multiply_scalar(reach), Vector3(), continuity, &section, &section_knots, blend_shape)
            try section_weights.fill(section.count, 1.0)
        var tilted = Tilted(start, along, length, kept_at.view(), kept_sizes.view(), kept_slopes.view(), section.view(), section_weights.view(), 5, section_knots.view(), (usize)continuity, ua, ub, n0, n1)
        var net = Growing[Vector3]()
        defer net.close()
        var weights = Growing[f64]()
        defer weights.close()
        var knots = Growing[f64]()
        defer knots.close()
        let along_degree = try tilted_net(&tilted, w.tolerances.fit, &net, &weights, &knots)
        var fitted_knots = Growing[f64]()
        defer fitted_knots.close()
        try fitted_knots.assign(section_knots.view())
        let count = net.count // section.count
        let tilted_tool = try strip_solid(w.tolerances, start, end, ua, ub, face_outward(w, fa), face_outward(w, fb), net.view(), weights.view(), section.count, count, 5, along_degree, fitted_knots.view(), knots.view(), length + reach * law.largest() * 4.0, name, convex_edge, ends)
        return tilted_tool
    return try variable_tool(w.tolerances, start, end, ua, ub, face_outward(w, fa), face_outward(w, fb), reach, weight, kept_at.view(), kept_sizes.view(), kept_slopes.view(), name, convex_edge, continuity, ends, shape, cone)

## Law `law`'s points that change something onto `kept_at`, `kept_sizes`
## and `kept_slopes`: those within one straight run (a flat one too, or
## between equal radii) go, for fewer knots.
func keep_law(law: const RadiusLaw*, kept_at: Growing[f64]*, kept_sizes: Growing[f64]*, kept_slopes: Growing[f64]*) -> !:
    let at = law.positions.view()
    let sizes = law.radii.view()
    let slopes = law.slopes.view()
    for k in 0..<at.length:
        if k > 0 and k + 1 < at.length:
            # Within one straight run: the slopes all the chord from the
            # last point kept to the next.
            let chord = (sizes[k + 1] - kept_sizes.items[kept_sizes.count - 1]) / (at[k + 1] - kept_at.items[kept_at.count - 1])
            let near = 1e-12 * math.max(1.0, math.abs(chord))
            let on = math.abs(sizes[k] - (sizes[k + 1] - chord * (at[k + 1] - at[k]))) <= near
            if on and math.abs(kept_slopes.items[kept_slopes.count - 1] - chord) <= near and math.abs(slopes[k] - chord) <= near and math.abs(slopes[k + 1] - chord) <= near: continue
        try kept_at.push(at[k])
        try kept_sizes.push(sizes[k])
        try kept_slopes.push(slopes[k])

## Whether a kept law is one straight piece: two points, both slopes its
## chord (a law held flat at a corner is not).
func straight_law(at: const f64[], sizes: const f64[], slopes: const f64[]) -> bool:
    if at.length != 2: return false
    let chord = sizes[1] - sizes[0]
    let near = 1e-12 * math.max(1.0, sizes[1])
    return math.abs(slopes[0] - chord) <= near and math.abs(slopes[1] - chord) <= near

## The end sections of straight edge `e`'s rolled strip under `law` from
## `start` to `end` (straight_law_tool's, G1 at the weight of 1): its six
## control points across at the start and at the end, from face a's
## contact to face b's; false when it does not roll (a straight law's cone,
## or a folding envelope, scaled).
func rolled_ends(w: const Shape*, e: usize, law: const RadiusLaw*, start: Vector3, end: Vector3, at_start: Growing[Vector3]*, at_end: Growing[Vector3]*) -> bool!:
    let (fa, fb) = try edge_faces(w, e)
    let length = end.subtract(start).length()
    let along = try end.subtract(start).normalized()
    let ua = try into_face(w, fa, e, along)
    let ub = try into_face(w, fb, e, along)
    let theta = math.acos(math.clamp(ua.dot(ub), -1.0, 1.0))
    var kept_at = Growing[f64]()
    defer kept_at.close()
    var kept_sizes = Growing[f64]()
    defer kept_sizes.close()
    var kept_slopes = Growing[f64]()
    defer kept_slopes.close()
    try keep_law(law, &kept_at, &kept_sizes, &kept_slopes)
    if straight_law(kept_at.view(), kept_sizes.view(), kept_slopes.view()): return false
    var rolled = Rolled(start, along, length, ua, ub, theta, try ua.add(ub).normalized().multiply_scalar(1.0 / math.sin(theta * 0.5)), kept_at.view(), kept_sizes.view(), kept_slopes.view(), along, along)
    if rolled_folds(&rolled): return false
    try rolled_section(&rolled, 0.0, at_start)
    try rolled_section(&rolled, length, at_end)
    return true

## The middle weight of the section square to the edge of the cone a ball
## rolls out between flat faces `theta` apart, its radius growing `slope`
## per length along the edge (Fusion's G1 at a Tangency weight of 1 along a
## straight law): the section is the conic tangent to both faces at the
## contacts (the ball touches each face at the foot of its center, r cot
## (θ/2) from the edge), its middle control point the edge's own point, and
## one weight all along (every section is the first scaled about the
## cone's apex, on the edge where the radius would be 0). Along the
## bisector, at d from the edge, the cone (its axis the centers' line, its
## half angle sin α = m / |c'| for c' = (1, m / sin(θ/2)) along and across)
## meets the plane square to the edge at
##   d / r = (1 / sin(θ/2) - √(1 + m² / sin²(θ/2) - m²)) / (1 - m²),
## and the conic's shoulder there gives w = r cos²(θ/2) / (sin(θ/2) d) - 1:
## sin(θ/2), the circle's, when the radius holds (m = 0).
func rolling_weight(theta: f64, slope: f64) -> f64:
    let half = math.sin(theta * 0.5)
    let m2 = slope * slope
    let reach = (1.0 / half - math.sqrt(1.0 + m2 / (half * half) - m2)) / (1.0 - m2)
    let c = math.cos(theta * 0.5)
    return c * c / (half * reach) - 1.0

## The planes a variable radius tool ends in, at its start and end
## (outward normals, offsets), where `slanted` (else it ends square).
struct EndPlanes:
    var n0: Vector3
    var h0: f64
    var slanted0: bool
    var n1: Vector3
    var h1: f64
    var slanted1: bool

## The radius of law piece `j` (points `positions`, `radii`, `slopes`) at
## `s`, its polynomial carried on outside the piece.
func piece_radius(positions: const f64[], radii: const f64[], slopes: const f64[], j: usize, s: f64) -> f64:
    return hermite(positions, radii, slopes, j, s)

## Where a row of a variable tool (the edge's point at `s` from `low` along
## `span`, plus law piece `j`'s radius times `q`) meets the plane n·x = h:
## Newton from `s`.
func row_meets(low: Vector3, span: Vector3, q: Vector3, positions: const f64[], radii: const f64[], slopes: const f64[], j: usize, n: Vector3, h: f64, s: f64) -> f64!:
    var x = s
    let step = 1e-7
    for _ in 0..<50:
        let f = n.dot(low.add(span.multiply_scalar(x)).add(q.multiply_scalar(piece_radius(positions, radii, slopes, j, x)))) - h
        let g = n.dot(low.add(span.multiply_scalar(x + step)).add(q.multiply_scalar(piece_radius(positions, radii, slopes, j, x + step)))) - h
        let slope = (g - f) / step
        if math.abs(slope) < 1e-12: error(invalid, "a variable radius fillet's strip runs along its end face")
        let next = x - f / slope
        if math.abs(next - x) <= 1e-14: return next
        x = next
    return x

## Whether the edge between faces `fa` and `fb` ends at vertex `v` at a
## flat face square to its way `along`.
func ends_square(w: const Shape*, v: i64, fa: usize, fb: usize, along: Vector3) -> bool:
    for f in 0..<w.faces.count:
        if f == fa or f == fb: continue
        var holds = false
        for use in w.face_uses(f):
            let o = w.edges.items[edge_index(use)]
            if o.start == v or o.end == v: holds = true
        if not holds: continue
        let s = &w.surfaces.items[(usize)w.faces.items[f].surface]
        if s.kind == surface_plane and math.abs(math.abs(s.z.dot(along)) - 1.0) < 1e-9: return true
    return false

## The tool of a variable radius fillet of the edge from `start` to `end`
## (faces' ways `ua`, `ub` into them and their outward normals `na`, `nb`,
## contacts `reach` r along each, a G1 arc's middle weight `weight`), its
## blend face named `name`, of `continuity`; `convex_edge` for a convex edge
## (the tool taken away; an inside one's, in the air, is added). Where
## `ends` says an end is slanted, each row of the net stops in that end's
## plane: its first (last) piece runs from there, the same polynomial, so
## every row keeps the knots and the end's section lies in the plane.
## With `cone` 0 or more (a G1 ball rolling, one straight piece:
## rolling_weight) the section's middle weight is that, and each row, a
## line through the cone's apex, is rational: weighted inversely to the
## radius where it starts and ends, so the rows at a slanted end, stopped
## at different radii, still meet in a section of the cone (a row's part
## along the cone's rulings is then the same for every row: the surface
## stays on the cone).
func variable_tool(tolerances: CadTolerances, start: Vector3, end: Vector3, ua: Vector3, ub: Vector3, na: Vector3, nb: Vector3, reach: f64, weight: f64, positions: const f64[], radii: const f64[], slopes: const f64[], name: str, convex_edge: bool, continuity: i64, ends: const EndPlanes*, blend_shape: BlendShape, cone: f64 = -1.0) -> interop.Reference[CadModel]!:
    let pieces = positions.length - 1
    let count = 3 * pieces + 1
    # Along: a cubic Bezier per piece, knots thrice at each point.
    var knots = Growing[f64]()
    defer knots.close()
    for _ in 0..<4: try knots.push(0.0)
    for k in 1..<pieces:
        for _ in 0..<3: try knots.push(positions[k])
    for _ in 0..<4: try knots.push(1.0)
    # The section at unit radius from the edge's point: control points
    # `unit_section` (contact a to contact b), their weights and knots.
    var unit_section = Growing[Vector3]()
    defer unit_section.close()
    var section_knots = Growing[f64]()
    defer section_knots.close()
    var section_weights = Growing[f64]()
    defer section_weights.close()
    var degree: i64 = 2
    if continuity == g1_spline:
        # The circle's arc as setback corners' patches carry it (degree 5,
        # rational, the middle knot twice), so the strip ends in their curve.
        try arc_section(ua.multiply_scalar(reach), ub.multiply_scalar(reach), Vector3(), &unit_section, &section_weights, &section_knots, blend_shape)
        try middle_knot_twice(&unit_section, &section_weights, 0, &section_knots)
        degree = 5
    elif continuity == 1:
        try unit_section.push(ua.multiply_scalar(reach))
        try unit_section.push(Vector3())
        try unit_section.push(ub.multiply_scalar(reach))
        let k6: f64[6] = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]
        try section_knots.assign(k6[..<6])
        let x3: f64[3] = [1.0, cone if cone >= 0.0 else blend_shape.middle(weight), 1.0]
        try section_weights.assign(x3[..<3])
    else:
        try fillet_section(ua.multiply_scalar(reach), ub.multiply_scalar(reach), Vector3(), continuity, &unit_section, &section_knots, blend_shape)
        try section_weights.fill(unit_section.count, 1.0)
        degree = 5
    let rows = unit_section.count
    # The net, u (across: contact a to contact b) by v (along): along each
    # row, the edge's point plus r times its section point, each piece's
    # Bezier values from the piece's polynomial between the row's own ends
    # of it (a slanted end's plane, else the piece's points).
    var net = Growing[Vector3]()
    defer net.close()
    var weights = Growing[f64]()
    defer weights.close()
    let length = end.subtract(start)
    for u in 0..<rows:
        let q = unit_section.items[u]
        var first_s = 0.0
        var last_s = 1.0
        if ends.slanted0: first_s = try row_meets(start, length, q, positions, radii, slopes, 0, ends.n0, ends.h0, 0.0)
        if ends.slanted1: last_s = try row_meets(start, length, q, positions, radii, slopes, pieces - 1, ends.n1, ends.h1, 1.0)
        if first_s >= (positions[1] if pieces > 1 else last_s) or last_s <= (positions[pieces - 1] if pieces > 1 else first_s):
            error(invalid, "a variable radius fillet's slanted end reaches past its next radius point: move the point along")
        for j in 0..<pieces:
            let a = first_s if j == 0 else positions[j]
            let b = last_s if j == pieces - 1 else positions[j + 1]
            # The piece's cubic on [a, b] as a Bezier: its values at the
            # thirds, solved for the inner control values.
            let v0 = piece_radius(positions, radii, slopes, j, a)
            let v1 = piece_radius(positions, radii, slopes, j, a + (b - a) / 3.0)
            let v2 = piece_radius(positions, radii, slopes, j, a + (b - a) * 2.0 / 3.0)
            let v3 = piece_radius(positions, radii, slopes, j, b)
            let c1 = (-5.0 * v0 + 18.0 * v1 - 9.0 * v2 + 2.0 * v3) / 6.0
            let c2 = (2.0 * v0 - 9.0 * v1 + 18.0 * v2 - 5.0 * v3) / 6.0
            let values: f64[4] = [v0, c1, c2, v3]
            for m in 0..<(usize)4:
                if j > 0 and m == 0: continue
                let s = a + (b - a) * (f64)m / 3.0
                if cone >= 0.0:
                    # The line's ends weighted by the first radius over
                    # theirs, raised to a cubic.
                    let w0 = section_weights.items[u] * radii[0] / v0
                    let w1 = section_weights.items[u] * radii[0] / v3
                    let f = (f64)m / 3.0
                    let h = start.add(length.multiply_scalar(a)).add(q.multiply_scalar(v0)).multiply_scalar(w0 * (1.0 - f)).add(start.add(length.multiply_scalar(b)).add(q.multiply_scalar(v3)).multiply_scalar(w1 * f))
                    let hw = w0 * (1.0 - f) + w1 * f
                    try net.push(h.multiply_scalar(1.0 / hw))
                    try weights.push(hw)
                    continue
                try net.push(start.add(length.multiply_scalar(s)).add(q.multiply_scalar(values[m])))
                try weights.push(section_weights.items[u])
    let size = length.length() + reach * radii[0] * 4.0
    return try strip_solid(tolerances, start, end, ua, ub, na, nb, net.view(), weights.view(), rows, count, degree, 3, section_knots.view(), knots.view(), size, name, convex_edge, ends)

## A variable fillet's tool from its blend face's net (`rows` by `count`,
## row-major, u across from contact a to contact b by v along, `degree`
## across on `section_knots`, `along_degree` along on `knots`, with
## `weights`): the face, the flat faces on the edge's faces `a` and `b`
## (ways `ua`, `ub` into them, outward normals `na`, `nb`) from the edge
## (`start` to `end`) to the contacts, and the ends (square, or the
## slanted planes of `ends`). The face is outward toward the ball's
## center, the bisector's way, the tool lying between the edge and it.
func strip_solid(tolerances: CadTolerances, start: Vector3, end: Vector3, ua: Vector3, ub: Vector3, na: Vector3, nb: Vector3, net: const Vector3[], weights: const f64[], rows: usize, count: usize, degree: i64, along_degree: i64, section_knots: const f64[], knots: const f64[], size: f64, name: str, convex_edge: bool, ends: const EndPlanes*) -> interop.Reference[CadModel]!:
    let last_row = (rows - 1) * count
    let length = end.subtract(start)
    let solid = try BrepModel.sized(tolerances, size)
    defer solid.release()
    let b = solid.value()
    let e0 = try b.add_vertex(start)
    let e1 = try b.add_vertex(end)
    let a0 = try b.add_vertex(net[0])
    let a1 = try b.add_vertex(net[count - 1])
    let b0 = try b.add_vertex(net[last_row])
    let b1 = try b.add_vertex(net[last_row + count - 1])
    let ridge = try b.add_line(e0, e1)
    let to_a0 = try b.add_line(e0, a0)
    let to_b0 = try b.add_line(e0, b0)
    let to_a1 = try b.add_line(e1, a1)
    let to_b1 = try b.add_line(e1, b1)
    # The edges: the first and last rows (the contacts) and columns (the
    # end sections), with the net's own weights.
    let curve_a = try b.add_spline_edge(a0, a1, net[0..<count], weights[0..<count], along_degree, knots, true)
    let curve_b = try b.add_spline_edge(b0, b1, net[last_row..<last_row + count], weights[last_row..<last_row + count], along_degree, knots, true)
    var first = Growing[Vector3]()
    defer first.close()
    var last = Growing[Vector3]()
    defer last.close()
    var first_weights = Growing[f64]()
    defer first_weights.close()
    var last_weights = Growing[f64]()
    defer last_weights.close()
    for u in 0..<rows:
        try first.push(net[u * count])
        try last.push(net[u * count + count - 1])
        try first_weights.push(weights[u * count])
        try last_weights.push(weights[u * count + count - 1])
    let arc0 = try b.add_spline_edge(a0, b0, first.view(), first_weights.view(), degree, section_knots, true)
    let arc1 = try b.add_spline_edge(a1, b1, last.view(), last_weights.view(), degree, section_knots, true)
    # The blend face: outward toward the ball's center (away from the edge,
    # the tool lying between the edge and the face, either way).
    let surface = try b.add_spline_surface(net, weights, (i64)rows, (i64)count, degree, along_degree, section_knots, knots)
    let toward = try ua.add(ub).normalized()
    let sizes: i64[1] = [4]
    # Counterclockwise in (u, v), unit_section (a to b) by along: about du x dv.
    let du = net[last_row + count // 2].subtract(net[count // 2])
    let forward = du.cross(length).dot(toward) > 0.0
    let ahead: i64[4] = [arc0 + 1, curve_b + 1, -(arc1 + 1), -(curve_a + 1)]
    let back: i64[4] = [curve_a + 1, arc1 + 1, -(curve_b + 1), -(arc0 + 1)]
    let face = try b.add_face(surface, ahead[..<4] if forward else back[..<4], sizes[..<1], forward)
    try b.set_face_name(face, name)
    # The flat faces: on face a, on face b (the body's faces' own planes:
    # outward as the body's for a tool taken away, inward for one added),
    # and the two ends.
    let normal_a = na if convex_edge else na.multiply_scalar(-1.0)
    let normal_b = nb if convex_edge else nb.multiply_scalar(-1.0)
    let on_a: i64[4] = [ridge + 1, to_a1 + 1, -(curve_a + 1), -(to_a0 + 1)]
    let around_a: Vector3[4] = [start, end, net[count - 1], net[0]]
    try flat_face(b, start, normal_a, on_a[..<4], around_a[..<4])
    let on_b: i64[4] = [to_b0 + 1, curve_b + 1, -(to_b1 + 1), -(ridge + 1)]
    let around_b: Vector3[4] = [start, net[last_row], net[last_row + count - 1], end]
    try flat_face(b, start, normal_b, on_b[..<4], around_b[..<4])
    let way = try length.normalized()
    # The ends: square to the edge, or a slanted end's plane, facing away
    # from the tool.
    var start_normal = way.multiply_scalar(-1.0)
    if ends.slanted0: start_normal = ends.n0.multiply_scalar(1.0 if ends.n0.dot(way) < 0.0 else -1.0)
    var end_normal = way
    if ends.slanted1: end_normal = ends.n1.multiply_scalar(1.0 if ends.n1.dot(way) > 0.0 else -1.0)
    let at_start: i64[3] = [to_a0 + 1, arc0 + 1, -(to_b0 + 1)]
    let around_start: Vector3[3] = [start, net[0], net[last_row]]
    try flat_face(b, start, start_normal, at_start[..<3], around_start[..<3])
    let at_end: i64[3] = [to_b1 + 1, -(arc1 + 1), -(to_a1 + 1)]
    let around_end: Vector3[3] = [end, net[last_row + count - 1], net[count - 1]]
    try flat_face(b, end, end_normal, at_end[..<3], around_end[..<3])
    return try CadModel.of_brep(solid)