Files
rtabmap/corelib/test/test_stereocameramodel.cpp

716 lines
24 KiB
C++
Raw Normal View History

Adding doc and tests (#1492) * added doc and tests for util2d.h * updated cmake-ros ci * Added util3d.h doc and tests * util3d_transforms.h: Added doc and tests * util3d_filtering.h: started doc and test * util3d_filtering.h: more tests and doc * Added more doc/tests * finished util3d_filtering doc and tests * added test for util2d::depthBleedingFiltering * Added util3d_registration tests * Added util3d_features.h doc/tests * added doc/tests for util3d_correspondences.h * added doc/gtest for util3d_mapping.h (missing hpp functions) * finished testing util3d_mapping.hpp * Added util3d_motion_estimation.h tests (2D->3D done) * finished util3d_motion_estimation.h tests * minimal util3d_surface.h * Added Transform and VisualWord tests * Added doc for CameraModel and StereoCameraModel * Added more logs in ros ci * Passing tests on fical * improved all devcontainer * added devcontainer kilted, fixed source setup.bash, removed ldconfig in ros-cmake workflow * cleanup * source ros * Added utilite tests * Added testing to appveyor, github actions cancellable on re-commit on same branch * appveyor testing without all targets * appveyor: specifying ALL_BUILD target * Fixed Util2dTest.NMSImageBoundsRespected test * Fixing PCL Indices error on old pcl * Added VWDictionary tests and doc. Fixed LSH not working (fix from https://github.com/flann-lib/flann/pull/472 * fixing some appveyor CI errors, added test to check dictionary serialization against all type * Added StereoDense, StereoBM and StereoSGBM doc and tests * Added Stereo tests * Added CameraModel and StereoCameraModel tests * Added doc and test for Statistics * Added doc/tests for Signature * Added doc/test for SensorEvent, added doc for SensorCaptureInfo * Added doc to SensorData * Added SensorData tests * Added SensorCapture and SensorCaptureThread doc and tests * fixed sensordata test * updated SSC test and doc * Added doc and tests for BayesFilter class * Enabled testing on mac, updated windows testing like on linux * added test_link * fixed unresolved on windows * fixed ThreadHandle error on macos ci * Added GPS and GeodeticCoords tests * Added tests for compression * Added Odometry tests (base class only) * Added DBDriver tests * Added coverage report * uniformized test names * fixing concurancy and coverage ci * dont built tools, examples and app for coverage build * fixed report tool rebuilt without qt compilation error * updated coverage option * updated coverage config * added doc CI job * fixing windows and mac ci errors * Added DBDriverSqlite3 tests * Added IMU tests * Added Graph tests * fixing flaky macos test * Added IMUThread and IMUFilter tests * Added Landmarks tests * Added LASWriter tests * fixing seed flaky test * fixing flaky macos timing tests * Added LocalGrid tests * Added LocalGridMaker tests * fixing ci errors * Added GlobalMap tests * Added doc for EnvSensor * Added Features2D tests * Added Registration tests * Added RegistrationVis tests * Added doc for Rtabmap and Memory classes * Added Memory and Rtabmap tests * making some tests less flaky * lcov 1.14 support * updated compatible tool arguments * Added integration tests (RGB-D, Stereo, Lidar2d, Lidar3d) * More octomap checks * Refactored how/when python interpretor is created to simplify library usage * Added python tests * fixed some flaky tests * suppressed some third party related warnings * fixed ceres tests * more flaky fixes * Fixing tests without libpointmatcher * Added RANSAC rejection filter to PCL ICP * fixing multi platform flakiness * Added test to detect regression * Fixing windows pcl link error * fixed some macos flakiness * bigger 2D2D registration error on opencv 4.6.0 * flakiness * fixing flaky tests on windows and mac * flaky thread test on slow mac VM * windows slow test * fixing more ci erros * fxing temp dir on windows * Added Optimizer tests and discovered some bugs (fixed) * fixing flaky tests in mac and windows * Added Optimizer doc * Added GTSAM BA, updated Ceres to use g2o ba parameters. Renamed g2o's ba related parameters to Optimizer group and used by both gtsam and ceres. * fixing build without gtsam * fixing home dir * fixing python ci isssues * Added multicam ba tests * Added Ceres multicam BA support * Aligned BundleAdjustment parameters with Optimizer/Strategy to avoid confusion in the code * Added BA integration test * Added robust graph optimization integration test * Added loop3it test * Added stereo20Hz test * Added smartfactor gtsam * Fixed bugged check and warn if python didn't return any descriptors * Fixing gtsam version build issues * fixing tilt on windows ci * loosing ceres integration test for ci * mac ci flakiness * updating missing param in gui * updating test bound for mac * added appearance-based tests, set min gftt quality to quality level * testing more stuff * improving features2d tests * ci flakiness * fixing flaky ci * ci fixes * flaky fixes * Added RegistrationIcp tests * Added icp integration test with real-worl corridor like env * intermediate nodes * fixing enum * Updated test to catch #1714 * Fixed 2d corridor failing on pcl * flaky pnp test * flaky brisk test * Set rtabmap_integration test as long * updating loop closure test * flaky ci tests * TEsting roundtrip g2o/toro save/load * loosing test bound * fixed cuda capable checks * flaky tests * Debugging test hanging * more debugging stuff * updating limit * windows: disabled cuda on ci to avoid incompatible driver issue. Fixing a bad test mem allocation * trying fixing cuda hanging issue * fixing ci flakyness * flaky tests * Updated BOW flaky tests by checking min precision/recall instead of recall@100precision. Fixed signature test * CameraModel::load() test initRectificationMap param * test dbdriver load dictionary idsOnly * Memory: test keepLinkedInDb param * added dummyDictionary tests * test intermediate nodes count * Added MarkerDetector tests * reverted breaking change of UMutex and USemaphore * Features2d: fixed compiltion warnings with clang about override * clang warnings * fixing test build with pcl 1.8 * g2o and gtsam build errors on android * opencv5 test fixes * disabled testing for ios and android builds * normalized endline characters for easier diff * added LF CRLF rule * bump 0.23.10. fixing doc version * Publish rtabmap website doc from ci * fixing MSCVC build error * macos icp flaky test * fixing ceres macos test bound * ficing more flaky tests * fixing opencv5 related test errors. Also fixed an actual bug in ENU_WGS84ToGeocentric_WGS84() * added comment about mrpt change * removed rosdoc2 (will add it for rtabmap_ros later) * fixing website style * updated download links * locally deployable website with api * sweep doxygen issues * improved/revised doxygen main pages * removed examples empty page * Updated doxygen style * more concise doxygen groups * added api link on main readme * fixing utilite test error * fixing CommonFilteringGroundNormalsUp test * updated precisionRecall test bounds for Freak and brief descriptors * fixing scale check in ba tests * disabled tests on windows cuda build (missing dlls amd runner cannot test cuda anyway) * ceres: missing suitesparse dep in windows ci * adjusting recall thr for fast/freak * ficing more flaky tests * fixing flaky tests * disabled coverage in ros ci * Enable integration tests for ros ci jobs * loosing up some threshold for failing tests * trigger cache * fixing test data in ros ci. Updated flaky test for mac * slaking some test limit * Fixed rtabmap-detectMoreLoopClosures inverted output value * loosing up sift recall on mac * optimizer re-ordered distribution for reproducible results (mac g2o) * macos dump test crash log * combining all tests to save time on shared library reload. Also fixed Logs with missing arguments. * Added ENABLE_FORMAT_ERRORS cmake option * do test only one time * fixed all format warnings * format security android build errors * less verbose tests * updated ImuUThread test * fixed a log * Fixed libpointmatcher 2d normals eigen issue * Fixing libpointmatcher conversion issues * fixing libpointmatcher test on windows ci * cleanup comments, relax some test thr * disabled sequoia-intel ci build (too flaky, would need extensive testing directly on that machine)
2026-08-06 13:32:20 -07:00
#include <gtest/gtest.h>
#include <opencv2/core.hpp>
#include "rtabmap/core/StereoCameraModel.h"
#include "rtabmap/core/CameraModel.h"
#include "rtabmap/core/Transform.h"
#include "rtabmap/utilite/UException.h"
#include "rtabmap/utilite/UDirectory.h"
#include "rtabmap/utilite/UFile.h"
#include <cmath>
using namespace rtabmap;
class StereoCameraModelTest : public ::testing::Test {
protected:
void SetUp() override {
// Create test camera parameters
fx_ = 525.0;
fy_ = 525.0;
cx_ = 320.0;
cy_ = 240.0;
baseline_ = 0.12; // 12 cm baseline
imageWidth_ = 640;
imageHeight_ = 480;
imageSize_ = cv::Size(imageWidth_, imageHeight_);
// Create intrinsic matrix K
K_ = (cv::Mat_<double>(3, 3) <<
fx_, 0.0, cx_,
0.0, fy_, cy_,
0.0, 0.0, 1.0);
// Create distortion coefficients
D_ = (cv::Mat_<double>(1, 4) << -0.1, 0.05, 0.001, -0.001);
// Create rectification matrix
R_ = cv::Mat::eye(3, 3, CV_64FC1);
// Create projection matrix P (with Tx = baseline * fx for left camera)
double Tx = baseline_ * fx_;
P_left_ = (cv::Mat_<double>(3, 4) <<
fx_, 0.0, cx_, Tx,
0.0, fy_, cy_, 0.0,
0.0, 0.0, 1.0, 0.0);
// Right camera projection matrix (Tx = 0 typically)
P_right_ = (cv::Mat_<double>(3, 4) <<
fx_, 0.0, cx_, 0.0,
0.0, fy_, cy_, 0.0,
0.0, 0.0, 1.0, 0.0);
// Create stereo extrinsic parameters
// R and T represent the left camera relative to the right camera coordinate system
// For parallel cameras with baseline along x-axis, T is negative baseline
// Translation of left camera relative to right camera coordinate system
T_ = (cv::Mat_<double>(3, 1) << -baseline_, 0.0, 0.0);
// Rotation matrix of left camera relative to right camera coordinate system
// (identity for parallel cameras)
R_stereo_ = cv::Mat::eye(3, 3, CV_64FC1);
}
void TearDown() override {
}
double fx_, fy_, cx_, cy_, baseline_;
int imageWidth_, imageHeight_;
cv::Size imageSize_;
cv::Mat K_, D_, R_, P_left_, P_right_, R_stereo_, T_;
};
// Constructor Tests
TEST_F(StereoCameraModelTest, DefaultConstructor)
{
StereoCameraModel model;
EXPECT_FALSE(model.isValidForProjection());
EXPECT_FALSE(model.isValidForRectification());
EXPECT_EQ(model.baseline(), 0.0);
}
TEST_F(StereoCameraModelTest, MinimalConstructor)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
EXPECT_TRUE(model.isValidForProjection());
EXPECT_NEAR(model.baseline(), baseline_, 0.001);
EXPECT_DOUBLE_EQ(model.left().fx(), fx_);
EXPECT_DOUBLE_EQ(model.right().fx(), fx_);
}
TEST_F(StereoCameraModelTest, MinimalConstructorWithName)
{
std::string name = "stereo_camera";
StereoCameraModel model(name, fx_, fy_, cx_, cy_, baseline_);
EXPECT_EQ(model.name(), name);
EXPECT_TRUE(model.isValidForProjection());
EXPECT_NEAR(model.baseline(), baseline_, 0.001);
}
TEST_F(StereoCameraModelTest, ConstructorFromCameraModels)
{
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
StereoCameraModel model("stereo", left, right, R_stereo_, T_);
EXPECT_EQ(model.name(), "stereo");
EXPECT_TRUE(model.isValidForProjection());
EXPECT_NEAR(model.baseline(), baseline_, 0.01);
EXPECT_EQ(model.left().name(), "stereo_left");
EXPECT_EQ(model.right().name(), "stereo_right");
}
TEST_F(StereoCameraModelTest, ConstructorFromCameraModelsWithTransform)
{
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
// Transform represents left camera relative to right camera coordinate system
// For baseline along x-axis, x should be negative
Transform extrinsics = Transform(-baseline_, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
StereoCameraModel model("stereo", left, right, extrinsics);
EXPECT_TRUE(model.isValidForProjection());
EXPECT_NEAR(model.baseline(), baseline_, 0.01);
// Verify stereo transform matches (left camera relative to right camera coordinate system)
Transform stereoTransform = model.stereoTransform();
EXPECT_NEAR(stereoTransform.x(), -baseline_, 0.001);
}
TEST_F(StereoCameraModelTest, FullConstructor)
{
std::string name = "stereo_camera";
StereoCameraModel model(
name,
imageSize_, K_, D_, R_, P_left_,
imageSize_, K_, D_, R_, P_right_,
R_stereo_, T_, cv::Mat(), cv::Mat()
);
EXPECT_EQ(model.name(), name);
EXPECT_TRUE(model.isValidForProjection());
EXPECT_TRUE(model.isValidForRectification());
EXPECT_NEAR(model.baseline(), baseline_, 0.01);
}
// Validation Tests
TEST_F(StereoCameraModelTest, IsValidForProjection)
{
StereoCameraModel invalid;
EXPECT_FALSE(invalid.isValidForProjection());
StereoCameraModel valid(fx_, fy_, cx_, cy_, baseline_);
EXPECT_TRUE(valid.isValidForProjection());
}
TEST_F(StereoCameraModelTest, IsValidForRectification)
{
StereoCameraModel minimal(fx_, fy_, cx_, cy_, baseline_);
EXPECT_FALSE(minimal.isValidForRectification());
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
StereoCameraModel full("stereo", left, right, R_stereo_, T_);
EXPECT_TRUE(full.isValidForRectification());
}
// Rectification Tests
TEST_F(StereoCameraModelTest, InitRectificationMap)
{
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
StereoCameraModel model("stereo", left, right, R_stereo_, T_);
EXPECT_FALSE(model.isRectificationMapInitialized());
model.initRectificationMap();
EXPECT_TRUE(model.isRectificationMapInitialized());
}
// Depth/Disparity Conversion Tests
TEST_F(StereoCameraModelTest, ComputeDepth)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
// Test with known disparity
// disparity = baseline * fx / depth
float depth = 1.0f; // 1 meter
float expectedDisparity = static_cast<float>(baseline_ * fx_ / depth);
float computedDepth = model.computeDepth(expectedDisparity);
EXPECT_NEAR(computedDepth, depth, 0.01f);
}
TEST_F(StereoCameraModelTest, ComputeDepthZeroDisparity)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
float depth = model.computeDepth(0.0f);
EXPECT_EQ(depth, 0.0f);
}
TEST_F(StereoCameraModelTest, ComputeDisparityFromDepth)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
float depth = 1.0f; // 1 meter
float disparity = model.computeDisparity(depth);
// Verify round-trip
float computedDepth = model.computeDepth(disparity);
EXPECT_NEAR(computedDepth, depth, 0.01f);
}
TEST_F(StereoCameraModelTest, ComputeDisparityFromDepthMM)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
unsigned short depthMM = 1000; // 1 meter in millimeters
float disparity = model.computeDisparity(depthMM);
// Should be same as computing from meters
float disparityFromMeters = model.computeDisparity(1.0f);
EXPECT_NEAR(disparity, disparityFromMeters, 0.1f);
}
TEST_F(StereoCameraModelTest, ComputeDisparityZeroDepth)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
float disparity = model.computeDisparity(0.0f);
EXPECT_EQ(disparity, 0.0f);
unsigned short depthMM = 0;
float disparityMM = model.computeDisparity(depthMM);
EXPECT_EQ(disparityMM, 0.0f);
}
// Getter Tests
TEST_F(StereoCameraModelTest, Baseline)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
EXPECT_NEAR(model.baseline(), baseline_, 0.001);
}
TEST_F(StereoCameraModelTest, LeftRightModels)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
const CameraModel& left = model.left();
const CameraModel& right = model.right();
EXPECT_DOUBLE_EQ(left.fx(), fx_);
EXPECT_DOUBLE_EQ(right.fx(), fx_);
EXPECT_DOUBLE_EQ(left.fy(), fy_);
EXPECT_DOUBLE_EQ(right.fy(), fy_);
}
TEST_F(StereoCameraModelTest, ExtrinsicMatrices)
{
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
StereoCameraModel model("stereo", left, right, R_stereo_, T_);
// R and T represent left camera relative to right camera coordinate system
const cv::Mat& R = model.R();
const cv::Mat& T = model.T();
EXPECT_FALSE(R.empty());
EXPECT_FALSE(T.empty());
EXPECT_EQ(R.rows, 3);
EXPECT_EQ(R.cols, 3);
EXPECT_EQ(T.rows, 3);
EXPECT_EQ(T.cols, 1);
// Verify T matches expected value (negative baseline for left relative to right)
EXPECT_NEAR(T.at<double>(0, 0), -baseline_, 0.001);
EXPECT_NEAR(T.at<double>(1, 0), 0.0, 0.001);
EXPECT_NEAR(T.at<double>(2, 0), 0.0, 0.001);
}
// Name and Suffix Tests
TEST_F(StereoCameraModelTest, SetName)
{
StereoCameraModel model;
std::string name = "my_stereo";
model.setName(name);
EXPECT_EQ(model.name(), name);
}
TEST_F(StereoCameraModelTest, SetNameWithSuffixes)
{
StereoCameraModel model;
model.setName("stereo", "cam1", "cam2");
EXPECT_EQ(model.name(), "stereo");
EXPECT_EQ(model.getLeftSuffix(), "cam1");
EXPECT_EQ(model.getRightSuffix(), "cam2");
}
TEST_F(StereoCameraModelTest, GetSuffixes)
{
StereoCameraModel model;
EXPECT_EQ(model.getLeftSuffix(), "left");
EXPECT_EQ(model.getRightSuffix(), "right");
}
// Transform Tests
TEST_F(StereoCameraModelTest, LocalTransform)
{
Transform transform = Transform(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0);
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_, transform);
EXPECT_FALSE(model.localTransform().isNull());
model.setLocalTransform(Transform());
EXPECT_TRUE(model.localTransform().isNull());
}
TEST_F(StereoCameraModelTest, StereoTransform)
{
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
StereoCameraModel model("stereo", left, right, R_stereo_, T_);
Transform stereoTransform = model.stereoTransform();
EXPECT_FALSE(stereoTransform.isNull());
// Stereo transform represents left camera relative to right camera coordinate system
// For a baseline of 0.12 m, the x value should be -0.12 (negative)
EXPECT_NEAR(stereoTransform.x(), -baseline_, 0.001);
}
TEST_F(StereoCameraModelTest, StereoTransformBaselineExample)
{
// Test the specific example from documentation: 15 cm baseline -> -0.15 x value
double testBaseline = 0.15; // 15 cm
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
// Create T with negative baseline (left camera relative to right camera coordinate system)
cv::Mat T_test = (cv::Mat_<double>(3, 1) << -testBaseline, 0.0, 0.0);
cv::Mat R_test = cv::Mat::eye(3, 3, CV_64FC1);
StereoCameraModel model("stereo", left, right, R_test, T_test);
Transform stereoTransform = model.stereoTransform();
EXPECT_FALSE(stereoTransform.isNull());
// Verify the x value is -0.15 as documented
EXPECT_NEAR(stereoTransform.x(), -0.15, 0.001);
// Verify baseline matches
EXPECT_NEAR(model.baseline(), testBaseline, 0.001);
}
// Scaling and ROI Tests
TEST_F(StereoCameraModelTest, Scale)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
model.setImageSize(imageSize_);
double scale = 0.5;
model.scale(scale);
EXPECT_NEAR(model.left().fx(), fx_ * scale, 0.01);
EXPECT_NEAR(model.right().fx(), fx_ * scale, 0.01);
EXPECT_EQ(model.left().imageWidth(), static_cast<int>(imageWidth_ * scale));
// Baseline should not be scaled (it's a physical distance)
EXPECT_NEAR(model.baseline(), baseline_, 0.001);
}
TEST_F(StereoCameraModelTest, ROI)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
model.setImageSize(imageSize_);
cv::Rect roi(100, 100, 200, 200);
model.roi(roi);
EXPECT_EQ(model.left().imageWidth(), roi.width);
EXPECT_EQ(model.left().imageHeight(), roi.height);
EXPECT_EQ(model.right().imageWidth(), roi.width);
EXPECT_EQ(model.right().imageHeight(), roi.height);
}
// SetImageSize Tests
TEST_F(StereoCameraModelTest, SetImageSize)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
cv::Size newSize(320, 240);
model.setImageSize(newSize);
EXPECT_EQ(model.left().imageSize(), newSize);
EXPECT_EQ(model.right().imageSize(), newSize);
}
// Serialization Tests
TEST_F(StereoCameraModelTest, SerializeDeserialize)
{
StereoCameraModel original(fx_, fy_, cx_, cy_, baseline_);
original.setName("test_stereo");
original.setImageSize(imageSize_);
std::vector<unsigned char> data = original.serialize();
EXPECT_FALSE(data.empty());
StereoCameraModel restored;
unsigned int bytesRead = restored.deserialize(data);
EXPECT_GT(bytesRead, 0u);
EXPECT_NEAR(restored.baseline(), original.baseline(), 0.001);
EXPECT_DOUBLE_EQ(restored.left().fx(), original.left().fx());
EXPECT_DOUBLE_EQ(restored.right().fx(), original.right().fx());
}
TEST_F(StereoCameraModelTest, SerializeDeserializeFromPointer)
{
StereoCameraModel original(fx_, fy_, cx_, cy_, baseline_);
original.setName("test_stereo");
std::vector<unsigned char> data = original.serialize();
StereoCameraModel restored;
unsigned int bytesRead = restored.deserialize(data.data(), data.size());
EXPECT_GT(bytesRead, 0u);
EXPECT_EQ(bytesRead, data.size());
EXPECT_NEAR(restored.baseline(), original.baseline(), 0.001);
}
// Round-trip Depth/Disparity Tests
TEST_F(StereoCameraModelTest, DepthDisparityRoundTrip)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, baseline_);
// Test multiple depths
float testDepths[] = {0.5f, 1.0f, 2.0f, 5.0f, 10.0f};
for(float depth : testDepths)
{
float disparity = model.computeDisparity(depth);
float computedDepth = model.computeDepth(disparity);
EXPECT_NEAR(computedDepth, depth, 0.01f) << "Depth: " << depth;
}
}
// Edge Cases
TEST_F(StereoCameraModelTest, InvalidBaseline)
{
StereoCameraModel model(fx_, fy_, cx_, cy_, 0.0); // Zero baseline
EXPECT_FALSE(model.isValidForProjection());
EXPECT_EQ(model.baseline(), 0.0);
}
TEST_F(StereoCameraModelTest, NegativeBaseline)
{
// Negative baseline should still compute, but may not be physically meaningful
StereoCameraModel model(fx_, fy_, cx_, cy_, -0.12);
EXPECT_DOUBLE_EQ(model.baseline(),-0.12);
}
// Save/Load Tests
TEST_F(StereoCameraModelTest, SaveLoadRoundTrip)
{
// Create a temporary directory for testing
std::string testDir = "test_stereo_calibration";
UDirectory::makeDir(testDir);
// Create original stereo camera model with full parameters
std::string cameraName = "test_stereo";
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
StereoCameraModel original(cameraName, left, right, R_stereo_, T_);
// Save the model (with stereo transform)
bool saveResult = original.save(testDir, false);
EXPECT_TRUE(saveResult);
// Verify files were created
std::string leftFile = testDir + "/" + cameraName + "_left.yaml";
std::string rightFile = testDir + "/" + cameraName + "_right.yaml";
std::string poseFile = testDir + "/" + cameraName + "_pose.yaml";
EXPECT_TRUE(UFile::exists(leftFile));
EXPECT_TRUE(UFile::exists(rightFile));
EXPECT_TRUE(UFile::exists(poseFile));
// Load the model back
StereoCameraModel loaded;
bool loadResult = loaded.load(testDir, cameraName, false);
EXPECT_TRUE(loadResult);
// Verify all parameters match
EXPECT_STREQ(loaded.name().c_str(), original.name().c_str());
EXPECT_NEAR(loaded.baseline(), original.baseline(), 0.001);
// Verify left camera parameters
EXPECT_DOUBLE_EQ(loaded.left().fx(), original.left().fx());
EXPECT_DOUBLE_EQ(loaded.left().fy(), original.left().fy());
EXPECT_DOUBLE_EQ(loaded.left().cx(), original.left().cx());
EXPECT_DOUBLE_EQ(loaded.left().cy(), original.left().cy());
EXPECT_DOUBLE_EQ(loaded.left().Tx(), original.left().Tx());
EXPECT_EQ(loaded.left().imageSize(), original.left().imageSize());
// Verify right camera parameters
EXPECT_DOUBLE_EQ(loaded.right().fx(), original.right().fx());
EXPECT_DOUBLE_EQ(loaded.right().fy(), original.right().fy());
EXPECT_DOUBLE_EQ(loaded.right().cx(), original.right().cx());
EXPECT_DOUBLE_EQ(loaded.right().cy(), original.right().cy());
EXPECT_DOUBLE_EQ(loaded.right().Tx(), original.right().Tx());
EXPECT_EQ(loaded.right().imageSize(), original.right().imageSize());
// Verify stereo extrinsic matrices
cv::Mat R_loaded = loaded.R();
cv::Mat R_original = original.R();
if(!R_loaded.empty() && !R_original.empty())
{
EXPECT_EQ(R_loaded.rows, R_original.rows);
EXPECT_EQ(R_loaded.cols, R_original.cols);
for(int i = 0; i < R_loaded.rows; ++i)
{
for(int j = 0; j < R_loaded.cols; ++j)
{
EXPECT_NEAR(R_loaded.at<double>(i, j), R_original.at<double>(i, j), 0.001);
}
}
}
cv::Mat T_loaded = loaded.T();
cv::Mat T_original = original.T();
if(!T_loaded.empty() && !T_original.empty())
{
EXPECT_EQ(T_loaded.rows, T_original.rows);
EXPECT_EQ(T_loaded.cols, T_original.cols);
for(int i = 0; i < T_loaded.rows; ++i)
{
for(int j = 0; j < T_loaded.cols; ++j)
{
EXPECT_NEAR(T_loaded.at<double>(i, j), T_original.at<double>(i, j), 0.001);
}
}
}
// Verify stereo transform (left camera relative to right camera coordinate system)
Transform stereoTransform_loaded = loaded.stereoTransform();
Transform stereoTransform_original = original.stereoTransform();
if(!stereoTransform_loaded.isNull() && !stereoTransform_original.isNull())
{
// Compare transform matrices element by element
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 4; ++j)
{
EXPECT_NEAR(stereoTransform_loaded.data()[i*4+j], stereoTransform_original.data()[i*4+j], 0.001);
}
}
// Verify x value is negative baseline (left camera relative to right camera coordinate system)
EXPECT_NEAR(stereoTransform_loaded.x(), -original.baseline(), 0.001);
EXPECT_NEAR(stereoTransform_original.x(), -original.baseline(), 0.001);
}
// Verify local transform
Transform localTransform_loaded = loaded.localTransform();
Transform localTransform_original = original.localTransform();
if(!localTransform_loaded.isNull() && !localTransform_original.isNull())
{
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 4; ++j)
{
EXPECT_NEAR(localTransform_loaded.data()[i*4+j], localTransform_original.data()[i*4+j], 0.001);
}
}
}
// Verify validation states
EXPECT_EQ(loaded.isValidForProjection(), original.isValidForProjection());
EXPECT_EQ(loaded.isValidForRectification(), original.isValidForRectification());
}
TEST_F(StereoCameraModelTest, SaveLoadRoundTripIgnoreTransform)
{
// Create a temporary directory for testing
std::string testDir = "test_stereo_calibration2";
UDirectory::makeDir(testDir);
// Create original stereo camera model
std::string cameraName = "test_stereo2";
StereoCameraModel original(cameraName, fx_, fy_, cx_, cy_, baseline_);
original.setImageSize(imageSize_);
// Save the model (without stereo transform)
bool saveResult = original.save(testDir, true);
EXPECT_TRUE(saveResult);
// Verify camera files were created (but not pose file)
std::string leftFile = testDir + "/" + cameraName + "_left.yaml";
std::string rightFile = testDir + "/" + cameraName + "_right.yaml";
EXPECT_TRUE(UFile::exists(leftFile));
EXPECT_TRUE(UFile::exists(rightFile));
// Load the model back (ignoring stereo transform)
StereoCameraModel loaded;
bool loadResult = loaded.load(testDir, cameraName, true);
EXPECT_TRUE(loadResult);
// Verify parameters match
EXPECT_EQ(loaded.name(), original.name());
EXPECT_NEAR(loaded.baseline(), original.baseline(), 0.001);
EXPECT_DOUBLE_EQ(loaded.left().fx(), original.left().fx());
EXPECT_DOUBLE_EQ(loaded.right().fx(), original.right().fx());
}
TEST_F(StereoCameraModelTest, LoadInitRectificationMaps)
{
// Create a temporary directory for testing
std::string testDir = "test_stereo_calibration5";
UDirectory::makeDir(testDir);
// A stereo pair valid for rectification, so both left and right maps
// would be built on load.
std::string cameraName = "rect_stereo";
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
StereoCameraModel original(cameraName, left, right, R_stereo_, T_);
ASSERT_TRUE(original.isValidForRectification());
ASSERT_TRUE(original.save(testDir, false));
// Default: rectification maps are built for both cameras while loading.
StereoCameraModel withMaps;
EXPECT_TRUE(withMaps.load(testDir, cameraName, false));
EXPECT_TRUE(withMaps.isRectificationMapInitialized());
EXPECT_TRUE(withMaps.left().isRectificationMapInitialized());
EXPECT_TRUE(withMaps.right().isRectificationMapInitialized());
// initRectificationMaps=false: both cameras are loaded but neither builds
// its maps, so the pair can be inspected without paying for them twice.
StereoCameraModel withoutMaps;
EXPECT_TRUE(withoutMaps.load(testDir, cameraName, false, false));
EXPECT_FALSE(withoutMaps.isRectificationMapInitialized());
EXPECT_FALSE(withoutMaps.left().isRectificationMapInitialized());
EXPECT_FALSE(withoutMaps.right().isRectificationMapInitialized());
// The calibration itself must be untouched by the flag.
EXPECT_TRUE(withoutMaps.isValidForRectification());
EXPECT_EQ(withoutMaps.name(), withMaps.name());
EXPECT_NEAR(withoutMaps.baseline(), withMaps.baseline(), 0.001);
EXPECT_DOUBLE_EQ(withoutMaps.left().fx(), withMaps.left().fx());
EXPECT_DOUBLE_EQ(withoutMaps.right().fx(), withMaps.right().fx());
// ... so the maps can still be built afterwards on demand.
withoutMaps.initRectificationMap();
EXPECT_TRUE(withoutMaps.isRectificationMapInitialized());
}
TEST_F(StereoCameraModelTest, SaveLoadRoundTripMinimal)
{
// Create a temporary directory for testing
std::string testDir = "test_stereo_calibration3";
UDirectory::makeDir(testDir);
// Create minimal stereo camera model
std::string cameraName = "minimal_stereo";
StereoCameraModel original(cameraName, fx_, fy_, cx_, cy_, baseline_);
original.setImageSize(imageSize_);
// Save the model
bool saveResult = original.save(testDir);
EXPECT_TRUE(saveResult);
// Load the model back
StereoCameraModel loaded;
bool loadResult = loaded.load(testDir, cameraName);
EXPECT_TRUE(loadResult);
// Verify parameters match
EXPECT_EQ(loaded.name(), original.name());
EXPECT_NEAR(loaded.baseline(), original.baseline(), 0.001);
EXPECT_DOUBLE_EQ(loaded.left().fx(), original.left().fx());
EXPECT_DOUBLE_EQ(loaded.right().fx(), original.right().fx());
EXPECT_EQ(loaded.left().imageSize(), original.left().imageSize());
EXPECT_EQ(loaded.right().imageSize(), original.right().imageSize());
}
TEST_F(StereoCameraModelTest, SaveStereoTransform)
{
// Create a temporary directory for testing
std::string testDir = "test_stereo_calibration4";
UDirectory::makeDir(testDir);
// Create stereo camera model with extrinsics
std::string cameraName = "stereo_with_extrinsics";
CameraModel left("left", imageSize_, K_, D_, R_, P_left_);
CameraModel right("right", imageSize_, K_, D_, R_, P_right_);
StereoCameraModel model(cameraName, left, right, R_stereo_, T_);
// Save stereo transform separately
bool saveResult = model.saveStereoTransform(testDir);
EXPECT_TRUE(saveResult);
// Verify pose file was created
std::string poseFile = testDir + "/" + cameraName + "_pose.yaml";
EXPECT_TRUE(UFile::exists(poseFile));
}