///|
// Native smooth B-rep for a general polygonal profile swept along a path.
//
// Every profile edge becomes one ruled B-spline surface: degree 1 across the
// edge (between the two vertex curves) and cubic along the path, with one
// cubic Bezier span per sampled path point. Adjacent surfaces share their
// vertex curve edges, and the start/end caps are planar faces built from the
// profile polygon, so the shell stays watertight. A loop that keeps the
// orientation of the first path point (straight_holes / straight_outline)
// produces straight vertex curves, so its side surfaces are exact planes.

///|
/// smooth_sweep_curve builds the B-spline curve traced by one profile vertex
/// along the whole path. One cubic Bezier span connects every pair of
/// consecutive path points, so the surface interpolates every sampled
/// cross-section exactly.
fn smooth_sweep_curve(
  ctx : StepContext,
  pts : Array[Vec3],
  num_path_pts : Int32,
  num_prof_verts : Int32,
  profile_index : Int32,
  v_mults : Array[Int32],
  v_knots : Array[String],
  sharp_creases : Array[Int32],
) -> SmoothCurve {
  let indices : Array[Int32] = Array::make(num_path_pts, 0)
  for i in 0.. Array[@step.Ref] {
  let xform = node_transform(node, parent)
  let path = s.path
  let profile = s.profile
  let num_path_pts = path.points.length()
  if num_path_pts < 2 || profile.vertices.length() < 3 {
    return []
  }

  // Profile loops: outer boundary first, then any holes.
  let loops : Array[Array[Vec3]] = [
    profile.vertices,
    ..profile.holes.filter(fn(h) { h.length() >= 3 }),
  ]
  let num_loops = loops.length()

  // Profile-local coordinates (the same mapping the faceted sweep uses).
  let prof_z = v_norm(profile.normal_dir)
  let prof_x = v_norm(profile.right_dir)
  let prof_y = v_norm(v_cross(prof_z, prof_x))
  let m_global_to_prof = Matrix3x3::{
    m00: prof_x.0,
    m01: prof_x.1,
    m02: prof_x.2,
    m10: prof_y.0,
    m11: prof_y.1,
    m12: prof_y.2,
    m20: prof_z.0,
    m21: prof_z.1,
    m22: prof_z.2,
  }
  let local_loops = loops.map(fn(lp) {
    lp.map(fn(v) { apply_matrix(m_global_to_prof, v_sub(v, profile.center)) })
  })
  let loop_offsets : Array[Int32] = []
  let local_verts : Array[Vec3] = []
  let is_hole : Array[Bool] = []
  let mut loop_index = 0
  for lp in local_loops {
    loop_offsets.push(local_verts.length())
    for v in lp {
      local_verts.push(v)
      is_hole.push(loop_index > 0)
    }
    loop_index = loop_index + 1
  }
  let num_prof_verts = local_verts.length()

  // Holes and/or the outer boundary can optionally stay parallel to the sweep
  // axis instead of rotating with the path frame.
  let t0 = v_norm(path.tangents[0])
  let n0 = v_norm(path.normals[0])
  let r0 = v_norm(v_cross(t0, n0))

  // World positions of every profile vertex at every path point.
  let pts : Array[Vec3] = []
  for i in 0.. Unit {
    let key = if a < b { "\{a}:\{b}" } else { "\{b}:\{a}" }
    if seen.get(key).unwrap_or(false) {
      return
    }
    seen.set(key, true)
    let start = if a < b { a } else { b }
    let end = if a < b { b } else { a }
    let dir_raw = v_norm(v_sub(pts[end], pts[start]))
    let dir_ref = ctx.direction(dir_raw.0, dir_raw.1, dir_raw.2)
    let vec_ref = ctx.vector(dir_ref, 1.0)
    let line_ref = ctx.line(cp_refs[start], vec_ref)
    let ec_ref = ctx.edge_curve(vp_refs[start], vp_refs[end], line_ref, true)
    edges.push(EdgeInfo::{ start, end, edge: ec_ref, })
  }

  let last = num_path_pts - 1
  let offset_last = last * num_prof_verts
  for base in [0, offset_last] {
    for li in 0.. Array[Int32] {
    let lo = loop_offsets[li]
    let n = local_loops[li].length()
    let cycle = Array::make(n, 0)
    for j in 0..