676 lines
25 KiB
C++
676 lines
25 KiB
C++
#include <gtest/gtest.h>
|
||
#include "vde/curves/surface_intersection.h"
|
||
#include "vde/curves/generative_surfaces.h"
|
||
#include "vde/curves/nurbs_surface.h"
|
||
#include "vde/curves/nurbs_curve.h"
|
||
#include "vde/curves/nurbs_operations.h"
|
||
#include "vde/core/plane.h"
|
||
#include <cmath>
|
||
|
||
using namespace vde::curves;
|
||
using vde::core::Point3D;
|
||
using vde::core::Vector3D;
|
||
|
||
// ===========================================================================
|
||
// Helper: unit-square planar surface on z=0
|
||
// ===========================================================================
|
||
static NurbsSurface make_plane_xy() {
|
||
return NurbsSurface(
|
||
{ {Point3D(0,0,0), Point3D(0,1,0)},
|
||
{Point3D(1,0,0), Point3D(1,1,0)} },
|
||
{0,0,1,1}, {0,0,1,1}, {}, 1, 1);
|
||
}
|
||
|
||
// 以原点为中心、半边长 h 的 XY 平面(z=0);用于完整包含半径为 1 的截圆
|
||
static NurbsSurface make_plane_xy_centered(double h) {
|
||
return NurbsSurface(
|
||
{ {Point3D(-h,-h,0), Point3D(-h,h,0)},
|
||
{Point3D( h,-h,0), Point3D( h,h,0)} },
|
||
{0,0,1,1}, {0,0,1,1}, {}, 1, 1);
|
||
}
|
||
|
||
// ===========================================================================
|
||
// Helper: NURBS cylinder (radius 1, height 2, along Z, centered at origin)
|
||
// Degree 2 in u (circular), degree 1 in v (axial)
|
||
// ===========================================================================
|
||
static NurbsSurface make_cylinder(double radius = 1.0, double height = 2.0) {
|
||
double hh = height * 0.5;
|
||
double r = radius;
|
||
double w = std::sqrt(2.0) / 2.0;
|
||
|
||
// 9 control points around the full circle (degree 2)
|
||
std::vector<Point3D> circle_bottom = {
|
||
{ r, 0, -hh}, { r, r, -hh}, { 0, r, -hh}, {-r, r, -hh},
|
||
{-r, 0, -hh}, {-r,-r, -hh}, { 0,-r, -hh}, { r,-r, -hh},
|
||
{ r, 0, -hh} // wrap closing point
|
||
};
|
||
std::vector<Point3D> circle_top = {
|
||
{ r, 0, hh}, { r, r, hh}, { 0, r, hh}, {-r, r, hh},
|
||
{-r, 0, hh}, {-r,-r, hh}, { 0,-r, hh}, { r,-r, hh},
|
||
{ r, 0, hh}
|
||
};
|
||
|
||
std::vector<std::vector<Point3D>> grid;
|
||
std::vector<std::vector<double>> weights;
|
||
std::vector<double> u_weights = {1, w, 1, w, 1, w, 1, w, 1};
|
||
for (int i = 0; i < 9; ++i) {
|
||
grid.push_back({circle_bottom[i], circle_top[i]});
|
||
weights.push_back({u_weights[i], u_weights[i]});
|
||
}
|
||
|
||
return NurbsSurface(
|
||
grid,
|
||
{0,0,0, 0.25,0.25, 0.5,0.5, 0.75,0.75, 1,1,1},
|
||
{0,0,1,1},
|
||
weights,
|
||
2, 1);
|
||
}
|
||
|
||
// ===========================================================================
|
||
// Helper: tilted planar surface (degree 1×1, spans [0,1]×[0,1])
|
||
// Rotates the standard XY plane by angle around X axis
|
||
// ===========================================================================
|
||
static NurbsSurface make_tilted_plane(double angle_rad) {
|
||
double c = std::cos(angle_rad), s = std::sin(angle_rad);
|
||
return NurbsSurface(
|
||
{ {Point3D(0, 0, 0), Point3D(0, c, s)},
|
||
{Point3D(1, 0, 0), Point3D(1, c, s)} },
|
||
{0,0,1,1}, {0,0,1,1}, {}, 1, 1);
|
||
}
|
||
|
||
// ===========================================================================
|
||
// Test: Plane–surface intersection
|
||
// ===========================================================================
|
||
|
||
TEST(SurfaceIntersection, PlaneCylinderHorizontal) {
|
||
auto cyl = make_cylinder(1.0, 4.0);
|
||
|
||
// Cut with z=0 plane (horizontal mid-plane)
|
||
auto curves = intersect_plane_surface(cyl, Point3D(0,0,0), Vector3D(0,0,1), 32);
|
||
|
||
ASSERT_GE(curves.size(), 1u);
|
||
EXPECT_GE(curves[0].points.size(), 8u);
|
||
|
||
// All points must lie in the z=0 plane
|
||
for (const auto& p : curves[0].points) {
|
||
EXPECT_NEAR(p.z(), 0.0, 1e-4);
|
||
}
|
||
|
||
// All points must be on the cylinder surface: x² + y² ≈ radius²
|
||
for (const auto& p : curves[0].points) {
|
||
double r_sq = p.x()*p.x() + p.y()*p.y();
|
||
EXPECT_NEAR(r_sq, 1.0, 0.15);
|
||
}
|
||
}
|
||
|
||
TEST(SurfaceIntersection, PlaneCylinderVertical) {
|
||
auto cyl = make_cylinder(1.0, 4.0);
|
||
|
||
// Cut with x=0 plane (vertical slice)
|
||
auto curves = intersect_plane_surface(cyl, Point3D(0,0,0), Vector3D(1,0,0), 32);
|
||
|
||
ASSERT_GE(curves.size(), 1u);
|
||
|
||
// Should get two curves (two lines along the cylinder walls at x=0)
|
||
// The curves should have points with x ≈ 0
|
||
for (const auto& curve : curves) {
|
||
for (const auto& p : curve.points) {
|
||
EXPECT_NEAR(p.x(), 0.0, 1e-3);
|
||
}
|
||
}
|
||
}
|
||
|
||
TEST(SurfaceIntersection, PlaneCylinderOblique) {
|
||
auto cyl = make_cylinder(1.0, 4.0);
|
||
|
||
// Cut with 45° plane: z = y
|
||
Vector3D normal(0, -1, 1);
|
||
normal.normalize();
|
||
auto curves = intersect_plane_surface(cyl, Point3D(0,0,0), normal, 32);
|
||
|
||
ASSERT_GE(curves.size(), 1u);
|
||
|
||
// All points should satisfy: z ≈ y (i.e., y - z ≈ 0)
|
||
for (const auto& p : curves[0].points) {
|
||
EXPECT_NEAR(p.y(), p.z(), 1e-3);
|
||
}
|
||
}
|
||
|
||
TEST(SurfaceIntersection, PlanePlanarSurface) {
|
||
auto plane = make_plane_xy();
|
||
|
||
// Cut xy-plane with z=0 plane → intersection is the entire surface
|
||
auto curves = intersect_plane_surface(plane, Point3D(0,0,0), Vector3D(0,0,1), 16);
|
||
|
||
ASSERT_GE(curves.size(), 1u);
|
||
|
||
// All points on the intersection curve should have z ≈ 0
|
||
for (const auto& p : curves[0].points) {
|
||
EXPECT_NEAR(p.z(), 0.0, 1e-4);
|
||
}
|
||
}
|
||
|
||
TEST(SurfaceIntersection, PlaneNoIntersection) {
|
||
auto plane = make_plane_xy();
|
||
|
||
// Cut with plane z=5 (no intersection)
|
||
auto curves = intersect_plane_surface(plane, Point3D(0,0,5), Vector3D(0,0,1), 16);
|
||
|
||
EXPECT_EQ(curves.size(), 0u);
|
||
}
|
||
|
||
// ===========================================================================
|
||
// Test: Surface–surface intersection
|
||
// ===========================================================================
|
||
|
||
TEST(SurfaceIntersection, ParallelPlanesNoIntersection) {
|
||
auto p1 = make_plane_xy();
|
||
// Plane shifted to z=2
|
||
NurbsSurface p2(
|
||
{ {Point3D(0,0,2), Point3D(0,1,2)},
|
||
{Point3D(1,0,2), Point3D(1,1,2)} },
|
||
{0,0,1,1}, {0,0,1,1}, {}, 1, 1);
|
||
|
||
auto curves = intersect_surfaces(p1, p2, 4, 1e-4);
|
||
EXPECT_EQ(curves.size(), 0u);
|
||
}
|
||
|
||
TEST(SurfaceIntersection, NonIntersectingDistant) {
|
||
auto p1 = make_plane_xy();
|
||
// Plane shifted to z=10
|
||
NurbsSurface p2(
|
||
{ {Point3D(0,0,10), Point3D(0,1,10)},
|
||
{Point3D(1,0,10), Point3D(1,1,10)} },
|
||
{0,0,1,1}, {0,0,1,1}, {}, 1, 1);
|
||
|
||
auto curves = intersect_surfaces(p1, p2, 3, 1e-4);
|
||
EXPECT_EQ(curves.size(), 0u);
|
||
}
|
||
|
||
TEST(SurfaceIntersection, TwoIntersectingPlanes) {
|
||
auto p1 = make_plane_xy(); // z = 0
|
||
|
||
// Tilted plane: rotate around X axis by 45° → line of intersection at y=0, z=0
|
||
auto p2 = make_tilted_plane(M_PI / 4.0); // z = y
|
||
|
||
auto curves = intersect_surfaces(p1, p2, 5, 1e-4);
|
||
|
||
// Two planes intersect in a line
|
||
ASSERT_GE(curves.size(), 1u);
|
||
ASSERT_GE(curves[0].points.size(), 3u);
|
||
|
||
// Intersection line: y = 0, z = 0 (along X axis)
|
||
for (const auto& p : curves[0].points) {
|
||
EXPECT_NEAR(p.z(), 0.0, 1e-3);
|
||
EXPECT_NEAR(p.y(), 0.0, 1e-3);
|
||
}
|
||
}
|
||
|
||
TEST(SurfaceIntersection, SelfIntersectionSameSurface) {
|
||
auto p1 = make_plane_xy();
|
||
auto p2 = make_plane_xy(); // identical copy
|
||
|
||
auto curves = intersect_surfaces(p1, p2, 4, 1e-3);
|
||
// Two identical coplanar surfaces → should find intersection
|
||
ASSERT_GE(curves.size(), 1u);
|
||
EXPECT_GE(curves[0].points.size(), 3u);
|
||
|
||
// All intersection points should be coplanar (z ≈ 0)
|
||
for (const auto& p : curves[0].points) {
|
||
EXPECT_NEAR(p.z(), 0.0, 1e-3);
|
||
}
|
||
}
|
||
|
||
TEST(SurfaceIntersection, CylinderAndPlane) {
|
||
auto cyl = make_cylinder(2.0, 6.0);
|
||
auto plane = make_plane_xy(); // z=0 plane
|
||
|
||
auto curves = intersect_surfaces(cyl, plane, 5, 1e-3);
|
||
|
||
// Should find intersection curves or at least fail gracefully
|
||
// A cylinder intersected with its mid-plane should yield a circle
|
||
if (curves.size() >= 1 && curves[0].points.size() >= 4) {
|
||
for (const auto& p : curves[0].points) {
|
||
EXPECT_NEAR(p.z(), 0.0, 1e-2);
|
||
}
|
||
}
|
||
// Note: surface-surface intersection with flat surface may produce partial results
|
||
SUCCEED(); // at minimum the algorithm should not crash
|
||
}
|
||
|
||
TEST(SurfaceIntersection, ReturnsEmptyForNonIntersecting) {
|
||
auto p1 = make_plane_xy();
|
||
|
||
// Plane far away at z=100
|
||
NurbsSurface p2(
|
||
{ {Point3D(100,100,100), Point3D(100,101,100)},
|
||
{Point3D(101,100,100), Point3D(101,101,100)} },
|
||
{0,0,1,1}, {0,0,1,1}, {}, 1, 1);
|
||
|
||
auto curves = intersect_surfaces(p1, p2, 2, 1e-4);
|
||
EXPECT_EQ(curves.size(), 0u);
|
||
}
|
||
|
||
// ===========================================================================
|
||
// Test: Sphere–cylinder intersection (using revolve for sphere)
|
||
// ===========================================================================
|
||
|
||
TEST(SurfaceIntersection, SphereCylinder) {
|
||
// Create a NURBS sphere via revolution of a half-circle
|
||
// Half-circle in XZ plane, centered at origin, radius 2
|
||
double r = 2.0;
|
||
double w = std::sqrt(2.0) / 2.0;
|
||
|
||
NurbsCurve half_circle(
|
||
{Point3D(r, 0, 0), Point3D(r, 0, r), Point3D(0, 0, r),
|
||
Point3D(-r, 0, r), Point3D(-r, 0, 0)},
|
||
{0,0,0, 0.5, 0.5, 1, 1,1},
|
||
{1, w, 1, w, 1},
|
||
2);
|
||
|
||
auto sphere = NurbsSurface::revolve(half_circle, Point3D(0,0,0),
|
||
Vector3D(0,0,1), 2.0 * M_PI);
|
||
|
||
// Cylinder along Z, radius 1.5, centered at origin
|
||
auto cyl = make_cylinder(1.5, 6.0);
|
||
|
||
auto curves = intersect_surfaces(sphere, cyl, 4, 1e-3);
|
||
|
||
// If intersection is found, verify points lie on both surfaces
|
||
for (const auto& curve : curves) {
|
||
for (const auto& p : curve.points) {
|
||
// On sphere: x² + y² + z² ≈ r²
|
||
double d_sphere = std::abs(p.x()*p.x() + p.y()*p.y() + p.z()*p.z() - r*r);
|
||
EXPECT_LT(d_sphere, 0.5); // generous tolerance for NURBS approximation
|
||
|
||
// On cylinder: x² + y² ≈ R²
|
||
double d_cyl = std::abs(p.x()*p.x() + p.y()*p.y() - 1.5*1.5);
|
||
EXPECT_LT(d_cyl, 0.5);
|
||
}
|
||
}
|
||
SUCCEED();
|
||
}
|
||
|
||
// ===========================================================================
|
||
// Test: Curve fitting
|
||
// ===========================================================================
|
||
|
||
TEST(SurfaceIntersection, FittedCurveMatchesPoints) {
|
||
auto p1 = make_plane_xy();
|
||
auto p2 = make_tilted_plane(M_PI / 6.0);
|
||
|
||
auto curves = intersect_surfaces(p1, p2, 5, 1e-3);
|
||
|
||
if (curves.size() >= 1) {
|
||
// Verify intersection points lie near the Y=0, Z=0 line
|
||
for (const auto& p : curves[0].points) {
|
||
EXPECT_NEAR(p.z(), 0.0, 1e-2);
|
||
EXPECT_NEAR(p.y(), 0.0, 5e-2); // looser due to angle
|
||
}
|
||
}
|
||
SUCCEED();
|
||
}
|
||
|
||
// ===========================================================================
|
||
// Test: Algorithm robustness
|
||
// ===========================================================================
|
||
|
||
TEST(SurfaceIntersection, HandlesEmptySurface) {
|
||
// This just validates the algorithm doesn't crash on edge cases
|
||
auto p1 = make_plane_xy();
|
||
// Degenerate: single-point surface with valid NURBS (1 CP, degree 1 needs 3 knots)
|
||
NurbsSurface tiny(
|
||
{ {Point3D(0,0,0)} },
|
||
{0,0,1}, {0,0,1}, {}, 1, 1);
|
||
|
||
auto curves = intersect_surfaces(p1, tiny, 2, 1e-3);
|
||
// May or may not find intersection; just shouldn't crash
|
||
SUCCEED();
|
||
}
|
||
|
||
TEST(SurfaceIntersection, MaxDepthLimitsSubdivision) {
|
||
auto p1 = make_plane_xy();
|
||
auto p2 = make_tilted_plane(M_PI / 4.0);
|
||
|
||
// Max depth = 1 should still work (coarser but functional)
|
||
auto curves = intersect_surfaces(p1, p2, 1, 1e-2);
|
||
// Either finds intersection or returns empty; shouldn't crash
|
||
SUCCEED();
|
||
}
|
||
|
||
// ===========================================================================
|
||
// Test: intersect_plane_surface boundary cases
|
||
// ===========================================================================
|
||
|
||
TEST(SurfaceIntersection, PlaneSurfaceBoundaryTouching) {
|
||
auto plane = make_plane_xy();
|
||
|
||
// Plane z=0 just touches the surface (which IS at z=0)
|
||
auto curves = intersect_plane_surface(plane, Point3D(0.5,0.5,0), Vector3D(0,0,1), 16);
|
||
|
||
// The entire surface is on the plane → should find boundary contours
|
||
ASSERT_GE(curves.size(), 1u);
|
||
for (const auto& p : curves[0].points) {
|
||
EXPECT_NEAR(p.z(), 0.0, 1e-4);
|
||
}
|
||
}
|
||
|
||
// ===========================================================================
|
||
// E12: SSI 并行 —— 并行种子收集 + 并行曲线追踪的确定性与正确性
|
||
// ===========================================================================
|
||
|
||
TEST(SurfaceIntersection, ParallelSeedTraceDeterministic) {
|
||
// 深细分产生多个叶面片对 → 触发 E12 并行种子收集与并行追踪路径。
|
||
// 并行结果按种子顺序收集,两次调用应逐曲线、逐点一致。
|
||
auto cyl = make_cylinder(1.0, 4.0);
|
||
auto plane = make_plane_xy();
|
||
|
||
auto r1 = intersect_surfaces(cyl, plane, 5, 1e-3);
|
||
auto r2 = intersect_surfaces(cyl, plane, 5, 1e-3);
|
||
|
||
ASSERT_EQ(r1.size(), r2.size());
|
||
for (size_t i = 0; i < r1.size(); ++i) {
|
||
EXPECT_EQ(r1[i].points.size(), r2[i].points.size()) << "curve " << i;
|
||
if (r1[i].points.size() == r2[i].points.size()) {
|
||
for (size_t j = 0; j < r1[i].points.size(); ++j) {
|
||
EXPECT_NEAR((r1[i].points[j] - r2[i].points[j]).norm(), 0.0, 1e-9);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
TEST(SurfaceIntersection, ParallelCylinderPlaneCorrect) {
|
||
// 并行路径下圆柱 × 平面产生的交线点都应落在平面 z=0 与圆柱面 |r|²=1 上
|
||
// (NURBS 圆柱为控制点近似,半径容差放宽,与 SphereCylinder 测试一致)
|
||
auto cyl = make_cylinder(1.0, 4.0);
|
||
auto plane = make_plane_xy();
|
||
|
||
auto curves = intersect_surfaces(cyl, plane, 4, 1e-3);
|
||
ASSERT_GE(curves.size(), 1u);
|
||
int checked = 0;
|
||
for (const auto& curve : curves) {
|
||
for (const auto& p : curve.points) {
|
||
EXPECT_NEAR(p.z(), 0.0, 1e-2);
|
||
EXPECT_NEAR(p.x()*p.x() + p.y()*p.y(), 1.0, 0.15);
|
||
++checked;
|
||
}
|
||
}
|
||
EXPECT_GT(checked, 0);
|
||
}
|
||
|
||
// ===========================================================================
|
||
// VDE-342: 拟合 NURBS 曲线 (§1) + 拓扑分类 (§4) + 切触报告 (§2)
|
||
// ===========================================================================
|
||
|
||
TEST(VDE342IntersectionTest, FittedCurveFieldsPopulated) {
|
||
auto p1 = make_plane_xy();
|
||
auto p2 = make_tilted_plane(M_PI / 6.0);
|
||
auto curves = intersect_surfaces(p1, p2, 5, 1e-3);
|
||
ASSERT_GE(curves.size(), 1u);
|
||
const auto& c = curves[0];
|
||
ASSERT_GE(c.points.size(), 2u);
|
||
|
||
// §1: 3D 拟合曲线存在,且端点贴合采样点列端点(clamped B 样条过首末控制点)
|
||
ASSERT_TRUE(c.fitted_curve_3d.has_value());
|
||
auto [t0, t1] = c.fitted_curve_3d->domain();
|
||
Point3D f0 = c.fitted_curve_3d->evaluate(t0);
|
||
Point3D f1 = c.fitted_curve_3d->evaluate(t1);
|
||
EXPECT_LT(std::min((f0 - c.points.front()).norm(),
|
||
(f0 - c.points.back()).norm()), 1e-6);
|
||
EXPECT_LT(std::min((f1 - c.points.front()).norm(),
|
||
(f1 - c.points.back()).norm()), 1e-6);
|
||
|
||
// §1: 两曲面参数域 p-curve 均存在(params_a/params_b 与采样点等长)
|
||
EXPECT_TRUE(c.fitted_curve_a.has_value());
|
||
EXPECT_TRUE(c.fitted_curve_b.has_value());
|
||
}
|
||
|
||
TEST(VDE342IntersectionTest, ClassificationClosedForCylinderPlaneLoop) {
|
||
auto cyl = make_cylinder(1.0, 4.0);
|
||
// 平面须完整包含半径 1 的截圆,否则只截得圆弧(开曲线)而非闭合环
|
||
auto plane = make_plane_xy_centered(2.0);
|
||
auto curves = intersect_surfaces(cyl, plane, 4, 1e-3);
|
||
ASSERT_GE(curves.size(), 1u);
|
||
// 圆柱被水平面截得闭合圆环
|
||
EXPECT_EQ(curves[0].classification, CurveClassification::Closed);
|
||
}
|
||
|
||
TEST(VDE342IntersectionTest, ClassificationOpenForPlanePlane) {
|
||
auto p1 = make_plane_xy();
|
||
auto p2 = make_tilted_plane(M_PI / 6.0);
|
||
auto curves = intersect_surfaces(p1, p2, 5, 1e-3);
|
||
ASSERT_GE(curves.size(), 1u);
|
||
// 两平面相交为直线段(开曲线,非 Unknown)
|
||
EXPECT_NE(curves[0].classification, CurveClassification::Unknown);
|
||
EXPECT_NE(curves[0].classification, CurveClassification::Closed);
|
||
}
|
||
|
||
TEST(VDE342IntersectionTest, TangentContactReported) {
|
||
// 球 A:半径 2,球心原点;球 B:半径 1,球心 (0,0,3) → 两球在 (0,0,2) 外切。
|
||
// 使用单覆盖球面工厂:两极落在 v=0/1(参数域端点),全域网格必然采到切点。
|
||
auto sphere_a = NurbsSurface::sphere(0, 0, 0, 2.0);
|
||
auto sphere_b = NurbsSurface::sphere(0, 0, 3, 1.0);
|
||
|
||
auto curves = intersect_surfaces(sphere_a, sphere_b, 4, 1e-3);
|
||
// §2: 切触不应静默返回空,而应报告接触点
|
||
ASSERT_EQ(curves.size(), 1u);
|
||
EXPECT_TRUE(curves[0].is_tangent_contact);
|
||
ASSERT_FALSE(curves[0].points.empty());
|
||
EXPECT_NEAR(curves[0].points[0].x(), 0.0, 1e-2);
|
||
EXPECT_NEAR(curves[0].points[0].y(), 0.0, 1e-2);
|
||
EXPECT_NEAR(curves[0].points[0].z(), 2.0, 1e-2);
|
||
}
|
||
|
||
|
||
TEST(SurfaceIntersection, SphereSphereResidualAndPcurve) {
|
||
auto s1 = vde::curves::make_sphere_surface(10.0, vde::core::Point3D(0, 0, 0));
|
||
auto s2 = vde::curves::make_sphere_surface(10.0, vde::core::Point3D(12, 0, 0));
|
||
auto curves = vde::curves::intersect_surfaces(s1, s2, 6, 1e-5);
|
||
ASSERT_FALSE(curves.empty());
|
||
int n = 0;
|
||
double max_pcurve_err = 0.0;
|
||
for (auto& c : curves)
|
||
(void)vde::curves::refine_shared_pcurves(c, s1, s2, 1e-7, 3);
|
||
for (const auto& c : curves) {
|
||
for (const auto& p : c.points) {
|
||
EXPECT_LT(s1.distance_to(p), 1e-3);
|
||
EXPECT_LT(s2.distance_to(p), 1e-3);
|
||
++n;
|
||
}
|
||
if (c.fitted_curve_a) {
|
||
for (int k = 0; k <= 8; ++k) {
|
||
const auto uv = c.fitted_curve_a->evaluate(k / 8.0);
|
||
const auto p = s1.evaluate(uv.x(), uv.y());
|
||
// 共享参数 4D Newton + 细分后目标 1e-3。
|
||
max_pcurve_err = std::max(max_pcurve_err, s2.distance_to(p));
|
||
EXPECT_LT(s2.distance_to(p), 1e-3);
|
||
}
|
||
}
|
||
}
|
||
std::printf("ssi pcurve max residual=%.6g\n", max_pcurve_err);
|
||
EXPECT_GT(n, 0);
|
||
}
|
||
|
||
|
||
TEST(SurfaceIntersection, PlaneSphereRefinedPcurve) {
|
||
using vde::core::Point3D;
|
||
std::vector<std::vector<Point3D>> grid = {
|
||
{Point3D(-15, -15, 0), Point3D(15, -15, 0)},
|
||
{Point3D(-15, 15, 0), Point3D(15, 15, 0)}};
|
||
vde::curves::NurbsSurface plane(
|
||
grid, {0, 0, 1, 1}, {0, 0, 1, 1},
|
||
std::vector<std::vector<double>>{{1, 1}, {1, 1}}, 1, 1);
|
||
auto sphere = vde::curves::make_sphere_surface(10.0);
|
||
auto curves = vde::curves::intersect_surfaces_refined(plane, sphere, 6, 1e-5, 3);
|
||
ASSERT_FALSE(curves.empty());
|
||
double max_err = 0.0;
|
||
for (const auto& c : curves) {
|
||
for (const auto& p : c.points) {
|
||
EXPECT_LT(plane.distance_to(p), 1e-3);
|
||
EXPECT_LT(sphere.distance_to(p), 1e-3);
|
||
}
|
||
if (c.fitted_curve_a) {
|
||
for (int k = 0; k <= 8; ++k) {
|
||
const auto uv = c.fitted_curve_a->evaluate(k / 8.0);
|
||
const auto p = plane.evaluate(uv.x(), uv.y());
|
||
max_err = std::max(max_err, sphere.distance_to(p));
|
||
EXPECT_LT(sphere.distance_to(p), 1e-3);
|
||
}
|
||
}
|
||
}
|
||
std::printf("plane-sphere pcurve max residual=%.6g\n", max_err);
|
||
}
|
||
|
||
|
||
TEST(SurfaceIntersection, PlaneBicubicRefinedPcurve) {
|
||
using vde::core::Point3D;
|
||
std::vector<std::vector<Point3D>> pg = {
|
||
{Point3D(-2, -2, 0), Point3D(2, -2, 0)},
|
||
{Point3D(-2, 2, 0), Point3D(2, 2, 0)}};
|
||
vde::curves::NurbsSurface plane(
|
||
pg, {0, 0, 1, 1}, {0, 0, 1, 1},
|
||
std::vector<std::vector<double>>{{1, 1}, {1, 1}}, 1, 1);
|
||
std::vector<std::vector<Point3D>> wg(4, std::vector<Point3D>(4));
|
||
for (int i = 0; i < 4; ++i) {
|
||
for (int j = 0; j < 4; ++j) {
|
||
const double x = -2.0 + 4.0 * j / 3.0;
|
||
const double y = -2.0 + 4.0 * i / 3.0;
|
||
wg[i][j] = Point3D(x, y, 0.2 * x + 0.1 * y);
|
||
}
|
||
}
|
||
const std::vector<double> k = {0, 0, 0, 0, 1, 1, 1, 1};
|
||
vde::curves::NurbsSurface bicubic(
|
||
wg, k, k, std::vector<std::vector<double>>(4, std::vector<double>(4, 1.0)),
|
||
3, 3);
|
||
auto curves = vde::curves::intersect_surfaces_refined(plane, bicubic, 5, 1e-4, 3);
|
||
ASSERT_FALSE(curves.empty());
|
||
double max_err = 0.0;
|
||
for (const auto& c : curves) {
|
||
for (const auto& p : c.points) {
|
||
EXPECT_LT(plane.distance_to(p), 2e-3);
|
||
EXPECT_LT(bicubic.distance_to(p), 2e-3);
|
||
}
|
||
if (c.fitted_curve_b) {
|
||
for (int s = 0; s <= 8; ++s) {
|
||
const auto uv = c.fitted_curve_b->evaluate(s / 8.0);
|
||
const auto p = bicubic.evaluate(uv.x(), uv.y());
|
||
max_err = std::max(max_err, plane.distance_to(p));
|
||
EXPECT_LT(plane.distance_to(p), 2e-3);
|
||
}
|
||
}
|
||
}
|
||
std::printf("plane-bicubic pcurve max residual=%.6g\n", max_err);
|
||
}
|
||
|
||
|
||
TEST(SurfaceIntersection, BicubicBicubicRefinedPcurve) {
|
||
using vde::core::Point3D;
|
||
const std::vector<double> k = {0, 0, 0, 0, 1, 1, 1, 1};
|
||
auto make = [&](double ax, double ay, double c) {
|
||
std::vector<std::vector<Point3D>> g(4, std::vector<Point3D>(4));
|
||
for (int i = 0; i < 4; ++i)
|
||
for (int j = 0; j < 4; ++j) {
|
||
const double x = -2.0 + 4.0 * j / 3.0;
|
||
const double y = -2.0 + 4.0 * i / 3.0;
|
||
g[i][j] = Point3D(x, y, ax * x + ay * y + c);
|
||
}
|
||
return vde::curves::NurbsSurface(
|
||
g, k, k, std::vector<std::vector<double>>(4, std::vector<double>(4, 1.0)),
|
||
3, 3);
|
||
};
|
||
auto a = make(0.2, 0.1, 0.0);
|
||
auto b = make(-0.1, 0.3, 0.2);
|
||
auto curves = vde::curves::intersect_surfaces_refined(a, b, 5, 1e-4, 3);
|
||
ASSERT_FALSE(curves.empty());
|
||
double max_err = 0.0;
|
||
for (const auto& c : curves) {
|
||
for (const auto& p : c.points) {
|
||
EXPECT_LT(a.distance_to(p), 2e-3);
|
||
EXPECT_LT(b.distance_to(p), 2e-3);
|
||
}
|
||
if (c.fitted_curve_a) {
|
||
for (int s = 0; s <= 8; ++s) {
|
||
const auto uv = c.fitted_curve_a->evaluate(s / 8.0);
|
||
const auto p = a.evaluate(uv.x(), uv.y());
|
||
max_err = std::max(max_err, b.distance_to(p));
|
||
EXPECT_LT(b.distance_to(p), 2e-3);
|
||
}
|
||
}
|
||
}
|
||
std::printf("bicubic-bicubic pcurve max residual=%.6g\n", max_err);
|
||
}
|
||
|
||
|
||
TEST(SurfaceIntersection, PlaneBowlRefinedPcurve) {
|
||
using vde::core::Point3D;
|
||
std::vector<std::vector<Point3D>> pg = {
|
||
{Point3D(-2, -2, 0), Point3D(2, -2, 0)},
|
||
{Point3D(-2, 2, 0), Point3D(2, 2, 0)}};
|
||
vde::curves::NurbsSurface plane(
|
||
pg, {0, 0, 1, 1}, {0, 0, 1, 1},
|
||
std::vector<std::vector<double>>{{1, 1}, {1, 1}}, 1, 1);
|
||
const std::vector<double> k = {0, 0, 0, 1, 1, 1};
|
||
std::vector<std::vector<Point3D>> g(3, std::vector<Point3D>(3));
|
||
for (int i = 0; i < 3; ++i)
|
||
for (int j = 0; j < 3; ++j) {
|
||
const double x = -2.0 + 2.0 * j;
|
||
const double y = -2.0 + 2.0 * i;
|
||
g[i][j] = Point3D(x, y,
|
||
(i == 1 ? 4.0 : -1.0) + (j == 1 ? 4.0 : -1.0) - 2.0);
|
||
}
|
||
vde::curves::NurbsSurface bowl(
|
||
g, k, k, std::vector<std::vector<double>>(3, std::vector<double>(3, 1.0)),
|
||
2, 2);
|
||
const auto t0 = std::chrono::steady_clock::now();
|
||
auto curves = vde::curves::intersect_surfaces_refined(plane, bowl, 5, 1e-3, 3);
|
||
const auto t1 = std::chrono::steady_clock::now();
|
||
size_t total_pts = 0, max_pts = 0;
|
||
double max_pc = 0.0;
|
||
for (const auto& c : curves) { total_pts += c.points.size(); max_pts = std::max(max_pts, c.points.size());
|
||
if (c.fitted_curve_a) for (int k=0;k<=8;++k) {
|
||
auto uv=c.fitted_curve_a->evaluate(k/8.0); auto p=plane.evaluate(uv.x(),uv.y());
|
||
max_pc=std::max(max_pc,bowl.distance_to(p)); }
|
||
if (c.fitted_curve_b) for (int k=0;k<=8;++k) {
|
||
auto uv=c.fitted_curve_b->evaluate(k/8.0); auto p=bowl.evaluate(uv.x(),uv.y());
|
||
max_pc=std::max(max_pc,plane.distance_to(p)); }
|
||
}
|
||
std::printf("bowl refined curves=%zu pts=%zu max=%zu pcurve=%.3g ms=%.1f\n", curves.size(),
|
||
total_pts, max_pts, max_pc, std::chrono::duration<double, std::milli>(t1 - t0).count());
|
||
EXPECT_LT(max_pc, 2e-3);
|
||
EXPECT_FALSE(curves.empty());
|
||
}
|
||
|
||
|
||
TEST(SurfaceIntersection, TangentSpheresDoNotProduceBadCurve) {
|
||
auto s1 = vde::curves::make_sphere_surface(10.0, vde::core::Point3D(0, 0, 0));
|
||
auto s2 = vde::curves::make_sphere_surface(10.0, vde::core::Point3D(20, 0, 0));
|
||
auto curves = vde::curves::intersect_surfaces(s1, s2, 5, 1e-5);
|
||
for (const auto& c : curves) {
|
||
if (c.is_tangent_contact) continue;
|
||
for (const auto& p : c.points) {
|
||
EXPECT_LT(s1.distance_to(p), 1e-2);
|
||
EXPECT_LT(s2.distance_to(p), 1e-2);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
TEST(SurfaceIntersection, BoundaryTangentPlaneSphere) {
|
||
using vde::core::Point3D;
|
||
std::vector<std::vector<Point3D>> pg = {
|
||
{Point3D(-15, -15, 10), Point3D(15, -15, 10)},
|
||
{Point3D(-15, 15, 10), Point3D(15, 15, 10)}};
|
||
vde::curves::NurbsSurface plane(
|
||
pg, {0, 0, 1, 1}, {0, 0, 1, 1},
|
||
std::vector<std::vector<double>>{{1, 1}, {1, 1}}, 1, 1);
|
||
auto sphere = vde::curves::make_sphere_surface(10.0);
|
||
auto curves = vde::curves::intersect_surfaces(plane, sphere, 5, 1e-5);
|
||
for (const auto& c : curves) {
|
||
if (c.is_tangent_contact) continue;
|
||
for (const auto& p : c.points) {
|
||
EXPECT_LT(plane.distance_to(p), 1e-2);
|
||
EXPECT_LT(sphere.distance_to(p), 1e-2);
|
||
}
|
||
}
|
||
}
|