29#include <oneapi/tbb/parallel_for.h>
32template<std::
floating_po
int T,
unsigned BUCKET_SIZE>
class QuadTree;
34template<std::
floating_po
int T =
double,
unsigned BUCKET_SIZE = 1>
struct RefNode2D :
Node2D<T> {
38template<std::
floating_po
int T =
double,
unsigned BUCKET_SIZE = 1>
class QuadTree {
44 using leaf_t = std::vector<node_ptr_t>;
45 using subtree_t = std::array<std::unique_ptr<QuadTree>, 4>;
49 std::variant<leaf_t, subtree_t> child;
53 void attach(
const QuadTree* in_parent) { parent = in_parent; }
57 : box(std::move(in_box)) {}
62 if(
auto& nodes = std::get<leaf_t>(child = std::move(in_child)); nodes.size() <= BUCKET_SIZE) {
63 for(
auto&& node : nodes) node->tree =
this;
67 std::array<leaf_t, 4> buckets;
68 for(
auto&& node : std::get<leaf_t>(child)) buckets[2 * (node->y > box.
center.
y) + (node->x > box.
center.
x)].push_back(node);
69 for(
auto i = 0; i < 4; ++i) buckets[i].shrink_to_fit();
71 auto& subtrees = std::get<subtree_t>(child = subtree_t());
74 if(!buckets[0].empty()) {
76 subtrees[0]->attach(
this);
78 if(!buckets[1].empty()) {
80 subtrees[1]->attach(
this);
82 if(!buckets[2].empty()) {
84 subtrees[2]->attach(
this);
86 if(!buckets[3].empty()) {
88 subtrees[3]->attach(
this);
92 tbb::parallel_for(0, 4, [&](
const auto i) {
93 if(subtrees[i]) subtrees[i]->
insert(std::move(buckets[i]));
96 for(
auto i = 0; i < 4; ++i)
97 if(subtrees[i]) subtrees[i]->insert(std::move(buckets[i]));
102 if(!box.
overlap(target))
return {};
104 if(std::holds_alternative<leaf_t>(child))
return {
this};
106 std::vector<const QuadTree*> result;
107 for(
auto&& subtree : std::get<subtree_t>(child)) {
108 if(!subtree)
continue;
109 auto sub_result = subtree->overlap(target);
110 result.insert(result.end(), sub_result.begin(), sub_result.end());
Storage< T >::iterator end(Storage< T > &S)
Definition Storage.hpp:211
Storage< T >::iterator begin(Storage< T > &S)
Definition Storage.hpp:209
Definition quadtree.hpp:38
RefNode2D< T, BUCKET_SIZE > node_t
Definition quadtree.hpp:40
void insert(IT begin, IT end)
Definition quadtree.hpp:59
node_t * node_ptr_t
Definition quadtree.hpp:41
QuadTree(BoundingBox< T > &&in_box)
Definition quadtree.hpp:56
std::vector< const QuadTree * > overlap(const BoundingBox< T > &target) const
Definition quadtree.hpp:101
void insert(leaf_t &&in_child)
Definition quadtree.hpp:61
bool overlap(const BoundingBox &other) const
Definition common.hpp:36
const Vector2D< T > center
Definition common.hpp:34
const Vector2D< T > dimension
Definition common.hpp:34
Definition quadtree.hpp:34
const QuadTree< T, BUCKET_SIZE > * tree
Definition quadtree.hpp:35
const T x
Definition common.hpp:26
const T y
Definition common.hpp:26