Added skeleton for basic Bounding Volume Hierarchy and Bounding Volume Node class function defs.

main
Michael Gaming 2025-04-03 20:27:21 +04:00
parent 5b2d6a02cd
commit f9ebcab19c
4 changed files with 87 additions and 2 deletions

View File

@ -20,6 +20,8 @@
#define BOUNDING_BOX_H
#include <blt/math/vectors.h>
#include <blt/std/types.h>
#include <vector>
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<blt::u8> 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<blt::u8> get_children() const
{
return childIndcs;
}
[[nodiscard]] blt::u8 get_depth()
{
return depth;
}
[[nodiscard]] bounding_box_t* get_bounds()
{
return bounds;
}
private:
std::vector<blt::u8> 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<bounding_box_t*> contains(const blt::vec2& point) const;
[[nodiscard]] std::vector<bounding_box_t*> intersections(const bounding_box_t& other) const;
private:
std::vector<b_box_node_t> nodeTree;
blt::u8 search_children(bounding_box_t& other, blt::u8 startIndx);
};
}
#endif //BOUNDING_BOX_H

@ -1 +1 @@
Subproject commit f49876dab27974b1644410010b94f59fa3d0b79c
Subproject commit 9b1c1a1bb116ca7f6fdec666f74cde8a6a40f7f9

View File

@ -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);
}
}
}

View File

@ -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("");