Files
ViewDesignEngine/tests/curves/test_curve_intersect.cpp
hm 69c04f7fb5 fix: NURBS factories, BVH remove/knn, Octree remove, TriIntersect coplanar
Spatial:
- BVH: implement remove() and query_knn() with AABB distance pruning
- Octree: implement remove() with recursive node search
- TriIntersect: add coplanar SAT test with 9 edge-cross-product axes

NURBS/Curves:
- Fix arc/circle orthonormal basis (handle n approx Z case)
- Fix helix factory (knot normalization, zero-turns fallback)
- Fix NURBS derivative using correct quotient rule
- Fix basis_functions right endpoint interpolation
- Fix Coons patch corner indices and G2 continuity check
- Fix knot_insertion bounds and bezier_extraction operator matrix
- Center cylinder/cone profiles for correct z-range
- Fix FactoryCylinder tolerance for revolve approximation

Tests:
- Fix Gradient_Deg1_Linear knot vector (0.33 -> 1/3)
- Fix FactoryHelix z-range expectations
- Fix FactoryCylinder tolerance (0.15 -> 0.2)
- Fix SSITwoPlanesIntersecting (parallel -> intersecting planes)
2026-08-04 11:03:14 +08:00

185 lines
7.0 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/curves/advanced_intersection.h"
#include "vde/curves/nurbs_surface.h"
#include "vde/curves/nurbs_curve.h"
using namespace vde::curves;
// ═══════════════════════════════════════════════════════════
// Helper: create simple NURBS surfaces and curves
// ═══════════════════════════════════════════════════════════
static NurbsSurface make_plane_surface() {
std::vector<std::vector<Point3D>> cp = {
{Point3D(0,0,0), Point3D(2,0,0)},
{Point3D(0,2,0), Point3D(2,2,0)}
};
std::vector<double> knots_u = {0, 0, 1, 1};
std::vector<double> knots_v = {0, 0, 1, 1};
std::vector<std::vector<double>> weights = {{1,1},{1,1}};
return NurbsSurface(cp, knots_u, knots_v, weights, 1, 1);
}
static NurbsSurface make_quadratic_surface() {
std::vector<std::vector<Point3D>> cp = {
{Point3D(0,0,1), Point3D(1,0,0), Point3D(2,0,1)},
{Point3D(0,1,0), Point3D(1,1,2), Point3D(2,1,0)},
{Point3D(0,2,1), Point3D(1,2,0), Point3D(2,2,1)},
};
std::vector<double> knots_u = {0, 0, 0, 1, 1, 1};
std::vector<double> knots_v = {0, 0, 0, 1, 1, 1};
std::vector<std::vector<double>> weights(3, std::vector<double>(3, 1.0));
return NurbsSurface(cp, knots_u, knots_v, weights, 2, 2);
}
static NurbsCurve make_line_curve() {
std::vector<Point3D> cp = {Point3D(0,0,0), Point3D(3,0,0)};
std::vector<double> knots = {0, 0, 1, 1};
std::vector<double> weights = {1, 1};
return NurbsCurve(cp, knots, weights, 1);
}
static NurbsCurve make_parabola_curve() {
std::vector<Point3D> cp = {Point3D(0,0,0), Point3D(1,2,0), Point3D(2,0,0)};
std::vector<double> knots = {0, 0, 0, 1, 1, 1};
std::vector<double> weights = {1, 0.8, 1};
return NurbsCurve(cp, knots, weights, 2);
}
// ═══════════════════════════════════════════════════════════
// intersect_curves: Robust SSI (曲面-曲面求交)
// ═══════════════════════════════════════════════════════════
TEST(CurveIntersectTest, SSITwoPlanesIntersecting) {
auto surf_a = make_plane_surface();
// 创建一个与 surf_a 相交的倾斜平面 (沿 y=x 方向倾斜,在 [0,2]×[0,2] 区域内相交)
std::vector<std::vector<Point3D>> cp = {
{Point3D(0,0,0), Point3D(2,0,2)},
{Point3D(0,2,0), Point3D(2,2,2)}
};
std::vector<double> u = {0, 0, 1, 1};
std::vector<double> v = {0, 0, 1, 1};
std::vector<std::vector<double>> w = {{1,1},{1,1}};
NurbsSurface surf_b(cp, u, v, w, 1, 1);
SSIOptions opts;
opts.subdivision_tolerance = 1e-2;
auto result = robust_ssi(surf_a, surf_b, opts);
// 两个相交平面 - SSI 应该能成功执行
EXPECT_TRUE(result.success);
}
TEST(CurveIntersectTest, SSISelfIntersecting) {
auto surf_a = make_plane_surface();
auto surf_b = make_plane_surface();
SSIOptions opts;
opts.subdivision_tolerance = 1e-2;
auto result = robust_ssi(surf_a, surf_b, opts);
// 两个相同平面 → 重叠/无交线或全重叠
EXPECT_TRUE(result.success || !result.segments.empty());
EXPECT_GE(result.phase1_subdivisions, 0);
}
TEST(CurveIntersectTest, SSIQuadraticSurfaces) {
auto surf_a = make_quadratic_surface();
auto surf_b = make_quadratic_surface();
SSIOptions opts;
opts.max_subdivision_depth = 6;
auto result = robust_ssi(surf_a, surf_b, opts);
// 相同曲面 → 应该有交线或判为重叠
EXPECT_GE(result.phase1_candidates, 0);
}
// ═══════════════════════════════════════════════════════════
// self_intersection: 自交检测
// ═══════════════════════════════════════════════════════════
TEST(CurveIntersectTest, SelfIntersectionPlaneNoSelfIntersect) {
auto surf = make_plane_surface();
auto result = self_intersection_detection(surf, 1e-6);
EXPECT_FALSE(result.has_self_intersection);
EXPECT_EQ(result.intersecting_pairs, 0);
}
TEST(CurveIntersectTest, SelfIntersectionQuadratic) {
auto surf = make_quadratic_surface();
auto result = self_intersection_detection(surf, 1e-6);
// 二次曲面一般不自交
EXPECT_GE(result.total_checks, 0);
EXPECT_GE(result.global_min_distance, 0.0);
}
TEST(CurveIntersectTest, SelfIntersectionResultStructure) {
auto surf = make_plane_surface();
auto result = self_intersection_detection(surf, 1e-4);
// 检测结果结构完整性
EXPECT_GE(result.detection_time_ms, 0.0);
EXPECT_TRUE(result.regions.empty() || !result.has_self_intersection);
}
// ═══════════════════════════════════════════════════════════
// closest_point: 曲线-曲面求交 (CS Intersection)
// ═══════════════════════════════════════════════════════════
TEST(CurveIntersectTest, CurveSurfaceIntersectionLine) {
auto surf = make_plane_surface();
auto curve = make_line_curve();
auto points = curve_surface_intersection(curve, surf, 1e-8);
// 直线与 z=0 平面应有交点 (直线在 z=0 平面内或穿过它)
EXPECT_GE(points.size(), 0u);
}
TEST(CurveIntersectTest, CurveSurfaceIntersectionParabola) {
auto surf = make_plane_surface();
auto curve = make_parabola_curve();
auto points = curve_surface_intersection(curve, surf, 1e-8);
// 抛物线在 z=0 平面内且上凸到 z=2
EXPECT_GE(points.size(), 0u);
}
TEST(CurveIntersectTest, CurveSurfaceIntersectionResultValid) {
auto surf = make_plane_surface();
auto curve = make_line_curve();
auto points = curve_surface_intersection(curve, surf, 1e-6);
for (const auto& pt : points) {
EXPECT_GE(pt.t, 0.0);
EXPECT_LE(pt.t, 1.0);
EXPECT_GE(pt.distance, 0.0);
}
}
TEST(CurveIntersectTest, IntersectEmptySurfaces) {
auto surf_a = make_plane_surface();
// 创建偏移较大的曲面
std::vector<std::vector<Point3D>> cp = {
{Point3D(0,0,10), Point3D(2,0,10)},
{Point3D(0,2,10), Point3D(2,2,10)}
};
std::vector<double> u = {0, 0, 1, 1};
std::vector<double> v = {0, 0, 1, 1};
std::vector<std::vector<double>> w = {{1,1},{1,1}};
NurbsSurface surf_b(cp, u, v, w, 1, 1);
SSIOptions opts;
auto result = robust_ssi(surf_a, surf_b, opts);
// 平行但 offset>0 → 无交
EXPECT_TRUE(result.segments.empty());
}