// EXACT layer (design D3, M3): 2D NURBS curve. A 2D point (x, y) with weight w is
// stored homogeneously as DVec3(x*w, y*w, w), so the curve reuses the generic
// BSpline[DVec3] kernel wholesale (evaluation, derivatives, knot/degree algebra).
// The rational projection divides by the z component (the weight). This is the
// foundation for 2D offset / boolean / fillet and for surface trim loops.

///|
pub(all) struct NurbsCurve2 {
  bspline : BSpline[@f64.DVec3]
}

///|
pub fn NurbsCurve2::from_points_weights(
  degree : Int,
  knots : KnotVector,
  points : FixedArray[@f64.DVec2],
  weights : FixedArray[Double],
) -> NurbsCurve2? {
  if points.length() != weights.length() {
    return None
  }
  let hw : FixedArray[@f64.DVec3] = FixedArray::make(points.length(), @f64.DVec3::zero())
  for i = 0; i < points.length(); i = i + 1 {
    let w = weights[i]
    if w <= 0.0 {
      return None
    }
    hw[i] = @f64.DVec3::new(points[i].x * w, points[i].y * w, w)
  }
  match BSpline::new(degree, knots, hw) {
    None => None
    Some(b) => Some({ bspline: b })
  }
}

///|
pub fn NurbsCurve2::from_points(
  degree : Int,
  knots : KnotVector,
  points : FixedArray[@f64.DVec2],
) -> NurbsCurve2? {
  let weights : FixedArray[Double] = FixedArray::make(points.length(), 1.0)
  NurbsCurve2::from_points_weights(degree, knots, points, weights)
}

///|
pub fn NurbsCurve2::new_homogeneous(
  degree : Int,
  knots : KnotVector,
  ctrlpts : FixedArray[@f64.DVec3],
) -> NurbsCurve2? {
  match BSpline::new(degree, knots, ctrlpts) {
    None => None
    Some(b) => Some({ bspline: b })
  }
}

///|
pub fn NurbsCurve2::degree(self : NurbsCurve2) -> Int {
  self.bspline.degree
}

///|
pub fn NurbsCurve2::num_ctrlpts(self : NurbsCurve2) -> Int {
  self.bspline.ctrlpts.length()
}

///|
pub fn NurbsCurve2::control_points(self : NurbsCurve2) -> FixedArray[@f64.DVec2] {
  let n = self.bspline.ctrlpts.length()
  let out : FixedArray[@f64.DVec2] = FixedArray::make(n, @f64.DVec2::zero())
  for i = 0; i < n; i = i + 1 {
    let p = self.bspline.ctrlpts[i]
    out[i] = @f64.DVec2::new(p.x / p.z, p.y / p.z)
  }
  out
}

///|
pub fn NurbsCurve2::weights(self : NurbsCurve2) -> FixedArray[Double] {
  let n = self.bspline.ctrlpts.length()
  let out : FixedArray[Double] = FixedArray::make(n, 0.0)
  for i = 0; i < n; i = i + 1 {
    out[i] = self.bspline.ctrlpts[i].z
  }
  out
}

///|
pub fn NurbsCurve2::point_at(self : NurbsCurve2, u : Double) -> @f64.DVec2? {
  match self.bspline.evaluate(u) {
    None => None
    Some(p) => {
      if p.z == 0.0 {
        None
      } else {
        Some(@f64.DVec2::new(p.x / p.z, p.y / p.z))
      }
    }
  }
}

///|
/// Rational derivatives 0..d (quotient rule, weight = z component).
pub fn NurbsCurve2::derivatives(
  self : NurbsCurve2,
  u : Double,
  d : Int,
) -> Array[@f64.DVec2]? {
  match self.bspline.evaluate_derivatives(u, d) {
    None => None
    Some(hd) => {
      let w0 = hd[0].z
      if w0 == 0.0 {
        return None
      }
      let out : Array[@f64.DVec2] = []
      out.push(@f64.DVec2::new(hd[0].x / w0, hd[0].y / w0))
      for k = 1; k <= d; k = k + 1 {
        let mut nx = hd[k].x
        let mut ny = hd[k].y
        for i = 1; i <= k; i = i + 1 {
          let coeff = binom(k, i).to_double() * hd[i].z
          nx = nx - coeff * out[k - i].x
          ny = ny - coeff * out[k - i].y
        }
        out.push(@f64.DVec2::new(nx / w0, ny / w0))
      }
      Some(out)
    }
  }
}

///|
pub fn NurbsCurve2::insert_knot(
  self : NurbsCurve2,
  u : Double,
  r? : Int = 1,
) -> NurbsCurve2? {
  match self.bspline.insert_knot(u, r=r) {
    None => None
    Some(b) => Some({ bspline: b })
  }
}

///|
pub fn NurbsCurve2::split(
  self : NurbsCurve2,
  u : Double,
) -> (NurbsCurve2, NurbsCurve2)? {
  match self.bspline.split(u) {
    None => None
    Some((a, b)) => Some(({ bspline: a }, { bspline: b }))
  }
}