// APPROXIMATE layer (M3): 2D curve offset. The exact offset of a general NURBS
// curve is not NURBS, so this samples the curve, displaces each sample along the
// left unit normal by `distance`, and re-fits the locus as a NURBS curve (the
// standard CAD approximation). Reference: curvo/src/offset/.
///|
/// Offset a 2D curve by `distance` (positive = left of the travel direction).
/// Samples `samples` points, displaces along the left normal, and fits a degree-
/// `degree` NURBS curve with `num_cpts` control points. None on evaluation failure.
pub fn NurbsCurve2::offset(
self : NurbsCurve2,
distance : Double,
samples? : Int = 48,
degree? : Int = 3,
num_cpts? : Int = 12,
) -> NurbsCurve2? {
if samples < 2 || degree < 1 {
return None
}
let n = self.bspline.ctrlpts.length() - 1
let knots = self.bspline.knots.knots
let u0 = knots[self.bspline.degree]
let u1 = knots[n + 1]
let pts : FixedArray[@f64.DVec2] = FixedArray::make(samples + 1, @f64.DVec2::zero())
for i = 0; i <= samples; i = i + 1 {
let u = u0 + (u1 - u0) * (i.to_double() / samples.to_double())
let p = match self.point_at(u) {
None => return None
Some(x) => x
}
let t = match self.derivatives(u, 1) {
None => return None
Some(d) => d[1]
}
let nv = @f64.DVec2::new(-t.y, t.x)
let normal = if nv.length() < 1.0e-12 {
@f64.DVec2::zero()
} else {
nv.normalize()
}
pts[i] = p + normal.scale(distance)
}
NurbsCurve2::fit(pts, degree, num_cpts=num_cpts)
}