v0.1.0: 初始工程骨架 — 7模块 40头文件 32源文件 17文档 Apache-2.0
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
#include "vde/spatial/spatial_index.h"
|
||||
#include "vde/core/aabb.h"
|
||||
#include "vde/core/triangle.h"
|
||||
|
||||
namespace vde::spatial {
|
||||
|
||||
enum class BVHSplitStrategy { Middle, Equal, SAH };
|
||||
|
||||
struct BVHBuildOptions {
|
||||
BVHSplitStrategy strategy = BVHSplitStrategy::SAH;
|
||||
int leaf_size = 4;
|
||||
int max_depth = 64;
|
||||
};
|
||||
|
||||
class BVH : public SpatialIndex<Triangle3D> {
|
||||
public:
|
||||
explicit BVH(const BVHBuildOptions& opts = {}) : opts_(opts) {}
|
||||
|
||||
void build(const std::vector<Triangle3D>& tris) override;
|
||||
void insert(const Triangle3D& tri) override;
|
||||
bool remove(const Triangle3D& tri) override;
|
||||
std::vector<Triangle3D> query_range(const AABB3D& range) const override;
|
||||
std::vector<Triangle3D> query_knn(const Point3D& point, size_t k) const override;
|
||||
std::vector<Triangle3D> query_ray(const Ray3Dd& ray) const override;
|
||||
void clear() override;
|
||||
size_t size() const override { return primitives_.size(); }
|
||||
|
||||
/// Find closest hit along ray
|
||||
struct HitResult { double t; Triangle3D tri; Point3D point; };
|
||||
std::optional<HitResult> query_ray_nearest(const Ray3Dd& ray) const;
|
||||
|
||||
private:
|
||||
struct Node {
|
||||
AABB3D bounds;
|
||||
int left = -1, right = -1;
|
||||
int first_prim = 0, prim_count = 0;
|
||||
bool leaf() const { return left < 0; }
|
||||
};
|
||||
std::vector<Node> nodes_;
|
||||
std::vector<Triangle3D> primitives_;
|
||||
BVHBuildOptions opts_;
|
||||
|
||||
void build_recursive(int node, int first, int count, int depth);
|
||||
double sah_cost(int n_left, int n_right, const AABB3D& left, const AABB3D& right) const;
|
||||
};
|
||||
|
||||
} // namespace vde::spatial
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include "vde/spatial/spatial_index.h"
|
||||
|
||||
namespace vde::spatial {
|
||||
|
||||
template <typename T>
|
||||
class KDTree : public SpatialIndex<T> {
|
||||
public:
|
||||
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 { return items_.size(); }
|
||||
private:
|
||||
std::vector<T> items_;
|
||||
};
|
||||
|
||||
} // namespace vde::spatial
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include "vde/spatial/spatial_index.h"
|
||||
|
||||
namespace vde::spatial {
|
||||
|
||||
template <typename T>
|
||||
class Octree : public SpatialIndex<T> {
|
||||
public:
|
||||
explicit Octree(int max_depth = 8, int max_items = 16)
|
||||
: max_depth_(max_depth), max_items_(max_items) {}
|
||||
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 { return count_; }
|
||||
private:
|
||||
int max_depth_, max_items_;
|
||||
size_t count_ = 0;
|
||||
};
|
||||
|
||||
} // namespace vde::spatial
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include "vde/spatial/spatial_index.h"
|
||||
|
||||
namespace vde::spatial {
|
||||
|
||||
template <typename T>
|
||||
class RTree : public SpatialIndex<T> {
|
||||
public:
|
||||
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 { return count_; }
|
||||
private:
|
||||
size_t count_ = 0;
|
||||
};
|
||||
|
||||
} // namespace vde::spatial
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "vde/core/aabb.h"
|
||||
#include "vde/core/line.h"
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace vde::spatial {
|
||||
|
||||
template <typename T>
|
||||
class SpatialIndex {
|
||||
public:
|
||||
virtual ~SpatialIndex() = default;
|
||||
virtual void build(const std::vector<T>& items) = 0;
|
||||
virtual void insert(const T& item) = 0;
|
||||
virtual bool remove(const T& item) = 0;
|
||||
virtual std::vector<T> query_range(const AABB3D& range) const = 0;
|
||||
virtual std::vector<T> query_knn(const Point3D& point, size_t k) const = 0;
|
||||
virtual std::vector<T> query_ray(const Ray3Dd& ray) const = 0;
|
||||
virtual void clear() = 0;
|
||||
virtual size_t size() const = 0;
|
||||
};
|
||||
|
||||
} // namespace vde::spatial
|
||||
Reference in New Issue
Block a user