From 2293a98ec5c3ad4d342e9cef9d6e010ca344ae57 Mon Sep 17 00:00:00 2001 From: MichalPelka Date: Fri, 4 Dec 2026 02:12:05 +0100 Subject: [PATCH] camera_lidar_calibration: place dropped chunks with the session poses When several laz files are dropped at once and a poses file sits next to the first one (session_poses.mrp, else the folder's only *.mrp), each other cloud from that folder with a pose there is moved into the first cloud's frame: inverse(pose of first) * pose of added. The first cloud keeps its own coordinate system. A single dropped laz, the Load and Add buttons, and drops without a poses file load clouds as before. calib_core now compiles Session.cpp, which holds parseMRP. Co-Authored-By: Claude Opus 5.5 --- apps/camera_lidar_calibration/App.cpp | 101 +++++++++++++++++++++----- apps/camera_lidar_calibration/App.h | 4 +- calib_core/CMakeLists.txt | 1 + 3 files changed, 87 insertions(+), 19 deletions(-) diff --git a/apps/camera_lidar_calibration/App.cpp b/apps/camera_lidar_calibration/App.cpp index a56befe8..0a14ca01 100644 --- a/apps/camera_lidar_calibration/App.cpp +++ b/apps/camera_lidar_calibration/App.cpp @@ -3,6 +3,7 @@ #include "raymath.h" #include "rlImGui.h" #include +#include #include #include #include @@ -16,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -522,7 +524,7 @@ void AppState::loadCloud(const char* path) lidarId = GetLidarSerial(path); } -void AppState::addCloud(const char* path) +void AppState::addCloud(const char* path, const Eigen::Affine3f* T) { PointCloud extra; if (!extra.load(path)) @@ -530,6 +532,27 @@ void AppState::addCloud(const char* path) statusMsg = std::string("Failed to load: ") + path; return; } + if (T) + { + for (auto& p : extra.points) + { + const Eigen::Vector3f q = *T * Eigen::Vector3f(p.x, p.y, p.z); + p.x = q.x(); + p.y = q.y(); + p.z = q.z(); + } + extra.minX = extra.minY = extra.minZ = std::numeric_limits::max(); + extra.maxX = extra.maxY = extra.maxZ = std::numeric_limits::lowest(); + for (const auto& p : extra.points) + { + extra.minX = std::min(extra.minX, p.x); + extra.maxX = std::max(extra.maxX, p.x); + extra.minY = std::min(extra.minY, p.y); + extra.maxY = std::max(extra.maxY, p.y); + extra.minZ = std::min(extra.minZ, p.z); + extra.maxZ = std::max(extra.maxZ, p.z); + } + } // merge bounding box if (cloud.empty()) { @@ -1077,13 +1100,63 @@ static bool altHeld() return IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT); } +// The poses file lidar_odometry_step_1 writes next to its chunks: session_poses.mrp, else the +// folder's only *.mrp; empty when there is none. +static std::filesystem::path posesFileNextTo(const std::filesystem::path& dir) +{ + namespace fs = std::filesystem; + if (fs::is_regular_file(dir / "session_poses.mrp")) + return dir / "session_poses.mrp"; + fs::path found; + int count = 0; + std::error_code ec; + for (const auto& e : fs::directory_iterator(dir, ec)) + if (e.is_regular_file() && e.path().extension() == ".mrp") + { + found = e.path(); + ++count; + } + return count == 1 ? found : fs::path(); +} + +// The clouds of one drop: the first replaces the loaded cloud (like UI::actionOpenPointCloud), the +// rest merge into it (like UI::actionAddPointCloud). With several clouds and a poses file next to +// the first, each other cloud from its folder that has a pose there is moved into the first +// cloud's frame: inverse(pose of first) * pose of added. +static void loadDroppedClouds(AppState& state, const std::vector& paths) +{ + namespace fs = std::filesystem; + if (paths.empty()) + return; + const fs::path first(paths.front()); + const fs::path mrp = paths.size() > 1 ? posesFileNextTo(first.parent_path()) : fs::path(); + const std::map poses = mrp.empty() ? std::map{} : calib::parseMRP(mrp); + const auto firstPose = poses.find(first.stem().string()); + + state.loadCloud(paths.front().c_str()); + int placed = 0; + for (size_t i = 1; i < paths.size(); ++i) + { + const fs::path p(paths[i]); + const auto pose = poses.find(p.stem().string()); + if (firstPose != poses.end() && pose != poses.end() && p.parent_path() == first.parent_path()) + { + const Eigen::Affine3f T = firstPose->second.inverse() * pose->second; + state.addCloud(paths[i].c_str(), &T); + ++placed; + } + else + state.addCloud(paths[i].c_str()); + } + if (!mrp.empty()) + state.statusMsg = "Placed " + std::to_string(placed) + " of " + std::to_string(paths.size() - 1) + " clouds relative to " + + first.filename().string() + " with " + mrp.filename().string(); +} + // Loads one dropped file by extension -- the same extension -> action mapping as main.cpp's // preloadByExt(), but applied immediately (the window is already running) instead of queued for -// App::run()'s startup pass. `sawCloud` tracks whether a *.laz/*.las has already landed in this -// drop batch, so the first one replaces the cloud (like UI::actionOpenPointCloud) and later ones -// merge into it (like UI::actionAddPointCloud) -- mirroring how a multi-file drop of point clouds -// mixes "open" and "add" semantics for a natural one-shot drop. -static void handleDroppedFile(AppState& state, const std::string& path, bool& sawCloud) +// App::run()'s startup pass. Point clouds are collected into `clouds` for loadDroppedClouds(). +static void handleDroppedFile(AppState& state, const std::string& path, std::vector& clouds) { auto pos = path.rfind('.'); std::string e = pos == std::string::npos ? "" : path.substr(pos + 1); @@ -1092,15 +1165,7 @@ static void handleDroppedFile(AppState& state, const std::string& path, bool& sa if (e == "jpg" || e == "jpeg" || e == "png" || e == "bmp") state.loadImage(path.c_str()); else if (e == "laz" || e == "las") - { - if (sawCloud) - state.addCloud(path.c_str()); - else - { - state.loadCloud(path.c_str()); - sawCloud = true; - } - } + clouds.push_back(path); else if (e == "yml" || e == "yaml") state.loadIntrinsics(path.c_str()); else if (e == "json") @@ -1127,11 +1192,10 @@ void App::update() if (IsFileDropped()) { FilePathList dropped = LoadDroppedFiles(); - bool sawCloud = false; // Pairs go last, so the image and calibration dropped with them are what // loadPairs() rescales to and checks rectification against. // Folders go after the clouds dropped with them, whose first point time picks the image. - std::vector pairFiles, imageDirs; + std::vector pairFiles, imageDirs, clouds; for (unsigned int i = 0; i < dropped.count; i++) { if (std::filesystem::is_directory(dropped.paths[i])) @@ -1139,8 +1203,9 @@ void App::update() else if (AppState::isPairsFile(dropped.paths[i])) pairFiles.push_back(dropped.paths[i]); else - handleDroppedFile(state, dropped.paths[i], sawCloud); + handleDroppedFile(state, dropped.paths[i], clouds); } + loadDroppedClouds(state, clouds); for (const std::string& dir : imageDirs) state.loadImageClosestToCloudTime(dir.c_str(), AppState::CloudTimeRef::First); for (const std::string& path : pairFiles) diff --git a/apps/camera_lidar_calibration/App.h b/apps/camera_lidar_calibration/App.h index 98302788..48c67152 100644 --- a/apps/camera_lidar_calibration/App.h +++ b/apps/camera_lidar_calibration/App.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -163,7 +164,8 @@ struct AppState //! statusMsg, when the clouds or images carry no times. void loadImageClosestToCloudTime(const char* dir, CloudTimeRef ref = CloudTimeRef::Middle); void loadCloud(const char* path); // clear + load - void addCloud(const char* path); // merge into existing cloud + //! Merges `path` into the loaded cloud, its points moved by `T` when given. + void addCloud(const char* path, const Eigen::Affine3f* T = nullptr); void loadIntrinsics(const char* path); void loadCalibration(const char* path); // full JSON (intrinsics + extrinsics) void saveCalibration(const char* path); diff --git a/calib_core/CMakeLists.txt b/calib_core/CMakeLists.txt index e40d6e38..34ef1287 100644 --- a/calib_core/CMakeLists.txt +++ b/calib_core/CMakeLists.txt @@ -21,6 +21,7 @@ add_library(calib_core STATIC src/CameraInfoYaml.cpp src/PointCloud.cpp src/Trajectory.cpp + src/Session.cpp src/CliArgs.cpp src/CameraCalibrationSolver.cpp src/CameraCalibrationSolverCeres.cpp