Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5872502
Camera-lidar calibration: Lock rotation, solving translation only
michalpelka Nov 13, 2026
c218e22
Camera-lidar calibration: lidar and camera models in the 3D view
michalpelka Nov 13, 2026
4bb1c47
Camera-lidar calibration: Shift+Alt+click picks LiDAR points in the i…
michalpelka Nov 13, 2026
7904424
Camera-lidar calibration: choose a PCB camera body for the camera model
michalpelka Nov 13, 2026
795c11e
raylib_widgets: timeline widget, moved out of the trajectory viewer
michalpelka Nov 13, 2026
2c3b4ee
Raw data viewer: playback bar along the bottom of the window
michalpelka Nov 13, 2026
cfcf2d0
Add camera_lidar_depth_images: lidar depth and intensity images per f…
michalpelka Nov 13, 2026
0957592
priors
michalpelka Nov 16, 2026
d994615
Camera-lidar calibration: Focus on sensors (F) in the 3D view
michalpelka Nov 16, 2026
c46a498
Camera-lidar calibration: save and load picked pairs
michalpelka Nov 16, 2026
ce3ab07
Camera-lidar calibration: resizable Controls panel
michalpelka Nov 16, 2026
67cbf64
Camera-lidar calibration: refine Mei intrinsics with the pair solve
michalpelka Nov 16, 2026
5be9cf1
calib_core: LAZ point times in nanoseconds
michalpelka Nov 16, 2026
e79b2c7
Camera-lidar calibration: --closest_to_laz picks the image nearest th…
michalpelka Nov 16, 2026
3350159
raylib_widgets: timeline bands and Gantt-style lanes
michalpelka Nov 16, 2026
12f659f
Step 1: load_data can report each input file's time span
michalpelka Nov 16, 2026
b44cc0e
Raw data viewer: motionless segments and file lanes on the timeline
michalpelka Nov 16, 2026
f64e22e
Camera-lidar calibration: mark the pair whose Remove button is hovered
michalpelka Nov 16, 2026
f93b9a8
Camera-lidar calibration: --pairs file, Ctrl+S saves the pairs to it
michalpelka Nov 16, 2026
5c4de0a
Camera-lidar calibration: own ID scope for the refine checkboxes
michalpelka Nov 16, 2026
246a3a5
Camera-lidar calibration: colour points by intensity by default
michalpelka Nov 16, 2026
3ce2cbb
Adjust code
michalpelka Nov 17, 2026
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
374 changes: 348 additions & 26 deletions apps/camera_lidar_calibration/App.cpp

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions apps/camera_lidar_calibration/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "UI.h"
#include "raylib.h"
#include <CalibCore/Camera.h>
#include <CalibCore/CameraCalibrationSolver.h>
#include <CalibCore/PointCloud.h>
#include <opencv2/core.hpp>
#include <string>
Expand Down Expand Up @@ -58,6 +59,14 @@ struct AppState
// ── visualization ─────────────────────────────────────────────────────────
VisualizationParams vizParams;
bool showCompassRuler = true;
//! Simplified real-size sensor models in the 3D view: a Mid-360 at the
//! LiDAR origin, cameraBody at the camera pose.
bool showLidarModel = true;
bool showCameraModel = true;
CameraBody cameraBody = CameraBody::Insta360X5;

//! Width of the Controls panel on the right, px; dragging its left edge changes it.
float panelW = 340.f;

// ── 3D camera ─────────────────────────────────────────────────────────────
OrbitCamera orbit;
Expand All @@ -82,22 +91,71 @@ struct AppState
// Row selected in the Correspondences panel list (-1 = none); drawn as
// a large cross in both the 3D view and the image view.
int selectedPairIndex = -1;
//! Pair whose Remove button is hovered (-1 = none), marked in red in both views
//! so it is clear which pair the click removes. Set by UI::draw each frame.
int hoveredRemoveIndex = -1;
// When set, "Solve Extrinsics from Pairs" refines orientation only and
// leaves tx/ty/tz exactly as they are -- for when the camera's position
// relative to the LiDAR is already known (e.g. measured by hand) and
// only orientation needs calibrating from the picked pairs. Also
// disables the tx/ty/tz drag sliders in the Extrinsics panel so they
// can't be nudged by accident while locked.
bool lockTranslation = false;
//! Rotation counterpart of lockTranslation: the solve refines tx/ty/tz
//! only and the om/fi/ka sliders are disabled.
bool lockRotation = false;
//! Soft constraints for "Solve Extrinsics from Pairs": each enabled one
//! adds an observation equation pulling that parameter towards its
//! target, e.g. to keep the camera height (tz) from drifting when the
//! pairs barely constrain it. Indexed as calib::kPriorTx..kPriorKa.
calib::ExtrinsicsPriors priors = defaultPriors();
//! Mei intrinsics "Solve Extrinsics from Pairs" refines too; ignored for
//! other models.
calib::MeiIntrinsicsRefinement refineIntrinsics;
//! @return the intrinsics the solve refines, e.g. "fx, fy, xi"; empty when none
std::string refinedIntrinsicsLabel() const;

/**
* @brief Priors all disabled, with a sigma of 1 cm for tx/ty/tz and
* 0.5 deg for om/fi/ka.
* @return the default priors
*/
static calib::ExtrinsicsPriors defaultPriors();

void setPendingImagePoint(float u, float v); // completes the pair if a cloud point is already pending
void setPendingCloudPoint(float px, float py, float pz); // completes the pair if an image point is already pending
void clearPending(); // discards the in-progress pending pick, if any
//! Makes the LiDAR point drawn at pixel (u, v) of the Image View's
//! intensity image (Shift+Alt) the pending cloud point: the nearest one
//! whose splat covers the pixel, else the closest within a few pixels.
//! @return false, with the reason in statusMsg, when there is none
bool pickLidarPointAtPixel(float u, float v);
//! `intrinsics` as the displayed image needs them: distortion zeroed when
//! the image is rectified.
Intrinsics displayIntrinsics() const;
void removePair(int index);
bool solvePairs(); // solves extrinsics from `pairs`, updates statusMsg
//! Writes `pairs` to a JSON file, with the size and rectification of the
//! image their u/v were picked in.
void savePairs(const char* path);
//! Replaces `pairs` with those in a savePairs() file, rescaling u/v when the
//! loaded image's size differs from the file's.
void loadPairs(const char* path);
//! @return whether `path` is a savePairs() file, i.e. a .json with a "pairs" array
static bool isPairsFile(const char* path);
//! File Ctrl+S saves the pairs to: --pairs, else the last pairs file loaded or
//! saved; empty = ask for one.
std::string pairsPath;

// ── operations ────────────────────────────────────────────────────────────
//! Points the 3D view at the LiDAR origin and the camera pose, close enough
//! to see the sensor models, keeping the current viewing angle.
void focusOnSensors();
void loadImage(const char* path);
//! Loads the image in `dir` taken closest to the middle of the loaded
//! clouds' point times (calib::closestImage); leaves the image as it is,
//! saying why in statusMsg, when the clouds or images carry no times.
void loadImageClosestToCloudTime(const char* dir);
void loadCloud(const char* path); // clear + load
void addCloud(const char* path); // merge into existing cloud
void loadIntrinsics(const char* path);
Expand Down Expand Up @@ -134,6 +192,18 @@ class App
{
pendingCalibration = path;
}
//! After the clouds load, replace the image with the one in `dir` taken
//! closest to their time (AppState::loadImageClosestToCloudTime).
void preloadImageClosestToClouds(const char* dir)
{
pendingImageDirByTime = dir;
}
//! Picked pairs file: loaded after everything else, as from a drop, when it
//! exists; Ctrl+S saves the pairs to it either way.
void preloadPairs(const char* path)
{
pendingPairs = path;
}

void run();

Expand All @@ -143,6 +213,8 @@ class App
std::vector<std::string> pendingClouds;
std::string pendingIntrinsics;
std::string pendingCalibration;
std::string pendingImageDirByTime;
std::string pendingPairs;

void update();
void draw();
Expand Down
146 changes: 140 additions & 6 deletions apps/camera_lidar_calibration/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,20 @@ void Renderer::renderImageOverlay(
const Extrinsics& E,
bool applyDistortion,
const VisualizationParams& vp,
bool showOverlay)
ImageOverlay overlay)
{
if (!imageTexValid)
return;

BeginTextureMode(imageTex);
ClearBackground(BLACK);

DrawTexturePro(
img, Rectangle{ 0, 0, (float)imgW, (float)imgH }, Rectangle{ 0, 0, (float)texW, (float)texH }, Vector2{ 0, 0 }, 0.f, WHITE);
const bool intensityImage = overlay == ImageOverlay::LidarIntensity;
if (!intensityImage)
DrawTexturePro(
img, Rectangle{ 0, 0, (float)imgW, (float)imgH }, Rectangle{ 0, 0, (float)texW, (float)texH }, Vector2{ 0, 0 }, 0.f, WHITE);

if (showOverlay && cloudCount > 0 && projShaderValid)
if (overlay != ImageOverlay::None && cloudCount > 0 && projShaderValid)
{
rlDrawRenderBatchActive(); // flush the image quad before raw GL draw

Expand Down Expand Up @@ -216,6 +218,12 @@ void Renderer::renderImageOverlay(
}
}
float depthRange[2] = { vp.depthMin, vp.depthMax };
// The intensity image is the points alone: opaque, coloured by
// intensity whatever the colour mode, nearest point on top.
const int colorMode = intensityImage ? 1 : vp.colorMode;
const float opacity = intensityImage ? 1.f : vp.opacity;
if (intensityImage)
rlEnableDepthTest();

rlEnableShader(projShader.id);
rlSetUniformMatrix(locPrjXform, xform);
Expand All @@ -228,18 +236,27 @@ void Renderer::renderImageOverlay(
rlSetUniform(locPrjXi, &xiVal, RL_SHADER_UNIFORM_FLOAT, 1);
rlSetUniform(locPrjThetaMax, &thetaMax, RL_SHADER_UNIFORM_FLOAT, 1);
rlSetUniform(locPrjDepthRange, depthRange, RL_SHADER_UNIFORM_VEC2, 1);
rlSetUniform(locPrjOpacity, &vp.opacity, RL_SHADER_UNIFORM_FLOAT, 1);
rlSetUniform(locPrjOpacity, &opacity, RL_SHADER_UNIFORM_FLOAT, 1);
rlSetUniform(locPrjPointSize, &vp.pointSize, RL_SHADER_UNIFORM_FLOAT, 1);
rlSetUniform(locPrjColorMode, &vp.colorMode, RL_SHADER_UNIFORM_INT, 1);
rlSetUniform(locPrjColorMode, &colorMode, RL_SHADER_UNIFORM_INT, 1);
rlSetUniform(locPrjDecim, &vp.drawDecim, RL_SHADER_UNIFORM_INT, 1);

raylib_widgets::drawPointBufferParts(cloudParts);
rlDisableShader();
if (intensityImage)
rlDisableDepthTest();
}

EndTextureMode();
}

bool Renderer::isPointDrawn(size_t index, int drawDecim)
{
// Mirrors the shaders' gl_VertexID % drawDecim, and gl_VertexID restarts
// in every buffer part.
return drawDecim <= 1 || (index % raylib_widgets::kMaxVerticesPerPart) % static_cast<size_t>(drawDecim) == 0;
}

void Renderer::draw3DCloud(
const PointCloud& cloud,
const VisualizationParams& vp,
Expand Down Expand Up @@ -377,3 +394,120 @@ void Renderer::drawAxes(float len)
DrawLine3D({ 0, 0, 0 }, { 0, len, 0 }, GREEN); // Y (= LiDAR Z = up)
DrawLine3D({ 0, 0, 0 }, { 0, 0, -len }, BLUE); // Z (= LiDAR Y)
}

// ── Sensor models ─────────────────────────────────────────────────────────────
// Dimensions in millimetres, read off the Livox Mid-360 and Insta360 X5 CAD
// models.
namespace
{
//! Pushes a local frame posed at (R, t) in the LiDAR frame onto the
//! matrix stack, with local lengths in millimetres. Pair with
//! rlPopMatrix().
//! @param R local axes in the LiDAR frame
//! @param t local origin in the LiDAR frame, metres
void pushPoseMm(const Eigen::Matrix3f& R, const Eigen::Vector3f& t)
{
Eigen::Matrix3f lidarToRaylib; // (x, y, z) -> (x, z, -y)
lidarToRaylib << 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, -1.f, 0.f;
const Eigen::Matrix3f M = lidarToRaylib * R * 0.001f;
const Eigen::Vector3f T = lidarToRaylib * t;
const Matrix m = { M(0, 0), M(0, 1), M(0, 2), T(0), M(1, 0), M(1, 1), M(1, 2), T(1),
M(2, 0), M(2, 1), M(2, 2), T(2), 0.f, 0.f, 0.f, 1.f };
rlPushMatrix();
rlMultMatrixf(MatrixToFloat(m));
}

void box(Vector3 lo, Vector3 hi, Color fill, Color edge)
{
const Vector3 centre = Vector3Scale(Vector3Add(lo, hi), 0.5f);
const Vector3 size = Vector3Subtract(hi, lo);
DrawCubeV(centre, size, fill);
DrawCubeWiresV(centre, size, edge);
}

void circleZ(float z, float r, Color color)
{
constexpr int kSegments = 32;
for (int i = 0; i < kSegments; ++i)
{
const float a0 = 2.f * PI * i / kSegments, a1 = 2.f * PI * (i + 1) / kSegments;
DrawLine3D({ r * cosf(a0), r * sinf(a0), z }, { r * cosf(a1), r * sinf(a1), z }, color);
}
}

//! Solid cone frustum along local z, outlined at both ends.
void coneZ(float z0, float z1, float r0, float r1, Color fill, Color edge)
{
DrawCylinderEx({ 0.f, 0.f, z0 }, { 0.f, 0.f, z1 }, r0, r1, 32, fill);
circleZ(z0, r0, edge);
circleZ(z1, r1, edge);
}

//! In the optical frame: the X5's optical centre sits midway between its
//! two lenses, with the body running down the image.
void drawInsta360X5()
{
const Color body = { 70, 70, 75, 255 }, bodyEdge = { 150, 150, 155, 255 };
const Color glass = { 25, 25, 30, 255 };

box({ -23.f, -21.5f, -12.5f }, { 23.f, 103.f, 12.5f }, body, bodyEdge);
for (const float side : { 1.f, -1.f })
{
coneZ(side * 12.5f, side * 15.f, 17.f, 17.f, body, side > 0.f ? YELLOW : bodyEdge);
DrawSphereEx({ 0.f, 0.f, side * 8.f }, 12.5f, 12, 24, glass);
}
}

//! In the optical frame: a Raspberry Pi format camera board (IMX519-style
//! autofocus module). The sensor sits ~4.3 mm (the lens' focal length)
//! behind the optical centre; the lens is 9.5 mm below the board's top
//! edge, the ribbon connector on the back at the bottom.
void drawPcbCamera()
{
const Color pcb = { 20, 110, 60, 255 }, pcbEdge = { 140, 200, 160, 255 };
const Color module = { 35, 35, 40, 255 }, moduleEdge = { 130, 130, 135, 255 };
const Color pad = { 200, 170, 80, 255 }, connector = { 60, 60, 60, 255 };
const Color glass = { 15, 15, 20, 255 };
constexpr float kSensorZ = -4.3f, kPcbT = 1.f;

box({ -12.5f, -9.5f, kSensorZ - kPcbT }, { 12.5f, 14.5f, kSensorZ }, pcb, pcbEdge);
for (const float x : { -10.5f, 10.5f })
for (const float y : { -7.5f, 5.f })
DrawCylinderEx({ x, y, kSensorZ - kPcbT - 0.05f }, { x, y, kSensorZ + 0.05f }, 1.2f, 1.2f, 12, pad);
box({ -10.5f, 10.f, kSensorZ - kPcbT - 3.f }, { 10.5f, 14.f, kSensorZ - kPcbT }, connector, moduleEdge);
box({ -4.25f, -4.25f, kSensorZ }, { 4.25f, 4.25f, 1.5f }, module, moduleEdge);
coneZ(1.5f, 2.2f, 2.5f, 2.5f, glass, YELLOW);
}
} // namespace

void Renderer::drawLidarModel()
{
const Color body = { 150, 150, 155, 255 }, bodyEdge = { 70, 70, 75, 255 };
const Color connector = { 95, 95, 100, 255 };
const Color window = { 40, 40, 45, 255 }, windowEdge = { 120, 120, 125, 255 };

pushPoseMm(Eigen::Matrix3f::Identity(), Eigen::Vector3f::Zero());
box({ -32.5f, -32.5f, -26.f }, { 32.5f, 32.5f, 1.f }, body, bodyEdge);
box({ -40.f, -10.f, -21.f }, { -32.5f, 10.f, -4.f }, connector, bodyEdge);
coneZ(1.f, 4.f, 31.f, 31.f, body, bodyEdge);
coneZ(4.f, 14.f, 25.5f, 25.5f, window, windowEdge);
coneZ(14.f, 34.f, 25.5f, 15.f, window, windowEdge);
rlPopMatrix();
}

void Renderer::drawCameraModel(const Extrinsics& E, CameraBody body)
{
// Local frame = the optical frame (x right, y down, z forward), origin at
// the optical centre.
pushPoseMm(omFiKaToMat3(E.om, E.fi, E.ka), Eigen::Vector3f(E.tx, E.ty, E.tz));
switch (body)
{
case CameraBody::Insta360X5:
drawInsta360X5();
break;
case CameraBody::PcbCamera:
drawPcbCamera();
break;
}
rlPopMatrix();
}
34 changes: 30 additions & 4 deletions apps/camera_lidar_calibration/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ struct VisualizationParams
float depthMin = 0.f;
float depthMax = 50.f;
float opacity = 1.f;
int colorMode = 0; // 0=depth(jet), 1=intensity, 2=height(z), 3=Camera RGB
int colorMode = 1; // 0=depth(jet), 1=intensity (the default), 2=height(z), 3=Camera RGB
// Draw only every Nth point (GPU-side, like camera_lidar_trajectory_viewer's
// "Draw decimation") -- 1 = draw all points.
int drawDecim = 1;
};

Color jetColor(float t); // t in [0,1]

//! Camera hardware Renderer::drawCameraModel draws at the camera pose.
enum class CameraBody
{
Insta360X5, //!< dual-fisheye body, optical centre midway between its lenses
PcbCamera, //!< bare board camera in the Raspberry Pi camera format: 25 x 24 mm board, lens module, connector at the bottom
};

//! What Renderer::renderImageOverlay draws into the Image View.
enum class ImageOverlay
{
Points, //!< the photo, projected points on top
None, //!< the photo alone, for picking an image pixel
LidarIntensity, //!< no photo: the projected points coloured by intensity, nearest on top, for picking a LiDAR point
};

class Renderer
{
public:
Expand All @@ -42,8 +57,6 @@ class Renderer

// Render image + GPU-projected point overlay into imageTex.
// If the displayed image is rectified, pass applyDistortion=false.
// showOverlay=false draws just the plain image (e.g. while picking, so
// projected points don't obscure the pixel the user is aiming for).
void renderImageOverlay(
const Texture2D& img,
int imgW,
Expand All @@ -52,7 +65,11 @@ class Renderer
const Extrinsics& E,
bool applyDistortion,
const VisualizationParams& vp,
bool showOverlay = true);
ImageOverlay overlay = ImageOverlay::Points);

//! Whether the GPU shaders draw cloud point `index` at "Draw decimation"
//! `drawDecim`, so a CPU pick can skip exactly the points not shown.
static bool isPointDrawn(size_t index, int drawDecim);

// Draw 3D point cloud into current BeginMode3D context (GPU shader path).
// For colorMode 3 (camera RGB) pass the displayed image texture and the
Expand All @@ -73,6 +90,15 @@ class Renderer
// Draw world axes at origin
void drawAxes(float len = 2.f);

//! Draws a simplified, real-size Livox Mid-360 at the LiDAR origin in the
//! current BeginMode3D context; its connector marks the back (-x).
void drawLidarModel();

//! Draws a simplified, real-size camera at the camera pose in the current
//! BeginMode3D context. The lens these extrinsics describe (optical +z) is
//! ringed in the frustum's yellow.
void drawCameraModel(const Extrinsics& E, CameraBody body);

private:
int texW = 0, texH = 0;

Expand Down
Loading
Loading