Files
rtabmap/corelib/test/test_dbdriversqlite3.cpp

444 lines
14 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/DBDriver.h>
#include <rtabmap/core/DBDriverSqlite3.h>
#include <rtabmap/core/Signature.h>
#include <rtabmap/core/Parameters.h>
#include <rtabmap/core/DBReader.h>
#include <rtabmap/core/SensorCaptureInfo.h>
#include <rtabmap/core/CameraModel.h>
#include <rtabmap/core/LaserScan.h>
#include <rtabmap/core/Compression.h>
#include <rtabmap/core/Link.h>
#include <rtabmap/core/Link.h>
#include <opencv2/core.hpp>
#include <memory>
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 <rtabmap/utilite/UFile.h>
#include <rtabmap/utilite/UConversion.h>
#include "TestUtils.h"
using namespace rtabmap;
namespace {
std::string uniqueDbPath()
{
static int counter = 0;
return test::tempPath(uFormat("rtabmap_dbdriversqlite3_test_%d_%d.db", test::getPid(), ++counter));
}
class DBDriverSqlite3Fixture : public ::testing::Test
{
protected:
void SetUp() override
{
dbPath_ = uniqueDbPath();
driver_ = new DBDriverSqlite3();
ASSERT_TRUE(driver_->openConnection(dbPath_, true));
}
void TearDown() override
{
if(driver_)
{
driver_->closeConnection(false);
delete driver_;
driver_ = nullptr;
}
if(!dbPath_.empty())
{
UFile::erase(dbPath_.c_str());
}
}
void saveSignature(Signature * s)
{
driver_->asyncSave(s);
driver_->emptyTrashes(false);
}
std::string dbPath_;
DBDriverSqlite3 * driver_ = nullptr;
};
} // namespace
TEST(DBDriverSqlite3Test, CreateFactoryReturnsSqliteDriver)
{
DBDriver * driver = DBDriver::create();
ASSERT_NE(driver, nullptr);
EXPECT_NE(dynamic_cast<DBDriverSqlite3 *>(driver), nullptr);
driver->closeConnection(false);
delete driver;
}
TEST(DBDriverSqlite3Test, EmptyUrlIsInMemory)
{
DBDriverSqlite3 driver;
ASSERT_TRUE(driver.openConnection(""));
EXPECT_TRUE(driver.isInMemory());
EXPECT_TRUE(driver.isConnected());
EXPECT_FALSE(driver.getDatabaseVersion().empty());
driver.closeConnection(false);
}
TEST(DBDriverSqlite3Test, FileBackedIsNotInMemory)
{
const std::string path = uniqueDbPath();
DBDriverSqlite3 driver;
ASSERT_TRUE(driver.openConnection(path, true));
EXPECT_FALSE(driver.isInMemory());
EXPECT_EQ(driver.getUrl(), path);
driver.closeConnection(false);
UFile::erase(path.c_str());
}
TEST(DBDriverSqlite3Test, ParseParametersEnablesInMemory)
{
const std::string path = uniqueDbPath();
ParametersMap params;
params.insert(ParametersPair(Parameters::kDbSqlite3InMemory(), "true"));
DBDriverSqlite3 driver(params);
ASSERT_TRUE(driver.openConnection(path, true));
EXPECT_TRUE(driver.isInMemory());
EXPECT_TRUE(driver.isConnected());
driver.asyncSave(new Signature(1));
driver.emptyTrashes(false);
EXPECT_EQ(driver.getTotalNodesSize(), 1);
driver.closeConnection(false);
UFile::erase(path.c_str());
}
TEST(DBDriverSqlite3Test, InMemorySaveToFileOnClose)
{
const std::string path = uniqueDbPath();
ParametersMap params;
params.insert(ParametersPair(Parameters::kDbSqlite3InMemory(), "true"));
DBDriverSqlite3 driver(params);
ASSERT_TRUE(driver.openConnection(path, true));
EXPECT_TRUE(driver.isInMemory());
driver.asyncSave(new Signature(1, 5, 1, 50.0, "sqlite_mem", Transform(1.f, 0.f, 0.f, 0.f, 0.f, 0.f)));
driver.emptyTrashes(false);
driver.closeConnection(true, path);
DBDriverSqlite3 driver2;
ASSERT_TRUE(driver2.openConnection(path));
EXPECT_FALSE(driver2.isInMemory());
Signature * loaded = driver2.loadSignature(1);
ASSERT_NE(loaded, nullptr);
EXPECT_EQ(loaded->getLabel(), "sqlite_mem");
delete loaded;
driver2.closeConnection(false);
UFile::erase(path.c_str());
}
TEST_F(DBDriverSqlite3Fixture, SetPragmasWhileConnected)
{
EXPECT_NO_THROW(driver_->setCacheSize(4000));
EXPECT_NO_THROW(driver_->setJournalMode(1));
EXPECT_NO_THROW(driver_->setSynchronous(1));
EXPECT_NO_THROW(driver_->setTempStore(2));
EXPECT_TRUE(driver_->isConnected());
}
TEST_F(DBDriverSqlite3Fixture, SaveAndLoadSignature)
{
saveSignature(new Signature(1, 5, 7, 100.0, "node1", Transform(1.f, 2.f, 3.f, 0.f, 0.f, 0.f)));
EXPECT_EQ(driver_->getTotalNodesSize(), 1);
Signature * loaded = driver_->loadSignature(1);
ASSERT_NE(loaded, nullptr);
EXPECT_EQ(loaded->id(), 1);
EXPECT_EQ(loaded->getLabel(), "node1");
EXPECT_EQ(loaded->getWeight(), 7);
delete loaded;
}
TEST_F(DBDriverSqlite3Fixture, ReopenPreservesData)
{
saveSignature(new Signature(1, 5, 3, 50.0, "persist", Transform(0.5f, 0.f, 0.f, 0.f, 0.f, 0.f)));
const std::string path = dbPath_;
driver_->closeConnection(true);
delete driver_;
driver_ = nullptr;
driver_ = new DBDriverSqlite3();
ASSERT_TRUE(driver_->openConnection(path));
Signature * loaded = driver_->loadSignature(1);
ASSERT_NE(loaded, nullptr);
EXPECT_EQ(loaded->getLabel(), "persist");
delete loaded;
}
TEST_F(DBDriverSqlite3Fixture, ExecuteNoResultPragma)
{
EXPECT_NO_THROW(driver_->executeNoResult("PRAGMA cache_size=8000;"));
EXPECT_TRUE(driver_->isConnected());
}
// ---------------------------------------------------------------------------
// Full write/read round trip: build a small database with rich sensor data,
// then replay it with DBReader.
//
// The tests above save bare Signatures, which exercises the schema but not the
// payload paths (compressed image/depth/scan blobs, calibration, links) nor
// DBReader at all -- until now those only ran during the end-to-end replay
// tests, which need the fetched sample databases.
// ---------------------------------------------------------------------------
namespace {
// A frame carrying everything the payload paths serialise: RGB, depth,
// calibration and a laser scan.
SensorData makeRichData(int id, double stamp)
{
cv::Mat rgb(60, 80, CV_8UC3);
cv::randu(rgb, cv::Scalar::all(0), cv::Scalar::all(255));
cv::Mat depth(60, 80, CV_16UC1);
cv::randu(depth, cv::Scalar::all(500), cv::Scalar::all(4000));
const CameraModel model(100.0, 100.0, 40.0, 30.0, Transform::getIdentity(), 0.0, cv::Size(80, 60));
cv::Mat scanData(1, 50, CV_32FC3);
for(int i = 0; i < 50; ++i)
{
scanData.at<cv::Vec3f>(0, i) = cv::Vec3f(0.01f * i, 0.02f * i, 0.0f);
}
// The driver persists the *compressed* buffers, so compress up front the
// way Memory does before saving; raw-only data would be written as empty
// blobs. setRGBDImage()/setLaserScan() detect a 1-row CV_8UC1 as compressed.
SensorData data;
data.setId(id);
data.setStamp(stamp);
data.setRGBDImage(compressImage2(rgb, ".png"), compressImage2(depth, ".png"), model);
data.setLaserScan(LaserScan(compressData2(scanData), /*maxPoints=*/0, /*maxRange=*/0.0f,
LaserScan::kXYZ));
return data;
}
} // namespace
TEST_F(DBDriverSqlite3Fixture, SavesAndLoadsRichSensorData)
{
const Transform pose(1.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.3f);
Signature * s = new Signature(10, 0, 1, 1.5, "node10", pose, Transform(), makeRichData(10, 1.5));
saveSignature(s);
std::list<Signature *> loaded;
driver_->loadSignatures(std::list<int>(1, 10), loaded);
ASSERT_EQ(1u, loaded.size());
Signature * back = loaded.front();
EXPECT_EQ(10, back->id());
EXPECT_EQ(0, back->mapId());
EXPECT_DOUBLE_EQ(1.5, back->getStamp());
EXPECT_EQ("node10", back->getLabel());
EXPECT_LT(back->getPose().getDistance(pose), 1e-4f);
// Payloads come back compressed; ask the driver to fill them in.
std::list<Signature *> toFill(1, back);
driver_->loadNodeData(toFill);
back->sensorData().uncompressData();
EXPECT_FALSE(back->sensorData().imageRaw().empty()) << "image blob did not round-trip";
EXPECT_FALSE(back->sensorData().depthRaw().empty()) << "depth blob did not round-trip";
EXPECT_FALSE(back->sensorData().laserScanRaw().isEmpty()) << "scan blob did not round-trip";
EXPECT_EQ(1u, back->sensorData().cameraModels().size());
for(std::list<Signature *>::iterator iter = loaded.begin(); iter != loaded.end(); ++iter)
{
delete *iter;
}
}
TEST_F(DBDriverSqlite3Fixture, DBReaderReplaysWrittenDatabase)
{
// Three consecutive nodes with odometry poses and neighbour links, i.e.
// the minimum a recorded session needs to be replayable.
const int kNodes = 3;
for(int i = 1; i <= kNodes; ++i)
{
const Transform pose(0.5f * i, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
Signature * s = new Signature(i, 0, 1, static_cast<double>(i), "", pose, Transform(),
makeRichData(i, static_cast<double>(i)));
if(i > 1)
{
const Transform motion(0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
s->addLink(Link(i, i - 1, Link::kNeighbor, motion.inverse()));
}
saveSignature(s);
}
// Close so the file is complete before DBReader opens it.
driver_->closeConnection(true);
delete driver_;
driver_ = nullptr;
std::unique_ptr<DBReader> reader(new DBReader(dbPath_, /*frameRate=*/0.0f,
/*odometryIgnored=*/false));
ASSERT_TRUE(reader->init()) << "DBReader could not open " << dbPath_;
int frames = 0;
Transform previousPose;
while(frames < kNodes + 1)
{
SensorCaptureInfo info;
SensorData data = reader->takeData(&info);
if(data.id() == 0 && data.imageRaw().empty() && data.imageCompressed().empty())
{
break; // end of database
}
++frames;
EXPECT_FALSE(info.odomPose.isNull()) << "frame " << data.id() << " has no odometry pose";
if(!previousPose.isNull() && !info.odomPose.isNull())
{
// Poses were written 50 cm apart along x.
EXPECT_NEAR(previousPose.getDistance(info.odomPose), 0.5f, 1e-3f);
}
previousPose = info.odomPose;
}
EXPECT_EQ(kNodes, frames) << "DBReader returned " << frames << " of " << kNodes << " nodes";
}
// Only compressed buffers are persisted, so raw-only sensor data is stored with
// empty payloads (documented on DBDriver::asyncSave()). Pinned here because it
// is silent from the caller's point of view -- see makeRichData() above, which
// compresses first.
TEST_F(DBDriverSqlite3Fixture, RawOnlySensorDataIsNotPersisted)
{
cv::Mat rgb(20, 20, CV_8UC3, cv::Scalar(10, 20, 30));
const CameraModel model(100.0, 100.0, 10.0, 10.0, Transform::getIdentity(), 0.0, cv::Size(20, 20));
SensorData raw(rgb, cv::Mat(), model, 42, 1.0); // raw, never compressed
ASSERT_FALSE(raw.imageRaw().empty());
ASSERT_TRUE(raw.imageCompressed().empty());
saveSignature(new Signature(42, 0, 1, 1.0, "", Transform::getIdentity(), Transform(), raw));
std::list<Signature *> loaded;
driver_->loadSignatures(std::list<int>(1, 42), loaded);
ASSERT_EQ(1u, loaded.size());
driver_->loadNodeData(loaded);
loaded.front()->sensorData().uncompressData();
EXPECT_TRUE(loaded.front()->sensorData().imageRaw().empty())
<< "raw-only image unexpectedly survived a save/load round trip";
delete loaded.front();
}
// ---------------------------------------------------------------------------
// Legacy schema round trips. Db/TargetVersion makes the driver create an older
// schema, which is how a database recorded by an earlier rtabmap looks. The
// readers for those layouts differ substantially -- e.g. link covariance was
// stored as separate rotVariance/transVariance columns before the full
// information matrix -- and none of it ran in the tests until now.
// ---------------------------------------------------------------------------
namespace {
class DBSchemaVersionTest : public ::testing::TestWithParam<const char *>
{
protected:
void SetUp() override
{
dbPath_ = uniqueDbPath();
ParametersMap parameters;
parameters.insert(ParametersPair(Parameters::kDbTargetVersion(), GetParam()));
driver_ = new DBDriverSqlite3(parameters);
ASSERT_TRUE(driver_->openConnection(dbPath_, true))
<< "could not create a " << GetParam() << " database";
}
void TearDown() override
{
if(driver_)
{
driver_->closeConnection(false);
delete driver_;
driver_ = nullptr;
}
UFile::erase(dbPath_.c_str());
}
void saveSignature(Signature * s)
{
driver_->asyncSave(s);
driver_->emptyTrashes(false);
}
std::string dbPath_;
DBDriverSqlite3 * driver_ = nullptr;
};
} // namespace
TEST_P(DBSchemaVersionTest, NodesAndLinksSurviveARoundTrip)
{
// Two nodes joined by a neighbour link carrying a non-default information
// matrix: older schemas store that as rotVariance/transVariance, newer ones
// as the full 6x6, so this exercises whichever reader the version needs.
cv::Mat info = cv::Mat::eye(6, 6, CV_64FC1);
info.at<double>(0,0) = info.at<double>(1,1) = info.at<double>(2,2) = 4.0; // 1/transVariance
info.at<double>(3,3) = info.at<double>(4,4) = info.at<double>(5,5) = 100.0; // 1/rotVariance
const Transform motion(0.5f, 0.1f, 0.0f, 0.0f, 0.0f, 0.2f);
for(int id = 1; id <= 2; ++id)
{
const Transform pose(0.5f * (id - 1), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
Signature * s = new Signature(id, 0, 1, static_cast<double>(id), "", pose, Transform(),
makeRichData(id, static_cast<double>(id)));
if(id == 2)
{
s->addLink(Link(2, 1, Link::kNeighbor, motion.inverse(), info));
}
saveSignature(s);
}
EXPECT_FALSE(driver_->getDatabaseVersion().empty());
std::list<Signature *> loaded;
driver_->loadSignatures(std::list<int>{1, 2}, loaded);
ASSERT_EQ(2u, loaded.size()) << "nodes did not survive the round trip";
// Payloads
driver_->loadNodeData(loaded);
for(Signature * s : loaded)
{
s->sensorData().uncompressData();
EXPECT_FALSE(s->sensorData().imageRaw().empty()) << "node " << s->id() << " lost its image";
EXPECT_EQ(1u, s->sensorData().cameraModels().size());
}
// Links: the second node must still point back at the first, with the
// variances recovered from whatever columns this schema uses.
std::multimap<int, Link> links;
driver_->loadLinks(2, links);
ASSERT_FALSE(links.empty()) << "link did not survive the round trip";
const Link & link = links.begin()->second;
EXPECT_EQ(1, link.to());
EXPECT_LT(link.transform().getDistance(motion.inverse()), 1e-3f);
EXPECT_NEAR(4.0, link.infMatrix().at<double>(0,0), 1e-6) << "translational variance lost";
EXPECT_NEAR(100.0, link.infMatrix().at<double>(3,3), 1e-6) << "rotational variance lost";
for(Signature * s : loaded)
{
delete s;
}
}
INSTANTIATE_TEST_SUITE_P(
Schemas,
DBSchemaVersionTest,
::testing::Values("0.16", "0.17", "0.18", "0.20", "0.22"),
[](const ::testing::TestParamInfo<const char *> & info) {
std::string name(info.param);
for(size_t i = 0; i < name.size(); ++i)
{
if(!isalnum(name[i])) name[i] = '_';
}
return "v" + name;
});