dymokomi/luce-cad
Source of release 0.9.5
0.9.5 / src / measure / curve_joins.lucb
## Continuity between curves (Stage 2's curves, a blend's ends, Attach's
## joint): by their derivatives by arc length (curves' jets), to G4.
## How two curves meet at a joint, by their derivatives by arc length
## (jets.lucb): the gap, the angle between the tangents (degrees), and the
## differences of the curvature vectors, their rates and their second
## rates, each relative to the larger of the two in percent (Alias's G2
## and G3 measures; G4 the next one). `level` is the highest order met
## within the thresholds: gap 1e-6 relative to the curves' size, 0.01°,
## 0.01% (-1: not even G0).
pub struct CadCurveJoin:
pub var gap: f64
pub var angle: f64
pub var curvature: f64
pub var rate: f64
pub var fourth: f64
pub var level: i64
## The relative difference of `a` and `b` in percent (0 when both are
## below `floor`).
func relative_percent(a: Vector3, b: Vector3, floor: f64) -> f64:
let larger = math.max(a.length(), b.length())
if larger <= floor: return 0.0
return a.subtract(b).length() / larger * 100.0
## The joint between `a` at `ta` and `b` at `tb`, each by a parameter
## running into the joint (`sa` 1: `a` reaches it from below, -1 from
## above; `sb` the same for `b` leaving it the other way).
pub func curve_join(a: const Spline*, ta: f64, sa: i64, b: const Spline*, tb: f64, sb: i64) -> CadCurveJoin!:
# Both by parameters running through the joint the same way: a's as it
# arrives, b's as it leaves.
let raw_a = try curve_jet(a, ta, 4, sa > 0)
let raw_b = try curve_jet(b, tb, 4, sb > 0)
let into = raw_a if sa > 0 else reversed_jet(&raw_a)
let out_of = raw_b if sb < 0 else reversed_jet(&raw_b)
let ja = try arc_jet(&into)
let jb = try arc_jet(&out_of)
let size = math.max(1.0, math.max(size_of(a), size_of(b)))
let gap = ja.d[0].subtract(jb.d[0]).length()
let cosine = math.clamp(ja.d[1].dot(jb.d[1]), -1.0, 1.0)
let angle = math.acos(cosine) * 180.0 / math.pi
# Floors: a millionth of the curvature of a curve the model's size.
let floor = 1e-6 / size
let curvature = relative_percent(ja.d[2], jb.d[2], floor)
let rate = relative_percent(ja.d[3], jb.d[3], floor / size)
let fourth = relative_percent(ja.d[4], jb.d[4], floor / (size * size))
var level: i64 = -1
if gap <= 1e-6 * size:
level = 0
if angle <= 0.01:
level = 1
if curvature <= 0.01:
level = 2
if rate <= 0.01:
level = 3
if fourth <= 0.01: level = 4
return CadCurveJoin(gap, angle, curvature, rate, fourth, level)