forked from VladyslavUsenko/basalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarg_data_io.cpp
296 lines (234 loc) · 9.37 KB
/
marg_data_io.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
/**
BSD 3-Clause License
This file is part of the Basalt project.
https://gitlab.com/VladyslavUsenko/basalt.git
Copyright (c) 2019, Vladyslav Usenko and Nikolaus Demmel.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <basalt/io/marg_data_io.h>
#include <basalt/serialization/headers_serialization.h>
#include <basalt/utils/filesystem.h>
namespace basalt {
MargDataSaver::MargDataSaver(const std::string& marg_data_path, const std::string& keyframe_data_path, const basalt::Calibration<double> calib) {
std::string img_path = marg_data_path + "/images/";
if(!marg_data_path.empty())
{
fs::remove_all(marg_data_path);
fs::create_directory(marg_data_path);
fs::create_directory(img_path);
}
if(!keyframe_data_path.empty())
{
fs::remove_all(keyframe_data_path);
fs::create_directory(keyframe_data_path);
}
save_image_queue.set_capacity(300);
in_marg_queue.set_capacity(1000);
auto save_func = [&, marg_data_path, keyframe_data_path, calib]() {
basalt::MargData::Ptr data;
std::unordered_set<int64_t> processed_opt_flow;
std::string poses_path = keyframe_data_path + "/poses/";
std::string keypoints_path = keyframe_data_path + "/keypoints/";
if(!keyframe_data_path.empty()){
fs::create_directory(poses_path);
fs::create_directory(keypoints_path);
}
const Sophus::SE3d T_imu_cam = calib.T_i_c[0];
std::string poses_cam_path = poses_path + "keyframeTrajectory_cam.txt";
std::string poses_imu_path = poses_path + "keyframeTrajectory_imu.txt";
std::ofstream fw_poses_cam(poses_cam_path, std::ofstream::out);
std::ofstream fw_poses_imu(poses_imu_path, std::ofstream::out);
while (true) {
in_marg_queue.pop(data);
if (data.get()) {
int64_t kf_id = *data->kfs_to_marg.begin();
if(!marg_data_path.empty())
{
std::string p = marg_data_path + "/" + std::to_string(kf_id) + ".cereal";
std::ofstream os(p, std::ios::binary);
{
cereal::BinaryOutputArchive archive(os);
archive(*data);
}
os.close();
for (const auto& d : data->opt_flow_res) {
if (processed_opt_flow.count(d->t_ns) == 0) {
save_image_queue.push(d);
processed_opt_flow.emplace(d->t_ns);
}
}
}
if(!keyframe_data_path.empty()){
// save poses in imu frame and cam frame
for(auto it=data->frame_poses.cbegin(); it!=data->frame_poses.cend(); ++it){
if(it->first == kf_id){
const Sophus::SE3d keyframePose_imu = it->second.getPoseLin();
const Sophus::SE3d keyframePose_cam = keyframePose_imu * T_imu_cam;
fw_poses_imu << std::scientific << std::setprecision(18) << kf_id << " "
<< keyframePose_imu.translation().x() << " " << keyframePose_imu.translation().y() << " "
<< keyframePose_imu.translation().z() << " " << keyframePose_imu.unit_quaternion().x() << " "
<< keyframePose_imu.unit_quaternion().y() << " " << keyframePose_imu.unit_quaternion().z()
<< " " << keyframePose_imu.unit_quaternion().w() << std::endl;
fw_poses_cam << std::scientific << std::setprecision(18) << kf_id << " "
<< keyframePose_cam.translation().x() << " " << keyframePose_cam.translation().y() << " "
<< keyframePose_cam.translation().z() << " " << keyframePose_cam.unit_quaternion().x() << " "
<< keyframePose_cam.unit_quaternion().y() << " " << keyframePose_cam.unit_quaternion().z()
<< " " << keyframePose_cam.unit_quaternion().w() << std::endl;
break;
}
}
// save keypoints for each keyframe
std::ofstream fw_keypoints(keypoints_path + std::to_string(kf_id) + ".txt", std::ofstream::out);
for(auto it=data->frame_keypoints.cbegin(); it!=data->frame_keypoints.cend(); ++it){
if(it->first == kf_id){
const auto& points = it->second[0];
fw_keypoints << points.size() << std::endl;
for (const auto& c : points) {
// x y inverse_depth
fw_keypoints << c[0] << " " << c[1] << " " << c[2] << std::endl;
}
break;
}
}
fw_keypoints.close();
}
} else {
save_image_queue.push(nullptr);
break;
}
}
if(!keyframe_data_path.empty()){
fw_poses_cam.close();
fw_poses_imu.close();
std::cout << "Finished KeyframeDataSaver" << std::endl;
}
if(!marg_data_path.empty())
std::cout << "Finished MargDataSaver" << std::endl;
};
auto save_image_func = [&, img_path]() {
basalt::OpticalFlowResult::Ptr data;
while (true) {
save_image_queue.pop(data);
if (data.get()) {
std::string p = img_path + "/" + std::to_string(data->t_ns) + ".cereal";
std::ofstream os(p, std::ios::binary);
{
cereal::BinaryOutputArchive archive(os);
archive(data);
}
os.close();
} else {
break;
}
}
if(!marg_data_path.empty())
std::cout << "Finished image MargDataSaver" << std::endl;
};
saving_thread.reset(new std::thread(save_func));
saving_img_thread.reset(new std::thread(save_image_func));
} // namespace basalt
MargDataLoader::MargDataLoader() : out_marg_queue(nullptr) {}
void MargDataLoader::start(const std::string& path) {
if (!fs::exists(path))
std::cerr << "No marg. data found in " << path << std::endl;
auto func = [&, path]() {
std::string img_path = path + "/images/";
std::unordered_set<uint64_t> saved_images;
std::map<int64_t, OpticalFlowResult::Ptr> opt_flow_res;
for (const auto& entry : fs::directory_iterator(img_path)) {
OpticalFlowResult::Ptr data;
// std::cout << entry.path() << std::endl;
std::ifstream is(entry.path(), std::ios::binary);
{
cereal::BinaryInputArchive archive(is);
archive(data);
}
is.close();
opt_flow_res[data->t_ns] = data;
}
std::map<int64_t, std::string> filenames;
for (auto& p : fs::directory_iterator(path)) {
std::string filename = p.path().filename();
if (!std::isdigit(filename[0])) continue;
size_t lastindex = filename.find_last_of(".");
std::string rawname = filename.substr(0, lastindex);
int64_t t_ns = std::stol(rawname);
filenames.emplace(t_ns, filename);
}
for (const auto& kv : filenames) {
basalt::MargData::Ptr data(new basalt::MargData);
std::string p = path + "/" + kv.second;
std::ifstream is(p, std::ios::binary);
{
cereal::BinaryInputArchive archive(is);
archive(*data);
}
is.close();
for (const auto& d : data->kfs_all) {
data->opt_flow_res.emplace_back(opt_flow_res.at(d));
}
out_marg_queue->push(data);
}
out_marg_queue->push(nullptr);
std::cout << "Finished MargDataLoader" << std::endl;
};
processing_thread.reset(new std::thread(func));
}
} // namespace basalt
namespace cereal {
template <class Archive, class T>
void save(Archive& ar, const basalt::ManagedImage<T>& m) {
ar(m.w);
ar(m.h);
ar(cereal::binary_data(m.ptr, sizeof(T) * m.w * m.h));
}
template <class Archive, class T>
void load(Archive& ar, basalt::ManagedImage<T>& m) {
size_t w;
size_t h;
ar(w);
ar(h);
m.Reinitialise(w, h);
ar(cereal::binary_data(m.ptr, sizeof(T) * m.w * m.h));
}
template <class Archive>
void serialize(Archive& ar, basalt::OpticalFlowResult& m) {
ar(m.t_ns);
ar(m.observations);
ar(m.input_images);
}
template <class Archive>
void serialize(Archive& ar, basalt::OpticalFlowInput& m) {
ar(m.t_ns);
ar(m.img_data);
}
template <class Archive>
void serialize(Archive& ar, basalt::ImageData& m) {
ar(m.exposure);
ar(m.img);
}
template <class Archive>
static void serialize(Archive& ar, Eigen::AffineCompact2f& m) {
ar(m.matrix());
}
} // namespace cereal