suanPan
🧮 An Open Source, Parallel and Heterogeneous Finite Element Analysis Framework
Loading...
Searching...
No Matches
quadtree.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2026 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17
18#ifndef QUADTREE_HPP
19#define QUADTREE_HPP
20
21#include "common.hpp"
22
23#include <array>
24#include <memory>
25#include <variant>
26#include <vector>
27
28#ifdef SUANPAN_MT
29#include <oneapi/tbb/parallel_for.h>
30#endif
31
32template<std::floating_point T, unsigned BUCKET_SIZE> class QuadTree;
33
34template<std::floating_point T = double, unsigned BUCKET_SIZE = 1> struct RefNode2D : Node2D<T> {
36};
37
38template<std::floating_point T = double, unsigned BUCKET_SIZE = 1> class QuadTree {
39public:
42
43private:
44 using leaf_t = std::vector<node_ptr_t>;
45 using subtree_t = std::array<std::unique_ptr<QuadTree>, 4>;
46
47 const BoundingBox<T> box;
48
49 std::variant<leaf_t, subtree_t> child;
50
51 const QuadTree* parent{nullptr};
52
53 void attach(const QuadTree* in_parent) { parent = in_parent; }
54
55public:
56 explicit QuadTree(BoundingBox<T>&& in_box)
57 : box(std::move(in_box)) {}
58
59 template<std::forward_iterator IT> void insert(IT begin, IT end) requires std::is_convertible_v<std::iter_value_t<IT>, node_ptr_t> { insert(leaf_t(begin, end)); }
60
61 void insert(leaf_t&& in_child) {
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;
64 return;
65 }
66
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();
70
71 auto& subtrees = std::get<subtree_t>(child = subtree_t());
72
73 const auto dx = T(.5) * box.dimension.x, dy = T(.5) * box.dimension.y;
74 if(!buckets[0].empty()) {
75 subtrees[0] = std::make_unique<QuadTree>(BoundingBox<T>{{box.center.x - dx, box.center.y - dy}, {dx, dy}});
76 subtrees[0]->attach(this);
77 }
78 if(!buckets[1].empty()) {
79 subtrees[1] = std::make_unique<QuadTree>(BoundingBox<T>{{box.center.x + dx, box.center.y - dy}, {dx, dy}});
80 subtrees[1]->attach(this);
81 }
82 if(!buckets[2].empty()) {
83 subtrees[2] = std::make_unique<QuadTree>(BoundingBox<T>{{box.center.x - dx, box.center.y + dy}, {dx, dy}});
84 subtrees[2]->attach(this);
85 }
86 if(!buckets[3].empty()) {
87 subtrees[3] = std::make_unique<QuadTree>(BoundingBox<T>{{box.center.x + dx, box.center.y + dy}, {dx, dy}});
88 subtrees[3]->attach(this);
89 }
90
91#ifdef SUANPAN_MT
92 tbb::parallel_for(0, 4, [&](const auto i) {
93 if(subtrees[i]) subtrees[i]->insert(std::move(buckets[i]));
94 });
95#else
96 for(auto i = 0; i < 4; ++i)
97 if(subtrees[i]) subtrees[i]->insert(std::move(buckets[i]));
98#endif
99 }
100
101 std::vector<const QuadTree*> overlap(const BoundingBox<T>& target) const {
102 if(!box.overlap(target)) return {};
103
104 if(std::holds_alternative<leaf_t>(child)) return {this};
105
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());
111 }
112 return result;
113 }
114};
115
116#endif
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
Definition common.hpp:33
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 common.hpp:29
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