Debian Codegraph @

src/atlas/util/KDTree.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include "eckit/geometry/Point3.h"
12
13 #include "atlas/grid.h"
14 #include "atlas/runtime/Exception.h"
15 #include "atlas/util/KDTree.h"
16 #include "atlas/util/detail/KDTree.h"
17
18 namespace atlas {
19 namespace util {
20
21 using Handle = typename ObjectHandle<detail::KDTreeBase<idx_t, Point3>>::Handle;
22 using Implementation = typename Handle::Implementation;
23 using Value = typename Implementation::Value;
24 using ValueList = typename Implementation::ValueList;
25
26 // C wrapper interfaces to C++ routines
27 IndexKDTree::Implementation* atlas__IndexKDTree__new() {
28 IndexKDTree::Implementation* tree;
29 {
30 IndexKDTree handle;
31 tree = handle.get();
32 tree->attach();
33 }
34 tree->detach();
35 return tree;
36 }
37
38 IndexKDTree::Implementation* atlas__IndexKDTree__new_geometry(const Geometry::Implementation* geometry) {
39 IndexKDTree::Implementation* tree;
40 {
41 IndexKDTree handle(Geometry{geometry});
42 tree = handle.get();
43 tree->attach();
44 }
45 tree->detach();
46 return tree;
47 }
48
49 void atlas__IndexKDTree__delete(IndexKDTree::Implementation* This) {
50 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
51 delete This;
52 }
53 void atlas__IndexKDTree__reserve(IndexKDTree::Implementation* This, const idx_t size) {
54 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
55 return This->reserve(size);
56 }
57 void atlas__IndexKDTree__insert(IndexKDTree::Implementation* This, const double lon, const double lat,
58 const idx_t index) {
59 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
60 return This->insert(PointLonLat{lon, lat}, index);
61 }
62 void atlas__IndexKDTree__build(IndexKDTree::Implementation* This) {
63 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
64 return This->build();
65 }
66 void atlas__IndexKDTree__closestPoints(const IndexKDTree::Implementation* This, const double plon, const double plat,
67 const size_t k, double*& lons, double*& lats, idx_t*& indices,
68 double*& distances) {
69 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
70 IndexKDTree::ValueList vl(This->closestPoints(PointLonLat{plon, plat}, k));
71 lons = new double[k];
72 lats = new double[k];
73 indices = new idx_t[k];
74 distances = new double[k];
75 for (size_t i = 0; i < k; ++i) {
76 PointLonLat lonlat;
77 This->geometry().xyz2lonlat(vl[i].point(), lonlat);
78 lonlat.normalise();
79 lons[i] = lonlat.lon();
80 lats[i] = lonlat.lat();
81 indices[i] = vl[i].payload();
82 distances[i] = vl[i].distance();
83 }
84 }
85 void atlas__IndexKDTree__closestPoint(const IndexKDTree::Implementation* This, const double plon, const double plat,
86 double& lon, double& lat, idx_t& index, double& distance) {
87 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
88 IndexKDTree::Value v(This->closestPoint(PointLonLat{plon, plat}));
89 PointLonLat lonlat;
90 This->geometry().xyz2lonlat(v.point(), lonlat);
91 lonlat.normalise();
92 lon = lonlat.lon();
93 lat = lonlat.lat();
94 index = v.payload();
95 distance = v.distance();
96 }
97 void atlas__IndexKDTree__closestPointsWithinRadius(const IndexKDTree::Implementation* This, const double plon,
98 const double plat, double radius, size_t& k, double*& lons,
99 double*& lats, idx_t*& indices, double*& distances) {
100 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
101 IndexKDTree::ValueList vl = This->closestPointsWithinRadius(PointLonLat{plon, plat}, radius);
102 k = vl.size();
103 lons = new double[k];
104 lats = new double[k];
105 indices = new idx_t[k];
106 distances = new double[k];
107 for (size_t i = 0; i < k; ++i) {
108 PointLonLat lonlat;
109 This->geometry().xyz2lonlat(vl[i].point(), lonlat);
110 lonlat.normalise();
111 lons[i] = lonlat.lon();
112 lats[i] = lonlat.lat();
113 indices[i] = vl[i].payload();
114 distances[i] = vl[i].distance();
115 }
116 }
117 const Geometry::Implementation* atlas__IndexKDTree__geometry(const IndexKDTree::Implementation* This) {
118 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
119 return This->geometry().get();
120 }
121
122 int atlas__IndexKDTree__empty(const IndexKDTree::Implementation* This) {
123 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
124 return bool(This->empty());
125 }
126
127 idx_t atlas__IndexKDTree__size(const IndexKDTree::Implementation* This) {
128 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_IndexKDTree");
129 return This->size();
130 }
131
132 // ------------------------------------------------------------------
133
134 } // namespace util
135 } // namespace atlas