From 4aeb94d42e70717bc7269da592ebf03e12ce798a Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sun, 16 Mar 2025 17:13:06 +0100 Subject: [PATCH] WIP (quad tree) --- src/db/unit_tests/dbQuadTreeTests.cc | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/db/unit_tests/dbQuadTreeTests.cc b/src/db/unit_tests/dbQuadTreeTests.cc index 26c06c196..219f8dc20 100644 --- a/src/db/unit_tests/dbQuadTreeTests.cc +++ b/src/db/unit_tests/dbQuadTreeTests.cc @@ -25,6 +25,7 @@ #include "dbBoxConvert.h" #include "tlUnitTest.h" #include "tlString.h" +#include "tlTimer.h" #include @@ -473,6 +474,13 @@ static db::DBox rbox () return box; } +static db::DBox rbox (double dim) +{ + db::DBox box; + db::DPoint c (rvalue (), rvalue ()); + return box = db::DBox (c, c).enlarged (db::DVector (dim * 0.5, dim * 0.5)); +} + TEST(many) { MyQuadTree tree; @@ -511,3 +519,73 @@ TEST(many) EXPECT_EQ (tree.levels (), size_t (1)); EXPECT_EQ (tree.size (), size_t (0)); } + +TEST(timing_insert) +{ + MyQuadTree tree; + + { + unsigned int n = 1000000; + tl::SelfTimer timer (tl::sprintf ("%d inserts ..", int (n))); + for (unsigned int i = 0; i < n; ++i) { + tree.insert (rbox ()); + } + } + + tree.clear (); + + { + unsigned int n = 2000000; + tl::SelfTimer timer (tl::sprintf ("%d inserts ..", int (n))); + for (unsigned int i = 0; i < n; ++i) { + tree.insert (rbox ()); + } + } +} + +TEST(timing_lookup) +{ + MyQuadTree tree; + + unsigned int n = 250000; + for (unsigned int i = 0; i < n; ++i) { + tree.insert (rbox (10.0)); + } + + unsigned int ntests = 100; + + std::vector > > tests; + for (unsigned int i = 0; i < ntests; ++i) { + db::DBox bx = rbox (10.0); + tests.push_back (std::make_pair (bx, std::make_pair (size_t (0), size_t (0)))); + } + + { + tl::SelfTimer timer (tl::sprintf ("%d tests (lookup) ..", int (ntests))); + for (auto t = tests.begin (); t != tests.end (); ++t) { + size_t n = 0; + for (auto i = tree.begin_touching (t->first); ! i.at_end (); ++i) { + ++n; + } + t->second.first = n; + } + } + + { + tl::SelfTimer timer (tl::sprintf ("%d tests (brute force) ..", int (ntests))); + for (auto t = tests.begin (); t != tests.end (); ++t) { + size_t n = 0; + for (auto i = tree.begin (); ! i.at_end (); ++i) { + if (i->touches (t->first)) { + ++n; + } + } + t->second.second = n; + } + } + + for (auto t = tests.begin (); t != tests.end (); ++t) { + EXPECT_EQ (t->second.first, t->second.second); + } +} +