Savarese Software Research Corporation

com.savarese.spatial
Class NearestNeighbors<Coord extends java.lang.Number & java.lang.Comparable<? super Coord>,P extends Point<Coord>,V>

java.lang.Object
  extended by com.savarese.spatial.NearestNeighbors<Coord,P,V>

public class NearestNeighbors<Coord extends java.lang.Number & java.lang.Comparable<? super Coord>,P extends Point<Coord>,V>
extends java.lang.Object

NearestNeighbors implements an algorithm for finding the k-nearest neighbors to a query point within the set of points contained by a KDTree instance. The algorithm can be specialized with a custom distance-finding function by passing a Distance instance to its constructor.


Nested Class Summary
static interface NearestNeighbors.Entry<Coord extends java.lang.Number & java.lang.Comparable<? super Coord>,P extends Point<Coord>,V>
          The Entry interface makes accessible the results of a NearestNeighbors search.
 
Constructor Summary
NearestNeighbors()
          Constructs a NearestNeighbors instance using a EuclideanDistance instance to calculate distances between points.
NearestNeighbors(Distance<Coord,P> distance)
          Constructs a new NearestNeighbors instance, using the specified distance-finding functor to calculate distances during searches.
 
Method Summary
 NearestNeighbors.Entry<Coord,P,V>[] get(KDTree<Coord,P,V> tree, P queryPoint, int numNeighbors)
          Same as get(tree, queryPoint, numNeighbors, true).
 NearestNeighbors.Entry<Coord,P,V>[] get(KDTree<Coord,P,V> tree, P queryPoint, int numNeighbors, boolean omitQueryPoint)
          Finds the k-nearest neighbors to a query point withina KDTree instance.
 void setDistance(Distance<Coord,P> distance)
          Sets the distance-finding functor used to calculate distances during searches.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NearestNeighbors

public NearestNeighbors(Distance<Coord,P> distance)
Constructs a new NearestNeighbors instance, using the specified distance-finding functor to calculate distances during searches.

Parameters:
distance - A distance-finding functor implementing the Distance interface.

NearestNeighbors

public NearestNeighbors()
Constructs a NearestNeighbors instance using a EuclideanDistance instance to calculate distances between points.

Method Detail

setDistance

public void setDistance(Distance<Coord,P> distance)
Sets the distance-finding functor used to calculate distances during searches.

Parameters:
distance - The distance-finding functor to use for distance calculations.

get

public NearestNeighbors.Entry<Coord,P,V>[] get(KDTree<Coord,P,V> tree,
                                               P queryPoint,
                                               int numNeighbors,
                                               boolean omitQueryPoint)
Finds the k-nearest neighbors to a query point withina KDTree instance. The neighbors are returned as an array of NearestNeighbors.Entry instances, sorted from nearest to farthest.

Parameters:
tree - The KDTree to search.
queryPoint - The query point.
numNeighbors - The number of nearest neighbors to find. This should be a positive value. Non-positive values result in no neighbors being found.
omitQueryPoint - If true, point-value mappings at a distance of zero are omitted from the result. If false, mappings at a distance of zero are included.
Returns:
An array containing the nearest neighbors and their distances sorted by least distance to greatest distance. If no neighbors are found, the array will have a length of zero.

get

public NearestNeighbors.Entry<Coord,P,V>[] get(KDTree<Coord,P,V> tree,
                                               P queryPoint,
                                               int numNeighbors)
Same as get(tree, queryPoint, numNeighbors, true).


Savarese Software Research Corporation

Copyright © 2001-2005 Daniel F. Savarese
Copyright © 2006-2010 Savarese Software Research Corporation