Files
ViewDesignEngine/tests/mesh/test_point_cloud_gpu.cpp
T
hm ea8aaf0759 perf(gpu): OpenCL point-cloud normal/SOR batch + cl_mem_flags fix (1.0.142)
- add ComputeBackend::point_normal_stats_batch (non-pure, CPU default fallback)
- OpenCL: spatial-hash grid build + 27-neighbourhood PCA normal/mean-distance kernels
- PointCloud normal_estimation / statistical_outlier_removal: GPU path above 20k points,
  always fall back to CPU; report capability honestly via point_cloud_gpu_available()
- fix cl_mem_flags constants to OpenCL spec (READ_ONLY=4, WRITE_ONLY=2, READ_WRITE=1);
  wrong values made new read-write buffers fail with CL_INVALID_VALUE(-30)
- fix tool selftest registration: use BUILD_TESTS and enable_testing() before tools/
- 100k pts: normals 162.7x, SOR 123.3x; real STL 100k pts: 57-111x; exact CPU agreement
- full CTest 8128/8128, release check 14/14
2026-11-04 07:59:36 +08:00

228 lines
9.5 KiB
C++
Raw 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.
/**
* @file tests/mesh/test_point_cloud_gpu.cpp
* @brief VDE-1278 点云 kNN 法线/SOR 的 GPU(OpenCL)批核与 CPU 结果一致性。
*
* 设计原则(诚实报告):
* - 无 GPU / OpenCL 不可用 / 点云小于触发阈值时,产品代码必须回退 CPU;
* 本测试只在 point_cloud_gpu_available() 为真时比较 GPU 与 CPU,否则 GTEST_SKIP。
* - 强制 GPU/CPU 通过环境变量 VDE_GPU_CLOUD_MIN_POINTS 控制(1 = 强制尝试 GPU,
* 极大值 = 强制 CPU),测试结束恢复原值。
* - GPU 与 CPU 允许浮点/PCA 迭代顺序造成的微小差异,法线用点积阈值、
* SOR 用输出点数容差比较,不断言逐位相等。
*/
#include "vde/mesh/point_cloud.h"
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <string>
#include <vector>
namespace {
using vde::core::Point3D;
using vde::core::Vector3D;
using vde::mesh::PointCloud;
constexpr const char* kMinPointsEnv = "VDE_GPU_CLOUD_MIN_POINTS";
constexpr const char* kForceGpu = "1";
constexpr const char* kForceCpu = "999999999";
void set_env(const char* name, const char* value) {
#ifdef _WIN32
_putenv_s(name, value ? value : "");
#else
if (value) {
setenv(name, value, 1);
} else {
unsetenv(name);
}
#endif
}
/// 作用域内覆盖阈值环境变量,析构恢复(清空 = 回到默认阈值)。
struct ScopedMinPoints {
explicit ScopedMinPoints(const char* value) { set_env(kMinPointsEnv, value); }
~ScopedMinPoints() { set_env(kMinPointsEnv, nullptr); }
};
/// 波形采样网格:z = 2*sin(0.5x)*cos(0.5y),法线有连续变化,便于比较 PCA 方向。
std::vector<Point3D> make_wave_grid(int side) {
std::vector<Point3D> pts;
pts.reserve(static_cast<std::size_t>(side) * static_cast<std::size_t>(side));
for (int iy = 0; iy < side; ++iy) {
for (int ix = 0; ix < side; ++ix) {
const double x = ix * 0.02;
const double y = iy * 0.02;
pts.emplace_back(x, y, 2.0 * std::sin(0.5 * x) * std::cos(0.5 * y));
}
}
return pts;
}
bool all_normals_finite(const PointCloud& cloud) {
for (const Vector3D& n : cloud.normals) {
if (!std::isfinite(n.x()) || !std::isfinite(n.y()) || !std::isfinite(n.z())) {
return false;
}
}
return true;
}
} // namespace
// ─────────────────────────────────────────────────────────────
// 阈值/可用性语义(不依赖本机 GPU,可无条件运行)
// ─────────────────────────────────────────────────────────────
TEST(PointCloudGpu, MinPointsThresholdSemantics) {
{
ScopedMinPoints restore(nullptr);
const std::size_t fallback = vde::mesh::point_cloud_gpu_min_points();
// 默认阈值 20000;若构建未启用 compute 后端点云侧返回 0(始终 CPU)。
EXPECT_TRUE(fallback == 20000 || fallback == 0);
}
{
ScopedMinPoints forced(kForceGpu);
EXPECT_EQ(vde::mesh::point_cloud_gpu_min_points(), 1u);
}
{
ScopedMinPoints invalid("not-a-number");
const std::size_t value = vde::mesh::point_cloud_gpu_min_points();
EXPECT_TRUE(value == 20000 || value == 0);
}
{
ScopedMinPoints custom("12345");
if (vde::mesh::point_cloud_gpu_min_points() != 0) {
EXPECT_EQ(vde::mesh::point_cloud_gpu_min_points(), 12345u);
}
}
}
TEST(PointCloudGpu, AvailabilityIsReportedWithoutCrash) {
const bool available = vde::mesh::point_cloud_gpu_available();
// 可用性探测必须可重复调用且结果稳定(内部缓存后端与探测结果)。
EXPECT_EQ(available, vde::mesh::point_cloud_gpu_available());
if (!available) {
GTEST_SKIP() << "OpenCL GPU 点云后处理不可用:产品代码按设计回退 CPU。";
}
}
// ─────────────────────────────────────────────────────────────
// GPU 与 CPU 的法线一致性
// ─────────────────────────────────────────────────────────────
TEST(PointCloudGpu, NormalEstimationGpuMatchesCpu) {
if (!vde::mesh::point_cloud_gpu_available()) {
GTEST_SKIP() << "OpenCL GPU 点云后处理不可用";
}
const PointCloud cloud(make_wave_grid(80)); // 6400 点,超过强制阈值 1
PointCloud cpu;
{
ScopedMinPoints force_cpu(kForceCpu);
cpu = cloud.normal_estimation(10);
}
PointCloud gpu;
{
ScopedMinPoints force_gpu(kForceGpu);
gpu = cloud.normal_estimation(10);
}
ASSERT_EQ(cpu.normals.size(), cloud.size());
ASSERT_EQ(gpu.normals.size(), cloud.size());
EXPECT_TRUE(all_normals_finite(cpu));
EXPECT_TRUE(all_normals_finite(gpu));
double min_dot = 1.0;
double sum_dot = 0.0;
for (std::size_t i = 0; i < cpu.normals.size(); ++i) {
const double dot = cpu.normals[i].dot(gpu.normals[i]);
min_dot = std::min(min_dot, dot);
sum_dot += dot;
EXPECT_NEAR(gpu.normals[i].norm(), 1.0, 1e-6);
}
EXPECT_GT(min_dot, 0.999) << "GPU 法线与 CPU 法线夹角过大";
EXPECT_GT(sum_dot / static_cast<double>(cpu.normals.size()), 0.9999);
}
// ─────────────────────────────────────────────────────────────
// GPU 与 CPU 的 SOR 一致性(含离群点)
// ─────────────────────────────────────────────────────────────
TEST(PointCloudGpu, StatisticalOutlierRemovalGpuMatchesCpu) {
if (!vde::mesh::point_cloud_gpu_available()) {
GTEST_SKIP() << "OpenCL GPU 点云后处理不可用";
}
std::vector<Point3D> pts = make_wave_grid(70); // 4900 个表面点
for (int i = 0; i < 120; ++i) {
pts.emplace_back(50.0 + i * 0.3, -40.0 - i * 0.2, 30.0 + i * 0.1); // 明显离群
}
const PointCloud cloud(std::move(pts));
PointCloud cpu;
{
ScopedMinPoints force_cpu(kForceCpu);
cpu = cloud.statistical_outlier_removal(6, 1.0);
}
PointCloud gpu;
{
ScopedMinPoints force_gpu(kForceGpu);
gpu = cloud.statistical_outlier_removal(6, 1.0);
}
// 两者都应把全部离群点剔除、保留大部分表面点。
EXPECT_LT(cpu.size(), cloud.size());
EXPECT_LT(gpu.size(), cloud.size());
EXPECT_GT(cpu.size(), cloud.size() / 2);
EXPECT_GT(gpu.size(), cloud.size() / 2);
// 输出点数允许阈值边界点的浮点差异,但不应成规模偏差(1%)。
const std::size_t diff =
cpu.size() > gpu.size() ? cpu.size() - gpu.size() : gpu.size() - cpu.size();
EXPECT_LE(diff, cloud.size() / 100);
}
// ─────────────────────────────────────────────────────────────
// 阈值与退化输入:小点云/空点云/单点走 CPU 且不崩溃
// ─────────────────────────────────────────────────────────────
TEST(PointCloudGpu, SmallCloudUsesCpuPath) {
const PointCloud cloud(make_wave_grid(4)); // 16 点
// 默认阈值下,小点云即使 GPU 可用也走 CPU;结果必须与强制 CPU 完全一致。
PointCloud default_path = cloud.normal_estimation(5);
PointCloud forced_cpu;
{
ScopedMinPoints force_cpu(kForceCpu);
forced_cpu = cloud.normal_estimation(5);
}
ASSERT_EQ(default_path.normals.size(), forced_cpu.normals.size());
for (std::size_t i = 0; i < default_path.normals.size(); ++i) {
EXPECT_DOUBLE_EQ(default_path.normals[i].x(), forced_cpu.normals[i].x());
EXPECT_DOUBLE_EQ(default_path.normals[i].y(), forced_cpu.normals[i].y());
EXPECT_DOUBLE_EQ(default_path.normals[i].z(), forced_cpu.normals[i].z());
}
}
TEST(PointCloudGpu, DegenerateCloudsDoNotCrash) {
ScopedMinPoints force_gpu(kForceGpu);
const PointCloud empty;
const PointCloud empty_normals = empty.normal_estimation(10);
EXPECT_TRUE(empty_normals.normals.empty());
EXPECT_TRUE(empty.statistical_outlier_removal(6, 1.0).vertices.empty());
std::vector<Point3D> single{Point3D(1.0, 2.0, 3.0)};
const PointCloud one(std::move(single));
const PointCloud one_normals = one.normal_estimation(10);
ASSERT_EQ(one_normals.normals.size(), 1u);
EXPECT_TRUE(all_normals_finite(one_normals));
// SOR 前置条件 vertices.size() >= k+1 不满足:直接返回原云。
EXPECT_EQ(one.statistical_outlier_removal(6, 1.0).size(), 1u);
// 共面退化点集:PCA 最小特征向量仍应是单位向量。
std::vector<Point3D> flat;
for (int i = 0; i < 30; ++i) {
flat.emplace_back(i * 0.1, (i % 5) * 0.1, 0.0);
}
const PointCloud flat_cloud(std::move(flat));
const PointCloud flat_normals = flat_cloud.normal_estimation(6);
ASSERT_EQ(flat_normals.normals.size(), flat_cloud.size());
EXPECT_TRUE(all_normals_finite(flat_normals));
}