Files
hm 306374e455
ci / ubuntu-latest / clang (push) Failing after 33s
ci / ubuntu-latest / gcc (push) Failing after 35s
ci / coverage / gcc (push) Failing after 34s
ci / macos-latest / apple-clang (push) Canceled after 0s
ci / windows-latest / mingw (push) Canceled after 0s
ci / windows-latest / msvc (push) Canceled after 0s
fix(solver): N145 软锚增广入口不可行时尺寸权威丢失——SolveModel 增加 settle-polish 二阶段(opt-in 门控)
问题:草图尺寸编辑(如先标 90° 再标长度 70)常态「入口不可行」——当前几何
违反新标注。g719 软锚/settle-in-place 的增广目标 ½‖r‖²+½Σw²Δ² 在入口
不可行时结构性失效:驻点满足 Jᵀr = −w²Δ ≠ 0,约束残差恒为 O(w·位移)
量级,收敛闸 1e-10 永远不可达,求解器只能停滞后报 SolverError(几何不动、
标注与几何脱节,用户看到先标的 90° 被后标的 70 改掉)。

修复:SolveOptions 新增 polish_stalled_soft_anchor(默认 false = g719 语义
逐位保留;拖拽预览等「折中几何即产出」场景不受影响,其 host 走
DragController buildOnly_ 独立路径)。置 true 时,SolveModel 在增广求解
停滞(未收敛但无 SolverError)后进入二段抛光:保存增广终态 → 剥软锚保留
硬锚 → 从增广终态续解纯约束 → 收敛则采用并回填 anchored_params,未收敛
则恢复增广终态(诚实不收敛)。尺寸权威语义(标注值精确满足 + 几何最小
移动)由此成立;有限权重罚函数不可能同时满足两者,必须二段。

shell 接入:ViewDesign 草图约束系统(constraint_system.cpp)尺寸编辑路径
置 opts.polish_stalled_soft_anchor = true;无软锚时惰性零开销。

测试:新增 NewtonTest.PolishStalledSoftAnchorDimensionAuthoritative——
线段 Length=12 + 双 Line2D Angle=π/2、三实体软锚 w=1:polish=true 时
converged 且 Length=12±1e-9、phi_a−phi_b=π/2±1e-9;polish=false(默认)
时 !converged 且 Length<11.5(折中驻点)。test_newton 26/26 全绿,
g719 两个软锚语义测试(SplitMotionByInverseSquare / EntityLevelMatches
PerParam)默认路径逐位保留。

回归:ctest 71 门——正确性全绿(含 fuzz 6/6、corpus、kinematics);
既有非本次改动失败如实保留:test_model.cpp(2115) 0.0/0.0 MSVC C2124 与
vdcs_fuzz_expression.cpp(306) C2220 既有编译失败、vdcs_host_adapter_demo
(本构建关 examples)、vdcs_install_smoke(ctest 环境无 Ninja/编译器)、
vdcs_suite_repro/vdcs_bench/test_decomposer 墙钟性能门(机器负载敏感)、
vdcs_ab_compare(1500s 预算 Release 树 A/B 性能门,CI --exclude-regex 排除)。

验证:无头复现 8 档权重扫描修复前全 SolverError → 修复后 angle=90.0000
len=70.0000 精确满足;连续编辑(90°→+70→改 45°)全精确;真实软件端到端
(四边形→标 90°→再标 70)90° 保持、右边竖直(verification/n145-real-90plus70.png)。
2026-11-13 05:54:37 +08:00

778 lines
34 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 <cmath>
#include <limits>
#include <memory>
#include <vector>
#include "vdcs/core/dogleg_solver.h"
#include "vdcs/core/newton_solver.h"
#include "vdcs/core/sparse_lm_solver.h"
#include "vdcs/model/constraint.h"
#include "vdcs/model/entity.h"
#include "vdcs/model/model.h"
using vdcs::Model;
using vdcs::Point2D;
using vdcs::Segment2D;
using vdcs::SolveOptions;
namespace {
struct TwoPointModel {
Model model;
vdcs::Uid a;
vdcs::Uid b;
};
TwoPointModel BuildTwoPointModel(double bx, double by, double d) {
TwoPointModel t;
auto a = t.model.AddPoint2D("P1", 0.0, 0.0);
auto b = t.model.AddPoint2D("P2", bx, by);
t.a = a.Value();
t.b = b.Value();
(void)t.model.AddFixed2D("F1", t.a);
(void)t.model.AddDistance2D("D1", t.a, t.b, d);
return t;
}
double PointDistance(const Model& model, vdcs::Uid a, vdcs::Uid b) {
const auto* pa = static_cast<const Point2D*>(model.FindEntity(a));
const auto* pb = static_cast<const Point2D*>(model.FindEntity(b));
const double dx = pb->X() - pa->X();
const double dy = pb->Y() - pa->Y();
return std::sqrt(dx * dx + dy * dy);
}
} // namespace
TEST(NewtonTest, TwoPointDistanceConverges) {
auto t = BuildTwoPointModel(10.0, 0.0, 12.5);
auto r = t.model.Solve(SolveOptions{});
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
// 固定点不动(残差容差内,允许 ulp 级漂移)
const auto* p1 = static_cast<const Point2D*>(t.model.FindEntity(t.a));
EXPECT_NEAR(p1->X(), 0.0, 1e-12);
EXPECT_NEAR(p1->Y(), 0.0, 1e-12);
// |AB| = 12.5
EXPECT_NEAR(PointDistance(t.model, t.a, t.b), 12.5, 1e-9);
}
TEST(NewtonTest, UnderConstrainedStillConverges) {
// M = 1(B 绕 A 旋转自由):求解应收敛到某个满足距离的解
auto t = BuildTwoPointModel(10.0, 0.1, 12.5);
auto r = t.model.Solve(SolveOptions{});
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_NEAR(PointDistance(t.model, t.a, t.b), 12.5, 1e-9);
}
/// VDCS-001:欠定系统的最小范数语义。两自由点 A、B 只受 |AB|=15 约束
/// (M=3:平移×2 + 旋转),无任何锚定——最简欠定面。求解应落在最小
/// 位移解上:牛顿步若返回「基本解」(colPivHouseholderQr 把自由变量
/// 置零)只移动 A 或只移动 B,中点(平移零空间分量)漂移;最小范数
/// 步(COD)落点在雅可比行空间内、与平移零空间正交 ⇒ 中点严格守恒。
/// 该不变量对 Newton(COD)与 LM(λ 正则化)两条下降路径同时成立,
/// 是对「欠定方向不乱动」的强判据(不依赖任何锚定/收敛容差口径)。
TEST(NewtonTest, UnderConstrainedStepPreservesBarycenter) {
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 10.0, 0.1);
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 15.0);
auto r = model.Solve(SolveOptions{});
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
const auto* pa = static_cast<const Point2D*>(model.FindEntity(a.Value()));
const auto* pb = static_cast<const Point2D*>(model.FindEntity(b.Value()));
ASSERT_NE(pa, nullptr);
ASSERT_NE(pb, nullptr);
EXPECT_NEAR(PointDistance(model, a.Value(), b.Value()), 15.0, 1e-8);
// 中点守恒 = 平移零空间不变量(最小范数解充要特征)
EXPECT_NEAR(pa->X() + pb->X(), 10.0, 1e-8);
EXPECT_NEAR(pa->Y() + pb->Y(), 0.1, 1e-8);
// 方向守恒 = 旋转零空间不变量(终态 BA 与初始 (10,0.1) 共线 ⇒ 叉积为 0)
EXPECT_NEAR((pb->X() - pa->X()) * 0.1 - (pb->Y() - pa->Y()) * 10.0, 0.0,
1e-8);
}
TEST(NewtonTest, CoincidentChain) {
// A 固定,B 与 A 重合,C 与 B 重合 → 三点全部重合
Model model;
auto a = model.AddPoint2D("P1", 1.0, 2.0);
auto b = model.AddPoint2D("P2", 5.0, -3.0);
auto c = model.AddPoint2D("P3", -7.0, 0.5);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddCoincident2D("C1", a.Value(), b.Value());
(void)model.AddCoincident2D("C2", b.Value(), c.Value());
auto r = model.Solve(SolveOptions{});
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
const auto* pa = static_cast<const Point2D*>(model.FindEntity(a.Value()));
const auto* pb = static_cast<const Point2D*>(model.FindEntity(b.Value()));
const auto* pc = static_cast<const Point2D*>(model.FindEntity(c.Value()));
EXPECT_NEAR(pb->X(), pa->X(), 1e-9);
EXPECT_NEAR(pb->Y(), pa->Y(), 1e-9);
EXPECT_NEAR(pc->X(), pa->X(), 1e-9);
EXPECT_NEAR(pc->Y(), pa->Y(), 1e-9);
}
TEST(NewtonTest, TriangleWellConstrained) {
// FRD §5.1 算例:完整约束三角形
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 4.0, 0.0);
auto c = model.AddPoint2D("P3", 1.0, 2.0);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 5.0);
(void)model.AddDistance2D("D2", a.Value(), c.Value(), 4.0);
(void)model.AddDistance2D("D3", b.Value(), c.Value(), 3.0);
// FRD §5.1:Horizontal(AB) 消除整体旋转 → M = 6 − 6 = 0
(void)model.AddHorizontal2D("H1", a.Value(), b.Value());
auto r = model.Solve(SolveOptions{});
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_EQ(r.Value().mobility, 0);
EXPECT_NEAR(PointDistance(model, a.Value(), b.Value()), 5.0, 1e-8);
EXPECT_NEAR(PointDistance(model, a.Value(), c.Value()), 4.0, 1e-8);
EXPECT_NEAR(PointDistance(model, b.Value(), c.Value()), 3.0, 1e-8);
}
TEST(NewtonTest, InconsistentNotConverged) {
// A 固定、B 固定(相距 10),但要求 |AB| = 5:矛盾,不可收敛
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 10.0, 0.0);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddFixed2D("F2", b.Value());
auto d = model.AddDistance2D("D1", a.Value(), b.Value(), 5.0);
ASSERT_TRUE(d.Ok());
auto r = model.Solve(SolveOptions{});
ASSERT_TRUE(r.Ok()); // Result 本身成功(有报告),但收敛标志为假
EXPECT_FALSE(r.Value().converged);
EXPECT_GT(r.Value().residual_norm, 1e-6);
}
TEST(NewtonTest, EmptyModel) {
Model model;
auto r = model.Solve(SolveOptions{});
ASSERT_FALSE(r.Ok());
EXPECT_EQ(r.Err().Code(), vdcs::ErrorCode::kEmptyModel);
}
TEST(NewtonTest, NoConstraintsTriviallyConverged) {
Model model;
auto a = model.AddPoint2D("P1", 1.0, 2.0);
ASSERT_TRUE(a.Ok());
auto r = model.Solve(SolveOptions{});
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
const auto* p = static_cast<const Point2D*>(model.FindEntity(a.Value()));
EXPECT_DOUBLE_EQ(p->X(), 1.0); // 未被扰动
}
/// g698:写回边界合法域拒绝(与 SparseLm/DogLeg/Decomposed 同口径)。
/// 精确退化参数(抛物线 F=V,焦距恰 0)经 AdoptEntity 装载 + Fixed2D
/// 钉扎当前参数 → 初值即残差 0(Newton 零步收敛)→ CommitSolveParams
/// 以 ParamDomainOk 拒绝(kInvalidInput),入口参数逐位保持
///(g150/g162 契约;此前该路径仅 SparseLm/DogLeg/Decomposed 覆盖)
TEST(NewtonTest, RejectsOutOfDomainProductAtCommit) {
Model model;
const vdcs::Uid pid = 910;
model.AdoptEntity(
std::make_unique<vdcs::Parabola2D>(pid, 0.0, 0.0, 0.0, 0.0));
Eigen::VectorXd pref(4);
pref << 0.0, 0.0, 0.0, 0.0;
model.AdoptConstraint(
std::make_unique<vdcs::Fixed2D>(9002, "PDEG", pid, 4, pref));
auto r = vdcs::NewtonSolver::Solve(model, SolveOptions{});
ASSERT_FALSE(r.Ok());
EXPECT_EQ(r.Err().Code(), vdcs::ErrorCode::kInvalidInput);
// Err 语义 = 模型未被本次求解改动(精确退化参数逐位保持)
const vdcs::Entity* e = model.FindEntity(pid);
ASSERT_NE(e, nullptr);
std::vector<double> p(4);
e->WriteParams(p.data());
EXPECT_DOUBLE_EQ(p[2], 0.0);
EXPECT_DOUBLE_EQ(p[3], 0.0);
}
// ---- VDCS-004:硬锚定参数集(g715)-----------------------------------------
/// VDCS-004 例 B(验收口径):自由两点 A=(2,3)、B=(9,7),新建长度尺寸
/// |AB|=10,锚定 A(实体级)。期望(SW 最小移动语义):A 逐位不动,
/// B 沿初始方向 (7,4)/√65 单侧伸长到 (10.6824314212446, 7.9613893835690)。
/// 对照:无锚定时最小范数解两点对称移动(中点守恒,VDCS-001 用例)
TEST(NewtonTest, AnchoredSolveHoldsAnchorAndGrowsSingleSide) {
Model model;
auto a = model.AddPoint2D("P1", 2.0, 3.0);
auto b = model.AddPoint2D("P2", 9.0, 7.0);
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 10.0);
SolveOptions opts;
opts.anchors = {vdcs::AnchorParam{a.Value(), -1}};
auto r = model.Solve(opts);
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_EQ(r.Value().anchored_params, 2);
const auto* pa = static_cast<const Point2D*>(model.FindEntity(a.Value()));
const auto* pb = static_cast<const Point2D*>(model.FindEntity(b.Value()));
ASSERT_NE(pa, nullptr);
ASSERT_NE(pb, nullptr);
// 锚定不动点:逐位保持入口初值
EXPECT_DOUBLE_EQ(pa->X(), 2.0);
EXPECT_DOUBLE_EQ(pa->Y(), 3.0);
// 单侧生长:B = A + 10·(7,4)/√65(方向保持 = VDCS-001 旋转零空间
// 不变量 + 锚定平移零空间锁定的组合)
EXPECT_NEAR(pb->X(), 10.6824314212446, 1e-9);
EXPECT_NEAR(pb->Y(), 7.9613893835690, 1e-9);
EXPECT_NEAR(PointDistance(model, a.Value(), b.Value()), 10.0, 1e-9);
}
/// 锚定不占 DOF(VDCS-004 期望行为 1 的核心不变量):锚定前后
/// total_dof / removed_dof / mobility 逐项相同——锚定是解空间投影,
/// 不是约束(Fixed2D 钉扎会 +2 removed_dof,对照语义)
TEST(NewtonTest, AnchoredSolveDoesNotConsumeDof) {
auto build = []() {
Model m;
auto a = m.AddPoint2D("P1", 2.0, 3.0);
auto b = m.AddPoint2D("P2", 9.0, 7.0);
(void)m.AddDistance2D("D1", a.Value(), b.Value(), 10.0);
return std::pair{std::move(m), a.Value()};
};
auto [m_free, uid] = build();
auto r_free = m_free.Solve(SolveOptions{});
ASSERT_TRUE(r_free.Ok() && r_free.Value().converged);
EXPECT_EQ(r_free.Value().anchored_params, 0);
auto [m_anchor, uid2] = build();
SolveOptions opts;
opts.anchors = {vdcs::AnchorParam{uid2, -1}};
auto r_anchor = m_anchor.Solve(opts);
ASSERT_TRUE(r_anchor.Ok() && r_anchor.Value().converged);
EXPECT_EQ(r_free.Value().total_dof, r_anchor.Value().total_dof);
EXPECT_EQ(r_free.Value().removed_dof, r_anchor.Value().removed_dof);
EXPECT_EQ(r_free.Value().mobility, r_anchor.Value().mobility);
EXPECT_EQ(r_anchor.Value().anchored_params, 2);
}
/// 锚定冲突诚实报告:A 被 Fixed2D 钉在原点、|AB|=5,但 B 初始距 A
/// 仅 3 且被实体级锚定——约束强迫锚定参数移动,投影下残差方程不可
/// 满足 → 不收敛(不静默漂移),B 逐位保持锚定值
TEST(NewtonTest, AnchorConflictFailsHonestly) {
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 3.0, 0.0);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 5.0);
SolveOptions opts;
opts.max_iterations = 200;
opts.anchors = {vdcs::AnchorParam{b.Value(), -1}};
auto r = model.Solve(opts);
ASSERT_TRUE(r.Ok()); // 有报告(Result 成功)
EXPECT_FALSE(r.Value().converged); // 但诚实不收敛
EXPECT_GT(r.Value().residual_norm, 1e-6);
// 锚定参数逐位保持(无静默漂移)
const auto* pb = static_cast<const Point2D*>(model.FindEntity(b.Value()));
ASSERT_NE(pb, nullptr);
EXPECT_DOUBLE_EQ(pb->X(), 3.0);
EXPECT_DOUBLE_EQ(pb->Y(), 0.0);
}
/// VDCS-004 例 A(验收口径):焊接矩形(HV×4 + 四边距离)底边 8→12
/// 尺寸编辑,锚定底边起点 p1。期望:p1 不动、底边单侧生长、高度 4
/// 刚性保持(整体平移欠定模被锚定锁定,而非最小范数折中)
TEST(NewtonTest, AnchoredWeldedRectangleEditGrowsFromAnchor) {
Model model;
auto p1 = model.AddPoint2D("P1", 2.0, 2.0); // 锚定角
auto p2 = model.AddPoint2D("P2", 10.0, 2.0); // 底边终点(初值 8 宽)
auto p3 = model.AddPoint2D("P3", 10.0, 6.0);
auto p4 = model.AddPoint2D("P4", 2.0, 6.0);
(void)model.AddHorizontal2D("H1", p1.Value(), p2.Value());
(void)model.AddVertical2D("V1", p2.Value(), p3.Value());
(void)model.AddHorizontal2D("H2", p3.Value(), p4.Value());
(void)model.AddVertical2D("V2", p4.Value(), p1.Value());
// 底边已编辑为 12(初值 8);其余三边 = 焊接链刚性跟随尺寸
(void)model.AddDistance2D("DB", p1.Value(), p2.Value(), 12.0);
(void)model.AddDistance2D("DR", p2.Value(), p3.Value(), 4.0);
(void)model.AddDistance2D("DT", p3.Value(), p4.Value(), 12.0);
(void)model.AddDistance2D("DL", p4.Value(), p1.Value(), 4.0);
SolveOptions opts;
opts.anchors = {vdcs::AnchorParam{p1.Value(), -1}};
auto r = model.Solve(opts);
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
const auto* q1 = static_cast<const Point2D*>(model.FindEntity(p1.Value()));
const auto* q2 = static_cast<const Point2D*>(model.FindEntity(p2.Value()));
const auto* q3 = static_cast<const Point2D*>(model.FindEntity(p3.Value()));
const auto* q4 = static_cast<const Point2D*>(model.FindEntity(p4.Value()));
ASSERT_NE(q1, nullptr);
ASSERT_NE(q2, nullptr);
ASSERT_NE(q3, nullptr);
ASSERT_NE(q4, nullptr);
EXPECT_DOUBLE_EQ(q1->X(), 2.0);
EXPECT_DOUBLE_EQ(q1->Y(), 2.0);
EXPECT_NEAR(q2->X(), 14.0, 1e-8); // 底边单侧生长 8→12
EXPECT_NEAR(q2->Y(), 2.0, 1e-8);
EXPECT_NEAR(q3->X(), 14.0, 1e-8);
EXPECT_NEAR(q3->Y(), 6.0, 1e-8); // 高度 4 刚性保持
EXPECT_NEAR(q4->X(), 2.0, 1e-8);
EXPECT_NEAR(q4->Y(), 6.0, 1e-8);
}
/// 锚定语义跨数值引擎一致:SparseLm(正规方程 λ 对角技巧)与 DogLeg
///(GN/Cauchy 组合步锚定分量恒 0)在例 B 模型上与 Newton 同落点
TEST(NewtonTest, AnchoredSolveOnSparseLmAndDogLegPaths) {
auto build = []() {
Model m;
auto a = m.AddPoint2D("P1", 2.0, 3.0);
auto b = m.AddPoint2D("P2", 9.0, 7.0);
(void)m.AddDistance2D("D1", a.Value(), b.Value(), 10.0);
return std::pair{std::move(m), std::pair{a.Value(), b.Value()}};
};
SolveOptions opts;
{
auto [m, uids] = build();
opts.anchors = {vdcs::AnchorParam{uids.first, -1}};
auto r = vdcs::SparseLmSolver::Solve(m, opts);
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_EQ(r.Value().anchored_params, 2);
const auto* pa =
static_cast<const Point2D*>(m.FindEntity(uids.first));
const auto* pb =
static_cast<const Point2D*>(m.FindEntity(uids.second));
ASSERT_NE(pa, nullptr);
ASSERT_NE(pb, nullptr);
EXPECT_DOUBLE_EQ(pa->X(), 2.0);
EXPECT_DOUBLE_EQ(pa->Y(), 3.0);
EXPECT_NEAR(pb->X(), 10.6824314212446, 1e-9);
EXPECT_NEAR(pb->Y(), 7.9613893835690, 1e-9);
}
{
auto [m, uids] = build();
opts.anchors = {vdcs::AnchorParam{uids.first, -1}};
auto r = vdcs::DogLegSolver::Solve(m, opts);
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_EQ(r.Value().anchored_params, 2);
const auto* pa =
static_cast<const Point2D*>(m.FindEntity(uids.first));
const auto* pb =
static_cast<const Point2D*>(m.FindEntity(uids.second));
ASSERT_NE(pa, nullptr);
ASSERT_NE(pb, nullptr);
EXPECT_DOUBLE_EQ(pa->X(), 2.0);
EXPECT_DOUBLE_EQ(pa->Y(), 3.0);
EXPECT_NEAR(pb->X(), 10.6824314212446, 1e-9);
EXPECT_NEAR(pb->Y(), 7.9613893835690, 1e-9);
}
}
// ---- g717:min-norm 秩亏回退路径覆盖(g714 双模 + g716 LDLT)--------------
// newton_solver.cpp 宽窄两路径各有「秩亏回退 COD」分支,常规语料
//(行满秩)从不触达:
// · 宽系统(m<n):J 行线性相关 ⇒ JJᵀ 奇异 ⇒ LDLT 的 D 含零
// ⇒ gram_spd 假 ⇒ 回退 COD 伪逆(L165-167);
// · 窄系统(m≥n):qr.rank() < n ⇒ COD 取最小范数解(L174)。
// 构造秩亏最简手段 = 同对同值的重复距离约束(J 行逐位相同)。
/// 宽系统秩亏:三自由点(n=6)+ |AB|=10 重复两次(m=2,行相同 ⇒
/// rank=1 < m)。契约:COD 伪逆下重复约束按「平均」语义分摊到 A/B
///(中点守恒 = VDCS-001 平移零空间不变量在秩亏下的延续),无约束点
/// C 的雅可比列全零 ⇒ 最小范数步分量恒 0(逐位不动)。
TEST(NewtonTest, WideRankDeficientGramFallsBackToCod) {
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
auto b = model.AddPoint2D("P2", 5.0, 0.2);
auto c = model.AddPoint2D("P3", -3.0, 7.0);
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 10.0);
(void)model.AddDistance2D("D2", a.Value(), b.Value(), 10.0);
auto r = model.Solve(SolveOptions{});
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
const auto* pa = static_cast<const Point2D*>(model.FindEntity(a.Value()));
const auto* pb = static_cast<const Point2D*>(model.FindEntity(b.Value()));
const auto* pc = static_cast<const Point2D*>(model.FindEntity(c.Value()));
ASSERT_NE(pa, nullptr);
ASSERT_NE(pb, nullptr);
ASSERT_NE(pc, nullptr);
EXPECT_NEAR(PointDistance(model, a.Value(), b.Value()), 10.0, 1e-8);
// 中点守恒(重复约束 = 单约束的最小范数等价)
EXPECT_NEAR(pa->X() + pb->X(), 5.0, 1e-8);
EXPECT_NEAR(pa->Y() + pb->Y(), 0.2, 1e-8);
// 无约束点逐位不动(零雅可比列 ⇒ 最小范数零分量)
EXPECT_DOUBLE_EQ(pc->X(), -3.0);
EXPECT_DOUBLE_EQ(pc->Y(), 7.0);
}
/// 窄系统秩亏:A 钉扎 + B 自由 + |AB|=5 重复两次——投影后自由参数
/// 仅 B(n=2),两行相同的距离方程(m=2 ≥ n)⇒ rank=1 < n ⇒
/// qr.rank() != n_par ⇒ COD。契约:收敛到 |AB|=5、A 逐位保持钉扎值。
TEST(NewtonTest, NarrowRankDeficientQrFallsBackToCod) {
Model model;
auto a = model.AddPoint2D("P1", 1.0, 2.0);
auto b = model.AddPoint2D("P2", 4.0, 6.0);
(void)model.AddFixed2D("F1", a.Value());
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 5.0);
(void)model.AddDistance2D("D2", a.Value(), b.Value(), 5.0);
auto r = model.Solve(SolveOptions{});
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
const auto* pa = static_cast<const Point2D*>(model.FindEntity(a.Value()));
ASSERT_NE(pa, nullptr);
EXPECT_DOUBLE_EQ(pa->X(), 1.0);
EXPECT_DOUBLE_EQ(pa->Y(), 2.0);
EXPECT_NEAR(PointDistance(model, a.Value(), b.Value()), 5.0, 1e-8);
}
/// 参数级锚定(index ≥ 0):只锚 A 的 x 分量——A.x 逐位不动,其余
/// 参数照常参与最小范数分摊。ResolveAnchors 单下标展开路径
///(sparse_jacobian.cpp)此前仅实体级(index=-1)被测试触达
TEST(NewtonTest, ParameterLevelAnchorHoldsSingleParam) {
Model model;
auto a = model.AddPoint2D("P1", 2.0, 3.0);
auto b = model.AddPoint2D("P2", 9.0, 7.0);
(void)model.AddDistance2D("D1", a.Value(), b.Value(), 10.0);
SolveOptions opts;
opts.anchors = {vdcs::AnchorParam{a.Value(), 0}};
auto r = model.Solve(opts);
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_EQ(r.Value().anchored_params, 1);
const auto* pa = static_cast<const Point2D*>(model.FindEntity(a.Value()));
ASSERT_NE(pa, nullptr);
EXPECT_DOUBLE_EQ(pa->X(), 2.0); // 参数级锚定:仅 x 不动
EXPECT_NEAR(PointDistance(model, a.Value(), b.Value()), 10.0, 1e-9);
}
/// g718(VDCS-004 建议 3 末条顺带):原生 Segment2D 笼架「改一边长、
/// 另一端不动」——参数级锚定端点 1((seg,0)+(seg,1),实体级会连对端
/// 一起钉死与长度驱动冲突)+ Length2D 驱动 12:端点 1 逐位不动、
/// 单侧伸长到 (0,0)-(12,0)(最小范数保持初始方向),不占 DOF 不过
/// 约束失败(Fixed2D 钉扎对照见 VDCS-004 背景)
TEST(NewtonTest, AnchoredSegmentLengthEditGrowsFromAnchor) {
Model model;
auto s = model.AddSegment2D("S", 0.0, 0.0, 4.0, 0.0);
(void)model.AddLength2D("L", s.Value(), 12.0);
SolveOptions opts;
opts.anchors = {vdcs::AnchorParam{s.Value(), 0},
vdcs::AnchorParam{s.Value(), 1}};
auto r = model.Solve(opts);
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_EQ(r.Value().anchored_params, 2);
const auto* seg =
static_cast<const Segment2D*>(model.FindEntity(s.Value()));
ASSERT_NE(seg, nullptr);
EXPECT_DOUBLE_EQ(seg->X1(), 0.0); // 锚定端逐位不动
EXPECT_DOUBLE_EQ(seg->Y1(), 0.0);
EXPECT_NEAR(seg->X2(), 12.0, 1e-8); // 单侧伸长(方向保持)
EXPECT_NEAR(seg->Y2(), 0.0, 1e-8);
EXPECT_NEAR(seg->Length(), 12.0, 1e-8);
}
/// 锚定配置错误入口拦截(10-安全):未知实体 / 参数下标越界 →
/// kInvalidInput,模型未被改动
TEST(NewtonTest, AnchorsConfigErrorsRejected) {
{
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
(void)a;
SolveOptions opts;
opts.anchors = {vdcs::AnchorParam{999999, 0}};
auto r = model.Solve(opts);
ASSERT_FALSE(r.Ok());
EXPECT_EQ(r.Err().Code(), vdcs::ErrorCode::kInvalidInput);
}
{
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
SolveOptions opts;
opts.anchors = {vdcs::AnchorParam{a.Value(), 5}}; // Dof=2 越界
auto r = model.Solve(opts);
ASSERT_FALSE(r.Ok());
EXPECT_EQ(r.Err().Code(), vdcs::ErrorCode::kInvalidInput);
}
}
// ---- g719(VDCS-004 未做项 1):软锚定 weight ------------------------------
// 语义:weight ≥ 0 = 软锚定移动代价 w(LM 目标 ‖r‖²+Σw²Δ² 的折中项);
// 收敛判据与 residual_norm 只看约束残差(软锚是偏好不是约束——
// 驻点 w·|Δ*| = |强迫移动|/w 恒非零,混入判据永不收敛)。
// 驻点公式(共线 1D 精确;Segment (0,0)-(4,0) + Length 驱动 12,
// 初始残差 8):单向软锚端点1(w)时 r* = 8/(1+2/w²),端点1 泄漏
// −r*/w²、自由端吸收 r*;双向软锚(w1、w2)时 r* = 8/(1+1/w1²+1/w2²),
// 移动比 |d1|/|d2| = w2²/w1²(按代价反比分配)。
/// 大 w 软锚数值上逼近硬锚定:端点1 双参数 w=1e6 软锚——约束残差
/// 由自由端完全吸收(r→0 收敛),端点1 泄漏 ~8/w² ≈ 1e-11(对比
/// AnchoredSegmentLengthEditGrowsFromAnchor 硬锚版的逐位不动)
TEST(NewtonTest, SoftAnchorLargeWeightApproximatesHard) {
Model model;
auto s = model.AddSegment2D("S", 0.0, 0.0, 4.0, 0.0);
(void)model.AddLength2D("L", s.Value(), 12.0);
// GCC16 -Wnonnull 误报规避:initializer_list 赋值在 -O3 内联下
// 触发 vector.tcc _M_assign_aux 的 memmove null 假传播(PR115233
// 类),逐元素写入不走该路径
SolveOptions opts;
opts.anchors.resize(2);
opts.anchors[0] = vdcs::AnchorParam{s.Value(), 0, 1e6};
opts.anchors[1] = vdcs::AnchorParam{s.Value(), 1, 1e6};
auto r = model.Solve(opts);
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_EQ(r.Value().anchored_params, 2);
// converged ⇔ residual_norm < tolerance 自洽(软项不进报告口径)
EXPECT_LT(r.Value().residual_norm, 1e-10);
const auto* seg =
static_cast<const Segment2D*>(model.FindEntity(s.Value()));
ASSERT_NE(seg, nullptr);
// 端点1 泄漏上界 8/w² = 8e-12(步序泄漏总和同量级)
EXPECT_NEAR(seg->X1(), 0.0, 1e-9);
EXPECT_NEAR(seg->Y1(), 0.0, 1e-9);
// 自由端单侧伸长(方向保持 = 共线初始的最小代价路径)
EXPECT_NEAR(seg->X2(), 12.0, 1e-8);
EXPECT_NEAR(seg->Y2(), 0.0, 1e-9);
EXPECT_NEAR(seg->Length(), 12.0, 1e-8);
}
/// 双向软锚按代价反比分配移动(软锚定的核心价值场景):端点1 w=4、
/// 端点2 w=1——伸长 8 无自由侧可完全吸收 ⇒ 约束残差与锚代价真折中,
/// 驻点 r* = 8/(1+1/16+1) = 3.8788、X1 = −0.2424、X2 = 7.8788
///(长度 8.1212 ∈ (4,12))、移动比 |d1|/|d2| = 1/16 = w2²/w1²。
/// 约束未完全满足 → 诚实不收敛(宿主可读驻点几何做拖拽预览)
TEST(NewtonTest, SoftAnchorWeightsSplitMotionByInverseSquare) {
Model model;
auto s = model.AddSegment2D("S", 0.0, 0.0, 4.0, 0.0);
(void)model.AddLength2D("L", s.Value(), 12.0);
SolveOptions opts;
opts.max_iterations = 60;
opts.anchors.resize(4); // GCC16 -Wnonnull 误报规避(同上)
opts.anchors[0] = vdcs::AnchorParam{s.Value(), 0, 4.0};
opts.anchors[1] = vdcs::AnchorParam{s.Value(), 1, 4.0};
opts.anchors[2] = vdcs::AnchorParam{s.Value(), 2, 1.0};
opts.anchors[3] = vdcs::AnchorParam{s.Value(), 3, 1.0};
auto r = model.Solve(opts);
ASSERT_TRUE(r.Ok());
EXPECT_FALSE(r.Value().converged); // 软锚代价吃掉约束:诚实不收敛
EXPECT_EQ(r.Value().anchored_params, 4);
const auto* seg =
static_cast<const Segment2D*>(model.FindEntity(s.Value()));
ASSERT_NE(seg, nullptr);
// 停点在驻点邻域(LM 熔断时机留 ±5% 路径余量)
EXPECT_NEAR(seg->X1(), -0.2424, 0.05);
EXPECT_NEAR(seg->X2(), 7.8788, 0.1);
EXPECT_NEAR(seg->Y1(), 0.0, 1e-6); // 共线对称:y 驻点 0
EXPECT_NEAR(seg->Y2(), 0.0, 1e-6);
// 移动比按 w² 反比(软锚定分配语义的核心断言)
const double d1 = std::abs(seg->X1());
const double d2 = std::abs(seg->X2() - 4.0);
EXPECT_NEAR(d1 / d2, 1.0 / 16.0, 0.15 * (1.0 / 16.0));
// 折中落点:长度严格介于初值与驱动值之间
EXPECT_GT(seg->Length(), 4.5);
EXPECT_LT(seg->Length(), 11.5);
}
/// w = 0 = 零代价锚(无约束力,仅 anchored_params 诊断计数):
/// 解与无锚最小范数解一致(中点守恒对称分摊 (−4, 8))
TEST(NewtonTest, SoftAnchorZeroWeightIsFree) {
auto build = []() {
Model m;
auto s = m.AddSegment2D("S", 0.0, 0.0, 4.0, 0.0);
(void)m.AddLength2D("L", s.Value(), 12.0);
return std::pair{std::move(m), s.Value()};
};
auto [m_free, sid_free] = build();
auto r_free = m_free.Solve(SolveOptions{});
ASSERT_TRUE(r_free.Ok() && r_free.Value().converged);
EXPECT_EQ(r_free.Value().anchored_params, 0);
auto [m_zero, sid] = build();
SolveOptions opts;
opts.anchors.resize(2); // GCC16 -Wnonnull 误报规避(同上)
opts.anchors[0] = vdcs::AnchorParam{sid, 0, 0.0};
opts.anchors[1] = vdcs::AnchorParam{sid, 1, 0.0};
auto r = m_zero.Solve(opts);
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_EQ(r.Value().anchored_params, 2); // 零代价仍计数(诊断)
const auto* seg_zero =
static_cast<const Segment2D*>(m_zero.FindEntity(sid));
const auto* seg_free =
static_cast<const Segment2D*>(m_free.FindEntity(sid_free));
ASSERT_NE(seg_zero, nullptr);
ASSERT_NE(seg_free, nullptr);
// w=0 软行全零:解与无锚最小范数解逐参数一致
EXPECT_NEAR(seg_zero->X1(), seg_free->X1(), 1e-9);
EXPECT_NEAR(seg_zero->Y1(), seg_free->Y1(), 1e-9);
EXPECT_NEAR(seg_zero->X2(), seg_free->X2(), 1e-9);
EXPECT_NEAR(seg_zero->Y2(), seg_free->Y2(), 1e-9);
EXPECT_NEAR(seg_zero->Length(), 12.0, 1e-9);
}
/// 软锚定权重域校验(10-安全):哨兵外负值 / NaN / +Inf →
/// kInvalidInput 入口拦截(与未知实体/下标越界同口径)
TEST(NewtonTest, SoftAnchorConfigRejected) {
const double probes[3] = {-0.5,
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::infinity()};
for (const double w : probes) {
Model model;
auto a = model.AddPoint2D("P1", 0.0, 0.0);
(void)a;
std::vector<vdcs::AnchorParam> anchors;
anchors.push_back(vdcs::AnchorParam{a.Value(), -1, w});
SolveOptions opts;
opts.anchors = anchors;
auto r = model.Solve(opts);
ASSERT_FALSE(r.Ok()) << "weight=" << w;
EXPECT_EQ(r.Err().Code(), vdcs::ErrorCode::kInvalidInput);
}
// g721:软锚参数下标越界(ResolveSoftAnchors 独立分支——权重域
// 合法时硬锚翻译先行放行,越界只有软侧拦截)
{
Model model;
auto s = model.AddSegment2D("S", 0.0, 0.0, 4.0, 0.0);
(void)s;
std::vector<vdcs::AnchorParam> anchors;
anchors.push_back(vdcs::AnchorParam{s.Value(), 4, 10.0});
SolveOptions opts;
opts.anchors = anchors;
auto r = model.Solve(opts);
ASSERT_FALSE(r.Ok());
EXPECT_EQ(r.Err().Code(), vdcs::ErrorCode::kInvalidInput);
}
}
/// 软锚引用未知实体(g721 覆盖轮):权重域合法 → 硬锚翻译放行,
/// ResolveSoftAnchors 侧拦截(与硬锚未知实体同口径 kInvalidInput)
TEST(NewtonTest, SoftAnchorUnknownEntityRejected) {
Model model;
(void)model.AddPoint2D("P1", 0.0, 0.0);
std::vector<vdcs::AnchorParam> anchors;
anchors.push_back(vdcs::AnchorParam{9999, 0, 10.0});
SolveOptions opts;
opts.anchors = anchors;
auto r = model.Solve(opts);
ASSERT_FALSE(r.Ok());
EXPECT_EQ(r.Err().Code(), vdcs::ErrorCode::kInvalidInput);
}
/// 实体级软锚(index=-1,同一 w 展开全部参数,g721 覆盖轮):
/// {sid,-1,w} ≡ 逐参数 {sid,k,w}——双侧全锚时约束吃不满 → 诚实
/// 不收敛 + 停点近初值(g719 驻点公式:残差 8/(1+2/w²)、每端泄漏
/// ~8/w²——w=1e6 下近零);两种声明方式逐位同解
TEST(NewtonTest, SoftAnchorEntityLevelMatchesPerParam) {
auto solve_with = [](bool entity_level) {
Model m;
auto s = m.AddSegment2D("S", 0.0, 0.0, 4.0, 0.0);
(void)m.AddLength2D("L", s.Value(), 12.0);
SolveOptions opts;
if (entity_level) {
// GCC16 -Wnonnull 误报规避(同上:push_back + 拷贝赋值)
std::vector<vdcs::AnchorParam> anchors;
anchors.push_back(vdcs::AnchorParam{s.Value(), -1, 1e6});
opts.anchors = anchors;
} else {
// GCC16 -Wnonnull 误报规避(同上)
opts.anchors.resize(4);
for (int k = 0; k < 4; ++k) {
opts.anchors[static_cast<size_t>(k)] =
vdcs::AnchorParam{s.Value(), k, 1e6};
}
}
auto r = m.Solve(opts);
EXPECT_TRUE(r.Ok());
EXPECT_FALSE(r.Value().converged); // 双侧全锚:诚实不收敛
EXPECT_EQ(r.Value().anchored_params, 4); // 实体级 = 全参数计数
const auto* seg = static_cast<const Segment2D*>(m.FindEntity(s.Value()));
return std::pair{seg->X1(), seg->X2()};
};
const auto [x1a, x2a] = solve_with(true);
const auto [x1b, x2b] = solve_with(false);
EXPECT_NEAR(x1a, x1b, 1e-9);
EXPECT_NEAR(x2a, x2b, 1e-8);
// 泄漏上界(强迫 8 / w² = 8e-12 量级,断言 1e-9 熔断余量)
EXPECT_NEAR(x1a, 0.0, 1e-9);
EXPECT_NEAR(x2a, 4.0, 1e-9);
}
/// N145 尺寸权威(polish_stalled_soft_anchor=true):入口不可行(当前
/// 几何违反新标注——尺寸编辑常态)时含软锚增广求解停滞,抛光剥软锚
/// (硬锚保留)从增广终态续解纯约束,标注值精确满足(角度 = π/2、
/// 长度 = 12),几何钉回约束流形。默认关(g719 语义)同场景诚实不
/// 收敛——驻点折中几何供拖拽预览,逐位不受影响。
TEST(NewtonTest, PolishStalledSoftAnchorDimensionAuthoritative) {
constexpr double kPiHalf = 1.57079632679489661923;
auto build = []() {
Model m;
auto s = m.AddSegment2D("S", 0.0, 0.0, 4.0, 1.0); // |S|≈4.123 ≠ 12
(void)m.AddLength2D("L", s.Value(), 12.0);
auto l1 = m.AddLine2D("L1", 0.0, 0.0); // phi=0(水平)
auto l2 = m.AddLine2D("L2", 0.3, 1.0); // 与 L1 夹角 0.3 ≠ π/2
(void)m.AddFixed2D("F", l1.Value());
(void)m.AddAngle2D("A", l1.Value(), l2.Value(), kPiHalf);
return std::tuple{std::move(m), s.Value(), l1.Value(), l2.Value()};
};
auto anchored_opts = [](bool polish) {
SolveOptions opts;
opts.polish_stalled_soft_anchor = polish;
return opts;
};
// 尺寸权威:抛光后标注值精确满足、求解收敛
{
auto [m, sid, l1id, l2id] = build();
SolveOptions opts = anchored_opts(true);
opts.anchors.resize(3); // GCC16 -Wnonnull 误报规避(同上)
opts.anchors[0] = vdcs::AnchorParam{sid, -1, 1.0};
opts.anchors[1] = vdcs::AnchorParam{l1id, -1, 1.0};
opts.anchors[2] = vdcs::AnchorParam{l2id, -1, 1.0};
auto r = m.Solve(opts);
ASSERT_TRUE(r.Ok());
EXPECT_TRUE(r.Value().converged);
EXPECT_EQ(r.Value().anchored_params, 8); // 实体级软锚展开为逐参数(4+2+2)
const auto* seg = static_cast<const Segment2D*>(m.FindEntity(sid));
ASSERT_NE(seg, nullptr);
EXPECT_NEAR(seg->Length(), 12.0, 1e-9);
const auto* l1 = static_cast<const vdcs::Line2D*>(m.FindEntity(l1id));
const auto* l2 = static_cast<const vdcs::Line2D*>(m.FindEntity(l2id));
ASSERT_NE(l1, nullptr);
ASSERT_NE(l2, nullptr);
// 残差约定:phi_a − phi_b = theta(Angle2D::Residual),故 l1 − l2 = π/2
EXPECT_NEAR(l1->Phi() - l2->Phi(), kPiHalf, 1e-9);
}
// 默认关(g719 语义):软锚代价吃掉约束 → 诚实不收敛
{
auto [m, sid, l1id, l2id] = build();
SolveOptions opts = anchored_opts(false);
opts.anchors.resize(3);
opts.anchors[0] = vdcs::AnchorParam{sid, -1, 1.0};
opts.anchors[1] = vdcs::AnchorParam{l1id, -1, 1.0};
opts.anchors[2] = vdcs::AnchorParam{l2id, -1, 1.0};
auto r = m.Solve(opts);
ASSERT_TRUE(r.Ok());
EXPECT_FALSE(r.Value().converged);
EXPECT_EQ(r.Value().anchored_params, 8); // 同上:逐参数展开 4+2+2
const auto* seg = static_cast<const Segment2D*>(m.FindEntity(sid));
ASSERT_NE(seg, nullptr);
EXPECT_LT(seg->Length(), 11.5); // 折中驻点:未达标注值
}
}