39#include <gtest/gtest.h>
54TEST(K2TreeBasic, ConstructorAndEmpty)
59 EXPECT_EQ(tree.
size(), 0);
62TEST(K2TreeBasic, InsertSinglePoint)
68 EXPECT_TRUE(inserted);
69 EXPECT_EQ(tree.
size(), 1);
73TEST(K2TreeBasic, InsertMultiplePoints)
81 EXPECT_EQ(tree.
size(), 3);
84TEST(K2TreeBasic, InsertDuplicatePoint)
93 EXPECT_EQ(tree.
size(), 1);
96TEST(K2TreeBasic, ContainsExisting)
107TEST(K2TreeBasic, ContainsNonExisting)
117TEST(K2TreeBasic, EmptyTreeContains)
128TEST(K2TreeInsertion, AscendingOrder)
132 for (
int i = 0; i < 10; ++i)
134 for (
int j = 0; j < 10; ++j)
140 EXPECT_EQ(tree.
size(), 100);
143 for (
int i = 0; i < 10; ++i)
145 for (
int j = 0; j < 10; ++j)
152TEST(K2TreeInsertion, DescendingOrder)
156 for (
int i = 9; i >= 0; --i)
158 for (
int j = 9; j >= 0; --j)
164 EXPECT_EQ(tree.
size(), 100);
167TEST(K2TreeInsertion, RandomOrder)
171 std::mt19937 gen(12345);
172 std::uniform_real_distribution<> dis(0, 1000);
174 std::vector<Point> points;
175 for (
int i = 0; i < 100; ++i)
177 Point p(dis(gen), dis(gen));
182 EXPECT_EQ(tree.
size(), points.size());
184 for (
const auto & p : points)
190TEST(K2TreeInsertion, ClusteredPoints)
195 for (
int i = 0; i < 20; ++i)
201 for (
int i = 0; i < 20; ++i)
206 EXPECT_EQ(tree.
size(), 40);
213TEST(K2TreeNearest, BasicQueries)
220 ASSERT_TRUE(nearest.has_value());
221 EXPECT_EQ(nearest->get_x(), 50);
222 EXPECT_EQ(nearest->get_y(), 50);
225TEST(K2TreeNearest, MultiplePoints)
235 ASSERT_TRUE(nearest.has_value());
236 EXPECT_EQ(nearest->get_x(), 50);
237 EXPECT_EQ(nearest->get_y(), 50);
241 ASSERT_TRUE(nearest.has_value());
242 EXPECT_EQ(nearest->get_x(), 10);
243 EXPECT_EQ(nearest->get_y(), 10);
247 ASSERT_TRUE(nearest.has_value());
248 EXPECT_EQ(nearest->get_x(), 90);
249 EXPECT_EQ(nearest->get_y(), 90);
257 EXPECT_FALSE(nearest.has_value());
268 ASSERT_TRUE(nearest.has_value());
269 EXPECT_EQ(nearest->get_x(), 50);
270 EXPECT_EQ(nearest->get_y(), 50);
273TEST(K2TreeNearest, GridOfPoints)
278 for (
int i = 0; i <= 100; i += 10)
280 for (
int j = 0; j <= 100; j += 10)
288 ASSERT_TRUE(nearest.has_value());
305 tree.
range(rect, &result);
310TEST(K2TreeRange, AllPointsInRange)
320 tree.
range(rect, &result);
322 EXPECT_EQ(result.
size(), 3);
325TEST(K2TreeRange, NoPointsInRange)
334 tree.
range(rect, &result);
339TEST(K2TreeRange, SomePointsInRange)
351 tree.
range(rect, &result);
353 EXPECT_EQ(result.
size(), 3);
356 bool found_25 =
false, found_50 =
false, found_75 =
false;
357 for (
const auto & p : result)
359 if (p ==
Point(25, 25)) found_25 =
true;
360 if (p ==
Point(50, 50)) found_50 =
true;
361 if (p ==
Point(75, 75)) found_75 =
true;
364 EXPECT_TRUE(found_25);
365 EXPECT_TRUE(found_50);
366 EXPECT_TRUE(found_75);
369TEST(K2TreeRange, BoundaryPoints)
379 tree.
range(rect, &result);
382 EXPECT_GE(result.
size(), 1);
383 EXPECT_LE(result.
size(), 3);
390TEST(K2TreeStress, LargeNumberOfPoints)
394 std::mt19937 gen(54321);
395 std::uniform_real_distribution<> dis(0, 10000);
397 const size_t num_points = 10000;
398 std::vector<Point> points;
400 for (
size_t i = 0; i < num_points; ++i)
402 Point p(dis(gen), dis(gen));
409 EXPECT_LE(tree.
size(), num_points);
410 EXPECT_GT(tree.
size(), num_points * 0.9);
413TEST(K2TreeStress, ManyNearestQueries)
417 std::mt19937 gen(99999);
418 std::uniform_real_distribution<> dis(0, 1000);
421 for (
int i = 0; i < 1000; ++i)
427 for (
int i = 0; i < 1000; ++i)
429 Point query(dis(gen), dis(gen));
430 auto nearest = tree.
nearest(query);
431 EXPECT_TRUE(nearest.has_value());
435TEST(K2TreeStress, ManyRangeQueries)
439 std::mt19937 gen(11111);
440 std::uniform_real_distribution<> dis(0, 1000);
443 for (
int i = 0; i < 1000; ++i)
449 for (
int i = 0; i < 100; ++i)
451 double x1 = dis(gen),
y1 = dis(gen);
452 double x2 = dis(gen), y2 = dis(gen);
454 if (x1 > x2) std::swap(x1, x2);
455 if (
y1 > y2) std::swap(
y1, y2);
459 tree.
range(rect, &result);
462 EXPECT_GE(result.
size(), 0);
466TEST(K2TreeStress, DenseAndSparseRegions)
471 for (
int i = 100; i <= 200; i += 2)
473 for (
int j = 100; j <= 200; j += 2)
480 for (
int i = 700; i <= 900; i += 50)
482 for (
int j = 700; j <= 900; j += 50)
489 Rectangle dense_rect(100, 100, 200, 200);
491 tree.
range(dense_rect, &dense_result);
492 EXPECT_GT(dense_result.
size(), 0);
495 Rectangle sparse_rect(700, 700, 900, 900);
497 tree.
range(sparse_rect, &sparse_result);
498 EXPECT_GT(sparse_result.
size(), 0);
499 EXPECT_LT(sparse_result.
size(), dense_result.
size());
506TEST(K2TreeEdgeCases, ColinearPoints)
511 for (
int i = 0; i <= 100; i += 10)
516 EXPECT_EQ(tree.
size(), 11);
519 for (
int i = 0; i <= 100; i += 10)
525TEST(K2TreeEdgeCases, VerticalLine)
529 for (
int i = 0; i <= 100; i += 10)
534 EXPECT_EQ(tree.
size(), 11);
537TEST(K2TreeEdgeCases, HorizontalLine)
541 for (
int i = 0; i <= 100; i += 10)
546 EXPECT_EQ(tree.
size(), 11);
549TEST(K2TreeEdgeCases, VeryClosePoints)
557 EXPECT_EQ(tree.
size(), 3);
560TEST(K2TreeEdgeCases, NegativeCoordinates)
568 EXPECT_EQ(tree.
size(), 3);
578TEST(K2TreeCorrectness, NearestIsActuallyNearest)
582 std::vector<Point> points = {
590 for (
const auto & p : points)
594 auto nearest = tree.
nearest(query);
595 ASSERT_TRUE(nearest.has_value());
598 Geom_Number min_dist = nearest->distance_to(query);
599 for (
const auto & p : points)
602 EXPECT_LE(min_dist, dist);
606TEST(K2TreeCorrectness, RangeIncludesAllInside)
610 std::mt19937 gen(77777);
611 std::uniform_real_distribution<> dis(0, 100);
613 std::vector<Point> all_points;
614 for (
int i = 0; i < 100; ++i)
616 Point p(dis(gen), dis(gen));
617 all_points.push_back(p);
623 tree.
range(rect, &result);
626 for (
const auto & p : result)
632 for (
const auto & p : all_points)
637 for (
const auto &
r : result)
645 EXPECT_TRUE(found) <<
"Point in rectangle not found in result";
654TEST(K2TreeFuzz, RandomInsertionsAndQueries)
658 std::mt19937 gen(31415);
659 std::uniform_real_distribution<> coord_dis(0, 10000);
660 std::uniform_int_distribution<> op_dis(0, 2);
662 std::set<std::pair<double, double>> inserted;
664 for (
int i = 0; i < 1000; ++i)
666 int op = op_dis(gen);
670 Point p(coord_dis(gen), coord_dis(gen));
674 inserted.insert(std::make_pair(
static_cast<double>(p.
get_x().
get_d()),
680 Point query(coord_dis(gen), coord_dis(gen));
681 auto nearest = tree.
nearest(query);
684 EXPECT_TRUE(nearest.has_value());
688 double x1 = coord_dis(gen),
y1 = coord_dis(gen);
689 double x2 = coord_dis(gen), y2 = coord_dis(gen);
690 if (x1 > x2) std::swap(x1, x2);
691 if (
y1 > y2) std::swap(
y1, y2);
695 tree.
range(rect, &result);
700 for (
const auto & [x,
y] : inserted)
710TEST(K2TreeInsertCorrectness, ReturnsTrueOnSuccess)
717 EXPECT_EQ(tree.
size(), 3);
720TEST(K2TreeInsertCorrectness, ReturnsFalseOnDuplicate)
726 EXPECT_EQ(tree.
size(), 1);
729TEST(K2TreeInsertCorrectness, AllInsertedPointsAreContained)
733 std::mt19937 gen(55555);
734 std::uniform_real_distribution<> dis(0, 1000);
736 std::vector<Point> points;
737 for (
int i = 0; i < 200; ++i)
739 Point p(dis(gen), dis(gen));
744 for (
const auto & p : points)
747 EXPECT_EQ(tree.
size(), points.size());
754TEST(K2TreeMove, MoveConstructor)
760 ASSERT_EQ(src.
size(), 3);
765 EXPECT_EQ(dst.
size(), 3);
771 EXPECT_EQ(src.
size(), 0);
775TEST(K2TreeMove, MoveAssignment)
783 ASSERT_EQ(dst.
size(), 1);
785 dst = std::move(src);
787 EXPECT_EQ(dst.
size(), 2);
792 EXPECT_EQ(src.
size(), 0);
802 tree = std::move(*self);
805 EXPECT_EQ(tree.
size(), 1);
818 EXPECT_TRUE(tree.is_empty());
819 EXPECT_EQ(tree.size(), 0);
829 EXPECT_EQ(tree.size(), 1);
830 EXPECT_TRUE(tree.contains(
Point(42, 17)));
833TEST(K2TreeBuild, MultiplePoints)
844 EXPECT_EQ(tree.size(), 5);
845 for (
size_t i = 0; i < pts.
size(); ++i)
846 EXPECT_TRUE(tree.contains(pts(i)));
849TEST(K2TreeBuild, DuplicatesRemoved)
860 EXPECT_EQ(tree.size(), 3);
861 EXPECT_TRUE(tree.contains(
Point(10, 10)));
862 EXPECT_TRUE(tree.contains(
Point(20, 20)));
863 EXPECT_TRUE(tree.contains(
Point(30, 30)));
866TEST(K2TreeBuild, NearestWorksOnBalancedTree)
869 for (
int i = 0; i <= 100; i += 10)
870 for (
int j = 0; j <= 100; j += 10)
875 EXPECT_EQ(tree.size(), 121);
877 auto nearest = tree.nearest(
Point(43, 57));
878 ASSERT_TRUE(nearest.has_value());
885TEST(K2TreeBuild, StressBuildVsInsert)
887 std::mt19937 gen(12321);
888 std::uniform_real_distribution<> dis(0, 10000);
891 for (
int i = 0; i < 5000; ++i)
897 for (
size_t i = 0; i < pts.
size(); ++i)
898 EXPECT_TRUE(balanced.contains(pts(i)));
901 for (
int i = 0; i < 100; ++i)
903 Point query(dis(gen), dis(gen));
904 auto near_bal = balanced.nearest(query);
905 ASSERT_TRUE(near_bal.has_value());
908 Geom_Number best_d2 = near_bal->distance_squared_to(query);
909 for (
size_t j = 0; j < pts.
size(); ++j)
911 Geom_Number d2 = pts(j).distance_squared_to(query);
912 EXPECT_LE(best_d2, d2);
931TEST(K2TreeForEach, VisitsAllPoints)
935 std::set<std::pair<double, double>>
expected;
936 for (
int i = 0; i <= 50; i += 10)
937 for (
int j = 0; j <= 50; j += 10)
940 expected.insert({
static_cast<double>(i),
static_cast<double>(j)});
943 std::set<std::pair<double, double>> visited;
949 EXPECT_EQ(visited.size(),
expected.size());
953TEST(K2TreeForEach, BalancedTreeVisitsAll)
956 for (
int i = 1; i <= 100; ++i)
964 EXPECT_EQ(
count, tree.size());
965 EXPECT_EQ(
count, 100);
980 size_t count_inside(
const std::vector<Point> & pts,
const Rectangle & rect)
982 return std::count_if(pts.begin(), pts.end(),
983 [&rect] (
const Point & p) { return rect.contains(p); });
986 Geom_Number nearest_distance2(
const std::vector<Point> & pts,
const Point & q)
988 Geom_Number best = pts.front().distance_squared_to(q);
989 for (
const Point & p : pts)
990 best =
std::
min(best, p.distance_squared_to(q));
995TEST(K2TreeRegion, RangeFindsPointsOutsideTheRegion)
1006 EXPECT_EQ(
l.
size(), 2u);
1010TEST(K2TreeRegion, NearestFindsPointsOutsideTheRegion)
1020 ASSERT_TRUE(n.has_value());
1021 EXPECT_EQ(*n,
Point(11, 100));
1024TEST(K2TreeRegion, PointsInsideAndOutsideMatchBruteForce)
1028 std::mt19937 gen(2718);
1029 std::uniform_int_distribution<int> coord(-200, 300);
1032 std::vector<Point> pts;
1033 for (
int i = 0; i < 400; ++i)
1035 const Point p(coord(gen), coord(gen));
1039 ASSERT_EQ(tree.
size(), pts.size());
1041 for (
int i = 0; i < 200; ++i)
1043 int x1 = coord(gen), x2 = coord(gen),
y1 = coord(gen), y2 = coord(gen);
1044 if (x1 > x2) std::swap(x1, x2);
1045 if (
y1 > y2) std::swap(
y1, y2);
1049 ASSERT_EQ(
l.
size(), count_inside(pts, rect))
1050 <<
"[" << x1 <<
", " << x2 <<
"] x [" <<
y1 <<
", " << y2 <<
"]";
1052 const Point q(coord(gen), coord(gen));
1053 const auto n = tree.
nearest(q);
1054 ASSERT_TRUE(n.has_value());
1055 ASSERT_EQ(n->distance_squared_to(q), nearest_distance2(pts, q));
1059TEST(K2TreeRegion, BuildExtendsTheRegion)
1067 EXPECT_EQ(tree.
size(), 4u);
1071 EXPECT_EQ(
l.
size(), 4u);
1075 EXPECT_EQ(left.
size(), 1u);
1078 ASSERT_TRUE(n.has_value());
1079 EXPECT_EQ(*n,
Point(40, 60));
1082TEST(K2TreeRegion, ADefaultTreeTakesItsRegionFromItsPoints)
1092 EXPECT_EQ(
l.
size(), 3u);
1095 ASSERT_TRUE(n.has_value());
1096 EXPECT_EQ(*n,
Point(9, -1));
1101 ::testing::InitGoogleTest(&argc, argv);
1102 return RUN_ALL_TESTS();
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
constexpr bool is_empty() const noexcept
Represents a point with rectangular coordinates in a 2D plane.
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
An axis-aligned rectangle.
bool contains(const Point &p) const noexcept
Checks if this axis-aligned rectangle contains a point.
Minimal std::expected-style result type for C++20.
2D k-d tree for efficient spatial point operations.
bool insert(const Point &p)
Insert a point into the tree.
constexpr size_t size() const noexcept
Get the number of points in the tree.
static void range(Node *root, const Rectangle &rect, DynList< Point > *q)
Recursively find points within a rectangle.
void for_each(Op &&op) const
Apply an operation to every point in the tree (inorder).
static K2Tree build(Array< Point > points, const Point &pmin, const Point &pmax)
Build a balanced k-d tree from an array of points.
bool contains(const Point &p) const noexcept
Check if the tree contains a point.
std::optional< Point > nearest(const Point &p) const noexcept
Find the nearest point to a query point.
constexpr bool is_empty() const noexcept
Check if the tree is empty.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
2D k-d tree implementation for spatial point indexing.
Dynamic array container with automatic resizing.