dymokomi/luce-cad
Source of release 0.9.4
0.9.4 / src / modeling / cap_result.lucb
## A cap loop's blend written out (cap_blend.lucb): the body's faces with
## their loops on the contacts, the strips, the run-outs' pieces and the
## flat ends' faces.
## The result: the body's faces, the cap's loops on its contacts (and an
## open end's line to its run-out point or corner), each wall's edge on its
## contact, its seams shortened or split and the edges going on cut (an end
## wall's corner closed by the section), then
## the strips (running on over a run-out on their own surface), the
## run-outs' own pieces and the flat ends' faces; turned to agree and
## checked closed (local_blend_result.lucb's orient).
func cap_result(b: Cap*, tag: str) -> interop.Reference[CadModel]!:
let w = b.w
var uses = Growing[i64]()
defer uses.close()
var sizes = Growing[i64]()
defer sizes.close()
for f in 0..<w.faces.count:
uses.clear()
sizes.clear()
let face_uses = w.face_uses(f)
var at: usize = 0
for size in w.face_sizes(f):
let before = uses.count
for use in face_uses[at..<at + (usize)size]: try cap_use(b, f, use, &uses)
try sizes.push((i64)(uses.count - before))
at += (usize)size
let face = w.faces.items[f]
try b.out.add_face(face.surface, face.sense, uses.view(), sizes.view(), try b.out.add_name(w.face_name(f)))
let fixed = b.out.faces.count
# A run-out past a strip on a plane or cylinder (a line's) runs on in
# it; any other gets its own piece.
for r in 0..<b.runouts.count:
let kind = b.out.surfaces.items[(usize)b.sides.items[b.runouts.items[r].side].surface].kind
if kind != surface_plane and kind != surface_cylinder: try runout_piece(b, &b.runouts.items[r])
for side in b.sides.view():
let edge = w.edges.items[side.edge]
# Against the cap's way along its contact, from that contact; a
# merged run-out's line and trim in place of its section.
uses.clear()
if try cap_runs_forward(w, side.cap, side.edge):
try uses.push(-(side.outer + 1))
try strip_end(b, edge.start, true, &uses)
try uses.push(side.inner + 1)
try strip_end(b, edge.end, false, &uses)
else:
try uses.push(side.outer + 1)
try strip_end(b, edge.end, true, &uses)
try uses.push(-(side.inner + 1))
try strip_end(b, edge.start, false, &uses)
sizes.clear()
try sizes.push((i64)uses.count)
try blend_name(w, side.edge, tag, &b.name)
try b.out.add_face(side.surface, side.sense, uses.view(), sizes.view(), try b.out.add_name((str)b.name.view()))
# Each run-out's own piece, between its section, trim and line.
for r in b.runouts.view():
if r.surface < 0: continue
let v = (usize)r.vertex
let loop: i64[3] = [b.sections.items[v] + 1, -(r.trim + 1), -(r.line + 1)]
let size: i64[1] = [3]
try role_name(&b.name, tag, "run[")
try edge_name(w, (usize)r.goes_on, &b.name)
try b.name.push(93)
try b.out.add_face(r.surface, r.sense, loop[..<3], size[..<1], try b.out.add_name((str)b.name.view()))
# Each flat end's face.
for v in 0..<w.points.count:
if flat_end(b, v): try end_face(b, v, tag)
try orient(&b.out, fixed)
let solid = try used_brep(&b.out)
defer solid.release()
return try CadModel.of_brep(solid)
## A strip's way across its end at vertex `v` onto `out`: from its cap
## contact to its wall contact (`down`) or back; its section, or a merged
## run-out's line to the run-out point and trim from there.
func strip_end(b: const Cap*, v: i64, down: bool, out: Growing[i64]*) -> !:
let r = b.runout_of.items[(usize)v]
if r < 0 or b.runouts.items[(usize)r].surface >= 0:
let section = b.sections.items[(usize)v]
try out.push((section + 1) if down else -(section + 1))
return
let run = b.runouts.items[(usize)r]
if down:
try out.push(run.line + 1)
try out.push(run.trim + 1)
else:
try out.push(-(run.trim + 1))
try out.push(-(run.line + 1))
## Whether face `cap` of `w` uses edge `e` from its start to its end.
func cap_runs_forward(w: const Shape*, cap: usize, e: usize) -> bool!:
for use in w.face_uses(cap):
if edge_index(use) == e: return use > 0
error(invalid, "a blended edge is not on its cap")
## Body face `f`'s loop use `use` as the result has it, onto `out`: a
## picked edge by its contact on that face (with an open end's line to
## the corner on the cap), a seam shortened (with its upper part on the
## wall going on past an open end), else the edge itself.
func cap_use(b: Cap*, f: usize, use: i64, out: Growing[i64]*) -> !:
let w = b.w
let e = edge_index(use)
let forward = use > 0
let edge = w.edges.items[e]
let k = b.side_of.items[e]
if k >= 0:
let side = b.sides.items[(usize)k]
let id = side.outer if side.cap == f else side.inner
let head = (usize)(edge.start if forward else edge.end)
let tail = (usize)(edge.end if forward else edge.start)
# On the cap, an open end's line: from the corner to the contact
# before, from the contact to the corner after (none at a corner
# end: the cut edge going on starts at the contact).
if side.cap == f and b.open.items[head] and not b.cornered.items[head]: try out.push(-(end_line(b, head) + 1))
try out.push((id + 1) if forward else -(id + 1))
if side.cap == f and b.open.items[tail] and not b.cornered.items[tail]: try out.push(end_line(b, tail) + 1)
return
# The edge going on from a corner end, cut at its cap contact; on the
# end wall the section closes the corner: from the seam's wall contact
# up to the cut, and from the cut down to the next seam's.
let cut = b.corner_cuts.items[e]
if cut >= 0:
let first = (usize)(edge.start if forward else edge.end)
let last = (usize)(edge.end if forward else edge.start)
let end_wall = not cap_of_corner(b, first, f) and not cap_of_corner(b, last, f)
if end_wall and b.cornered.items[first]: try out.push(-(b.sections.items[first] + 1))
try out.push((cut + 1) if forward else -(cut + 1))
if end_wall and b.cornered.items[last]: try out.push(b.sections.items[last] + 1)
return
# An edge a run-out cuts: on the wall going on, with the trims up
# from the run-out points to the wall contacts.
var cut_by: i64 = -1
var on_wall = false
for r in b.runouts.view():
if r.goes_on != (i64)e: continue
cut_by = r.cut
on_wall = b.sides.items[r.side].cap != f
if cut_by >= 0:
let first = (usize)(edge.start if forward else edge.end)
let last = (usize)(edge.end if forward else edge.start)
if on_wall and b.runout_of.items[first] >= 0: try out.push(-(b.runouts.items[(usize)b.runout_of.items[first]].trim + 1))
try out.push((cut_by + 1) if forward else -(cut_by + 1))
if on_wall and b.runout_of.items[last] >= 0: try out.push(b.runouts.items[(usize)b.runout_of.items[last]].trim + 1)
return
let seam = b.seams.items[e]
if seam < 0:
try out.push(use)
return
let top_part = b.uppers.items[e]
if top_part < 0:
try out.push((seam + 1) if forward else -(seam + 1))
return
# Split at an open end `v`: the blended edge's wall keeps the lower
# part; the wall going on keeps both, in the seam's own order (the
# upper part first when the seam starts at `v`).
let v = edge.start if b.open.items[(usize)edge.start] else edge.end
for side in b.sides.view():
let picked = w.edges.items[side.edge]
if side.wall == f and (picked.start == v or picked.end == v):
try out.push((seam + 1) if forward else -(seam + 1))
return
let upper_first = edge.start == v
let first = top_part if upper_first else seam
let second = seam if upper_first else top_part
if forward:
try out.push(first + 1)
try out.push(second + 1)
else:
try out.push(-(second + 1))
try out.push(-(first + 1))
## An open end's line on the cap from its contact: to its run-out point,
## or to the corner (a flat end).
func end_line(b: const Cap*, v: usize) -> i64:
let r = b.runout_of.items[v]
return b.runouts.items[(usize)r].line if r >= 0 else b.end_lines.items[v]
## Open end `v`'s flat face, in its section's plane: from its cap contact
## along its section to its wall contact, up the seam's upper part to the
## corner, back along the line on the cap. It faces the blend: away from
## the corner going on (into the blend's air on a convex edge).
func end_face(b: Cap*, v: usize, tag: str) -> !:
let w = b.w
var top_part: i64 = -1
for g in 0..<w.edges.count:
if b.uppers.items[g] < 0: continue
let seam = w.edges.items[g]
if seam.start == (i64)v or seam.end == (i64)v: top_part = b.uppers.items[g]
if top_part < 0: error(invalid, "an open end's seam was not split")
# From the wall contact up to the corner.
let upward = b.out.edges.items[(usize)top_part].end == (i64)v
let loop: i64[3] = [b.sections.items[v] + 1, (top_part + 1) if upward else -(top_part + 1), -(b.end_lines.items[v] + 1)]
let size: i64[1] = [3]
let head = b.at(b.on_cap.items[v])
let corner = w.points.items[v]
let tail = b.at(b.on_wall.items[v])
let normal = try head.subtract(corner).cross(tail.subtract(corner)).normalized()
# Outward: along the blended edge from the corner (convex), else back.
var outward = Vector3()
for side in b.sides.view():
let edge = w.edges.items[side.edge]
if edge.start == (i64)v or edge.end == (i64)v:
outward = (try w.leaving(side.edge, 0 if edge.start == (i64)v else 1)).multiply_scalar(1.0 if side.convex else -1.0)
var empty_points: Vector3[]
var empty_numbers: f64[]
try b.out.surfaces.push(try Surface(0, corner, try head.subtract(corner).normalized(), normal, 0.0, 0.0, empty_points, empty_numbers, empty_numbers, empty_numbers, 0, 0, 0, 0))
# Named by the edge going on: `tag/end[A&B]`.
try role_name(&b.name, tag, "end[")
try edge_name(w, (usize)b.continues.items[v], &b.name)
try b.name.push(93)
try b.out.add_face((i64)b.out.surfaces.count - 1, normal.dot(outward) > 0.0, loop[..<3], size[..<1], try b.out.add_name((str)b.name.view()))
## Whether face `f` is the cap of the side ending at corner end `v` (false
## when `v` is not one).
func cap_of_corner(b: const Cap*, v: usize, f: usize) -> bool:
if not b.cornered.items[v]: return false
for side in b.sides.view():
let edge = b.w.edges.items[side.edge]
if edge.start == (i64)v or edge.end == (i64)v: return side.cap == f
return false