From b4d42fdec0e60ca1d27ade128c256c4e19711d5a Mon Sep 17 00:00:00 2001 From: Sagi Amangeldi Date: Sun, 1 Nov 2026 16:34:51 +0900 Subject: [PATCH 1/2] Add find_lidar_gaps to detect gaps in LiDAR data --- .../lidar_odometry_utils.cpp | 14 +++++ .../lidar_odometry_utils.h | 13 +++++ .../tests/CMakeLists.txt | 6 +- .../tests/test_lidar_gaps.cpp | 58 +++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 apps/lidar_odometry_step_1/tests/test_lidar_gaps.cpp diff --git a/apps/lidar_odometry_step_1/lidar_odometry_utils.cpp b/apps/lidar_odometry_step_1/lidar_odometry_utils.cpp index 42359ddf7..32c7cd6d6 100644 --- a/apps/lidar_odometry_step_1/lidar_odometry_utils.cpp +++ b/apps/lidar_odometry_step_1/lidar_odometry_utils.cpp @@ -612,6 +612,20 @@ std::vector load_point_cloud( return points; } +std::vector find_lidar_gaps(const std::vector& points, double min_gap_s) +{ + std::vector gaps; + for (size_t i = 1; i < points.size(); i++) + { + const double length = points[i].timestamp - points[i - 1].timestamp; + if (length > min_gap_s) + { + gaps.push_back({ points[i - 1].timestamp, length }); + } + } + return gaps; +} + std::unordered_map MLvxCalib::GetIdToSnMapping(const std::string& filename) { if (!std::filesystem::exists(filename)) diff --git a/apps/lidar_odometry_step_1/lidar_odometry_utils.h b/apps/lidar_odometry_step_1/lidar_odometry_utils.h index 102cd2414..575c086fb 100644 --- a/apps/lidar_odometry_step_1/lidar_odometry_utils.h +++ b/apps/lidar_odometry_step_1/lidar_odometry_utils.h @@ -356,6 +356,19 @@ std::vector load_point_cloud( double filter_threshold_xy_outer, const std::unordered_map& calibrations); +//! A stretch of time without LiDAR points, in seconds. +struct LidarGap +{ + double start; + double length; +}; + +//! Finds gaps in LiDAR data, e.g. a recording damaged by a hardware/firmware fault or by copying. +//! @param points - points sorted by timestamp, as returned by @ref load_point_cloud after sorting +//! @param min_gap_s - smallest gap to report, in seconds +//! @return gaps longer than min_gap_s between consecutive points, in time order +std::vector find_lidar_gaps(const std::vector& points, double min_gap_s); + bool save_poses(const std::string& file_name, const std::vector& m_poses, const std::vector& filenames); fs::path get_next_result_path(const std::string& working_directory); diff --git a/apps/lidar_odometry_step_1/tests/CMakeLists.txt b/apps/lidar_odometry_step_1/tests/CMakeLists.txt index 549b9bec9..0bdddde76 100644 --- a/apps/lidar_odometry_step_1/tests/CMakeLists.txt +++ b/apps/lidar_odometry_step_1/tests/CMakeLists.txt @@ -2,8 +2,9 @@ cmake_minimum_required(VERSION 4.0.0) project(lidar_odometry_step_1_tests) -# Unit tests for the MLvxCalib namespace (lidar_odometry_utils.h/.cpp), which -# parses Livox extrinsic/IMU calibration files (.json/.mjc and .sn). Compiles +# Unit tests for lidar_odometry_utils.h/.cpp: the MLvxCalib namespace, which +# parses Livox extrinsic/IMU calibration files (.json/.mjc and .sn), and +# find_lidar_gaps(). Compiles # the real lidar_odometry_utils.cpp (no GUI code path, WITH_GUI left # undefined) instead of the whole lidar_odometry_step_1 app, so the include # dirs/link libraries below are the subset that TU actually needs: laszip and @@ -14,6 +15,7 @@ project(lidar_odometry_step_1_tests) # same as shared/tests. add_executable(lidar_odometry_step_1_tests test_mlvx_calib.cpp + test_lidar_gaps.cpp ../lidar_odometry_utils.cpp ) diff --git a/apps/lidar_odometry_step_1/tests/test_lidar_gaps.cpp b/apps/lidar_odometry_step_1/tests/test_lidar_gaps.cpp new file mode 100644 index 000000000..fa5e09564 --- /dev/null +++ b/apps/lidar_odometry_step_1/tests/test_lidar_gaps.cpp @@ -0,0 +1,58 @@ +#include + +#include "../lidar_odometry_utils.h" + +namespace +{ + // Points every `step` seconds from `start`, `count` of them + void appendPoints(std::vector& points, double start, double step, int count) + { + for (int i = 0; i < count; i++) + { + Point3Di p{}; + p.timestamp = start + i * step; + points.push_back(p); + } + } +} // namespace + +TEST_CASE("find_lidar_gaps: continuous data has no gaps") +{ + std::vector points; + appendPoints(points, 100.0, 0.001, 1000); + CHECK(find_lidar_gaps(points, 0.05).empty()); +} + +TEST_CASE("find_lidar_gaps: reports where a gap starts and how long it is") +{ + std::vector points; + appendPoints(points, 100.0, 0.001, 100); // last point at 100.099 + appendPoints(points, 100.3, 0.001, 100); + + const auto gaps = find_lidar_gaps(points, 0.05); + REQUIRE(gaps.size() == 1); + CHECK(gaps[0].start == doctest::Approx(100.099)); + CHECK(gaps[0].length == doctest::Approx(0.201)); +} + +TEST_CASE("find_lidar_gaps: only gaps longer than the threshold count") +{ + std::vector points; + appendPoints(points, 0.0, 0.001, 10); // last point at 0.009 + appendPoints(points, 0.04, 0.001, 10); // 31 ms gap: below the threshold + appendPoints(points, 0.2, 0.001, 10); // 151 ms gap + appendPoints(points, 0.5, 0.001, 10); // 291 ms gap + + const auto gaps = find_lidar_gaps(points, 0.05); + REQUIRE(gaps.size() == 2); + CHECK(gaps[0].length == doctest::Approx(0.151)); + CHECK(gaps[1].length == doctest::Approx(0.291)); +} + +TEST_CASE("find_lidar_gaps: empty or single-point input has no gaps") +{ + std::vector points; + CHECK(find_lidar_gaps(points, 0.05).empty()); + appendPoints(points, 1.0, 0.001, 1); + CHECK(find_lidar_gaps(points, 0.05).empty()); +} From 3c33e85c7f2421639e749d310249aa4def24b571 Mon Sep 17 00:00:00 2001 From: Sagi Amangeldi Date: Sun, 1 Nov 2026 16:41:11 +0900 Subject: [PATCH 2/2] Warn about LiDAR data gaps when loading step 1 data --- apps/lidar_odometry_step_1/lidar_odometry.cpp | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/apps/lidar_odometry_step_1/lidar_odometry.cpp b/apps/lidar_odometry_step_1/lidar_odometry.cpp index 26d024364..c47db2687 100644 --- a/apps/lidar_odometry_step_1/lidar_odometry.cpp +++ b/apps/lidar_odometry_step_1/lidar_odometry.cpp @@ -43,6 +43,55 @@ static void write_calibration_validation(const fs::path& wdp, const std::vector< } } +// Gaps in the LiDAR data longer than this are reported: odometry can fail on them, which then looks like a SLAM problem +static constexpr double kLidarGapWarningS = 0.05; + +// Warns about gaps inside each file and between consecutive files (t_first/t_last are 0 for a file without +// points). The first file is skipped, as its points are never used. +static void report_lidar_gaps( + const std::vector& files, + const std::vector>& gaps_in_file, + const std::vector& t_first, + const std::vector& t_last) +{ + std::vector lines; + auto add_line = [&lines](const LidarGap& gap, const std::string& where) + { + std::ostringstream line; + line << std::fixed << std::setprecision(0) << gap.length * 1000.0 << " ms at t=" << std::setprecision(3) << gap.start << " s (" + << where << ")"; + lines.push_back(line.str()); + }; + + double previous_last = 0.0; + std::string previous_name; + for (size_t i = 1; i < gaps_in_file.size(); i++) + { + if (t_first[i] == 0.0 && t_last[i] == 0.0) + continue; + + const std::string name = fs::path(files[i]).filename().string(); + if (!previous_name.empty() && t_first[i] - previous_last > kLidarGapWarningS) + add_line({ previous_last, t_first[i] - previous_last }, "between " + previous_name + " and " + name); + for (const auto& gap : gaps_in_file[i]) + add_line(gap, "in " + name); + + previous_last = t_last[i]; + previous_name = name; + } + + if (lines.empty()) + return; + + constexpr size_t kMaxLines = 10; + std::cerr << "WARNING: " << lines.size() << " gap(s) longer than " << kLidarGapWarningS * 1000.0 + << " ms in the LiDAR data, odometry may fail around them (damaged or incomplete recording?):\n"; + for (size_t i = 0; i < std::min(lines.size(), kMaxLines); i++) + std::cerr << "WARNING: " << lines[i] << "\n"; + if (lines.size() > kMaxLines) + std::cerr << "WARNING: ... and " << lines.size() - kMaxLines << " more\n"; +} + bool load_data( std::vector& input_file_names, LidarOdometryParams& params, @@ -332,6 +381,7 @@ bool load_data( L.n.assign(minSize, 0); L.loaded.assign(minSize, 0); L.done.assign(minSize, 0); + std::vector> gaps_in_file(minSize); tbb::parallel_for( size_t(1), // upstream clears the first file's cloud after loading: it is never used minSize, @@ -347,7 +397,9 @@ bool load_data( L.t_first[i] = data.front().timestamp; L.t_last[i] = data.back().timestamp; } + gaps_in_file[i] = find_lidar_gaps(data, kLidarGapWarningS); }); + report_lidar_gaps(L.files, gaps_in_file, L.t_first, L.t_last); if (params.save_calibration_validation && minSize > 0) // as the eager load: the first file, then dropped write_calibration_validation(wdp, L.load(0), params); size_t totalPoints = 0; @@ -358,6 +410,9 @@ bool load_data( } // --- Parallel load of LAZ files + std::vector> gaps_in_file(minSize); + std::vector t_first(minSize, 0.0); + std::vector t_last(minSize, 0.0); tbb::parallel_for( size_t(0), minSize, @@ -380,9 +435,17 @@ bool load_data( if ((i == 0) && params.save_calibration_validation) write_calibration_validation(wdp, data, params); + if (!data.empty()) + { + t_first[i] = data.front().timestamp; + t_last[i] = data.back().timestamp; + } + gaps_in_file[i] = find_lidar_gaps(data, kLidarGapWarningS); + // Store results pointsPerFile[i] = std::move(data); }); + report_lidar_gaps(laz_files, gaps_in_file, t_first, t_last); if (pointsPerFile.size() > 0) pointsPerFile.front().clear();