// Ported from Draco 1.5.7 (compression/: the kd-tree attributes decoder, the point cloud kd-tree
// and sequential decoders, the float points tree decoder, quantize_points_3.h): Apache License
// 2.0, Copyright 2016 The Draco Authors. See NOTICE.

///|
/// `KdTreeAttributesEncodingMethod`: `kKdTreeQuantizationEncoding` (the float tree of older
/// bitstreams) and `kKdTreeIntegerEncoding`.
const KD_TREE_QUANTIZATION : Int32 = 0

///|
const KD_TREE_INTEGER : Int32 = 1

///|
fn is_signed_type(dt : Int32) -> Bool {
  dt == DT_INT8 || dt == DT_INT16 || dt == DT_INT32
}

///|
fn is_unsigned_type(dt : Int32) -> Bool {
  dt == DT_UINT8 || dt == DT_UINT16 || dt == DT_UINT32
}

///|
/// `ConvertSymbolToSignedInt`.
fn signed_of_symbol(v : Int32) -> Int32 {
  let half = (v.reinterpret_as_uint() >> 1).reinterpret_as_int()
  if (v & 1) == 0 {
    half
  } else {
    -half - 1
  }
}

///|
/// `PointAttribute::Reset(n)` with an identity mapping: false past `MAX_POINTS` or
/// `MAX_ATTRIBUTE_BYTES`.
fn Attribute::reset(self : Attribute, n : Int64) -> Bool {
  let bytes = n * self.stride().to_int64()
  guard n >= 0L &&
    n <= MAX_POINTS.to_int64() &&
    bytes <= MAX_ATTRIBUTE_BYTES.to_int64() else {
    return false
  }
  self.values = FixedArray::make(bytes.to_int(), b'\x00')
  self.size = n.to_int()
  self.map = None
  true
}

///|
/// A float attribute's quantization (`AttributeQuantizationTransform`'s parameters).
priv struct Quantization {
  min_values : FixedArray[Float]
  range : Float
  bits : Int32
}

///|
/// `KdTreeAttributesDecoder::DecodeAttributes`: its portable attributes, the data of their
/// transforms, then the attributes in their own types.
fn Decoder::decode_kd_attributes(self : Decoder, d : AttributesDecoder) -> Bool {
  let b = self.b
  guard b.version >= bitstream(2, 3) else { return self.decode_legacy_kd(d) }
  let level = b.u8()
  guard !b.failed else { return false }
  let n = self.num_points
  let targets = []
  let mut dim = 0
  let mut signed_components = 0
  for att in d.ids {
    let a = self.attributes[att]
    guard a.reset(n.to_int64()) else { return false }
    let dt = a.data_type
    if dt == DT_FLOAT32 {
      guard n.to_int64() * a.num_components.to_int64() * 4L <=
        MAX_ATTRIBUTE_BYTES.to_int64() else {
        return false
      }
      let ints = FixedArray::make(n * a.num_components, 0)
      a.portable = Some(ints)
      a.portable_components = a.num_components
      targets.push({
        bytes: [],
        ints,
        portable: true,
        offset: dim,
        data_size: 4,
        nc: a.num_components,
        size: n,
      })
    } else if is_signed_type(dt) || is_unsigned_type(dt) {
      if is_signed_type(dt) {
        signed_components += a.num_components
      }
      targets.push({
        bytes: a.values,
        ints: [],
        portable: false,
        offset: dim,
        data_size: data_type_length(dt),
        nc: a.num_components,
        size: n,
      })
    } else {
      return false
    }
    dim += a.num_components
  }
  guard level <= 6 else { return false }
  let out = KdOut::to_attributes(targets)
  guard decode_kd_points(b, level, dim, n.reinterpret_as_uint(), out) &&
    out.count == n.to_int64() else {
    return false
  }
  let quantizations = []
  for att in d.ids {
    let a = self.attributes[att]
    if a.data_type == DT_FLOAT32 {
      let nc = a.num_components
      guard b.has(4 * nc) else { return false }
      let min_values = FixedArray::makei(nc, fn(_) { b.f32() })
      let range = b.f32()
      guard !b.failed else { return false }
      let bits = b.u8()
      guard !b.failed && bits >= 1 && bits <= 30 else { return false }
      quantizations.push({ min_values, range, bits, })
    }
  }
  let min_signed = FixedArray::make(signed_components, 0)
  for i in 0.. Bool {
  let mut q = 0
  let mut s = 0
  for att in d.ids {
    let a = self.attributes[att]
    let nc = a.num_components
    if is_signed_type(a.data_type) {
      let len = data_type_length(a.data_type)
      for i in 0..<(a.size * nc) {
        let mut u = 0
        for k in 0..= 0 else { return false }
        let v = u + min_signed[s + i % nc]
        for k in 0..> (8 * k)).to_byte()
        }
      }
      s += nc
    } else if a.data_type == DT_FLOAT32 {
      let t = quantizations[q]
      q += 1
      guard a.portable is Some(src) else { return false }
      let delta = t.range / Float::from_int((1 << t.bits) - 1)
      for i in 0..<(a.size * nc) {
        let value = Float::from_int(src[i]) * delta
        let value = value + t.min_values[i % nc]
        store_u32(a.values, 4 * i, value.reinterpret_as_int())
      }
    }
  }
  true
}

///|
/// The kd-tree attributes of bitstreams before 2.3 (`DecodeDataNeededByPortableTransforms`):
/// three float coordinates by `FloatPointsTreeDecoder`, or integers by the kd-tree.
fn Decoder::decode_legacy_kd(self : Decoder, d : AttributesDecoder) -> Bool {
  let b = self.b
  let mut dim = 0
  for att in d.ids {
    let a = self.attributes[att]
    guard data_type_length(a.data_type) <= 4 else { return false }
    dim += a.num_components
  }
  let first = self.attributes[d.ids[0]]
  first.map = None
  let encoding = b.u8()
  guard !b.failed else { return false }
  if encoding == KD_TREE_QUANTIZATION {
    guard d.ids.length() == 1 && first.num_components == 3 else { return false }
    let _ = b.u8()
    guard !b.failed else { return false }
    let num_points = b.u32().reinterpret_as_uint()
    guard !b.failed else { return false }
    guard first.reset(num_points.to_uint64().reinterpret_as_int64()) else {
      return false
    }
    decode_float_tree(b, first, num_points)
  } else if encoding == KD_TREE_INTEGER {
    let level = b.u8()
    guard !b.failed && level <= 6 else { return false }
    let num_points = b.u32().reinterpret_as_uint()
    guard !b.failed else { return false }
    let targets = []
    let mut offset = 0
    for att in d.ids {
      let a = self.attributes[att]
      guard a.reset(num_points.to_uint64().reinterpret_as_int64()) else {
        return false
      }
      targets.push({
        bytes: a.values,
        ints: [],
        portable: false,
        offset,
        data_size: data_type_length(a.data_type),
        nc: a.num_components,
        size: a.size,
      })
      offset += a.num_components
    }
    decode_kd_points(b, level, dim, 0xffffffffU, KdOut::to_attributes(targets))
  } else {
    false
  }
}

///|
/// `FloatPointsTreeDecoder::DecodePointCloud` into `att` (its first `size` points): the points
/// quantized around zero, then `DequantizePoints3`, each stored as the first bytes of its three
/// floats that the attribute's value takes.
fn decode_float_tree(
  b : Buffer,
  att : Attribute,
  header_points : UInt32,
) -> Bool {
  let version = b.u32()
  guard !b.failed else { return false }
  if version == 3 {
    let encoding = b.i8()
    guard !b.failed && encoding == 1 else { return false }
  } else {
    guard version == 2 else { return false }
  }
  let bits = b.u32().reinterpret_as_uint()
  guard !b.failed && bits <= 31 else { return false }
  let range = b.f32()
  guard !b.failed else { return false }
  let num_points = b.u32().reinterpret_as_uint()
  guard !b.failed else { return false }
  guard header_points == 0 || num_points == header_points else { return false }
  let level = b.u32().reinterpret_as_uint()
  guard !b.failed && level <= 6 else { return false }
  let out = KdOut::collecting(num_points.to_uint64().reinterpret_as_int64())
  if num_points > 0 {
    guard num_points <= MAX_POINTS.reinterpret_as_uint() else { return false }
    let _ = decode_kd_points(b, level.reinterpret_as_int(), 3, 0xffffffffU, out)
    guard !out.over else { return false }
    b.failed = false
  }
  guard out.count == out.limit else { return false }
  let max = (1U << bits.reinterpret_as_int()) - 1
  let delta = if max > 0 {
    range / Float::from_int(max.reinterpret_as_int())
  } else {
    1.0
  }
  let stride = att.stride()
  let floats = FixedArray::make(12, b'\x00')
  let count = if out.count < att.size.to_int64() {
    out.count.to_int()
  } else {
    att.size
  }
  for i in 0.. Bool {
  let b = self.b
  let num_points = b.u32()
  guard !b.failed && num_points >= 0 && num_points <= MAX_POINTS else {
    return false
  }
  self.num_points = num_points
  self.kd = encoding == KD_TREE_ENCODING
  self.decode_point_attributes()
}