///|
priv struct CalibrationConsensus {
  mask : Array[Bool]
  errors : Array[Double]
  count : Int32
  rms : Double
}

///|
fn require_robust_inputs(
  control_pixels : Array[@types.Point2],
  control_world : Array[@types.Point2],
  check_pixels : Array[@types.Point2],
  check_world : Array[@types.Point2],
  roi : Array[@types.Point2],
  max_fit_error_m : Double,
  max_check_error_m : Double,
  max_meters_per_pixel : Double,
  ground_noise_floor_m : Double,
) -> Unit raise @types.GroundError {
  if control_pixels.length() != control_world.length() ||
    control_pixels.length() < 6 ||
    control_pixels.length() > 4096 ||
    check_pixels.length() != check_world.length() ||
    check_pixels.length() < 2 ||
    check_pixels.length() > 4096 {
    raise @types.InvalidInput(
      "calibration needs 6 to 4096 control pairs and 2 to 4096 check pairs",
    )
  }
  if !@types.is_finite(max_fit_error_m) ||
    max_fit_error_m <= 0.0 ||
    !@types.is_finite(max_check_error_m) ||
    max_check_error_m <= 0.0 {
    raise @types.InvalidInput("residual limits must be finite and positive")
  }
  if !@types.is_finite(max_meters_per_pixel) || max_meters_per_pixel <= 0.0 {
    raise @types.InvalidInput(
      "metres-per-pixel limit must be finite and positive",
    )
  }
  let variance = ground_noise_floor_m * ground_noise_floor_m
  if !@types.is_finite(ground_noise_floor_m) ||
    ground_noise_floor_m <= 0.0 ||
    !@types.is_finite(variance) ||
    variance <= 0.0 {
    raise @types.InvalidInput(
      "ground noise floor must have finite positive variance",
    )
  }
  let domain = make_roi(roi)
  for i in 0.. CalibrationConsensus raise @types.GroundError {
  let errors : Array[Double] = []
  let mask : Array[Bool] = []
  let mut count = 0
  let mut maximum = 0.0
  for i in 0.. 0.0 {
    for i in 0.. Unit raise @types.GroundError {
  if consensus.count < 6 ||
    consensus.count.to_double() / consensus.mask.length().to_double() <
    options.min_inlier_ratio {
    raise @types.InvalidInput("robust calibration has insufficient consensus")
  }
}

///|
fn select_inlier_points(
  points : Array[@types.Point2],
  mask : Array[Bool],
) -> Array[@types.Point2] {
  let result : Array[@types.Point2] = []
  for i in 0.. Int32? {
  if length > 12 {
    return None
  }
  let mut count = 1
  for i in 1..<=6 {
    count = count * (length - 6 + i) / i
  }
  if count <= budget {
    Some(count)
  } else {
    None
  }
}

///|
fn next_six_combination(indices : Array[Int32], length : Int32) -> Unit {
  let mut index = 5
  while index >= 0 {
    if indices[index] < length - 6 + index {
      indices[index] += 1
      for next in (index + 1)..<6 {
        indices[next] = indices[next - 1] + 1
      }
      return
    }
    index -= 1
  }
}

///|
// Explicit little-endian Int32 followed by 28 zero bytes, independent of the
// host, locale and platform entropy. The pinned core supplies ChaCha8/shuffle.
fn robust_random(seed : Int32) -> @random.Rand {
  let bytes = FixedArray::make(32, b'\x00')
  for i in 0..<4 {
    bytes[i] = (seed.reinterpret_as_uint() >> (i * 8)).to_byte()
  }
  @random.Rand::chacha8(seed=Bytes::from_array(bytes))
}