11template<math::Number T>
12constexpr Vec2D<T> nearestNeighbourBinarySearch(
const Vec2D<T> & query,
const std::set<Vec2D<T>,LexicographicOrder> & points, util::Predicate<Vec2D<T>>
auto const & predicate = util::TruePredicate{})
noexcept {
16 return *points.begin();
18 for(
auto it = (points.lower_bound(query)==points.end()?--points.lower_bound(query):points.lower_bound(query)) ;
19 std::make_reverse_iterator(it) != points.rend() && fabs(it->x - query.x) < minDistance ; --it)
21 const Vec2D<T> & neighbour = *it;
22 if(query.distance(neighbour) < minDistance and neighbour != query and predicate(neighbour)){
23 minDistance = query.distance(neighbour);
27 for(
auto it = points.upper_bound(query); it != points.end() && fabs(it->x - query.x) < minDistance; ++it){
28 const Vec2D<T> & neighbour = *it;
29 if(query.distance(neighbour) < minDistance and neighbour != query and predicate(neighbour)){
30 minDistance = query.distance(neighbour);
47template<math::Number T>
49 return __impl::nearestNeighbourBinarySearch<T>(query,points,[](
const auto & e){
return true;});
61template<math::Number T>
63 const size_t _k = std::min(k,
util::size(points) - 1 );
64 std::vector<Vec2D<T>> neighbours;
65 neighbours.resize(_k);
66 for(
size_t i = 0; i < _k; i++){
67 auto notYetNeigbourPredicate = [neighbours](
const Vec2D<T> & p){
return std::ranges::find(neighbours,p) == neighbours.end();};
68 neighbours[i] = __impl::nearestNeighbourBinarySearch(query,points,notYetNeigbourPredicate);
84 std::vector<std::pair<Vec2D<T>,std::vector<Vec2D<T>>>> result;
85 for(
const auto & p: points){
86 orderedSetOfPoints.emplace(p);
88 for(
auto & point : orderedSetOfPoints){
89 result.emplace_back(std::make_pair(point,
kNearestNeighbours(point,orderedSetOfPoints,k)));
Implementation of a two-dimensional vector.
Definition Vec2D.hpp:16
Definition CollectionConcepts.hpp:27
Definition BoundingBoxWrapper.hpp:5
constexpr std::vector< Vec2D< T > > kNearestNeighbours(const Vec2D< T > &query, std::set< Vec2D< T >, LexicographicOrder > const &points, const size_t k) noexcept
Compute the k nearest neighours for a query point.
Definition NearestNeighbours.hpp:62
constexpr std::vector< std::pair< Vec2D< T >, std::vector< Vec2D< T > > > > AllKNearestNeighbours(util::forward_range_of< Vec2D< T > > auto const &points, const size_t k) noexcept
Compute the k nearest neighbours for each point.
Definition NearestNeighbours.hpp:82
constexpr Vec2D< T > nearestNeighbour(const Vec2D< T > &query, std::set< Vec2D< T >, LexicographicOrder > const &points) noexcept
Compute the nearest neighbour for a query point.
Definition NearestNeighbours.hpp:48
double DEFAULT_FLOATING_POINT
Definition NumericConcepts.hpp:12
constexpr size_t size(std::ranges::range auto &&range) noexcept
Definition CollectionConcepts.hpp:50
Comparator for lexigraphically-ordering of Vec2D objects Compares first the x-Coordinate and then the...
Definition Vec2D.hpp:195