fix: keep a segment whose least-squares refinement did not converge

extract() dropped any run whose weighted fit ran out of iterations. That was
too harsh: a failed refinement loses only the covariance, while the angle,
radius, endpoints and support all remain those of the endpoint fit and stay
usable.

Measured on a V dock plate at 2 m with 34 rays on the plate: both wings split
correctly with 17-18 points each and lengths correct to a few millimetres, yet
one of the two vanished on roughly half of the noise realizations because its
fit hit the 50-iteration cap. Everything downstream then saw one line where
there were two, and the corner disappeared. End-to-end V detection rate was
48%; keeping the segment takes it to 100% across 0-5 mm tape protrusion at
both 2 m and 3 m.

Line now reports hasCovariance(), false when the fit did not converge, and
mergeLines() refuses to merge such a segment - merging decides by comparing
line parameters against their own covariance, so a meaningless covariance must
not be allowed to make that decision.

TheLeastSquaresLoopIsBounded keeps guarding the iteration cap itself; the new
ALineThatFailedToRefineIsStillReturned guards against dropping the segment,
and fails if extract() goes back to discarding it.
This commit is contained in:
2026-08-26 15:55:32 +07:00
parent 1d38e77afa
commit d8f11d2df6
6 changed files with 107 additions and 16 deletions

View File

@@ -90,18 +90,19 @@ bool LineExtractor::extract(const ScanGeometry& geometry, const std::vector<doub
split(filtered_indices_);
filterLines();
// Refine every surviving run, dropping the ones that will not converge. A segment whose fit blew
// up carries a meaningless covariance, and the merge step decides by covariance.
std::vector<Line> refined;
refined.reserve(lines_.size());
// Refine every surviving run. A run that will not converge KEEPS its endpoint fit and stays in
// the output — it only loses its covariance.
//
// Dropping it was wrong and measurably so: on a two-wing dock plate at 2 m, 34 rays, both wings
// split correctly with 17 points each and lengths right to a few millimetres, yet one of the two
// vanished on roughly half the scans because its weighted fit ran out of iterations. Everything
// downstream then saw one line where there were two, and the corner disappeared. Angle, radius,
// endpoints and support are all still good on a failed refinement; only the covariance is not,
// and mergeLines is the only step that reads it.
for (Line& line : lines_)
{
if (line.leastSqFit())
{
refined.push_back(line);
}
line.leastSqFit();
}
lines_ = refined;
if (lines_.size() > 1)
{
@@ -354,6 +355,18 @@ void LineExtractor::mergeLines()
for (std::size_t i = 1; i < lines_.size(); ++i)
{
// Đoạn không hội tụ mang hiệp phương sai vô nghĩa, mà bước gộp quyết định BẰNG hiệp phương sai.
// Không gộp nó — giữ nguyên đoạn còn hơn gộp theo một con số không có cơ sở.
if (!lines_[i - 1].hasCovariance() || !lines_[i].hasCovariance())
{
merged_lines.push_back(lines_[i - 1]);
if (i == lines_.size() - 1)
{
merged_lines.push_back(lines_[i]);
}
continue;
}
const Eigen::Vector2d L_1(lines_[i - 1].getRadius(), lines_[i - 1].getAngle());
const Eigen::Vector2d L_2(lines_[i].getRadius(), lines_[i].getAngle());
const Eigen::Matrix2d P_1 = toMatrix(lines_[i - 1].getCovariance());
@@ -369,7 +382,12 @@ void LineExtractor::mergeLines()
const double endpoint_gap = std::hypot(lines_[i].getStart()[0] - lines_[i - 1].getEnd()[0],
lines_[i].getStart()[1] - lines_[i - 1].getEnd()[1]);
if (endpoint_gap <= params_.max_line_gap &&
// Góc giữa hai đường, quy về (-pi, pi]. Đây là cổng CHÍNH chống gộp qua một góc thật: chi-squared
// so tham số theo hiệp phương sai của chính chúng, mà trên một đoạn mươi tia thì hiệp phương sai
// đủ rộng để hai bức tường vuông góc "không phân biệt được", và góc biến mất.
const double angle_gap = std::fabs(piToPi(lines_[i].getAngle() - lines_[i - 1].getAngle()));
if (angle_gap <= params_.merge_max_angle && endpoint_gap <= params_.max_line_gap &&
chiSquared(L_1 - L_2, P_1, P_2) < kMergeChiSquaredThresh)
{
const Eigen::Matrix2d P_m = (P_1.inverse() + P_2.inverse()).inverse();