insert method

bool insert(
  1. AABB aabb,
  2. int elementData, [
  3. int level = 0
])

Insert data into this node @return True if successful, otherwise false

Implementation

bool insert(AABB aabb, int elementData, [int level = 0]){
  final nodeData = data;

  // Ignore objects that do not belong in this node
  if (!this.aabb.contains(aabb)) {
    return false; // object cannot be added
  }

  final children = this.children;
  final maxDepth = this.maxDepth ?? root!.maxDepth;//(this as any).maxDepth ?? (root! as any).maxDepth;

  if (level < maxDepth!) {
    // Subdivide if there are no children yet
    bool subdivided = false;
    if (children.isEmpty) {
      subdivide();
      subdivided = true;
    }
    // add to whichever node will accept it
    for (int i = 0; i != 8; i++) {
      if (children[i].insert(aabb, elementData, level + 1)) {
        return false;
      }
    }

    if (subdivided) {
      // No children accepted! Might as well just remove em since they contain none
      children.clear();
    }
  }

  // Too deep, or children didnt want it. add it in current node
  nodeData.add(elementData);
  return true;
}