Files
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

181 lines
6.6 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/core/transform.h"
#include <cmath>
using namespace vde::core;
namespace {
constexpr double kPi = 3.14159265358979323846;
// 独立参考:绕单位轴 k 旋转 θ 的 Rodrigues 公式
// v' = v cosθ + (k×v) sinθ + k (k·v)(1 − cosθ)
Point3D rodrigues(const Point3D& p, const Vector3D& axis, double theta) {
Vector3D k = axis.normalized();
Vector3D v(p.x(), p.y(), p.z());
const double c = std::cos(theta), s = std::sin(theta);
Vector3D r = v * c + k.cross(v) * s + k * (k.dot(v) * (1.0 - c));
return Point3D(r.x(), r.y(), r.z());
}
} // namespace
TEST(TransformTest, Translate) {
auto T = translate(1, 2, 3);
Point3D p(0, 0, 0);
Point3D q = T * p;
EXPECT_DOUBLE_EQ(q.x(), 1.0);
EXPECT_DOUBLE_EQ(q.y(), 2.0);
EXPECT_DOUBLE_EQ(q.z(), 3.0);
}
TEST(TransformTest, Scale) {
auto S = scale(2.0);
Point3D p(1, 2, 3);
Point3D q = S * p;
EXPECT_DOUBLE_EQ(q.x(), 2.0);
EXPECT_DOUBLE_EQ(q.y(), 4.0);
EXPECT_DOUBLE_EQ(q.z(), 6.0);
}
// ═══════════════════════════════════════════════════════════
// VDE-893: rotate* / mirror* 未测试公开函数补测
// ═══════════════════════════════════════════════════════════
TEST(TransformTest, RotateXMatchesIndependentTrig) {
const double th = kPi / 2.0;
auto R = rotate_x(th);
const double c = std::cos(th), s = std::sin(th);
// 独立复算 (x, c y − s z, s y + c z)
const Point3D pts[] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {2, -3, 5}};
for (const auto& p : pts) {
Point3D q = R * p;
EXPECT_NEAR(q.x(), p.x(), 1e-12);
EXPECT_NEAR(q.y(), c * p.y() - s * p.z(), 1e-12);
EXPECT_NEAR(q.z(), s * p.y() + c * p.z(), 1e-12);
}
// 判别点:绕 X 转 90°,(0,1,0) → (0,0,1)
Point3D q = R * Point3D(0, 1, 0);
EXPECT_NEAR(q.y(), 0.0, 1e-12);
EXPECT_NEAR(q.z(), 1.0, 1e-12);
}
TEST(TransformTest, RotateYMatchesIndependentTrig) {
const double th = kPi / 2.0;
auto R = rotate_y(th);
const double c = std::cos(th), s = std::sin(th);
// 独立复算 (c x + s z, y, −s x + c z)
const Point3D pts[] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {2, -3, 5}};
for (const auto& p : pts) {
Point3D q = R * p;
EXPECT_NEAR(q.x(), c * p.x() + s * p.z(), 1e-12);
EXPECT_NEAR(q.y(), p.y(), 1e-12);
EXPECT_NEAR(q.z(), -s * p.x() + c * p.z(), 1e-12);
}
// 判别点:绕 Y 转 90°,(1,0,0) → (0,0,−1)
Point3D q = R * Point3D(1, 0, 0);
EXPECT_NEAR(q.x(), 0.0, 1e-12);
EXPECT_NEAR(q.z(), -1.0, 1e-12);
}
TEST(TransformTest, RotateZMatchesIndependentTrig) {
const double th = kPi / 2.0;
auto R = rotate_z(th);
const double c = std::cos(th), s = std::sin(th);
// 独立复算 (c x − s y, s x + c y, z)
const Point3D pts[] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {2, -3, 5}};
for (const auto& p : pts) {
Point3D q = R * p;
EXPECT_NEAR(q.x(), c * p.x() - s * p.y(), 1e-12);
EXPECT_NEAR(q.y(), s * p.x() + c * p.y(), 1e-12);
EXPECT_NEAR(q.z(), p.z(), 1e-12);
}
// 判别点:绕 Z 转 90°,(1,0,0) → (0,1,0)
Point3D q = R * Point3D(1, 0, 0);
EXPECT_NEAR(q.x(), 0.0, 1e-12);
EXPECT_NEAR(q.y(), 1.0, 1e-12);
}
TEST(TransformTest, RotateNormalizesArbitraryAxis) {
// 未归一化轴 (0,0,5) 必须与 UnitZ 等价(rotate 内部归一化)
Point3D p(1, 2, 3);
Point3D a = rotate(Vector3D(0, 0, 5), 0.7) * p;
Point3D b = rotate_z(0.7) * p;
EXPECT_NEAR(a.x(), b.x(), 1e-12);
EXPECT_NEAR(a.y(), b.y(), 1e-12);
EXPECT_NEAR(a.z(), b.z(), 1e-12);
// 斜轴 (1,1,1) 与独立 Rodrigues 复算一致,且保范数
Vector3D axis(1, 1, 1);
const double th = 1.0;
Point3D q = rotate(axis, th) * p;
Point3D ref = rodrigues(p, axis, th);
EXPECT_NEAR(q.x(), ref.x(), 1e-12);
EXPECT_NEAR(q.y(), ref.y(), 1e-12);
EXPECT_NEAR(q.z(), ref.z(), 1e-12);
EXPECT_NEAR(q.norm(), p.norm(), 1e-12);
}
TEST(TransformTest, MirrorReflectsAcrossOriginPlane) {
// 关于 YZ 平面(法线 X)反射
auto Mx = mirror(Vector3D::UnitX());
Point3D q = Mx * Point3D(3, 1, 0);
EXPECT_NEAR(q.x(), -3.0, 1e-12);
EXPECT_NEAR(q.y(), 1.0, 1e-12);
EXPECT_NEAR(q.z(), 0.0, 1e-12);
// 关于法线 (1,1,0) 的过原点平面:独立复算 p − 2 n̂ (n̂·p)
Vector3D n = Vector3D(1, 1, 0).normalized();
auto M = mirror(Vector3D(1, 1, 0));
Point3D p(1, 0, 0);
Point3D r = M * p;
Vector3D vp(p.x(), p.y(), p.z());
Vector3D expect = vp - 2.0 * n * n.dot(vp);
EXPECT_NEAR(r.x(), expect.x(), 1e-12);
EXPECT_NEAR(r.y(), expect.y(), 1e-12);
EXPECT_NEAR(r.z(), expect.z(), 1e-12);
// 判别点:(1,0,0) → (0,−1,0)
EXPECT_NEAR(r.x(), 0.0, 1e-12);
EXPECT_NEAR(r.y(), -1.0, 1e-12);
// 反射矩阵性质:对合 (M²=I)、行列式 = −1
Transform3D MM = M * M;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
EXPECT_NEAR(MM.linear()(i, j), (i == j) ? 1.0 : 0.0, 1e-12);
EXPECT_NEAR(M.linear().determinant(), -1.0, 1e-12);
}
TEST(TransformTest, MirrorAboutPlaneMatchesHessianReflection) {
// 平面 x=1(法线 X,d=−1):点 (3,0,0) 距平面 2 → 反射到 (−1,0,0)
Plane3D plane(Point3D(1, 0, 0), Vector3D::UnitX());
auto R = mirror_about_plane(plane);
Point3D p(3, 0, 0);
Point3D q = R * p;
EXPECT_NEAR(q.x(), -1.0, 1e-12);
EXPECT_NEAR(q.y(), 0.0, 1e-12);
EXPECT_NEAR(q.z(), 0.0, 1e-12);
// 独立复算 p − 2 (n·p + d) n
Vector3D n = plane.normal();
Vector3D vp(p.x(), p.y(), p.z());
Vector3D expect = vp - 2.0 * (n.dot(vp) + plane.d()) * n;
EXPECT_NEAR(q.x(), expect.x(), 1e-12);
EXPECT_NEAR(q.y(), expect.y(), 1e-12);
EXPECT_NEAR(q.z(), expect.z(), 1e-12);
// 反射后到平面的有向距离变号、绝对值不变
EXPECT_NEAR(plane.signed_distance(q), -plane.signed_distance(p), 1e-12);
// 一般位置平面 z=2,法线 Z:点 (5,7,3) → (5,7,1)
Plane3D pz(Point3D(0, 0, 2), Vector3D::UnitZ());
Point3D q2 = mirror_about_plane(pz) * Point3D(5, 7, 3);
EXPECT_NEAR(q2.x(), 5.0, 1e-12);
EXPECT_NEAR(q2.y(), 7.0, 1e-12);
EXPECT_NEAR(q2.z(), 1.0, 1e-12);
// 平面上的点保持不变
Point3D on(1, 2, 2);
Point3D q3 = mirror_about_plane(pz) * on;
EXPECT_NEAR(q3.x(), 1.0, 1e-12);
EXPECT_NEAR(q3.y(), 2.0, 1e-12);
EXPECT_NEAR(q3.z(), 2.0, 1e-12);
}