#include <vector>
#include <memory>
class Vector3 {
public:
double x, y, z;
Vector3(double x, double y, double z)
: x(x), y(y), z(z) {}
Vector3 operator + (const Vector3& v) const {
return Vector3(x + v.x, y + v.y, z + v.z);
}
Vector3 operator - (const Vector3& v) const {
return Vector3(x - v.x, y - v.y, z - v.z);
}
double Magnitude() const {
return sqrt(x * x + y * y + z * z);
}
};
class OctreeNode {
public:
Vector3 center;
double radius;
std::vector<Vector3> points;
private:
std::unique_ptr<OctreeNode> children[8];
public:
OctreeNode(const Vector3& center, double radius) {
this->center = center;
this->radius = radius;
}
void AddPoint(const Vector3& point) {
// If the point is outside of our boundaries, don't add it.
if ((point - center).Magnitude() > radius) {
return;
}
// If we don't have any children, add the point to our list
if (IsLeafNode()) {
points.push_back(point);
// If we have too many points, subdivide
if (points.size() > 8) {
Subdivide();
}
}
else {
int childIndex = GetChildIndex(point);
children[childIndex]->AddPoint(point);
}
}
void RemovePoint(const Vector3& point) {
// If the point is outside of our boundaries, don't remove it.
if ((point - center).Magnitude() > radius) {
return;
}
// If we don't have any children, remove the point from our list
if (IsLeafNode()) {
for (auto it = points.begin(); it != points.end(); ++it) {
if (*it == point) {
points.erase(it);
break;
}
}
}
else {
int childIndex = GetChildIndex(point);
children[childIndex]->RemovePoint(point);
}
}
void GetPointsNearby(const Vector3& point, double radius, std::vector<Vector3>& pointsInRange) const {
// If the point is outside of our boundaries, don't look for nearby points.
if ((point - center).Magnitude() > radius + this->radius) {
return;
}
// Check all points in this node for the specified radius
for (const auto& p : points) {
if ((p - point).Magnitude() <= radius) {
pointsInRange.push_back(p);
}
}
// If we're not a leaf node, recurse
if (!IsLeafNode()) {
for (const auto& child : children) {
child->GetPointsNearby(point, radius, pointsInRange);
}
}
}
private:
void Subdivide() {
double halfRadius = radius / 2.0;
Vector3 childCenter = center;
// Create children nodes
for (int i = 0; i < 8; i++) {
int x = i % 2 ? 1 : -1;
int y = i > 3 ? 1 : -1;
int z = (i == 1 || i == 3 || i == 5 || i == 7) ? 1 : -1;
Vector3 offset(x * halfRadius, y * halfRadius, z * halfRadius);
childCenter = center + offset;
children[i] = std::make_unique<OctreeNode>(childCenter, halfRadius);
}
// Move points into children nodes
for (const auto& p : points) {
int childIndex = GetChildIndex(p);
children[childIndex]->AddPoint(p);
}
points.clear();
}
int GetChildIndex(const Vector3& point) const {
int index = 0;
if (point.z > center.z) index |= 1;
if (point.y > center.y) index |= 2;
if (point.x > center.x) index |= 4;
return index;
}
bool IsLeafNode() const {
return children[0] == nullptr;
}
};
class Octree {
public:
Octree(const Vector3& center, double radius)
: root(std::make_unique<OctreeNode>(center, radius)) {}
void AddPoint(const Vector3& point) {
root->AddPoint(point);
}
void RemovePoint(const Vector3& point) {
root->RemovePoint(point);
}
std::vector<Vector3> GetPointsNearby(const Vector3& point, double radius) const {
std::vector<Vector3> pointsInRange;
root->GetPointsNearby(point, radius, pointsInRange);
return pointsInRange;
}
private:
std::unique_ptr<OctreeNode> root;
};