dymokomi/luce-cad
Source of release 0.9.2
0.9.2 / src / layout / curve_spacing.lucb
## Shared physical-length metrics for distributing rows over compound sides.
## These tables guide spacing only; exact curves still own every output point.
## Sampled cumulative arc length and the distribution accepted for one curve.
pub struct ArcMetric:
pub var lengths: f64[]
pub var uniform: bool = true
## An even physical station, or the original parameter station when its
## curvature check could not accept physical spacing within the budget.
pub func station(fraction: f64) -> f64:
return self.parameter(fraction * self.at(1.0)) if self.uniform else fraction
## Invert station() to recognize baseline stations after new cuts arrive.
pub func fraction(parameter: f64) -> f64:
let length = self.at(1.0)
return self.at(parameter) / length if self.uniform and length > 1e-30 else parameter
## Interpolated physical distance at a normalized exact-curve parameter.
pub func at(t: f64) -> f64:
let position = math.clamp(t, 0.0, 1.0) * (f64)(self.lengths.length - 1)
let index = (usize)math.min((f64)(self.lengths.length - 2), math.floor(position))
return self.lengths[index] + (self.lengths[index + 1] - self.lengths[index]) * (position - (f64)index)
## Invert cumulative distance; output geometry still evaluates the curve.
pub func parameter(distance: f64) -> f64:
var low: usize = 0
var high = self.lengths.length - 1
while high - low > 1:
let mid = (low + high) // 2
if self.lengths[mid] < distance: low = mid
else: high = mid
let span = self.lengths[high] - self.lengths[low]
let fraction = math.clamp((distance - self.lengths[low]) / span, 0.0, 1.0) if span > 1e-30 else 0.5
return ((f64)low + fraction) / (f64)(self.lengths.length - 1)
## Bounded sampled arc length; shared once by all planning passes and faces.
pub func measure_curves(edges: const Curve[]) -> ArcMetric[]!:
let result = try new ArcMetric[edges.length] --- in memory.heap
for at in 0..<result.length: result[at] = ArcMetric()
errdefer close_metrics(result)
for at in 0..<edges.length:
let count: usize = 2 if edges[at].kind == curve_line else 65
result[at].lengths = try new f64[count] --- in memory.heap
# Sampling is pure per curve: in parallel, then serially if one failed
# (so the error is the serial one).
var sampling = Sampling(edges, result, 0)
try parallel.for_each_range(edges.length, 32, (void*)&sampling, sample_lengths)
if sampling.failed.load(.acquire) != 0:
for at in 0..<edges.length: try sample_curve_lengths(edges[at], result[at].lengths)
return result
struct Sampling:
let edges: const Curve[]
let metrics: ArcMetric[]
var failed: @u32
func sample_lengths(context: void*, first: usize, end: usize, worker: usize):
let work = (Sampling*)context
for at in first..<end:
sample_curve_lengths(work.edges[at], work.metrics[at].lengths) catch failure:
_ = failure
work.failed.store(1, .relaxed)
return
## Cumulative chord lengths at even parameter steps.
func sample_curve_lengths(edge: Curve, values: f64[]) -> !:
values[0] = 0.0
var previous = try edge.at(0.0)
for step in 1..<values.length:
let current = try edge.at((f64)step / (f64)(values.length - 1))
values[step] = values[step - 1] + current.subtract(previous).length()
previous = current
## Release a metric array returned by measure.
pub func close_metrics(metrics: const ArcMetric[]):
for metric in metrics:
if metric.lengths.length > 0: free(metric.lengths) in memory.heap
free(metrics) in memory.heap
## Allocate extra intervals to the largest physical pitch, never lowering any
## edge's curvature requirement or treating tiny coedges as whole-length sides.
pub func raise_side(face: BrepFace, sides: PatchSides, side: usize, counts: usize[], metrics: const ArcMetric[], required: usize):
let count = side_count(face, sides, side, counts)
if count >= required: return
for extra in 0..<required - count:
var longest: f64 = -1.0
var selected: usize = 0
for segment in 0..<sides.lengths[side]:
let edge = edge_index(face.uses[(sides.starts[side] + segment) % face.uses.length])
let pitch = metrics[edge].at(1.0) / (f64)counts[edge]
if pitch > longest:
longest = pitch
selected = edge
counts[selected] += 1