1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
#include <list>
#include "eckit/container/KDTree.h"
#include "eckit/geometry/Point2.h"
#include "eckit/os/Semaphore.h"
#include "eckit/testing/Test.h"
using namespace std;
using namespace eckit;
using namespace eckit::testing;
using namespace eckit::geometry;
namespace eckit::test {
//----------------------------------------------------------------------------------------------------------------------
struct TestTreeTrait {
typedef Point2 Point;
typedef double Payload;
};
//----------------------------------------------------------------------------------------------------------------------
/// \brief Class used to test whether any point in a kd-tree lies in the interior of an
/// axis-aligned box.
template <typename TreeTrait>
class PointInBoxInteriorFinder {
public:
typedef eckit::KDTreeX<TreeTrait> KDTree;
typedef typename KDTree::Point Point;
private:
typedef typename KDTree::Alloc Alloc;
typedef typename KDTree::Node Node;
public:
/// \brief Returns true if any point in \p tree lies in the interior of the specified
/// axis-aligned box.
///
/// \param tree
/// Tree to search.
/// \param lbound
/// Lower-left corner of the axis-aligned box.
/// \param ubound
/// Upper-right corner of the axis-aligned box.
static bool isAnyPointInBoxInterior(const KDTree& tree, const Point& lbound, const Point& ubound) {
if (!tree.root_) {
return false;
}
Alloc& alloc = tree.alloc_;
Node* root = alloc.convert(tree.root_, static_cast<Node*>(nullptr));
return isAnyPointInBoxInterior(root, alloc, lbound, ubound);
}
private:
/// \brief Returns true if the point stored in \p node or any of its descendants lies in the
/// interior of the axis-aligned box with bottom-left and top-right corners at
/// \p lbound and \p ubound.
static bool isAnyPointInBoxInterior(const Node* node, Alloc& alloc, const Point& lbound, const Point& ubound) {
const Point& point = node->value().point();
if (isPointInBoxInterior(point, lbound, ubound)) {
return true;
}
const size_t axis = node->axis();
if (lbound.x(axis) < point.x(axis)) {
if (Node* left = node->left(alloc)) {
if (isAnyPointInBoxInterior(left, alloc, lbound, ubound)) {
return true;
}
}
}
if (ubound.x(axis) > point.x(axis)) {
if (Node* right = node->right(alloc)) {
if (isAnyPointInBoxInterior(right, alloc, lbound, ubound)) {
return true;
}
}
}
return false;
}
/// \brief Returns true if \p point is in the interior of the axis-aligned box
/// with bottom-left and top-right corners at \p lbound and \p ubound.
static bool isPointInBoxInterior(const Point& point, const Point& lbound, const Point& ubound) {
for (size_t d = 0; d < Point::DIMS; ++d) {
if (point.x(d) <= lbound.x(d) || point.x(d) >= ubound.x(d)) {
return false;
}
}
return true;
}
};
//----------------------------------------------------------------------------------------------------------------------
/// \brief Returns true if any point in \p tree is in the interior of the axis-aligned box
/// with bottom-left and top-right corners at \p lbound and \p ubound.
template <typename TreeTraits>
bool isAnyPointInBoxInterior(const eckit::KDTreeX<TreeTraits>& tree,
const typename eckit::KDTreeX<TreeTraits>::Point& lbound,
const typename eckit::KDTreeX<TreeTraits>::Point& ubound) {
return PointInBoxInteriorFinder<TreeTraits>::isAnyPointInBoxInterior(tree, lbound, ubound);
}
//----------------------------------------------------------------------------------------------------------------------
CASE("test_eckit_container_kdtree_constructor") {
typedef KDTreeMemory<TestTreeTrait> Tree;
Tree kd;
typedef Tree::PointType Point;
std::vector<Tree::Value> points;
// test it for single closest point
for (unsigned int i = 0; i < 10; i++) {
for (unsigned int j = 0; j < 10; j++) {
Point p = Point(double(i), double(j));
Tree::Value v(p, 99.9);
points.push_back(v);
}
}
kd.build(points.begin(), points.end());
// pick some point from the vector
Point refPoint = points[points.size() / 2].point();
// perturb it a little
Point delta(0.1, 0.1);
Point testPoint = Point::add(refPoint, delta);
Log::info() << "testPoint perturb " << testPoint.x(0) << ", " << testPoint.x(1) << std::endl;
Point nr = kd.nearestNeighbour(testPoint).point();
// we should find the same point
for (unsigned int i = 0; i < Point::dimensions(); i++) {
EXPECT(nr.x(i) == refPoint.x(i));
}
// test exact match to a point
nr = kd.nearestNeighbour(refPoint).point();
for (unsigned int i = 0; i < Point::dimensions(); i++) {
EXPECT(nr.x(i) == refPoint.x(i));
}
// test "off the scale" - i.e. not within a group of points
delta = Point(1000.0, 0.0);
// add it to the last point
testPoint = Point::add(points.back().point(), delta);
nr = kd.nearestNeighbour(testPoint).point();
for (unsigned int i = 0; i < Point::dimensions(); i++) {
EXPECT(nr.x(i) == points.back().point().x(i));
}
// and negatively
//
delta = Point(-1000.0, 0.0);
// add it to the point() point
testPoint = Point::add(points.front().point(), delta);
nr = kd.nearestNeighbour(testPoint).point();
for (unsigned int i = 0; i < Point::dimensions(); i++) {
EXPECT(nr.x(i) == points.front().point().x(i));
}
// test N nearest
refPoint = points[points.size() / 2].point();
// move this point so it lies between four equally
delta = Point(0.5, 0.5);
testPoint = Point::add(refPoint, delta);
Tree::NodeList nn = kd.kNearestNeighbours(testPoint, 4);
for (Tree::NodeList::iterator it = nn.begin(); it != nn.end(); ++it) {
Point diff = Point::sub(it->point(), testPoint);
// make sure we differ by 0.5 along each axis
for (unsigned int i = 0; i < Point::dimensions(); ++i) {
Log::info() << "distance along point " << Point::distance(Point(0.0, 0.0), diff, i) << std::endl;
EXPECT(Point::distance(Point(0.0, 0.0), diff, i) == 0.5);
}
}
// Test a custom visitor. The purpose of doing that in this test is to ensure that the public
// interface of KDTree is sufficient to write a custom class traversing the tree.
delta = Point(0.25, 0.25);
Point lbound = Point::sub(refPoint, delta);
Point ubound = Point::add(refPoint, delta);
EXPECT(isAnyPointInBoxInterior(kd, lbound, ubound));
delta = Point(0.5, 0.5);
lbound = Point::add(lbound, delta);
ubound = Point::add(ubound, delta);
EXPECT_NOT(isAnyPointInBoxInterior(kd, lbound, ubound));
// Test size()
EXPECT_EQUAL(kd.size(), points.size());
}
CASE("test_eckit_container_kdtree_insert") {
typedef KDTreeMemory<TestTreeTrait> Tree;
Tree kd;
typedef Tree::PointType Point;
std::vector<Tree::Value> points;
// test it for single closest point
for (unsigned int i = 0; i < 10; i++) {
for (unsigned int j = 0; j < 10; j++) {
Point p = Point(double(i), double(j));
Tree::Value v(p, 99.9);
points.push_back(v);
kd.insert(v);
}
}
// pick some point from the vector
Point refPoint = points[points.size() / 2].point();
// perturb it a little
Point delta(0.1, 0.1);
Point testPoint = Point::add(refPoint, delta);
Log::info() << "testPoint perturb " << testPoint.x(0) << ", " << testPoint.x(1) << std::endl;
Point nr = kd.nearestNeighbour(testPoint).point();
// we should find the same point
for (unsigned int i = 0; i < Point::dimensions(); i++) {
EXPECT(nr.x(i) == refPoint.x(i));
}
// test exact match to a point
nr = kd.nearestNeighbour(refPoint).point();
for (unsigned int i = 0; i < Point::dimensions(); i++) {
EXPECT(nr.x(i) == refPoint.x(i));
}
// test "off the scale" - i.e. not within a group of points
delta = Point(1000.0, 0.0);
// add it to the last point
testPoint = Point::add(points.back().point(), delta);
nr = kd.nearestNeighbour(testPoint).point();
for (unsigned int i = 0; i < Point::dimensions(); i++) {
EXPECT(nr.x(i) == points.back().point().x(i));
}
// and negatively
//
delta = Point(-1000.0, 0.0);
// add it to the point() point
testPoint = Point::add(points.front().point(), delta);
nr = kd.nearestNeighbour(testPoint).point();
for (unsigned int i = 0; i < Point::dimensions(); i++) {
EXPECT(nr.x(i) == points.front().point().x(i));
}
// test N nearest
refPoint = points[points.size() / 2].point();
// move this point so it lies between four equally
delta = Point(0.5, 0.5);
testPoint = Point::add(refPoint, delta);
Tree::NodeList nn = kd.kNearestNeighbours(testPoint, 4);
for (Tree::NodeList::iterator it = nn.begin(); it != nn.end(); ++it) {
Point diff = Point::sub(it->point(), testPoint);
// make sure we differ by 0.5 along each axis
for (unsigned int i = 0; i < Point::dimensions(); ++i) {
Log::info() << "distance along point " << Point::distance(Point(0.0, 0.0), diff, i) << std::endl;
EXPECT(Point::distance(Point(0.0, 0.0), diff, i) == 0.5);
}
}
}
CASE("test_kdtree_mapped") {
using Tree = KDTreeMapped<TestTreeTrait>;
using Point = Tree::PointType;
std::vector<Tree::Value> points;
for (unsigned int i = 0; i < 10; i++) {
for (unsigned int j = 0; j < 10; j++) {
Point p = Point(double(i), double(j));
Tree::Value v(p, 99.9);
points.push_back(v);
}
}
auto passTest = [&](Tree& kd) -> bool {
// pick some point from the vector
Point refPoint = points[points.size() / 2].point();
// perturb it a little
Point delta(0.1, 0.1);
Point testPoint = Point::add(refPoint, delta);
Point nr = kd.nearestNeighbour(testPoint).point();
// we should find the same point
for (unsigned int i = 0; i < Point::dimensions(); i++) {
if (nr.x(i) != refPoint.x(i)) {
return false;
}
}
return true;
};
eckit::PathName path("test_kdtree_mapped.kdtree");
// Write file with kdtree
{
if (path.exists()) {
path.unlink();
}
Tree kd(path, points.size(), 0);
EXPECT_EQUAL(kd.size(), 0);
kd.build(points);
EXPECT_EQUAL(kd.size(), points.size());
EXPECT(passTest(kd));
}
// Load file with kdtree
{
Tree kd(path, 0, 0);
// Cannot insert point as the tree is readonly
EXPECT_THROWS_AS(kd.insert(points.front()), eckit::AssertionFailed);
// Cannot build with points as the tree is readonly
EXPECT_THROWS_AS(kd.build(points), eckit::AssertionFailed);
EXPECT_EQUAL(kd.size(), points.size());
EXPECT(passTest(kd));
}
}
CASE("test_kdtree_iterate_empty") {
using Tree = KDTreeMemory<TestTreeTrait>;
Tree kd;
size_t count{0};
for (auto& item : kd) {
count++;
}
EXPECT_EQUAL(count, 0);
}
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
int main(int argc, char** argv) {
return run_tests(argc, argv);
}
|