Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / measure / combs.lucb

## Curvature combs (Alias's Curve Curvature locator, Rhino's CurvatureGraph):
## at samples along a curve, a quill square to it, away from its center of
## curvature, as long as its curvature times a scale; the outline joins
## the quills' tips. A circle's comb is a ring of equal quills; a line has
## none.

## One sample of a comb: where it is on the curve, the unit direction away
## from its center of curvature, and the curvature (1 / radius).
pub struct CombSample:
    pub var point: Vector3
    pub var direction: Vector3
    pub var curvature: f64

## A curve's curvature at a jet (|C' x C''| / |C'|^3) and the unit
## direction from its center of curvature through it (zero where it is
## straight).
pub func curve_curvature(j: CurveJet) -> (f64, Vector3):
    let speed = j.d1.length()
    if not (speed > 0.0): return (0.0, Vector3())
    let bend = j.d1.cross(j.d2)
    let size = bend.length()
    let curvature = size / (speed * speed * speed)
    if not (size > 0.0): return (0.0, Vector3())
    # The principal normal (b x t) points at the center; the quill leaves it.
    let toward = bend.cross(j.d1)
    let length = toward.length()
    if not (length > 0.0): return (0.0, Vector3())
    return (curvature, toward.multiply_scalar(-1.0 / length))

## `samples` samples of edge `e` (both ends included) onto `out`; nothing
## for a line.
pub func edge_comb(w: const Shape*, e: usize, samples: usize, out: Growing[CombSample]*) -> !:
    let edge = w.edges.items[e]
    if w.carriers.items[(usize)edge.carrier].kind == curve_line or samples < 2: return
    for k in 0..<samples:
        let t = edge.t0 + (edge.t1 - edge.t0) * (f64)k / (f64)(samples - 1)
        let j = try carrier_jet(w, edge.carrier, t)
        let (curvature, direction) = curve_curvature(j)
        try out.push(CombSample(j.p, direction, curvature))

## The comb of `samples` (one curve's, in order) at `scale` as point pairs
## onto `out`: each quill, then the outline through their tips.
pub func comb_lines(samples: const CombSample[], scale: f64, out: Growing[Vector3]*) -> !:
    for at in 0..<samples.length:
        let s = samples[at]
        let tip = s.point.add(s.direction.multiply_scalar(s.curvature * scale))
        if s.curvature > 0.0:
            try out.push(s.point)
            try out.push(tip)
        if at > 0:
            let before = samples[at - 1]
            try out.push(before.point.add(before.direction.multiply_scalar(before.curvature * scale)))
            try out.push(tip)

## The scale at which the largest of `curvatures` draws `length` long (0
## when all are flat).
pub func comb_scale(largest: f64, length: f64) -> f64:
    if not (largest > 0.0) or not math.is_finite(largest): return 0.0
    return length / largest

## A CAD curve's comb (Alias's Curve Curvature locator on a curve):
## `samples` in each span between distinct knots, both ends included, onto
## `out`; nothing when the curve is straight everywhere.
pub func spline_comb(s: const Spline*, samples: usize, out: Growing[CombSample]*) -> !:
    let first = out.count
    var bent = false
    for k in (usize)s.degree..<s.points.count:
        let a = s.knots.items[k]
        let b = s.knots.items[k + 1]
        if not (b > a): continue
        let begin: usize = 0 if out.count == first else 1
        for i in begin..<samples + 1:
            let t = a + (b - a) * (f64)i / (f64)samples
            let j = try curve_jet(s, t, 3, t >= s.high())
            let (curvature, direction) = curve_curvature(CurveJet(j.d[0], j.d[1], j.d[2], j.d[3]))
            bent = bent or curvature > 0.0
            try out.push(CombSample(j.d[0], direction, curvature))
    if not bent: out.count = first