dymokomi/luced-3d
Source of release 0.11.7
## Surfacing Stage 7's Fill (docs/research/SURFACING-ALIAS-PLASTICITY.md §5.2,
## §6.8, §7.1), luce-cad's CadSurfaceFill: xNURBS's variational fill
## (Plasticity's xNURBS and Patch, Alias's N-sided, Rhino 9's Patch). The
## smoothest surface meeting a closed loop of boundaries (edges, curves,
## isoparms, picked in any order; one edge of a hole picks the whole hole),
## each Free, G0, G1 or G2 to the surface beside it, through guide curves
## and points (a double-click on a surface adds a point); N-sided (one
## trimmed face) or Quad (an untrimmed four-sided face); Tension, Flatness,
## the Quad's boundary flow, Explicit Control, refinement and its
## tolerances. Shared by Edit CAD's Fill step (edit_cad_build.luc) and the
## CAD network's Fill node (tool_fill_surface.luc), through blend_ops.luc.
## The cook publishes each boundary's label (its continuity, a click
## cycles it: the Continuity row's, then Free, G0, G1, G2), the picks'
## lines, Alias's Continuity Check, and the conflicts: where the
## neighbors' demands disagree, drawn on the model as xNURBS 7 draws them
## (build_labels.luc). Its name is Fill Surface: Edit Mesh has a Fill.
from luce_cad.surfacing import CadSurfaceFill, CadFill
from luce_3d.graphics import Vector3
from geometry_data import GeometryData
from parameter_rows import Parameter, number, toggle, menu
import build_ops
from luce_cad.model import CadTolerances
pub let fill_verb = "Fill Surface"
## The texts: the boundaries, then the guides (curves, and points as
## "isoparm_at x y z" from a double-click).
pub let boundaries_text = 0
pub let guides_text = 1
## The conflicts' state lines: from, to (x y z each), the order, the
## amount, 1 at a corner, the boundary.
pub let conflict_head = "build_conflict "
# -- Slots.
pub let continuity_slot = 0
pub let quad_slot = 1
pub let tension_slot = 2
pub let flatness_slot = 3
pub let flow_slot = 4
pub let guide_slot = 5
pub let explicit_slot = 6
pub let degree_u_slot = 7
pub let spans_u_slot = 8
pub let degree_v_slot = 9
pub let spans_v_slot = 10
pub let refine_slot = 11
pub let most_slot = 12
pub let position_slot = 13
pub let angle_slot = 14
pub let curvature_slot = 19
pub let labels_slot = 16
pub let check_slot = 17
pub let conflicts_slot = 18
## (Slot 15 is a node's Group Type, node_catalog.group_type_slot.)
## Each boundary's own continuity, by its place in the loop picked (0: the
## Continuity row's; 1 to 4: Free, G0, G1, G2), set by its label.
pub let boundary_slot = 20
pub let boundary_count = 16
## N-sided's domain (after the boundaries' slots): Auto, Projected, Molded,
## Quads.
pub let domain_slot = 36
pub let continuity_cycle = "Free|G0|G1|G2"
# -- Rows.
pub func rows() -> list[Parameter]:
let result = [menu("Continuity", continuity_slot, continuity_cycle, 2.0, "Each boundary's continuity to the surface beside it (xNURBS's, Alias's N-sided): Free keeps its position, G0 position, G1 tangent, G2 curvature. A boundary's label on the model sets its own (a click cycles it)."),
toggle("Quad", quad_slot, 0.0, "xNURBS's Quad sided: four boundaries make an untrimmed four-sided face (else one trimmed face over any number)."),
number("Tension", tension_slot, 0.0, 0.0, 1.0, 0.05, "Plasticity's Tension: 0 the smoothest bend, toward 1 tighter (shorter between the boundaries)."),
number("Flatness", flatness_slot, 0.0, 0.0, 1.0, 0.05, "xNURBS's Flatness: a pull toward the boundaries' best plane.")]
let domain = menu("Domain", domain_slot, "Auto|Projected|Molded|Quads", 0.0, "Alias's N-sided domain: the boundaries projected onto their best plane (Projected), laid round a polygon (Molded, for holes whose neighbors stand square to that plane: a lid on walls), or N quads round a center (Quads: fillets' and walls' corners, G2 in a second). Auto fills a ball's hole with its ball, fillets' corners with quads, projects the rest unless a neighbor stands square.")
domain.hide_when = "{ quad != 0 }"
result.append(domain)
let flow = menu("Boundary flow", flow_slot, "Natural|Normal|Next|Adjacent", 0.0, "Plasticity's Boundary (Quad): the isoparms leaving each side as they fall (Natural), square to it (Normal), along the next sides (Next) or along the neighbor's own (Adjacent).")
flow.hide_when = "{ quad == 0 }"
result.append(flow)
result.append(menu("Guide continuity", guide_slot, "G0|G1", 0.0, "Guide curves the fill passes through (G0), or tangent to the face a guide lies on (G1)."))
result.append(toggle("Explicit control", explicit_slot, 0.0, "Alias's Explicit Control: the degrees and spans as set here, no refining (else multi-span quintic, refined)."))
let dims = [("Degree U", degree_u_slot, 5.0, 2.0, 9.0), ("Spans U", spans_u_slot, 4.0, 1.0, 64.0), ("Degree V", degree_v_slot, 5.0, 2.0, 9.0), ("Spans V", spans_v_slot, 4.0, 1.0, 64.0)]
for (label, slot, initial, low, high) in dims:
let row = number(label, slot, initial, low, high, 1.0, "Explicit Control's degree or span count that way.")
row.hide_when = "{ explicit_control == 0 }"
result.append(row)
result.append(toggle("Refine", refine_slot, 1.0, "Rhino's Refine, xNURBS's Quality: spans are halved while a boundary misses its tolerance."))
let most = number("Most spans", most_slot, 32.0, 1.0, 64.0, 1.0, "The most spans refining reaches each way.")
most.hide_when = "{ refine == 0 }"
result.append(most)
let position = number("Position tolerance", position_slot, 0.0, 0.0, 1.0, 0.000001, "The gap a boundary may keep (0: a millionth of the hole's size).")
position.length = true
result.extend([position,
number("Angle tolerance", angle_slot, 0.05, 0.001, 5.0, 0.01, "The angle between normals a G1 or G2 boundary may keep, in degrees."),
number("Curvature tolerance", curvature_slot, 0.5, 0.01, 20.0, 0.1, "The normal curvatures' difference a G2 boundary may keep, in percent of the larger."),
toggle("Boundary labels", labels_slot, 1.0, "Each boundary's continuity at its middle; a click cycles it."),
toggle("Continuity check", check_slot, 1.0, "Alias's Continuity Check: the fill's edges against the surfaces beside them, drawn as the Check node's."),
toggle("Show conflicts", conflicts_slot, 1.0, "Where the neighbors' demands disagree (xNURBS 7): the corners and stretches no surface meets, drawn red with their amounts.")])
for k in 0..<boundary_count:
let own = menu(f"Boundary {k + 1}", boundary_slot + k, "Continuity row|Free|G0|G1|G2", 0.0, "This boundary's own continuity (its label sets it).")
own.hide_when = "{ quad >= 0 }"
result.append(own)
return result
## A new Fill's values: its rows' starting values.
pub func starting_values() -> list[float]:
return build_ops.starting_of(rows())
## What Fill waits for ("" when it has what it needs).
pub func waits_for(t: list[str]) -> str:
if build_ops.items(t[boundaries_text]).length == 0:
return "Pick the hole's boundaries (edges, curves or isoparms, any order), or one edge of a hole, then Enter"
return ""
## Boundary `k`'s continuity code (0 Free to 3 G2) by values `v`.
pub func code_of(v: list[float], k: int) -> int:
if k < boundary_count and boundary_slot + k < v.length and v[boundary_slot + k] > 0.0:
return int(v[boundary_slot + k]) - 1
return int(v[continuity_slot])
# -- The cook.
## Fill on `data` by values `v` and texts `t`; `name` names the sheet.
pub func build(data: GeometryData, v: list[float], t: list[str], name: str, tolerances: CadTolerances) -> GeometryData!:
let picked = build_ops.items(t[boundaries_text])
let guide_items = build_ops.items(t[guides_text] if t.length > guides_text else "")
let waiting = waits_for(t)
if waiting != "":
return labelled(data.passed(waiting), data, v, picked, guide_items, none)
var words = build_ops.resolve_isoparms(data, picked, false)
if words.length == 1 and words[0].starts_with("edge "):
# One edge of a hole: the whole hole (Plasticity's and Alias's).
words = CadSurfaceFill.hole(data.geometry, words[0]).split(";")
let codes: list[int] = []
for k in 0..<words.length:
codes.append(code_of(v, k))
let points: list[Vector3] = []
let guides: list[str] = []
for g in guide_items:
let parts = g.split(" ")
if parts.length == 4 and parts[0] == "isoparm_at":
points.append(Vector3(float(parts[1]), float(parts[2]), float(parts[3])))
else:
guides.append(g)
let made = CadSurfaceFill.fill(data.geometry, words.join(";"), codes, points, guides = guides.join(";"), guide_order = int(v[guide_slot]), quad = v[quad_slot] != 0.0,
tension = v[tension_slot], flatness = v[flatness_slot], flow = int(v[flow_slot]), explicit = v[explicit_slot] != 0.0,
degree_u = int(v[degree_u_slot]), spans_u = int(v[spans_u_slot]), degree_v = int(v[degree_v_slot]), spans_v = int(v[spans_v_slot]),
refine = v[refine_slot] != 0.0, most_spans = int(v[most_slot]), position = v[position_slot], angle = v[angle_slot], curvature = v[curvature_slot], name = name,
domain = int(v[domain_slot]) if domain_slot < v.length else 0, tolerances = tolerances)
let result = GeometryData(geometry = made.geometry())
var worst = 0
for code in codes:
if code > worst:
worst = code
# Auto molds a four-sided hole whose neighbors stand square onto the
# square: Quad, untrimmed.
var kind = ("Quad" if v[quad_slot] != 0.0 else "Quad (molded)") if not made.trimmed else f"{words.length}-sided" + (" molded" if made.molded else "")
if made.quads > 0:
kind = f"{words.length}-sided by {made.quads} quads"
let found: list[FillConflictLine] = []
for k in 0..<made.conflict_count():
let c = made.conflict(k)
found.append(FillConflictLine(c.start, c.finish, c.corner, c.boundary, c.order, c.amount))
let marks = marks_of(found)
# The boundaries that hold what they ask away from the conflicting
# corners (the fill's own measures at its stations).
var met = 0
var asked = 0
for k in 0..<words.length:
let code = codes[k]
if code < 2:
continue
asked += 1
if made.angle(k) <= v[angle_slot] and (code < 3 or made.curvature(k) <= v[curvature_slot]):
met += 1
result.report = f"{kind} {continuity_cycle.split("|")[worst]}, {made.spans_u} by {made.spans_v} spans of degree {made.degree_u}" + (f", {met} of {asked} boundaries met" if asked > 0 else "") + (f", {marks.length} conflicts" if marks.length > 0 else "")
if marks.length > 0:
result.warning = conflict_warning(marks)
if v[check_slot] != 0.0 and worst >= 1:
let eased: list[Vector3] = []
let reach: list[float] = []
for k in 0..<made.conflict_count():
let c = made.conflict(k)
if c.corner:
eased.append(c.start)
reach.append(c.reach)
build_ops.checked(result, name, words, worst - 1 if worst >= 2 else 0, eased = eased, reach = reach)
return labelled(result, data, v, words, guide_items, made)
## `result` with each boundary's label (when Boundary labels is on), the
## conflicts (when Show conflicts is), and the picks' lines as state lines.
func labelled(result: GeometryData, data: GeometryData, v: list[float], boundaries: list[str], guides: list[str], made: CadFill?) -> GeometryData!:
let lines: list[str] = []
if v[labels_slot] != 0.0:
for k in 0..<(boundaries.length if boundaries.length < boundary_count else boundary_count):
if boundaries[k].starts_with("isoparm_at"):
continue
let own = v[boundary_slot + k] > 0.0
let text = continuity_cycle.split("|")[code_of(v, k)] + ("" if own else " (all)")
let at = build_ops.middle_of(data, boundaries[k]) catch failure:
continue
lines.append(build_ops.label_line(at, boundary_slot + k, 5, text))
if let fill = made:
if v[conflicts_slot] != 0.0:
for k in 0..<fill.conflict_count():
let c = fill.conflict(k)
lines.append(f"{conflict_head}{c.start.x} {c.start.y} {c.start.z} {c.finish.x} {c.finish.y} {c.finish.z} {c.order} {c.amount} {1 if c.corner else 0} {c.boundary}")
lines.extend(build_ops.pick_lines(data, boundaries + guides))
if lines.length > 0:
result.sketch_state = (result.sketch_state + "\n" if result.sketch_state != "" else "") + lines.join("\n")
return result
## A conflict read back: from, to, whether a corner, its boundary and
## order, its amount.
pub class FillConflictLine:
pub let start: Vector3
pub let finish: Vector3
pub let corner: bool
pub let boundary: int
pub let order: int
pub let amount: float
pub func init(self, start: Vector3, finish: Vector3, corner: bool, boundary: int, order: int, amount: float):
self.start = start
self.finish = finish
self.corner = corner
self.boundary = boundary
self.order = order
self.amount = amount
## The conflicts a cook's state holds.
pub func conflicts_of(state: str) -> list[FillConflictLine]:
let found: list[FillConflictLine] = []
for line in state.split("\n"):
if not line.starts_with(conflict_head):
continue
let words = line[conflict_head.length..].split(" ")
if words.length < 10:
continue
let numbers: list[float] = []
for k in 0..<10:
let value = float(words[k]) catch failure:
recover 0.0
numbers.append(value)
found.append(FillConflictLine(Vector3(numbers[0], numbers[1], numbers[2]), Vector3(numbers[3], numbers[4], numbers[5]), numbers[8] != 0.0, int(numbers[9]), int(numbers[6]),
numbers[7]))
return found
## Conflicts gathered as xNURBS 7 marks them, one mark a place: a corner
## (every conflict there, its worst amount), or a boundary's runs of one
## order (each run drawn, one label with the worst and how many). `anchor`
## is where its label points: the corner, or the worst run's middle.
pub class FillConflictMark:
pub var anchor: Vector3
pub let corner: bool
pub let order: int
pub var amount: float
pub let runs: list[(Vector3, Vector3)] = []
pub func init(self, anchor: Vector3, corner: bool, order: int, amount: float):
self.anchor = anchor
self.corner = corner
self.order = order
self.amount = amount
## The label: the order, what disagrees (a corner, or how many runs) and
## the worst amount in its unit (a gap in the document's units is
## left to the caller: model units here).
pub func text(self) -> str:
let orders = ["G0", "G1", "G2"]
let sign = "°" if self.order == 1 else ("%" if self.order == 2 else "")
let what = "corner" if self.corner else (f"{self.runs.length} runs" if self.runs.length > 1 else "run")
return f"{orders[self.order]} {what} {self.amount:.3g}{sign}"
## `conflicts` gathered into marks: corners at one point merged, each
## boundary's runs of one order merged.
pub func marks_of(conflicts: list[FillConflictLine]) -> list[FillConflictMark]:
let marks: list[FillConflictMark] = []
let keys: list[str] = []
for c in conflicts:
if c.corner:
var merged = false
for m in marks:
if m.corner and m.anchor.subtract(c.start).length() < 1e-9:
if c.order > m.order or c.order == m.order and c.amount > m.amount:
m.amount = c.amount
merged = true
if not merged:
marks.append(FillConflictMark(c.start, true, c.order, c.amount))
keys.append("")
continue
let key = f"{c.boundary} {c.order}"
var mark: FillConflictMark? = none
for k in 0..<marks.length:
if keys[k] == key:
mark = marks[k]
let middle = c.start.add(c.finish).multiply_scalar(0.5)
if let m = mark:
m.runs.append((c.start, c.finish))
if c.amount > m.amount:
# The label points at the worst run.
m.amount = c.amount
m.anchor = middle
else:
let made = FillConflictMark(middle, false, c.order, c.amount)
made.runs.append((c.start, c.finish))
marks.append(made)
keys.append(key)
return marks
## The warning for `marks`: how many corners and boundaries disagree.
pub func conflict_warning(marks: list[FillConflictMark]) -> str:
var corners = 0
var runs = 0
for m in marks:
if m.corner:
corners += 1
else:
runs += 1
let parts: list[str] = []
if corners > 0:
parts.append(f"{corners} corner" + ("s" if corners > 1 else ""))
if runs > 0:
parts.append(f"{runs} stretch" + ("es" if runs > 1 else "") + " of the boundaries")
return "Fill: the boundaries' demands disagree at " + parts.join(" and ") + " (drawn red)"