dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / src / modeling / corners.lucb
## Fillets meeting at corners. Two fillets of one radius meeting where a
## third edge stays sharp meet in a miter: their tools, each reaching past
## the corner, cross in the ellipses of crossing_cylinders (intersect.lucb),
## and the booleans make the rest. Three meeting at a corner of three flat
## faces (all outside, or all inside) are rounded by a ball (OpenCASCADE's
## Rotule): each edge's tool
## stops at the plane through the ball's center square to it, where its
## cylinder touches the ball, and a corner tool (the corner beyond those
## planes, without the ball) leaves the sphere patch.
## A corner where three fillets meet: its vertex, the center of the ball
## rolling into it, and whether it is an inside corner (the ball rolls in
## the air outside the faces and material is added around it).
struct Ball:
var vertex: i64
var center: Vector3
var inside: bool
## The corners where chosen edges' fillets (of `radius`, `continuity`) meet. Two meeting
## where a third edge stays sharp meet in a miter, which their tools give
## (crossing_cylinders): nothing to add. Three meeting at a corner of three
## faces get a ball there (`balls`), or with G2 and G3 a setback corner
## (`setbacks`, setback.lucb); four to eight, all of the corner's edges, a
## ball where one touches every face (G1), else a setback corner of 2N
## sides. Anything else is refused.
## Two edges continuing each other tangentially (a chain) are no corner:
## their vertex goes in `joints`, where their tools end square. Only
## fillets (`round`) have corners to make; chamfers' come from booleans.
func fillet_corners(w: const Shape*, chosen: const usize[], radius: f64, continuity: i64, round: bool, balls: Growing[Ball]*, setbacks: Growing[Setback]*, joints: Growing[i64]*, setback: bool = false) -> !:
var seen = Growing[i64]()
defer seen.close()
var directions = Growing[usize]()
defer directions.close()
for e in chosen:
let edge = w.edges.items[e]
let ends: i64[2] = [edge.start, edge.end]
for vertex in ends:
var known = false
for v in seen.view():
if v == vertex: known = true
if known: continue
try seen.push(vertex)
# The chosen edges here, one per line (pieces of one edge are one).
directions.clear()
for other in chosen:
let o = w.edges.items[other]
if o.start != vertex and o.end != vertex: continue
var same = false
for d in directions.view():
if try same_curve(w, w.edges.items[d].carrier, other, w.tolerance): same = true
if not same: try directions.push(other)
if directions.count < 2: continue
if directions.count == 2:
let a = w.edges.items[directions.items[0]]
let b = w.edges.items[directions.items[1]]
let da = try w.leaving(directions.items[0], 0 if a.start == vertex else 1)
let db = try w.leaving(directions.items[1], 0 if b.start == vertex else 1)
if da.dot(db) < -1.0 + 1e-6:
try joints.push(vertex)
continue
if not round: continue
# All outside or all inside; mixed corners are not supported yet.
var concave_count = 0
for d in directions.view():
if not try convex(w, d): concave_count += 1
if concave_count > 0 and concave_count < (i64)directions.count:
error(invalid, "an inside-corner fillet meeting an outside one at a corner is not supported yet: fillet them one at a time")
let degree = edge_degree(w, vertex)
if directions.count == 2 and degree == 3: continue
if directions.count == 3 and degree == 3 and continuity > 1 and not setback:
# No ball is tangent to G2 and G3 fillets: Fusion's Rolling
# ball corner of them, three-sided (three_sided.lucb).
try setbacks.push(try three_sided_at(w, vertex, directions.view(), radius, concave_count > 0))
continue
if directions.count == 3 and degree == 3 and setback:
# Fusion's Setback corner type: a setback corner.
try setbacks.push(try setback_at(w, vertex, directions.view(), radius, concave_count > 0))
continue
if directions.count == 3 and degree == 3:
# Inside, the ball's planes move out into the air.
try balls.push(Ball(vertex, try ball_center(w, vertex, -radius if concave_count > 0 else radius), concave_count > 0))
continue
# Four or more, all of the corner's edges: G1, a ball when one
# touches every face there (a regular pyramid's apex); else, and
# G2 and G3, a setback corner of 2N sides (setback.lucb).
if directions.count >= 4 and (i64)directions.count == degree and directions.count <= most_setback_edges:
if continuity == 1 and not setback:
let center = ball_center(w, vertex, -radius if concave_count > 0 else radius) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
recover Vector3(1e300, 0.0, 0.0)
if center.x < 1e299:
try balls.push(Ball(vertex, center, concave_count > 0))
continue
var corner = try setback_at(w, vertex, directions.view(), radius, concave_count > 0)
try roomy_setback(w, &corner, radius, continuity)
try setbacks.push(corner)
continue
error(invalid, "fillets meeting at a corner are supported where two to eight edges meet, all of the corner's; fillet edges that do not meet, or chamfer them")
## A ball's corner tool: the corner beyond the planes through the ball's
## center square to each edge (where the edges' tools stop), reaching past
## the faces, without the ball. Subtracted (added, at an inside corner), it
## leaves the sphere patch (named `name`) that rounds the corner.
func corner_tool(w: const Shape*, ball: const Ball*, radius: f64, name: str) -> interop.Reference[CadModel]!:
let c = ball.center
let corner = w.points.items[(usize)ball.vertex]
# Beyond the plane through the center square to each edge here (its way
# from the center toward the corner), and past each face at the corner:
# into the air outside, or (an inside corner, whose tool is added) into
# the material.
var normals = Growing[Vector3]()
defer normals.close()
var offsets = Growing[f64]()
defer offsets.close()
var diagonal = Vector3()
for e in 0..<w.edges.count:
let edge = w.edges.items[e]
if edge.start != ball.vertex and edge.end != ball.vertex: continue
var d = try along_edge(w, e)
if corner.subtract(c).dot(d) < 0.0: d = d.multiply_scalar(-1.0)
diagonal = diagonal.add(d)
try normals.push(d.multiply_scalar(-1.0))
try offsets.push(-d.dot(c))
for f in 0..<w.faces.count:
var at_corner = false
for use in w.face_uses(f):
let other = w.edges.items[edge_index(use)]
if other.start == ball.vertex or other.end == ball.vertex: at_corner = true
if not at_corner: continue
let n = face_outward(w, f)
let h = n.dot(w.surfaces.items[(usize)w.faces.items[f].surface].origin)
try normals.push(n.multiply_scalar(-1.0) if ball.inside else n)
try offsets.push((-h if ball.inside else h) + radius)
if normals.count < 6: error(invalid, "a rounded corner needs three edges")
let cell = try convex_cell(normals.view(), offsets.view(), radius)
defer cell.release()
let ball_solid = try ball_at(c, try diagonal.normalized(), radius)
defer ball_solid.release()
let tool = try combine(cell.value(), ball_solid.value(), op_subtract)
defer tool.release()
# The ball's face inside the cell is the corner's.
var shape = try shape_of(tool.value())
defer shape.close()
let named = try shape.add_name(name)
for f in 0..<shape.faces.count:
let kind = shape.surfaces.items[(usize)shape.faces.items[f].surface].kind
shape.faces.items[f].name = named if kind == surface_sphere else -1
return try shape_model(&shape)
## A solid ball about `center`, its poles along `pole`.
func ball_at(center: Vector3, pole: Vector3, radius: f64) -> interop.Reference[CadModel]!:
let solid = try BrepModel.sized(radius * 4.0)
defer solid.release()
let b = solid.value()
let x = try across(pole)
let east = try b.add_vertex(center.add(x.multiply_scalar(radius)))
let equator = try b.add_circle(east, east, center, pole, x, radius)
let sphere = try b.add_surface(4, center, pole, x, radius)
let sizes: i64[1] = [1]
let north: i64[1] = [equator + 1]
_ = try b.add_face(sphere, north[..<1], sizes[..<1])
let south: i64[1] = [-(equator + 1)]
_ = try b.add_face(sphere, south[..<1], sizes[..<1])
return try CadModel.of_brep(solid)
## The hexahedron of six half-spaces n . p <= offset (outward `normals`),
## plane i opposite plane i + 3: corner bits (b0, b1, b2) where planes
## i + 3 b_i meet. `size` scales its tolerance.
func hexahedron(normals: const Vector3[], offsets: const f64[], size: f64) -> interop.Reference[CadModel]!:
let solid = try BrepModel.sized(size * 8.0)
defer solid.release()
let b = solid.value()
var three_normals: Vector3[3] = [Vector3(), Vector3(), Vector3()]
var three_offsets: f64[3] = [0.0, 0.0, 0.0]
var corners: Vector3[8] = [Vector3(), Vector3(), Vector3(), Vector3(), Vector3(), Vector3(), Vector3(), Vector3()]
for bits in 0..<(usize)8:
for i in 0..<(usize)3:
let plane = i + 3 * ((bits >> i) & 1)
three_normals[i] = normals[plane]
three_offsets[i] = offsets[plane]
corners[bits] = try least_squares_point(three_normals[..<3], three_offsets[..<3])
_ = try b.add_vertex(corners[bits])
# Edges between corners one bit apart, made once.
var keys = Growing[i64]()
defer keys.close()
var ids = Growing[i64]()
defer ids.close()
for side in 0..<(usize)6:
let i = side % 3
let bit = side // 3
let j = (i + 1) % 3
let k = (i + 2) % 3
var ring: usize[4] = [0, 0, 0, 0]
let steps: usize[8] = [0, 0, 1, 0, 1, 1, 0, 1]
for q in 0..<(usize)4:
ring[q] = (bit << i) | (steps[2 * q] << j) | (steps[2 * q + 1] << k)
# Counterclockwise about the outward normal.
let normal = corners[ring[1]].subtract(corners[ring[0]]).cross(corners[ring[2]].subtract(corners[ring[0]]))
if normal.dot(normals[side]) < 0.0:
let swap = ring[1]
ring[1] = ring[3]
ring[3] = swap
var uses: i64[4] = [0, 0, 0, 0]
for q in 0..<(usize)4:
let head = (i64)ring[q]
let tail = (i64)ring[(q + 1) % 4]
let low = head if head < tail else tail
let high = tail if head < tail else head
var id: i64 = -1
for at in 0..<keys.count:
if keys.items[at] == low * 8 + high: id = ids.items[at]
if id < 0:
id = try b.add_line(low, high)
try keys.push(low * 8 + high)
try ids.push(id)
uses[q] = (id + 1) if head == low else -(id + 1)
let plane = try b.add_surface(0, corners[ring[0]], normals[side], corners[ring[1]].subtract(corners[ring[0]]))
let sizes: i64[1] = [4]
_ = try b.add_face(plane, uses[..<4], sizes[..<1])
return try CadModel.of_brep(solid)
## How many edges meet at vertex `v`.
func edge_degree(w: const Shape*, v: i64) -> i64:
var count: i64 = 0
for edge in w.edges.view():
if edge.start == v: count += 1
if edge.end == v: count += 1
return count
## Where a ball of `radius` touches the three faces at vertex `v`: their
## planes moved in by the radius meet there.
func ball_center(w: const Shape*, v: i64, radius: f64) -> Vector3!:
var normals = Growing[Vector3]()
defer normals.close()
var offsets = Growing[f64]()
defer offsets.close()
for f in 0..<w.faces.count:
var touches = false
for use in w.face_uses(f):
let edge = w.edges.items[edge_index(use)]
if edge.start == v or edge.end == v: touches = true
if not touches: continue
let s = w.surfaces.items[(usize)w.faces.items[f].surface]
if s.kind != surface_plane: error(invalid, "fillets meeting at a corner need flat faces there")
let n = face_outward(w, f)
try normals.push(n)
try offsets.push(n.dot(s.origin) - radius)
if normals.count < 3: error(invalid, "fillets meeting at a corner need three faces there")
let center = try least_squares_point(normals.view(), offsets.view())
# More than three planes moved in meet in one point only at a
# symmetric corner.
for at in 0..<normals.count:
if math.abs(normals.items[at].dot(center) - offsets.items[at]) > w.tolerance * 100.0: error(invalid, "no ball touches every face at this corner")
return center
## The name of the corner at vertex `v`, `tag/corner[A&B&C]` (its faces'
## names, sorted), into `out`.
func corner_name(w: const Shape*, v: usize, tag: str, out: Growing[u8]*) -> !:
var names = Growing[str]()
defer names.close()
for f in 0..<w.faces.count:
for use in w.face_uses(f):
let edge = w.edges.items[edge_index(use)]
if edge.start != (i64)v and edge.end != (i64)v: continue
var known = false
for other in names.view():
if other == w.face_name(f): known = true
if not known: try names.push(w.face_name(f))
for i in 1..<names.count:
let value = names.items[i]
var j = i
while j > 0 and text_before(value, names.items[j - 1]):
names.items[j] = names.items[j - 1]
j -= 1
names.items[j] = value
try role_name(out, tag, "corner[")
for at in 0..<names.count:
if at > 0: try out.push(face_separator)
try push_text(out, names.items[at])
try out.push(93)
## The convex solid of half-spaces n . p <= offset (outward `normals`):
## its corners where three planes meet inside the rest, each plane holding
## three or more of them a face, its corners round it. `size` scales its
## tolerance.
func convex_cell(normals: const Vector3[], offsets: const f64[], size: f64) -> interop.Reference[CadModel]!:
let eps = size * 1e-9
var corners = Growing[Vector3]()
defer corners.close()
for i in 0..<normals.length:
for j in i + 1..<normals.length:
for k in j + 1..<normals.length:
let three: Vector3[3] = [normals[i], normals[j], normals[k]]
if math.abs(three[0].dot(three[1].cross(three[2]))) < 1e-9: continue
let at: f64[3] = [offsets[i], offsets[j], offsets[k]]
let p = try least_squares_point(three[..<3], at[..<3])
var within = true
for m in 0..<normals.length:
if normals[m].dot(p) > offsets[m] + eps: within = false
if not within: continue
var known = false
for q in corners.view():
if q.subtract(p).length() <= eps * 10.0: known = true
if not known: try corners.push(p)
let solid = try BrepModel.sized(size * 8.0)
defer solid.release()
let b = solid.value()
for p in corners.view(): _ = try b.add_vertex(p)
var keys = Growing[i64]()
defer keys.close()
var ids = Growing[i64]()
defer ids.close()
var ring = Growing[usize]()
defer ring.close()
var angles = Growing[f64]()
defer angles.close()
let n_corners = (i64)corners.count
for plane in 0..<normals.length:
let n = normals[plane]
ring.clear()
angles.clear()
var middle = Vector3()
for at in 0..<corners.count:
if math.abs(n.dot(corners.items[at]) - offsets[plane]) > eps * 10.0: continue
try ring.push(at)
middle = middle.add(corners.items[at])
if ring.count < 3: continue
middle = middle.multiply_scalar(1.0 / (f64)ring.count)
# Counterclockwise about the outward normal.
let x = try corners.items[ring.items[0]].subtract(middle).normalized()
let y = n.cross(x)
for at in ring.view():
let d = corners.items[at].subtract(middle)
try angles.push(math.atan2(d.dot(y), d.dot(x)))
for a in 1..<ring.count:
var k = a
while k > 0 and angles.items[k] < angles.items[k - 1]:
let ta = angles.items[k]
angles.items[k] = angles.items[k - 1]
angles.items[k - 1] = ta
let tr = ring.items[k]
ring.items[k] = ring.items[k - 1]
ring.items[k - 1] = tr
k -= 1
var uses = Growing[i64]()
defer uses.close()
for q in 0..<ring.count:
let head = (i64)ring.items[q]
let tail = (i64)ring.items[(q + 1) % ring.count]
let low = head if head < tail else tail
let high = tail if head < tail else head
var id: i64 = -1
for at in 0..<keys.count:
if keys.items[at] == low * n_corners + high: id = ids.items[at]
if id < 0:
id = try b.add_line(low, high)
try keys.push(low * n_corners + high)
try ids.push(id)
try uses.push((id + 1) if head == low else -(id + 1))
let surface = try b.add_surface(0, corners.items[ring.items[0]], n, x)
let sizes: i64[1] = [(i64)ring.count]
_ = try b.add_face(surface, uses.view(), sizes[..<1])
return try CadModel.of_brep(solid)