Phase 1: Fix 6 stubs (ConstraintGraph ctor, G2/G3 continuity, surface_torsion, fill_n_sided, unfold_sheet_metal) Phase 2: New modules (as_array/from_array, closest_point BVH, tolerance_stackup WorstCase/RSS/MonteCarlo) Phase 3: Curve factories (arc_3point/rectangle/polygon/slot/ellipse/closed_from_points) + explode_to_brep Phase 4: Modeling wrappers (wrap/dome, boundary_surface_feature, create_fitting/auto_route_pipe) Phase 5: Fix pipe_surface guard, constraint_solver warnings, arc_3point angle frame; sync 6 docs to Fixed; 31 tests pass
89 lines
3.4 KiB
C++
89 lines
3.4 KiB
C++
#include <gtest/gtest.h>
|
|
#include "vde/collision/closest_point.h"
|
|
#include "vde/spatial/bvh.h"
|
|
#include <cmath>
|
|
|
|
using namespace vde::collision;
|
|
using namespace vde::core;
|
|
using namespace vde::spatial;
|
|
|
|
// ═══════════════════════════════════════════════════════════
|
|
// VDE-116: closest_point 测试
|
|
// ═══════════════════════════════════════════════════════════
|
|
|
|
TEST(ClosestPointTest, TriangleVertexRegion) {
|
|
// 三角形在 XY 平面
|
|
Triangle3D tri(Point3D(0, 0, 0), Point3D(10, 0, 0), Point3D(0, 10, 0));
|
|
// 查询点在 v0 的 Voronoi 区域(投影落在 v0 附近外侧)
|
|
Point3D query(-1.0, -1.0, 0.0);
|
|
Point3D cp = closest_point_triangle(query, tri);
|
|
EXPECT_NEAR(cp.x(), 0.0, 1e-9);
|
|
EXPECT_NEAR(cp.y(), 0.0, 1e-9);
|
|
EXPECT_NEAR(cp.z(), 0.0, 1e-9);
|
|
}
|
|
|
|
TEST(ClosestPointTest, TriangleEdgeRegion) {
|
|
// 查询点投影落在边 AB 的外侧
|
|
Triangle3D tri(Point3D(0, 0, 0), Point3D(10, 0, 0), Point3D(0, 10, 0));
|
|
Point3D query(5.0, -2.0, 0.0);
|
|
Point3D cp = closest_point_triangle(query, tri);
|
|
// 最近点应在边 AB 上 (5,0,0)
|
|
EXPECT_NEAR(cp.x(), 5.0, 1e-9);
|
|
EXPECT_NEAR(cp.y(), 0.0, 1e-9);
|
|
EXPECT_NEAR(cp.z(), 0.0, 1e-9);
|
|
}
|
|
|
|
TEST(ClosestPointTest, TriangleFaceRegion) {
|
|
// 查询点投影落在三角形内部
|
|
Triangle3D tri(Point3D(0, 0, 0), Point3D(10, 0, 0), Point3D(0, 10, 0));
|
|
Point3D query(2.0, 2.0, 5.0);
|
|
Point3D cp = closest_point_triangle(query, tri);
|
|
// 最近点应为垂直投影到平面的点
|
|
EXPECT_NEAR(cp.x(), 2.0, 1e-9);
|
|
EXPECT_NEAR(cp.y(), 2.0, 1e-9);
|
|
EXPECT_NEAR(cp.z(), 0.0, 1e-9);
|
|
}
|
|
|
|
TEST(ClosestPointTest, MeshBruteForce) {
|
|
// 两个分离的三角形
|
|
std::vector<Triangle3D> tris = {
|
|
Triangle3D(Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(0, 1, 0)),
|
|
Triangle3D(Point3D(10, 0, 0), Point3D(11, 0, 0), Point3D(10, 1, 0)),
|
|
};
|
|
// 查询点靠近第二个三角形
|
|
Point3D query(10.5, 0.2, 0.5);
|
|
Point3D cp = closest_point_mesh(query, tris);
|
|
EXPECT_NEAR(cp.x(), 10.5, 1e-6);
|
|
EXPECT_NEAR(cp.y(), 0.2, 1e-6);
|
|
EXPECT_NEAR(cp.z(), 0.0, 1e-6);
|
|
|
|
// distance_to_mesh 一致性
|
|
double d = distance_to_mesh(query, tris);
|
|
EXPECT_NEAR(d, 0.5, 1e-9);
|
|
}
|
|
|
|
TEST(ClosestPointTest, MeshBVHMatchesBruteForce) {
|
|
// 构建一个 2×2 三角形网格(4 个三角形组成正方形)
|
|
std::vector<Triangle3D> tris = {
|
|
Triangle3D(Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(1, 1, 0)),
|
|
Triangle3D(Point3D(0, 0, 0), Point3D(1, 1, 0), Point3D(0, 1, 0)),
|
|
Triangle3D(Point3D(1, 0, 0), Point3D(2, 0, 0), Point3D(2, 1, 0)),
|
|
Triangle3D(Point3D(1, 0, 0), Point3D(2, 1, 0), Point3D(1, 1, 0)),
|
|
};
|
|
|
|
BVH bvh;
|
|
bvh.build(tris);
|
|
|
|
// 多个查询点验证 BVH 与暴力结果一致
|
|
for (int i = 0; i <= 10; ++i) {
|
|
for (int j = 0; j <= 10; ++j) {
|
|
Point3D query(i * 0.2, j * 0.2, 1.5);
|
|
Point3D cp_brute = closest_point_mesh(query, tris);
|
|
Point3D cp_bvh = closest_point_mesh(query, tris, bvh);
|
|
double dist_diff = (cp_brute - cp_bvh).norm();
|
|
EXPECT_LT(dist_diff, 1e-6)
|
|
<< "BVH/brute mismatch at (" << i * 0.2 << "," << j * 0.2 << ")";
|
|
}
|
|
}
|
|
}
|