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
101 changes: 83 additions & 18 deletions apps/camera_lidar_calibration/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "raymath.h"
#include "rlImGui.h"
#include <CalibCore/CameraCalibrationSolver.h>
#include <CalibCore/Session.h>
#include <HDMapping/HDMAPPING_ConfigureInfo.hpp>
#include <RaylibWidgets/CompassRuler.h>
#include <RaylibWidgets/PlatformKeys.h>
Expand All @@ -16,6 +17,7 @@
#include <filesystem>
#include <fstream>
#include <limits>
#include <map>
#include <nlohmann/json.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgcodecs.hpp>
Expand Down Expand Up @@ -522,14 +524,35 @@ 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))
{
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<float>::max();
extra.maxX = extra.maxY = extra.maxZ = std::numeric_limits<float>::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())
{
Expand Down Expand Up @@ -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<std::string>& 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<std::string, Eigen::Affine3f> poses = mrp.empty() ? std::map<std::string, Eigen::Affine3f>{} : 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<std::string>& clouds)
{
auto pos = path.rfind('.');
std::string e = pos == std::string::npos ? "" : path.substr(pos + 1);
Expand All @@ -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")
Expand All @@ -1127,20 +1192,20 @@ 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<std::string> pairFiles, imageDirs;
std::vector<std::string> pairFiles, imageDirs, clouds;
for (unsigned int i = 0; i < dropped.count; i++)
{
if (std::filesystem::is_directory(dropped.paths[i]))
imageDirs.push_back(dropped.paths[i]);
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)
Expand Down
4 changes: 3 additions & 1 deletion apps/camera_lidar_calibration/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <CalibCore/Camera.h>
#include <CalibCore/CameraCalibrationSolver.h>
#include <CalibCore/PointCloud.h>
#include <Eigen/Geometry>
#include <opencv2/core.hpp>
#include <string>
#include <vector>
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions calib_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading