New tests: capi/test_capi.cpp (C API 9 functions) curves/test_curve_intersect.cpp (3 tests) curves/test_iga_prep.cpp (5 tests) mesh/test_medial_axis.cpp (3 tests) mesh/test_mesh_quality.cpp (4 tests) mesh/test_grid_mesh.cpp (3 tests) boolean/test_boolean_edge_cases.cpp (5 tests) collision/test_gjk.cpp (3 tests) spatial/test_octree.cpp (4 tests) spatial/test_bvh.cpp (4 tests) spatial/test_spatial_index.cpp (3 tests) sketch/test_sketch_ops.cpp (4 tests) foundation/test_io_utils.cpp (3 tests) Enhanced: mesh/test_mesh_boolean (4 tests) mesh/test_hex_mesh (3 tests) Total: ~60+ new test cases
195 lines
5.1 KiB
C++
195 lines
5.1 KiB
C++
#include <gtest/gtest.h>
|
|
#include "vde/spatial/bvh.h"
|
|
#include "vde/core/triangle.h"
|
|
#include "vde/core/line.h"
|
|
#include <vector>
|
|
#include <cmath>
|
|
|
|
using namespace vde::spatial;
|
|
using namespace vde::core;
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// BVH 测试: build / intersect / update / query_ray
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
static std::vector<Triangle3D> make_triangles() {
|
|
return {
|
|
Triangle3D({0,0,0}, {1,0,0}, {0,1,0}),
|
|
Triangle3D({0,0,0}, {0,1,0}, {0,0,1}),
|
|
Triangle3D({0,0,0}, {0,0,1}, {1,0,0}),
|
|
Triangle3D({1,1,1}, {1,0,1}, {0,1,1}),
|
|
};
|
|
}
|
|
|
|
// ── build ────────────────────────────────────────────
|
|
|
|
TEST(BVHTest, BuildEmpty) {
|
|
BVH bvh;
|
|
bvh.build({});
|
|
EXPECT_EQ(bvh.size(), 0u);
|
|
}
|
|
|
|
TEST(BVHTest, BuildSingle) {
|
|
BVH bvh;
|
|
Triangle3D tri({0,0,0}, {1,0,0}, {0,1,0});
|
|
bvh.build({tri});
|
|
EXPECT_EQ(bvh.size(), 1u);
|
|
}
|
|
|
|
TEST(BVHTest, BuildMultiple) {
|
|
BVH bvh;
|
|
auto tris = make_triangles();
|
|
bvh.build(tris);
|
|
EXPECT_EQ(bvh.size(), tris.size());
|
|
}
|
|
|
|
TEST(BVHTest, BuildWithStrategy) {
|
|
BVHBuildOptions opts;
|
|
opts.strategy = BVHSplitStrategy::Middle;
|
|
opts.leaf_size = 2;
|
|
|
|
BVH bvh(opts);
|
|
auto tris = make_triangles();
|
|
bvh.build(tris);
|
|
EXPECT_EQ(bvh.size(), tris.size());
|
|
}
|
|
|
|
TEST(BVHTest, BuildSAHStrategy) {
|
|
BVHBuildOptions opts;
|
|
opts.strategy = BVHSplitStrategy::SAH;
|
|
opts.leaf_size = 1;
|
|
|
|
BVH bvh(opts);
|
|
bvh.build(make_triangles());
|
|
EXPECT_GT(bvh.size(), 0u);
|
|
}
|
|
|
|
// ── intersect (query_range) ──────────────────────────
|
|
|
|
TEST(BVHTest, IntersectRange) {
|
|
BVH bvh;
|
|
bvh.build(make_triangles());
|
|
|
|
// 范围查询
|
|
AABB3D range(Point3D(0, 0, 0), Point3D(0.5, 0.5, 0.5));
|
|
auto results = bvh.query_range(range);
|
|
|
|
EXPECT_GE(results.size(), 1u);
|
|
}
|
|
|
|
TEST(BVHTest, IntersectRangeEmpty) {
|
|
BVH bvh;
|
|
bvh.build(make_triangles());
|
|
|
|
// 范围远离三角形
|
|
AABB3D range(Point3D(100, 100, 100), Point3D(101, 101, 101));
|
|
auto results = bvh.query_range(range);
|
|
|
|
EXPECT_EQ(results.size(), 0u);
|
|
}
|
|
|
|
TEST(BVHTest, IntersectRangeFull) {
|
|
BVH bvh;
|
|
auto tris = make_triangles();
|
|
bvh.build(tris);
|
|
|
|
// 全覆盖
|
|
AABB3D range(Point3D(-1, -1, -1), Point3D(2, 2, 2));
|
|
auto results = bvh.query_range(range);
|
|
|
|
EXPECT_EQ(results.size(), tris.size());
|
|
}
|
|
|
|
// ── update (insert/remove) ───────────────────────────
|
|
|
|
TEST(BVHTest, UpdateInsert) {
|
|
BVH bvh;
|
|
bvh.insert(Triangle3D({0,0,0}, {1,0,0}, {0,1,0}));
|
|
EXPECT_EQ(bvh.size(), 1u);
|
|
|
|
bvh.insert(Triangle3D({1,0,0}, {1,1,0}, {0,1,0}));
|
|
EXPECT_EQ(bvh.size(), 2u);
|
|
}
|
|
|
|
TEST(BVHTest, UpdateRemove) {
|
|
BVH bvh;
|
|
Triangle3D tri({0,0,0}, {1,0,0}, {0,1,0});
|
|
bvh.insert(tri);
|
|
EXPECT_EQ(bvh.size(), 1u);
|
|
|
|
bool removed = bvh.remove(tri);
|
|
EXPECT_TRUE(removed);
|
|
EXPECT_EQ(bvh.size(), 0u);
|
|
}
|
|
|
|
TEST(BVHTest, UpdateRemoveNonExistent) {
|
|
BVH bvh;
|
|
Triangle3D tri1({0,0,0}, {1,0,0}, {0,1,0});
|
|
bvh.insert(tri1);
|
|
EXPECT_EQ(bvh.size(), 1u);
|
|
|
|
Triangle3D tri2({100,100,100}, {200,100,100}, {100,200,100});
|
|
bool removed = bvh.remove(tri2);
|
|
EXPECT_FALSE(removed);
|
|
EXPECT_EQ(bvh.size(), 1u);
|
|
}
|
|
|
|
// ── query_ray ────────────────────────────────────────
|
|
|
|
TEST(BVHTest, QueryRayHit) {
|
|
BVH bvh;
|
|
bvh.build(make_triangles());
|
|
|
|
// 沿 +z 轴发射射线应命中三角形
|
|
Ray3Dd ray(Point3D(0.2, 0.2, -1), Vector3D(0, 0, 1));
|
|
auto results = bvh.query_ray(ray);
|
|
|
|
EXPECT_GE(results.size(), 1u);
|
|
}
|
|
|
|
TEST(BVHTest, QueryRayMiss) {
|
|
BVH bvh;
|
|
bvh.build(make_triangles());
|
|
|
|
// 远离的射线
|
|
Ray3Dd ray(Point3D(100, 100, 100), Vector3D(0, 0, 1));
|
|
auto results = bvh.query_ray(ray);
|
|
|
|
EXPECT_EQ(results.size(), 0u);
|
|
}
|
|
|
|
TEST(BVHTest, QueryRayNearest) {
|
|
BVH bvh;
|
|
bvh.build(make_triangles());
|
|
|
|
Ray3Dd ray(Point3D(0.2, 0.2, -1), Vector3D(0, 0, 1));
|
|
auto hit = bvh.query_ray_nearest(ray);
|
|
|
|
if (hit.has_value()) {
|
|
EXPECT_GT(hit->t, 0.0);
|
|
EXPECT_GE(hit->tri_index, 0);
|
|
}
|
|
}
|
|
|
|
// ── KNN ──────────────────────────────────────────────
|
|
|
|
TEST(BVHTest, QueryKNN) {
|
|
BVH bvh;
|
|
bvh.build(make_triangles());
|
|
|
|
auto results = bvh.query_knn(Point3D(0, 0, 0), 2);
|
|
EXPECT_GE(results.size(), 1u);
|
|
EXPECT_LE(results.size(), 2u);
|
|
}
|
|
|
|
// ── clear ────────────────────────────────────────────
|
|
|
|
TEST(BVHTest, ClearBVH) {
|
|
BVH bvh;
|
|
bvh.build(make_triangles());
|
|
EXPECT_GT(bvh.size(), 0u);
|
|
|
|
bvh.clear();
|
|
EXPECT_EQ(bvh.size(), 0u);
|
|
}
|