/// <summary> /// Grow the octree to fit in all objects. /// </summary> /// <param name="direction">Direction to grow.</param> public void Grow(Vector3 direction) { sbyte xDirection = direction.x >= 0 ? (sbyte)1 : (sbyte)-1; sbyte yDirection = direction.y >= 0 ? (sbyte)1 : (sbyte)-1; sbyte zDirection = direction.z >= 0 ? (sbyte)1 : (sbyte)-1; PointOctreeNode <T> oldRoot = RootNode; var half = RootNode.SideLength / 2; var newLength = RootNode.SideLength * 2; var newCenter = RootNode.Center + new Vector3(xDirection * half, yDirection * half, zDirection * half); RootNode = new PointOctreeNode <T>(newLength, MinSize, newCenter); // Create 7 new octree children to go with the old root as children of the new root... var rootPos = GetRootPosIndex(xDirection, yDirection, zDirection); PointOctreeNode <T>[] children = new PointOctreeNode <T> [8]; for (var i = 0; i < 8; i++) { if (i == rootPos) { children[i] = oldRoot; } else { xDirection = i % 2 == 0 ? (sbyte)-1 : (sbyte)1; yDirection = i > 3 ? (sbyte)-1 : (sbyte)1; zDirection = (i < 2 || (i > 3 && i < 6)) ? (sbyte)-1 : (sbyte)1; children[i] = new PointOctreeNode <T>(RootNode.SideLength, MinSize, newCenter + new Vector3(xDirection * half, yDirection * half, zDirection * half)); } } RootNode.SetChildren(ref children); }
/// <summary> /// Shrink the octree if possible, else leave it the same. /// </summary> public void Shrink() { RootNode = RootNode.ShrinkIfPossible(InitialSize); }