dymokomi/luce-cad
Source of release 0.9.5
0.9.5 / src / layout / face_targets.lucb
## This file: a curved face's interior target, how far its mesh may stray
## from the surface. The meshers refine a face's interior to it
## (analytic_trim, surface_trim), and before any edge's samples are frozen
## and shared, each face asks its edges to be sampled at least that finely
## (edge_demands; the finest demand wins). So the boundary a face is given
## never sags more than its interior may, and refining the interior can
## always reach its target.
## The target of a face on a cylinder, cone, sphere or torus: the sag of a
## mesh edge turning the step angle (a quarter turn over `divisions`) on
## the surface's tightest radius, so a face meshes by angle, whatever its
## size or extent (`extent` is not used), never finer than the mesher's
## identity `tolerance` (the B-rep's exactness, or what the display can
## show). A plane has none.
pub func analytic_target(s: Surface, extent: f64, divisions: Density, tolerance: f64) -> f64:
_ = extent
var radius = math.abs(s.radius)
if s.kind == surface_torus: radius = math.min(math.abs(s.angle), math.abs(math.abs(s.radius) - math.abs(s.angle)))
return math.max(tolerance, divisions.within(step_sag(radius, divisions)))
## How far a chord of a circle of `radius` turning a quarter turn over
## `divisions` strays from it.
pub func step_sag(radius: f64, divisions: Density) -> f64:
return radius * (1.0 - math.cos(math.pi * 0.25 / (f64)divisions.steps))
## How far a mesh of surface `s` may stray from it: the sag of a mesh edge
## turning the step angle on the surface's tightest radius of curvature
## (analytic_target; a B-spline's from its normal curvatures sampled over
## its domain, spline_radius), within the chord limit, never under the
## mesher's identity `tolerance`. A plane has no limit (`flat_sag`).
pub func surface_sag(s: Surface, divisions: Density, tolerance: f64) -> f64!:
if s.kind == surface_plane: return flat_sag
if s.kind != surface_spline: return analytic_target(s, 0.0, divisions, tolerance)
let radius = try spline_radius(s)
if not (radius < flat_radius): return math.max(tolerance, divisions.within(flat_sag))
return math.max(tolerance, divisions.within(step_sag(radius, divisions)))
## The sag a mesh of analytic surface `s` may have where it curves least
## (a torus's around its axis, at its outer radius): what a cell stepping
## the step angle that way strays. A mapped grid is checked against it, so
## a cell is not split for straying, the way it turns least, by the sag
## its tightest way allows (surface_sag is that tightest one).
pub func loosest_sag(s: Surface, divisions: Density, tolerance: f64) -> f64!:
if s.kind == surface_torus: return math.max(tolerance, divisions.within(step_sag(math.abs(s.radius) + math.abs(s.angle), divisions)))
return try surface_sag(s, divisions, tolerance)
## How far a chord along chart direction `axis` (0 u, 1 v) of surface `s`
## may stray: the sag of a chord turning the step angle on the tightest
## radius of curvature that way (a B-spline's normal curvature along that
## parameter, sampled over its domain; a torus's around its axis or round
## its tube; a cylinder's or cone's round its axis, none along it), within
## the chord limit, never under `tolerance`. A cell's rows split each way
## by its own (refine_axis), so a surface curving one way only is not
## refined across the way it is straight or gently bent.
pub func axis_sag(s: Surface, axis: usize, divisions: Density, tolerance: f64) -> f64!:
if s.kind == surface_plane: return flat_sag
var radius = flat_radius
if s.kind == surface_spline: radius = try spline_radius(s, axis)
elif s.kind == surface_torus: radius = math.abs(math.abs(s.radius) - math.abs(s.angle)) if axis == 0 else math.abs(s.angle)
elif s.kind == surface_sphere or axis == 0: radius = math.abs(s.radius)
if not (radius < flat_radius): return math.max(tolerance, divisions.within(flat_sag))
return math.max(tolerance, divisions.within(step_sag(radius, divisions)))
## A radius of curvature taken as flat.
let flat_radius: f64 = 1.0e300
## The tightest radius of curvature of B-spline `s`: of its normal
## curvatures along u, along v and across (the twist), at a 9 by 9 lattice
## over its domain; along u alone (`axis` 0) or v alone (1).
pub func spline_radius(s: Surface, axis: usize = 2) -> f64!:
var most = 0.0
for i in 0..<9:
for j in 0..<9:
let uv = Vector3((f64)i / 8.0, (f64)j / 8.0, 0.0)
let jet = try surface_jet(&s, uv, i == 8, j == 8)
let lu = jet.su.length()
let lv = jet.sv.length()
let normal = jet.su.cross(jet.sv)
let ln = normal.length()
if lu <= 0.0 or lv <= 0.0 or ln <= 0.0: continue
let n = normal.multiply_scalar(1.0 / ln)
if axis != 1: most = math.max(most, math.abs(jet.suu.dot(n)) / (lu * lu))
if axis != 0: most = math.max(most, math.abs(jet.svv.dot(n)) / (lv * lv))
if axis == 2: most = math.max(most, math.abs(jet.suv.dot(n)) / (lu * lv))
return 1.0 / most if most > 0.0 else flat_radius
## A B-spline surface's lengths along u and v (x and y): polylines of 16
## spans along its middle isoparms.
pub func spline_lengths(s: Surface) -> Vector3!:
var lengths = Vector3()
for at in 0..<16:
let a = (f64)at / 16.0
let b = (f64)(at + 1) / 16.0
lengths.x += (try evaluate(s, Vector3(b, 0.5, 0.0))).subtract(try evaluate(s, Vector3(a, 0.5, 0.0))).length()
lengths.y += (try evaluate(s, Vector3(0.5, b, 0.0))).subtract(try evaluate(s, Vector3(0.5, a, 0.0))).length()
return lengths
## The target of a trimmed B-spline face with those lengths.
pub func spline_target(lengths: Vector3, divisions: Density, tolerance: f64) -> f64:
return divisions.sag(math.min(lengths.x, lengths.y), 4.0, tolerance)
## The angle a circle of `radius` may turn between samples so that its
## chords sag at most `target`.
pub func sag_step(radius: f64, target: f64) -> f64:
if target >= radius: return math.pi
return 2.0 * math.acos(1.0 - target / radius)
## Whether `face` is a sphere triangle: one loop of three great-circle arcs.
pub func sphere_triangle(face: BrepFace, surface: Surface, edges: const Curve[], tolerance: f64) -> bool:
if surface.kind != surface_sphere or face.sizes.length != 1 or face.uses.length != 3: return false
for use in face.uses:
let edge = edges[edge_index(use)]
if edge.kind != curve_circle or edge.origin.subtract(surface.origin).length() > tolerance * 10.0: return false
if math.abs(edge.radius - surface.radius) > tolerance * 10.0: return false
return true
## Raise each edge's interval count to what its curved faces demand.
pub func edge_demands(edges: const Curve[], surfaces: const Surface[], faces: const BrepFace[], metrics: ArcMetric[], counts: usize[], divisions: Density, tolerance: f64) -> !:
for face in faces:
let s = surfaces[(usize)face.surface]
# A B-spline face's interior target is the trimmed fallback's
# (surface_trim): its mapped strips refine no interior, and the
# fallback relaxes beside a coarser boundary (refine_uv).
if s.kind == surface_plane or s.kind == surface_spline: continue
# A sphere triangle's grid is as fine as its arcs (spherical_triangle);
# another sphere face with a loop round its axis is meshed in rows
# from that loop (spherical_cap, sphere_disk), with no interior target.
let corner = sphere_triangle(face, s, edges, tolerance)
if s.kind == surface_sphere and not corner and (try circles_axis(face, s, edges)): continue
# Each edge takes as many pieces as the surface's normal turns along
# it over the step angle (the turn that way, whatever the surface
# does across it).
let step = math.pi * 0.5 / (f64)divisions.steps
# A cylinder's rows across its axis step the angle round it: an
# edge running along the axis takes as many pieces as keep its
# cells no more than row_stretch times longer than wide.
let across = math.abs(s.radius) * math.pi * 0.5 / (f64)divisions.steps
for use in face.uses:
let index = edge_index(use)
let turn = try normal_turn(edges[index], s)
counts[index] = (usize)math.max((f64)counts[index], math.min(1024.0, math.ceil(turn / step - count_slack)))
if s.kind == surface_cylinder and across > 0.0:
let along = math.abs((try edges[index].at(1.0)).subtract(try edges[index].at(0.0)).dot(s.z))
counts[index] = (usize)math.max((f64)counts[index], math.min(1024.0, math.ceil(along / (across * row_stretch) - count_slack)))
## How far analytic surface `s`'s normal turns along `edge` (radians), by
## 64 pieces.
func normal_turn(edge: Curve, s: Surface) -> f64!:
var seed = Vector3()
var previous = Vector3()
var turn = 0.0
for at in 0..<65:
let p = try edge.at((f64)at / 64.0)
seed = chart_uv(s, p, seed, at > 0)
let n = try support_normal(s, seed)
if at > 0: turn += math.acos(math.clamp(n.dot(previous) / math.max(n.length() * previous.length(), nonzero), -1.0, 1.0))
previous = n
return turn
## Whether one of a sphere face's loops goes once round the sphere's axis.
func circles_axis(face: BrepFace, s: Surface, edges: const Curve[]) -> bool!:
var offset: usize = 0
for size in face.sizes:
var total: f64 = 0.0
var previous: f64 = 0.0
var first = true
for at in offset..<offset + (usize)size:
let use = face.uses[at]
let edge = edges[edge_index(use)]
for step in 0..<17:
let p = (try edge.at((f64)step / 16.0 if use > 0 else 1.0 - (f64)step / 16.0)).subtract(s.origin)
let angle = math.atan2(p.dot(s.y), p.dot(s.x))
if not first: total += wrap(angle, previous) - previous
previous = angle
first = false
offset += (usize)size
if math.abs(math.abs(total) - 2.0 * math.pi) < 0.5: return true
return false