forked from JingwenWang95/DSP-SLAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystem_util.cc
151 lines (133 loc) · 5.71 KB
/
System_util.cc
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
/**
* This file is part of https://github.com/JingwenWang95/DSP-SLAM
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "System.h"
namespace ORB_SLAM2
{
void System::SaveMapCurrentFrame(const string &dir, int frameId) {
stringstream ss;
ss << setfill('0') << setw(6) << frameId;
string fname_pts = dir + "/" + ss.str() + "-MapPoints.txt";
ofstream f_pts;
f_pts.open(fname_pts.c_str());
f_pts << fixed;
const vector<MapPoint *> &vpMPs = mpMap->GetAllMapPoints();
for (size_t i = 0, iend = vpMPs.size(); i < iend; i++) {
if (vpMPs[i]->isBad())
continue;
cv::Mat pos = vpMPs[i]->GetWorldPos();
f_pts << setprecision(9) << pos.at<float>(0) << " " << pos.at<float>(1) << " " << pos.at<float>(2) << endl;
}
f_pts.close();
string fname_objects = dir + "/" + ss.str() + "-MapObjects.txt";
ofstream f_obj;
f_obj.open(fname_objects.c_str());
f_obj << fixed;
auto mvpMapObjects = mpMap->GetAllMapObjects();
sort(mvpMapObjects.begin(), mvpMapObjects.end(), MapObject::lId);
for (MapObject *pMO : mvpMapObjects) {
if (!pMO)
continue;
if (pMO->isBad())
continue;
if (pMO->mRenderId < 0)
continue;
if (pMO->isDynamic())
continue;
f_obj << pMO->mnId << endl;
auto Two = pMO->GetPoseSim3();
f_obj << setprecision(9) << Two(0, 0) << " " << Two(0, 1) << " " << Two(0, 2) << " " << Two(0, 3) << " " <<
Two(1, 0) << " " << Two(1, 1) << " " << Two(1, 2) << " " << Two(1, 3) << " " <<
Two(2, 0) << " " << Two(2, 1) << " " << Two(2, 2) << " " << Two(2, 3) << endl;
f_obj << setprecision(9) << pMO->GetShapeCode().transpose() << endl;
}
f_obj.close();
vector<KeyFrame *> vpKFs = mpMap->GetAllKeyFrames();
sort(vpKFs.begin(), vpKFs.end(), KeyFrame::lId);
string fname_keyframes = dir + "/" + ss.str() + "-KeyFrames.txt";
ofstream f_kfs;
f_kfs.open(fname_keyframes.c_str());
f_kfs << fixed;
for (size_t i = 0; i < vpKFs.size(); i++) {
KeyFrame *pKF = vpKFs[i];
if (pKF->isBad())
continue;
cv::Mat Tcw = pKF->GetPose();
cv::Mat Rwc = Tcw.rowRange(0, 3).colRange(0, 3).t();
cv::Mat twc = -Rwc * Tcw.rowRange(0, 3).col(3);
f_kfs << setprecision(9) << Rwc.at<float>(0, 0) << " " << Rwc.at<float>(0, 1) << " " << Rwc.at<float>(0, 2)
<< " " << twc.at<float>(0) << " " <<
Rwc.at<float>(1, 0) << " " << Rwc.at<float>(1, 1) << " " << Rwc.at<float>(1, 2) << " "
<< twc.at<float>(1) << " " <<
Rwc.at<float>(2, 0) << " " << Rwc.at<float>(2, 1) << " " << Rwc.at<float>(2, 2) << " "
<< twc.at<float>(2) << endl;
}
f_kfs.close();
string fname_frame = dir + "/" + ss.str() + "-Camera.txt";
ofstream f_camera;
f_camera.open(fname_frame.c_str());
f_camera << fixed;
cv::Mat Tcw = mpTracker->mCurrentFrame.mTcw;
cv::Mat Rwc = Tcw.rowRange(0, 3).colRange(0, 3).t();
cv::Mat twc = -Rwc * Tcw.rowRange(0, 3).col(3);
f_camera << setprecision(9) << Rwc.at<float>(0, 0) << " " << Rwc.at<float>(0, 1) << " " << Rwc.at<float>(0, 2)
<< " " << twc.at<float>(0) << " " <<
Rwc.at<float>(1, 0) << " " << Rwc.at<float>(1, 1) << " " << Rwc.at<float>(1, 2) << " "
<< twc.at<float>(1) << " " <<
Rwc.at<float>(2, 0) << " " << Rwc.at<float>(2, 1) << " " << Rwc.at<float>(2, 2) << " "
<< twc.at<float>(2) << endl;
f_camera.close();
cv::Mat frame = mpViewer->GetFrame();
cv::imwrite(dir + "/" + ss.str() + "-Frame.png", frame);
}
void System::SaveEntireMap(const string &dir) {
string fname_pts = dir + "/MapPoints.txt";
ofstream f_pts;
f_pts.open(fname_pts.c_str());
f_pts << fixed;
const vector<MapPoint *> &vpMPs = mpMap->GetAllMapPoints();
for (size_t i = 0, iend = vpMPs.size(); i < iend; i++) {
if (vpMPs[i]->isBad())
continue;
cv::Mat pos = vpMPs[i]->GetWorldPos();
f_pts << setprecision(9) << pos.at<float>(0) << " " << pos.at<float>(1) << " " << pos.at<float>(2) << endl;
}
string fname_objects = dir + "/MapObjects.txt";
ofstream f_obj;
f_obj.open(fname_objects.c_str());
f_obj << fixed;
auto mvpMapObjects = mpMap->GetAllMapObjects();
sort(mvpMapObjects.begin(), mvpMapObjects.end(), MapObject::lId);
for (MapObject *pMO : mvpMapObjects) {
if (!pMO)
continue;
if (pMO->isBad())
continue;
if (pMO->GetRenderId() < 0)
continue;
if (pMO->isDynamic())
continue;
f_obj << pMO->mnId << endl;
auto Two = pMO->GetPoseSim3();
f_obj << setprecision(9) << Two(0, 0) << " " << Two(0, 1) << " " << Two(0, 2) << " " << Two(0, 3) << " " <<
Two(1, 0) << " " << Two(1, 1) << " " << Two(1, 2) << " " << Two(1, 3) << " " <<
Two(2, 0) << " " << Two(2, 1) << " " << Two(2, 2) << " " << Two(2, 3) << endl;
f_obj << setprecision(9) << pMO->GetShapeCode().transpose() << endl;
}
f_obj.close();
SaveTrajectoryKITTI(dir + "/Cameras.txt");
}
}