///|
/// A centered pad aperture. All parameters are in millimetres.
///
/// Gerber `C`, `R`, `O` and `P` apertures map directly onto these variants; the
/// same shapes are reused by the geometry checks so parsing and verification
/// share one area/containment implementation.
pub enum Shape {
  /// Radius.
  Circle(Double)
  /// Width and height.
  Rectangle(Double, Double)
  /// Width and height of the stadium.
  Obround(Double, Double)
  /// Circumradius and vertex count.
  Polygon(Double, Int32)
  /// A centered aperture under a rotation, reflection, or uniform scale.
  Transformed(Shape, Transform2D)
  /// Convex outline relative to the copper centroid and its rounding radius.
  RoundedOutline(Array[Point], Double)
  /// Solid shape, outer center, hole center (both relative to the copper
  /// centroid), and hole radius in mm.
  RoundHole(Shape, Point, Point, Double)
  /// A simple polygon relative to its centroid.
  Outline(Array[Point])
  /// Centred thermal: outer radius, inner radius and half gap.
  Thermal(Double, Double, Double)
  /// Solid shape and round holes, positioned relative to the copper centroid.
  Punched(Shape, Point, Array[(Point, Double)])
}

///|
pub fn Shape::area(self : Shape) -> Double {
  match self {
    Circle(radius) => @math.PI * radius * radius
    Rectangle(width, height) => width * height
    Obround(width, height) => {
      let long = width.max(height)
      let short = width.min(height)
      (long - short) * short + @math.PI * short * short / 4.0
    }
    Polygon(radius, sides) => {
      let count = sides.to_double()
      0.5 * count * radius * radius * @math.sin(2.0 * @math.PI / count)
    }
    RoundedOutline(vertices, radius) => rounded_outline_area(vertices, radius)
    Outline(vertices) => polygon_area(vertices).abs()
    Thermal(outer, inner, gap) =>
      4.0 * (thermal_quadrant(outer, gap) - thermal_quadrant(inner, gap))
    Punched(shape, _, holes) => {
      let mut area = shape.area()
      for hole in holes {
        area -= @math.PI * hole.1.max(0.0) * hole.1.max(0.0)
      }
      area
    }
    RoundHole(shape, _, _, radius) =>
      shape.area() - @math.PI * radius.max(0.0) * radius.max(0.0)
    Transformed(shape, matrix) => shape.area() * matrix.determinant().abs()
  }
}

///|
/// Radius of a circle enclosing this centered shape.
fn Shape::bounding_radius(self : Shape) -> Double {
  match self {
    Circle(radius) | Polygon(radius, _) => radius
    Rectangle(width, height) => @math.hypot(width * 0.5, height * 0.5)
    Obround(width, height) => width.max(height) * 0.5
    RoundedOutline(vertices, radius) => {
      let mut result = 0.0
      for p in vertices {
        result = result.max(@math.hypot(p.x, p.y))
      }
      result + radius
    }
    Transformed(shape, matrix) => shape.bounding_radius() * matrix.scale()
    RoundHole(shape, outer, _, _) =>
      shape.bounding_radius() + @math.hypot(outer.x, outer.y)
    Punched(shape, outer, _) => shape.bounding_radius() + magnitude(outer)
    Thermal(outer, _, _) => outer
    Outline(vertices) => {
      let mut radius = 0.0
      for p in vertices {
        radius = radius.max(magnitude(p))
      }
      radius
    }
  }
}

///|
/// Whether an entire disk lies within one of this aperture's transparent holes.
fn Shape::hole_contains(self : Shape, point : Point, margin : Double) -> Bool {
  match self {
    RoundHole(_, _, hole, radius) =>
      magnitude(difference(point, hole)) + margin < radius
    Punched(_, _, holes) =>
      holes.any(hole => magnitude(difference(point, hole.0)) + margin < hole.1)
    Transformed(shape, m) => {
      let det = m.determinant()
      det != 0.0 &&
      shape.hole_contains(
        {
          x: (m.e * point.x - m.b * point.y) / det,
          y: (m.a * point.y - m.d * point.x) / det,
        },
        margin / m.scale(),
      )
    }
    _ => false
  }
}

///|
pub struct GerberBounds {
  lower : Point
  upper : Point
}

///|
fn bounds_at(at : Point, radius : Double) -> GerberBounds {
  {
    lower: { x: at.x - radius, y: at.y - radius, },
    upper: { x: at.x + radius, y: at.y + radius, },
  }
}

///|
fn GerberBounds::union(
  self : GerberBounds,
  other : GerberBounds,
) -> GerberBounds {
  {
    lower: {
      x: self.lower.x.min(other.lower.x),
      y: self.lower.y.min(other.lower.y),
    },
    upper: {
      x: self.upper.x.max(other.upper.x),
      y: self.upper.y.max(other.upper.y),
    },
  }
}

///|
fn GerberBounds::overlaps(self : GerberBounds, other : GerberBounds) -> Bool {
  self.lower.x <= other.upper.x &&
  other.lower.x <= self.upper.x &&
  self.lower.y <= other.upper.y &&
  other.lower.y <= self.upper.y
}

///|
fn is_digit(ch : Char) -> Bool {
  ch >= '0' && ch <= '9'
}

///|
fn digit_value(ch : Char) -> Int32 {
  ch.to_int() - 48
}

///|
fn polygon_vertices(radius : Double, sides : Int32) -> Array[Point] {
  let vertices : Array[Point] = []
  if sides < 3 {
    return vertices
  }
  let step = 2.0 * @math.PI / sides.to_double()
  for i in 0.. Bool {
  let long = width.max(height)
  let short = width.min(height)
  let radius = short * 0.5
  let straight = long * 0.5 - radius
  let (along, across) = if width >= height { (dx, dy) } else { (dy, dx) }
  if along.abs() <= straight {
    return across.abs() <= radius
  }
  let center = if along > 0.0 { straight } else { -straight }
  let offset = along - center
  offset * offset + across * across <= radius * radius
}

///|
fn convex_contains(vertices : Array[Point], point : Point) -> Bool {
  if vertices.length() < 3 {
    return false
  }
  let mut sign = 0.0
  for i in 0.. Shape {
  match self {
    Circle(radius) => Circle(radius + amount)
    Rectangle(width, height) =>
      Rectangle(width + 2.0 * amount, height + 2.0 * amount)
    Obround(width, height) =>
      Obround(width + 2.0 * amount, height + 2.0 * amount)
    Polygon(radius, sides) => Polygon(radius + amount, sides)
    RoundedOutline(vertices, radius) =>
      RoundedOutline(vertices, radius + amount)
    RoundHole(shape, outer, hole, radius) =>
      RoundHole(shape.expanded(amount), outer, hole, radius - amount)
    Punched(shape, outer, holes) =>
      Punched(
        shape.expanded(amount),
        outer,
        holes.map(hole => (hole.0, hole.1 - amount)),
      )
    Thermal(_, _, _) | Outline(_) => self
    Transformed(shape, matrix) =>
      Transformed(shape.expanded(amount / matrix.scale()), matrix)
  }
}

///|
/// Whether `point` lies inside the shape centered at `center`.
pub fn Shape::contains(self : Shape, center : Point, point : Point) -> Bool {
  let dx = point.x - center.x
  let dy = point.y - center.y
  match self {
    Circle(radius) => dx * dx + dy * dy <= radius * radius
    Rectangle(width, height) =>
      dx.abs() <= width * 0.5 && dy.abs() <= height * 0.5
    Obround(width, height) => obround_contains(width, height, dx, dy)
    Polygon(radius, sides) => {
      let vertices = polygon_vertices(radius, sides)
      let moved = vertices.map(v => { x: v.x - dx, y: v.y - dy, })
      convex_contains(moved, { x: 0.0, y: 0.0, })
    }
    RoundedOutline(vertices, radius) =>
      rounded_outline_contains(vertices, radius, { x: dx, y: dy, })
    RoundHole(shape, outer, hole, radius) =>
      (radius <= 0.0 || @math.hypot(dx - hole.x, dy - hole.y) >= radius) &&
      shape.contains(outer, { x: dx, y: dy, })
    Punched(shape, outer, holes) => {
      let p : Point = { x: dx, y: dy, }
      shape.contains(outer, p) &&
      !holes.any(hole => magnitude(difference(p, hole.0)) < hole.1)
    }
    Thermal(outer, inner, gap) => {
      let radial = @math.hypot(dx, dy)
      dx.abs() >= gap && dy.abs() >= gap && radial >= inner && radial <= outer
    }
    Outline(vertices) => polygon_contains(vertices, { x: dx, y: dy, })
    Transformed(shape, matrix) => {
      let determinant = matrix.determinant()
      if determinant == 0.0 {
        return false
      }
      shape.contains({ x: 0.0, y: 0.0, }, {
        x: (matrix.e * dx - matrix.b * dy) / determinant,
        y: (matrix.a * dy - matrix.d * dx) / determinant,
      })
    }
  }
}

///|
/// Signed area of a closed polygon (shoelace formula). Positive for a
/// counter-clockwise ring.
pub fn polygon_area(points : Array[Point]) -> Double {
  let count = points.length()
  if count < 3 {
    return 0.0
  }
  let mut twice = 0.0
  for i in 0.. Point {
  let count = points.length()
  let mut area = 0.0
  let mut x = 0.0
  let mut y = 0.0
  for i in 0.. Bool {
  let count = points.length()
  if count < 3 {
    return false
  }
  let mut inside = false
  let mut j = count - 1
  for i in 0.. point.y) != (b.y > point.y) {
      let x = (b.x - a.x) * (point.y - a.y) / (b.y - a.y) + a.x
      if point.x < x {
        inside = !inside
      }
    }
    j = i
  }
  inside
}

///|
fn rounded_outline_area(vertices : Array[Point], radius : Double) -> Double {
  let mut perimeter = 0.0
  for i, a in vertices {
    let b = vertices[(i + 1) % vertices.length()]
    perimeter += @math.hypot(b.x - a.x, b.y - a.y)
  }
  polygon_area(vertices).abs() + perimeter * radius + @math.PI * radius * radius
}

///|
fn rounded_outline_contains(
  vertices : Array[Point],
  radius : Double,
  point : Point,
) -> Bool {
  if convex_contains(vertices, point) {
    return true
  }
  for i, a in vertices {
    let b = vertices[(i + 1) % vertices.length()]
    let dx = b.x - a.x
    let dy = b.y - a.y
    let length = @math.hypot(dx, dy)
    if length == 0.0 {
      continue
    }
    let ux = dx / length
    let uy = dy / length
    let px = point.x - a.x
    let py = point.y - a.y
    let along = (px * ux + py * uy).max(0.0).min(length)
    if @math.hypot(px - along * ux, py - along * uy) <= radius {
      return true
    }
  }
  false
}