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:
@@ -42,10 +42,17 @@ public:
|
||||
Line(const CachedData& c_data, const RangeData& r_data, const LineExtractionParams& params,
|
||||
std::vector<unsigned int> indices);
|
||||
|
||||
/// @brief Build from already-known parameters. Used when two lines merge into one.
|
||||
Line(double angle, double radius, const std::array<double, 4>& covariance,
|
||||
const std::array<double, 2>& start, const std::array<double, 2>& end,
|
||||
const std::vector<unsigned int>& indices);
|
||||
/**
|
||||
* @brief Build from already-known parameters. Used when two lines merge into one.
|
||||
*
|
||||
* Takes the scan data as well, even though the fit is already done and none of it is needed to
|
||||
* describe the line. It is needed to describe the RAYS: a merged segment still answers @ref
|
||||
* pointAt for every ray it supports, and a consumer cannot tell a merged segment from a fresh
|
||||
* one. Leaving these empty is how @ref pointAt was made to read past the end of an empty vector.
|
||||
*/
|
||||
Line(const CachedData& c_data, const RangeData& r_data, double angle, double radius,
|
||||
const std::array<double, 4>& covariance, const std::array<double, 2>& start,
|
||||
const std::array<double, 2>& end, const std::vector<unsigned int>& indices);
|
||||
|
||||
double getAngle() const
|
||||
{
|
||||
@@ -102,6 +109,10 @@ public:
|
||||
*
|
||||
* @warning @p index is a RAY number, the same numbering @ref getIndices returns. It is not a
|
||||
* position within this segment's support.
|
||||
*
|
||||
* @return {NaN, NaN} if the segment does not carry the ray. Callers already have to reject
|
||||
* non-finite points because a scan contains them, so a guard that reads out of bounds
|
||||
* instead would be caught by nobody.
|
||||
*/
|
||||
std::array<double, 2> pointAt(unsigned int index) const;
|
||||
|
||||
|
||||
16
src/line.cpp
16
src/line.cpp
@@ -10,6 +10,7 @@
|
||||
#include <robot_line_extraction/line.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
namespace robot_line_extraction
|
||||
@@ -36,10 +37,12 @@ Line::Line(const CachedData& c_data, const RangeData& r_data, const LineExtracti
|
||||
{
|
||||
}
|
||||
|
||||
Line::Line(double angle, double radius, const std::array<double, 4>& covariance,
|
||||
const std::array<double, 2>& start, const std::array<double, 2>& end,
|
||||
const std::vector<unsigned int>& indices)
|
||||
: indices_(indices)
|
||||
Line::Line(const CachedData& c_data, const RangeData& r_data, double angle, double radius,
|
||||
const std::array<double, 4>& covariance, const std::array<double, 2>& start,
|
||||
const std::array<double, 2>& end, const std::vector<unsigned int>& indices)
|
||||
: c_data_(c_data)
|
||||
, r_data_(r_data)
|
||||
, indices_(indices)
|
||||
, angle_(angle)
|
||||
, radius_(radius)
|
||||
, start_(start)
|
||||
@@ -55,6 +58,11 @@ Line::Line(double angle, double radius, const std::array<double, 4>& covariance,
|
||||
|
||||
std::array<double, 2> Line::pointAt(unsigned int index) const
|
||||
{
|
||||
if (index >= r_data_.xs.size() || index >= r_data_.ys.size())
|
||||
{
|
||||
const double nan = std::numeric_limits<double>::quiet_NaN();
|
||||
return { { nan, nan } };
|
||||
}
|
||||
return { { r_data_.xs[index], r_data_.ys[index] } };
|
||||
}
|
||||
|
||||
|
||||
@@ -405,7 +405,8 @@ void LineExtractor::mergeLines()
|
||||
indices.insert(indices.end(), ind_1.begin(), ind_1.end());
|
||||
indices.insert(indices.end(), ind_2.begin(), ind_2.end());
|
||||
|
||||
Line merged_line(L_m[1], L_m[0], cov, lines_[i - 1].getStart(), lines_[i].getEnd(), indices);
|
||||
Line merged_line(c_data_, r_data_, L_m[1], L_m[0], cov, lines_[i - 1].getStart(),
|
||||
lines_[i].getEnd(), indices);
|
||||
merged_line.projectEndpoints();
|
||||
lines_[i] = merged_line;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user