Skip to content

Commit

Permalink
eckit::geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaciel committed Aug 15, 2023
1 parent f22b458 commit 006c8c9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
24 changes: 24 additions & 0 deletions src/eckit/geometry/Grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ std::vector<Point> Grid::to_points() const {
}


SearchLonLat::Result Grid::nearest(const PointLonLat& p) const {
SearchLonLat search;

size_t index = 0;
for (const auto& p : to_points()) {
search.insert({std::get<PointLonLat>(p), index});
}

return search.nearestNeighbour(p);
}


SearchLonLat::Results Grid::nearest(const PointLonLat& p, size_t k) const {
SearchLonLat search;

size_t index = 0;
for (const auto& p : to_points()) {
search.insert({std::get<PointLonLat>(p), index});
}

return search.kNearestNeighbours(p, k);
}


static pthread_once_t __once;

static Mutex* __mutex = nullptr;
Expand Down
4 changes: 4 additions & 0 deletions src/eckit/geometry/Grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "eckit/geometry/Point.h"
#include "eckit/geometry/Projection.h"
#include "eckit/geometry/Renumber.h"
#include "eckit/geometry/Search.h"
#include "eckit/geometry/area/BoundingBox.h"


Expand Down Expand Up @@ -102,6 +103,9 @@ class Grid {

virtual std::vector<Point> to_points() const;

virtual SearchLonLat::Result nearest(const PointLonLat&) const;
virtual SearchLonLat::Results nearest(const PointLonLat&, size_t k) const;

// -- Overridden methods
// None

Expand Down
48 changes: 30 additions & 18 deletions src/eckit/geometry/Search.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

#pragma once

#include <tuple>
#include <vector>

#include "eckit/container/KDTree.h"
#include "eckit/container/sptree/SPValue.h"
#include "eckit/geometry/Point2.h"
#include "eckit/geometry/Point3.h"
#include "eckit/geometry/PointLonLat.h"
#include "eckit/geometry/Point.h"
#include "eckit/geometry/UnitSphere.h"


Expand All @@ -42,38 +43,49 @@ struct SearchLonLat : Search3 {
using Point = PointLonLat;
using Value = SPValue<TT<search::Traits<Point, Payload>, KDMemory>>;

using Result = std::tuple<Point, Payload, double>;
using Results = std::vector<Result>;

using Search3::Search3;

void insert(const SearchLonLat::Value& value) {
Search3::insert({convert(value.point()), value.payload()});
Search3::insert({to_cartesian(value.point()), value.payload()});
}

template <typename ITER>
void build(ITER begin, ITER end) {
for (auto it = begin; it != end; ++it) {
insert(*it);
template <typename Container>
void build(const Container& c) {
size_t index = 0;
for (const auto& p : c) {
insert({p, index++});
}
}

template <typename Container>
void build(Container& c) {
build(c.begin(), c.end());
Result nearestNeighbour(const Point& p) {
auto n = Search3::nearestNeighbour(to_cartesian(p));
return {UnitSphere::convertCartesianToSpherical(n.point()), n.payload(), n.distance()};
}

NodeInfo nearestNeighbour(const Point& p) { return Search3::nearestNeighbour(convert(p)); }

NodeList findInSphere(const Point& p, double radius) {
return Search3::findInSphere(convert(p), radius);
Results findInSphere(const Point& p, double radius) {
return to_spherical(Search3::findInSphere(to_cartesian(p), radius));
}

NodeList kNearestNeighbours(const Point& p, size_t k) {
return Search3::kNearestNeighbours(convert(p), k);
Results kNearestNeighbours(const Point& p, size_t k) {
return to_spherical(Search3::kNearestNeighbours(to_cartesian(p), k));
}

private:
static Search3::Point convert(const Point& p) {
static Search3::Point to_cartesian(const Point& p) {
return UnitSphere::convertSphericalToCartesian(p);
}

static Results to_spherical(const NodeList& nodes) {
Results list;
list.reserve(nodes.size());
for (const auto& n : nodes) {
list.emplace_back(UnitSphere::convertCartesianToSpherical(n.point()), n.payload(), n.distance());
}
return list;
}
};


Expand Down

0 comments on commit 006c8c9

Please sign in to comment.