Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions apps/lidar_odometry_step_1/lidar_odometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& files,
const std::vector<std::vector<LidarGap>>& gaps_in_file,
const std::vector<double>& t_first,
const std::vector<double>& t_last)
{
std::vector<std::string> 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<std::string>& input_file_names,
LidarOdometryParams& params,
Expand Down Expand Up @@ -332,6 +381,7 @@ bool load_data(
L.n.assign(minSize, 0);
L.loaded.assign(minSize, 0);
L.done.assign(minSize, 0);
std::vector<std::vector<LidarGap>> gaps_in_file(minSize);
tbb::parallel_for(
size_t(1), // upstream clears the first file's cloud after loading: it is never used
minSize,
Expand All @@ -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;
Expand All @@ -358,6 +410,9 @@ bool load_data(
}

// --- Parallel load of LAZ files
std::vector<std::vector<LidarGap>> gaps_in_file(minSize);
std::vector<double> t_first(minSize, 0.0);
std::vector<double> t_last(minSize, 0.0);
tbb::parallel_for(
size_t(0),
minSize,
Expand All @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions apps/lidar_odometry_step_1/lidar_odometry_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,20 @@ std::vector<Point3Di> load_point_cloud(
return points;
}

std::vector<LidarGap> find_lidar_gaps(const std::vector<Point3Di>& points, double min_gap_s)
{
std::vector<LidarGap> 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<int, std::string> MLvxCalib::GetIdToSnMapping(const std::string& filename)
{
if (!std::filesystem::exists(filename))
Expand Down
13 changes: 13 additions & 0 deletions apps/lidar_odometry_step_1/lidar_odometry_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,19 @@ std::vector<Point3Di> load_point_cloud(
double filter_threshold_xy_outer,
const std::unordered_map<int, Eigen::Affine3d>& 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<LidarGap> find_lidar_gaps(const std::vector<Point3Di>& points, double min_gap_s);

bool save_poses(const std::string& file_name, const std::vector<Eigen::Affine3d>& m_poses, const std::vector<std::string>& filenames);

fs::path get_next_result_path(const std::string& working_directory);
Expand Down
6 changes: 4 additions & 2 deletions apps/lidar_odometry_step_1/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)

Expand Down
58 changes: 58 additions & 0 deletions apps/lidar_odometry_step_1/tests/test_lidar_gaps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <doctest.h>

#include "../lidar_odometry_utils.h"

namespace
{
// Points every `step` seconds from `start`, `count` of them
void appendPoints(std::vector<Point3Di>& 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<Point3Di> 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<Point3Di> 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<Point3Di> 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<Point3Di> points;
CHECK(find_lidar_gaps(points, 0.05).empty());
appendPoints(points, 1.0, 0.001, 1);
CHECK(find_lidar_gaps(points, 0.05).empty());
}
Loading