Package com.irurueta.geometry
Class KDTree<P extends Point<P>>
java.lang.Object
com.irurueta.geometry.KDTree<P>
Implementation of a k-D tree in an arbitrary dimension.
Once a K-D tree is built for a collection of points, it can later be used to efficiently do certain operations
such as point location, nearest points searches, etc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classKDTree.BoxNode<P extends Point<P>>Contains a node of a KD Tree. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final doubleA very large value to consider as the maximum allowed coordinate value.protected KDTree.BoxNode<P>[]Array of boxes stored in this tree as its nodes.static final intMinimum number of allowed points to be stored in the tree.private static final intNumber of tasks that can be queued.private final intNumber of points stored by the tree.private final int[]Indices of points going from boxes in the tree to the input collection of points.private P[]Array of points containing input collection of points.private final int[]Indices of points going from input collection of points to boxes in the tree. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract PCopies a point.protected abstract PcreatePoint(double value) Creates a point.doubledistance(int jpt, int kpt) Gets distance between points located at provided positions on input collection.abstract intGets number of dimensions supported by this k-D tree implementation on provided list of points.private intlocate(int jpt) Gets position of point on input collection for provided internal boxes position.Gets smallest box containing provided point in the input list of points.intlocateBoxIndex(P pt) Gets position of smallest box containing provided point in the input list of points.intlocateNear(P pt, double r, int[] list, int nmax) Locates some near points to provided one up to a certain radius of search.intlocateNear(P pt, double r, P[] plist, int nmax) Locates near points to provided one up to a certain radius of search defined in a bounding box.intnearestIndex(P pt) Index in provided input list of points of closest point to provided one.nearestPoint(P pt) Closest point to provided one.voidnNearest(int jpt, int[] nn, double[] dn, int n) Gets n nearest point indices to a given one in the input collection.voidGets n nearest points to a given point index in the input collection.voidGets n nearest point indices to a given point in the input collection.voidGets n nearest points to a given point in the input collection.private static intselecti(int k, int indxOffset, int[] indx, int n, int arrOffset, double[] arr) Makes a selection so that we obtain ordered index at provided k position so that distances are ordered in such a way that resulting array arr contains distances as follows: arr[indx[0 .. k-1]] <= arr[indx[k]] <= arr[indx[k+1 .. n]].private static voidsiftDown(double[] heap, int[] ndx, int nn) Moves things around.private static voidswap(int[] a, int posA, int[] b, int posB) Swaps values.
-
Field Details
-
MIN_PTS
public static final int MIN_PTSMinimum number of allowed points to be stored in the tree.- See Also:
-
BIG
protected static final double BIGA very large value to consider as the maximum allowed coordinate value.- See Also:
-
N_TASKS
private static final int N_TASKSNumber of tasks that can be queued.- See Also:
-
boxes
Array of boxes stored in this tree as its nodes. -
ptIndx
private final int[] ptIndxIndices of points going from boxes in the tree to the input collection of points. Indices are sorted as the tree is built. -
rPtIndx
private final int[] rPtIndxIndices of points going from input collection of points to boxes in the tree. This is the reverse of mPtIndx. -
nPts
private final int nPtsNumber of points stored by the tree. -
pts
Array of points containing input collection of points.
-
-
Constructor Details
-
KDTree
Constructor.- Parameters:
pts- collection of points to store in the tree.clazz- class of point implementation to use.
-
-
Method Details
-
distance
public double distance(int jpt, int kpt) Gets distance between points located at provided positions on input collection.- Parameters:
jpt- index of 1st point.kpt- index of 2nd point.- Returns:
- distance between points or BIG if indices are equal.
-
locateBoxIndex
Gets position of smallest box containing provided point in the input list of points.- Parameters:
pt- point to locate its containing box. Does not need to be contained in input collection.- Returns:
- position of smallest box containing the point.
-
locateBox
Gets smallest box containing provided point in the input list of points.- Parameters:
pt- point to locate its containing box. Does not need to be contained in input collection.- Returns:
- smallest box containing the point.
-
nearestIndex
Index in provided input list of points of closest point to provided one.- Parameters:
pt- point to check against. Does not need to be contained in input collection.- Returns:
- position of closest point.
-
nearestPoint
Closest point to provided one.- Parameters:
pt- point to be checked. Does not need to be contained in input collection.- Returns:
- closest point.
-
nNearest
public void nNearest(int jpt, int[] nn, double[] dn, int n) Gets n nearest point indices to a given one in the input collection.- Parameters:
jpt- index of point to search nearest ones for.nn- array containing resulting indices of nearest points up to the number of found points.dn- array containing resulting distances to nearest points up to the number of found points.n- number of nearest points to find.- Throws:
IllegalArgumentException- if number of nearest points is invalid or if length of arrays containing results are not valid either.
-
nNearest
Gets n nearest point indices to a given point in the input collection.- Parameters:
pt- point to search nearest ones for.nn- array containing resulting indices of nearest points up to the number of found points.dn- array containing resulting distances to nearest points up to the number of found points.n- number of nearest points to find.- Throws:
IllegalArgumentException- if number of nearest points is invalid or if length of arrays containing results are not valid either.
-
nNearest
Gets n nearest points to a given point index in the input collection.- Parameters:
jpt- index of point to search nearest ones for.pn- array containing nearest points up to the number of found points.dn- array containing resulting distances to nearest points up to the number of found points.n- number of nearest points to find.- Throws:
IllegalArgumentException- if number of nearest points is invalid or if length of arrays containing results are not valid either.
-
nNearest
Gets n nearest points to a given point in the input collection.- Parameters:
pt- point to search nearest ones for.pn- array containing nearest points up to the number of found points.dn- array containing resulting distances to nearest points up to the number of found points.n- number of nearest points to find.- Throws:
IllegalArgumentException- if number of nearest points is invalid or if length of arrays containing results are not valid either.
-
locateNear
Locates some near points to provided one up to a certain radius of search. This method only returns up to nmax results, which means that not all points within required radius are returned if more points than provided nmax value are within such radius.- Parameters:
pt- point to search nearby.r- radius of search.list- list where indices of found points are stored up to the number of found points.nmax- maximum number of points to search.- Returns:
- number of found points.
- Throws:
IllegalArgumentException- if radius is negative or maximum number of points to search is zero or negative, or list where indices are stored is not large enough.
-
locateNear
Locates near points to provided one up to a certain radius of search defined in a bounding box.- Parameters:
pt- point to search nearby.r- radius of search defining a bounding box.plist- list where found points are stored up to the number of found points.nmax- maximum number of points to search.- Returns:
- number of found points.
- Throws:
IllegalArgumentException- if radius is negative or maximum number of points to search is zero or negative, or list where points are stored is not large enough.
-
getDimensions
public abstract int getDimensions()Gets number of dimensions supported by this k-D tree implementation on provided list of points.- Returns:
- number of dimensions.
-
createPoint
Creates a point.- Parameters:
value- value to be set on point coordinates.- Returns:
- created point.
-
copyPoint
Copies a point.- Parameters:
point- point to be copied.- Returns:
- copied point.
-
locate
private int locate(int jpt) Gets position of point on input collection for provided internal boxes position.- Parameters:
jpt- internal position in the boxes.- Returns:
- position in the input collection of points.
-
selecti
private static int selecti(int k, int indxOffset, int[] indx, int n, int arrOffset, double[] arr) Makes a selection so that we obtain ordered index at provided k position so that distances are ordered in such a way that resulting array arr contains distances as follows: arr[indx[0 .. k-1]] <= arr[indx[k]] <= arr[indx[k+1 .. n]]. So that positions between 0 and k-1 are not in any particular order but is less than k position, and positions between k+1 and n neither have any particular order but is more than k position.- Parameters:
k- sorted position to retrieve.indxOffset- offset where indx search starts.indx- array to be sorted (i.e. selected).n- length of arrays.arrOffset- offset of distances array. This is usually equal to indxOffset.arr- resulting array containing distances to each selected point.- Returns:
- index of selected point.
-
siftDown
private static void siftDown(double[] heap, int[] ndx, int nn) Moves things around.- Parameters:
heap- array of distances.ndx- array of indices.nn- number of indices to move.
-
swap
private static void swap(int[] a, int posA, int[] b, int posB) Swaps values.- Parameters:
a- 1st array containing values to swap.posA- position to be swapped on 1st array.b- 2nd array containing values to swap.posB- position to be swapped on 2nd array.
-