Files
ViewDesignEngine/tests/spatial/test_octree.cpp
T
hm 646b3eee8a fix(core,spatial): 修复 Octree 插入 SEGFAULT 与 ThreadSafeObjectPool 堆破坏
Octree(src/spatial/octree.cpp):
- 根因:subdivide() 在 nodes.reserve() 之前取 OctreeNode& 引用,扩容后
  node.children[i] 写入悬垂内存 → InsertMultiple SEGFAULT;subdivide_recursive /
  insert_recursive 同样持引用跨扩容。
- 修复:改为快照 + 按索引 append,不在 vector 扩容期间持有引用;子节点改默认
  构造后 children.fill(-1)(避免聚合初始化把 children 置 0 造成"每个子节点都像
  内部节点"的无限递归);remove_from_node 改为无引用实现;根包围盒扩张语义
  文档化并补回归(越界项保留在节点、仍可查询/删除)。
- 新增回归:多级细分查询、根扩张后插入可查询。

ThreadSafeObjectPool(include/vde/core/object_pool.h):
- 根因:static thread_local 池被所有实例共享且生命周期长于实例;release() 会把
  任意来源指针塞进当前线程池,实例销毁后仍留下悬垂指针 → 下次 acquire 返回已
  释放内存(0xc0000374 堆破坏)。
- 修复:改为每实例 mutex + 私有 ObjectPool + 原子计数(不再跨实例/跨线程混用
  指针),API 保持兼容;单线程 ObjectPool 语义不变。
- 新增回归:双实例隔离、同线程重复创建销毁 200 轮、跨线程 release。
2026-10-25 16:12:41 +08:00

219 lines
7.1 KiB
C++

#include <gtest/gtest.h>
#include "vde/spatial/octree.h"
#include "vde/core/aabb.h"
#include "vde/core/line.h"
#include <algorithm>
#include <cmath>
#include <vector>
using namespace vde::spatial;
using namespace vde::core;
// ═══════════════════════════════════════════════════════════
// 八叉树测试: insert / query_range / query_knn / remove
// ═══════════════════════════════════════════════════════════
struct TestItem {
Point3D pos;
int id;
bool operator==(const TestItem& other) const {
return id == other.id && (pos - other.pos).norm() < 1e-9;
}
};
// Octree 需要的特化 (由模板在 .cpp 中实现)
// 这里仅声明使用 Point3D 的实例化
// ── insert ──────────────────────────────────────────
TEST(OctreeTest, InsertSingle) {
Octree<Point3D> octree(4, 8);
Point3D p(0.5, 0.5, 0.5);
octree.insert(p);
EXPECT_EQ(octree.size(), 1u);
}
TEST(OctreeTest, InsertMultiple) {
Octree<Point3D> octree(4, 8);
std::vector<Point3D> pts = {
{0.1, 0.2, 0.3}, {0.5, 0.5, 0.5}, {0.9, 0.8, 0.7},
{0.3, 0.6, 0.1}, {0.7, 0.1, 0.9}, {0.2, 0.8, 0.4},
{0.8, 0.3, 0.6}, {0.4, 0.7, 0.2}, {0.6, 0.9, 0.5},
};
for (const auto& p : pts)
octree.insert(p);
EXPECT_EQ(octree.size(), pts.size());
}
TEST(OctreeTest, InsertBuild) {
Octree<Point3D> octree(5, 10);
std::vector<Point3D> pts = {
{0,0,0}, {0.5,0,0}, {0,0.5,0}, {0,0,0.5},
{1,0,0}, {0,1,0}, {0,0,1},
};
octree.build(pts);
EXPECT_EQ(octree.size(), pts.size());
}
// ── query_range ─────────────────────────────────────
TEST(OctreeTest, QueryRangeFindsAll) {
Octree<Point3D> octree(5, 10);
std::vector<Point3D> pts = {
{0.1, 0.1, 0.1}, {0.3, 0.3, 0.3}, {0.5, 0.5, 0.5},
{0.7, 0.7, 0.7}, {0.9, 0.9, 0.9},
};
octree.build(pts);
AABB3D range(Point3D(0, 0, 0), Point3D(0.4, 0.4, 0.4));
auto results = octree.query_range(range);
EXPECT_GE(results.size(), 2u);
}
TEST(OctreeTest, QueryRangeEmpty) {
Octree<Point3D> octree(5, 10);
std::vector<Point3D> pts = {{0.1, 0.1, 0.1}};
octree.build(pts);
AABB3D range(Point3D(0.9, 0.9, 0.9), Point3D(1.0, 1.0, 1.0));
auto results = octree.query_range(range);
EXPECT_EQ(results.size(), 0u);
}
// ── query_knn ───────────────────────────────────────
TEST(OctreeTest, QueryKNNBasic) {
Octree<Point3D> octree(5, 10);
std::vector<Point3D> pts = {
{0.1, 0, 0}, {0.5, 0, 0}, {0.9, 0, 0},
{0.3, 0.3, 0}, {0.7, 0.7, 0},
};
octree.build(pts);
auto results = octree.query_knn(Point3D(0.12, 0, 0), 2);
EXPECT_GE(results.size(), 1u);
EXPECT_LE(results.size(), 2u);
}
TEST(OctreeTest, QueryKNNExceedsData) {
Octree<Point3D> octree(5, 10);
std::vector<Point3D> pts = {{0.5, 0.5, 0.5}};
octree.build(pts);
auto results = octree.query_knn(Point3D(0, 0, 0), 10);
EXPECT_EQ(results.size(), 1u);
}
// ── remove ──────────────────────────────────────────
TEST(OctreeTest, RemoveExisting) {
Octree<Point3D> octree(4, 8);
Point3D p(0.5, 0.3, 0.7);
octree.insert(p);
EXPECT_EQ(octree.size(), 1u);
bool removed = octree.remove(p);
EXPECT_TRUE(removed);
EXPECT_EQ(octree.size(), 0u);
}
TEST(OctreeTest, RemoveNonExisting) {
Octree<Point3D> octree(4, 8);
Point3D p(0.1, 0.2, 0.3);
octree.insert(p);
EXPECT_EQ(octree.size(), 1u);
Point3D q(0.9, 0.9, 0.9);
bool removed = octree.remove(q);
EXPECT_FALSE(removed);
EXPECT_EQ(octree.size(), 1u);
}
// ── query_ray ───────────────────────────────────────
TEST(OctreeTest, QueryRay) {
Octree<Point3D> octree(5, 10);
std::vector<Point3D> pts = {
{0.1, 0.1, 0.1}, {0.5, 0.5, 0.5}, {0.9, 0.9, 0.9},
};
octree.build(pts);
Ray3Dd ray(Point3D(0, 0, 0), Vector3D(1, 1, 1).normalized());
auto results = octree.query_ray(ray);
EXPECT_GE(results.size(), 0u);
}
// ── clear ───────────────────────────────────────────
TEST(OctreeTest, ClearTree) {
Octree<Point3D> octree(4, 8);
octree.insert(Point3D(0.5, 0.5, 0.5));
octree.insert(Point3D(0.3, 0.3, 0.3));
EXPECT_GT(octree.size(), 0u);
octree.clear();
EXPECT_EQ(octree.size(), 0u);
}
// ── subdivision / root-expansion regression ─────────
// Build() used to keep a node reference across subdivide(), which reallocates
// the node vector (dangling write). Exercise multi-level subdivision.
TEST(OctreeTest, BuildMultiLevelSubdivisionQueriesAll) {
Octree<Point3D> octree(4, 4);
std::vector<Point3D> pts;
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
for (int k = 0; k < 10; ++k) {
pts.emplace_back(i * 0.1 + 0.03, j * 0.1 + 0.031,
k * 0.1 + 0.032);
}
}
}
octree.build(pts);
ASSERT_EQ(octree.size(), pts.size());
// Every built point must be discoverable (a point may be duplicated by the
// existing closed-boundary child assignment, so only require >= 1 hit).
for (const auto& p : pts) {
AABB3D probe(p, p);
auto hits = octree.query_range(probe);
EXPECT_GE(hits.size(), 1u);
}
}
// Insert() expands only the root bounds after previous subdivisions; items that
// fall outside every existing child cell are kept at the node. Queries must
// still find them, and remove() must still locate them.
TEST(OctreeTest, InsertAfterRootExpansionStaysQueryable) {
Octree<Point3D> octree(4, 8);
std::vector<Point3D> pts = {
{0.1, 0.2, 0.3}, {0.5, 0.5, 0.5}, {0.9, 0.8, 0.7},
{0.3, 0.6, 0.1}, {0.7, 0.1, 0.9}, {0.2, 0.8, 0.4},
{0.8, 0.3, 0.6}, {0.4, 0.7, 0.2}, {0.6, 0.9, 0.5},
};
for (const auto& p : pts) octree.insert(p);
// Far outlier forces a root-bounds expansion after subdivision.
const Point3D outlier(100.0, 100.0, 100.0);
octree.insert(outlier);
EXPECT_EQ(octree.size(), pts.size() + 1);
AABB3D everything(Point3D(-1e6, -1e6, -1e6),
Point3D(1e6, 1e6, 1e6));
auto all = octree.query_range(everything);
EXPECT_GE(all.size(), pts.size() + 1);
EXPECT_NE(std::find(all.begin(), all.end(), outlier), all.end());
for (const auto& p : pts) {
EXPECT_NE(std::find(all.begin(), all.end(), p), all.end());
}
EXPECT_TRUE(octree.remove(outlier));
EXPECT_EQ(octree.size(), pts.size());
auto after = octree.query_range(everything);
EXPECT_EQ(std::find(after.begin(), after.end(), outlier), after.end());
}