tools/GlobalBundleAdjustment/main.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <rtabmap/core/DBDriver.h>
29 #include <rtabmap/core/Rtabmap.h>
30 #include <rtabmap/core/Optimizer.h>
31 #include <rtabmap/utilite/UTimer.h>
32 #include <rtabmap/utilite/UFile.h>
33 #include <rtabmap/utilite/UStl.h>
34 
35 using namespace rtabmap;
36 
37 void showUsage()
38 {
39  printf("\nUsage:\n"
40  "rtabmap-globalBundleAdjustment database.db\n"
41  "\n%s", Parameters::showUsage());
42  exit(1);
43 }
44 
45 int main(int argc, char * argv[])
46 {
49 
50  if(argc < 2)
51  {
52  showUsage();
53  }
54 
55  for(int i=1; i<argc-1; ++i)
56  {
57  if(std::strcmp(argv[i], "--help") == 0)
58  {
59  showUsage();
60  }
61  }
62  ParametersMap inputParams = Parameters::parseArguments(argc, argv);
63 
64  std::string dbPath = argv[argc-1];
65  if(!UFile::exists(dbPath))
66  {
67  printf("Database %s doesn't exist!\n", dbPath.c_str());
68  }
69 
70  // Get parameters
71  ParametersMap parameters;
72  DBDriver * driver = DBDriver::create();
73  if(driver->openConnection(dbPath))
74  {
75  if(uStrNumCmp(driver->getDatabaseVersion(), "0.17.0")<0)
76  {
77  printf("Database is too old (%s), we cannot save back optimized poses. "
78  "Consider upgrading the database with:\n"
79  "rtabmap-reprocess --Db/TargetVersion \"\" \"%s\" \"output.db\"\n",
80  driver->getDatabaseVersion().c_str(),
81  dbPath.c_str());
82  driver->closeConnection(false);
83  delete driver;
84  return -1;
85  }
86 
87  parameters = driver->getLastParameters();
88  // This will force rtabmap_ros to regenerate the global occupancy grid if there was one
89  driver->save2DMap(cv::Mat(), 0, 0, 0);
90  driver->saveOptimizedMesh(cv::Mat());
91  driver->closeConnection(false);
92  }
93  else
94  {
95  UERROR("Cannot open database %s!", dbPath.c_str());
96  }
97  delete driver;
98 
99  for(ParametersMap::iterator iter=inputParams.begin(); iter!=inputParams.end(); ++iter)
100  {
101  printf("Added custom parameter %s=%s\n",iter->first.c_str(), iter->second.c_str());
102  }
103 
104  UTimer timer;
105 
106  printf("Loading database \"%s\"...\n", dbPath.c_str());
107  // Get the global optimized map
109  uInsert(parameters, inputParams);
110  rtabmap.init(parameters, dbPath);
111  printf("Loading database \"%s\"... done (%fs).\n", dbPath.c_str(), timer.ticks());
112 
113  std::map<int, Signature> nodes;
114  std::map<int, Transform> optimizedPoses;
115  std::multimap<int, Link> links;
116  printf("Optimizing the map...\n");
117  rtabmap.getGraph(optimizedPoses, links, true, true, &nodes, true, true, true, true);
118  printf("Optimizing the map... done (%fs, poses=%d).\n", timer.ticks(), (int)optimizedPoses.size());
119 
120  printf("Global bundle adjustment...\n");
121  Optimizer * optimizer = Optimizer::create(Optimizer::kTypeG2O, parameters);
122  optimizedPoses = optimizer->optimizeBA(optimizedPoses.lower_bound(1)->first, optimizedPoses, links, nodes, true);
123  delete optimizer;
124  printf("Global bundle adjustment... done (%fs).\n", timer.ticks());
125 
126  if(!optimizedPoses.empty())
127  {
128  rtabmap.setOptimizedPoses(optimizedPoses, links);
129  }
130  else
131  {
132  UERROR("Returned empty poses!");
133  }
134 
135  rtabmap.close();
136 
137  return 0;
138 }
rtabmap::DBDriver::getLastParameters
ParametersMap getLastParameters() const
Definition: DBDriver.cpp:239
rtabmap::Optimizer::optimizeBA
virtual std::map< int, Transform > optimizeBA(int rootId, const std::map< int, Transform > &poses, const std::multimap< int, Link > &links, const std::map< int, std::vector< CameraModel > > &models, std::map< int, cv::Point3f > &points3DMap, const std::map< int, std::map< int, FeatureBA > > &wordReferences, std::set< int > *outliers=0)
Definition: Optimizer.cpp:430
ULogger::kError
@ kError
Definition: ULogger.h:252
timer
rtabmap::DBDriver::openConnection
bool openConnection(const std::string &url, bool overwritten=false)
Definition: DBDriver.cpp:86
rtabmap::Parameters::parseArguments
static ParametersMap parseArguments(int argc, char *argv[], bool onlyParameters=false)
Definition: Parameters.cpp:599
ULogger::kTypeConsole
@ kTypeConsole
Definition: ULogger.h:244
ULogger::setLevel
static void setLevel(ULogger::Level level)
Definition: ULogger.h:339
rtabmap::Optimizer
Definition: Optimizer.h:61
rtabmap::Optimizer::create
static Optimizer * create(const ParametersMap &parameters)
Definition: Optimizer.cpp:72
rtabmap::ParametersMap
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
UTimer.h
rtabmap::DBDriver::save2DMap
void save2DMap(const cv::Mat &map, float xMin, float yMin, float cellSize) const
Definition: DBDriver.cpp:1205
uInsert
void uInsert(std::map< K, V > &map, const std::pair< K, V > &pair)
Definition: UStl.h:441
uStrNumCmp
int uStrNumCmp(const std::string &a, const std::string &b)
Definition: UStl.h:717
Rtabmap.h
Optimizer.h
rtabmap_netvlad.argv
argv
Definition: rtabmap_netvlad.py:15
rtabmap::DBDriver::getDatabaseVersion
std::string getDatabaseVersion() const
Definition: DBDriver.cpp:275
ULogger::setType
static void setType(Type type, const std::string &fileName=kDefaultLogFileName, bool append=true)
Definition: ULogger.cpp:176
DBDriver.h
rtabmap::Parameters::showUsage
static const char * showUsage()
Definition: Parameters.cpp:572
rtabmap::DBDriver
Definition: DBDriver.h:62
nodes
KeyVector nodes
showUsage
void showUsage()
Definition: tools/GlobalBundleAdjustment/main.cpp:37
main
int main(int argc, char *argv[])
Definition: tools/GlobalBundleAdjustment/main.cpp:45
iter
iterator iter(handle obj)
UStl.h
Wrappers of STL for convenient functions.
UTimer
Definition: UTimer.h:46
rtabmap::Optimizer::kTypeG2O
@ kTypeG2O
Definition: Optimizer.h:67
rtabmap::Rtabmap
Definition: Rtabmap.h:54
UFile.h
rtabmap
Definition: CameraARCore.cpp:35
UFile::exists
bool exists()
Definition: UFile.h:104
UERROR
#define UERROR(...)
i
int i
rtabmap::DBDriver::closeConnection
void closeConnection(bool save=true, const std::string &outputUrl="")
Definition: DBDriver.cpp:64
rtabmap::DBDriver::saveOptimizedMesh
void saveOptimizedMesh(const cv::Mat &cloud, const std::vector< std::vector< std::vector< RTABMAP_PCL_INDEX > > > &polygons=std::vector< std::vector< std::vector< RTABMAP_PCL_INDEX > > >(), const std::vector< std::vector< Eigen::Vector2f > > &texCoords=std::vector< std::vector< Eigen::Vector2f > >(), const cv::Mat &textures=cv::Mat()) const
Definition: DBDriver.cpp:1220
rtabmap::DBDriver::create
static DBDriver * create(const ParametersMap &parameters=ParametersMap())
Definition: DBDriver.cpp:41


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:12