Files
rtabmap/corelib/test/test_util3d_correspondences.cpp

469 lines
16 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 "rtabmap/core/util3d.h"
#include "rtabmap/core/util3d_correspondences.h"
#include "rtabmap/core/CameraModel.h"
#include "rtabmap/utilite/UException.h"
#include "rtabmap/utilite/UConversion.h"
#include <pcl/io/pcd_io.h>
using namespace rtabmap;
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesValidOneToOneMatch) {
std::multimap<int, pcl::PointXYZ> words1 = {
{1, pcl::PointXYZ(1, 2, 3)},
{2, pcl::PointXYZ(4, 5, 6)}
};
std::multimap<int, pcl::PointXYZ> words2 = {
{1, pcl::PointXYZ(1.1f, 2.1f, 3.1f)},
{2, pcl::PointXYZ(4.1f, 5.1f, 6.1f)}
};
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
util3d::extractXYZCorrespondences(words1, words2, cloud1, cloud2);
ASSERT_EQ(cloud1.size(), 2);
ASSERT_EQ(cloud2.size(), 2);
EXPECT_EQ(cloud1.points[0].x, 1);
EXPECT_EQ(cloud2.points[1].z, 6.1f);
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesDuplicateKeysIgnored) {
std::multimap<int, pcl::PointXYZ> words1 = {
{1, pcl::PointXYZ(0, 0, 0)},
{1, pcl::PointXYZ(1, 1, 1)}, // duplicate
{2, pcl::PointXYZ(2, 2, 2)}
};
std::multimap<int, pcl::PointXYZ> words2 = {
{1, pcl::PointXYZ(1, 1, 1)},
{2, pcl::PointXYZ(2.1f, 2.1f, 2.1f)}
};
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
util3d::extractXYZCorrespondences(words1, words2, cloud1, cloud2);
ASSERT_EQ(cloud1.size(), 1);
ASSERT_EQ(cloud2.size(), 1);
EXPECT_EQ(cloud1[0].x, 2);
EXPECT_EQ(cloud2[0].z, 2.1f);
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesInvalidPointsIgnored) {
pcl::PointXYZ nanPt(std::numeric_limits<float>::quiet_NaN(), 0, 0);
std::multimap<int, pcl::PointXYZ> words1 = {
{1, pcl::PointXYZ(1, 2, 3)},
{2, nanPt}
};
std::multimap<int, pcl::PointXYZ> words2 = {
{1, pcl::PointXYZ(1.5f, 2.5f, 3.5f)},
{2, pcl::PointXYZ(4, 5, 6)}
};
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
util3d::extractXYZCorrespondences(words1, words2, cloud1, cloud2);
ASSERT_EQ(cloud1.size(), 1);
ASSERT_EQ(cloud2.size(), 1);
EXPECT_EQ(cloud1[0].x, 1);
EXPECT_EQ(cloud2[0].y, 2.5f);
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesNoCommonIDs) {
std::multimap<int, pcl::PointXYZ> words1 = {
{10, pcl::PointXYZ(1, 2, 3)}
};
std::multimap<int, pcl::PointXYZ> words2 = {
{20, pcl::PointXYZ(4, 5, 6)}
};
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
util3d::extractXYZCorrespondences(words1, words2, cloud1, cloud2);
EXPECT_TRUE(cloud1.empty());
EXPECT_TRUE(cloud2.empty());
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesRANSACAcceptsCleanMatches) {
std::multimap<int, pcl::PointXYZ> words1;
std::multimap<int, pcl::PointXYZ> words2;
// 10 consistent matches
for (int i = 0; i < 10; ++i) {
words1.insert({i, pcl::PointXYZ(i * 1.0f, exp2(i)/10.0f, 0.0f)});
words2.insert({i, pcl::PointXYZ(i * 1.0f + 1.1f, exp2(i)/10.0f + 1.1f, 0.0f)}); // Slight noise
}
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
util3d::extractXYZCorrespondencesRANSAC(words1, words2, cloud1, cloud2);
EXPECT_EQ(cloud1.size(), cloud2.size());
EXPECT_GE(cloud1.size(), 8); // At least 8 inliers from 10 consistent matches
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesRANSACRejectsOutliers) {
std::multimap<int, pcl::PointXYZ> words1;
std::multimap<int, pcl::PointXYZ> words2;
// 8 inliers
for (int i = 0; i < 8; ++i) {
words1.insert({i, pcl::PointXYZ(i * 1.0f, exp2(i)/10.0f, 0.0f)});
words2.insert({i, pcl::PointXYZ(i * 1.0f + 1.1f, exp2(i)/10.0f + 1.1f, 0.0f)}); // Slight noise
}
// 2 outliers
words1.insert({100, pcl::PointXYZ(0.0f, 0.0f, 0.0f)});
words2.insert({100, pcl::PointXYZ(100.0f, 100.0f, 0.0f)});
words1.insert({101, pcl::PointXYZ(1.0f, 1.0f, 0.0f)});
words2.insert({101, pcl::PointXYZ(200.0f, -50.0f, 0.0f)});
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
util3d::extractXYZCorrespondencesRANSAC(words1, words2, cloud1, cloud2);
EXPECT_EQ(cloud1.size(), cloud2.size());
EXPECT_EQ(cloud1.size(), 8); // RANSAC should reject 2 outliers
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesRANSACFailsGracefullyOnTooFewMatches) {
std::multimap<int, pcl::PointXYZ> words1 = {
{1, pcl::PointXYZ(0, 0, 0)},
{2, pcl::PointXYZ(1, 1, 1)},
{3, pcl::PointXYZ(2, 2, 2)}
};
std::multimap<int, pcl::PointXYZ> words2 = {
{1, pcl::PointXYZ(0.1f, 0.1f, 0)},
{2, pcl::PointXYZ(1.1f, 1.1f, 1)},
{3, pcl::PointXYZ(2.1f, 2.1f, 2)}
};
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
util3d::extractXYZCorrespondencesRANSAC(words1, words2, cloud1, cloud2);
EXPECT_TRUE(cloud1.empty());
EXPECT_TRUE(cloud2.empty());
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesValidCorrespondencesAreExtracted) {
// Create simple 5x5 depth images with valid depth
cv::Mat depth1 = cv::Mat::ones(5, 5, CV_32FC1) * 1.0f;
cv::Mat depth2 = cv::Mat::ones(5, 5, CV_32FC1) * 1.5f;
std::list<std::pair<cv::Point2f, cv::Point2f>> matches = {
{cv::Point2f(2, 2), cv::Point2f(2, 2)},
{cv::Point2f(1, 1), cv::Point2f(1, 1)},
{cv::Point2f(3, 3), cv::Point2f(3, 3)}
};
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
float fx = 1.0f, fy = 1.0f, cx = 2.0f, cy = 2.0f;
util3d::extractXYZCorrespondences(matches, depth1, depth2, cx, cy, fx, fy, 2.0f, cloud1, cloud2);
ASSERT_EQ(cloud1.size(), 3);
ASSERT_EQ(cloud2.size(), 3);
// Check one known point
EXPECT_FLOAT_EQ(cloud1[0].z, 1.0f);
EXPECT_FLOAT_EQ(cloud2[0].z, 1.5f);
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesFiltersInvalidDepth) {
cv::Mat depth1 = cv::Mat::ones(5, 5, CV_32FC1) * 1.0f;
cv::Mat depth2 = cv::Mat::ones(5, 5, CV_32FC1) * 1.5f;
depth1.at<float>(2, 2) = 0.0f; // Invalid
depth2.at<float>(1, 1) = std::numeric_limits<float>::quiet_NaN(); // Invalid
std::list<std::pair<cv::Point2f, cv::Point2f>> matches = {
{cv::Point2f(2, 2), cv::Point2f(2, 2)},
{cv::Point2f(1, 1), cv::Point2f(1, 1)},
{cv::Point2f(3, 3), cv::Point2f(3, 3)}
};
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
util3d::extractXYZCorrespondences(matches, depth1, depth2, 2.0f, 2.0f, 1.0f, 1.0f, 2.0f, cloud1, cloud2);
// Only the third match should remain
ASSERT_EQ(cloud1.size(), 1);
ASSERT_EQ(cloud2.size(), 1);
EXPECT_FLOAT_EQ(cloud1[0].z, 1.0f);
EXPECT_FLOAT_EQ(cloud2[0].z, 1.5f);
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesRespectsMaxDepthConstraint) {
cv::Mat depth1 = cv::Mat::ones(5, 5, CV_32FC1) * 3.0f; // Exceeds maxDepth
cv::Mat depth2 = cv::Mat::ones(5, 5, CV_32FC1) * 1.0f;
std::list<std::pair<cv::Point2f, cv::Point2f>> matches = {
{cv::Point2f(2, 2), cv::Point2f(2, 2)},
{cv::Point2f(1, 1), cv::Point2f(1, 1)}
};
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
util3d::extractXYZCorrespondences(matches, depth1, depth2, 2.0f, 2.0f, 1.0f, 1.0f, 2.5f, cloud1, cloud2);
// All points should be rejected due to depth1 being too large
EXPECT_TRUE(cloud1.empty());
EXPECT_TRUE(cloud2.empty());
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesOrgCloudsValidCorrespondencesAreExtracted) {
int width = 5, height = 5;
// Create two organized point clouds
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
cloud1.width = cloud2.width = width;
cloud1.height = cloud2.height = height;
cloud1.is_dense = cloud2.is_dense = false;
cloud1.points.resize(width * height);
cloud2.points.resize(width * height);
// Fill the clouds with some values
for (int v = 0; v < height; ++v) {
for (int u = 0; u < width; ++u) {
int idx = v * width + u;
cloud1.at(idx).x = u;
cloud1.at(idx).y = v;
cloud1.at(idx).z = 1.0f;
cloud2.at(idx).x = u + 0.5f;
cloud2.at(idx).y = v + 0.5f;
cloud2.at(idx).z = 1.5f;
}
}
// Set correspondences to valid pixel positions
std::list<std::pair<cv::Point2f, cv::Point2f>> correspondences = {
{cv::Point2f(1, 1), cv::Point2f(1, 1)},
{cv::Point2f(2, 2), cv::Point2f(2, 2)},
{cv::Point2f(3, 3), cv::Point2f(3, 3)}
};
pcl::PointCloud<pcl::PointXYZ> inliers1, inliers2;
util3d::extractXYZCorrespondences(correspondences, cloud1, cloud2, inliers1, inliers2);
ASSERT_EQ(inliers1.size(), 3);
ASSERT_EQ(inliers2.size(), 3);
EXPECT_FLOAT_EQ(inliers1[0].x, 1);
EXPECT_FLOAT_EQ(inliers1[0].z, 1.0f);
EXPECT_FLOAT_EQ(inliers2[0].x, 1.5f);
EXPECT_FLOAT_EQ(inliers2[0].z, 1.5f);
}
TEST(Util3dCorrespondencesTest, ExtractXYZCorrespondencesOrgCloudsInvalidPointsAreFilteredOut) {
int width = 3, height = 3;
pcl::PointCloud<pcl::PointXYZ> cloud1, cloud2;
cloud1.width = cloud2.width = width;
cloud1.height = cloud2.height = height;
cloud1.is_dense = cloud2.is_dense = false;
cloud1.points.resize(width * height);
cloud2.points.resize(width * height);
// Set all points to NaN
for (size_t i = 0; i < cloud1.size(); ++i) {
cloud1[i].x = cloud1[i].y = cloud1[i].z = std::numeric_limits<float>::quiet_NaN();
cloud2[i].x = cloud2[i].y = cloud2[i].z = std::numeric_limits<float>::quiet_NaN();
}
// Set one valid point at (1,1)
int idx = 1 * width + 1;
cloud1[idx].x = 1.0f;
cloud1[idx].y = 1.0f;
cloud1[idx].z = 1.0f;
cloud2[idx].x = 2.0f;
cloud2[idx].y = 2.0f;
cloud2[idx].z = 2.0f;
std::list<std::pair<cv::Point2f, cv::Point2f>> correspondences = {
{cv::Point2f(0, 0), cv::Point2f(0, 0)}, // Invalid
{cv::Point2f(1, 1), cv::Point2f(1, 1)}, // Valid
{cv::Point2f(2, 2), cv::Point2f(2, 2)} // Invalid
};
pcl::PointCloud<pcl::PointXYZ> inliers1, inliers2;
util3d::extractXYZCorrespondences(correspondences, cloud1, cloud2, inliers1, inliers2);
ASSERT_EQ(inliers1.size(), 1);
ASSERT_EQ(inliers2.size(), 1);
EXPECT_FLOAT_EQ(inliers1[0].x, 1.0f);
EXPECT_FLOAT_EQ(inliers2[0].x, 2.0f);
}
TEST(Util3dCorrespondencesTest, CountUniquePairsNoPairs) {
std::multimap<int, pcl::PointXYZ> wordsA, wordsB;
wordsA.insert({1, pcl::PointXYZ(1, 2, 3)});
wordsB.insert({2, pcl::PointXYZ(1, 2, 3)}); // No overlapping key
EXPECT_EQ(util3d::countUniquePairs(wordsA, wordsB), 0);
}
TEST(Util3dCorrespondencesTest, CountUniquePairsOneUniquePair) {
std::multimap<int, pcl::PointXYZ> wordsA, wordsB;
wordsA.insert({1, pcl::PointXYZ(1, 1, 1)});
wordsB.insert({1, pcl::PointXYZ(2, 2, 2)}); // One unique pair
EXPECT_EQ(util3d::countUniquePairs(wordsA, wordsB), 1);
}
TEST(Util3dCorrespondencesTest, CountUniquePairsMultipleUniquePairs) {
std::multimap<int, pcl::PointXYZ> wordsA, wordsB;
wordsA.insert({1, pcl::PointXYZ(1, 1, 1)});
wordsA.insert({2, pcl::PointXYZ(2, 2, 2)});
wordsA.insert({3, pcl::PointXYZ(3, 3, 3)});
wordsB.insert({1, pcl::PointXYZ(1, 1, 1)});
wordsB.insert({2, pcl::PointXYZ(2, 2, 2)});
wordsB.insert({3, pcl::PointXYZ(3, 3, 3)});
EXPECT_EQ(util3d::countUniquePairs(wordsA, wordsB), 3);
}
TEST(Util3dCorrespondencesTest, CountUniquePairsDuplicatedPointsNotCounted) {
std::multimap<int, pcl::PointXYZ> wordsA, wordsB;
wordsA.insert({1, pcl::PointXYZ(1, 1, 1)});
wordsA.insert({1, pcl::PointXYZ(1.1f, 1.1f, 1.1f)}); // duplicate in A
wordsB.insert({1, pcl::PointXYZ(2, 2, 2)});
EXPECT_EQ(util3d::countUniquePairs(wordsA, wordsB), 0);
wordsA.clear();
wordsB.clear();
wordsA.insert({2, pcl::PointXYZ(1, 1, 1)});
wordsB.insert({2, pcl::PointXYZ(2, 2, 2)});
wordsB.insert({2, pcl::PointXYZ(3, 3, 3)}); // duplicate in B
EXPECT_EQ(util3d::countUniquePairs(wordsA, wordsB), 0);
}
TEST(Util3dCorrespondencesTest, FilterMaxDepthFiltersByMaxDepthZ) {
pcl::PointCloud<pcl::PointXYZ> cloud1;
pcl::PointCloud<pcl::PointXYZ> cloud2;
// Add points (some above and some below maxDepth = 5.0)
cloud1.push_back(pcl::PointXYZ(1.0f, 1.0f, 4.0f));
cloud2.push_back(pcl::PointXYZ(1.1f, 1.0f, 4.0f));
cloud1.push_back(pcl::PointXYZ(2.0f, 2.0f, 6.0f)); // exceeds maxDepth
cloud2.push_back(pcl::PointXYZ(2.1f, 2.0f, 6.0f));
cloud1.push_back(pcl::PointXYZ(3.0f, 3.0f, 3.0f));
cloud2.push_back(pcl::PointXYZ(3.1f, 3.0f, 3.0f));
// Filter by maxDepth=5.0 on 'z' axis, no duplicates removal
util3d::filterMaxDepth(cloud1, cloud2, 5.0f, 'z', false);
EXPECT_EQ(cloud1.size(), 2);
EXPECT_EQ(cloud2.size(), 2);
for (const auto& pt : cloud1) {
EXPECT_LT(pt.z, 5.0f);
}
}
TEST(Util3dCorrespondencesTest, FilterMaxDepthRemovesDuplicates) {
pcl::PointCloud<pcl::PointXYZ> cloud1;
pcl::PointCloud<pcl::PointXYZ> cloud2;
// Duplicate points in cloud1, but different points in cloud2
cloud1.push_back(pcl::PointXYZ(1.0f, 1.0f, 1.0f));
cloud2.push_back(pcl::PointXYZ(1.1f, 1.0f, 1.0f));
cloud1.push_back(pcl::PointXYZ(1.0f, 1.0f, 1.0f)); // duplicate
cloud2.push_back(pcl::PointXYZ(1.2f, 1.0f, 1.0f));
cloud1.push_back(pcl::PointXYZ(2.0f, 2.0f, 2.0f));
cloud2.push_back(pcl::PointXYZ(2.1f, 2.0f, 2.0f));
// maxDepth large enough to keep all points, removeDuplicates = true
util3d::filterMaxDepth(cloud1, cloud2, 10.0f, 'z', true);
EXPECT_EQ(cloud1.size(), 2);
EXPECT_EQ(cloud2.size(), 2);
// Check that duplicate is removed (only one point with 1.0,1.0,1.0)
int countPoint = 0;
for (const auto& pt : cloud1) {
if (pt.x == 1.0f && pt.y == 1.0f && pt.z == 1.0f) {
countPoint++;
}
}
EXPECT_EQ(countPoint, 1);
}
TEST(Util3dCorrespondencesTest, FindCorrespondencesBasicMatching)
{
std::multimap<int, cv::KeyPoint> wordsA, wordsB;
std::list<std::pair<cv::Point2f, cv::Point2f>> pairs;
// Setup wordsA: IDs 1, 2, 3 (only 2 is unique)
wordsA.insert({1, cv::KeyPoint(10.0f, 10.0f, 1)});
wordsA.insert({2, cv::KeyPoint(20.0f, 20.0f, 1)});
wordsA.insert({3, cv::KeyPoint(30.0f, 30.0f, 1)});
wordsA.insert({3, cv::KeyPoint(31.0f, 31.0f, 1)});
// Setup wordsB: IDs 2, 3 (only 2 is unique in both)
wordsB.insert({2, cv::KeyPoint(20.5f, 20.5f, 1)});
wordsB.insert({3, cv::KeyPoint(30.5f, 30.5f, 1)});
wordsB.insert({3, cv::KeyPoint(32.0f, 32.0f, 1)});
util3d::findCorrespondences(wordsA, wordsB, pairs);
ASSERT_EQ(pairs.size(), 1);
EXPECT_EQ(pairs.front().first, cv::Point2f(20.0f, 20.0f));
EXPECT_EQ(pairs.front().second, cv::Point2f(20.5f, 20.5f));
}
TEST(Util3dCorrespondencesTest, FindCorrespondencesMatchesWithDepthCheck)
{
std::multimap<int, cv::Point3f> words1, words2;
std::vector<cv::Point3f> inliers1, inliers2;
std::vector<int> correspondences;
// Insert matching and non-matching entries
words1.insert({1, cv::Point3f(1, 1, 1)});
words1.insert({2, cv::Point3f(2, 2, 2)});
words1.insert({3, cv::Point3f(100, 100, 100)}); // out of depth
words2.insert({1, cv::Point3f(1.1f, 1.1f, 1.1f)});
words2.insert({2, cv::Point3f(2.1f, 2.1f, 2.1f)});
words2.insert({3, cv::Point3f(101, 101, 101)}); // out of depth
float maxDepth = 10.0f;
util3d::findCorrespondences(words1, words2, inliers1, inliers2, maxDepth, &correspondences);
ASSERT_EQ(inliers1.size(), 2);
ASSERT_EQ(inliers2.size(), 2);
ASSERT_EQ(correspondences.size(), 2);
EXPECT_EQ(correspondences[0], 1);
EXPECT_EQ(correspondences[1], 2);
}
TEST(Util3dCorrespondencesTest, FindCorrespondencesMatchesWithMaxDepth)
{
std::map<int, cv::Point3f> words1, words2;
std::vector<cv::Point3f> inliers1, inliers2;
std::vector<int> correspondences;
words1[1] = cv::Point3f(1, 1, 1);
words1[2] = cv::Point3f(2, 2, 2);
words1[3] = cv::Point3f(100, 100, 100); // Exceeds maxDepth
words2[1] = cv::Point3f(1.2f, 1.2f, 1.2f);
words2[2] = cv::Point3f(2.2f, 2.2f, 2.2f);
words2[3] = cv::Point3f(101, 101, 101); // Exceeds maxDepth
util3d::findCorrespondences(words1, words2, inliers1, inliers2, 10.0f, &correspondences);
ASSERT_EQ(inliers1.size(), 2);
ASSERT_EQ(inliers2.size(), 2);
ASSERT_EQ(correspondences.size(), 2);
EXPECT_EQ(correspondences[0], 1);
EXPECT_EQ(correspondences[1], 2);
}