dymokomi/luce-cad
Source of release 0.9.4
0.9.4 / src / modeling / cap_rows.lucb
## The rows of a cap loop's blends (cap_blend.lucb): how each wall runs
## away from its edge, the section across the edge there, and the strip's
## rows of control points along the edge.
##
## A wall runs away from its edge along its rulings (its straight lines):
## - a plane between two seams: from one seam's way to the other's, so its
## contact with the blend is the line between its seams' points;
## - a cylinder or cone about the cap's normal: its line through the point;
## - a surface swept straight along one way (a B-spline sweep, an earlier
## fillet's strip, a cylinder along a slanted upright): that way.
## Square walls (a box's sides) run along the cap's normal. A drafted wall
## (a tapered extrusion's) leans: the corner between the cap and the wall
## is not square, the same along a plane or a cone about the normal, and
## changing along a slanted sweep.
##
## The section lies on the cap's way `u` (into the cap, square to the edge)
## and the ruling `S`, scaled so that across the edge it is one unit long
## (S = D + g T: D the wall's way square to the edge, T the edge's way): a
## section point `a` along u and `b` along S is a + b λ into the cap and b μ
## along the normal (λ = D · u, the corner's cosine). A fillet's contacts are
## r cot(θ/2) from the corner: (1 + λ) r / μ. Rows are e(t) + a_k u(t) +
## b_k S(t): on the cap where b is 0, on the wall where a is 0 (its ruling),
## exact lines along a line, exact arcs about an arc, fitted elsewhere.
## How a side's wall runs away from its edge (CapSide.ruling).
let ruling_between: i64 = 0
let ruling_cone: i64 = 1
let ruling_sweep: i64 = 2
## Side `side`'s ruling at its carrier's parameter `t`: the way into its
## wall, scaled to a unit across the edge.
func ruling_at(b: const Cap*, side: const CapSide*, t: f64) -> Vector3!:
let w = b.w
let edge = w.edges.items[side.edge]
var s = Vector3()
if side.ruling == ruling_between:
let f = (t - edge.t0) / (edge.t1 - edge.t0)
return side.s0.add(side.s1.subtract(side.s0).multiply_scalar(f))
if side.ruling == ruling_cone:
let wall = &w.surfaces.items[(usize)w.faces.items[side.wall].surface]
let p = try w.at(edge.carrier, t)
let d = p.subtract(wall.origin)
let radial = try d.subtract(wall.z.multiply_scalar(d.dot(wall.z))).normalized()
s = wall.z.add(radial.multiply_scalar(math.tan(wall.angle) if wall.kind == surface_cone else 0.0))
else:
s = side.sweep
if s.dot(side.into_wall) < 0.0: s = s.multiply_scalar(-1.0)
let tangent = try (try w.tangent(edge.carrier, t)).normalized()
let spread = s.subtract(tangent.multiply_scalar(s.dot(tangent))).length()
if spread <= 1e-9: error(invalid, "a blend's wall runs along its edge")
return s.multiply_scalar(1.0 / spread)
## The corner's cosine λ at `t` (the wall's way square to the edge, against
## the way into the cap) and the contact distance into the cap there (a
## fillet's r cot(θ/2), a chamfer's leg on the cap).
func corner_at(b: const Cap*, side: const CapSide*, t: f64) -> (f64, f64)!:
let edge = b.w.edges.items[side.edge]
let (u, _) = try into_cap(b, side, t)
let s = try ruling_at(b, side, t)
let tangent = try (try b.w.tangent(edge.carrier, t)).normalized()
let lambda = s.subtract(tangent.multiply_scalar(s.dot(tangent))).dot(u)
if math.abs(lambda) > 1.0 - 1e-6: error(invalid, "a blend's wall lies along its cap")
return (lambda, try contact_distance(b, lambda, try reach_at(b, side, t)))
## The fillet's radius (a chamfer's size) at side `side`'s carrier
## parameter `t`: the blend's own, or its radius law's there.
func reach_at(b: const Cap*, side: const CapSide*, t: f64) -> f64!:
if let law = b.law: return try law.at_edge(b.w, side.edge, t)
return b.reach
## How far a blend of radius (or size) `r` has its contact on the cap from
## a corner of cosine `lambda`: a fillet's, both its contacts' r cot(θ/2); a
## chamfer's leg on the cap (its first distance, or the second's when the
## wall is its first face: chamfers.lucb).
func contact_distance(b: const Cap*, lambda: f64, r: f64) -> f64!:
if not b.round:
if b.cap_first: return r
return try second_leg(b.chamfer, r, math.acos(lambda))
return r * (1.0 + lambda) / math.sqrt(1.0 - lambda * lambda)
## How far a blend of radius (or size) `r` has its contact on the wall from
## a corner of cosine `lambda` (along the wall's ruling, across the edge): a
## fillet's as the cap's, a chamfer's leg on the wall.
func wall_distance(b: const Cap*, lambda: f64, r: f64) -> f64!:
if not b.round:
if not b.cap_first: return r
return try second_leg(b.chamfer, r, math.acos(lambda))
return try contact_distance(b, lambda, r)
## The section of radius (or size) `r` at a corner of cosine `lambda`: its control points as steps
## along u (`along_cap`) and S (`along_wall`) from the corner, from the cap's
## contact to the wall's; its knots, weights and degree. A chamfer's line;
## G1 a circle's arc (rational, its middle weight the chain's: b.weight);
## G2, G3 the fillet's B-spline (fillet_section).
func section_at(b: const Cap*, lambda: f64, r: f64, along_cap: Growing[f64]*, along_wall: Growing[f64]*, knots: Growing[f64]*, weights: Growing[f64]*) -> i64!:
along_cap.clear()
along_wall.clear()
knots.clear()
weights.clear()
if b.rolled:
# Rolled (cap_rolled.lucb): a degree 5 Bezier, its points made
# there; six placeholder steps.
try along_cap.fill(6, 0.0)
try along_wall.fill(6, 0.0)
for _ in 0..<(usize)6: try knots.push(0.0)
for _ in 0..<(usize)6: try knots.push(1.0)
try weights.fill(6, 1.0)
return 5
let c = try contact_distance(b, lambda, r)
if not b.round:
let a: f64[2] = [c, 0.0]
let z: f64[2] = [0.0, try wall_distance(b, lambda, r)]
let k: f64[4] = [0.0, 0.0, 1.0, 1.0]
try along_cap.assign(a[..<2])
try along_wall.assign(z[..<2])
try knots.assign(k[..<4])
try weights.fill(2, 1.0)
return 1
if b.continuity == 1:
let a: f64[3] = [c, 0.0, 0.0]
let z: f64[3] = [0.0, 0.0, c]
let k: f64[6] = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]
# (Times Fusion's Tangency weight: a conic.)
let x: f64[3] = [1.0, b.blend_shape.middle(b.weight), 1.0]
try along_cap.assign(a[..<3])
try along_wall.assign(z[..<3])
try knots.assign(k[..<6])
try weights.assign(x[..<3])
return 2
var points = Growing[Vector3]()
defer points.close()
try fillet_section(Vector3(c, 0.0, 0.0), Vector3(0.0, c, 0.0), Vector3(), b.continuity, &points, knots, b.blend_shape)
for p in points.view():
try along_cap.push(p.x)
try along_wall.push(p.y)
try weights.fill(points.count, 1.0)
return 5
## Every row of side `side`'s section at parameter `t` (the section
## there) onto `out`: e(t) + a_k u(t) + b_k S(t).
func rows_at(b: const Cap*, side: const CapSide*, t: f64, out: Growing[Vector3]*) -> !:
if b.rolled: return try cap_rolled_section(b, side, t, out)
let edge = b.w.edges.items[side.edge]
let (lambda, _) = try corner_at(b, side, t)
var a = Growing[f64]()
defer a.close()
var z = Growing[f64]()
defer z.close()
var knots = Growing[f64]()
defer knots.close()
var weights = Growing[f64]()
defer weights.close()
_ = try section_at(b, lambda, try reach_at(b, side, t), &a, &z, &knots, &weights)
let (u, _) = try into_cap(b, side, t)
let s = try ruling_at(b, side, t)
let e = try b.w.at(edge.carrier, t)
out.clear()
for k in 0..<a.count: try out.push(e.add(u.multiply_scalar(a.items[k])).add(s.multiply_scalar(z.items[k])))
## Side `side`'s rows (`rows` of them) as curves sharing knots: the net
## (row-major, a row per section point) onto `net`, the along knots and
## weights onto `knots` and `weights`; the degree. Along a line with a
## plane's ruling between its seams they are exact lines, along an arc
## with a cone's or cylinder's ruling exact rational arcs (concentric) over
## [0, 1]; else fitted.
func along_rows(b: const Cap*, side: const CapSide*, rows: usize, net: Growing[Vector3]*, knots: Growing[f64]*, weights: Growing[f64]*) -> i64!:
let w = b.w
let edge = w.edges.items[side.edge]
let carrier = w.carriers.items[(usize)edge.carrier]
# A radius changing along the edge: always fitted.
if b.law != none: return try fitted_rows(b, side, rows, net, knots, weights)
if carrier.kind == curve_line and side.ruling == ruling_between:
var first = Growing[Vector3]()
defer first.close()
var last = Growing[Vector3]()
defer last.close()
try rows_at(b, side, edge.t0, &first)
try rows_at(b, side, edge.t1, &last)
for k in 0..<rows:
try net.push(first.items[k])
try net.push(last.items[k])
let ends: f64[4] = [0.0, 0.0, 1.0, 1.0]
try knots.assign(ends[..<4])
try weights.fill(2, 1.0)
return 1
if carrier.kind != curve_circle or side.ruling != ruling_cone: return try fitted_rows(b, side, rows, net, knots, weights)
# An arc in spans of at most a quarter turn, each a rational quadratic:
# a section point a along u and b along S is a + b λ out (or in) and b μ
# down the axis.
let (lambda, _) = try corner_at(b, side, edge.t0)
let mu = math.sqrt(1.0 - lambda * lambda)
var a = Growing[f64]()
defer a.close()
var z = Growing[f64]()
defer z.close()
var section_knots = Growing[f64]()
defer section_knots.close()
var section_weights = Growing[f64]()
defer section_weights.close()
_ = try section_at(b, lambda, b.reach, &a, &z, §ion_knots, §ion_weights)
let sweep = edge.t1 - edge.t0
let spans = (usize)math.max(1.0, math.ceil(math.abs(sweep) / (math.pi * 0.5) - 1e-9))
let half = sweep / (2.0 * (f64)spans)
let radial = w.points.items[(usize)edge.start].subtract(carrier.origin)
let (u0, _) = try into_cap(b, side, edge.t0)
let outward = 1.0 if u0.dot(radial) > 0.0 else -1.0
for i in 0..<rows:
let radius = carrier.a + (a.items[i] + z.items[i] * lambda) * outward
let center = carrier.origin.add(side.into_wall.multiply_scalar(z.items[i] * mu))
for j in 0..<spans * 2 + 1:
let angle = edge.t0 + half * (f64)j
let direction = carrier.x.multiply_scalar(math.cos(angle)).add(carrier.y.multiply_scalar(math.sin(angle)))
let scale = radius if j % 2 == 0 else radius / math.cos(half)
try net.push(center.add(direction.multiply_scalar(scale)))
for _ in 0..<(usize)3: try knots.push(0.0)
for j in 1..<spans:
try knots.push((f64)j / (f64)spans)
try knots.push((f64)j / (f64)spans)
for _ in 0..<(usize)3: try knots.push(1.0)
for j in 0..<spans * 2 + 1: try weights.push(1.0 if j % 2 == 0 else math.cos(half))
return 2
## Side `side`'s rows fitted: least squares on the edge's parameter, a
## B-spline edge's degree (at least 3) and knots, else degree 5 over two
## spans; each row's ends and end tangents exact; spans whose rows stray
## more than the tolerance are halved, and it is fitted again.
func fitted_rows(b: const Cap*, side: const CapSide*, rows: usize, net: Growing[Vector3]*, knots: Growing[f64]*, weights: Growing[f64]*) -> i64!:
let w = b.w
let edge = w.edges.items[side.edge]
let carrier = w.carriers.items[(usize)edge.carrier]
let low = math.min(edge.t0, edge.t1)
let high = math.max(edge.t0, edge.t1)
knots.clear()
var p: usize = 5
if carrier.kind == curve_spline:
let (_, _, ck, cd) = w.spline_net(carrier.spline)
p = (usize)math.max((f64)cd, 3.0)
for _ in 0..<p + 1: try knots.push(low)
var at: usize = 0
while at < ck.length:
let value = ck[at]
var multiplicity: usize = 0
while at + multiplicity < ck.length and ck[at + multiplicity] == value: multiplicity += 1
at += multiplicity
if value <= low or value >= high: continue
for _ in 0..<(usize)math.min((f64)p, (f64)(multiplicity + p - (usize)cd)): try knots.push(value)
for _ in 0..<p + 1: try knots.push(high)
else:
for _ in 0..<p + 1: try knots.push(low)
try knots.push((low + high) * 0.5)
for _ in 0..<p + 1: try knots.push(high)
if let law = b.law: try law_knots(law, w, side.edge, low, high, p, knots)
var context = CapRows(b, side)
# The rows are fitted to the project's fit tolerance F, whatever the
# body's size (docs/research/KERNEL-ROBUSTNESS.md, 4.2).
let tolerance = w.tolerances.fit
var source = RowSource((void*)&context, cap_rows_at, low, high, tolerance)
fit_rows(&source, rows, p, knots, net) catch failure:
if not b.rolled or failure.code != invalid: error(failure.code, failure.message)
error(invalid, "a G1 variable fillet (Tangency weight 1) rolling its ball round this loop could not be fitted: its radius comes near the loop's own curvature; give it a smaller radius there or another weight")
try weights.fill(knots.count - p - 1, 1.0)
return (i64)p
## A side's rows as a row source: the cap blend and the side.
struct CapRows:
var b: const Cap*
var side: const CapSide*
func cap_rows_at(context: void*, t: f64, out: Growing[Vector3]*) -> !:
let rows = (CapRows*)context
try rows_at(rows.b, rows.side, t, out)