dymokomi/luce-cad
Source of release 0.9.5
0.9.5 / src / modeling / blend.lucb
## Chamfers and fillets made with booleans (the first rows of OpenCASCADE's
## ChFiKPart table): for each chosen edge a tool solid (blend_tools.lucb),
## the corner the blend takes away (a convex edge) or adds (a concave one),
## swept along the edge or turned about a rim's axis. Edges blended: lines
## between two planes, lines between a plane and a cylinder along them (a
## D-shaft's flat), and rims (a circle or arc between a flat face and a
## cylinder or cone about it). A chamfer's tool is the triangle between the
## edge and the points `distance` along each face; a fillet's the region
## between the edge and its section (a circle of `radius` touching both
## faces, or a G2/G3 B-spline). Where fillets meet at corners: corners.lucb
## (balls), setback.lucb (G2/G3); every edge of a convex polyhedron rounded
## whole: rounded.lucb. Tangent loops round a flat face (cap_blend.lucb) and
## straight edges between planes (local_blend.lucb) are first built directly.
## Whether a blend fits: blend_fits.lucb.
## How far into a face, as a share of the edge's length, its way in is
## sampled.
let into_face_share: f64 = 0.01
## A rim: the circle edge's center within this share of its radius of the
## round's axis, and its radius within this share of the round's there.
let rim_share: f64 = 1e-6
## A corner's radii differ from the size when apart by more than this share
## of it.
let radius_differs_share: f64 = 1e-9
## The unit direction across face `f` from the middle of edge `e`, into the
## face, perpendicular to the edge.
func into_face(w: const Shape*, f: usize, e: usize, along: Vector3) -> Vector3!:
let middle = try w.edge_at(e, 0.5)
let n = try w.face_normal(f, middle)
let side = try n.cross(along).normalized()
let reach = math.max((try w.edge_at(e, 0.0)).subtract(try w.edge_at(e, 1.0)).length(), (try w.edge_at(e, 0.0)).subtract(middle).length()) * into_face_share
if try face_contains(w, f, middle.add(side.multiply_scalar(reach)), w.tolerance) == inside_face: return side
return side.multiply_scalar(-1.0)
## The unit direction along edge `e` at its middle.
func along_edge(w: const Shape*, e: usize) -> Vector3!:
let edge = w.edges.items[e]
let t = (edge.t0 + edge.t1) * 0.5
return try (try w.tangent(edge.carrier, t)).multiply_scalar(1.0 if edge.t1 > edge.t0 else -1.0).normalized()
## A circle, or an arc of one, between a flat face across its axis and a
## cylinder or cone about it (a hole's rim, a boss's edge, the edge around
## an earlier fillet, a countersink's rim).
func is_rim(w: const Shape*, e: usize, fa: usize, fb: usize) -> bool:
let edge = w.edges.items[e]
let c = w.carriers.items[(usize)edge.carrier]
if c.kind != curve_circle: return false
let axis = c.x.cross(c.y)
let sa = w.surfaces.items[(usize)w.faces.items[fa].surface]
let sb = w.surfaces.items[(usize)w.faces.items[fb].surface]
let plane = sa if sa.kind == surface_plane else sb
let round = sb if sa.kind == surface_plane else sa
if plane.kind != surface_plane or (round.kind != surface_cylinder and round.kind != surface_cone): return false
if math.abs(math.abs(plane.z.dot(axis)) - 1.0) > parallel_slack or math.abs(math.abs(round.z.dot(axis)) - 1.0) > parallel_slack: return false
# On the cylinder or cone, about its axis: its radius there is the
# circle's (a cone's along its height).
let offset = c.origin.subtract(round.origin)
let height = offset.dot(round.z)
let radius = round.radius + (height * math.tan(round.angle) if round.kind == surface_cone else 0.0)
return offset.subtract(round.z.multiply_scalar(height)).length() < c.a * rim_share and math.abs(radius - c.a) < c.a * rim_share
## Whether edge `e` can be blended: a line between two planar faces, a
## rim (is_rim), or an edge round a flat face whose wall is ruled away
## from it (cap_blend.lucb: a box's top after its uprights were filleted,
## a tapered one's), convex or concave but not flat.
func blendable(w: const Shape*, e: usize) -> bool!:
let edge = w.edges.items[e]
let kind = w.carriers.items[(usize)edge.carrier].kind
if kind != curve_line and kind != curve_circle and kind != curve_ellipse and kind != curve_spline: return false
# A seam (one face's both ways) is no edge between two faces.
let (fa, fb) = edge_faces(w, e) catch failure:
_ = failure
return false
let capped = cap_candidate(w, e, fa, fb) or cap_candidate(w, e, fb, fa)
let curved = curved_pair(w, fa, fb)
if kind == curve_spline or kind == curve_ellipse:
if not capped and not curved: return false
elif kind == curve_circle:
if not is_rim(w, e, fa, fb) and sphere_rim(w, e, fa, fb) < 0 and not curved: return false
elif not flat_pair(w, fa, fb) and ruled_pair(w, e, fa, fb) < 0 and not capped and not curved: return false
let along = try along_edge(w, e)
let ua = try into_face(w, fa, e, along)
let ub = try into_face(w, fb, e, along)
return ua.cross(ub).length() > parallel_slack
## Whether faces fa and fb are both curved (neither flat): their edges
## are blended by a ball rolling between them (curved_blend.lucb).
func curved_pair(w: const Shape*, fa: usize, fb: usize) -> bool:
return w.surfaces.items[(usize)w.faces.items[fa].surface].kind != surface_plane and w.surfaces.items[(usize)w.faces.items[fb].surface].kind != surface_plane
func flat_pair(w: const Shape*, fa: usize, fb: usize) -> bool:
return w.surfaces.items[(usize)w.faces.items[fa].surface].kind == surface_plane and w.surfaces.items[(usize)w.faces.items[fb].surface].kind == surface_plane
## Of a straight edge `e` between a plane and a cylinder along it (a
## D-shaft's flat, a slot's side meeting its round end), the cylinder's
## face (fa or fb); -1 when it is not one.
func ruled_pair(w: const Shape*, e: usize, fa: usize, fb: usize) -> i64:
let sa = w.surfaces.items[(usize)w.faces.items[fa].surface]
let sb = w.surfaces.items[(usize)w.faces.items[fb].surface]
var cylinder: i64 = -1
if sa.kind == surface_plane and sb.kind == surface_cylinder: cylinder = (i64)fb
elif sa.kind == surface_cylinder and sb.kind == surface_plane: cylinder = (i64)fa
else: return -1
let c = w.surfaces.items[(usize)w.faces.items[(usize)cylinder].surface]
let edge = w.edges.items[e]
let direction = w.carriers.items[(usize)edge.carrier].x
if c.z.cross(direction).length() > parallel_slack: return -1
return cylinder
## Whether edge `e`'s faces turn away from each other (the other face runs
## below this one's outward normal): a blend takes material away there.
## Concave edges (an inside corner) are blended by adding it.
func convex(w: const Shape*, e: usize) -> bool!:
let (fa, fb) = try edge_faces(w, e)
let ub = try into_face(w, fb, e, try along_edge(w, e))
return ub.dot(try w.face_normal(fa, try w.edge_at(e, 0.5))) < 0.0
## `model` with the edges `references` refers to (references.lucb; empty:
## every straight convex edge between two planes) blended: filleted to
## `size` when `round`, else chamfered `size` along each face. The blend
## faces are named for feature `tag`. A fillet's `continuity` is 1 (G1,
## circular), 2 (G2) or 3 (G3): see corner_profile.
## With `chain`, each referenced edge brings the edges that continue it
## tangentially (Fusion's Tangent Chain): a slot's rim, the edges around an
## earlier fillet.
## `direct` false skips the direct build (local_blend.lucb), for tests
## that compare it with the booleans.
## With `cap` (Parasolid's caps by faces or sheets) every edge's blend
## stops at that surface (cap_cut).
pub func blended(model: const CadModel*, references: str, size: f64, round: bool, tag: str, continuity: i64 = 1, chain: bool = true, direct: bool = true, corner_type: i64 = 1, setback: bool = false, blend_shape: BlendShape = plain_blend, limits: BlendLimits = no_limits, cap: const BlendCap*? = none) -> interop.Reference[CadModel]!:
var w = try shape_of(model)
defer w.close()
var chosen = Growing[usize]()
defer chosen.close()
try choose_edges(&w, references, chain, &chosen)
return try blend_chosen(model, &w, chosen.view(), trimmed(references) == "", size, round, tag, continuity, direct, none, none, corner_type, setback, blend_shape, none, limits, cap)
## The edges of `w` that `references` names onto `chosen` (empty: every
## straight convex edge between flat faces; rims and inside corners only
## when named), with the edges continuing them tangentially when `chain`.
func choose_edges(w: Shape*, references: str, chain: bool, chosen: Growing[usize]*) -> !:
if trimmed(references) == "":
for e in 0..<w.edges.count:
if w.carriers.items[(usize)w.edges.items[e].carrier].kind == curve_line and try blendable(w, e) and try convex(w, e): try chosen.push(e)
else:
try resolve_edges(w, references, chosen)
for e in chosen.view():
if not try blendable(w, e): error(invalid, "only straight edges between flat faces, rims (a circle or arc between a flat face and a cylinder, cone or sphere about it), edges round a flat face whose walls are ruled away from it, and loops between two curved faces can be blended yet")
if chain: try tangent_chains(w, chosen)
if chosen.count == 0: error(invalid, "the body has no edges to blend")
## `model` (its shape `w`) with edges `chosen` blended to `size` (`every`:
## they are every edge, as blended's empty references). `patches` keeps
## setback corners' patches between sizes (FilletPlan), when given. A
## chamfer's `sides` (chamfers.lucb) give it two legs; none: Equal distance.
## A chamfer's `corner_type` is Fusion's Corner Type where three chamfers meet
## (chamfer_corners.lucb: corner_chamfer, corner_miter, corner_blend); a
## fillet's `setback` is Fusion's Setback corner type, which G1 fillets
## take only when asked (G2 and G3 corners are always setbacks).
## A fillet's `blend_shape` is its section's shape (blend_shape.lucb:
## Fusion's Tangency weight, Parasolid's conic and G2 shapes; fillet_section,
## arc_section): 1 leaves the sections as they are. A G1 fillet with
## another weight is a conic, not a circle, so it takes the booleans'
## tools (straight edges and rims, their sections rational splines) and
## setback corners, which a ball can't round; curved loops (curved_blend)
## fit the conic's weight directly.
## With `laws`, the edges they hold are rounded by their radius laws
## between where the corners stop them (variable_corners.lucb).
func blend_chosen(model: const CadModel*, w: Shape*, chosen: const usize[], every: bool, size: f64, round: bool, tag: str, continuity: i64, direct_asked: bool, patches: PatchCache*?, sides: const ChamferSides*? = none, corner_type: i64 = 1, setback_asked: bool = false, blend_shape: BlendShape = plain_blend, laws: const EdgeLaws*? = none, asked_limits: BlendLimits = no_limits, cap: const BlendCap*? = none) -> interop.Reference[CadModel]!:
# Parasolid's overlap limit: the limits where the blend fits.
var limits = asked_limits
if limits.overlap:
try check_limits(w, chosen, limits)
let (start, finish) = try overlap_limits(w, chosen[0], size, round)
limits.start = math.max(limits.start, start)
limits.finish = math.min(limits.finish, finish)
limits.overlap = false
if not math.is_finite(size) or size <= 0.0: error(invalid, "a blend needs a positive size")
if continuity < 1 or continuity > 3: error(invalid, "a fillet's continuity is G1, G2 or G3")
if round: try check_shape(blend_shape)
let conic = round and continuity == 1 and not blend_shape.plain()
# Limits (BlendLimits) take one straight edge, or its collinear pieces,
# by the booleans: the tool stops square at each limit, and the cut
# leaves the cap. A cap by a face or sheet likewise (cap_cut).
let limited = limits.limited() or cap != none
if limits.limited(): try check_limits(w, chosen, limits)
if not (limits.setback >= 0.0 and limits.setback < 100.0): error(invalid, "a fillet's setback is a share of its radius from 0 (as the corner needs) to 100")
let direct = direct_asked and not conic and not limited
let setback = setback_asked or conic or limits.setback > 0.0
# Each edge's blend must fit its faces: said precisely, before any tool.
# One that runs off a face (overflow) is the booleans' (their tools
# carry its spill; the direct builds follow the faces as they are).
var spills = false
for e in chosen:
# (Overlap limits keep it where it fits.)
if asked_limits.overlap: break
let (leg_a, leg_b) = try chamfer_legs(w, e, size, sides)
let (spill_a, spill_b) = try fits_plan(w, e, math.max(leg_a, leg_b), round, limits.overflow, continuity, blend_shape)
spills = spills or spill_a.how != spill_fits or spill_b.how != spill_fits
# Tangent loops round a flat face (its walls square to it) and
# straight edges between flat faces are built directly; what those
# decline (curved faces at a corner, mixed corners, chamfers...) the
# booleans below make.
# (Under a Tangency weight a G1 section is a conic, which a cap loop's
# strip carries as well; only the corner balls below need circles.)
if direct_asked and not limited and not spills:
if let made = try cap_blend(w, chosen, size, round, continuity, tag, sides, none, blend_shape): return made
if round and direct and not spills:
if let made = try local_blend(w, chosen, size, continuity, tag, patches, blend_shape, none, setback, limits): return made
# Loops between two curved faces (or a flat face and a sphere): a ball
# rolling between them, its G1 section a conic under a weight too.
if direct_asked and not limited and not spills:
if let made = try curved_blend(w, chosen, size, round, continuity, tag, sides, none, blend_shape): return made
# The booleans' tools follow lines and rims only.
for e in chosen:
let kind = w.carriers.items[(usize)w.edges.items[e].carrier].kind
if kind == curve_spline or kind == curve_ellipse:
error(invalid, "an edge along a B-spline or slanted face is blended only round a flat face whose walls are ruled away from it, each vertex a tangent joint or a smooth end: pick the loop or its tangent chain")
# Inside and outside fillets meeting at a corner: one side first, the
# other then runs around it (a torus), by name on the result.
if round and not every:
var later = Growing[usize]()
defer later.close()
try mixed_order(w, chosen, &later)
if later.count > 0:
var first = Growing[i64]()
defer first.close()
var second = Growing[i64]()
defer second.close()
for e in chosen:
if edge_in(later.view(), e): try second.push((i64)e)
else: try first.push((i64)e)
var text = Growing[u8]()
defer text.close()
try references_of(w, first.view(), &text)
let earlier = try blended(model, (str)text.view(), size, round, tag, continuity, false, direct, 1, setback_asked, blend_shape)
defer earlier.release()
try references_of(w, second.view(), &text)
return try blended(earlier.value(), (str)text.view(), size, round, tag, continuity, true, direct, 1, setback_asked, blend_shape)
# Every edge of a convex polyhedron rounded: built whole.
if round and every and continuity == 1 and not setback:
if let whole = try rounded_polyhedron(w, size, tag): return whole
# Where three fillets meet, a ball rounds the corner.
var balls = Growing[Ball]()
defer balls.close()
# Where three G2 or G3 fillets meet, a three-sided corner (a setback
# corner under the Setback corner type).
var setbacks = Growing[Setback]()
defer setbacks.close()
# Where chained edges meet tangentially, their tools end square.
var joints = Growing[i64]()
defer joints.close()
try fillet_corners(w, chosen, size, continuity, round, &balls, &setbacks, &joints, setback)
# A variable edge's law ending at another radius than the feature's at
# a corner: that corner's setbacks and patch take each edge's own radius
# there (an unequal-radius setback corner, fillet_sets.lucb's).
if let held = laws:
for at in 0..<setbacks.count:
let corner = setbacks.items[at]
# A straight law running in: Fusion's corner, its strip a cone
# (variable_corners.lucb's law_setback).
var straight = false
for i in 0..<corner.count:
let k = held.find(corner.edges[i])
if k >= 0 and straight_piece(&held.laws.items[(usize)k]): straight = true
if straight:
setbacks.items[at] = try law_setback(w, corner.vertex, corner.edges[..<corner.count], size, corner.inside, held)
continue
var radii: f64[8] = [size, size, size, size, size, size, size, size]
var differs = false
for i in 0..<corner.count:
let k = held.find(corner.edges[i])
if k < 0: continue
let law = &held.laws.items[(usize)k]
let edge = w.edges.items[corner.edges[i]]
let starts = law.forward.items[corner.edges[i]] == (edge.start == corner.vertex)
radii[i] = law.radii.items[0] if starts else law.radii.items[law.radii.count - 1]
differs = differs or math.abs(radii[i] - size) > size * radius_differs_share
if not differs: continue
var remade = try setback_at(w, corner.vertex, corner.edges[..<corner.count], size, corner.inside, radii[..<corner.count])
try roomy_setback(w, &remade, size, continuity)
setbacks.items[at] = remade
# Setbacks asked: no fillet stops nearer its corner than that.
for at in 0..<setbacks.count:
let corner = &setbacks.items[at]
corner.edge_shape = limits.corner_shape
corner.fullness = limits.corner_fullness
for i in 0..<corner.count: corner.distances[i] = math.max(corner.distances[i], limits.setback * size)
# G1 setback corners: the edges' sections the circle's arc as a
# B-spline, so their ends are the curves the corner's patch meets.
let tool_continuity = g1_spline if round and continuity == 1 and (setbacks.count > 0 or conic) else continuity
# Chamfer corners (Chamfer and Blend types): the tip taken off by a
# plane after the tools.
var tips = Growing[i64]()
defer tips.close()
if not round and corner_type != corner_miter: try chamfer_corners(w, chosen, &tips)
var body = try shape_rays(w)
defer body.close()
var tools = Growing[interop.Reference[CadModel]]()
defer:
for tool in tools.view(): tool.release()
tools.close()
# Whether each tool is added (a concave edge's) rather than taken away.
var adds = Growing[bool]()
defer adds.close()
# Variable edges' own end sections at setback corners.
var given = Growing[GivenSection]()
defer given.close()
# Collinear chosen edges between the same two faces (one edge cut into
# pieces by earlier operations) are one chain: one tool spans them.
var done = Growing[bool]()
defer done.close()
for _ in chosen: try done.push(false)
var name = Growing[u8]()
defer name.close()
for i in 0..<chosen.length:
if done.items[i]: continue
done.items[i] = true
let e = chosen[i]
var low = try w.edge_at(e, 0.0)
var high = try w.edge_at(e, 1.0)
try blend_name(w, e, tag, &name)
let adding = not try convex(w, e)
# A chamfer's legs along the edge's faces.
let (leg_a, leg_b) = try chamfer_legs(w, e, size, sides)
# A rim is its own tool.
if w.carriers.items[(usize)w.edges.items[e].carrier].kind != curve_line:
let edge = w.edges.items[e]
let at_start = vertex_in(joints.view(), edge.start)
let at_end = vertex_in(joints.view(), edge.end)
try tools.push(try cap_cut(try edge_tool(w, e, leg_a, round, tool_continuity, low, high, at_start, at_end, &body, (str)name.view(), leg_b, blend_shape), cap, (str)name.view()))
try adds.push(adding)
continue
let (fa, fb) = try edge_faces(w, e)
let along = try high.subtract(low).normalized()
for j in i + 1..<chosen.length:
if done.items[j]: continue
let other = chosen[j]
if not try same_curve(w, w.edges.items[e].carrier, other, w.tolerance): continue
let (ga, gb) = try edge_faces(w, other)
if not ((ga == fa and gb == fb) or (ga == fb and gb == fa)): continue
done.items[j] = true
for k in 0..<2:
let p = try w.edge_at(other, (f64)k)
if p.subtract(low).dot(along) < 0.0: low = p
if p.subtract(high).dot(along) > 0.0: high = p
# Limits: the tool from and to its fractions of the edge, square there.
if limited:
let span = high.subtract(low).dot(along)
let begin = low.add(along.multiply_scalar(span * limits.start))
high = low.add(along.multiply_scalar(span * limits.finish))
low = begin
# An end at a ball's corner stops where the ball's center is along it.
var exact_low = false
var exact_high = false
for ball in balls.view():
let corner = w.points.items[(usize)ball.vertex]
let foot = low.add(along.multiply_scalar(ball.center.subtract(low).dot(along)))
if corner.subtract(low).length() <= w.tolerance * 10.0:
low = foot
exact_low = true
elif corner.subtract(high).length() <= w.tolerance * 10.0:
high = foot
exact_high = true
# An end at a setback corner stops at its setback.
for corner in setbacks.view():
let apex = w.points.items[(usize)corner.vertex]
for k in 0..<corner.count:
if math.abs(corner.ways[k].dot(along)) < 1.0 - parallel_slack: continue
let foot = apex.add(corner.ways[k].multiply_scalar(corner.distances[k]))
if apex.subtract(low).length() <= w.tolerance * 10.0:
low = foot
exact_low = true
elif apex.subtract(high).length() <= w.tolerance * 10.0:
high = foot
exact_high = true
# The corners at both ends must leave the edge some length.
if exact_low or exact_high:
let original = try w.edge_at(e, 1.0).subtract(try w.edge_at(e, 0.0)).length()
let left = high.subtract(low).dot(along)
if left <= w.tolerance * 10.0:
let most = size * original / (original - left)
var buffer: u8[256]
let owned = try interop.copy_text(try strings.format(buffer, f"a fillet radius of {rounded_text(size)} is too large for this edge between its rounded corners: it must be less than {rounded_text(most)}"))
if let owner = owned.owner: _ = ownership.pool_object(owner)
error(invalid, owned.value)
for v in joints.view():
let corner = w.points.items[(usize)v]
if corner.subtract(low).length() <= w.tolerance * 10.0: exact_low = true
elif corner.subtract(high).length() <= w.tolerance * 10.0: exact_high = true
if limits.start > 0.0: exact_low = true
if limits.finish < 1.0: exact_high = true
var lawed = false
if let held = laws:
let at = held.find(e)
if at >= 0:
# Its law from its start to its end, as far as the corners
# let it go (the law's end radius held through them).
let first = try w.edge_at(e, 0.0)
let forward = low.subtract(first).length() <= high.subtract(first).length()
let start = low if forward else high
let end = high if forward else low
let way = try end.subtract(start).normalized()
let ends = EndPlanes(way.multiply_scalar(-1.0), 0.0, false, way, 0.0, false)
# A straight law is Fusion's along the whole edge, its strip
# cut where the corners stop it.
if straight_piece(&held.laws.items[(usize)at]):
var cut = try cut_law(w, e, &held.laws.items[(usize)at], start, end)
defer cut.close()
try tools.push(try straight_law_tool(w, e, &cut, tool_continuity, blend_shape, (str)name.view(), start, end, &ends))
else:
try tools.push(try straight_law_tool(w, e, &held.laws.items[(usize)at], tool_continuity, blend_shape, (str)name.view(), start, end, &ends))
# A G1 one rolls its ball (straight_law_tool): its end
# sections are the setback corners' sides there.
if round and continuity == 1 and blend_shape.plain(): try give_sections(w, e, &held.laws.items[(usize)at], start, end, setbacks.view(), &given)
lawed = true
if not lawed:
if adding:
# An inside corner's wall lower than the fillet: the ball
# rolled onto its rounded top, or its cliff-edge blend
# (fits_plan).
let (spill_a, spill_b) = (no_spill, no_spill) if asked_limits.overlap else try fits_plan(w, e, math.max(leg_a, leg_b), round, limits.overflow, continuity, blend_shape)
let spill = spill_a if spill_a.how == spill_cliff or spill_a.how == spill_rolled else spill_b
try tools.push(try cap_cut(try concave_tool(w, e, leg_a, round, tool_continuity, low, high, exact_low, exact_high, (str)name.view(), leg_b, blend_shape, spill), cap, (str)name.view()))
else:
# Running off a face (fits_plan): rolled on or ending on a
# cliff edge, its own section; over a ledge, stopped at its
# plane.
let (spill_a, spill_b) = (no_spill, no_spill) if asked_limits.overlap else try fits_plan(w, e, math.max(leg_a, leg_b), round, limits.overflow, continuity, blend_shape)
let spill = spill_a if spill_a.how == spill_rolled or spill_a.how == spill_cliff else spill_b
var tool = try edge_tool(w, e, leg_a, round, tool_continuity, low, high, exact_low, exact_high, &body, (str)name.view(), leg_b, blend_shape, spill)
for side in [spill_a, spill_b]:
if side.how != spill_ledge: continue
let cut = try cliff_cut(w, tool, side.face, (str)name.view())
tool.release()
tool = cut
try tools.push(try cap_cut(tool, cap, (str)name.view()))
try adds.push(adding)
for ball in balls.view():
try corner_name(w, (usize)ball.vertex, tag, &name)
try tools.push(try corner_tool(w, &ball, size, (str)name.view()))
try adds.push(ball.inside)
for at in setbacks.view():
try corner_name(w, (usize)at.vertex, tag, &name)
try tools.push(try setback_tool(w, &at, size, continuity, (str)name.view(), blend_shape, given.view()))
try adds.push(at.inside)
# Chamfers meeting three at a corner: its own face through the contact
# lines' crossings (Chamfer) or set back past them (Blend); Miter is the
# tools as they are.
for v in tips.view():
try corner_name(w, (usize)v, tag, &name)
try tools.push(try chamfer_corner_tool(w, v, chosen, size, sides, (str)name.view(), corner_type == corner_blend))
try adds.push(false)
# Material taken away first, then added.
var result = try copy_model(model)
for pass in 0..<2:
for at in 0..<tools.count:
if adds.items[at] != (pass == 1): continue
let next = combine(result.value(), tools.items[at].value(), op_union if pass == 1 else op_subtract) catch failure:
result.release()
error(failure.code, failure.message)
result.release()
result = next
return result
func edge_in(edges: const usize[], e: usize) -> bool:
for known in edges:
if known == e: return true
return false
## The edges of `chosen` to fillet after the rest, where inside and
## outside fillets meet at a corner: the lone outside edge first where it
## meets two inside ones (a boss's upright on a plate: the base then runs
## around its fillet), the lone inside edge first where two outside ones
## meet it (an L's inside upright under its top edges). Others: refused.
func mixed_order(w: const Shape*, chosen: const usize[], later: Growing[usize]*) -> !:
var seen = Growing[i64]()
defer seen.close()
var here = Growing[usize]()
defer here.close()
for e in chosen:
let ends: i64[2] = [w.edges.items[e].start, w.edges.items[e].end]
for v in ends:
if vertex_in(seen.view(), v): continue
try seen.push(v)
here.clear()
var concaves = 0
for other in chosen:
let o = w.edges.items[other]
if o.start != v and o.end != v: continue
try here.push(other)
if not try convex(w, other): concaves += 1
if concaves == 0 or concaves == (i64)here.count: continue
if here.count == 2:
error(invalid, "an inside and an outside fillet meeting where the corner's third edge stays sharp (a Y corner) are built between flat faces, the two edges square to each other and to the third face: fillet them one at a time")
if here.count != 3:
error(invalid, "an inside-corner fillet meeting an outside one at a corner is supported where two or three edges meet: fillet them one at a time")
# The lone kind goes first; the pair after.
let lone_inside = concaves == 1
for other in here.view():
let goes_later = (not try convex(w, other)) != lone_inside
if goes_later and not edge_in(later.view(), other): try later.push(other)
if not goes_later and edge_in(later.view(), other): error(invalid, "these fillets meet at corners in orders that conflict: fillet them in two features")
# An edge sent later at one corner but first at another conflicts.
seen.clear()
for e in later.view():
let ends: i64[2] = [w.edges.items[e].start, w.edges.items[e].end]
for v in ends:
var concaves = 0
var count = 0
for other in chosen:
let o = w.edges.items[other]
if o.start != v and o.end != v: continue
count += 1
if not try convex(w, other): concaves += 1
if concaves == 0 or concaves == count: continue
if (not try convex(w, e)) == (concaves == 1): error(invalid, "these fillets meet at corners in orders that conflict: fillet them in two features")
func vertex_in(vertices: const i64[], v: i64) -> bool:
for known in vertices:
if known == v: return true
return false
## `chosen` with the edges that continue its edges tangentially, of the
## same convexity, through vertices where nothing else could be blended
## (a slot's rim around its ends, the edges around an earlier fillet).
func tangent_chains(w: const Shape*, chosen: Growing[usize]*) -> !:
var at: usize = 0
while at < chosen.count:
let e = chosen.items[at]
at += 1
let outward = try convex(w, e)
for end in 0..<2:
let v = w.edges.items[e].start if end == 0 else w.edges.items[e].end
# Arriving at v along e.
let arriving = (try w.leaving(e, (i64)end)).multiply_scalar(-1.0)
for f in 0..<w.edges.count:
if f == e: continue
let other = w.edges.items[f]
if other.start != v and other.end != v: continue
var known = false
for c in chosen.view():
if c == f: known = true
if known: continue
let leaving = try w.leaving(f, 0 if other.start == v else 1)
if leaving.dot(arriving) < 1.0 - parallel_slack_loose: continue
if not try blendable(w, f) or try convex(w, f) != outward: continue
try chosen.push(f)
## The name of edge `e`'s blend, `tag/blend[A&B]`, into `out` (empty
## when its faces have no names).
func blend_name(w: const Shape*, e: usize, tag: str, out: Growing[u8]*) -> !:
out.clear()
let (fa, fb) = try edge_faces(w, e)
if w.face_name(fa) == "" or w.face_name(fb) == "": return
try role_name(out, tag, "blend[")
try edge_name(w, e, out)
try out.push(93)
## A new reference to `model`'s B-rep, placed as it is.
func copy_model(model: const CadModel*) -> interop.Reference[CadModel]!:
var w = try shape_of(model)
defer w.close()
let b = try brep_of(&w)
defer b.release()
return try CadModel.of_brep(b)
## Limits checked against the edges they cut: fractions 0 <= start <
## finish <= 1, one straight edge or collinear pieces of it between the same
## two faces (a limit is a place along one edge).
func check_limits(w: const Shape*, chosen: const usize[], limits: BlendLimits) -> !:
if not (limits.start >= 0.0 and limits.finish <= 1.0 and limits.start < limits.finish): error(invalid, "a blend's limits run from 0 to 1 along its edge, the first before the second")
if chosen.length == 0: return
let first = chosen[0]
if w.carriers.items[(usize)w.edges.items[first].carrier].kind != curve_line: error(invalid, "a limited blend takes a straight edge")
let (fa, fb) = try edge_faces(w, first)
for e in chosen:
let (ga, gb) = try edge_faces(w, e)
let same_faces = (ga == fa and gb == fb) or (ga == fb and gb == fa)
if not same_faces or not try same_curve(w, w.edges.items[first].carrier, e, w.tolerance): error(invalid, "a limited blend takes one straight edge (or its pieces along one line between the same faces)")
## Of straight edge `e` rolled by `law` from `start` to `end`, its end
## sections at the setback corners `corners` it reaches onto `given`
## (rolled_ends; none when its strip does not roll).
func give_sections(w: const Shape*, e: usize, law: const RadiusLaw*, start: Vector3, end: Vector3, corners: const Setback[], given: Growing[GivenSection]*) -> !:
var at_start = Growing[Vector3]()
defer at_start.close()
var at_end = Growing[Vector3]()
defer at_end.close()
if not try rolled_ends(w, e, law, start, end, &at_start, &at_end): return
let edge = w.edges.items[e]
for corner in corners:
if corner.vertex != edge.start and corner.vertex != edge.end: continue
let apex = w.points.items[(usize)corner.vertex]
let near_start = apex.subtract(start).length() <= apex.subtract(end).length()
let section = at_start if near_start else at_end
var points: Vector3[6] = [Vector3(), Vector3(), Vector3(), Vector3(), Vector3(), Vector3()]
for k in 0..<(usize)6: points[k] = section.items[k]
try given.push(GivenSection(corner.vertex, e, points))
## A cap (Parasolid's caps by faces or sheets): a blend stops where it meets
## surface `surface` (a face's of the body or of a sheet), the cut face left
## as the blend's cap (`<name>/cap/to/f<i>`). `away`: the side the surface
## bounds (a plane's normal side, a curved surface's inside) is taken
## away; else only it is kept.
pub struct BlendCap:
pub var surface: Surface
pub var away: bool
## Edge tool `tool` stopped at cap `cap` (none: the tool itself).
func cap_cut(tool: interop.Reference[CadModel], cap: const BlendCap*?, name: str) -> interop.Reference[CadModel]!:
let held = cap else return tool
defer tool.release()
var w = try shape_of(tool.value())
defer w.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 = &held.surface
let span = high.subtract(low).length() * 4.0 + s.origin.subtract(low).length() * 2.0
var label = Growing[u8]()
defer label.close()
for byte in name.bytes: try label.push(byte)
for byte in "/cap".bytes: try label.push(byte)
let (_, side) = try surface_side(s, s.origin, 1.0, s.z, 0.0, span, (str)label.view())
defer side.release()
let kept = combine(tool.value(), side.value(), op_subtract if held.away else op_intersect) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
error(invalid, "a fillet's cap does not cross its edge")
if kept.value().face_count() == 0:
kept.release()
error(invalid, "a fillet's cap leaves nothing of it: flip the cap")
return kept