Files

594 lines
24 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <gtest/gtest.h>
#include "vde/compute/compute_backend.h"
#include "vde/core/simd_vector.h"
#include "vde/collision/tri_intersect.h"
#include "vde/core/triangle.h"
#include "vde/mesh/voxelize.h"
#include "vde/mesh/marching_cubes.h"
#include <cmath>
#include <map>
#include <random>
#include <utility>
#include <vector>
using namespace vde::compute;
using namespace vde::core;
namespace {
// 标量球体 SDF(与 marching_cubes.h::sdf_sphere 一致)
double ref_sphere(double x, double y, double z, double cx, double cy, double cz, double r) {
return std::sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy) + (z - cz) * (z - cz)) - r;
}
// 标量盒 SDF(与 marching_cubes.h::sdf_box 一致)
double ref_box(double x, double y, double z, double hx, double hy, double hz) {
double dx = std::abs(x) - hx, dy = std::abs(y) - hy, dz = std::abs(z) - hz;
return std::sqrt(std::max(dx, 0.0) * std::max(dx, 0.0) +
std::max(dy, 0.0) * std::max(dy, 0.0) +
std::max(dz, 0.0) * std::max(dz, 0.0)) +
std::min(std::max({dx, dy, dz}), 0.0);
}
// 水密性检查(O(F))
bool is_watertight(const vde::mesh::MCMesh& mc) {
std::map<std::pair<int, int>, int> edge_cnt;
for (const auto& t : mc.triangles) {
for (int k = 0; k < 3; ++k) {
int a = t[k], b = t[(k + 1) % 3];
if (a > b) std::swap(a, b);
++edge_cnt[{a, b}];
}
}
if (edge_cnt.empty()) return false;
for (const auto& [key, cnt] : edge_cnt)
if (cnt != 2) return false;
return true;
}
// 由 9 个坐标(v0.xyz,v1.xyz,v2.xyz)构造 Triangle3D,供与 collision 口径交叉验证
Triangle3D tri_from9(const double* t) {
return Triangle3D(Point3D(t[0], t[1], t[2]),
Point3D(t[3], t[4], t[5]),
Point3D(t[6], t[7], t[8]));
}
} // namespace
// ═══════════════════════════════════════════════════════════
// CPU SIMD 后端
// ═══════════════════════════════════════════════════════════
TEST(ComputeBackendTest, CpuSimd_Info) {
CpuSimdComputeBackend b;
const BackendInfo info = b.info();
EXPECT_EQ(info.type, BackendType::CpuSimd);
EXPECT_FALSE(info.gpu);
EXPECT_EQ(info.lanes, simd_width_d);
EXPECT_EQ(info.name, std::string("cpu-simd(") + [] {
switch (current_simd_isa()) {
case SimdIsa::AVX: return "avx";
case SimdIsa::SSE2: return "sse";
case SimdIsa::NEON: return "neon";
default: return "scalar";
}
}() + ")");
}
TEST(ComputeBackendTest, CpuSimd_SphereBatch_MatchesScalar) {
CpuSimdComputeBackend b;
std::mt19937 rng(42);
std::uniform_real_distribution<double> uni(-2.0, 2.0);
const int n = 1003; // 含非 4 倍数尾部
std::vector<double> xs(n), ys(n), zs(n), simd(n), ref(n);
for (int i = 0; i < n; ++i) {
xs[i] = uni(rng);
ys[i] = uni(rng);
zs[i] = uni(rng);
}
b.sdf_sphere_batch(xs.data(), ys.data(), zs.data(), simd.data(), n, 0.3, -0.2, 0.1, 1.25);
for (int i = 0; i < n; ++i) {
ref[i] = ref_sphere(xs[i], ys[i], zs[i], 0.3, -0.2, 0.1, 1.25);
EXPECT_NEAR(simd[i], ref[i], 1e-12) << "i=" << i;
}
}
TEST(ComputeBackendTest, CpuSimd_BoxBatch_MatchesScalar) {
CpuSimdComputeBackend b;
std::mt19937 rng(7);
std::uniform_real_distribution<double> uni(-2.0, 2.0);
const int n = 502; // 非 4 倍数
std::vector<double> xs(n), ys(n), zs(n), simd(n), ref(n);
for (int i = 0; i < n; ++i) {
xs[i] = uni(rng);
ys[i] = uni(rng);
zs[i] = uni(rng);
}
b.sdf_box_batch(xs.data(), ys.data(), zs.data(), simd.data(), n, 0.8, 0.5, 1.1);
for (int i = 0; i < n; ++i) {
ref[i] = ref_box(xs[i], ys[i], zs[i], 0.8, 0.5, 1.1);
EXPECT_NEAR(simd[i], ref[i], 1e-12) << "i=" << i;
}
}
TEST(ComputeBackendTest, CpuSimd_AabbIntersect_MatchesScalar) {
CpuSimdComputeBackend b;
std::mt19937 rng(99);
std::uniform_real_distribution<double> uni(-3.0, 3.0);
std::uniform_real_distribution<double> sz(0.1, 1.0);
const size_t n = 257; // 非 4 倍数
std::vector<double> mins(n * 3), maxs(n * 3);
for (size_t i = 0; i < n; ++i) {
const double cx = uni(rng), cy = uni(rng), cz = uni(rng);
const double hx = sz(rng), hy = sz(rng), hz = sz(rng);
mins[i * 3 + 0] = cx - hx; maxs[i * 3 + 0] = cx + hx;
mins[i * 3 + 1] = cy - hy; maxs[i * 3 + 1] = cy + hy;
mins[i * 3 + 2] = cz - hz; maxs[i * 3 + 2] = cz + hz;
}
const double rx0 = -0.5, ry0 = -0.5, rz0 = -0.5;
const double rx1 = 0.5, ry1 = 0.5, rz1 = 0.5;
std::vector<int> simd_hits(n), ref_hits(n);
const size_t ns = b.aabb_intersect(mins.data(), maxs.data(), n,
rx0, ry0, rz0, rx1, ry1, rz1, simd_hits.data());
size_t nr = 0;
for (size_t i = 0; i < n; ++i) {
if (mins[i * 3 + 0] <= rx1 && maxs[i * 3 + 0] >= rx0 &&
mins[i * 3 + 1] <= ry1 && maxs[i * 3 + 1] >= ry0 &&
mins[i * 3 + 2] <= rz1 && maxs[i * 3 + 2] >= rz0)
ref_hits[nr++] = static_cast<int>(i);
}
ASSERT_EQ(ns, nr);
for (size_t i = 0; i < ns; ++i)
EXPECT_EQ(simd_hits[i], ref_hits[i]) << "hit #" << i;
}
// ── 视锥剔除标量参考(p-vertex 测试,与接口语义逐条对应)──
// planes[k*4 + {0,1,2,3}] = (a,b,c,d),法线朝内:内点 ⇔ a·x+b·y+c·z+d ≥ 0
size_t ref_frustum_cull(const double* mins, const double* maxs, size_t n,
const double* planes, std::vector<int>& out) {
out.clear();
for (size_t i = 0; i < n; ++i) {
bool visible = true;
for (int k = 0; k < 6 && visible; ++k) {
const double a = planes[k * 4 + 0], b = planes[k * 4 + 1];
const double c = planes[k * 4 + 2], d = planes[k * 4 + 3];
const double px = (a >= 0.0) ? maxs[i * 3 + 0] : mins[i * 3 + 0];
const double py = (b >= 0.0) ? maxs[i * 3 + 1] : mins[i * 3 + 1];
const double pz = (c >= 0.0) ? maxs[i * 3 + 2] : mins[i * 3 + 2];
if (a * px + b * py + c * pz + d < 0.0) visible = false;
}
if (visible) out.push_back(static_cast<int>(i));
}
return out.size();
}
// 轴对齐盒视锥 [-2,2]³ 的 6 个内向平面
void box_frustum_planes(double* planes) {
const double p[6][4] = {
{ 1, 0, 0, 2}, {-1, 0, 0, 2},
{ 0, 1, 0, 2}, { 0,-1, 0, 2},
{ 0, 0, 1, 2}, { 0, 0,-1, 2},
};
for (int k = 0; k < 6; ++k)
for (int j = 0; j < 4; ++j) planes[k * 4 + j] = p[k][j];
}
TEST(ComputeBackendTest, CpuSimd_FrustumCull_MatchesScalar) {
CpuSimdComputeBackend b;
std::mt19937 rng(2024);
std::uniform_real_distribution<double> uni(-5.0, 5.0);
std::uniform_real_distribution<double> sz(0.1, 1.5);
std::normal_distribution<double> nrm(0.0, 1.0);
const size_t n = 259; // 非 4 倍数
std::vector<double> mins(n * 3), maxs(n * 3);
for (size_t i = 0; i < n; ++i) {
const double cx = uni(rng), cy = uni(rng), cz = uni(rng);
const double hx = sz(rng), hy = sz(rng), hz = sz(rng);
mins[i * 3 + 0] = cx - hx; maxs[i * 3 + 0] = cx + hx;
mins[i * 3 + 1] = cy - hy; maxs[i * 3 + 1] = cy + hy;
mins[i * 3 + 2] = cz - hz; maxs[i * 3 + 2] = cz + hz;
}
// 6 个随机平面(法线归一化,d 随机)
std::vector<double> planes(6 * 4);
for (int k = 0; k < 6; ++k) {
double a = nrm(rng), bb = nrm(rng), c = nrm(rng);
const double len = std::sqrt(a * a + bb * bb + c * c);
a /= len; bb /= len; c /= len;
planes[k * 4 + 0] = a; planes[k * 4 + 1] = bb;
planes[k * 4 + 2] = c; planes[k * 4 + 3] = uni(rng);
}
std::vector<int> simd_hits(n), ref_hits;
const size_t ns = b.frustum_cull_batch(mins.data(), maxs.data(), n,
planes.data(), simd_hits.data());
const size_t nr = ref_frustum_cull(mins.data(), maxs.data(), n,
planes.data(), ref_hits);
ASSERT_EQ(ns, nr);
for (size_t i = 0; i < ns; ++i)
EXPECT_EQ(simd_hits[i], ref_hits[i]) << "visible #" << i;
}
TEST(ComputeBackendTest, CpuSimd_FrustumCull_SemanticsAndBoundary) {
CpuSimdComputeBackend b;
double planes[6 * 4];
box_frustum_planes(planes);
// 盒列表:0=内部, 1=外部(+X 远处→剔除), 2=跨界(z 跨 2 平面→保守保留),
// 3=贴面(x∈[2,2],p-vertex 距离=0→保留)
double mins[4 * 3] = {
-1, -1, -1, // 0
3, 0, 0, // 1
-1, -1, -1, // 2
2, -1, -1, // 3
};
double maxs[4 * 3] = {
1, 1, 1, // 0
4, 1, 1, // 1
1, 1, 3, // 2
2, 1, 1, // 3
};
std::vector<int> hits(4);
const size_t nh = b.frustum_cull_batch(mins, maxs, 4, planes, hits.data());
ASSERT_EQ(nh, 3u);
EXPECT_EQ(hits[0], 0);
EXPECT_EQ(hits[1], 2); // 跨界:AABB 与平面相交即保留(保守)
EXPECT_EQ(hits[2], 3); // 贴面:距离≥0 保留
// 退化:n=0 → 0,不写越界、不崩溃
int dummy = -1;
EXPECT_EQ(b.frustum_cull_batch(mins, maxs, 0, planes, &dummy), 0u);
EXPECT_EQ(dummy, -1);
}
TEST(ComputeBackendTest, CpuSimd_TriTriIntersect_KnownCasesAndBoundary) {
CpuSimdComputeBackend b;
// 三对三角形:0=平行分离, 1=共面重叠, 2=非共面穿越(与 collision 既有用例同形)
double a[3 * 9] = {
0, 0, 0, 1, 0, 0, 0, 1, 0, // 0
0, 0, 0, 2, 0, 0, 0, 2, 0, // 1
0, 0, 0, 2, 0, 0, 0, 2, 0, // 2
};
double c[3 * 9] = {
0, 0, 5, 1, 0, 5, 0, 1, 5, // 0:z 平移 → 分离
1, 0, 0, 3, 0, 0, 1, 2, 0, // 1:共面重叠
1, -1, 0, 1, 1, 0, 1, 0, 3, // 2:跨平面穿越
};
std::vector<int> hits(3);
const size_t nh = b.triangle_intersect_batch(a, c, 3, hits.data());
ASSERT_EQ(nh, 2u);
EXPECT_EQ(hits[0], 1);
EXPECT_EQ(hits[1], 2);
// 边界:n=0 → 0,不写越界、不崩溃
int dummy = -1;
EXPECT_EQ(b.triangle_intersect_batch(a, c, 0, &dummy), 0u);
EXPECT_EQ(dummy, -1);
}
TEST(ComputeBackendTest, CpuSimd_TriTriIntersect_CrossCheckCollision) {
// CPU 批量核必须与既有 collision::tri_tri_intersect 口径逐项一致
CpuSimdComputeBackend b;
std::mt19937 rng(321);
std::uniform_real_distribution<double> uni(-1.5, 1.5);
const size_t n = 401; // 含素数长度
std::vector<double> ta(n * 9), tb(n * 9);
for (size_t i = 0; i < n; ++i) {
for (int k = 0; k < 9; ++k) ta[i * 9 + k] = uni(rng);
if (i % 3 == 0) {
// 三分之一的样本强制共面(z=0),覆盖共面 9 轴分支
for (int v = 0; v < 3; ++v) {
ta[i * 9 + v * 3 + 2] = 0.0;
tb[i * 9 + v * 3 + 0] = uni(rng);
tb[i * 9 + v * 3 + 1] = uni(rng);
tb[i * 9 + v * 3 + 2] = 0.0;
}
} else {
for (int k = 0; k < 9; ++k) tb[i * 9 + k] = uni(rng);
}
}
std::vector<int> hits(n);
const size_t nh = b.triangle_intersect_batch(ta.data(), tb.data(), n, hits.data());
std::vector<int> ref;
for (size_t i = 0; i < n; ++i) {
if (vde::collision::tri_tri_intersect(tri_from9(&ta[i * 9]),
tri_from9(&tb[i * 9])))
ref.push_back(static_cast<int>(i));
}
ASSERT_EQ(nh, ref.size());
for (size_t i = 0; i < nh; ++i) EXPECT_EQ(hits[i], ref[i]) << "pair #" << i;
}
TEST(ComputeBackendTest, CudaBackend_FrustumCull_FallsBackEquivalent) {
auto cuda = create_backend(BackendType::Cuda);
CpuSimdComputeBackend cpu;
ASSERT_NE(cuda, nullptr);
double planes[6 * 4];
box_frustum_planes(planes);
const size_t n = 37;
std::vector<double> mins(n * 3), maxs(n * 3);
std::mt19937 rng(5);
std::uniform_real_distribution<double> uni(-4.0, 4.0);
for (size_t i = 0; i < n; ++i) {
const double c0 = uni(rng), c1 = uni(rng), c2 = uni(rng);
mins[i * 3 + 0] = c0 - 0.3; maxs[i * 3 + 0] = c0 + 0.3;
mins[i * 3 + 1] = c1 - 0.3; maxs[i * 3 + 1] = c1 + 0.3;
mins[i * 3 + 2] = c2 - 0.3; maxs[i * 3 + 2] = c2 + 0.3;
}
std::vector<int> hc(n), hs(n);
const size_t nc = cuda->frustum_cull_batch(mins.data(), maxs.data(), n,
planes, hc.data());
const size_t nsc = cpu.frustum_cull_batch(mins.data(), maxs.data(), n,
planes, hs.data());
ASSERT_EQ(nc, nsc);
for (size_t i = 0; i < nc; ++i) EXPECT_EQ(hc[i], hs[i]) << "diff #" << i;
}
// ═══════════════════════════════════════════════════════════
// 工厂与默认后端
// ═══════════════════════════════════════════════════════════
TEST(ComputeBackendTest, Factory_DefaultBackend_Executes) {
ComputeBackend& b = default_backend();
const BackendInfo info = b.info();
EXPECT_GE(info.lanes, 1);
const double xs[8] = {0, 0.5, -0.5, 1.0, 2.0, -2.0, 0.0, 0.0};
const double ys[8] = {0, 0, 0, 0, 0, 0, 0.5, 0};
const double zs[8] = {0, 0, 0, 0, 0, 0, 0, 0.5};
double out[8];
b.sdf_sphere_batch(xs, ys, zs, out, 8, 0, 0, 0, 1.0);
for (int i = 0; i < 8; ++i)
EXPECT_NEAR(out[i], ref_sphere(xs[i], ys[i], zs[i], 0, 0, 0, 1.0), 1e-12);
}
TEST(ComputeBackendTest, OpenClBackend_GpuDeviceIsReported) {
auto b = create_backend(BackendType::OpenCl);
ASSERT_NE(b, nullptr);
const BackendInfo info = b->info();
if (!info.gpu) GTEST_SKIP() << "no OpenCL GPU device in this environment";
EXPECT_EQ(info.type, BackendType::OpenCl);
EXPECT_GT(info.device_count, 0);
EXPECT_FALSE(info.device_name.empty());
EXPECT_FALSE(info.device_vendor.empty());
EXPECT_NE(info.name.find("opencl(gpu)"), std::string::npos);
}
TEST(ComputeBackendTest, OpenClBackend_AvailableOrFallsBack) {
auto b = create_backend(BackendType::OpenCl);
ASSERT_NE(b, nullptr);
const BackendInfo info = b->info();
EXPECT_EQ(info.type, BackendType::OpenCl);
EXPECT_TRUE(info.gpu || info.name.find("cpu-fallback") != std::string::npos);
std::vector<double> xs, ys, zs, out, ref;
for (int i = 0; i < 64; ++i) {
xs.push_back(0.1 * i - 1.0);
ys.push_back(0.05 * i - 0.5);
zs.push_back(0.02 * i);
}
out.resize(xs.size());
ref.resize(xs.size());
CpuSimdComputeBackend cpu;
b->sdf_sphere_batch(xs.data(), ys.data(), zs.data(), out.data(),
static_cast<int>(xs.size()), 0.25, -0.1, 0.3, 1.5);
cpu.sdf_sphere_batch(xs.data(), ys.data(), zs.data(), ref.data(),
static_cast<int>(xs.size()), 0.25, -0.1, 0.3, 1.5);
for (size_t i = 0; i < out.size(); ++i)
EXPECT_NEAR(out[i], ref[i], 1e-9) << "i=" << i;
// AABB / frustum 批量核与 CPU 结果一致(索引集合)
const int nb = 137;
std::vector<double> mins(nb * 3), maxs(nb * 3);
std::mt19937 rng(7);
std::uniform_real_distribution<double> u(-2.0, 2.0);
for (int i = 0; i < nb; ++i) {
for (int k = 0; k < 3; ++k) {
const double a = u(rng), b = u(rng);
mins[i * 3 + k] = std::min(a, b);
maxs[i * 3 + k] = std::max(a, b);
}
}
std::vector<int> ga(nb), gc(nb), fa(nb), fc(nb);
const size_t nga = b->aabb_intersect(mins.data(), maxs.data(), nb, -0.5, -0.5,
-0.5, 0.5, 0.5, 0.5, ga.data());
const size_t ngc = cpu.aabb_intersect(mins.data(), maxs.data(), nb, -0.5, -0.5,
-0.5, 0.5, 0.5, 0.5, gc.data());
ga.resize(nga); gc.resize(ngc);
std::sort(ga.begin(), ga.end()); std::sort(gc.begin(), gc.end());
EXPECT_EQ(ga, gc);
const double planes[24] = {1,0,0,2, -1,0,0,2, 0,1,0,2, 0,-1,0,2, 0,0,1,2, 0,0,-1,2};
const size_t nfa = b->frustum_cull_batch(mins.data(), maxs.data(), nb, planes, fa.data());
const size_t nfc = cpu.frustum_cull_batch(mins.data(), maxs.data(), nb, planes, fc.data());
fa.resize(nfa); fc.resize(nfc);
std::sort(fa.begin(), fa.end()); std::sort(fc.begin(), fc.end());
EXPECT_EQ(fa, fc);
// 三角形精确相交:与 CPU 逐项一致
const int nt = 257;
std::vector<double> ta(nt * 9), tb(nt * 9);
for (int i = 0; i < nt; ++i) {
for (int k = 0; k < 9; ++k) { ta[i * 9 + k] = u(rng); tb[i * 9 + k] = u(rng); }
}
std::vector<int> ia(nt), ic(nt);
const size_t nia = b->triangle_intersect_batch(ta.data(), tb.data(), nt, ia.data());
const size_t nic = cpu.triangle_intersect_batch(ta.data(), tb.data(), nt, ic.data());
ia.resize(nia); ic.resize(nic);
std::sort(ia.begin(), ia.end()); std::sort(ic.begin(), ic.end());
EXPECT_EQ(ia, ic);
}
TEST(ComputeBackendTest, CudaBackend_AvailableOrFallsBack) {
auto b = create_backend(BackendType::Cuda);
ASSERT_NE(b, nullptr);
const BackendInfo info = b->info();
EXPECT_EQ(info.type, BackendType::Cuda);
// 无 CUDA 构建:回退路径结果与 CPU 一致
const double xs[4] = {0, 1, 2, -1};
const double ys[4] = {0, 0, 0, 0};
const double zs[4] = {0, 0, 0, 0};
double out[4];
b->sdf_sphere_batch(xs, ys, zs, out, 4, 0, 0, 0, 1.5);
for (int i = 0; i < 4; ++i)
EXPECT_NEAR(out[i], ref_sphere(xs[i], ys[i], zs[i], 0, 0, 0, 1.5), 1e-12);
// 回退路径的名称标注(gpu=true 仅在真实 CUDA 时)
if (!info.gpu) {
EXPECT_NE(info.name.find("fallback"), std::string::npos);
} else {
EXPECT_NE(info.name.find("cuda"), std::string::npos);
}
}
// ═══════════════════════════════════════════════════════════
// GPU 诚实性(VDE-512 item2):能力探测 / 选型 / 五核等价
// ═══════════════════════════════════════════════════════════
TEST(ComputeBackendTest, GpuCapability_HonestReporting) {
const GpuCapability g = probe_gpu_capability();
// device_count 与 device_present 必须自洽
EXPECT_EQ(g.device_present, g.device_count > 0);
auto cuda = create_backend(BackendType::Cuda);
ASSERT_NE(cuda, nullptr);
auto* cuda_impl = dynamic_cast<CudaComputeBackend*>(cuda.get());
ASSERT_NE(cuda_impl, nullptr);
const BackendInfo info = cuda->info();
// 核心不变量:info().gpu 必须与「是否真有可用设备」一致(禁止假 GPU)
EXPECT_EQ(info.gpu, g.device_present);
EXPECT_EQ(info.gpu, g.toolkit_compiled && g.device_present);
EXPECT_EQ(cuda_impl->is_gpu_active(), info.gpu);
if (g.device_present) {
EXPECT_NE(info.name.find("cuda"), std::string::npos);
EXPECT_GE(info.device_count, 1);
EXPECT_EQ(info.lanes, 32);
} else {
EXPECT_NE(info.name.find("fallback"), std::string::npos);
EXPECT_EQ(info.device_count, 1);
EXPECT_EQ(info.lanes, 1);
}
}
TEST(ComputeBackendTest, DefaultBackend_HonestGpuSelection) {
ComputeBackend& b = default_backend();
const GpuCapability g = probe_gpu_capability();
// 默认后端报告的执行路径必须与真实设备存在性一致;
// probe_gpu_capability 仅描述 CUDA,OpenCL 后端单独诚实报告 gpu。
if (b.info().type == BackendType::OpenCl)
EXPECT_TRUE(b.info().gpu);
else
EXPECT_EQ(b.info().gpu, g.device_present);
if (b.info().type != BackendType::OpenCl && !g.device_present) {
// 无真实 GPU 时必须是 CPU SIMD(此前会伪装成 CUDA 类型)
EXPECT_EQ(b.info().type, BackendType::CpuSimd);
EXPECT_FALSE(b.info().gpu);
}
}
TEST(ComputeBackendTest, CudaBackend_AllKernelsMatchCpu) {
auto cuda = create_backend(BackendType::Cuda);
CpuSimdComputeBackend cpu;
ASSERT_NE(cuda, nullptr);
std::mt19937 rng(123);
std::uniform_real_distribution<double> uni(-2.5, 2.5);
// ① 球 SDF
const int n = 613;
std::vector<double> xs(n), ys(n), zs(n), a(n), r(n);
for (int i = 0; i < n; ++i) { xs[i] = uni(rng); ys[i] = uni(rng); zs[i] = uni(rng); }
cuda->sdf_sphere_batch(xs.data(), ys.data(), zs.data(), a.data(), n,
0.4, -0.3, 0.2, 1.1);
cpu.sdf_sphere_batch(xs.data(), ys.data(), zs.data(), r.data(), n,
0.4, -0.3, 0.2, 1.1);
for (int i = 0; i < n; ++i) EXPECT_NEAR(a[i], r[i], 1e-12) << "sphere i=" << i;
// ② 盒 SDF
cuda->sdf_box_batch(xs.data(), ys.data(), zs.data(), a.data(), n, 0.7, 0.6, 0.9);
cpu.sdf_box_batch(xs.data(), ys.data(), zs.data(), r.data(), n, 0.7, 0.6, 0.9);
for (int i = 0; i < n; ++i) EXPECT_NEAR(a[i], r[i], 1e-12) << "box i=" << i;
// ③ AABB 相交(含升序约定)
const size_t m = 301;
std::vector<double> mins(m * 3), maxs(m * 3);
for (size_t i = 0; i < m; ++i) {
const double c0 = uni(rng), c1 = uni(rng), c2 = uni(rng), h = 0.4;
mins[i * 3 + 0] = c0 - h; maxs[i * 3 + 0] = c0 + h;
mins[i * 3 + 1] = c1 - h; maxs[i * 3 + 1] = c1 + h;
mins[i * 3 + 2] = c2 - h; maxs[i * 3 + 2] = c2 + h;
}
std::vector<int> ha(m), hr(m);
const size_t na = cuda->aabb_intersect(mins.data(), maxs.data(), m,
-0.5, -0.5, -0.5, 0.5, 0.5, 0.5, ha.data());
const size_t nr = cpu.aabb_intersect(mins.data(), maxs.data(), m,
-0.5, -0.5, -0.5, 0.5, 0.5, 0.5, hr.data());
ASSERT_EQ(na, nr);
for (size_t i = 0; i < na; ++i) EXPECT_EQ(ha[i], hr[i]) << "aabb #" << i;
// ④ 视锥剔除(含升序约定)
double planes[6 * 4];
box_frustum_planes(planes);
std::vector<int> fa(m), fr(m);
const size_t nf = cuda->frustum_cull_batch(mins.data(), maxs.data(), m,
planes, fa.data());
const size_t nfr = cpu.frustum_cull_batch(mins.data(), maxs.data(), m,
planes, fr.data());
ASSERT_EQ(nf, nfr);
for (size_t i = 0; i < nf; ++i) EXPECT_EQ(fa[i], fr[i]) << "frustum #" << i;
// ⑤ 三角形-三角形相交(含共面/非共面混合 + 升序约定)
std::vector<double> ta(m * 9), tb(m * 9);
for (size_t i = 0; i < m; ++i) {
for (int k = 0; k < 9; ++k) ta[i * 9 + k] = uni(rng);
if (i % 3 == 0) { // 三分之一强制共面(z=0)
for (int v = 0; v < 3; ++v) {
ta[i * 9 + v * 3 + 2] = 0.0;
tb[i * 9 + v * 3 + 0] = uni(rng);
tb[i * 9 + v * 3 + 1] = uni(rng);
tb[i * 9 + v * 3 + 2] = 0.0;
}
} else {
for (int k = 0; k < 9; ++k) tb[i * 9 + k] = uni(rng);
}
}
std::vector<int> pa(m), pr(m);
const size_t npa = cuda->triangle_intersect_batch(ta.data(), tb.data(), m, pa.data());
const size_t npr = cpu.triangle_intersect_batch(ta.data(), tb.data(), m, pr.data());
ASSERT_EQ(npa, npr);
for (size_t i = 0; i < npa; ++i) EXPECT_EQ(pa[i], pr[i]) << "tri #" << i;
}
// ═══════════════════════════════════════════════════════════
// 集成:后端 → VoxelGrid → Marching Cubes
// ═══════════════════════════════════════════════════════════
TEST(ComputeBackendTest, BackendSdf_FeedsVoxelSampling_Watertight) {
ComputeBackend& b = default_backend();
const AABB3D bounds{{-1.5, -1.5, -1.5}, {1.5, 1.5, 1.5}};
vde::mesh::VoxelizeParams p;
p.resolution = 32;
// 将 ComputeBackend 的批核包装为 BatchSdfFn 喂给 sample_sdf_batch
auto grid = vde::mesh::sample_sdf_batch(
[&b](const double* xs, const double* ys, const double* zs,
double* out, int n) {
b.sdf_sphere_batch(xs, ys, zs, out, n, 0, 0, 0, 1.0);
},
bounds, p);
auto mc = vde::mesh::marching_cubes(grid, 0.0);
ASSERT_FALSE(mc.triangles.empty());
EXPECT_TRUE(is_watertight(mc)) << "后端驱动的体素采样重建应为水密网格";
}