44 lines
1.4 KiB
C++
44 lines
1.4 KiB
C++
#pragma once
|
|
#include "vde/core/aabb.h"
|
|
#include "vde/spatial/spatial_index.h"
|
|
#include <memory>
|
|
|
|
namespace vde::spatial {
|
|
using core::Point3D;
|
|
using core::Vector3D;
|
|
using core::Ray3Dd;
|
|
using core::Triangle3D;
|
|
|
|
template <typename T> struct OctreeNode;
|
|
template <typename T> struct OctreeData;
|
|
|
|
template <typename T>
|
|
class Octree : public SpatialIndex<T> {
|
|
public:
|
|
explicit Octree(int max_depth = 8, int max_items = 16);
|
|
void build(const std::vector<T>& items) override;
|
|
void insert(const T& item) override;
|
|
bool remove(const T& item) override;
|
|
std::vector<T> query_range(const AABB3D& range) const override;
|
|
std::vector<T> query_knn(const Point3D& point, size_t k) const override;
|
|
std::vector<T> query_ray(const Ray3Dd& ray) const override;
|
|
void clear() override;
|
|
size_t size() const override;
|
|
|
|
private:
|
|
void insert_recursive(int node_idx, const T& item, int depth);
|
|
void subdivide_recursive(int node_idx, int depth);
|
|
void query_range_recursive(int node_idx, const AABB3D& range,
|
|
std::vector<T>& result) const;
|
|
void query_ray_recursive(int node_idx, const Ray3Dd& ray,
|
|
std::vector<T>& result) const;
|
|
|
|
/// Item position helper (for non-Point3D types)
|
|
static Point3D get_position(const T& item);
|
|
|
|
std::shared_ptr<OctreeData<T>> data_;
|
|
int max_depth_, max_items_;
|
|
};
|
|
|
|
} // namespace vde::spatial
|