4 #include <glm/vec2.hpp>
12 template <
typename Derived>
13 class BRectBase :
public std::enable_shared_from_this<Derived> {
14 using SplitFn = std::function<float(
unsigned int, glm::vec2)>;
20 unsigned int numLevels,
21 glm::vec2 _id = {0, 0},
27 auto getLeaves() -> std::vector<std::shared_ptr<Derived>>;
29 auto getLeafAt(
int x,
int y) -> std::optional<std::shared_ptr<Derived>>;
38 const static float splitfn(
unsigned int _level, glm::vec2 _id) {
50 template <
typename Derived>
52 unsigned int numLevels,
63 float t = splitfn(levels,
id);
64 left = std::make_shared<Derived>();
65 right = std::make_shared<Derived>();
66 if (orientation_ == Orientation::VERTICAL) {
68 float x2 = x_ + width_ * t;
69 float w1 = width_ * t;
70 float w2 = width_ - width_ * t;
71 left->setDimensions(x1, y_, w1, height_);
72 left->setOrientation(Orientation::HORIZONTAL);
73 left->subdivide(numLevels - 1, {
id.x * 2,
id.y}, splitfn);
74 right->setOrientation(Orientation::HORIZONTAL);
75 right->setDimensions(x2, y_, w2, height_);
76 right->subdivide(numLevels - 1, {
id.x * 2 + 1,
id.y}, splitfn);
79 float y2 = y_ + height_ * t;
80 float h1 = height_ * t;
81 float h2 = height_ - height_ * t;
82 left->setDimensions(x_, y1, width_, h1);
83 left->subdivide(numLevels - 1, {
id.x,
id.y * 2}, splitfn);
84 right->setDimensions(x_, y2, width_, h2);
85 right->subdivide(numLevels - 1, {
id.x,
id.y * 2 + 1}, splitfn);
89 template <
typename Derived>
91 -> std::optional<std::shared_ptr<Derived>> {
92 for (
auto leaf : getLeaves()) {
93 if (leaf->id.x == x && leaf->id.y == y) {
100 template <
typename Derived>
102 std::vector<std::shared_ptr<Derived>> leaves;
104 for (
auto p : left->getLeaves()) {
107 for (
auto p : right->getLeaves()) {
111 leaves.push_back(std::static_pointer_cast<Derived>(this->shared_from_this())