// VDE-1222: 2D geometry construction/editing operators — regression tests. // Contract: docs/reference/36-开放反馈全量实现计划.md §1, docs/feedback/VDE-1222.md #include #include "vde/curves/geom2d.h" #include "vde/curves/nurbs_curve.h" #include "vde/core/parametric.h" #include #include using namespace vde; using namespace vde::core; using namespace vde::curves; namespace { constexpr double kPi = 3.14159265358979323846; // minimum distance from a point to a sampled 2D curve (golden refined) double dist_to_curve(const Curve2D& c, const Point2D& p) { const int n = 400; double best = 1e300; int best_i = 0; for (int i = 0; i <= n; ++i) { const double d = (c.evaluate(static_cast(i) / n) - p).squaredNorm(); if (d < best) { best = d; best_i = i; } } double lo = std::max(0.0, static_cast(best_i - 1) / n); double hi = std::min(1.0, static_cast(best_i + 1) / n); for (int it = 0; it < 100; ++it) { const double m1 = lo + (hi - lo) / 3.0, m2 = hi - (hi - lo) / 3.0; if ((c.evaluate(m1) - p).squaredNorm() < (c.evaluate(m2) - p).squaredNorm()) hi = m2; else lo = m1; } return (c.evaluate(0.5 * (lo + hi)) - p).norm(); } double hit_residual(const Curve2D& a, const Curve2D& b, const Curve2DHit& h) { return (a.evaluate(h.sA) - b.evaluate(h.sB)).norm(); } // signed distance to the infinite line through two points double dist_to_line(const Point2D& a, const Point2D& b, const Point2D& p) { const Vector2D d = b - a; const double len = d.norm(); if (len < 1e-15) return (p - a).norm(); return std::abs((d.x() * (p.y() - a.y()) - d.y() * (p.x() - a.x())) / len); } } // namespace // ── classification / constructors ─────────────────────────────────────────── TEST(VDE1222Classify, Line) { auto c = make_line2d(Point2D(-1, 2), Point2D(3, 4)); auto cl = classify_curve2d(c); EXPECT_EQ(cl.kind, Curve2DKind::Line); EXPECT_TRUE(cl.valid); EXPECT_NEAR(cl.p0.x(), -1.0, 1e-12); EXPECT_NEAR(cl.p1.y(), 4.0, 1e-12); } TEST(VDE1222Classify, Circle) { auto c = make_circle2d(Point2D(2, -1), 3.0); auto cl = classify_curve2d(c); EXPECT_EQ(cl.kind, Curve2DKind::Circle); EXPECT_NEAR(cl.center.x(), 2.0, 1e-9); EXPECT_NEAR(cl.center.y(), -1.0, 1e-9); EXPECT_NEAR(cl.radius, 3.0, 1e-9); } TEST(VDE1222Classify, Arc) { auto c = make_arc2d(Point2D(0, 0), 2.0, 0.0, kPi / 2.0); auto cl = classify_curve2d(c); EXPECT_EQ(cl.kind, Curve2DKind::Arc); EXPECT_NEAR(cl.radius, 2.0, 1e-9); EXPECT_NEAR(cl.angle0, 0.0, 1e-9); EXPECT_NEAR(cl.angle1, kPi / 2.0, 1e-9); } TEST(VDE1222Classify, EllipseRotated) { auto c = make_ellipse2d(Point2D(1, 1), 3.0, 1.0, 0.4); auto cl = classify_curve2d(c); EXPECT_EQ(cl.kind, Curve2DKind::Ellipse); EXPECT_NEAR(cl.center.x(), 1.0, 1e-6); EXPECT_NEAR(cl.semi_a, 3.0, 1e-6); EXPECT_NEAR(cl.semi_b, 1.0, 1e-6); } // ── intersections ─────────────────────────────────────────────────────────── TEST(VDE1222Intersect, LineLine) { auto a = make_line2d(Point2D(-1, -1), Point2D(1, 1)); auto b = make_line2d(Point2D(-1, 1), Point2D(1, -1)); auto r = intersect_curves2d(a, b); ASSERT_EQ(r.hits.size(), 1u); EXPECT_LE(hit_residual(a, b, r.hits[0]), 1e-7); EXPECT_NEAR(r.hits[0].point.x(), 0.0, 1e-9); EXPECT_NEAR(r.hits[0].point.y(), 0.0, 1e-9); EXPECT_NEAR(r.hits[0].sA, 0.5, 1e-9); } TEST(VDE1222Intersect, LineLineParallel) { auto a = make_line2d(Point2D(0, 0), Point2D(2, 1)); auto b = make_line2d(Point2D(0, 1), Point2D(2, 2)); auto r = intersect_curves2d(a, b); EXPECT_TRUE(r.hits.empty()); EXPECT_TRUE(r.overlaps.empty()); EXPECT_FALSE(r.unsupported); } TEST(VDE1222Intersect, LineLineCoincidentOverlap) { auto a = make_line2d(Point2D(0, 0), Point2D(4, 0)); auto b = make_line2d(Point2D(1, 0), Point2D(3, 0)); auto r = intersect_curves2d(a, b); EXPECT_FALSE(r.overlaps.empty()); EXPECT_NEAR(r.overlaps[0].first.first, 0.25, 1e-9); EXPECT_NEAR(r.overlaps[0].first.second, 0.75, 1e-9); } TEST(VDE1222Intersect, LineCircleTwoHits) { auto line = make_line2d(Point2D(-3, 0), Point2D(3, 0)); auto circle = make_circle2d(Point2D(0, 0), 2.0); auto r = intersect_curves2d(line, circle); ASSERT_EQ(r.hits.size(), 2u); for (const auto& h : r.hits) { EXPECT_LE(hit_residual(line, circle, h), 1e-7); EXPECT_NEAR(std::abs(h.point.x()), 2.0, 1e-9); EXPECT_NEAR(h.point.y(), 0.0, 1e-9); } } TEST(VDE1222Intersect, LineCircleTangentOneHit) { auto line = make_line2d(Point2D(-3, 2), Point2D(3, 2)); auto circle = make_circle2d(Point2D(0, 0), 2.0); auto r = intersect_curves2d(line, circle); ASSERT_EQ(r.hits.size(), 1u); EXPECT_LE(hit_residual(line, circle, r.hits[0]), 1e-7); } TEST(VDE1222Intersect, LineArcFiltersOutsideArc) { auto line = make_line2d(Point2D(-3, 1), Point2D(3, 1)); auto arc = make_arc2d(Point2D(0, 0), 2.0, 0.0, kPi / 2.0); auto r = intersect_curves2d(line, arc); ASSERT_EQ(r.hits.size(), 1u); EXPECT_LE(hit_residual(line, arc, r.hits[0]), 1e-7); EXPECT_GE(r.hits[0].point.x(), 0.0); } TEST(VDE1222Intersect, CircleCircleTwoHits) { auto a = make_circle2d(Point2D(0, 0), 2.0); auto b = make_circle2d(Point2D(2, 0), 2.0); auto r = intersect_curves2d(a, b); ASSERT_EQ(r.hits.size(), 2u); for (const auto& h : r.hits) { EXPECT_LE(hit_residual(a, b, h), 1e-7); EXPECT_NEAR(h.point.x(), 1.0, 1e-9); } } TEST(VDE1222Intersect, CircleCircleCoincidentOverlap) { auto a = make_circle2d(Point2D(0, 0), 2.0); auto b = make_circle2d(Point2D(0, 0), 2.0); auto r = intersect_curves2d(a, b); EXPECT_FALSE(r.overlaps.empty()); EXPECT_TRUE(r.hits.empty()); } TEST(VDE1222Intersect, LineEllipse) { auto line = make_line2d(Point2D(-4, 0), Point2D(4, 0)); auto ellipse = make_ellipse2d(Point2D(0, 0), 3.0, 1.0); auto r = intersect_curves2d(line, ellipse); ASSERT_EQ(r.hits.size(), 2u); for (const auto& h : r.hits) { EXPECT_LE(hit_residual(line, ellipse, h), 1e-7); EXPECT_NEAR(std::abs(h.point.x()), 3.0, 1e-8); } } TEST(VDE1222Intersect, CircleEllipse) { auto circle = make_circle2d(Point2D(0, 0), 1.0); auto ellipse = make_ellipse2d(Point2D(0, 0), 2.0, 0.5); auto r = intersect_curves2d(circle, ellipse); ASSERT_GE(r.hits.size(), 2u); for (const auto& h : r.hits) EXPECT_LE(hit_residual(circle, ellipse, h), 1e-7); } TEST(VDE1222Intersect, EllipseEllipse) { auto e1 = make_ellipse2d(Point2D(0, 0), 2.0, 1.0); auto e2 = make_ellipse2d(Point2D(1.0, 0), 2.0, 1.0); auto r = intersect_curves2d(e1, e2); ASSERT_GE(r.hits.size(), 2u); for (const auto& h : r.hits) EXPECT_LE(hit_residual(e1, e2, h), 1e-7); } TEST(VDE1222Intersect, LineVsSpline) { auto spline = Curve2D([](double s) { const double t = 2.0 * kPi * s; const double r = 1.0 + 0.15 * std::sin(3.0 * t); return Point2D(r * std::cos(t), r * std::sin(t)); }, 256); auto line = make_line2d(Point2D(-2, 0), Point2D(2, 0)); auto r = intersect_curves2d(line, spline); EXPECT_FALSE(r.hits.empty()); for (const auto& h : r.hits) EXPECT_LE(hit_residual(line, spline, h), 1e-7); } TEST(VDE1222Intersect, SplineVsSpline) { // two polyline-like free-form curves crossing twice auto a = Curve2D([](double s) { if (s < 0.5) return Point2D(4.0 * s, 2.0 * s); return Point2D(2.0 - 2.0 * (s - 0.5), 2.0 - 2.0 * (s - 0.5)); }, 256); auto b = Curve2D([](double s) { if (s < 0.5) return Point2D(4.0 * s, 1.0 - 2.0 * s); return Point2D(2.0 - 2.0 * (s - 0.5), -1.0 + 2.0 * (s - 0.5)); }, 256); auto r = intersect_curves2d(a, b); EXPECT_FALSE(r.hits.empty()); for (const auto& h : r.hits) EXPECT_LE(hit_residual(a, b, h), 1e-6); } TEST(VDE1222Intersect, UnsupportedDoesNotCrash) { Curve2D invalid; auto line = make_line2d(Point2D(0, 0), Point2D(1, 1)); auto r = intersect_curves2d(invalid, line); EXPECT_TRUE(r.unsupported); EXPECT_TRUE(r.hits.empty()); auto r2 = intersect_curves2d(invalid, invalid); EXPECT_TRUE(r2.unsupported); } // ── fillet ────────────────────────────────────────────────────────────────── TEST(VDE1222Fillet, LineLine) { auto a = make_line2d(Point2D(-2, 0), Point2D(0, 0)); auto b = make_line2d(Point2D(0, 0), Point2D(0, 2)); auto tools = fillet_corner2d(a, b, 0.3, Point2D(-1, 1)); ASSERT_FALSE(tools.empty()); for (const auto& t : tools) { EXPECT_NEAR(t.radius, 0.3, 1e-12); EXPECT_LE(dist_to_curve(a, t.tangent_a), 1e-7); EXPECT_LE(dist_to_curve(b, t.tangent_b), 1e-7); const auto arc_cl = classify_curve2d(t.curve); EXPECT_EQ(arc_cl.kind, Curve2DKind::Arc); EXPECT_NEAR(arc_cl.radius, 0.3, 1e-9); EXPECT_NEAR((arc_cl.center - t.tangent_a).norm(), 0.3, 1e-7); EXPECT_NEAR((arc_cl.center - t.tangent_b).norm(), 0.3, 1e-7); } EXPECT_NEAR(tools.front().tangent_a.x(), -0.3, 1e-6); } TEST(VDE1222Fillet, LineLineNearestPickOrdering) { auto a = make_line2d(Point2D(-2, 0), Point2D(2, 0)); auto b = make_line2d(Point2D(0, -2), Point2D(0, 2)); auto tools_pos = fillet_corner2d(a, b, 0.5, Point2D(1, 1)); auto tools_neg = fillet_corner2d(a, b, 0.5, Point2D(-1, -1)); ASSERT_FALSE(tools_pos.empty()); ASSERT_FALSE(tools_neg.empty()); EXPECT_GT(tools_pos.front().tangent_a.x(), 0.0); EXPECT_LT(tools_neg.front().tangent_a.x(), 0.0); } TEST(VDE1222Fillet, LineCircle) { auto line = make_line2d(Point2D(-5, 0), Point2D(5, 0)); auto circle = make_circle2d(Point2D(0, 2), 1.0); auto tools = fillet_corner2d(line, circle, 0.5, Point2D(0, 0.2), 1e-9); ASSERT_FALSE(tools.empty()); const Point2D c2(0, 2); for (const auto& t : tools) { const auto arc_cl = classify_curve2d(t.curve); ASSERT_EQ(arc_cl.kind, Curve2DKind::Arc); EXPECT_NEAR(dist_to_line(Point2D(-5, 0), Point2D(5, 0), arc_cl.center), 0.5, 1e-4); const double dc = (arc_cl.center - c2).norm(); const bool external = std::abs(dc - 1.5) < 1e-3; const bool internal = std::abs(dc - 0.5) < 1e-3; EXPECT_TRUE(external || internal); EXPECT_LE(dist_to_curve(line, t.tangent_a), 1e-5); EXPECT_LE(dist_to_curve(circle, t.tangent_b), 1e-5); } } TEST(VDE1222Fillet, CircleCircle) { auto c1 = make_circle2d(Point2D(0, 0), 2.0); auto c2 = make_circle2d(Point2D(3, 0), 1.0); auto tools = fillet_corner2d(c1, c2, 0.3, Point2D(0.0, 1.0)); ASSERT_FALSE(tools.empty()); for (const auto& t : tools) { const auto arc_cl = classify_curve2d(t.curve); ASSERT_EQ(arc_cl.kind, Curve2DKind::Arc); const double d1 = (arc_cl.center - Point2D(0, 0)).norm(); const double d2 = (arc_cl.center - Point2D(3, 0)).norm(); const bool ok = (std::abs(d1 - 2.3) < 1e-3 && std::abs(d2 - 1.3) < 1e-3) || (std::abs(d1 - 1.7) < 1e-3 && std::abs(d2 - 0.7) < 1e-3) || (std::abs(d1 - 2.3) < 1e-3 && std::abs(d2 - 0.7) < 1e-3) || (std::abs(d1 - 1.7) < 1e-3 && std::abs(d2 - 1.3) < 1e-3); EXPECT_TRUE(ok) << "d1=" << d1 << " d2=" << d2; EXPECT_FALSE(t.trimmed_a); EXPECT_FALSE(t.trimmed_b); } } TEST(VDE1222Fillet, LineEllipse) { auto line = make_line2d(Point2D(-4, 0), Point2D(4, 0)); auto ellipse = make_ellipse2d(Point2D(0, 0.5), 2.0, 1.0); auto tools = fillet_corner2d(line, ellipse, 0.4, Point2D(0, 0.2)); EXPECT_FALSE(tools.empty()); for (const auto& t : tools) { const auto arc_cl = classify_curve2d(t.curve); ASSERT_EQ(arc_cl.kind, Curve2DKind::Arc); EXPECT_NEAR(std::abs(arc_cl.center.y()), 0.4, 1e-3); EXPECT_LE(dist_to_curve(line, t.tangent_a), 1e-4); EXPECT_LE(dist_to_curve(ellipse, t.tangent_b), 1e-4); } } TEST(VDE1222Chamfer, LineLine) { auto a = make_line2d(Point2D(-2, 0), Point2D(0, 0)); auto b = make_line2d(Point2D(0, 0), Point2D(0, 2)); auto tools = chamfer_corner2d(a, b, 0.3, 0.4, Point2D(-1, 1)); ASSERT_FALSE(tools.empty()); const auto& t = tools.front(); EXPECT_NEAR((t.tangent_a - Point2D(-0.3, 0)).norm(), 0.0, 1e-6); EXPECT_NEAR((t.tangent_b - Point2D(0, 0.4)).norm(), 0.0, 1e-6); EXPECT_NEAR((t.tangent_b - t.tangent_a).norm(), std::sqrt(0.09 + 0.16), 1e-6); EXPECT_LE(dist_to_curve(a, t.tangent_a), 1e-7); EXPECT_LE(dist_to_curve(b, t.tangent_b), 1e-7); } // ── TTR / TTT ─────────────────────────────────────────────────────────────── TEST(VDE1222TTR, LineLine) { auto a = make_line2d(Point2D(-5, 0), Point2D(5, 0)); auto b = make_line2d(Point2D(1, -5), Point2D(1, 5)); auto sols = tangent_circle_ttr(a, b, 0.5, Point2D(0.5, 0.5)); ASSERT_FALSE(sols.empty()); for (const auto& s : sols) { EXPECT_NEAR(s.radius, 0.5, 1e-12); EXPECT_NEAR(std::abs(s.center.y()), 0.5, 1e-7); EXPECT_NEAR(std::abs(s.center.x() - 1.0), 0.5, 1e-7); } EXPECT_NEAR((sols.front().center - Point2D(0.5, 0.5)).norm(), 0.0, 1e-6); } TEST(VDE1222TTR, LineCircle) { auto line = make_line2d(Point2D(-5, 0), Point2D(5, 0)); auto circle = make_circle2d(Point2D(0, 1), 1.0); auto sols = tangent_circle_ttr(line, circle, 0.5, Point2D(0, 0.5)); ASSERT_FALSE(sols.empty()); for (const auto& s : sols) { EXPECT_NEAR(std::abs(s.center.y()), 0.5, 1e-6); const double dc = (s.center - Point2D(0, 1)).norm(); const bool tang = std::abs(dc - 1.5) < 1e-5 || std::abs(dc - 0.5) < 1e-5; EXPECT_TRUE(tang) << "dist=" << dc; EXPECT_LE(dist_to_curve(circle, s.tangent_b), 1e-5); } } TEST(VDE1222TTR, CircleCircle) { auto c1 = make_circle2d(Point2D(0, 0), 2.0); auto c2 = make_circle2d(Point2D(4, 0), 1.0); auto sols = tangent_circle_ttr(c1, c2, 0.5, Point2D(2, 0)); ASSERT_FALSE(sols.empty()); for (const auto& s : sols) { const double d1 = s.center.norm(); const double d2 = (s.center - Point2D(4, 0)).norm(); const bool ext = std::abs(d1 - 2.5) < 1e-4 && std::abs(d2 - 1.5) < 1e-4; const bool ext2 = std::abs(d1 - 1.5) < 1e-4 && std::abs(d2 - 1.5) < 1e-4; const bool ext3 = std::abs(d1 - 2.5) < 1e-4 && std::abs(d2 - 0.5) < 1e-4; const bool ext4 = std::abs(d1 - 1.5) < 1e-4 && std::abs(d2 - 0.5) < 1e-4; EXPECT_TRUE(ext || ext2 || ext3 || ext4) << "d1=" << d1 << " d2=" << d2; } } TEST(VDE1222TTT, ThreeLinesIncircle) { auto a = make_line2d(Point2D(-1, 0), Point2D(5, 0)); // y = 0 auto b = make_line2d(Point2D(0, -1), Point2D(0, 5)); // x = 0 auto c = make_line2d(Point2D(2.5, -0.5), Point2D(-0.5, 2.5)); // x + y = 2 auto sols = tangent_circle_ttt(a, b, c, Point2D(0.6, 0.6)); ASSERT_FALSE(sols.empty()); bool found_incircle = false; for (const auto& s : sols) { EXPECT_NEAR(std::abs(s.center.y()), s.radius, 1e-5); EXPECT_NEAR(std::abs(s.center.x()), s.radius, 1e-5); EXPECT_NEAR(std::abs(s.center.x() + s.center.y() - 2.0) / std::sqrt(2.0), s.radius, 1e-4); if (std::abs(s.radius - (2.0 / (2.0 + std::sqrt(2.0)))) < 1e-4 && s.center.x() > 0 && s.center.y() > 0) { found_incircle = true; } } EXPECT_TRUE(found_incircle); } // ── closed interpolation + fairing ────────────────────────────────────────── TEST(VDE1222Interpolate, ClosedCurveClosesAndPassesPoints) { std::vector pts; for (int i = 0; i < 8; ++i) { const double a = 2.0 * kPi * i / 8.0; pts.emplace_back(2.0 * std::cos(a), 2.0 * std::sin(a), 0.0); } auto c = NurbsCurve::interpolate(pts, 3, /*closed=*/true); ASSERT_FALSE(c.control_points().empty()); auto [t0, t1] = c.domain(); EXPECT_NEAR((c.evaluate(t0) - c.evaluate(t1)).norm(), 0.0, 1e-9); for (const auto& p : pts) { double best = 1e300; for (int i = 0; i <= 800; ++i) best = std::min(best, (c.evaluate(t0 + (t1 - t0) * i / 800.0) - p).norm()); EXPECT_LE(best, 1e-3); } EXPECT_LE(c.control_points().size(), pts.size() + 2); // old open API still available / compatible auto open = NurbsCurve::interpolate(pts); EXPECT_FALSE(open.control_points().empty()); EXPECT_GT((open.evaluate(open.domain().second) - open.evaluate(open.domain().first)).norm(), 1.0); } TEST(VDE1222Fair, LaplacianEnergyDownEndpointsLocked) { std::vector pts; for (int i = 0; i < 12; ++i) { const double x = static_cast(i) / 11.0; const double noise = (i % 2 == 0 ? 1.0 : -1.0) * 0.08; pts.emplace_back(x, noise, 0.0); } auto r = fair_curve(pts, CurveFairMode::Laplacian, 1e-10, 0.0, 0); ASSERT_EQ(r.points.size(), pts.size()); EXPECT_LT(r.final_energy, r.initial_energy); EXPECT_NEAR((r.points.front() - pts.front()).norm(), 0.0, 1e-12); EXPECT_NEAR((r.points.back() - pts.back()).norm(), 0.0, 1e-12); ASSERT_EQ(r.status.size(), pts.size()); EXPECT_EQ(r.status.front().state, CurveFairPointStatus::State::Clamped); EXPECT_GT(r.iterations, 0); } TEST(VDE1222Fair, BendingTubeAndG1Lock) { std::vector pts; for (int i = 0; i < 16; ++i) { const double x = static_cast(i) / 15.0; const double noise = std::sin(7.0 * x) * 0.12; pts.emplace_back(x, noise, 0.0); } const double tube = 0.05; auto r = fair_curve(pts, CurveFairMode::BendingEnergy, 1e-10, tube, 1); EXPECT_LT(r.final_energy, r.initial_energy); for (size_t i = 0; i < pts.size(); ++i) EXPECT_LE((r.points[i] - pts[i]).norm(), tube + 1e-9); EXPECT_NEAR((r.points.front() - pts.front()).norm(), 0.0, 1e-12); EXPECT_NEAR((r.points[1] - pts[1]).norm(), 0.0, 1e-12); EXPECT_NEAR((r.points.back() - pts.back()).norm(), 0.0, 1e-12); } // ── analytic parallel curves ──────────────────────────────────────────────── TEST(VDE1222Parallel, LineExact) { auto line = make_line2d(Point2D(0, 0), Point2D(4, 0)); auto off = parallel_curve_analytic(line, 1.0); auto cl = classify_curve2d(off); ASSERT_EQ(cl.kind, Curve2DKind::Line); for (int i = 0; i <= 20; ++i) { const Point2D p = off.evaluate(i / 20.0); EXPECT_NEAR(std::abs(p.y()), 1.0, 1e-9); EXPECT_NEAR(std::abs(p.y() - line.evaluate(i / 20.0).y()), 1.0, 1e-9); } } TEST(VDE1222Parallel, CircleExact) { auto circle = make_circle2d(Point2D(1, -1), 2.0); auto off = parallel_curve_analytic(circle, 0.5); auto cl = classify_curve2d(off); ASSERT_EQ(cl.kind, Curve2DKind::Circle); EXPECT_NEAR(cl.center.x(), 1.0, 1e-9); EXPECT_NEAR(cl.center.y(), -1.0, 1e-9); EXPECT_NEAR(cl.radius, 1.5, 1e-9); for (int i = 0; i <= 32; ++i) { const Point2D p = off.evaluate(i / 32.0); EXPECT_NEAR(std::abs((p - Point2D(1, -1)).norm() - 2.0), 0.5, 1e-9); } } TEST(VDE1222Parallel, EllipseApproximation) { auto ellipse = make_ellipse2d(Point2D(0, 0), 3.0, 1.5, 0.3); const double d = 0.4; auto off = parallel_curve_analytic(ellipse, d); ASSERT_TRUE(off.valid()); double max_err = 0.0; for (int i = 0; i <= 40; ++i) { const Point2D p = off.evaluate(i / 40.0); max_err = std::max(max_err, std::abs(dist_to_curve(ellipse, p) - d)); } EXPECT_LT(max_err, 0.02) << "approx offset max error=" << max_err; } // ═══════════════════════════════════════════════════════════ // VDE-1222 follow-up (independent verification): real spline // intersections + multi-solution fillet/chamfer pick quadrant. // ═══════════════════════════════════════════════════════════ namespace { // 5-point cubic spline (NurbsCurve::interpolate) as a normalized-parameter Curve2D Curve2D spline_curve2d(const std::vector& pts) { auto nc = NurbsCurve::interpolate(pts, 3); auto [a, b] = nc.domain(); auto fn = [nc, a, b](double s) { const Point3D p = nc.evaluate(a + (b - a) * s); return Point2D(p.x(), p.y()); }; return Curve2D(fn, 256); } int y_crossings(const Curve2D& c) { int n = 4000, count = 0; double prev = c.evaluate(0.0).y(); for (int i = 1; i <= n; ++i) { const double y = c.evaluate(static_cast(i) / n).y(); if ((prev <= 0.0 && y > 0.0) || (prev >= 0.0 && y < 0.0)) ++count; prev = y; } return count; } } // namespace TEST(VDE1222Intersect, LineSplineFourIntersections) { auto spline = spline_curve2d({{-4, 2, 0}, {-2, -3, 0}, {0, 3, 0}, {2, -3, 0}, {4, 2, 0}}); auto line = make_line2d(Point2D(-6, 0), Point2D(6, 0)); const int brute = y_crossings(spline); ASSERT_EQ(brute, 4); auto r = intersect_curves2d(spline, line, 1e-9, 64); EXPECT_FALSE(r.unsupported); ASSERT_EQ(r.hits.size(), static_cast(brute)) << "expected all 4 line-spline intersections, got " << r.hits.size(); for (const auto& h : r.hits) { EXPECT_LE((spline.evaluate(h.sA) - line.evaluate(h.sB)).norm(), 1e-7); EXPECT_LE(std::abs(h.point.y()), 1e-7); EXPECT_GE(h.sA, 0.0); EXPECT_LE(h.sA, 1.0); } // reversed argument order auto r2 = intersect_curves2d(line, spline, 1e-9, 64); EXPECT_EQ(r2.hits.size(), r.hits.size()); for (const auto& h : r2.hits) EXPECT_LE((spline.evaluate(h.sB) - line.evaluate(h.sA)).norm(), 1e-7); } TEST(VDE1222Intersect, SplineSplineFourIntersections) { auto a = spline_curve2d({{-4, 2, 0}, {-2, -3, 0}, {0, 3, 0}, {2, -3, 0}, {4, 2, 0}}); auto b = spline_curve2d({{-4, -2, 0}, {-2, 3, 0}, {0, -3, 0}, {2, 3, 0}, {4, -2, 0}}); auto r = intersect_curves2d(a, b, 1e-9, 64); EXPECT_FALSE(r.unsupported); ASSERT_EQ(r.hits.size(), 4u) << "expected 4 spline-spline intersections"; for (const auto& h : r.hits) { EXPECT_LE((a.evaluate(h.sA) - b.evaluate(h.sB)).norm(), 1e-7); EXPECT_NEAR(h.point.y(), 0.0, 1e-6); } } TEST(VDE1222Chamfer, PickQuadrants) { auto ax = make_line2d(Point2D(-2, 0), Point2D(2, 0)); auto ay = make_line2d(Point2D(0, -2), Point2D(0, 2)); struct Case { Point2D pick; double sx, sy; }; const Case cases[4] = {{{1, 1}, 1, 1}, {{-1, 1}, -1, 1}, {{-1, -1}, -1, -1}, {{1, -1}, 1, -1}}; for (const auto& c : cases) { auto tools = chamfer_corner2d(ax, ay, 0.5, 0.5, c.pick); ASSERT_FALSE(tools.empty()) << "pick " << c.pick.x() << "," << c.pick.y(); const auto& t = tools.front(); EXPECT_NEAR(t.tangent_a.x(), c.sx * 0.5, 1e-6) << "chamfer trim on A wrong side for pick (" << c.pick.x() << "," << c.pick.y() << ")"; EXPECT_NEAR(t.tangent_a.y(), 0.0, 1e-6); EXPECT_NEAR(t.tangent_b.y(), c.sy * 0.5, 1e-6); EXPECT_NEAR(t.tangent_b.x(), 0.0, 1e-6); EXPECT_LE(dist_to_curve(ax, t.tangent_a), 1e-7); EXPECT_LE(dist_to_curve(ay, t.tangent_b), 1e-7); } } TEST(VDE1222Fillet, PickQuadrants) { auto ax = make_line2d(Point2D(-2, 0), Point2D(2, 0)); auto ay = make_line2d(Point2D(0, -2), Point2D(0, 2)); struct Case { Point2D pick; double sx, sy; }; const Case cases[4] = {{{1, 1}, 1, 1}, {{-1, 1}, -1, 1}, {{-1, -1}, -1, -1}, {{1, -1}, 1, -1}}; for (const auto& c : cases) { auto tools = fillet_corner2d(ax, ay, 0.5, c.pick); ASSERT_FALSE(tools.empty()) << "pick " << c.pick.x() << "," << c.pick.y(); const auto& t = tools.front(); EXPECT_NEAR(t.tangent_a.x(), c.sx * 0.5, 1e-6) << "fillet trim on A wrong side for pick (" << c.pick.x() << "," << c.pick.y() << ")"; EXPECT_NEAR(t.tangent_a.y(), 0.0, 1e-6); EXPECT_NEAR(t.tangent_b.y(), c.sy * 0.5, 1e-6); EXPECT_NEAR(t.tangent_b.x(), 0.0, 1e-6); } }