dymokomi/luce-cad
Source of release 0.9.4
0.9.4 / src / surfacing / api.lucb
## Luce's surfacing calls (Stage 3: CadSurfacing). Each takes a geometry
## set and returns it changed, its bodies named by index (as
## CadGeometry.model) and faces by name; curves on faces are the set's
## CadCurves. A trim pushes the face as it was onto the set's trim stages
## (cad_trims.lucb), which Untrim pops.
## What a surfacing edit made: the set, and what it measured (Trim
## Convert's deviations, Stitch's joins and widest gap, Project's fit).
pub struct CadSurfaceEdit:
var made: interop.Reference[GeometrySet]?
pub var deviation: f64
pub var edge_deviation: f64
pub var count: i64
pub var widest: f64
pub var closed: bool
## Stitch's Detach options: periodic faces split in two, closed edges
## split in two.
pub var periodic: i64
pub var single: i64
## Square: the side of the patch each picked boundary became (0 u = 0,
## 1 u = 1, 2 v = 0, 3 v = 1).
var sides: Growing[i64]
pub func init():
self.made = none
self.deviation = 0.0
self.edge_deviation = 0.0
self.count = 0
self.widest = 0.0
self.closed = false
self.periodic = 0
self.single = 0
self.sides = Growing[i64]()
pub func geometry() -> interop.Reference[GeometrySet]!:
let made = self.made else error(invalid, "a surfacing edit has no geometry")
return made.clone()
## Square: picked boundary `index`'s side (-1 none).
pub func side(index: i64) -> i64:
if index < 0 or (usize)index >= self.sides.count: return -1
return self.sides.items[(usize)index]
pub func close():
if let made = self.made: made.release()
self.made = none
self.sides.close()
pub let cad_surface_edit_type: interop.Type[CadSurfaceEdit] = interop.Type[CadSurfaceEdit]("CadSurfaceEdit", CadSurfaceEdit.close, closeable = true)
func edit_of(set: interop.Reference[GeometrySet]) -> interop.Reference[CadSurfaceEdit]!:
let result = interop.Reference[CadSurfaceEdit].adopt(new CadSurfaceEdit() in memory.heap, cad_surface_edit_type, memory.heap) catch failure:
set.release()
error(failure.code, failure.message)
result.value().made = set
return result
## A face cut by its curves (Trim's region markers and Show Intersections):
## a marker inside each region, where the curves cross the boundary and
## each other, and the loose ends that cut nothing.
pub struct CadTrimRegions:
var cut: TrimCut
## The curves cutting the face as the trim takes them (3D Trimming's
## cast curves among them), each sampled at cut_samples points.
var cuts: Growing[Vector3]
pub func init():
self.cut = TrimCut(Growing[Vector3](), Growing[Vector3](), Growing[Vector3]())
self.cuts = Growing[Vector3]()
## How many curves cut the face.
pub func cut_count() -> i64:
return (i64)self.cuts.count // cut_samples
## Point `index` (0 to cut_samples - 1) along cutting curve `cut`.
pub func cut_point(cut: i64, index: i64) -> Vector3!:
if cut < 0 or index < 0 or index >= cut_samples or cut >= self.cut_count(): error(invalid, "cutting curve point out of range")
return self.cuts.items[(usize)(cut * cut_samples + index)]
pub func marker_count() -> i64:
return (i64)self.cut.markers.count
pub func marker(index: i64) -> Vector3!:
if index < 0 or (usize)index >= self.cut.markers.count: error(invalid, "region marker out of range")
return self.cut.markers.items[(usize)index]
pub func loose_count() -> i64:
return (i64)self.cut.loose.count
pub func loose(index: i64) -> Vector3!:
if index < 0 or (usize)index >= self.cut.loose.count: error(invalid, "loose end out of range")
return self.cut.loose.items[(usize)index]
pub func crossing_count() -> i64:
return (i64)self.cut.crossings.count
pub func crossing(index: i64) -> Vector3!:
if index < 0 or (usize)index >= self.cut.crossings.count: error(invalid, "crossing out of range")
return self.cut.crossings.items[(usize)index]
pub func close():
self.cut.close()
self.cuts.close()
## The points a cutting curve is drawn by (CadTrimRegions.cut_point).
pub let cut_samples: i64 = 65
pub let cad_trim_regions_type: interop.Type[CadTrimRegions] = interop.Type[CadTrimRegions]("CadTrimRegions", CadTrimRegions.close, closeable = true)
## What an Align made (CadSurfacing.aligned): the set, Alias's Diagnostic
## Feedback (the smallest and largest gap, angle in degrees, curvature and
## G3 differences in percent along the edge, and the start and end angles
## against the master's own cross direction), whether every station met
## the limits (else Alias's dashed edge), the side aligned and its order,
## and the aligned rows' control points for the viewport (rows of
## row_size, row 0 on the edge).
pub struct CadAlignEdit:
var made: interop.Reference[GeometrySet]?
pub var gap_low: f64
pub var gap_high: f64
pub var angle_low: f64
pub var angle_high: f64
pub var curvature_low: f64
pub var curvature_high: f64
pub var rate_low: f64
pub var rate_high: f64
pub var start_angle: f64
pub var end_angle: f64
pub var met: bool
pub var side: i64
pub var order: i64
pub var row_size: i64
var rows: Growing[Vector3]
var proxy: Growing[Vector3]
## The input's hull before the align: rows of proxy_size.
pub var proxy_size: i64
pub func init():
self.made = none
self.gap_low = 0.0
self.gap_high = 0.0
self.angle_low = 0.0
self.angle_high = 0.0
self.curvature_low = 0.0
self.curvature_high = 0.0
self.rate_low = 0.0
self.rate_high = 0.0
self.start_angle = 0.0
self.end_angle = 0.0
self.met = true
self.side = 0
self.order = 0
self.row_size = 0
self.rows = Growing[Vector3]()
self.proxy = Growing[Vector3]()
self.proxy_size = 0
pub func geometry() -> interop.Reference[GeometrySet]!:
let made = self.made else error(invalid, "an align has no geometry")
return made.clone()
## How many rows it moved (the order's and the extra ones).
pub func row_count() -> i64:
if self.row_size == 0: return 0
return (i64)self.rows.count // self.row_size
## Control point `index` of row `row`.
pub func row_point(row: i64, index: i64) -> Vector3!:
if row < 0 or index < 0 or index >= self.row_size or row >= self.row_count(): error(invalid, "align row point out of range")
return self.rows.items[(usize)(row * self.row_size + index)]
## How many rows the hull before the align has (Alias's proxy display).
pub func proxy_row_count() -> i64:
if self.proxy_size == 0: return 0
return (i64)self.proxy.count // self.proxy_size
## Control point `index` of the hull's row `row` before the align, row
## 0 on the edge.
pub func proxy_point(row: i64, index: i64) -> Vector3!:
if row < 0 or index < 0 or index >= self.proxy_size or row >= self.proxy_row_count(): error(invalid, "align proxy point out of range")
return self.proxy.items[(usize)(row * self.proxy_size + index)]
pub func close():
if let made = self.made: made.release()
self.made = none
self.rows.close()
self.proxy.close()
pub let cad_align_edit_type: interop.Type[CadAlignEdit] = interop.Type[CadAlignEdit]("CadAlignEdit", CadAlignEdit.close, closeable = true)
## Body `body` of `set`.
func body_of(set: interop.Reference[GeometrySet], body: i64) -> const CadModel*!:
let models = set_models(try set.get())
if body < 0 or (usize)body >= models.length: error(invalid, "the body is no longer in the model: pick it again")
return models[(usize)body].value()
## `set` with body `body` replaced by `models` (none: removed).
func with_body(set: interop.Reference[GeometrySet], body: i64, models: const interop.Reference[CadModel][]) -> interop.Reference[GeometrySet]!:
let source = try set.get()
let existing = set_models(source)
var list = Growing[interop.Reference[CadModel]]()
defer list.close()
for at in 0..<existing.length:
if (i64)at == body:
for m in models: try list.push(m)
else:
try list.push(existing[at])
return try with_models(source, list.view())
## The tolerance curves are fitted to on body `model`: a ten-millionth of
## its size, but not below its own (BrepModel's, a tolerant edge's where
## wider). Finer than the project's fit tolerance: a G3 face's trims are
## measured for rate continuity along them.
func fit_tolerance(model: const CadModel*) -> f64!:
var w = try shape_of(model)
defer w.close()
var low = Vector3(1.0e300, 1.0e300, 1.0e300)
var high = Vector3(-1.0e300, -1.0e300, -1.0e300)
for p in w.points.view():
low = Vector3(math.min(low.x, p.x), math.min(low.y, p.y), math.min(low.z, p.z))
high = Vector3(math.max(high.x, p.x), math.max(high.y, p.y), math.max(high.z, p.z))
let b = model.brep() else error(invalid, "fitting a curve needs a B-rep body")
return math.max(b.model_tolerance(), high.subtract(low).length() * 1e-7)
## The curves Project casts: the set's curves `names` names (apart by `;`;
## empty: every curve), and among them temporary isoparms ("isoparm <face>
## <0 or 1> <fraction>", Alias's temporary pick objects), onto `out`.
func projection_curves(set: interop.Reference[GeometrySet], names: str, out: Growing[Spline]*) -> !:
var plain = Growing[u8]()
defer plain.close()
let bytes = names.bytes
var start: usize = 0
for at in 0..<bytes.length + 1:
if at < bytes.length and bytes[at] != 59: continue
let item = (str)bytes[start..<at]
start = at + 1
if item.length > 8 and (str)item.bytes[0..<8] == "isoparm ":
var m = try master_of(set, item, -1, Vector3())
defer m.close()
try out.push(try m.curve.copy())
continue
if item.length == 0: continue
if plain.count > 0: try plain.push(59)
for b in item.bytes: try plain.push(b)
if out.count > 0 and plain.count == 0: return
var chosen = Growing[i64]()
defer chosen.close()
try chosen_curves(set, (str)plain.view(), "", &chosen)
for index in chosen.view():
let curve = try CadCurves.curve(set, index)
defer curve.release()
try out.push(try spline_in(curve.value()).copy())
## The set's curves `names` (apart by `;`; empty: every curve, or with
## `face` every curve on that face) by index.
func chosen_curves(set: interop.Reference[GeometrySet], names: str, face: str, out: Growing[i64]*) -> !:
let count = try CadCurves.count(set)
if names.length > 0:
var start: usize = 0
let bytes = names.bytes
for at in 0..<bytes.length + 1:
if at < bytes.length and bytes[at] != 59: continue
let name = (str)bytes[start..<at]
start = at + 1
if name.length == 0: continue
let index = try CadCurves.find(set, name)
if index < 0: error(invalid, "a curve to use is no longer in the model: pick it again")
try out.push(index)
return
for index in 0..<count:
if face.length > 0:
let on = try CadCurves.face(set, index)
defer on.release()
if on.value != face: continue
try out.push(index)
## Surfacing calls, as static functions.
pub struct CadSurfacing:
let reserved: u8 = 0
## Project (project.lucb): the curves `curves` of `set` (names apart by
## `;`) cast onto face `face` of body `body` along `direction` (both
## ways with `both`) or along the face's normals (`normal`), each piece
## a curve on the face named `stem` and a number, fitted within
## `tolerance` (0: a ten-millionth of the body) or, with `original`, on
## each curve's own degree and spans. `count` says how many pieces,
## `deviation` the worst fit; none at all is refused.
pub static func projected(set: interop.Reference[GeometrySet], body: i64, face: str, curves: str, direction: Vector3, normal: bool = false, both: bool = true,
original: bool = false, tolerance: f64 = 0.0, stem: str = "Projected") -> interop.Reference[CadSurfaceEdit]!:
let model = try body_of(set, body)
let fit = tolerance if tolerance > 0.0 else try fit_tolerance(model)
var picked = Growing[Spline]()
defer close_splines(&picked)
try projection_curves(set, curves, &picked)
if picked.count == 0: error(invalid, "Project: pick the curves to project")
var result = set.clone()
var made: i64 = 0
var worst = 0.0
for c in picked.view():
var charts = Growing[Spline]()
defer close_splines(&charts)
var lifted = Growing[Spline]()
defer close_splines(&lifted)
let cast = Casting(direction, normal, both)
worst = math.max(worst, try project_curve(model, face, &c, cast, fit, original, &charts, &lifted))
for k in 0..<charts.count:
let next = try add_on_face(result, stem, face, &lifted.items[k], &charts.items[k], "project")
result.release()
result = next
made += 1
if made == 0:
result.release()
error(invalid, "Project: the curves miss the face (along this direction)")
let edit = try edit_of(result)
edit.value().count = made
edit.value().deviation = worst
return edit
## Intersect: where face `face` of body `body` meets face `other_face`
## of body `other`, curves on the first face (and with `both` on the
## second too), named `stem` and a number.
pub static func intersected(set: interop.Reference[GeometrySet], body: i64, face: str, other: i64, other_face: str, both: bool = false, tolerance: f64 = 0.0,
stem: str = "Intersect") -> interop.Reference[CadSurfaceEdit]!:
let a = try body_of(set, body)
let b = try body_of(set, other)
var meets = try face_meets(a, face, b, other_face)
defer meets.close()
return try meeting_curves(set, &meets, a, face, b if both else none, other_face, tolerance, stem)
## Intersect with a plane: where face `face` of body `body` meets the
## plane through `origin` square to `normal`, as curves on the face.
pub static func intersected_plane(set: interop.Reference[GeometrySet], body: i64, face: str, origin: Vector3, normal: Vector3, tolerance: f64 = 0.0,
stem: str = "Intersect") -> interop.Reference[CadSurfaceEdit]!:
let a = try body_of(set, body)
let sheet = try plane_sheet(a, origin, normal)
defer sheet.release()
var meets = try face_meets(a, face, sheet.value(), "plane")
defer meets.close()
return try meeting_curves(set, &meets, a, face, none, "", tolerance, stem)
## Face `face` of body `body` cut by its curves (`curves`, apart by `;`;
## empty: every curve on it): the regions' markers, the crossings and
## the loose ends (Trim's region markers, Show Intersections).
## With Alias's 3D Trimming, `cast` (trim_curves.lucb's: 1 along
## `direction`, 2 along the normals) casts the curves off the face onto
## it as the trim runs, and `cutters` (face names apart by `;`, of any
## body) cut it where they meet it; `extend` carries curve ends that
## stop short on to the boundary or the next curve.
pub static func trim_regions(set: interop.Reference[GeometrySet], body: i64, face: str, curves: str = "", cast: i64 = 0, direction: Vector3 = Vector3(),
cutters: str = "", extend: bool = false) -> interop.Reference[CadTrimRegions]!:
let model = try body_of(set, body)
let how = TrimCurves(cast, direction, cutters, extend)
var cuts = try cutting_shape(set, model, face, curves, &how)
defer cuts.close()
let cut = try trim_regions(model, face, &cuts)
let result = interop.Reference[CadTrimRegions].adopt(new CadTrimRegions() in memory.heap, cad_trim_regions_type, memory.heap) catch failure:
var owned = cut
owned.close()
error(failure.code, failure.message)
result.value().cut.close()
result.value().cut = cut
# The cutting curves, for the viewport to draw while it waits.
for carrier in cuts.carriers.view():
if carrier.kind != curve_spline: continue
let (points, weights, knots, degree) = cuts.spline_net(carrier.spline)
var c = try spline_of(points, weights, degree, knots)
defer c.close()
for k in 0..<(usize)cut_samples: try result.value().cuts.push(c.point_at(c.low() + (c.high() - c.low()) * (f64)k / (f64)(cut_samples - 1)))
return result
## Trim: face `face` of body `body` cut by its curves (as trim_regions),
## the regions holding `picks` kept (`mode` 0), discarded (1), or every
## region its own face (2, Divide); the face before it kept as a trim
## stage. `tag` names Divide's new faces.
## `cast`, `direction`, `cutters` and `extend` as trim_regions's.
pub static func trimmed(set: interop.Reference[GeometrySet], body: i64, face: str, picks: const Vector3[], mode: i64 = 0, curves: str = "", tag: str = "trim",
cast: i64 = 0, direction: Vector3 = Vector3(), cutters: str = "", extend: bool = false) -> interop.Reference[GeometrySet]!:
let model = try body_of(set, body)
let how = TrimCurves(cast, direction, cutters, extend)
var cuts = try cutting_shape(set, model, face, curves, &how)
defer cuts.close()
let precision = try fit_tolerance(model)
let made = try trim_face(model, face, &cuts, picks, mode, tag, precision)
defer made.release()
let one: interop.Reference[CadModel][1] = [made]
let replaced = try with_body(set, body, one[..<1])
defer replaced.release()
let before = try face_alone(model, face)
defer before.release()
return try with_stage(try replaced.get(), face, before, 0)
## Untrim: face `face` of body `body` as it was before its last trim
## (`all`: its first), from the set's trim stages; with none, its
## natural surface (natural_face). The stages popped.
pub static func untrimmed(set: interop.Reference[GeometrySet], body: i64, face: str, all: bool = false) -> interop.Reference[GeometrySet]!:
let model = try body_of(set, body)
let source = try set.get()
var earlier = stage_model(source, face, not all)
defer:
if let held = earlier: held.release()
if earlier == none: earlier = try natural_face(model, face)
let restored = earlier else error(invalid, "Untrim found nothing to restore")
let made = try replaced_face(model, face, restored.value())
defer made.release()
let one: interop.Reference[CadModel][1] = [made]
let replaced = try with_body(set, body, one[..<1])
defer replaced.release()
if stage_count(source, face) == 0: return replaced.clone()
return try with_stage(try replaced.get(), face, none, -1 if all else 1)
## How many trim stages `set` keeps for face `face`.
pub static func stages(set: interop.Reference[GeometrySet], face: str) -> i64!:
return (i64)stage_count(try set.get(), face)
## Trim Convert (convert.lucb): face `face` of body `body` as an
## untrimmed B-spline face of degrees `du`, `dv` on `su` by `sv` spans
## (`swap`: u and v exchanged), in its place (stitched to its neighbors
## within its edge deviation); with `keep`, the original kept as a body
## of its own after the set's others. The deviations are reported.
pub static func trim_converted(set: interop.Reference[GeometrySet], body: i64, face: str, du: i64 = 5, dv: i64 = 5, su: i64 = 4, sv: i64 = 4, swap: bool = false,
keep: bool = false) -> interop.Reference[CadSurfaceEdit]!:
let model = try body_of(set, body)
var converted = try trim_convert(model, face, du, dv, su, sv, swap)
defer converted.model.release()
let b = model.brep() else error(invalid, "Trim Convert needs a B-rep body")
let gap = math.min(1.0, math.max(b.base_tolerance() * 10.0, converted.edge_deviation * 1.5))
let made = try replaced_face(model, face, converted.model.value(), gap)
defer made.release()
var bodies = Growing[interop.Reference[CadModel]]()
defer bodies.close()
try bodies.push(made)
if keep:
let original = try face_alone(model, face)
defer original.release()
let replaced = try with_body(set, body, bodies.view())
defer replaced.release()
var all = Growing[interop.Reference[CadModel]]()
defer all.close()
for m in set_models(try replaced.get()): try all.push(m)
try all.push(original)
let both = try with_models(try replaced.get(), all.view())
let edit = try edit_of(both)
edit.value().deviation = converted.surface_deviation
edit.value().edge_deviation = converted.edge_deviation
return edit
let edit = try edit_of(try with_body(set, body, bodies.view()))
edit.value().deviation = converted.surface_deviation
edit.value().edge_deviation = converted.edge_deviation
return edit
## Shrink (convert.lucb): face `face` of body `body` on its surface cut
## down to its loops' box.
pub static func shrunk(set: interop.Reference[GeometrySet], body: i64, face: str) -> interop.Reference[GeometrySet]!:
let model = try body_of(set, body)
let made = try shrunk_face(model, face)
defer made.release()
let one: interop.Reference[CadModel][1] = [made]
return try with_body(set, body, one[..<1])
## Stitch (stitch.lucb): the bodies `bodies` (empty: all of them) joined
## into one body where their boundary edges meet within `gap`, in the
## first's place; `count` the edges joined, `widest` the widest gap
## closed, `closed` whether every edge is shared (a solid). For export,
## Alias's Detach options (stitch.lucb's detached): `detach_periodic`
## splits each face wrapping round its surface in two, `detach_single`
## each loop of one closed edge into two edges. A closed result's joins
## are rebuilt where their faces meet; `tolerant` keeps them tolerant.
pub static func stitched(set: interop.Reference[GeometrySet], bodies: const i64[], gap: f64 = 0.001, detach_periodic: bool = false, detach_single: bool = false, tolerant: bool = false) -> interop.Reference[CadSurfaceEdit]!:
let source = try set.get()
let existing = set_models(source)
var chosen = Growing[interop.Reference[CadModel]]()
defer chosen.close()
var marked = Growing[bool]()
defer marked.close()
try marked.fill(existing.length, bodies.length == 0)
for index in bodies:
if index < 0 or (usize)index >= existing.length: error(invalid, "a body to stitch is no longer in the model: pick it again")
marked.items[(usize)index] = true
for at in 0..<existing.length:
if marked.items[at]: try chosen.push(existing[at])
if chosen.count < 1: error(invalid, "Stitch: pick the surfaces to join")
# One body with only Detach asked: nothing to join, the body as is.
let alone = chosen.count == 1 and (detach_periodic or detach_single)
let sewn = Stitched(chosen.items[0].clone(), 0, 0.0, try CadSurfacing.is_closed(chosen.items[0])) if alone else try stitch_models(chosen.view(), gap, tolerant)
defer sewn.model.release()
# Closed B-spline faces split here (detach.lucb), the analytic ones
# by the stitcher's detached.
var spline_splits: i64 = 0
let opened = try split_closed_splines(sewn.model, &spline_splits) if detach_periodic else sewn.model.clone()
defer opened.release()
let ready = try detached(opened, detach_periodic, detach_single)
defer ready.model.release()
var list = Growing[interop.Reference[CadModel]]()
defer list.close()
var placed = false
for at in 0..<existing.length:
if not marked.items[at]:
try list.push(existing[at])
continue
if not placed:
try list.push(ready.model)
placed = true
let edit = try edit_of(try with_models(source, list.view()))
edit.value().periodic = ready.periodic + spline_splits
edit.value().single = ready.single
edit.value().count = sewn.joined
edit.value().widest = sewn.widest
edit.value().closed = sewn.closed
return edit
## Unstitch (stitch.lucb): body `body`'s faces each an open body of its
## own, in its place.
pub static func unstitched(set: interop.Reference[GeometrySet], body: i64) -> interop.Reference[GeometrySet]!:
let model = try body_of(set, body)
var pieces = Growing[interop.Reference[CadModel]]()
defer:
for m in pieces.view(): m.release()
pieces.close()
try unstitch_model(model, &pieces)
return try with_body(set, body, pieces.view())
## Align (align.lucb, align_master.lucb, align_apply.lucb; Alias's
## Align): the input `input` ("face <name> x y z": the face's side
## nearest the point; "curve <name> x y z": the curve's end nearest it)
## moved onto the master `master` ("edge <face> x y z", "isoparm <face>
## <0 or 1> <fraction>", "curve <name>", "face <name>", "plane ox oy oz
## nx ny nz [free]": Symmetry Plane Align, free leaving the edge off the plane) and matched G`continuity` (0 to 3).
## `second`: a second master, for the side nearest `second_near` (none
## given: the side across). `kind`: 0 Edge, 1 Project (along `vector`;
## the normals without one; `cos` leaves a curve on the master face,
## `shrink` Shrink to Fit), 2 U/V (`vector`.x 0 U, 1 V), 3 Vector (rows
## along `vector`). Position `influence` 0 to 1; Tangent `balance`;
## `tangent`, `curvature`, `g3` the starting scales and the `_end` ones
## the ending (each 1 the input's own hull); `auto_g2`, `auto_g3`;
## `extra` rows blended by `blend`; `partial` from `start` to `end` of
## the input's edge; Modify `both`; Explicit Control `explicit` with
## degrees and spans (0: the input's own); `refine` up to `most` new
## spans within the limits (G0 `gap`, G1 `angle` degrees, G2 and G3
## `difference` percent); `skew` and `skew_end` in degrees; `attach`
## for curves (0 to 1 on the master curve; -1 the nearest point).
pub static func aligned(set: interop.Reference[GeometrySet], input: str, master: str, continuity: i64 = 1, second: str = "", second_near: Vector3 = Vector3(),
kind: i64 = 0, vector: Vector3 = Vector3(), influence: f64 = 0.0, balance: bool = false,
tangent: f64 = 1.0, tangent_end: f64 = 1.0, curvature: f64 = 1.0, curvature_end: f64 = 1.0, g3: f64 = 1.0, g3_end: f64 = 1.0,
auto_g2: bool = false, auto_g3: bool = false, extra: i64 = 0, blend: f64 = 0.0, partial: bool = false, start: f64 = 0.0, end: f64 = 1.0,
both: bool = false, explicit: bool = false, degree_u: i64 = 0, spans_u: i64 = 0, degree_v: i64 = 0, spans_v: i64 = 0,
refine: bool = false, most: i64 = 10, gap: f64 = 0.001, angle: f64 = 0.1, difference: f64 = 0.5,
skew: f64 = 0.0, skew_end: f64 = 0.0, attach: f64 = -1.0, cos: bool = true, shrink: bool = true) -> interop.Reference[CadAlignEdit]!:
if continuity < 0 or continuity > 3: error(invalid, "Align's continuity is G0 to G3")
if kind < 0 or kind > 3: error(invalid, "Align's type is Edge, Project, U/V or Vector")
let opts = AlignOptions(continuity, kind, vector, influence, balance, [tangent, tangent_end, curvature, curvature_end, g3, g3_end], auto_g2, auto_g3, extra, blend,
partial, start, end, both, explicit, degree_u, spans_u, degree_v, spans_v, refine, most, [gap, angle, difference, difference],
[skew, skew_end], attach, cos, shrink)
var made = try aligned_refined(set, input, master, second, second_near, &opts)
let result = interop.Reference[CadAlignEdit].adopt(new CadAlignEdit() in memory.heap, cad_align_edit_type, memory.heap) catch failure:
made.close()
error(failure.code, failure.message)
let edit = result.value()
edit.made = made.set.clone()
let r = made.report
edit.gap_low = r.gap[0] if r.stations > 0 else 0.0
edit.gap_high = r.gap[1]
edit.angle_low = r.angle[0] if r.angle[0] < 1.0e299 else 0.0
edit.angle_high = r.angle[1]
edit.curvature_low = r.curvature[0] if r.curvature[0] < 1.0e299 else 0.0
edit.curvature_high = r.curvature[1]
edit.rate_low = r.rate[0] if r.rate[0] < 1.0e299 else 0.0
edit.rate_high = r.rate[1]
edit.start_angle = r.start_angle
edit.end_angle = r.end_angle
edit.met = r.met
edit.side = made.side
edit.order = made.order
edit.row_size = (i64)made.row_size
try edit.rows.assign(made.rows.view())
try edit.proxy.assign(made.proxy.view())
edit.proxy_size = (i64)made.proxy_size
made.close()
return result
## A body of one B-spline face over its whole domain: control points
## `points` (u-major, index u nv + v) and `weights`, degrees `du`, `dv`,
## clamped knots `ku`, `kv`, the face named `name` and facing along its
## normal (Su x Sv). Surfacing's sheets start this way.
pub static func patch(points: const Vector3[], weights: const f64[], nu: i64, nv: i64, du: i64, dv: i64, ku: const f64[], kv: const f64[], name: str, tolerances: CadTolerances = CadTolerances()) -> interop.Reference[CadModel]!:
if nu < 2 or nv < 2 or points.length != (usize)(nu * nv) or weights.length != points.length: error(invalid, "a patch needs nu x nv control points and a weight each")
if du < 1 or dv < 1 or du > most_degree or dv > most_degree or du >= nu or dv >= nv: error(invalid, "a patch's degrees are 1 to 11 and below its counts")
if ku.length != (usize)(nu + du + 1) or kv.length != (usize)(nv + dv + 1): error(invalid, "a patch's knots are count + degree + 1 each way")
var low = Vector3(1.0e300, 1.0e300, 1.0e300)
var high = Vector3(-1.0e300, -1.0e300, -1.0e300)
for q in points:
if not q.is_finite(): error(invalid, "a patch's control points must be finite")
low = Vector3(math.min(low.x, q.x), math.min(low.y, q.y), math.min(low.z, q.z))
high = Vector3(math.max(high.x, q.x), math.max(high.y, q.y), math.max(high.z, q.z))
return try spline_face(tolerances, points, weights, nu, nv, du, dv, ku, kv, true, name, high.subtract(low).length() * 1e-9)
## Edge `edge`'s precision in `model` (Stitch's tolerant edges: how far
## its faces may lie from it; the model's tolerance otherwise).
pub static func edge_precision(model: interop.Reference[CadModel], edge: i64) -> f64!:
let b = model.value().brep() else error(invalid, "the model has no B-rep")
return try b.edge_precision_of(edge)
pub static func vertex_precision(model: interop.Reference[CadModel], vertex: i64) -> f64!:
let b = model.value().brep() else error(invalid, "the model has no B-rep")
return try b.vertex_precision_of(vertex)
## Whether every edge of `model` is shared by two faces (a closed solid).
pub static func is_closed(model: interop.Reference[CadModel]) -> bool!:
var w = try shape_of(model.value())
defer w.close()
for e in 0..<w.edges.count:
if w.sides.items[e * 2] < 0: continue
if w.sides.items[e * 2 + 1] < 0:
# A seam (one face both ways) is closed.
var count = 0
for f in 0..<w.faces.count:
for use in w.face_uses(f):
if edge_index(use) == e: count += 1
if count < 2: return false
return true
## The meeting curves of `meets` as curves on face `face` of `a` (and on
## `other_face` of `b` when given), named `stem` and a number.
func meeting_curves(set: interop.Reference[GeometrySet], meets: const Shape*, a: const CadModel*, face: str, b: const CadModel*?, other_face: str, tolerance: f64, stem: str) -> interop.Reference[CadSurfaceEdit]!:
if meets.edges.count == 0: error(invalid, "Intersect: the faces do not meet")
let fit = tolerance if tolerance > 0.0 else try fit_tolerance(a)
var result = set.clone()
var made: i64 = 0
var worst = 0.0
for e in 0..<meets.edges.count:
var exact = try edge_spline(meets, e)
defer exact.close()
for side in 0..<2:
var model = a
if side == 1:
let second = b else break
model = second
let on = face if side == 0 else other_face
var charts = Growing[Spline]()
defer close_splines(&charts)
var lifted = Growing[Spline]()
defer close_splines(&lifted)
let pull = Casting(Vector3(), true, true)
worst = math.max(worst, try project_curve(model, on, &exact, pull, fit, false, &charts, &lifted))
for k in 0..<charts.count:
let next = try add_on_face(result, stem, on, &lifted.items[k], &charts.items[k], "intersect")
result.release()
result = next
made += 1
if made == 0:
result.release()
error(invalid, "Intersect: the faces do not meet")
let edit = try edit_of(result)
edit.value().count = made
edit.value().deviation = worst
return edit
## `set` with a curve on face `face`: its 3D curve `curve`, chart curve
## `chart`, named `stem` and the first number free.
func add_on_face(set: interop.Reference[GeometrySet], stem: str, face: str, curve: const Spline*, chart: const Spline*, source: str) -> interop.Reference[GeometrySet]!:
let name = try CadCurves.free_name(set, stem)
defer name.release()
let held = try held_spline(try curve.copy())
defer held.release()
let held_chart = try held_spline(try chart.copy())
defer held_chart.release()
return try CadCurves.with_curve(set, name.value, held, true, source, face, held_chart)
func close_splines(list: Growing[Spline]*):
for s in list.view():
var owned = s
owned.close()
list.close()
## The curves cutting face `face` of `model` (trim_curves.lucb): the set's
## curves on it (or those of `names`), and by `how` curves off it cast onto
## it, faces meeting it, loose ends extended; their 3D curves as a shape's
## B-spline carriers. A curve on the face must lie on its surface within
## the fit tolerance (a curve left on a face whose surface changed since is
## refused: project it again).
func cutting_shape(set: interop.Reference[GeometrySet], model: const CadModel*, face: str, names: str, how: const TrimCurves*) -> Shape!:
var chosen = Growing[i64]()
defer chosen.close()
# Off the face (3D Trimming), named curves anywhere, or with no names
# every curve of the set; on it, those on the face.
try chosen_curves(set, names, face if how.cast == trim_on_face else "", &chosen)
var t = try target_of(model, face)
defer t.close()
let s = t.surface()
let fit = try fit_tolerance(model)
var curves = Growing[Spline]()
defer close_splines(&curves)
for index in chosen.view():
let on = try CadCurves.face(set, index)
defer on.release()
let curve = try CadCurves.curve(set, index)
defer curve.release()
let c = spline_in(curve.value())
if on.value != face:
if how.cast == trim_on_face: error(invalid, "a curve to trim with lies on another face")
# 3D Trimming: cast onto the face as the trim runs.
var charts = Growing[Spline]()
defer close_splines(&charts)
let cast = Casting(how.direction, how.cast == trim_cast_normal, true)
_ = try project_curve(model, face, c, cast, fit, false, &charts, &curves)
continue
var seed = Vector3(0.5, 0.5, 0.0)
for k in 0..<33:
let p = c.point_at(c.low() + (c.high() - c.low()) * (f64)k / 32.0)
var off = 0.0
if s.kind == surface_spline:
seed = try nearest_parameters(*s, p, seed, k == 0)
off = (try evaluate(*s, seed)).subtract(p).length()
else:
off = surface_distance(s, p)
if off > fit * 10.0: error(invalid, "a curve to trim with no longer lies on the face (its surface changed): project it again")
try curves.push(try c.copy())
# 3D Trimming by faces: where each meets this one, pulled onto it.
var start: usize = 0
let bytes = how.cutters.bytes
for at in 0..<bytes.length + 1:
if at < bytes.length and bytes[at] != 59: continue
let other_face = (str)bytes[start..<at]
start = at + 1
if other_face.length == 0 or other_face == face: continue
let other = try body_named(set, other_face)
var meets = try face_meets(model, face, other, other_face)
defer meets.close()
for e in 0..<meets.edges.count:
var exact = try edge_spline(&meets, e)
defer exact.close()
var charts = Growing[Spline]()
defer close_splines(&charts)
_ = try project_curve(model, face, &exact, Casting(Vector3(), true, true), fit, false, &charts, &curves)
if curves.count == 0: error(invalid, "Trim: the face has no curves on it to trim with (Project or Intersect first, or cast curves onto it with 3D Trimming)")
if how.extend: _ = try extend_loose(&t, &curves, fit)
var cuts = empty_shape(t.w.tolerances, t.w.tolerance)
errdefer cuts.close()
for c in curves.view():
var points = Growing[Vector3]()
defer points.close()
var weights = Growing[f64]()
defer weights.close()
for q in c.points.view():
try points.push(q.point())
try weights.push(q.w)
let stored = try cuts.add_spline(points.view(), weights.view(), c.knots.view(), c.degree)
_ = try cuts.add_carrier(Carrier(curve_spline, Vector3(), Vector3(), Vector3(), 0.0, 0.0, stored))
return cuts
## The body of `set` holding a face named `face`.
func body_named(set: interop.Reference[GeometrySet], face: str) -> const CadModel*!:
let models = set_models(try set.get())
for m in models:
let model = m.value()
for f in 0..<model.face_count():
if (try model.face_name(f)) == face: return model
error(invalid, "a face to trim by is no longer in the model: pick it again")
## The faces of `model` named `face`, as a body (a trim's stage).
func face_alone(model: const CadModel*, face: str) -> interop.Reference[CadModel]!:
var chosen = Growing[i64]()
defer chosen.close()
for f in 0..<model.face_count():
if (try model.face_name(f)) == face: try chosen.push(f)
if chosen.count == 0: error(invalid, "the face is not in the body: pick it again")
return try model.subset(chosen.view())