Class S2ClosestPointQuery<T>
java.lang.Object
com.google.common.geometry.S2ClosestPointQuery<T>
Given a set of points stored in an S2PointIndex, S2ClosestPointQuery provides methods that find
the closest point(s) to a given query point.
Example usage:
void test(Listpoints, List targets) { // The template argument allows auxiliary data to be attached to each point (in this case, the // array index). S2PointIndex index = new S2PointIndexinvalid input: '<'>(); for (int i = 0; i invalid input: '<' points.size(); i++) { index.add(points.get(i), i); } S2ClosestPointQuery query = new S2ClosestPointQueryinvalid input: '<'>(index); query.setMaxPoints(15); for (S2Point target : targets) { for (Result result : query.findClosestPoints(target)) { // result.entry().point() is one of the found closest points. // result.entry().data() is the auxiliary data (the "points" array index). // result.distance() is the distance to the target point. doSomething(target, result.entry().point(), result.entry().data(), result.distance()); } } }
You can find either the k closest points, or all points within a given radius, or both (i.e.,
the k closest points up to a given maximum radius). E.g. to find all the points within 5
kilometers, call query.setMaxDistance(S1Angle.fromEarthDistance(5000));.
You can also restrict the results to an arbitrary S2Region via setRegion(S2Region).
The implementation is designed to be very fast for both small and large point sets.
This class is not thread-safe. In particular, setters must not be called during queries.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA query result paired with the distance to the query target. -
Constructor Summary
ConstructorsConstructorDescriptionS2ClosestPointQuery(S2PointIndex<T> index) Construct a new query for the given index. -
Method Summary
Modifier and TypeMethodDescriptionfindClosestPoint(S2Point target) Convenience method that returns the closest point to the given target point, or null if no points satisfy thegetMaxDistance()andgetRegion()criteria.findClosestPoints(S2Point target) Returns the closest points totargetthat satisfy thegetMaxDistance(),getMaxPoints(), andgetRegion()criteria, ordered by increasing distance.voidfindClosestPoints(List<S2ClosestPointQuery.Result<T>> results, S2Point target) AsfindClosestPoints(S2Point), but sorts the results and adds them at the end of the given list.Returns the closest points to the given edge AB.voidfindClosestPointsToEdge(List<S2ClosestPointQuery.Result<T>> results, S2Point a, S2Point b) AsfindClosestPointsToEdge(S2Point, S2Point), but adds results to the given list.Returns the max distance between returned points and the given target.intReturns the max number of closest points to find.Returns the region in which point searches will be done.index()Returns the underlying S2PointIndex.voidreset()Resets the query state.voidsetMaxDistance(S1Angle maxDistance) Sets a new max distance to search for points.voidsetMaxPoints(int maxPoints) Sets a new max number of closest points to find.void
-
Constructor Details
-
S2ClosestPointQuery
Construct a new query for the given index. Must call reset() before using the query, if the index has been modified since the query was constructed.
-
-
Method Details
-
reset
public void reset()Resets the query state. This method must be called after modifying the underlying index. -
index
Returns the underlying S2PointIndex. -
getMaxPoints
public int getMaxPoints()Returns the max number of closest points to find. -
setMaxPoints
public void setMaxPoints(int maxPoints) Sets a new max number of closest points to find. -
getMaxDistance
Returns the max distance between returned points and the given target. Default is +inf. -
setMaxDistance
Sets a new max distance to search for points. -
getRegion
Returns the region in which point searches will be done. -
setRegion
-
findClosestPoints
Returns the closest points totargetthat satisfy thegetMaxDistance(),getMaxPoints(), andgetRegion()criteria, ordered by increasing distance. If there are no criteria set, then all points are returned. -
findClosestPoints
AsfindClosestPoints(S2Point), but sorts the results and adds them at the end of the given list. -
findClosestPoint
Convenience method that returns the closest point to the given target point, or null if no points satisfy thegetMaxDistance()andgetRegion()criteria. -
findClosestPointsToEdge
Returns the closest points to the given edge AB. Otherwise similar tofindClosestPoints(S2Point). -
findClosestPointsToEdge
public void findClosestPointsToEdge(List<S2ClosestPointQuery.Result<T>> results, S2Point a, S2Point b) AsfindClosestPointsToEdge(S2Point, S2Point), but adds results to the given list.
-