fix: a merged segment lost the scan it was built from, so pointAt read past the end

The constructor mergeLines uses took only the fitted parameters and the ray
indices, leaving c_data_ and r_data_ default-constructed. pointAt indexes
r_data_.xs directly, so asking a merged segment where one of its own rays is
read past the end of an empty vector.

Two callers already do exactly that. The reflective-tape check walks the ray
span of a wing and reads each point; the arc matcher gathers the points of
adjacent segments to fit a circle to them. The arc matcher is how it surfaced -
it segfaulted, and AddressSanitizer put it on line.cpp:58.

It stayed hidden because mergeLines almost never fires with the default
parameters: a gap wide enough to split at is already wider than max_line_gap,
which is the gate merging has to pass. That is the merge gate working as
intended, and it is also why this could not be reached from a synthesized
scene - a scene-based test here would guard nothing, so the regression test
builds a merged Line directly instead.

The constructor now takes the scan data, like the other one. A merged segment
is still built from the same scan, so there was never a reason for it to lose
it, and a consumer cannot tell a merged segment from a fresh one - the
invariant has to be that every segment answers for every ray it supports.

pointAt also bounds-checks now and returns NaN for a ray it does not carry.
That is a second line of defence, not the fix: every caller already has to
reject non-finite points because a real scan contains them, so a future
constructor that forgets the data degrades instead of reading out of bounds.
This commit is contained in:
2026-08-26 20:09:06 +07:00
parent 524f3432e2
commit 8df353ded6
4 changed files with 108 additions and 9 deletions

View File

@@ -383,6 +383,85 @@ TEST(LineExtractor, ALineThatFailedToRefineIsStillReturned)
// 3. Đầu vào rác
// ================================================================================================
TEST(Line, AMergedSegmentStillKnowsItsRays)
{
// Hồi quy cho một lần đọc TRÀN MẢNG có thật. Constructor dùng khi gộp hai đoạn không mang theo dữ
// liệu scan, nên đoạn đã gộp có `r_data_` RỖNG — mà `pointAt` lại đọc thẳng vào đó. Bất kỳ ai hỏi
// đoạn đã gộp về toạ độ một tia đều đọc qua cuối một vector rỗng. Cổng dải phản quang làm đúng
// thế; bộ khớp cung tròn cũng vậy, và nó segfault — đó là cách lỗi này lộ ra.
//
// Người dùng đoạn thẳng KHÔNG phân biệt được đoạn đã gộp với đoạn nguyên, nên bất biến là: mọi
// đoạn đều trả lời được về mọi tia nó đỡ.
//
// Test đánh THẲNG vào constructor gộp chứ không đi vòng qua bộ trích đoạn, vì với tham số mặc
// định `mergeLines` gần như không bao giờ nổ: khe hở đủ rộng để TÁCH thì đã quá `max_line_gap` để
// GỘP. Chính vì thế lỗi này nằm im được lâu, và cũng chính vì thế một cảnh dựng sẵn không phải là
// chỗ canh đáng tin.
const ScanGeometry g = geometry(40, -0.4, 0.4);
const std::vector<double> ranges = wallScan(g, 2.0);
robot_line_extraction::CachedData c_data;
robot_line_extraction::RangeData r_data;
for (std::size_t i = 0; i < g.size; ++i)
{
const double bearing = bearingOf(g, i);
c_data.bearings.push_back(bearing);
c_data.cos_bearings.push_back(std::cos(bearing));
c_data.sin_bearings.push_back(std::sin(bearing));
c_data.indices.push_back(static_cast<unsigned int>(i));
r_data.ranges.push_back(ranges[i]);
r_data.xs.push_back(ranges[i] * std::cos(bearing));
r_data.ys.push_back(ranges[i] * std::sin(bearing));
}
// Đúng thứ `mergeLines` dựng: tham số đã khớp xong, tập tia của cả hai nửa nối lại.
std::vector<unsigned int> indices;
for (unsigned int i = 0; i < 40u; ++i)
{
indices.push_back(i);
}
const std::array<double, 4> cov{ { 1e-6, 0.0, 0.0, 1e-6 } };
const Line merged(c_data, r_data, /*angle=*/0.0, /*radius=*/2.0, cov,
{ { r_data.xs.front(), r_data.ys.front() } },
{ { r_data.xs.back(), r_data.ys.back() } }, indices);
ASSERT_TRUE(merged.hasCovariance());
for (const unsigned int ray : merged.getIndices())
{
const std::array<double, 2> p = merged.pointAt(ray);
ASSERT_TRUE(std::isfinite(p[0]) && std::isfinite(p[1])) << "tia " << ray;
// Và phải là toạ độ của CHÍNH tia đó, không phải một tia khác.
EXPECT_NEAR(p[0], r_data.xs[ray], 1e-12);
EXPECT_NEAR(p[1], r_data.ys[ray], 1e-12);
}
}
TEST(LineExtractor, EverySegmentTheExtractorReturnsCanReportItsOwnRays)
{
// Bất biến rộng, quét trên một cảnh nhiều mặt. Không ép được đường gộp từ đây (xem test trên),
// nên nó không thay thế test kia — nó canh mọi đường DẪN TỚI một đoạn, kể cả đường sau này mới có.
Rig rig;
ASSERT_TRUE(rig.ok_) << rig.error_;
const ScanGeometry g = geometry(360, -1.2, 1.2);
const std::vector<double> ranges = vPlateScan(g, 1.5, M_PI / 4.0, 0.30);
ASSERT_TRUE(rig.run(g, ranges)) << rig.error_;
ASSERT_FALSE(rig.lines_.empty());
for (const Line& line : rig.lines_)
{
for (const unsigned int ray : line.getIndices())
{
const std::array<double, 2> p = line.pointAt(ray);
ASSERT_TRUE(std::isfinite(p[0]) && std::isfinite(p[1])) << "tia " << ray;
const double bearing = bearingOf(g, ray);
EXPECT_NEAR(p[0], ranges[ray] * std::cos(bearing), 1e-9);
EXPECT_NEAR(p[1], ranges[ray] * std::sin(bearing), 1e-9);
}
}
}
TEST(LineExtractor, AnEmptySceneYieldsNoLineAndNoError)
{
Rig rig;