Files
ViewDesignEngine/tests/core/test_simd_vector.cpp
T
hm 136e154183
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
test(core): VDE-893 复核 core 模块未测试清单并补判别性测试
真实缺口:transform rotate_x/y/z·rotate·mirror·mirror_about_plane(+6);Polygon2D perimeter/is_convex/simplify(+9);exact_point_in_polyhedron(+3);simd_vector dot3/intersect4/intersect4_count/aabb_intersect_batch*(新增 test_simd_vector 4 例)。逐点/逐字段对手写公式或独立标量复算。

假阳性:add_task/cache_line_count/prefetch_array/set_aos/invalidate_soa(vde_infra)、base32_encode(vde_license)、make_temp_file/delete_file/run_command(vde_platform)非 core;decompress_brep/edgebreaker_decompress/serialize/project 已有专测。
2026-10-06 09:49:35 +08:00

132 lines
5.2 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.
#include <gtest/gtest.h>
#include "vde/core/simd_vector.h"
#include <algorithm>
#include <vector>
using namespace vde::core;
// ═══════════════════════════════════════════════════════════
// VDE-893: simd_vector 未测试公开函数补测
// dot3 / intersect4 / intersect4_count / aabb_intersect_batch*
// ═══════════════════════════════════════════════════════════
TEST(SimdVectorVDE893, Vec4fDot3IgnoresWComponent) {
Vec4f a(1.0f, 2.0f, 3.0f, 100.0f);
Vec4f b(4.0f, 5.0f, 6.0f, -7.0f);
// 独立复算:x*y 三项之和 = 4 + 10 + 18 = 32
EXPECT_FLOAT_EQ(a.dot3(b), 32.0f);
// 判别:含 w 的完整 dot = 32 + 100·(−7) = −668,必与 dot3 不同
EXPECT_FLOAT_EQ(a.dot(b), -668.0f);
EXPECT_NE(a.dot3(b), a.dot(b));
// 第二个样本(含负分量)
Vec4f c(0.5f, -2.0f, 4.0f, 9.0f);
Vec4f d(-1.0f, 3.0f, 2.0f, 8.0f);
const float expect = 0.5f * (-1.0f) + (-2.0f) * 3.0f + 4.0f * 2.0f; // 1.5
EXPECT_NEAR(c.dot3(d), expect, 1e-6f);
}
namespace {
BatchAABB4 make_batch4(const double mn[4][3], const double mx[4][3]) {
double min_x[4], min_y[4], min_z[4], max_x[4], max_y[4], max_z[4];
for (int i = 0; i < 4; ++i) {
min_x[i] = mn[i][0]; min_y[i] = mn[i][1]; min_z[i] = mn[i][2];
max_x[i] = mx[i][0]; max_y[i] = mx[i][1]; max_z[i] = mx[i][2];
}
return BatchAABB4::load(min_x, min_y, min_z, max_x, max_y, max_z);
}
} // namespace
TEST(SimdVectorVDE893, BatchAABB4Intersect4BitmaskAndCount) {
// 参考盒 [−1,1]^3
// lane0 完全在内; lane1 x∈[2,3] 不交; lane2 x∈[1,2] 边界相切应算相交; lane3 y∈[5,6] 分离
const double mn[4][3] = {{-0.5, -0.5, -0.5}, {2, 0, 0}, {1, 0, 0}, {0, 5, 0}};
const double mx[4][3] = {{0.5, 0.5, 0.5}, {3, 1, 1}, {2, 1, 1}, {1, 6, 1}};
BatchAABB4 b = make_batch4(mn, mx);
// 独立复算
int expect_mask = 0;
for (int i = 0; i < 4; ++i) {
bool hit = mn[i][0] <= 1.0 && mx[i][0] >= -1.0 &&
mn[i][1] <= 1.0 && mx[i][1] >= -1.0 &&
mn[i][2] <= 1.0 && mx[i][2] >= -1.0;
if (hit) expect_mask |= (1 << i);
}
uint32_t mask = BatchAABB4::intersect4(b, -1, -1, -1, 1, 1, 1);
EXPECT_EQ(static_cast<int>(mask), expect_mask);
EXPECT_EQ(static_cast<int>(mask), 0x5); // lane0 + lane2
EXPECT_EQ(BatchAABB4::intersect4_count(b, -1, -1, -1, 1, 1, 1), 2);
}
TEST(SimdVectorVDE893, AabbIntersectBatchTemplateMatchesScalarRecompute) {
constexpr size_t N = 5;
const double mins[N * 3] = {-0.5, -0.5, -0.5,
2, 0, 0,
1, 0, 0,
0, 5, 0,
-2, -2, -2};
const double maxs[N * 3] = {0.5, 0.5, 0.5,
3, 1, 1,
2, 1, 1,
1, 6, 1,
-1.5, -1.5, -1.5};
int out[N] = {0};
size_t cnt = aabb_intersect_batch<N>(mins, maxs, -1, -1, -1, 1, 1, 1, out);
// 独立复算相交索引
std::vector<int> expect;
for (size_t i = 0; i < N; ++i) {
if (mins[i * 3 + 0] <= 1.0 && maxs[i * 3 + 0] >= -1.0 &&
mins[i * 3 + 1] <= 1.0 && maxs[i * 3 + 1] >= -1.0 &&
mins[i * 3 + 2] <= 1.0 && maxs[i * 3 + 2] >= -1.0) {
expect.push_back(static_cast<int>(i));
}
}
ASSERT_EQ(cnt, expect.size());
for (size_t k = 0; k < cnt; ++k) EXPECT_EQ(out[k], expect[k]);
EXPECT_EQ(cnt, 2u); // lane0(内含)+ lane2(边界相切)
}
TEST(SimdVectorVDE893, AabbIntersectBatchSimdMatchesScalarForAllTailCounts) {
// 8 个盒,交替相交/分离,覆盖 4-wide 主循环 + 尾数标量路径
const double mins[8 * 3] = {
-0.5, -0.5, -0.5, // 0 相交
2, 0, 0, // 1 分离
0.5, 0.5, 0.5, // 2 相交(完全在内)
-2, -2, -2, // 3 分离
1, 0, 0, // 4 相交(x 边界相切,落在尾数)
0, 5, 0, // 5 分离
-1, -1, -1, // 6 相交(恰为参考盒边界)
3, 3, 3 // 7 分离
};
const double maxs[8 * 3] = {
0.5, 0.5, 0.5,
3, 1, 1,
0.6, 0.6, 0.6,
-1.5, -1.5, -1.5,
2, 1, 1,
1, 6, 1,
1, 1, 1,
4, 4, 4
};
for (size_t n = 4; n <= 8; ++n) {
int out[8];
std::fill(out, out + 8, -1);
size_t cnt = aabb_intersect_batch_simd(mins, maxs, n, -1, -1, -1, 1, 1, 1, out);
std::vector<int> expect;
for (size_t i = 0; i < n; ++i) {
if (mins[i * 3 + 0] <= 1.0 && maxs[i * 3 + 0] >= -1.0 &&
mins[i * 3 + 1] <= 1.0 && maxs[i * 3 + 1] >= -1.0 &&
mins[i * 3 + 2] <= 1.0 && maxs[i * 3 + 2] >= -1.0) {
expect.push_back(static_cast<int>(i));
}
}
ASSERT_EQ(cnt, expect.size()) << "n=" << n;
for (size_t k = 0; k < cnt; ++k) EXPECT_EQ(out[k], expect[k]) << "n=" << n;
}
}