// Recursively build a tree by separating points at plane boundaries. static KdTree MakeFromPointsInner( int depth, int stIndex, int enIndex, Vector3[] points, int[] inds, int dimensions, KdTree tree) { KdTree root = tree != null ? tree : new KdTree(dimensions); root.enabled = true; if (depth == 0 && root.rootPoints != points) { root.rootPoints = points; root.rootIndices = inds; } root.axis = depth % root.numDims; int splitPoint = FindPivotIndex(points, inds, stIndex, enIndex, root.axis); root.pivotIndex = inds[splitPoint]; root.pivot = points[root.pivotIndex]; int leftEndIndex = splitPoint - 1; if (leftEndIndex >= stIndex) { root.lr[0] = MakeFromPointsInner(depth + 1, stIndex, leftEndIndex, points, inds, dimensions, root.lr[0]); } else if (root.lr[0] != null) { root.lr[0].enabled = false; } int rightStartIndex = splitPoint + 1; if (rightStartIndex <= enIndex) { root.lr[1] = MakeFromPointsInner(depth + 1, rightStartIndex, enIndex, points, inds, dimensions, root.lr[1]); } else if (root.lr[1] != null) { root.lr[1].enabled = false; } return(root); }
/// <summary> /// Rebalance the tree. /// </summary> public static void Rebalance(KdTree root) { Regenerate(root, root.numDims, root.rootPoints); }