/// <summary> /// Initializes a new instance of the <see cref="Spatial3DTree{T}" /> class. /// </summary> /// <param name="size">The initial size of the root.</param> /// <param name="center">The center of the root.</param> /// <param name="allowDuplicates">A value indicating whether duplicate elements are detected and duplicate entries are prevented when adding or moving items (Two items are duplicates if value and position are equal)</param> public Spatial3DTree(Vector3 size, Vector3 center, bool allowDuplicates = true) { this.allowDuplicates = allowDuplicates; this.initialSize = size; this.center = center; root = Spatial3DCell <T> .GetCell(this.center - initialSize / 2f, initialSize, false); }
/// <summary> /// The clear. /// </summary> public void Clear() { version++; Spatial3DCell <T> .Pool(root); root = Spatial3DCell <T> .GetCell(center - initialSize / 2, initialSize); }
private static void DrawTreeCellGizmos <T>(Spatial3DCell <T> cell, int currentDepth) { Gizmos.color = LevelColors[Mathf.Clamp(currentDepth, 0, 15)]; Gizmos.DrawWireCube(cell.Center, cell.Size); if (cell.Children != null) { foreach (var child in cell.Children) { if (child != null) { DrawTreeCellGizmos(child, currentDepth + 1); } } } }
/// <summary> /// The shrink. /// </summary> private void Shrink() { Debug.Assert(CanShrink(), "The tree cannot be shrunk."); if (root.Children == null) { var sc = Spatial3DCell <T> .SubdivisionAmount; root.Size = root.Size / sc; root.Start = root.Start + root.Size * (sc / 2); } else { var middle = root.Children.Length / 2; var center = root.Children[middle]; root.Children[middle] = null; Spatial3DCell <T> .Pool(root); root = center; Debug.Assert(center != null); } }
/// <summary> /// The grow. /// </summary> private void Grow() { const int sc = Spatial3DCell <T> .SubdivisionAmount; const int scCenter = sc / 2; var start = root.Start - scCenter * root.Size; var size = root.Size * sc; if (root.Children == null) { root.Start = start; root.Size = size; } else { var newRoot = Spatial3DCell <T> .GetCell(start, size, true); newRoot.TotalItemAmount = root.TotalItemAmount; newRoot.Children[newRoot.Children.Length / 2] = root; root = newRoot; } }
/// <summary> /// Initializes a new instance of the <see cref="PathEntry" /> struct. /// </summary> /// <param name="cell"> /// The cell. /// </param> /// <param name="index"> /// The index. /// </param> public PathEntry(Spatial3DCell <T> cell, int index, bool fullyInside) { Cell = cell; Index = index; FullyInside = fullyInside; }
public PathEntry(Spatial3DCell <T> cell, int index) { Cell = cell; Index = index; }
/// <summary> /// Initializes a new instance of the <see cref="PathEntry" /> struct. /// </summary> /// <param name="cell"> /// The cell. /// </param> /// <param name="childIndex"> /// The childIndex. /// </param> public PathEntry(Spatial3DCell <T> cell, int childIndex, bool flag) { Cell = cell; ChildIndex = childIndex; Flag = flag; }
/// <summary> /// Initializes a new instance of the <see cref="Spatial3DTree{T}" /> class. /// </summary> /// <param name="size">The initial size of the root.</param> /// <param name="center">The center of the root.</param> public Spatial3DTree(Vector3 size, Vector3 center) { initialSize = size; this.center = center; root = Spatial3DCell <T> .GetCell(this.center - initialSize / 2f, initialSize, false); }