Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / src / modeling / local_corners.lucb

## The corners of a direct fillet (local_blend.lucb), one per vertex where
## a chosen edge ends; each vertex has three edges and three flat faces, or
## four to eight, all chosen.
## - One chosen edge (an end): its strip runs to the third face's plane
##   and stops there, cut by it. The face gains that cut; the two sharp
##   edges there end where the contact lines reach the plane.
## - Two (a miter): the strips meet in the plane bisecting their edges,
##   through where their contact lines cross on the face they share. Both
##   strips' sections there are one curve only when the corner is
##   symmetric about that plane (the sharp edge in it); else declined.
## - Three, G1 (a ball): each strip stops square to its edge through the
##   ball's center, in an arc of the ball, and a sphere patch closes the
##   corner, touching each face at one point.
## - Three, G2 or G3 (Fusion's Rolling ball corner): each strip stops
##   square to its edge through the ball's center, and three quads between
##   their end sections close the corner (three_sided.lucb).
## - Three with Fusion's Setback corner type, or of unequal radii (a
##   setback): each strip stops at its setback, and the six quads of the
##   patch (setback.lucb) close the corner; each face gains the curve
##   between its two fillets' contacts. Three of mixed kinds under Setback:
##   a vertex blend, the same patch.
## - Four to eight, all chosen: G1, a ball when one touches every face (a
##   regular pyramid's apex); else, and G2 and G3, a setback corner of 2N
##   quads (a G1 one's rational, about the strips' circles).
## - Two of different kinds where the third edge stays sharp (a Y
##   corner, y_blend.lucb): the one of the sharp edge's kind runs on, the
##   other's section rolls round its end (a torus at G1); three, one of a
##   kind: the pair's section rolls round all of the lone one's end.
## Otherwise all outside, or all inside.

## A corner's plan: its vertex and kind (1 an end, 2 a miter, 3 a ball, 4
## a setback, 5 a Y corner, 6 three-sided), the chosen edges there, a ball's center, a setback's frame,
## a miter's or end's plane (normal, offset).
struct CornerPlan:
    var vertex: i64
    var kind: i64
    var chosen: usize[8]
    var count: usize
    var center: Vector3
    var setback: Setback
    var plane: Vector3
    var offset: f64

    func close():
        self.count = 0

## Strip `s`'s end at vertex `v` (0 its edge's start, 1 its end).
func end_at(b: const Local*, s: usize, v: i64) -> usize:
    return 0 if b.w.edges.items[b.strips.items[s].edge].start == v else 1

## Vertex `v`'s plan; each strip's end there given its plane.
func plan_corner(b: Local*, v: i64) -> CornerPlan!:
    let w = b.w
    var plan = CornerPlan(v, 0, [0, 0, 0, 0, 0, 0, 0, 0], 0, Vector3(), empty_setback(v), Vector3(), 0.0)
    var count: usize = 0
    for e in 0..<w.edges.count:
        let edge = w.edges.items[e]
        if edge.start != v and edge.end != v: continue
        if count == most_setback_edges: error(invalid, "a corner has more than eight edges")
        count += 1
        if b.strip_of.items[e] >= 0:
            plan.chosen[plan.count] = e
            plan.count += 1
    if count < 3: error(invalid, "a corner needs three edges")
    if count > 3 and plan.count != count: error(invalid, "a corner of four or more edges needs all of them")
    var convex_count: usize = 0
    for k in 0..<plan.count:
        if b.strips.items[(usize)b.strip_of.items[plan.chosen[k]]].convex: convex_count += 1
    let mixed = convex_count != 0 and convex_count != plan.count
    if mixed:
        if count != 3: error(invalid, "an inside and an outside fillet meet")
        if plan.count == 2:
            try plan_y(b, v, &plan)
            return plan
        if not b.setback:
            try plan_y3(b, v, &plan)
            return plan
    let p = w.points.items[(usize)v]
    if plan.count == 1:
        # The third face: the one the edge does not lie on.
        let s = (usize)b.strip_of.items[plan.chosen[0]]
        let strip = b.strips.items[s]
        for f in try vertex_faces(b, v):
            if f == strip.faces[0] or f == strip.faces[1]: continue
            let n = face_outward(w, f)
            plan.kind = 1
            plan.plane = n
            plan.offset = n.dot(p)
        try set_end(b, s, v, plan.plane, plan.offset)
        return plan
    if plan.count == 2:
        let sa = (usize)b.strip_of.items[plan.chosen[0]]
        let sb = (usize)b.strip_of.items[plan.chosen[1]]
        let a = b.strips.items[sa]
        let c = b.strips.items[sb]
        let shared = shared_strip_face(&a, &c)
        if shared < 0: error(invalid, "a miter's fillets share no face")
        let da = a.along.multiply_scalar(1.0 if end_at(b, sa, v) == 0 else -1.0)
        let dc = c.along.multiply_scalar(1.0 if end_at(b, sb, v) == 0 else -1.0)
        let ia = a.intos[0 if a.faces[0] == (usize)shared else 1]
        let ic = c.intos[0 if c.faces[0] == (usize)shared else 1]
        let x = try contacts_cross(p, da, ia, a.reach, dc, ic, c.reach)
        plan.kind = 2
        plan.plane = try da.subtract(dc).normalized()
        plan.offset = plan.plane.dot(x)
        try set_end(b, sa, v, plan.plane, plan.offset)
        try set_end(b, sb, v, plan.plane, plan.offset)
        return plan
    # Each chosen edge's radius here (a feature's may differ edge by edge).
    var radii: f64[8] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    var largest = 0.0
    var same_radius = true
    for k in 0..<plan.count:
        radii[k] = b.strips.items[(usize)b.strip_of.items[plan.chosen[k]]].radius
        largest = math.max(largest, radii[k])
        same_radius = same_radius and radii[k] == radii[0]
    if b.continuity > 1 and same_radius and not mixed and plan.count == 3 and not b.setback and b.limits.setback == 0.0:
        # Three G2 or G3 fillets of one radius and kind: their strips stop
        # through the ball's center, a three-sided patch between their end
        # sections (Fusion's Rolling ball corner, three_sided.lucb).
        plan.kind = 6
        plan.setback = try three_sided_at(w, v, plan.chosen[..<plan.count], radii[0], convex_count == 0)
        plan.setback.fullness = b.limits.corner_fullness
        for i in 0..<plan.count:
            let s = (usize)b.strip_of.items[plan.setback.edges[i]]
            let way = plan.setback.ways[i]
            try set_end(b, s, v, way, way.dot(p.add(way.multiply_scalar(plan.setback.distances[i]))))
        return plan
    if b.continuity == 1 and same_radius and not mixed and not (b.setback and plan.count == 3):
        # Three G1 fillets of one radius meet in a ball; four or more where
        # one touches every face (inside, the ball's planes move out into
        # the air). Unequal radii meet at a setback corner.
        let radius = radii[0] if convex_count > 0 else -radii[0]
        var center = Vector3(1e300, 0.0, 0.0)
        if plan.count == 3: center = try ball_center(w, v, radius)
        else:
            center = ball_center(w, v, radius) catch failure:
                if failure.code != invalid: error(failure.code, failure.message)
                recover Vector3(1e300, 0.0, 0.0)
        if center.x < 1e299:
            plan.kind = 3
            plan.center = center
            for k in 0..<plan.count:
                let s = (usize)b.strip_of.items[plan.chosen[k]]
                let along = b.strips.items[s].along
                try set_end(b, s, v, along, along.dot(plan.center))
            return plan
    plan.kind = 4
    # (Mixed kinds: a vertex blend, its patch between faces it meets from
    # either side.)
    plan.setback = try setback_at(w, v, plan.chosen[..<plan.count], largest, convex_count == 0, radii[..<plan.count])
    plan.setback.mixed = mixed
    plan.setback.edge_shape = b.limits.corner_shape
    plan.setback.fullness = b.limits.corner_fullness
    if plan.count > 3 or not same_radius: try roomy_setback(w, &plan.setback, largest, b.continuity)
    # Setbacks asked: no fillet stops nearer its corner than that.
    for i in 0..<plan.count: plan.setback.distances[i] = math.max(plan.setback.distances[i], b.limits.setback * largest)
    for i in 0..<plan.count:
        let s = (usize)b.strip_of.items[plan.setback.edges[i]]
        let way = plan.setback.ways[i]
        try set_end(b, s, v, way, way.dot(p.add(way.multiply_scalar(plan.setback.distances[i]))))
    return plan

func set_end(b: Local*, s: usize, v: i64, n: Vector3, h: f64) -> !:
    let end = end_at(b, s, v)
    b.strips.items[s].planes[end] = n
    b.strips.items[s].offsets[end] = h

## The face two strips share, or -1.
func shared_strip_face(a: const Strip*, c: const Strip*) -> i64:
    for f in a.faces:
        if f == c.faces[0] or f == c.faces[1]: return (i64)f
    return -1

## Plan `plan`'s corner: its strips' contacts and sections there, the
## sharp edges' new ends, the curves it leaves on faces and its faces.
func make_corner(b: Local*, plan: const CornerPlan*, tag: str) -> !:
    if plan.kind == 1: try make_end(b, plan)
    elif plan.kind == 2: try make_miter(b, plan)
    elif plan.kind == 3: try make_ball(b, plan, tag)
    elif plan.kind == 5: try make_y(b, plan, tag)
    elif plan.kind == 6: try make_three_sided(b, plan, tag)
    else: try make_setback(b, plan, tag)

## A strip's contacts at its end at `v`, where its plane cuts the contact
## lines, as new points.
func cut_contacts(b: Local*, s: usize, v: i64) -> (i64, i64)!:
    let strip = b.strips.items[s]
    let end = end_at(b, s, v)
    let a = try b.new_point(contact_at(&strip, 0, try contact_stop(&strip, 0, strip.planes[end], strip.offsets[end])))
    let z = try b.new_point(contact_at(&strip, 1, try contact_stop(&strip, 1, strip.planes[end], strip.offsets[end])))
    b.strips.items[s].contacts[end * 2] = a
    b.strips.items[s].contacts[end * 2 + 1] = z
    return (a, z)

## The sharp edge at `v` between faces `f` and `g`, ending at point `p`
## there now.
func end_sharp(b: Local*, v: i64, f: usize, g: usize, p: i64) -> !:
    let w = b.w
    for e in 0..<w.edges.count:
        let edge = w.edges.items[e]
        if edge.start != v and edge.end != v: continue
        if b.strip_of.items[e] >= 0: continue
        let (fa, fb) = faces_of(b, e)
        if not ((fa == f and fb == g) or (fa == g and fb == f)): continue
        b.ends.items[e * 2 + (0 if edge.start == v else 1)] = p
        return
    error(invalid, "a corner's sharp edge is missing")

func make_end(b: Local*, plan: const CornerPlan*) -> !:
    let v = plan.vertex
    let s = (usize)b.strip_of.items[plan.chosen[0]]
    let (a, z) = try cut_contacts(b, s, v)
    let strip = b.strips.items[s]
    let section = try plane_section(b, &strip, plan.plane, plan.offset, a, z)
    b.strips.items[s].sections[end_at(b, s, v)] = (i64)section
    # The end face: the face at the corner the strip does not lie on.
    var g: usize = 0
    for f in try vertex_faces(b, v):
        if f != strip.faces[0] and f != strip.faces[1]: g = f
    try end_sharp(b, v, strip.faces[0], g, a)
    try end_sharp(b, v, strip.faces[1], g, z)
    try b.links.push(Link(v, g, section))

func make_miter(b: Local*, plan: const CornerPlan*) -> !:
    let v = plan.vertex
    let w = b.w
    let sa = (usize)b.strip_of.items[plan.chosen[0]]
    let sc = (usize)b.strip_of.items[plan.chosen[1]]
    let a = b.strips.items[sa]
    let c = b.strips.items[sc]
    let shared = (usize)shared_strip_face(&a, &c)
    let side_a = (usize)(0 if a.faces[0] == shared else 1)
    let side_c = (usize)(0 if c.faces[0] == shared else 1)
    # Where each strip's contact lines reach the plane.
    let xa = contact_at(&a, side_a, try contact_stop(&a, side_a, plan.plane, plan.offset))
    let xc = contact_at(&c, side_c, try contact_stop(&c, side_c, plan.plane, plan.offset))
    let ya = contact_at(&a, 1 - side_a, try contact_stop(&a, 1 - side_a, plan.plane, plan.offset))
    let yc = contact_at(&c, 1 - side_c, try contact_stop(&c, 1 - side_c, plan.plane, plan.offset))
    let close = w.tolerance * 10.0
    if xa.subtract(xc).length() > close or ya.subtract(yc).length() > close: error(invalid, "an unsymmetric miter")
    let x = try b.new_point(xa)
    let y = try b.new_point(ya)
    let end_a = end_at(b, sa, v)
    let end_c = end_at(b, sc, v)
    b.strips.items[sa].contacts[end_a * 2 + side_a] = x
    b.strips.items[sa].contacts[end_a * 2 + 1 - side_a] = y
    b.strips.items[sc].contacts[end_c * 2 + side_c] = x
    b.strips.items[sc].contacts[end_c * 2 + 1 - side_c] = y
    let cut = b.strips.items[sa]
    let section = try plane_section(b, &cut, plan.plane, plan.offset, cut.contacts[end_a * 2], cut.contacts[end_a * 2 + 1])
    # The other strip's section must be the same curve: its middle on it.
    try same_section(b, sa, sc, plan.plane, plan.offset)
    b.strips.items[sa].sections[end_a] = (i64)section
    b.strips.items[sc].sections[end_c] = (i64)section
    try end_sharp(b, v, a.faces[1 - side_a], c.faces[1 - side_c], y)

## Whether strips `sa` and `sc` cut by one plane give one curve: the
## middle of each one's section (sampled as plane_section makes it) the
## same point.
func same_section(b: const Local*, sa: usize, sc: usize, n: Vector3, h: f64) -> !:
    let a = b.strips.items[sa]
    let c = b.strips.items[sc]
    let ma = try section_middle(b, &a, n, h)
    let mc = try section_middle(b, &c, n, h)
    if ma.subtract(mc).length() > b.w.tolerance * 10.0: error(invalid, "an unsymmetric miter")

## The middle of strip `strip`'s section in plane (`n`, `h`): the profile's
## middle (the circle's, or the curve's at half its parameter) moved along
## the edge onto the plane.
func section_middle(b: const Local*, strip: const Strip*, n: Vector3, h: f64) -> Vector3!:
    let ua = strip.intos[0]
    let ub = strip.intos[1]
    let angle = math.acos(math.clamp(ua.dot(ub), -1.0, 1.0))
    let bisector = try ua.add(ub).normalized()
    var middle = strip.origin.add(bisector.multiply_scalar(strip.radius / math.sin(angle * 0.5) - strip.radius))
    if b.continuity > 1:
        var section = Growing[Vector3]()
        defer section.close()
        var knots = Growing[f64]()
        defer knots.close()
        try fillet_section(strip.origin.add(ua.multiply_scalar(strip.reach)), strip.origin.add(ub.multiply_scalar(strip.reach)), strip.origin, b.continuity, &section, &knots, b.blend_shape)
        var weights = Growing[f64]()
        defer weights.close()
        try weights.fill(section.count, 1.0)
        middle = try NurbsCurve.at(section.view(), weights.view(), 5, knots.view(), 0.5)
    return middle.add(strip.along.multiply_scalar((h - n.dot(middle)) / n.dot(strip.along)))

func make_ball(b: Local*, plan: const CornerPlan*, tag: str) -> !:
    let v = plan.vertex
    let w = b.w
    let c = plan.center
    let n = plan.count
    let first = (usize)b.strip_of.items[plan.chosen[0]]
    let sign = 1.0 if b.strips.items[first].convex else -1.0
    # (The strips here are of one radius: plan_corner.)
    let radius = b.strips.items[first].radius
    # The ball touches each face at one point.
    var faces = Growing[usize]()
    defer faces.close()
    try corner_faces(b, v, &faces)
    var touches: i64[8] = [0, 0, 0, 0, 0, 0, 0, 0]
    for k in 0..<faces.count: touches[k] = try b.new_point(c.add(face_outward(w, faces.items[k]).multiply_scalar(sign * radius)))
    var sections: usize[8] = [0, 0, 0, 0, 0, 0, 0, 0]
    for k in 0..<n:
        let s = (usize)b.strip_of.items[plan.chosen[k]]
        let end = end_at(b, s, v)
        for side in 0..<(usize)2:
            for j in 0..<faces.count:
                if faces.items[j] == b.strips.items[s].faces[side]: b.strips.items[s].contacts[end * 2 + side] = touches[j]
        let a = b.strips.items[s].contacts[end * 2]
        let z = b.strips.items[s].contacts[end * 2 + 1]
        let arc = try b.arc(a, z, c, radius)
        let uses: i64[1] = [arc + 1]
        sections[k] = try b.chain(a, z, uses[..<1])
        b.strips.items[s].sections[end] = (i64)sections[k]
    # The sphere patch: the arcs head to tail.
    var loop = Growing[i64]()
    defer loop.close()
    var at = b.chains.items[sections[0]].head
    var used: bool[8] = [false, false, false, false, false, false, false, false]
    for _ in 0..<n:
        var found = false
        for k in 0..<n:
            if used[k]: continue
            let chain = b.chains.items[sections[k]]
            if chain.head != at and chain.tail != at: continue
            try b.walk(sections[k], at, &loop)
            at = b.other_end(sections[k], at)
            used[k] = true
            found = true
            break
        if not found: error(invalid, "a ball corner's arcs do not close")
    var middle = Vector3()
    for k in 0..<faces.count: middle = middle.add(try b.at(touches[k]).subtract(c).normalized())
    let pole = try middle.normalized()
    var empty_points: Vector3[]
    var empty_numbers: f64[]
    try b.out.surfaces.push(try Surface(4, c, try across(pole), pole, radius, 0.0, empty_points, empty_numbers, empty_numbers, empty_numbers, 0, 0, 0, 0))
    b.name.clear()
    try corner_name(w, (usize)v, tag, &b.name)
    try b.extras.push(Extra((i64)b.out.surfaces.count - 1, sign > 0.0, try b.out.add_name((str)b.name.view()), b.store.count, loop.count))
    for use in loop.view(): try b.store.push(use)

func make_setback(b: Local*, plan: const CornerPlan*, tag: str) -> !:
    let v = plan.vertex
    let w = b.w
    let corner = &plan.setback
    let n = corner.count
    let sides = n * 2
    var quads = Growing[Vector3]()
    defer quads.close()
    var knots = Growing[f64]()
    defer knots.close()
    var quad_weights = Growing[f64]()
    defer quad_weights.close()
    let p = w.points.items[(usize)v]
    var largest = 0.0
    for i in 0..<n: largest = math.max(largest, b.strips.items[(usize)b.strip_of.items[corner.edges[i]]].radius)
    # Fitted once per corner when a plan keeps patches; scaled after (G2
    # and G3, whose weights are all 1; a G1 corner's quads are rational).
    var kept = false
    if b.continuity > 1:
        if let cache = b.patches: kept = try cache.scaled(v, p, largest, &quads, &knots)
    if kept: try quad_weights.fill(quads.count, 1.0)
    else:
        try setback_patch(w, corner, largest, b.continuity, &quads, &quad_weights, &knots, b.blend_shape)
        if b.continuity > 1:
            if let cache = b.patches: try cache.keep(v, p, largest, quads.view(), knots.view())
    let frame = try setback_frame(w, corner, largest)
    # Hole corner 2i opens fillet i's end section, on face i - 1; 2i + 1
    # closes it, on face i.
    var touching: usize[16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    for k in 0..<sides: touching[k] = frame.faces[(k // 2 + n - 1) % n] if k % 2 == 0 else frame.faces[k // 2]
    let made = try patch_faces(b, v, tag, quads.view(), quad_weights.view(), knots.view(), sides, touching[..<sides])
    # Each strip ends in its curve's halves; each face gains the B curve.
    for i in 0..<n:
        let s = (usize)b.strip_of.items[corner.edges[i]]
        let end = end_at(b, s, v)
        for side in 0..<(usize)2:
            let f = b.strips.items[s].faces[side]
            if f == frame.faces[(i + n - 1) % n]: b.strips.items[s].contacts[end * 2 + side] = made.corners[i * 2]
            elif f == frame.faces[i]: b.strips.items[s].contacts[end * 2 + side] = made.corners[i * 2 + 1]
            else: error(invalid, "a setback's fillet is not on its faces")
        let halves: i64[2] = [made.firsts[i * 2] + 1, made.seconds[i * 2] + 1]
        b.strips.items[s].sections[end] = (i64)(try b.chain(made.corners[i * 2], made.corners[i * 2 + 1], halves[..<2]))
        let along_face: i64[2] = [made.firsts[i * 2 + 1] + 1, made.seconds[i * 2 + 1] + 1]
        try b.links.push(Link(v, frame.faces[i], try b.chain(made.corners[i * 2 + 1], made.corners[(i * 2 + 2) % sides], along_face[..<2])))

## A corner patch's out points and edges (patch_faces): the hole's corners
## (quad k's corner (0, 0)), and each curve's halves (curve k from hole
## corner k to its middle, `firsts[k]`, then on to corner k + 1,
## `seconds[k]`).
struct PatchEdges:
    var corners: i64[16]
    var firsts: i64[16]
    var seconds: i64[16]

## Corner patch `quads` (`sides` quads of degree 5, `quad_weights`, knots
## `knots` both ways; patch_fit.lucb's corner_patch) at vertex `v` as faces
## of the result, named for the corner: its points and edges made from the
## quads' own rows (each curve's halves, each seam) and its faces turned
## outward as face `touching[k]`, which quad k meets tangentially at its
## hole corner (an outside corner's, an inside one's and a vertex blend's
## alike).
func patch_faces(b: Local*, v: i64, tag: str, quads: const Vector3[], quad_weights: const f64[], knots: const f64[], sides: usize, touching: const usize[]) -> PatchEdges!:
    let w = b.w
    let m = knots.length - 6
    let mm = m * m
    var reversed = Growing[f64]()
    defer reversed.close()
    var k_ = knots.length
    while k_ > 0:
        k_ -= 1
        try reversed.push(1.0 - knots[k_])
    var made = PatchEdges([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    # The hole's corners, its curves' middles (quad k's (1, 0)) and the
    # center; each curve's halves and each seam, from the quads' rows (as
    # corner_solid).
    var middles: i64[16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    for k in 0..<sides:
        made.corners[k] = try b.new_point(quads[k * mm])
        middles[k] = try b.new_point(quads[k * mm + (m - 1) * m])
    let center = try b.new_point(quads[mm - 1])
    var seams: i64[16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    var net = Growing[Vector3]()
    defer net.close()
    var net_weights = Growing[f64]()
    defer net_weights.close()
    for k in 0..<sides:
        let next = (k + 1) % sides
        net.clear()
        net_weights.clear()
        for a in 0..<m:
            try net.push(quads[k * mm + a * m])
            try net_weights.push(quad_weights[k * mm + a * m])
        made.firsts[k] = try b.rational(made.corners[k], middles[k], net.view(), net_weights.view(), knots)
        net.clear()
        net_weights.clear()
        for b_ in 0..<m:
            try net.push(quads[next * mm + m - 1 - b_])
            try net_weights.push(quad_weights[next * mm + m - 1 - b_])
        made.seconds[k] = try b.rational(middles[k], made.corners[next], net.view(), net_weights.view(), reversed.view())
        net.clear()
        net_weights.clear()
        for b_ in 0..<m:
            try net.push(quads[k * mm + (m - 1) * m + b_])
            try net_weights.push(quad_weights[k * mm + (m - 1) * m + b_])
        seams[k] = try b.rational(middles[k], center, net.view(), net_weights.view(), knots)
    var bu: f64[36]
    var bv: f64[36]
    spline_basis(knots, 0.05, bu[..<36])
    spline_basis(knots, 0.05, bv[..<36])
    b.name.clear()
    try corner_name(w, (usize)v, tag, &b.name)
    let name = try b.out.add_name((str)b.name.view())
    for k in 0..<sides:
        let before = (k + sides - 1) % sides
        # Homogeneous sums: the rational quad's derivatives at (0.05, 0.05).
        var su = Vector3()
        var sv = Vector3()
        var sp = Vector3()
        var wu = 0.0
        var wv = 0.0
        var wp = 0.0
        for a in 0..<m:
            for b_ in 0..<m:
                let weight = quad_weights[k * mm + a * m + b_]
                let q = quads[k * mm + a * m + b_].multiply_scalar(weight)
                su = su.add(q.multiply_scalar(bu[9 + a] * bv[b_]))
                sv = sv.add(q.multiply_scalar(bu[a] * bv[9 + b_]))
                sp = sp.add(q.multiply_scalar(bu[a] * bv[b_]))
                wu += weight * bu[9 + a] * bv[b_]
                wv += weight * bu[a] * bv[9 + b_]
                wp += weight * bu[a] * bv[b_]
        let at = sp.multiply_scalar(1.0 / wp)
        let du = su.subtract(at.multiply_scalar(wu))
        let dv = sv.subtract(at.multiply_scalar(wv))
        try b.out.surfaces.push(try Surface(3, Vector3(), Vector3(), Vector3(), 0.0, 0.0, quads[k * mm..<(k + 1) * mm], quad_weights[k * mm..<(k + 1) * mm], knots, knots, (i64)m, (i64)m, 5, 5))
        let outward = du.cross(dv).dot(face_outward(w, touching[k])) > 0.0
        let loop: i64[4] = [made.firsts[k] + 1, seams[k] + 1, -(seams[before] + 1), made.seconds[before] + 1]
        try b.extras.push(Extra((i64)b.out.surfaces.count - 1, outward, name, b.store.count, 4))
        for use in loop: try b.store.push(use)
    return made