dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / src / modeling / shell.lucb
## Shell (Fusion's Shell): a solid hollowed to a wall of one thickness,
## with the chosen faces removed so the hollow opens there (none: a closed
## void inside). The wall lies inside the body, outside it, or both ways,
## half each.
##
## Two offset bodies are made with the local face operations
## (face_moves.lucb): an outer one, every face moved out by the outside
## thickness (the removed faces stay), and an inner one, every face moved
## in by the inside thickness while the removed faces move out past the
## outer body, so the inner one pokes through where the hollow opens. The
## shell is the outer body less the inner one (the booleans). Faces keep
## their names; the inner wall's are the feature's tag, `/`, and the face's
## own name.
##
## Faces offset as Offset Face's do: a plane moved, a cylinder's, cone's,
## sphere's or torus's radius changed, a B-spline's offset fitted
## (offset_surface.lucb). A thickness larger than a curved face's radius,
## or one that changes the body's topology (a wall thinner than the
## thickness), is refused, with the inner wall's own reason.
## Shell sides (shelled's `side`).
pub let shell_inside: i64 = 0
pub let shell_outside: i64 = 1
pub let shell_both: i64 = 2
## `model` shelled to `thickness` (see above), faces `removed` (marked)
## opened, the wall on `side`. Both ways, Fusion's Inside and Outside
## thickness: `thickness` inside and `outside_thickness` outside; a
## negative one splits `thickness` half each way.
func shell_of(model: interop.Reference[CadModel], removed: const bool[], thickness: f64, side: i64, tag: str, outside_thickness: f64) -> interop.Reference[CadModel]!:
if not (thickness > 0.0) or not math.is_finite(thickness): error(invalid, "a shell needs a positive thickness")
if side < shell_inside or side > shell_both: error(invalid, "a shell's wall is inside (0), outside (1) or both (2)")
if not math.is_finite(outside_thickness): error(invalid, "a shell needs a finite outside thickness")
var wall_in = thickness if side == shell_inside else (0.0 if side == shell_outside else thickness * 0.5)
var wall_out = thickness - wall_in
if side == shell_both and outside_thickness >= 0.0:
wall_in = thickness
wall_out = outside_thickness
let faces = removed.length
var all = Growing[usize]()
defer all.close()
for f in 0..<faces: try all.push(f)
var distances = Growing[f64]()
defer distances.close()
var targets = Growing[Surface]()
defer targets.close()
var senses = Growing[bool]()
defer senses.close()
let m = FaceMove(move_offset, Vector3(), Vector3(), Vector3(), 0.0, 0.0)
# The outer body: every face out by the outside thickness, the removed
# ones where they are.
var outer = model.clone()
defer outer.release()
if wall_out > 0.0:
var bigger = try shape_of(model.value())
defer bigger.close()
distances.clear()
for f in 0..<faces: try distances.push(0.0 if removed[f] else wall_out)
let grown = try substitute(&bigger, all.view(), &m, targets.view(), senses.view(), distances.view())
outer.release()
outer = grown
# The inner body: every face in by the inside thickness, the removed
# ones out past the outer body.
var w = try shape_of(model.value())
defer w.close()
distances.clear()
for f in 0..<faces: try distances.push(wall_in + wall_out if removed[f] else -wall_in)
# Its own reason, not a guess at one (it blamed a thin wall for a
# B-spline face it could not offset).
var reason = Growing[u8]()
defer reason.close()
var inner = substitute(&w, all.view(), &m, targets.view(), senses.view(), distances.view()) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
for byte in failure.message.bytes: try reason.push(byte)
recover model.clone()
defer inner.release()
if reason.count > 0:
# Convex rounds thinner than the wall vanish from the inner body
# (vanishing_inner): it is the inner body of the body without them.
var past = try vanishing_inner(model, removed, wall_in, wall_out, &m)
if past == none: past = try convex_inner(model, removed, wall_in, wall_out)
if past == none: past = try refilleted_inner(model, removed, wall_in, &m)
# Not convex, its rounds not healed together, or a flat face
# swallowed: the cells of its planes (shell_cells.lucb).
if past == none: past = try cells_inner(model, removed, wall_in, wall_out)
if let made = past:
inner.release()
inner = made
else:
# Opened at faces met tangentially all round (a body rounded on
# every edge): the closed shell, opened square to them.
if let opened = try tangent_opening(model, removed, thickness, side, tag, outside_thickness, wall_in, wall_out): return opened
var buffer: u8[640]
error(invalid, try strings.format(buffer, f"the shell's thickness is too large for this body: {(str)reason.view()}"))
var name = Growing[u8]()
defer name.close()
for byte in tag.bytes: try name.push(byte)
try name.push(47)
let named = try CadModeling.prefixed(inner, (str)name.view())
defer named.release()
# Nothing removed: the inner body lies wholly inside the outer one (its
# faces moved in, theirs out), a void. No face meets another, so the
# shell is the two, the inner turned inside out, without the booleans
# (their work there is every face pair's boxes overlapping for nothing:
# 11 s for a box rounded G2 on every edge).
var opened = false
for f in 0..<faces: opened = opened or removed[f]
if not opened: return try with_void(outer.value(), named.value())
return try combine(outer.value(), named.value(), op_subtract)
## A shell opened at faces that every neighbor meets tangentially (a body
## rounded on every edge, opened at its top: Limits 9). The neighbors'
## inner offsets never reach those faces' planes (a round's offset stays
## tangent below them), so the inner body cannot poke through there; Fusion
## takes the whole tangent chain instead, which is every face. Here the
## closed shell (a void) is opened by each removed face swept in by the
## wall (closed_quilt), so the opening's rim stands square to the face, the
## wall cut through between the outer face and the inner one's offset. Flat
## removed faces, the wall inside only; none otherwise.
func tangent_opening(model: interop.Reference[CadModel], removed: const bool[], thickness: f64, side: i64, tag: str, outside_thickness: f64, wall_in: f64, wall_out: f64) -> interop.Reference[CadModel]?!:
if wall_out > 0.0 or not (wall_in > 0.0): return none
var w = try shape_of(model.value())
defer w.close()
var any = false
for f in 0..<w.faces.count:
if not removed[f]: continue
any = true
if w.surfaces.items[(usize)w.faces.items[f].surface].kind != surface_plane: return none
for use in w.face_uses(f):
let e = edge_index(use)
let fa = w.sides.items[e * 2]
let fb = w.sides.items[e * 2 + 1]
if fa < 0 or fb < 0 or removed[(usize)fa] == removed[(usize)fb]: continue
if not try tangent_edge(&w, e): return none
if not any: return none
var none_removed = Growing[bool]()
defer none_removed.close()
try none_removed.fill(removed.length, false)
var current = try shell_of(model, none_removed.view(), thickness, side, tag, outside_thickness)
var rim = Growing[u8]()
defer rim.close()
for byte in tag.bytes: try rim.push(byte)
for byte in "/rim".bytes: try rim.push(byte)
var inner_name = Growing[u8]()
defer inner_name.close()
var found = Growing[usize]()
defer found.close()
for f in 0..<w.faces.count:
if not removed[f]: continue
# The hollow's own copy of the face (`<tag>/` and its name), swept
# out through the wall: the inner body poking through, as Fusion's
# opening (where the wall swallows the rounds round the face, the
# hollow's face is wider than the face itself: an L rounded 1,
# shelled 2 open, 3107.4377).
var c = try shape_of(current.value())
defer c.close()
inner_name.clear()
for byte in tag.bytes: try inner_name.push(byte)
try inner_name.push(47)
for byte in w.face_name(f).bytes: try inner_name.push(byte)
found.clear()
faces_named(&c, (str)inner_name.view(), &found) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
recover ()
let outward = face_outward(&w, f)
let cut = (closed_quilt(&c, found.view()[..<1], outward, wall_in * 2.0, "", (str)rim.view()) if found.count == 1 else closed_quilt(&w, [f][..<1], outward.multiply_scalar(-1.0), wall_in, "", (str)rim.view())) catch failure:
current.release()
error(failure.code, failure.message)
defer cut.release()
let next = combine(current.value(), cut.value(), op_subtract) catch failure:
current.release()
error(failure.code, failure.message)
current.release()
current = next
return current
## `outer` with the void `inner` (wholly inside it, meeting none of its
## faces): their faces as two shells of one body, the inner's turned.
func with_void(outer: const CadModel*, inner: const CadModel*) -> interop.Reference[CadModel]!:
var w = try shape_of(outer)
defer w.close()
let first = w.faces.count
var hollow = try shape_of(inner)
defer hollow.close()
try append_shape(&w, &hollow)
for f in first..<w.faces.count:
let face = &w.faces.items[f]
face.sense = not face.sense
var at = face.first
for k in 0..<face.loops:
let size = (usize)w.sizes.items[face.loop_first + k]
# The loop run backwards: each use turned, in reverse order.
var low = at
var high = at + size - 1
while low < high:
let swap = w.uses.items[low]
w.uses.items[low] = -w.uses.items[high]
w.uses.items[high] = -swap
low += 1
high -= 1
if low == high: w.uses.items[low] = -w.uses.items[low]
at += size
try w.index_sides()
let b = try brep_of(&w)
defer b.release()
return try CadModel.of_brep(b)
## Whether round face `f` (a cylinder, sphere or torus) turns outward: its
## outward normal away from its axis (a torus's tube's center line), a
## convex round.
func outward_round(w: const Shape*, f: usize) -> bool!:
let s = &w.surfaces.items[(usize)w.faces.items[f].surface]
let p = try w.edge_at(edge_index(w.face_uses(f)[0]), 0.5)
var core = s.origin
if s.kind == surface_cylinder: core = s.origin.add(s.z.multiply_scalar(p.subtract(s.origin).dot(s.z)))
if s.kind == surface_torus:
let flat = p.subtract(s.origin).subtract(s.z.multiply_scalar(p.subtract(s.origin).dot(s.z)))
core = s.origin.add((try flat.normalized()).multiply_scalar(s.radius))
return (try w.face_normal(f, p)).dot(p.subtract(core)) > 0.0
## The inner body of a shell whose wall (`wall_in` inside, nothing
## outside) is thicker than some convex rounds' radii (cylinders, spheres
## and tori turned outward, a fillet's strip or ball corner; G2 and G3
## strips between flat faces and their setback corners: swallowed_strips): their offsets
## vanish, and the faces round them meet past them. For a convex round that
## is exact: the body's inner parallel body at a distance beyond a round's
## radius is the one of the body with the round healed sharp (its tangent
## planes in between are then implied by its neighbors'). So the rounds are
## deleted (Delete's healing) and that body offset in; `removed` faces
## (marked on `model`) keep their names. None when nothing so vanishes or
## the healed body cannot be offset either.
func vanishing_inner(model: interop.Reference[CadModel], removed: const bool[], wall_in: f64, wall_out: f64, m: const FaceMove*) -> interop.Reference[CadModel]?!:
if wall_out > 0.0 or not (wall_in > 0.0): return none
var w = try shape_of(model.value())
defer w.close()
var names = Growing[u8]()
defer names.close()
for f in 0..<w.faces.count:
if removed[f]: continue
let s = &w.surfaces.items[(usize)w.faces.items[f].surface]
if s.kind != surface_cylinder and s.kind != surface_sphere and s.kind != surface_torus: continue
let radius = s.angle if s.kind == surface_torus else s.radius
if radius > wall_in + w.tolerance * 10.0: continue
if not try outward_round(&w, f): continue
if names.count > 0: try names.push(59)
for byte in w.face_name(f).bytes: try names.push(byte)
# G2 and G3 strips between flat faces whose radius the wall passes, and
# the setback corners between them (swallowed_strips).
try swallowed_strips(&w, removed, wall_in, &names)
if names.count == 0: return none
let sharp = CadDirect.deleted_faces(model, (str)names.view()) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
return none
defer sharp.release()
var v = try shape_of(sharp.value())
defer v.close()
# Healing leaves each sharp edge in the pieces the rounds' ends cut it
# into; a vertex between two pieces has only two faces to be found on.
try join_straight_pieces(&v)
var all = Growing[usize]()
defer all.close()
var distances = Growing[f64]()
defer distances.close()
for f in 0..<v.faces.count:
try all.push(f)
var open = false
for g in 0..<w.faces.count:
if removed[g] and w.face_name(g) == v.face_name(f): open = true
try distances.push(wall_in if open else -wall_in)
var targets = Growing[Surface]()
defer targets.close()
var senses = Growing[bool]()
defer senses.close()
let made = substitute(&v, all.view(), m, targets.view(), senses.view(), distances.view()) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
return none
return made
## Straight edges of `w` meeting end to end at a vertex no other edge has,
## along one line between the same two faces, joined into one (the vertex
## goes from every loop).
func join_straight_pieces(w: Shape*) -> !:
try w.index_sides()
var gone = Growing[bool]()
defer gone.close()
try gone.fill(w.edges.count, false)
var at_vertex = Growing[i64]()
defer at_vertex.close()
var changed = true
while changed:
changed = false
for v in 0..<w.points.count:
at_vertex.clear()
for e in 0..<w.edges.count:
if gone.items[e]: continue
let edge = w.edges.items[e]
if edge.start == (i64)v or edge.end == (i64)v: try at_vertex.push((i64)e)
if at_vertex.count != 2: continue
let a = (usize)at_vertex.items[0]
let b = (usize)at_vertex.items[1]
let ea = w.edges.items[a]
let eb = w.edges.items[b]
if w.carriers.items[(usize)ea.carrier].kind != curve_line or w.carriers.items[(usize)eb.carrier].kind != curve_line: continue
let same_faces = (w.sides.items[a * 2] == w.sides.items[b * 2] and w.sides.items[a * 2 + 1] == w.sides.items[b * 2 + 1]) or (w.sides.items[a * 2] == w.sides.items[b * 2 + 1] and w.sides.items[a * 2 + 1] == w.sides.items[b * 2])
if not same_faces: continue
let far_a = ea.start if ea.end == (i64)v else ea.end
let far_b = eb.start if eb.end == (i64)v else eb.end
let pa = w.points.items[(usize)far_a]
let pb = w.points.items[(usize)far_b]
let pv = w.points.items[v]
let line = pb.subtract(pa)
if line.length() <= w.tolerance: continue
let off = pv.subtract(pa).subtract(line.multiply_scalar(pv.subtract(pa).dot(line) / line.dot(line))).length()
if off > w.tolerance * 10.0: continue
# Edge a now runs from its far end to b's, on a's line, its way kept.
let c = w.carriers.items[(usize)ea.carrier]
let start = far_a if ea.end == (i64)v else far_b
let end = far_b if ea.end == (i64)v else far_a
let t0 = w.points.items[(usize)start].subtract(c.origin).dot(c.x)
let t1 = w.points.items[(usize)end].subtract(c.origin).dot(c.x)
w.edges.items[a] = ShapeEdge(ea.carrier, t0, t1, start, end)
gone.items[b] = true
changed = true
# Every loop without the joined edges' second pieces.
var uses = Growing[i64]()
defer uses.close()
var sizes = Growing[i64]()
defer sizes.close()
var faces = Growing[ShapeFace]()
defer faces.close()
for f in 0..<w.faces.count:
let face = w.faces.items[f]
let first_use = uses.count
let first_size = sizes.count
var at: usize = 0
for size in w.face_sizes(f):
var kept: i64 = 0
for use in w.face_uses(f)[at..<at + (usize)size]:
if gone.items[edge_index(use)]: continue
try uses.push(use)
kept += 1
try sizes.push(kept)
at += (usize)size
try faces.push(ShapeFace(face.surface, face.sense, first_use, uses.count - first_use, first_size, sizes.count - first_size, face.name))
try w.uses.assign(uses.view())
try w.sizes.assign(sizes.view())
try w.faces.assign(faces.view())
try compact_unused(w)
try w.index_sides()
## `w` without the edges no face uses and the points no edge ends at (left
## by joining pieces), the rest renumbered.
func compact_unused(w: Shape*) -> !:
var edge_map = Growing[i64]()
defer edge_map.close()
try edge_map.fill(w.edges.count, -1)
for use in w.uses.view(): edge_map.items[edge_index(use)] = 0
var point_map = Growing[i64]()
defer point_map.close()
try point_map.fill(w.points.count, -1)
var edges = Growing[ShapeEdge]()
defer edges.close()
var slack = Growing[f64]()
defer slack.close()
for e in 0..<w.edges.count:
if edge_map.items[e] < 0: continue
edge_map.items[e] = (i64)edges.count
try edges.push(w.edges.items[e])
try slack.push(w.edge_slack_of(e))
point_map.items[(usize)w.edges.items[e].start] = 0
point_map.items[(usize)w.edges.items[e].end] = 0
var points = Growing[Vector3]()
defer points.close()
var point_slack = Growing[f64]()
defer point_slack.close()
for p in 0..<w.points.count:
if point_map.items[p] < 0: continue
point_map.items[p] = (i64)points.count
try points.push(w.points.items[p])
try point_slack.push(w.point_slack_of(p))
for at in 0..<edges.count:
edges.items[at].start = point_map.items[(usize)edges.items[at].start]
edges.items[at].end = point_map.items[(usize)edges.items[at].end]
for at in 0..<w.uses.count:
let use = w.uses.items[at]
let moved = edge_map.items[edge_index(use)] + 1
w.uses.items[at] = moved if use > 0 else -moved
try w.edges.assign(edges.view())
try w.edge_slack.assign(slack.view())
try w.points.assign(points.view())
try w.point_slack.assign(point_slack.view())
## Onto `names` (apart by `;`), the B-spline strips of `w` the wall
## `wall` swallows, and the setback corners between them: a G2 or G3
## fillet's strip between two flat faces it meets tangentially along its
## long edges, round a convex edge, of a radius (its contacts' distance
## from the faces' meeting line over tan(φ/2), φ their turn) no more than
## the wall, and lying between its circle of that radius and the corner
## (as Fusion's G2 and G3 sections do), so a ball of the wall's radius at
## the corner's inner parallel stays clear of it: the inner parallel body is
## the sharp body's. A corner patch goes with them when every face it
## meets is a strip that goes, a flat face, or another such patch.
func swallowed_strips(w: const Shape*, removed: const bool[], wall: f64, names: Growing[u8]*) -> !:
var gone = Growing[bool]()
defer gone.close()
try gone.fill(w.faces.count, false)
var any = false
for f in 0..<w.faces.count:
if removed[f] or w.surfaces.items[(usize)w.faces.items[f].surface].kind != surface_spline: continue
if try swallowed_strip(w, f, wall):
gone.items[f] = true
any = true
if not any: return
# Corner patches: spline faces meeting only gone strips, flat faces and
# each other, tangentially.
var patch = Growing[bool]()
defer patch.close()
try patch.fill(w.faces.count, false)
for f in 0..<w.faces.count:
patch.items[f] = not removed[f] and not gone.items[f] and w.surfaces.items[(usize)w.faces.items[f].surface].kind == surface_spline
var changed = true
while changed:
changed = false
for f in 0..<w.faces.count:
if not patch.items[f]: continue
var keeps = true
var touches = false
for use in w.face_uses(f):
let e = edge_index(use)
let fa = w.sides.items[e * 2]
let fb = w.sides.items[e * 2 + 1]
if fa < 0 or fb < 0: continue
let other = (usize)(fb if (usize)fa == f else fa)
if other == f: continue
if gone.items[other]: touches = true
let flat = w.surfaces.items[(usize)w.faces.items[other].surface].kind == surface_plane
if not (gone.items[other] or flat or patch.items[other]) or not try tangent_edge(w, e): keeps = false
if not keeps or not touches:
if not keeps:
patch.items[f] = false
changed = true
for f in 0..<w.faces.count:
if not gone.items[f] and not patch.items[f]: continue
if patch.items[f] and not gone.items[f]:
# Only patches reached from a gone strip.
var reached = false
for use in w.face_uses(f):
let e = edge_index(use)
let fa = w.sides.items[e * 2]
let fb = w.sides.items[e * 2 + 1]
if fa < 0 or fb < 0: continue
let other = (usize)(fb if (usize)fa == f else fa)
reached = reached or gone.items[other] or (patch.items[other] and other != f)
if not reached: continue
if names.count > 0: try names.push(59)
for byte in w.face_name(f).bytes: try names.push(byte)
## Whether B-spline face `f` is a strip swallowed_strips takes at `wall`.
func swallowed_strip(w: const Shape*, f: usize, wall: f64) -> bool!:
# Its flat walls, met tangentially along straight edges.
var walls = Growing[usize]()
defer walls.close()
var contacts = Growing[Vector3]()
defer contacts.close()
for use in w.face_uses(f):
let e = edge_index(use)
let fa = w.sides.items[e * 2]
let fb = w.sides.items[e * 2 + 1]
if fa < 0 or fb < 0: continue
let other = (usize)(fb if (usize)fa == f else fa)
if w.surfaces.items[(usize)w.faces.items[other].surface].kind != surface_plane: continue
if w.carriers.items[(usize)w.edges.items[e].carrier].kind != curve_line: continue
if not try tangent_edge(w, e): continue
var known = false
for k in walls.view(): known = known or k == other
if known: continue
try walls.push(other)
try contacts.push(try w.edge_at(e, 0.5))
if walls.count != 2: return false
let na = face_outward(w, walls.items[0])
let nb = face_outward(w, walls.items[1])
let turn = math.acos(math.clamp(na.dot(nb), -1.0, 1.0))
if turn < 1e-6 or turn > math.pi - 1e-6: return false
# The walls' meeting line, and the contact's distance from it in its wall.
let line = try na.cross(nb).normalized()
let ca = contacts.items[0]
let sa = &w.surfaces.items[(usize)w.faces.items[walls.items[0]].surface]
let sb = &w.surfaces.items[(usize)w.faces.items[walls.items[1]].surface]
let ha = na.dot(sa.origin)
let hb = nb.dot(sb.origin)
# The point of the meeting line nearest the contact: on both planes.
let into_a = line.cross(na)
let step = (hb - nb.dot(ca)) / nb.dot(into_a)
let apex = ca.add(into_a.multiply_scalar(step))
if math.abs(na.dot(apex) - ha) > w.tolerance * 100.0: return false
let leg = apex.subtract(ca).length()
let radius = leg / math.tan(turn * 0.5)
if radius > wall + w.tolerance * 10.0: return false
# Round a convex edge: the meeting line outside, in front of the strip.
let middle = try point_on_face(w, f)
if (try w.face_normal(f, middle)).dot(apex.subtract(middle)) <= 0.0: return false
# Between its circle and the corner: its points across the section (the
# nearest to the chord between its contacts) no nearer the circle's
# center than the radius.
let center = ca.subtract(na.multiply_scalar(radius))
let other = contacts.items[1]
let cb = other.subtract(line.multiply_scalar(other.subtract(ca).dot(line)))
if math.abs(cb.subtract(center).length() - radius) > radius * 1e-6 + w.tolerance * 10.0: return false
for k in 1..<8:
let q = ca.add(cb.subtract(ca).multiply_scalar((f64)k / 8.0))
let p = try nearest_on_surface(w, f, q)
if p.subtract(center).length() < radius * (1.0 - 1e-6) - w.tolerance * 10.0: return false
return true
## The inner body of a body of flat faces and circular rounds between them
## (each a cylinder blending two flat walls, convex or concave, and their
## corners: balls and tori), not necessarily convex (an L rounded on every
## edge, where Delete cannot heal the convex rounds alone past the concave
## ones): every round deleted (Delete heals between flat faces), the sharp
## body's faces offset, and the rounds made again on it as one fillet of
## edge sets: a convex round of r at r - wall (gone when the wall passes
## it), a concave one at r + wall. Exact: a round's offset is the round of
## the offset walls about the same axis, and the fillet's corners (balls,
## setback corners, the rolled corners where both kinds meet) at the new
## radii are the old ones' offsets. `removed` faces must be flat. None when
## a curved face is no such round, or a step fails.
func refilleted_inner(model: interop.Reference[CadModel], removed: const bool[], wall_in: f64, m: const FaceMove*) -> interop.Reference[CadModel]?!:
if not (wall_in > 0.0): return none
var w = try shape_of(model.value())
defer w.close()
var names = Growing[u8]()
defer names.close()
var store = Growing[u8]()
defer store.close()
var walls = Growing[WallPair]()
defer walls.close()
for f in 0..<w.faces.count:
let s = &w.surfaces.items[(usize)w.faces.items[f].surface]
if s.kind == surface_plane: continue
if removed[f]: return none
if s.kind != surface_cylinder and s.kind != surface_sphere and s.kind != surface_torus: return none
# A round beside an opened face is tangent_opening's (the hollow
# opens square to the face there).
for use in w.face_uses(f):
let e = edge_index(use)
let fa = w.sides.items[e * 2]
let fb = w.sides.items[e * 2 + 1]
if (fa >= 0 and removed[(usize)fa]) or (fb >= 0 and removed[(usize)fb]): return none
if names.count > 0: try names.push(59)
for byte in w.face_name(f).bytes: try names.push(byte)
if s.kind != surface_cylinder: continue
# A strip: its two flat walls, met tangentially along its length.
let (fa, fb) = strip_walls(&w, f) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
return none
if w.surfaces.items[(usize)w.faces.items[fa].surface].kind != surface_plane or w.surfaces.items[(usize)w.faces.items[fb].surface].kind != surface_plane: return none
let size = s.radius - wall_in if try outward_round(&w, f) else s.radius + wall_in
if size <= w.tolerance * 100.0: continue
let a_from = store.count
for byte in w.face_name(fa).bytes: try store.push(byte)
let b_from = store.count
for byte in w.face_name(fb).bytes: try store.push(byte)
try walls.push(WallPair(a_from, b_from, b_from, store.count, size))
if names.count == 0: return none
let sharp = CadDirect.deleted_faces(model, (str)names.view()) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
return none
defer sharp.release()
var v = try shape_of(sharp.value())
defer v.close()
try join_straight_pieces(&v)
# The sharp body offset: every face in by the wall, removed ones out.
var all = Growing[usize]()
defer all.close()
var distances = Growing[f64]()
defer distances.close()
for f in 0..<v.faces.count:
try all.push(f)
var open = false
for g in 0..<w.faces.count:
if removed[g] and w.face_name(g) == v.face_name(f): open = true
try distances.push(wall_in if open else -wall_in)
var targets = Growing[Surface]()
defer targets.close()
var senses = Growing[bool]()
defer senses.close()
let offset = substitute(&v, all.view(), m, targets.view(), senses.view(), distances.view()) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
return none
if walls.count == 0: return offset
defer offset.release()
# The rounds again, as edge sets by radius.
var o = try shape_of(offset.value())
defer o.close()
var radii = Growing[f64]()
defer radii.close()
for pair in walls.view():
var known = false
for r in radii.view(): known = known or math.abs(r - pair.size) <= w.tolerance
if not known: try radii.push(pair.size)
var sets = Growing[u8]()
defer sets.close()
var edges = Growing[i64]()
defer edges.close()
var text = Growing[u8]()
defer text.close()
for at in 0..<radii.count:
edges.clear()
for pair in walls.view():
if math.abs(pair.size - radii.items[at]) > w.tolerance: continue
edges_between(&o, (str)store.view()[pair.a_from..<pair.a_to], (str)store.view()[pair.b_from..<pair.b_to], &edges) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
return none
try references_of(&o, edges.view(), &text)
if at > 0: try sets.push(124)
for byte in text.view(): try sets.push(byte)
let made = filleted_sets(offset.value(), (str)sets.view(), radii.view(), "r", 1, false) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
return none
return made
## The inner body of a convex body whose curved faces the wall all swallows
## (convex rounds of no more than its radius, G2 and G3 strips and their
## corners: vanishing_inner's), when Delete cannot heal them all (a G2
## setback corner's quads): the cell of its flat faces' planes, each moved
## in by the wall (a removed face's out past the outer body), every face
## named after its plane's face. For a convex body that is its inner
## parallel body: the rounds lie between their circles and their corners,
## clear of a ball of the wall's radius. None when the body is not convex
## or a curved face stays.
func convex_inner(model: interop.Reference[CadModel], removed: const bool[], wall_in: f64, wall_out: f64) -> interop.Reference[CadModel]?!:
if wall_out > 0.0 or not (wall_in > 0.0): return none
var w = try shape_of(model.value())
defer w.close()
for e in 0..<w.edges.count:
let fa = w.sides.items[e * 2]
let fb = w.sides.items[e * 2 + 1]
if fa < 0 or fb < 0 or fa == fb: continue
if not try tangent_edge(&w, e) and not try convex(&w, e): return none
# Convex all over: every vertex behind every flat face's plane (a body
# rounded on every edge has only tangent edges, an L's inside rounds
# too: its notch's planes cut it).
let slack = w.tolerance * 100.0
for f in 0..<w.faces.count:
let s = &w.surfaces.items[(usize)w.faces.items[f].surface]
if s.kind != surface_plane: continue
let n = face_outward(&w, f)
let h = n.dot(s.origin)
for p in w.points.view():
if n.dot(p) > h + slack: return none
# Every curved face swallowed.
var swallowed = Growing[u8]()
defer swallowed.close()
try swallowed_strips(&w, removed, wall_in, &swallowed)
var gone = Growing[usize]()
defer gone.close()
if swallowed.count > 0: try faces_named(&w, (str)swallowed.view(), &gone)
var normals = Growing[Vector3]()
defer normals.close()
var offsets = Growing[f64]()
defer offsets.close()
var sources = Growing[usize]()
defer sources.close()
var low = Vector3(1.0e300, 1.0e300, 1.0e300)
var high = Vector3(-1.0e300, -1.0e300, -1.0e300)
for f in 0..<w.faces.count:
let (l, h) = try face_box(&w, f)
low = lower(low, l)
high = upper(high, h)
let s = &w.surfaces.items[(usize)w.faces.items[f].surface]
if s.kind != surface_plane:
var taken = false
for g in gone.view(): taken = taken or g == f
let radius = s.angle if s.kind == surface_torus else s.radius
# A round the wall swallows, turned outward (a concave one, an
# inside corner's, grows instead: the body is not convex).
let round = (s.kind == surface_cylinder or s.kind == surface_sphere or s.kind == surface_torus) and radius <= wall_in + w.tolerance * 10.0 and try outward_round(&w, f)
if not taken and not round: return none
continue
let n = face_outward(&w, f)
let offset = n.dot(s.origin) + (wall_in if removed[f] else -wall_in)
# One plane once (a face split in pieces).
var known = false
for k in 0..<normals.count:
if normals.items[k].dot(n) > 1.0 - 1e-9 and math.abs(offsets.items[k] - offset) <= w.tolerance * 10.0: known = true
if known: continue
try normals.push(n)
try offsets.push(offset)
try sources.push(f)
# A wall thicker than the body leaves no cell: refused as before.
let cell = convex_cell(normals.view(), offsets.view(), high.subtract(low).length()) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
return none
defer cell.release()
# Each cell face named after the face whose plane it lies on.
var c = try shape_of(cell.value())
defer c.close()
if c.faces.count < 4: return none
for f in 0..<c.faces.count:
let n = face_outward(&c, f)
let o = n.dot(c.surfaces.items[(usize)c.faces.items[f].surface].origin)
for k in 0..<normals.count:
if normals.items[k].dot(n) > 1.0 - 1e-9 and math.abs(offsets.items[k] - o) <= w.tolerance * 100.0:
c.faces.items[f].name = try c.add_name(w.face_name(sources.items[k]))
let b = try brep_of(&c)
defer b.release()
return try CadModel.of_brep(b)