305 lines
11 KiB
C++
305 lines
11 KiB
C++
/**
|
||
* @file test_lbvh.cpp
|
||
* @brief VDE G-4:LBVH 构建(CPU 等价 + CUDA 调度诚实语义)与查询一致性
|
||
*/
|
||
#include <gtest/gtest.h>
|
||
|
||
#include "vde/compute/lbvh.h"
|
||
#include "vde/spatial/bvh.h"
|
||
#include "vde/core/line.h"
|
||
#include "vde/core/triangle.h"
|
||
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
#include <cstdint>
|
||
#include <iostream>
|
||
#include <numeric>
|
||
#include <random>
|
||
#include <vector>
|
||
|
||
using namespace vde::compute;
|
||
using namespace vde::spatial;
|
||
using namespace vde::core;
|
||
|
||
namespace {
|
||
|
||
struct Lcg {
|
||
std::uint64_t s = 0x9e3779b97f4a7c15ull;
|
||
double next(double lo, double hi) {
|
||
s = s * 6364136223846793005ull + 1442695040888963407ull;
|
||
const double u = static_cast<double>(s >> 11) /
|
||
static_cast<double>(1ull << 53);
|
||
return lo + (hi - lo) * u;
|
||
}
|
||
};
|
||
|
||
std::vector<double> random_tris(int n, std::uint64_t seed) {
|
||
Lcg rng;
|
||
rng.s = seed;
|
||
std::vector<double> tv(static_cast<size_t>(n) * 9);
|
||
for (int i = 0; i < n; ++i) {
|
||
for (int k = 0; k < 3; ++k) {
|
||
tv[static_cast<size_t>(i) * 9 + k * 3 + 0] = rng.next(-10.0, 10.0);
|
||
tv[static_cast<size_t>(i) * 9 + k * 3 + 1] = rng.next(-10.0, 10.0);
|
||
tv[static_cast<size_t>(i) * 9 + k * 3 + 2] = rng.next(-10.0, 10.0);
|
||
}
|
||
}
|
||
return tv;
|
||
}
|
||
|
||
std::vector<Triangle3D> to_spatial(const std::vector<double>& tv) {
|
||
std::vector<Triangle3D> out;
|
||
const size_t n = tv.size() / 9;
|
||
out.reserve(n);
|
||
for (size_t i = 0; i < n; ++i) {
|
||
const double* p = tv.data() + i * 9;
|
||
out.emplace_back(Point3D(p[0], p[1], p[2]),
|
||
Point3D(p[3], p[4], p[5]),
|
||
Point3D(p[6], p[7], p[8]));
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// ── 暴力参考(与 LBVH 查询共用同一图元谓词,保证口径一致) ──
|
||
bool brute_ray_nearest(const std::vector<double>& tv, const Point3D& o,
|
||
const Vector3D& d, double& out_t, int& out_prim) {
|
||
const int n = static_cast<int>(tv.size() / 9);
|
||
const double ori[3] = {o.x(), o.y(), o.z()};
|
||
const double dir[3] = {d.x(), d.y(), d.z()};
|
||
double best = 1e300;
|
||
int best_prim = -1;
|
||
for (int i = 0; i < n; ++i) {
|
||
double t = 0.0;
|
||
if (lbvh_tri_ray_t(tv.data(), i, ori, dir, &t) && t < best) {
|
||
best = t;
|
||
best_prim = i;
|
||
}
|
||
}
|
||
out_t = best;
|
||
out_prim = best_prim;
|
||
return best_prim >= 0;
|
||
}
|
||
|
||
std::vector<LbvhKnnEntry> brute_knn(const std::vector<double>& tv,
|
||
const Point3D& q, int k) {
|
||
const int n = static_cast<int>(tv.size() / 9);
|
||
const double p[3] = {q.x(), q.y(), q.z()};
|
||
std::vector<LbvhKnnEntry> all;
|
||
all.reserve(static_cast<size_t>(n));
|
||
for (int i = 0; i < n; ++i)
|
||
all.push_back({lbvh_point_triangle_dist_sq(tv.data(), i, p), i});
|
||
std::sort(all.begin(), all.end(), [](const LbvhKnnEntry& a,
|
||
const LbvhKnnEntry& b) {
|
||
if (a.dist_sq != b.dist_sq) return a.dist_sq < b.dist_sq;
|
||
return a.prim < b.prim;
|
||
});
|
||
if (static_cast<int>(all.size()) > k) all.resize(static_cast<size_t>(k));
|
||
return all;
|
||
}
|
||
|
||
std::vector<int> brute_range(const std::vector<double>& tv,
|
||
const double* mn, const double* mx) {
|
||
const int n = static_cast<int>(tv.size() / 9);
|
||
std::vector<int> out;
|
||
for (int i = 0; i < n; ++i)
|
||
if (lbvh_tri_aabb_overlap(tv.data(), i, mn, mx)) out.push_back(i);
|
||
std::sort(out.begin(), out.end());
|
||
return out;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(LbvhTest, Build_10k_PermutationAndRootBounds) {
|
||
const auto tv = random_tris(10000, 42);
|
||
const auto bvh = build_bvh_cpu_lbvh(tv.data(), 10000);
|
||
ASSERT_TRUE(bvh.ok()) << bvh.to_string();
|
||
EXPECT_FALSE(bvh.gpu);
|
||
EXPECT_EQ(bvh.device_count, 0);
|
||
EXPECT_GE(bvh.node_count(), 2u);
|
||
ASSERT_EQ(bvh.prim_order.size(), 10000u);
|
||
// 叶范围覆盖图元且是排列(每个图元恰好一次)
|
||
std::vector<int> seen(10000, 0);
|
||
for (const auto& nd : bvh.nodes) {
|
||
if (!nd.leaf()) continue;
|
||
ASSERT_GE(nd.first_prim, 0);
|
||
ASSERT_LE(nd.first_prim + nd.prim_count, 10000);
|
||
for (int k = 0; k < nd.prim_count; ++k)
|
||
++seen[static_cast<size_t>(bvh.prim_order[static_cast<size_t>(
|
||
nd.first_prim + k)])];
|
||
}
|
||
for (int s : seen) EXPECT_EQ(s, 1);
|
||
// 根节点 AABB = 全部图元 AABB 并集
|
||
double mn[3] = {1e300, 1e300, 1e300};
|
||
double mx[3] = {-1e300, -1e300, -1e300};
|
||
for (int i = 0; i < 10000; ++i) {
|
||
double bmn[3], bmx[3];
|
||
lbvh_tri_aabb_overlap(tv.data(), i, bmn, bmx); // no-op 仅占位
|
||
for (int a = 0; a < 3; ++a) {
|
||
const double* p = tv.data() + static_cast<size_t>(i) * 9;
|
||
const double lo = std::min({p[a], p[3 + a], p[6 + a]});
|
||
const double hi = std::max({p[a], p[3 + a], p[6 + a]});
|
||
mn[a] = std::min(mn[a], lo);
|
||
mx[a] = std::max(mx[a], hi);
|
||
}
|
||
}
|
||
ASSERT_FALSE(bvh.nodes.empty());
|
||
for (int a = 0; a < 3; ++a) {
|
||
EXPECT_NEAR(bvh.nodes[0].bmin[a], mn[a], 1e-12);
|
||
EXPECT_NEAR(bvh.nodes[0].bmax[a], mx[a], 1e-12);
|
||
}
|
||
}
|
||
|
||
TEST(LbvhTest, Ray_Knn_Range_WorstZero_vsBruteForce) {
|
||
const auto tv = random_tris(10000, 7);
|
||
const auto bvh = build_bvh_cpu_lbvh(tv.data(), 10000);
|
||
ASSERT_TRUE(bvh.ok());
|
||
Lcg rng;
|
||
rng.s = 12345;
|
||
|
||
// 射线最近命中(与暴力参考逐项一致)
|
||
int ray_mismatch = 0;
|
||
double max_t_diff = 0.0;
|
||
for (int q = 0; q < 64; ++q) {
|
||
Point3D o(rng.next(-12, 12), rng.next(-12, 12), rng.next(-12, 12));
|
||
Vector3D d(rng.next(-1, 1), rng.next(-1, 1), rng.next(-1, 1));
|
||
d.normalize();
|
||
LbvhRayHit hit;
|
||
const bool lb = lbvh_ray_nearest(bvh, tv.data(), &o.x(), &d.x(), &hit);
|
||
double bt = 0.0;
|
||
int bp = -1;
|
||
const bool bb = brute_ray_nearest(tv, o, d, bt, bp);
|
||
const double lb_t = (hit.t > 1e299) ? -1.0 : hit.t;
|
||
if (lb != bb) {
|
||
++ray_mismatch;
|
||
continue;
|
||
}
|
||
if (lb) {
|
||
max_t_diff = std::max(max_t_diff, std::abs(lb_t - bt));
|
||
if (std::abs(lb_t - bt) > 1e-12) ++ray_mismatch;
|
||
}
|
||
}
|
||
EXPECT_EQ(ray_mismatch, 0);
|
||
|
||
// k 近邻(k=8,距离序列逐项一致)
|
||
int knn_mismatch = 0;
|
||
for (int q = 0; q < 64; ++q) {
|
||
Point3D p(rng.next(-12, 12), rng.next(-12, 12), rng.next(-12, 12));
|
||
const auto got = lbvh_knn(bvh, tv.data(), &p.x(), 8);
|
||
const auto want = brute_knn(tv, p, 8);
|
||
ASSERT_EQ(got.size(), want.size());
|
||
for (size_t i = 0; i < got.size(); ++i)
|
||
if (got[i].prim != want[i].prim ||
|
||
std::abs(got[i].dist_sq - want[i].dist_sq) > 1e-12)
|
||
++knn_mismatch;
|
||
}
|
||
EXPECT_EQ(knn_mismatch, 0);
|
||
|
||
// 范围查询(AABB 相交图元集合完全一致)
|
||
int range_mismatch = 0;
|
||
for (int q = 0; q < 32; ++q) {
|
||
const double cx = rng.next(-10, 10), cy = rng.next(-10, 10),
|
||
cz = rng.next(-10, 10);
|
||
const double e = rng.next(0.5, 6.0);
|
||
const double mn[3] = {cx - e, cy - e, cz - e};
|
||
const double mx[3] = {cx + e, cy + e, cz + e};
|
||
EXPECT_EQ(lbvh_range_aabb(bvh, tv.data(), mn, mx),
|
||
brute_range(tv, mn, mx));
|
||
}
|
||
EXPECT_EQ(range_mismatch, 0);
|
||
|
||
// 与现有 spatial::BVH 交叉核对最近命中
|
||
BVH spatial_bvh;
|
||
spatial_bvh.build(to_spatial(tv));
|
||
int bvh_mismatch = 0;
|
||
rng.s = 999;
|
||
for (int q = 0; q < 64; ++q) {
|
||
Point3D o(rng.next(-12, 12), rng.next(-12, 12), rng.next(-12, 12));
|
||
Vector3D d(rng.next(-1, 1), rng.next(-1, 1), rng.next(-1, 1));
|
||
d.normalize();
|
||
LbvhRayHit hit;
|
||
const bool lb = lbvh_ray_nearest(bvh, tv.data(), &o.x(), &d.x(), &hit);
|
||
const auto sp = spatial_bvh.query_ray_nearest(Ray3Dd(o, d));
|
||
if (lb != sp.has_value()) {
|
||
++bvh_mismatch;
|
||
continue;
|
||
}
|
||
if (lb && std::abs(hit.t - sp->t) > 1e-12) ++bvh_mismatch;
|
||
}
|
||
EXPECT_EQ(bvh_mismatch, 0);
|
||
|
||
std::cout << "[G4] ray_mismatch=" << ray_mismatch
|
||
<< " max_t_diff=" << max_t_diff
|
||
<< " knn_mismatch=" << knn_mismatch
|
||
<< " range_mismatch=" << range_mismatch
|
||
<< " bvh_mismatch=" << bvh_mismatch << std::endl;
|
||
}
|
||
|
||
TEST(LbvhTest, EmptyAndDegenerateInputs_NoCrash) {
|
||
{ // 空输入
|
||
auto r = build_bvh_cpu_lbvh(nullptr, 0);
|
||
EXPECT_EQ(r.status, BvhBuildStatus::InvalidInput);
|
||
EXPECT_FALSE(r.ok());
|
||
auto r2 = build_bvh_cpu_lbvh(nullptr, 100);
|
||
EXPECT_EQ(r2.status, BvhBuildStatus::InvalidInput);
|
||
auto r3 = build_bvh(nullptr, 0);
|
||
EXPECT_EQ(r3.status, BvhBuildStatus::InvalidInput);
|
||
EXPECT_FALSE(lbvh_ray_nearest(r, nullptr, nullptr, nullptr, nullptr));
|
||
EXPECT_TRUE(lbvh_knn(r, nullptr, nullptr, 4).empty());
|
||
EXPECT_TRUE(lbvh_range_aabb(r, nullptr, nullptr, nullptr).empty());
|
||
}
|
||
{ // 退化:全部三角形重合(质心全同)
|
||
std::vector<double> tv(300 * 9, 0.0);
|
||
for (int i = 0; i < 300; ++i) {
|
||
for (int k = 0; k < 3; ++k) {
|
||
tv[static_cast<size_t>(i) * 9 + k * 3 + 0] = 0.0;
|
||
tv[static_cast<size_t>(i) * 9 + k * 3 + 1] = 0.0;
|
||
tv[static_cast<size_t>(i) * 9 + k * 3 + 2] = 0.0;
|
||
}
|
||
}
|
||
const auto r = build_bvh_cpu_lbvh(tv.data(), 300);
|
||
ASSERT_TRUE(r.ok()) << r.to_string();
|
||
LbvhRayHit hit;
|
||
EXPECT_FALSE(lbvh_ray_nearest(r, tv.data(), nullptr, nullptr, &hit));
|
||
EXPECT_NO_THROW(lbvh_knn(r, tv.data(), nullptr, 4));
|
||
EXPECT_NO_THROW(lbvh_range_aabb(r, tv.data(), nullptr, nullptr));
|
||
}
|
||
{ // 分块构建:两份子集分别构建不崩、根 AABB 有效
|
||
const auto tv = random_tris(1000, 3);
|
||
for (int half = 0; half < 2; ++half) {
|
||
const double* base = tv.data() + static_cast<size_t>(half) * 500 * 9;
|
||
const auto r = build_bvh_cpu_lbvh(base, 500);
|
||
ASSERT_TRUE(r.ok());
|
||
EXPECT_FALSE(r.nodes.empty());
|
||
for (int a = 0; a < 3; ++a)
|
||
EXPECT_LE(r.nodes[0].bmin[a], r.nodes[0].bmax[a]);
|
||
}
|
||
}
|
||
}
|
||
|
||
TEST(LbvhTest, CudaDispatch_HonestUnsupported_AndCpuFallback) {
|
||
const auto tv = random_tris(1000, 11);
|
||
const auto cuda = build_bvh_cuda(tv.data(), 1000);
|
||
if (cuda.status == BvhBuildStatus::Ok) {
|
||
// 宿主若有 CUDA 工具链且设备可用:允许但本机未验证
|
||
EXPECT_TRUE(cuda.gpu);
|
||
EXPECT_GT(cuda.device_count, 0);
|
||
} else {
|
||
EXPECT_EQ(cuda.status, BvhBuildStatus::Unsupported);
|
||
EXPECT_FALSE(cuda.gpu);
|
||
EXPECT_EQ(cuda.device_count, 0);
|
||
EXPECT_FALSE(cuda.ok());
|
||
EXPECT_STREQ(bvh_build_status_name(cuda.status), "unsupported");
|
||
}
|
||
// 统一入口:CUDA 不可用 → CPU LBVH(保证有结果且诚实 gpu=false)
|
||
const auto all = build_bvh(tv.data(), 1000);
|
||
ASSERT_TRUE(all.ok()) << all.to_string();
|
||
EXPECT_FALSE(all.gpu);
|
||
if (cuda.status != BvhBuildStatus::Ok) EXPECT_EQ(all.device_count, 0);
|
||
// 强制 CPU
|
||
LbvhBuildOptions opts;
|
||
opts.prefer_cuda = false;
|
||
const auto cpu = build_bvh(tv.data(), 1000, opts);
|
||
EXPECT_TRUE(cpu.ok());
|
||
EXPECT_FALSE(cpu.gpu);
|
||
}
|