/
io.cpp
144 lines (123 loc) · 5.52 KB
/
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
#include "io.hpp"
#include <fstream>
#include <iomanip>
#include <iostream>
#ifdef __has_include(filesystem) // C++17 feature
#define CT_ICP_WITH_FS
#include <filesystem>
namespace fs = std::filesystem;
#endif
namespace ct_icp {
const char sep = ' ';
/* -------------------------------------------------------------------------------------------------------------- */
ArrayPoses LoadPoses(const std::string &file_path) {
ArrayPoses poses;
std::ifstream pFile(file_path);
if (pFile.is_open()) {
while (!pFile.eof()) {
std::string line;
std::getline(pFile, line);
if (line.empty()) continue;
std::stringstream ss(line);
Eigen::Matrix4d P = Eigen::Matrix4d::Identity();
ss >> P(0, 0) >> P(0, 1) >> P(0, 2) >> P(0, 3) >> P(1, 0) >> P(1, 1) >> P(1, 2) >> P(1, 3) >> P(2, 0)
>> P(2, 1) >> P(2, 2) >> P(2, 3);
poses.push_back(P);
}
pFile.close();
} else {
std::cout << "Unable to open file" << std::endl;
}
return poses;
}
/* -------------------------------------------------------------------------------------------------------------- */
bool SavePoses(const std::string &file_path, const ArrayPoses &trajectory) {
#ifdef CT_ICP_WITH_FS
auto parent_path = fs::path(file_path).parent_path();
if (!exists(parent_path))
fs::create_directories(parent_path);
#endif
std::ofstream pFile(file_path);
if (pFile.is_open()) {
pFile << std::setprecision(std::numeric_limits<long double>::digits10 + 1);
Eigen::Matrix3d R;
Eigen::Vector3d t;
for (auto &pose: trajectory) {
R = pose.block<3, 3>(0, 0);
t = pose.block<3, 1>(0, 3);
pFile << R(0, 0) << " " << R(0, 1) << " " << R(0, 2) << " " << t(0)
<< " " << R(1, 0) << " " << R(1, 1) << " " << R(1, 2) << " "
<< t(1) << " " << R(2, 0) << " " << R(2, 1) << " " << R(2, 2)
<< " " << t(2) << std::endl;
}
pFile.close();
std::cout << "Saved Poses to " << file_path << std::endl;
return true;
}
std::cout << "Could not open file " << file_path << std::endl;
return false;
}
/* -------------------------------------------------------------------------------------------------------------- */
void save_array_in_stream(std::ostream &os, const double *begin, int size, bool append_sep_to_last) {
for (int i(0); i < size; ++i) {
os << begin[i];
if (i < size - 1 || append_sep_to_last)
os << sep;
}
}
/* -------------------------------------------------------------------------------------------------------------- */
bool SaveTrajectoryFrame(const std::string &file_path, const std::vector<TrajectoryFrame> &trajectory) {
#ifdef CT_ICP_WITH_FS
auto parent_path = fs::path(file_path).parent_path();
if (!exists(parent_path))
fs::create_directories(parent_path);
#endif
std::ofstream pFile(file_path);
if (pFile.is_open()) {
pFile << std::setprecision(std::numeric_limits<long double>::digits10 + 1);
Eigen::Quaterniond q_begin, q_end;
for (auto &pose: trajectory) {
q_begin = Eigen::Quaterniond(pose.begin_R);
q_end = Eigen::Quaterniond(pose.end_R);
pFile << pose.success << sep << pose.begin_timestamp << sep << pose.end_timestamp << sep;
save_array_in_stream(pFile, &q_begin.x(), 4, true);
save_array_in_stream(pFile, &(pose.begin_t.x()), 3, true);
save_array_in_stream(pFile, &q_end.x(), 4, true);
save_array_in_stream(pFile, &(pose.end_t.x()), 3, false);
pFile << std::endl;
}
pFile.close();
std::cout << "Saved Poses to " << file_path << std::endl;
return true;
}
std::cout << "Could not open file " << file_path << std::endl;
return false;
}
/* -------------------------------------------------------------------------------------------------------------- */
std::vector<TrajectoryFrame> LoadTrajectory(const std::string &file_path) {
std::vector<TrajectoryFrame> frames;
std::ifstream pFile(file_path);
if (pFile.is_open()) {
Eigen::Quaterniond begin_quat, end_quat;
while (!pFile.eof()) {
std::string line;
std::getline(pFile, line);
if (line.empty()) continue;
std::stringstream ss(line);
TrajectoryFrame frame;
ss >> frame.success >> frame.begin_timestamp >> frame.end_timestamp >>
begin_quat.x() >> begin_quat.y() >> begin_quat.z() >> begin_quat.w() >>
frame.begin_t.x() >> frame.begin_t.y() >> frame.begin_t.z() >>
end_quat.x() >> end_quat.y() >> end_quat.z() >> end_quat.w() >>
frame.end_t.x() >> frame.end_t.y() >> frame.end_t.z();
frame.begin_R = begin_quat.toRotationMatrix();
frame.end_R = end_quat.toRotationMatrix();
frames.push_back(frame);
}
pFile.close();
} else {
std::cout << "Unable to open file" << std::endl;
return frames;
}
}
} // namespace ct_icp