diff --git a/include/robot_line_extraction/line.h b/include/robot_line_extraction/line.h index 7aab599..fcdb6c2 100644 --- a/include/robot_line_extraction/line.h +++ b/include/robot_line_extraction/line.h @@ -42,10 +42,17 @@ public: Line(const CachedData& c_data, const RangeData& r_data, const LineExtractionParams& params, std::vector indices); - /// @brief Build from already-known parameters. Used when two lines merge into one. - Line(double angle, double radius, const std::array& covariance, - const std::array& start, const std::array& end, - const std::vector& 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& covariance, const std::array& start, + const std::array& end, const std::vector& 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 pointAt(unsigned int index) const; diff --git a/src/line.cpp b/src/line.cpp index 893e815..be74c06 100644 --- a/src/line.cpp +++ b/src/line.cpp @@ -10,6 +10,7 @@ #include #include +#include #include 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& covariance, - const std::array& start, const std::array& end, - const std::vector& indices) - : indices_(indices) +Line::Line(const CachedData& c_data, const RangeData& r_data, double angle, double radius, + const std::array& covariance, const std::array& start, + const std::array& end, const std::vector& 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& covariance, std::array Line::pointAt(unsigned int index) const { + if (index >= r_data_.xs.size() || index >= r_data_.ys.size()) + { + const double nan = std::numeric_limits::quiet_NaN(); + return { { nan, nan } }; + } return { { r_data_.xs[index], r_data_.ys[index] } }; } diff --git a/src/line_extractor.cpp b/src/line_extractor.cpp index 92c583a..cdc780d 100644 --- a/src/line_extractor.cpp +++ b/src/line_extractor.cpp @@ -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; } diff --git a/test/line_extraction_test.cpp b/test/line_extraction_test.cpp index 8934278..53873e0 100644 --- a/test/line_extraction_test.cpp +++ b/test/line_extraction_test.cpp @@ -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 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(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 indices; + for (unsigned int i = 0; i < 40u; ++i) + { + indices.push_back(i); + } + const std::array 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 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 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 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;