subdivide method

void subdivide()

Create 8 equally sized children nodes and put them in the children array

Implementation

void subdivide(){
  final aabb = this.aabb;
  final l = aabb.lowerBound;
  final u = aabb.upperBound;

  final children = this.children;

  children.addAll([
    OctreeNode(aabb: AABB(lowerBound: Vec3(0, 0, 0))),
    OctreeNode(aabb: AABB(lowerBound: Vec3(1, 0, 0))),
    OctreeNode(aabb: AABB(lowerBound: Vec3(1, 1, 0))),
    OctreeNode(aabb: AABB(lowerBound: Vec3(1, 1, 1))),
    OctreeNode(aabb: AABB(lowerBound: Vec3(0, 1, 1))),
    OctreeNode(aabb: AABB(lowerBound: Vec3(0, 0, 1))),
    OctreeNode(aabb: AABB(lowerBound: Vec3(1, 0, 1))),
    OctreeNode(aabb: AABB(lowerBound: Vec3(0, 1, 0)))
  ]);

  u.vsub(l, halfDiagonal);
  halfDiagonal.scale(0.5, halfDiagonal);

  final root = this.root ?? this;

  for (int i = 0; i != 8; i++) {
    final child = children[i];

    // Set current node as root
    child.root = root;

    // Compute bounds
    final lowerBound = child.aabb.lowerBound;
    lowerBound.x *= halfDiagonal.x;
    lowerBound.y *= halfDiagonal.y;
    lowerBound.z *= halfDiagonal.z;

    lowerBound.vadd(l, lowerBound);

    // Upper bound is always lower bound + halfDiagonal
    lowerBound.vadd(halfDiagonal, child.aabb.upperBound);
  }
}