48 lines
1.3 KiB
C++
48 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <vector>
|
|
#include <cstdint>
|
|
#include <J3ML/LinearAlgebra/Vector2.h>
|
|
|
|
namespace Geometry
|
|
{
|
|
template <typename T>
|
|
class QuadTree
|
|
{
|
|
public:
|
|
struct Node
|
|
{
|
|
Node *parent;
|
|
uint32_t childIndex;
|
|
std::vector<T> objects;
|
|
|
|
Vector2 center;
|
|
Vector2 radius;
|
|
|
|
bool IsLeaf() const { return childIndex == 0xFFFFFFFF;}
|
|
|
|
uint32_t TopLeftChildIndex() const { return childIndex;}
|
|
uint32_t TopRightChildIndex() const { return childIndex+1;}
|
|
uint32_t BottomLeftChildIndex() const { return childIndex+2;}
|
|
uint32_t BottomRightChildIndex() const { return childIndex+3;}
|
|
|
|
/// This assumes that the QuadTree contains unique objects and never duplicates
|
|
void Remove(const T &object)
|
|
{
|
|
for (size_t i = 0; i < objects.size(); ++i){
|
|
if (objects[i] == object) {
|
|
AssociateQuadTreeNode(object, 0); // Mark in the object that it has been removed from the QuadTree.
|
|
std::swap(objects[i], objects.back());
|
|
objects.pop_back();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
AABB2D ComputeAABB()
|
|
{}
|
|
|
|
};
|
|
|
|
};
|
|
} |