/// <summary> /// Splits the octree into eight children. /// </summary> void Split() { float quarter = SideLength / 4f; float newLength = SideLength / 2; children = new PointOctreeNode <T> [8]; children[0] = new PointOctreeNode <T>(newLength, minSize, Center + new Vector3(-quarter, quarter, -quarter)); children[1] = new PointOctreeNode <T>(newLength, minSize, Center + new Vector3(quarter, quarter, -quarter)); children[2] = new PointOctreeNode <T>(newLength, minSize, Center + new Vector3(-quarter, quarter, quarter)); children[3] = new PointOctreeNode <T>(newLength, minSize, Center + new Vector3(quarter, quarter, quarter)); children[4] = new PointOctreeNode <T>(newLength, minSize, Center + new Vector3(-quarter, -quarter, -quarter)); children[5] = new PointOctreeNode <T>(newLength, minSize, Center + new Vector3(quarter, -quarter, -quarter)); children[6] = new PointOctreeNode <T>(newLength, minSize, Center + new Vector3(-quarter, -quarter, quarter)); children[7] = new PointOctreeNode <T>(newLength, minSize, Center + new Vector3(quarter, -quarter, quarter)); }
// #### PRIVATE METHODS #### /// <summary> /// Grow the octree to fit in all objects. /// </summary> /// <param name="direction">Direction to grow.</param> void Grow(Vector3 direction) { int xDirection = direction.x >= 0 ? 1 : -1; int yDirection = direction.y >= 0 ? 1 : -1; int zDirection = direction.z >= 0 ? 1 : -1; PointOctreeNode <T> oldRoot = rootNode; float half = rootNode.SideLength / 2; float newLength = rootNode.SideLength * 2; Vector3 newCenter = rootNode.Center + new Vector3(xDirection * half, yDirection * half, zDirection * half); // Create a new, bigger octree root node rootNode = new PointOctreeNode <T>(newLength, minSize, newCenter); // Create 7 new octree children to go with the old root as children of the new root int rootPos = GetRootPosIndex(xDirection, yDirection, zDirection); PointOctreeNode <T>[] children = new PointOctreeNode <T> [8]; for (int i = 0; i < 8; i++) { if (i == rootPos) { children[i] = oldRoot; } else { xDirection = i % 2 == 0 ? -1 : 1; yDirection = i > 3 ? -1 : 1; zDirection = (i < 2 || (i > 3 && i < 6)) ? -1 : 1; children[i] = new PointOctreeNode <T>(rootNode.SideLength, minSize, newCenter + new Vector3(xDirection * half, yDirection * half, zDirection * half)); } } // Attach the new children to the new root node rootNode.SetChildren(children); }
/// <summary> /// Shrink the octree if possible, else leave it the same. /// </summary> void Shrink() { rootNode = rootNode.ShrinkIfPossible(initialSize); }