-
Notifications
You must be signed in to change notification settings - Fork 634
/
graph-slam_main.cpp
373 lines (313 loc) · 13.1 KB
/
graph-slam_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2024, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
/*---------------------------------------------------------------
APPLICATION: Graph-SLAM hub application
PURPOSE: Visualization of .graph files and evaluation of
Graph-SLAM methods.
See the "--help" output for list of supported operations and
further instructions, the man-page or online docs at:
https://www.mrpt.org/list-of-mrpt-apps/application-graph-slam
---------------------------------------------------------------*/
#include <mrpt/3rdparty/tclap/CmdLine.h>
#include <mrpt/graphs.h>
#include <mrpt/graphslam/levmarq.h>
#include <mrpt/system/datetime.h>
using namespace mrpt;
using namespace mrpt::graphslam;
using namespace mrpt::graphs;
using namespace mrpt::poses;
using namespace mrpt::opengl;
using namespace mrpt::system;
using namespace mrpt::math;
using namespace std;
// Declarations:
#define VERBOSE_COUT \
if (verbose) std::cout << "[graph-slam] "
#define DECLARE_OP_FUNCTION(_NAME) \
void _NAME(const std::string& in_file, bool is3D, TCLAP::CmdLine& cmdline, bool verbose)
#define IMPLEMENT_OP_FUNCTION(_NAME) \
template <class GRAPHTYPE> \
void _NAME##impl(const std::string& in_file, bool is3D, TCLAP::CmdLine& cmdline, bool verbose); \
void _NAME(const std::string& in_file, bool is3D, TCLAP::CmdLine& cmdline, bool verbose) \
{ \
if (!is3D) \
_NAME##impl<CNetworkOfPoses2D>(in_file, is3D, cmdline, verbose); \
else \
_NAME##impl<CNetworkOfPoses3D>(in_file, is3D, cmdline, verbose); \
} \
template <class GRAPHTYPE> \
void _NAME##impl(const std::string& in_file, bool is3D, TCLAP::CmdLine& cmdline, bool verbose)
/**
* http://stackoverflow.com/questions/3982470/what-does-typedef-void-something-mean
* Define a pointer-to-function type "TOperationFunctor". TOperationFunctor
* returns void and takes 4 input parameters as explained in the latter
* parentheses
*/
using TOperationFunctor =
void (*)(const std::string& in_file, bool is3D, TCLAP::CmdLine& cmdline, bool verbose);
/** Forward declaration.
* display_graph function template provided in display_graph.cpp
*/
template <class GRAPHTYPE>
void display_graph(const GRAPHTYPE& g);
DECLARE_OP_FUNCTION(op_view);
DECLARE_OP_FUNCTION(op_dijkstra);
DECLARE_OP_FUNCTION(op_levmarq);
DECLARE_OP_FUNCTION(op_info);
// Declare the supported command line switches ===========
TCLAP::CmdLine cmd("graph-slam", ' ', MRPT_getVersion().c_str());
TCLAP::ValueArg<std::string> arg_input_file(
"i", "input", "Input file (required) (*.graph,*.graphbin)", true, "", "test.graph", cmd);
TCLAP::ValueArg<std::string> arg_output_file(
"o", "output", "Output file (optional) (*.graph,*.graphbin)", false, "", "result.graph", cmd);
TCLAP::SwitchArg arg_2d(
"", "2d", "Use 2D poses (Must use exactly one of --2d and --3d)", cmd, false);
TCLAP::SwitchArg arg_3d(
"", "3d", "Use 3D poses (Must use exactly one of --2d and --3d)", cmd, false);
TCLAP::SwitchArg arg_view(
"",
"view",
"Op: Visualize the graph in a 3D view. If used alone, represent VERTEX2 or "
"VERTEX3 poses directly as stored in the input file. "
"If used together with another operation, the final obtained graph after "
"the operation will be shown, not the input original one.\n",
cmd,
false);
TCLAP::SwitchArg arg_quiet("q", "quiet", "Terse output", cmd, false);
TCLAP::ValueArg<int> arg_max_iters(
"", "max-iters", "Maximum number of iterations (optional)", false, 100, "N", cmd);
TCLAP::ValueArg<double> arg_initial_lambda(
"", "initial-lambda", "Initial lambda parameter (optional, lev-marq)", false, 0, "val", cmd);
TCLAP::SwitchArg arg_no_span(
"", "no-span", "Don't use dijkstra initial spanning tree guess (optional)", cmd, false);
// ======================================================================
// main() of graph-slam
// ======================================================================
int main(int argc, char** argv)
{
vector<std::unique_ptr<TCLAP::Arg>> arg_ops;
int ret_val = 0;
try
{
// --------------- List of possible operations ---------------
// Only one of the following operators have to be specified by the user
map<string, TOperationFunctor> ops_functors;
arg_ops.push_back(std::make_unique<TCLAP::SwitchArg>(
"", "levmarq",
"Op: Optimizes the graph with sparse Levenberg-Marquartd using "
"global coordinates (via "
"mrpt::graphslam::optimize_graph_spa_levmarq).\n"
" Can be used together with: --view, --output, --max-iters, "
"--no-span, --initial-lambda",
cmd, false));
ops_functors["levmarq"] = &op_levmarq;
arg_ops.push_back(std::make_unique<TCLAP::SwitchArg>(
"", "dijkstra",
"Op: Executes CNetworkOfPoses::dijkstra_nodes_estimate() to "
"estimate the global pose of nodes from a Dijkstra tree and "
"the edge relative poses.\n"
" Can be used together with: --view, --output",
cmd, false));
ops_functors["dijkstra"] = &op_dijkstra;
arg_ops.push_back(std::make_unique<TCLAP::SwitchArg>(
"", "info",
"Op: Loads the graph and displays statistics and information "
"on it.\n",
cmd, false));
ops_functors["info"] = &op_info;
// --------------- End of list of possible operations --------
// Parse arguments:
if (!cmd.parse(argc, argv)) throw std::runtime_error(""); // should exit.
// Exactly 1 or --2d & --3d must be specified:
if ((arg_2d.isSet() && arg_3d.isSet()) || (!arg_2d.isSet() && !arg_3d.isSet()))
throw std::runtime_error("Exactly one --2d or --3d must be used.");
const bool is3d = arg_3d.isSet();
string input_file = arg_input_file.getValue();
const bool verbose = !arg_quiet.getValue();
// Check the selected operation:
// Only one of the ops should be selected:
string selected_op;
for (auto& arg_op : arg_ops)
{
if (arg_op->isSet())
{
if (selected_op.empty())
{
selected_op = arg_op->getName();
}
else
throw std::runtime_error(
"Exactly one operation must be indicated on command "
"line.\n"
"Use --help to see the list of possible operations.");
}
}
// The "--view" argument needs a bit special treatment:
if (selected_op.empty())
{
if (!arg_view.isSet())
throw std::runtime_error(
"Don't know what to do: No operation was indicated.\n"
"Use --help to see the list of possible operations.");
else
{
VERBOSE_COUT << "Operation to perform: "
"view"
<< endl;
op_view(input_file, is3d, cmd, verbose);
}
}
else
{
VERBOSE_COUT << "Operation to perform: " << selected_op << endl;
// ------------------------------------
// EXECUTE THE REQUESTED OPERATION
// ------------------------------------
ASSERTMSG_(
ops_functors.find(selected_op) != ops_functors.end(),
"Internal error: Unknown operation functor!");
// Call the selected functor:
ops_functors[selected_op](input_file, is3d, cmd, verbose);
}
// successful end of program.
ret_val = 0;
}
catch (const std::exception& e)
{
std::cerr << mrpt::exception_to_str(e) << std::endl;
ret_val = -1;
}
// end:
return ret_val;
}
// ===================================================================================
// Implementation of the functions operations
// ===================================================================================
// -----------------------------------------------------------------------------------
// op: --view
// Load and visualize the graph
// -----------------------------------------------------------------------------------
IMPLEMENT_OP_FUNCTION(op_view)
{
// Load:
GRAPHTYPE g;
g.loadFromTextFile(in_file);
// Display:
display_graph(g);
}
// -----------------------------------------------------------------------------------
// op: --info
// Load and visualize basic info on graph
// -----------------------------------------------------------------------------------
IMPLEMENT_OP_FUNCTION(op_info)
{
// Load:
GRAPHTYPE g;
g.loadFromTextFile(in_file);
set<TNodeID> lstNodeIDs;
g.getAllNodes(lstNodeIDs);
// Show graph stats:
cout << "Edge count : " << g.edgeCount() << endl;
cout << "Nodes count (in VERTEX2/3 entries) : " << g.nodes.size() << endl;
cout << "Nodes count (in edge entries) : " << lstNodeIDs.size() << endl;
}
// -----------------------------------------------------------------------------------
// op: --dijkstra
// Load and visualize the graph
// -----------------------------------------------------------------------------------
IMPLEMENT_OP_FUNCTION(op_dijkstra)
{
const bool save_to_file = arg_output_file.isSet(); // Output to file??
const bool display_3D = arg_view.isSet(); // Output to 3D view??
if (!save_to_file && !display_3D)
std::cerr << "\n ** WARNING **: Neither --view nor --output specified. "
"The result will not be saved anywhere.\n\n";
// Load:
GRAPHTYPE g;
g.loadFromTextFile(in_file);
// Find the first node ID and use it as root:
VERBOSE_COUT << "Making a list with all node IDs...\n";
set<TNodeID> lstNodeIDs;
g.getAllNodes(lstNodeIDs);
ASSERT_(!lstNodeIDs.empty());
const TNodeID id_root = *lstNodeIDs.begin();
VERBOSE_COUT << "Using root node ID=" << id_root << endl;
g.root = id_root;
VERBOSE_COUT << "Executing Dijkstra...\n";
CTicTac tictac;
tictac.Tic();
g.dijkstra_nodes_estimate();
VERBOSE_COUT << "Took: " << mrpt::system::formatTimeInterval(tictac.Tac()) << endl;
if (save_to_file)
{
const string out_file = arg_output_file.getValue();
VERBOSE_COUT << "Saving resulting graph to: " << out_file << endl;
g.saveToTextFile(out_file);
}
if (display_3D)
{
VERBOSE_COUT << "Displaying resulting graph\n";
display_graph(g);
}
} // end op_dijkstra
// -----------------------------------------------------------------------------------
// op: --levmarq
// Load and optimize a graph with the sparse Lev-Marq algorithm.
// -----------------------------------------------------------------------------------
IMPLEMENT_OP_FUNCTION(op_levmarq)
{
const bool save_to_file = arg_output_file.isSet(); // Output to file??
const bool display_3D = arg_view.isSet(); // Output to 3D view??
const bool skip_dijkstra = arg_no_span.isSet();
if (!save_to_file && !display_3D)
std::cerr << "\n ** WARNING **: Neither --view nor --output specified. "
"The result will not be saved anywhere.\n\n";
// Load:
GRAPHTYPE g;
g.loadFromTextFile(in_file);
// Find the first node ID and use it as root:
VERBOSE_COUT << "Making a list with all node IDs...\n";
set<TNodeID> lstNodeIDs;
g.getAllNodes(lstNodeIDs);
ASSERT_(!lstNodeIDs.empty());
const TNodeID id_root = *lstNodeIDs.begin();
VERBOSE_COUT << "Using root node ID=" << id_root << endl;
g.root = id_root;
// Executes dijkstra (spanning tree) for initial guess?
// --------------------------------
if (!skip_dijkstra)
{
VERBOSE_COUT << "Executing Dijkstra...\n";
CTicTac tictac;
tictac.Tic();
g.dijkstra_nodes_estimate();
VERBOSE_COUT << "Took: " << mrpt::system::formatTimeInterval(tictac.Tac()) << endl;
}
// Executes the optimization:
// --------------------------------
mrpt::containers::yaml params;
params["verbose"] = verbose;
params["profiler"] = verbose;
params["max_iterations"] = arg_max_iters.getValue();
params["initial_lambda"] = arg_initial_lambda.getValue();
graphslam::TResultInfoSpaLevMarq info;
graphslam::optimize_graph_spa_levmarq(g, info, nullptr, params);
// Output:
if (save_to_file)
{
const string out_file = arg_output_file.getValue();
VERBOSE_COUT << "Saving resulting graph to: " << out_file << endl;
g.saveToTextFile(out_file);
}
if (display_3D)
{
VERBOSE_COUT << "Displaying resulting graph\n";
display_graph(g);
}
} // end op_levmarq