-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
208 lines (166 loc) · 5.31 KB
/
parser.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
#include <string>
#include <sstream>
#include <array>
#include <unistd.h>
#include <boost/filesystem/operations.hpp>
#include <GrabData.hpp>
#include <StampedGPSPose.hpp>
#include <StringHelper.hpp>
void
prepare_all_directories()
{
// remove the directory contents recursively, and then removes the directory.
boost::filesystem::remove_all("/dados/tmp");
boost::filesystem::create_directory("/dados/tmp");
boost::filesystem::create_directory("/dados/tmp/sick");
boost::filesystem::create_directory("/dados/tmp/velodyne");
boost::filesystem::create_directory("/dados/tmp/lgm");
boost::filesystem::create_directory("/dados/tmp/lgm/sick");
boost::filesystem::create_directory("/dados/tmp/lgm/velodyne");
boost::filesystem::create_directory("/dados/tmp/images");
boost::filesystem::create_directory("/dados/tmp/loops");
boost::filesystem::create_directory("/dados/tmp/loops/sick");
boost::filesystem::create_directory("/dados/tmp/loops/velodyne");
std::cout << "Necessary directories created." << std::endl;
}
std::vector<std::array<std::string, 3>> log_list_parser(std::string log_list_filename)
{
std::string carmen_home(getenv("CARMEN_HOME"));
std::string carmen_src_folder(carmen_home + "/src/");
std::string config_folder(carmen_src_folder + "hypergraphsclam/config/");
std::vector<std::array<std::string, 3>> log_list;
std::ifstream file(log_list_filename);
if (file.is_open())
{
std::stringstream current_line;
while(-1 != hyper::StringHelper::ReadLine(file, current_line))
{
std::string log;
std::string parser_config_file;
std::string carmen_ini;
current_line >> log;
current_line >> parser_config_file;
current_line >> carmen_ini;
if (log.empty() || parser_config_file.empty() || carmen_ini.empty())
{
std::cerr << "Invalid line! Take a look in your input file!" << std::endl;
}
else
{
log_list.emplace_back(std::array<std::string, 3> { log, config_folder + parser_config_file, carmen_src_folder + carmen_ini });
}
}
}
else
{
std::cerr << "Unable to open the log list file: " << log_list_filename << "\n";
}
return log_list;
}
bool
parse_logs(std::vector<hyper::GrabData> &gds, std::vector<std::array<std::string, 3>> &logs)
{
unsigned gid = 1;
unsigned last_id = 6;
for (std::array<std::string, 3> &input_files : logs)
{
gds.emplace_back(hyper::GrabData(gid++));
hyper::GrabData &gd = gds.back();
gd.Configure(input_files[1], input_files[2]);
last_id = gd.ParseLogFile(input_files[0], last_id);
if (0 < last_id)
{
gd.BuildHyperGraph();
}
else
{
std::cerr << "Could not parse the log file: " << input_files[0] << std::endl;
return false;
}
}
return true;
}
void
build_loop_closures(std::vector<hyper::GrabData> &gds, double external_loop_min_distance, double external_loop_required_distance)
{
for (unsigned i = 0; i < gds.size(); ++i)
for (unsigned j = i + 1; j < gds.size(); ++j)
gds[i].BuildExternalLoopClosures(gds[j], external_loop_min_distance, external_loop_required_distance);
}
void
save_separated_graph(hyper::GrabData &gd, unsigned b)
{
std::stringstream ss;
std::string base;
ss << b;
ss >> base;
base += "_";
std::ofstream sync(base + "sync.txt");;
if (!sync.is_open())
{
std::cerr << "Could not open the separated sync.txt output file!" << std::endl;
return;
}
gd.SaveHyperGraph(sync, false);
std::cout << "The base: " << base << std::endl;
gd.SaveEstimates(base);
}
void
save_hyper_graphs(std::vector<hyper::GrabData> &gds)
{
std::ofstream sync("sync.txt");
if (!sync.is_open())
{
std::cerr << "Could not open the sync.txt output file!" << std::endl;
return;
}
unsigned b = 1;
for (hyper::GrabData &gd : gds)
{
// save the hyper graph in a global file
gd.SaveHyperGraph(sync, true);
save_separated_graph(gd, b++);
}
for (hyper::GrabData &gd : gds)
{
gd.SaveExternalLidarLoopEdges(sync);
}
sync.close();
}
int
main (int argc, char **argv)
{
if (2 > argc)
{
std::cout << "Usage: ./parser <log_list_file_path> [external_loop_min_distance] [external_loop_required_distance]" << std::endl;
return -1;
}
prepare_all_directories();
double external_loop_min_distance = 1.0f;
double external_loop_required_distance = 5.0f;
if (2 < argc)
{
std::string elmd(argv[2]);
std::stringstream ss;
ss << elmd;
ss >> external_loop_min_distance;
}
if (3 < argc)
{
std::string eld(argv[3]);
std::stringstream ss;
ss << eld;
ss >> external_loop_required_distance;
}
std::vector<hyper::GrabData> gds(0);
std::vector<std::array<std::string, 3>> logs(log_list_parser(argv[1]));
if (parse_logs(gds, logs))
{
build_loop_closures(gds, external_loop_min_distance, external_loop_required_distance);
save_hyper_graphs(gds);
}
for (hyper::GrabData &gd : gds)
gd.Clear();
std::cout << "Done!\n";
return 0;
}