private void ExpandRecursively(QtNode <T> node, ref int i) { if (node.Depth == MaxDepth) { return; } int depth = node.Depth + 1; float size = node.Size / 2f; for (int x = 0; x < 2; x++) { for (int y = 0; y < 2; y++) { var childCoord = node.Coord * 2 + new IntVector2(x, y); var childPosition = node.Position + new Vector2(size * x, size * y); var child = CreateNode(ref i, depth, childCoord, childPosition, size); int localChildIndex = x * 2 + y; node.Children[localChildIndex] = i - 1; ExpandRecursively(child, ref i); } } }
public QuadTree(Vector2 posMin, int size, int patchSize, Func <QtNode <T>, T> constructor) { //log2(1024*8/32) int numPatchesAtLod0 = size / patchSize; if (!Mathf.IsPowerOfTwo(numPatchesAtLod0)) { throw new ArgumentException("Ratio of size/patchSize must be power-of-two"); } MaxDepth = (int)Math.Log(numPatchesAtLod0, 2); _constructor = constructor; int totalNodes = CalculateNumNodes(MaxDepth); Debug.Log( "QuadTree || Depth: " + MaxDepth + ", Total Nodes: " + totalNodes + ", Total Mem: " + totalNodes * QtNode <T> .SizeBytes / 1024 + "KB"); _nodes = new QtNode <T> [totalNodes]; int i = 0; QtNode <T> root = CreateNode(ref i, 0, IntVector2.Zero, posMin, size); ExpandRecursively(root, ref i); }
private QtNode <T> CreateNode(ref int i, int depth, IntVector2 childCoord, Vector2 childPosition, float size) { var child = new QtNode <T>(depth, childCoord, childPosition, size); child.Value = _constructor(child); _nodes[i++] = child; return(child); }