Class KDTreeUtils
- java.lang.Object
-
- net.imglib2.kdtree.KDTreeUtils
-
final class KDTreeUtils extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classKDTreeUtils.MakeTree
-
Field Summary
Fields Modifier and Type Field Description (package private) static intMAX_ARRAY_SIZE
-
Constructor Summary
Constructors Modifier Constructor Description privateKDTreeUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static voidcomputeMinMax(double[][] positions, double[] min, double[] max)Compute bounding box of positions in nested layout(package private) static voidcomputeMinMax(double[] flatPositions, double[] min, double[] max)Compute bounding box of positions in flat layout(package private) static double[]flatten(double[][] positions)Flatten the nestedpositionsarray.(package private) static intgetNumDimensions(java.lang.Iterable<? extends RealLocalizable> positions)Returns the number of dimensions of the first element ofpositions.(package private) static <T> TgetType(java.lang.Iterable<T> values)Returns the first element ofvalues.(package private) static double[][]initPositions(int numDimensions, int numPoints, java.lang.Iterable<? extends RealLocalizable> points)Copy the coordinates of the givenpointsto a newdouble[][] positionsarray.(package private) static int[]invert(int[] tree)Invert the given permutationtree.(package private) static intleftChildIndex(int i)If the tree is flattened into an array the left child of node at indexihas index2 * i + 1.(package private) static int[]makeTree(double[][] positions)Sort the given points into a k-d tree.(package private) static <T extends NativeType<T>>
Img<T>orderValuesImg(int[] invtree, java.lang.Iterable<T> values)Re-order the nodevaluesto form a tree corresponding to the index arraytree'={0,1,2,...}.(package private) static <T> java.util.List<T>orderValuesList(int[] invtree, java.lang.Iterable<T> values)Re-order the nodevaluesto form a tree corresponding to the index arraytree'={0,1,2,...}.(package private) static intparentIndex(int i)If the tree is flattened into an array the parent of node at indexihas index(i - 1) / 2(except for the root nodei==0).(package private) static intpartition(int i, int j, double[] values, int[] order)Partition a sublist.(package private) static voidquicksort(int i, int j, double[] values, int[] order)Sort a sublist.(package private) static double[][]reorder(double[][] positions, int[] tree)Re-order the nodepositionsto form a tree corresponding to the index arraytree'={0,1,2,...}.(package private) static double[]reorder(double[] values, int[] order)Create a newdouble[]array that contains the elements ofvalues, ordered such thatvalues[order[i]]is at indexi.(package private) static int[]reorder(int[] values, int[] order)Create a newint[]array that contains the elements ofvalues, ordered such thatvalues[order[i]]is at indexi.(package private) static double[]reorderToFlatLayout(double[][] positions, int[] tree)Re-order the nodepositionsto form a tree corresponding to the index arraytree={0,1,2,...}.(package private) static intrightChildIndex(int i)If the tree is flattened into an array the right child of node at indexihas index2 * i + 2.private static voidswap(int i, int j, int[] order)Swaporder[i]andorder[j].(package private) static double[][]unflatten(double[] positions, int n)Transform flatpositionsarray into a nesteddouble[numDimensions][numPoints]array.
-
-
-
Field Detail
-
MAX_ARRAY_SIZE
static final int MAX_ARRAY_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
leftChildIndex
static int leftChildIndex(int i)
If the tree is flattened into an array the left child of node at indexihas index2 * i + 1.
-
rightChildIndex
static int rightChildIndex(int i)
If the tree is flattened into an array the right child of node at indexihas index2 * i + 2.
-
parentIndex
static int parentIndex(int i)
If the tree is flattened into an array the parent of node at indexihas index(i - 1) / 2(except for the root nodei==0).
-
initPositions
static double[][] initPositions(int numDimensions, int numPoints, java.lang.Iterable<? extends RealLocalizable> points)Copy the coordinates of the givenpointsto a newdouble[][] positionsarray. The coordinate in dimensiondof theith point is stored atpositions[d][i]. That is,positions[0]has all X coordinates,positions[0]has all Y coordinates, and so on.
-
makeTree
static int[] makeTree(double[][] positions)
Sort the given points into a k-d tree.The tree is given as a flat (heap-like) array of point indices,
int[] tree. The index of the point chosen as the root node istree[0]. The coordinates of the root node are atpositions[d][tree[0]].The indices of the children (less-or-equal and greater-or-equal, respectively) of root are at
tree[1]andtree[2], and so on.- Parameters:
positions- The coordinates for theith point are stored atpositions[d][i]wheredis the dimension. SeeinitPositions(int, int, Iterable).- Returns:
- flattened tree of point indices
-
reorder
static double[][] reorder(double[][] positions, int[] tree)Re-order the nodepositionsto form a tree corresponding to the index arraytree'={0,1,2,...}.- Parameters:
positions-tree-- Returns:
-
reorder
static double[] reorder(double[] values, int[] order)Create a newdouble[]array that contains the elements ofvalues, ordered such thatvalues[order[i]]is at indexi.
-
reorder
static int[] reorder(int[] values, int[] order)Create a newint[]array that contains the elements ofvalues, ordered such thatvalues[order[i]]is at indexi.
-
reorderToFlatLayout
static double[] reorderToFlatLayout(double[][] positions, int[] tree)Re-order the nodepositionsto form a tree corresponding to the index arraytree={0,1,2,...}. Then flatten the result into a 1-D array, interleaving coordinates in all dimensions.- Parameters:
positions-tree-- Returns:
-
flatten
static double[] flatten(double[][] positions)
Flatten the nestedpositionsarray.- Parameters:
positions- positions in nested layout- Returns:
- positions in flattened layout
-
unflatten
static double[][] unflatten(double[] positions, int n)Transform flatpositionsarray into a nesteddouble[numDimensions][numPoints]array.With flat layout, positions are stored as a flat
double[]array, wherepositions[d + i*n]is dimensiondof thei-th point, withnthe number of dimensions.With nested layout, positions are stored as a nested
double[][]array wherepositions[d][i]is dimensiondof thei-th point.- Parameters:
positions- positions in flattened layoutn- number of dimensions- Returns:
- positions in nested layout
-
computeMinMax
static void computeMinMax(double[][] positions, double[] min, double[] max)Compute bounding box of positions in nested layout
-
computeMinMax
static void computeMinMax(double[] flatPositions, double[] min, double[] max)Compute bounding box of positions in flat layout
-
invert
static int[] invert(int[] tree)
Invert the given permutationtree.For example,
tree = {3, 4, 1, 0, 5, 2}indicates that coordinates and value for the node at heap indexican be found at indextree[i]in the respective input list.The inverse,
inv = {3, 4, 1, 0, 5, 2}indicates that coordinates and value at indexiin the respective input list belong to the node at heap indexinv[i].- Parameters:
tree- a permutation- Returns:
- the inverse permutation
-
orderValuesList
static <T> java.util.List<T> orderValuesList(int[] invtree, java.lang.Iterable<T> values)Re-order the nodevaluesto form a tree corresponding to the index arraytree'={0,1,2,...}. The tree is given as aninverted permutation, so that we can iterate through thevaluesin order, putting each at the right index in the returnedList.- Type Parameters:
T-- Parameters:
invtree-values-- Returns:
-
orderValuesImg
static <T extends NativeType<T>> Img<T> orderValuesImg(int[] invtree, java.lang.Iterable<T> values)
Re-order the nodevaluesto form a tree corresponding to the index arraytree'={0,1,2,...}. The tree is given as aninverted permutation, so that we can iterate through thevaluesin order, putting each at the right index in the returned 1DImg.- Type Parameters:
T-- Parameters:
invtree-values-- Returns:
-
getType
static <T> T getType(java.lang.Iterable<T> values)
Returns the first element ofvalues.- Throws:
java.lang.IllegalArgumentException- ifvalueshas no elements.
-
getNumDimensions
static int getNumDimensions(java.lang.Iterable<? extends RealLocalizable> positions)
Returns the number of dimensions of the first element ofpositions. Ifpositionshas no elements, returns0.- Parameters:
positions- list of points- Returns:
- number of dimensions of the first point
-
swap
private static void swap(int i, int j, int[] order)Swaporder[i]andorder[j].
-
partition
static int partition(int i, int j, double[] values, int[] order)Partition a sublist. The list is given by an immutable array ofvalues, and an index array that represents theorderof values in the list. This method only rearranges theorderarray.A pivot element is chosen by median-of-three method. Then
[i,j]is reordered, such that all elements before the pivot are smaller-equal and all elements after the pivot are larger-equal the pivot. The index of the pivot element is returned.- Parameters:
i- index of first element of the sublistj- index of last element of the sublistvalues- the array of values of list elements.order- order of list elements. E.g.,order[0]=3means thatvalues[3]is the first element of the list.- Returns:
- index of pivot element
-
quicksort
static void quicksort(int i, int j, double[] values, int[] order)Sort a sublist. The list is given by an immutable array ofvalues, and an index array that represents theorderof values in the list. This method only rearranges theorderarray.- Parameters:
i- index of first element of the sublistj- index of last element of the sublistvalues- the array of values of list elements.order- order of list elements. E.g.,order[0]=3means thatvalues[3]is the first element of the list.
-
-