diff --git a/include/bounding_box.h b/include/bounding_box.h index 66dc6a2..2917176 100644 --- a/include/bounding_box.h +++ b/include/bounding_box.h @@ -20,6 +20,8 @@ #define BOUNDING_BOX_H #include +#include +#include namespace td { @@ -53,6 +55,54 @@ namespace td private: blt::vec2 m_min, m_max; }; + + class b_box_node_t + { + public: + b_box_node_t(const blt::u8 parentIndex, bounding_box_t* bBox, std::vector childIndices): parentIndx(parentIndex), bounds(bBox), childIndcs(childIndices) + {} + + b_box_node_t(const blt::u8 parentIndex, bounding_box_t* bBox, const blt::u8 depth): parentIndx(parentIndex), bounds(bBox), depth(depth) + {} + + void add_child_index(blt::u8 index) + { + childIndcs.push_back(index); + } + + [[nodiscard]] std::vector get_children() const + { + return childIndcs; + } + [[nodiscard]] blt::u8 get_depth() + { + return depth; + } + [[nodiscard]] bounding_box_t* get_bounds() + { + return bounds; + } + private: + std::vector childIndcs; + blt::u8 parentIndx; + blt::u8 depth; + bounding_box_t* bounds; + }; + + class b_box_hierarchy_t + { + public: + void add_node(bounding_box_t* bBox); + blt::u8 size() { + return nodeTree.size(); + } + [[nodiscard]] b_box_node_t container_group(const blt::vec2& point) const; + [[nodiscard]] std::vector contains(const blt::vec2& point) const; + [[nodiscard]] std::vector intersections(const bounding_box_t& other) const; + private: + std::vector nodeTree; + blt::u8 search_children(bounding_box_t& other, blt::u8 startIndx); + }; } #endif //BOUNDING_BOX_H diff --git a/lib/blt-with-graphics b/lib/blt-with-graphics index f49876d..9b1c1a1 160000 --- a/lib/blt-with-graphics +++ b/lib/blt-with-graphics @@ -1 +1 @@ -Subproject commit f49876dab27974b1644410010b94f59fa3d0b79c +Subproject commit 9b1c1a1bb116ca7f6fdec666f74cde8a6a40f7f9 diff --git a/src/bounding_box.cpp b/src/bounding_box.cpp index 747cbac..8ad566d 100644 --- a/src/bounding_box.cpp +++ b/src/bounding_box.cpp @@ -39,4 +39,38 @@ namespace td { return other.m_min <= m_max && other.m_max >= m_min; } + + blt::u8 b_box_hierarchy_t::search_children(bounding_box_t& other, blt::u8 startIndx) { // [NOT FINISHED] + if (startIndx == 0) + { + for (blt::u8 i = startIndx; i < nodeTree.size(); i++) + { + if (nodeTree[i].get_bounds()->intersects(other)) return i; + } + } + else + { + for (blt::u8 childIndex: nodeTree[startIndx].get_children()) { + if (nodeTree[childIndex].get_bounds()->intersects(other)) return childIndex; + } + } + return -1; + } + + void b_box_hierarchy_t::add_node(bounding_box_t* bBox) { // [NOT FINISHED] + if (nodeTree.size() == 0) return nodeTree.push_back(b_box_node_t(0, bBox, 0)); + + blt::u8 parentIndex, i = -1; + + do + { + parentIndex = i; + } while (i = search_children(*bBox, parentIndex) != -1); + + if (parentIndex != -1) + { + nodeTree.push_back(b_box_node_t(parentIndex, bBox, nodeTree[parentIndex].get_depth() + 1)); + nodeTree[parentIndex].add_child_index(nodeTree.size() - 1); + } + } } diff --git a/src/michael_examples.cpp b/src/michael_examples.cpp index 81312ae..7b6f3e6 100644 --- a/src/michael_examples.cpp +++ b/src/michael_examples.cpp @@ -367,6 +367,7 @@ void michael_examples() // you can use the filter option to filter elements from the container. // it is an ugly hack because of the way c++ iterators work // so it forcefully returns an optional + /* (MICHAEL'S NOTE: Removed below line so it compiles) for (const auto v : blt::enumerate(some_map).flatten().filter([](const auto& data) { auto [i, k, v] = data; if (i % 2 == 0) @@ -380,7 +381,7 @@ void michael_examples() { if (v) BLT_TRACE("{} {} {}", std::get<0>(*v), std::get<1>(*v), std::get<2>(*v)); - } + } */ BLT_TRACE(""); BLT_TRACE("");