GeographicLib 2.1.2
|
Nearest-neighbor calculations. More...
#include <GeographicLib/NearestNeighbor.hpp>
Public Member Functions | |
NearestNeighbor () | |
NearestNeighbor (const std::vector< pos_t > &pts, const distfun_t &dist, int bucket=4) | |
void | Initialize (const std::vector< pos_t > &pts, const distfun_t &dist, int bucket=4) |
dist_t | Search (const std::vector< pos_t > &pts, const distfun_t &dist, const pos_t &query, std::vector< int > &ind, int k=1, dist_t maxdist=std::numeric_limits< dist_t >::max(), dist_t mindist=-1, bool exhaustive=true, dist_t tol=0) const |
int | NumPoints () const |
void | Save (std::ostream &os, bool bin=true) const |
void | Load (std::istream &is, bool bin=true) |
void | swap (NearestNeighbor &t) |
void | Statistics (int &setupcost, int &numsearches, int &searchcost, int &mincost, int &maxcost, double &mean, double &sd) const |
void | ResetStatistics () const |
Friends | |
std::ostream & | operator<< (std::ostream &os, const NearestNeighbor &t) |
std::istream & | operator>> (std::istream &is, NearestNeighbor &t) |
Nearest-neighbor calculations.
This class solves the nearest-neighbor problm using a vantage-point tree as described in Finding nearest neighbors.
This class is templated so that it can handle arbitrary metric spaces as follows:
dist_t | the type used for measuring distances; it can be a real or signed integer type; in typical geodetic applications, dist_t might be double . |
pos_t | the type for specifying the positions of points; geodetic application might bundle the latitude and longitude into a std::pair<dist_t, dist_t> . |
distfun_t | the type of a function object which takes takes two positions (of type pos_t) and returns the distance (of type dist_t); in geodetic applications, this might be a class which is constructed with a Geodesic object and which implements a member function with a signature dist_t operator() (const pos_t&, const pos_t&) const , which returns the geodesic distance between two points. |
The dist_t type must support numeric_limits queries (specifically: is_signed, is_integer, max(), digits).
The NearestNeighbor object is constructed with a vector of points (type pos_t) and a distance function (type distfun_t). However the object does not store the points. When querying the object with Search(), it's necessary to supply the same vector of points and the same distance function.
There's no capability in this implementation to add or remove points from the set. Instead Initialize() should be called to re-initialize the object with the modified vector of points.
Because of the overhead in constructing a NearestNeighbor object for a large set of points, functions Save() and Load() are provided to save the object to an external file. operator<<(), operator>>() and Boost serialization can also be used to save and restore a NearestNeighbor object. This is illustrated in the example.
Example of use:
Definition at line 103 of file NearestNeighbor.hpp.
|
inline |
Default constructor for NearestNeighbor.
This is equivalent to specifying an empty set of points.
Definition at line 118 of file NearestNeighbor.hpp.
|
inline |
Constructor for NearestNeighbor.
[in] | pts | a vector of points to include in the set. |
[in] | dist | the distance function object. |
[in] | bucket | the size of the buckets at the leaf nodes; this must lie in [0, 2 + 4*sizeof(dist_t)/sizeof(int)] (default 4). |
GeographicErr | if the value of bucket is out of bounds or the size of pts is too big for an int. |
std::bad_alloc | if memory for the tree can't be allocated. |
pts may contain coincident points (i.e., the distance between them vanishes); these are treated as distinct.
The choice of bucket is a tradeoff between space and efficiency. A larger bucket decreases the size of the NearestNeighbor object which scales as pts.size() / max(1, bucket) and reduces the number of distance calculations to construct the object by log2(bucket) * pts.size(). However each search then requires about bucket additional distance calculations.
Definition at line 149 of file NearestNeighbor.hpp.
References GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t >::Initialize().
|
inline |
Initialize or re-initialize NearestNeighbor.
[in] | pts | a vector of points to include in the tree. |
[in] | dist | the distance function object. |
[in] | bucket | the size of the buckets at the leaf nodes; this must lie in [0, 2 + 4*sizeof(dist_t)/sizeof(int)] (default 4). |
GeographicErr | if the value of bucket is out of bounds or the size of pts is too big for an int. |
std::bad_alloc | if memory for the tree can't be allocated. |
See also the documentation on the constructor.
If an exception is thrown, the state of the NearestNeighbor is unchanged.
Definition at line 170 of file NearestNeighbor.hpp.
Referenced by GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t >::NearestNeighbor().
|
inline |
Search the NearestNeighbor.
[in] | pts | the vector of points used for initialization. |
[in] | dist | the distance function object used for initialization. |
[in] | query | the query point. |
[out] | ind | a vector of indices to the closest points found. |
[in] | k | the number of points to search for (default = 1). |
[in] | maxdist | only return points with distances of maxdist or less from query (default is the maximum dist_t). |
[in] | mindist | only return points with distances of more than mindist from query (default = −1). |
[in] | exhaustive | whether to do an exhaustive search (default true). |
[in] | tol | the tolerance on the results (default 0). |
GeographicErr | if pts has a different size from that used to construct the object. |
The indices returned in ind are sorted by distance from query (closest first).
The simplest invocation is with just the 4 non-optional arguments. This returns the closest distance and the index to the closest point in ind0. If there are several points equally close, then ind0 gives the index of an arbirary one of them. If there's no closest point (because the set of points is empty), then ind is empty and −1 is returned.
With exhaustive = true and tol = 0 (their default values), this finds the indices of k closest neighbors to query whose distances to query are in (mindist, maxdist]. If mindist and maxdist have their default values, then these bounds have no effect. If query is one of the points in the tree, then set mindist = 0 to prevent this point (and other coincident points) from being returned.
If exhaustive = false, exit as soon as k results satisfying the distance criteria are found. If less than k results are returned then the search was exhaustive even if exhaustive = false.
If tol is positive, do an approximate search; in this case the results are to be interpreted as follows: if the k'th distance is dk, then all results with distances less than or equal dk − tol are correct; all others are suspect — there may be other closer results with distances greater or equal to dk − tol. If less than k results are found, then the search is exact.
mindist should be used to exclude a "small" neighborhood of the query point (relative to the average spacing of the data). If mindist is large, the efficiency of the search deteriorates.
Definition at line 258 of file NearestNeighbor.hpp.
|
inline |
Definition at line 355 of file NearestNeighbor.hpp.
|
inline |
Write the object to an I/O stream.
[in,out] | os | the stream to write to. |
[in] | bin | if true (the default) save in binary mode. |
std::bad_alloc | if memory for the string representation of the object can't be allocated. |
The counters tracking the statistics of searches are not saved; however the initializtion cost is saved. The format of the binary saves is not portable.
Definition at line 374 of file NearestNeighbor.hpp.
|
inline |
Read the object from an I/O stream.
[in,out] | is | the stream to read from |
[in] | bin | if true (the default) load in binary mode. |
GeographicErr | if the state read from is is illegal. |
std::bad_alloc | if memory for the tree can't be allocated. |
The counters tracking the statistics of searches are reset by this operation. Binary data must have been saved on a machine with the same architecture. If an exception is thrown, the state of the NearestNeighbor is unchanged.
Definition at line 452 of file NearestNeighbor.hpp.
|
inline |
Swap with another NearestNeighbor object.
[in,out] | t | the NearestNeighbor object to swap with. |
Definition at line 560 of file NearestNeighbor.hpp.
References std::swap().
Referenced by std::swap().
|
inline |
The accumulated statistics on the searches so far.
[out] | setupcost | the cost of initializing the NearestNeighbor. |
[out] | numsearches | the number of calls to Search(). |
[out] | searchcost | the total cost of the calls to Search(). |
[out] | mincost | the minimum cost of a Search(). |
[out] | maxcost | the maximum cost of a Search(). |
[out] | mean | the mean cost of a Search(). |
[out] | sd | the standard deviation in the cost of a Search(). |
Here "cost" measures the number of distance calculations needed. Note that the accumulation of statistics is not thread safe.
Definition at line 587 of file NearestNeighbor.hpp.
|
inline |
Reset the counters for the accumulated statistics on the searches so far.
Definition at line 599 of file NearestNeighbor.hpp.
|
friend |
Write the object to stream os as text.
[in,out] | os | the output stream. |
[in] | t | the NearestNeighbor object to be saved. |
std::bad_alloc | if memory for the string representation of the object can't be allocated. |
Definition at line 541 of file NearestNeighbor.hpp.
|
friend |
Read the object from stream is as text.
[in,out] | is | the input stream. |
[out] | t | the NearestNeighbor object to be loaded. |
GeographicErr | if the state read from is is illegal. |
std::bad_alloc | if memory for the tree can't be allocated. |
Definition at line 552 of file NearestNeighbor.hpp.