-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
324 lines (277 loc) · 12 KB
/
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
#include <omp.h>
#include <stdio.h>
#include <ctime>
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <sstream>
#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/thread/thread.hpp>
#include "GCSLAM/GCSLAM.h"
#include "GCSLAM/MILD/BayesianFilter.hpp"
#include "GCSLAM/MILD/loop_closure_detector.hpp"
#include "GCSLAM/MultiViewGeometry.h"
#include "GCSLAM/frame.h"
#include "GCFusion/MapMaintain.hpp"
#include "GCFusion/MobileFusion.h"
#include "GCFusion/MobileGUI.hpp"
#include "Structure/Chisel.h"
#include "open_chisel/Stopwatch.h"
#include "open_chisel/camera/PinholeCamera.h"
#include "open_chisel/utils/ProjectionIntegrator.h"
#include "BasicAPI.h"
#include "Tools/config.h"
using namespace std;
using namespace cv;
#define MULTI_THREAD 1
#define INPUT_SOURCE_DATABASE 0
#define INPUT_SOURCE_OPENNI 1
#define INPUT_SOURCE_REALSENSE 2
#define INPUT_SOURCE_KINECT 3
int main(int argc, char *argv[]) {
int showCaseMode = 0;
string basepath;
float inputVoxelResolution = 0.005;
int sensorType = 0;
BasicAPI::parseInput(argc, argv, showCaseMode, inputVoxelResolution, basepath,
MultiViewGeometry::g_para, sensorType);
vector<float> processingTimePerFrame;
MultiViewGeometry::CameraPara camera;
Wrapper *wrap;
if (sensorType == INPUT_SOURCE_DATABASE) wrap = new DatasetWrapper(basepath);
#if OPENNI2_FOUND
if (sensorType == INPUT_SOURCE_OPENNI) wrap = new OpenNI2Wrapper();
#endif
#if LIBREALSENSE_FOUND
if (sensorType == INPUT_SOURCE_REALSENSE) wrap = new RealSenseWrapper();
#endif
#if LIBFREENECT2_FOUND
if (sensorType == INPUT_SOURCE_REALSENSE) wrap = new KinectWrapper();
#endif
// choose sensor type according to input
if (!wrap->init(camera)) {
std::cout << "fail to init camera" << std::endl;
exit(1);
}
cout << "begin init rendering" << endl;
MobileGUI gui(showCaseMode);
MobileFusion gcFusion;
gcFusion.initChiselMap(camera, inputVoxelResolution,
MultiViewGeometry::g_para.far_plane_distance);
cout << "begin init gcSLAM" << endl;
int maxFrameNum = 20000;
gcFusion.initGCSLAM(maxFrameNum, MultiViewGeometry::g_para, camera);
pangolin::GlTexture imageTexture(640, 480, GL_RGB, false, 0, GL_RGB,
GL_UNSIGNED_BYTE);
#if MULTI_THREAD
boost::thread map_thread(
boost::bind(&MobileFusion::MapManagement, &gcFusion));
#endif
float integrateLocalFrameNum = 6;
int portableDeviceFlag = 0;
// for portable devices
if (thread::hardware_concurrency() < 4) {
portableDeviceFlag = 1;
integrateLocalFrameNum = 3;
}
double localization_time = 0;
while (!pangolin::ShouldQuit() && (wrap != NULL) && !gcFusion.is_stopped)
//**********************************************************the main
// loop*********************************************************
{
if (!gui.pause->Get() || pangolin::Pushed(*gui.step)) {
Frame f;
TICK("System::1::FrameTime");
if (!wrap->LoadSingleFrame(f, camera)) {
gcFusion.is_stopped = true;
break;
}
int feature_num = MultiViewGeometry::g_para.max_feature_num;
if (portableDeviceFlag) feature_num = 600;
BasicAPI::detectAndExtractFeatures(f, feature_num, camera);
BasicAPI::extractNormalMapSIMD(f.refined_depth, f.normal_map, camera.c_fx,
camera.c_fy, camera.c_cx, camera.c_cy);
gcFusion.gcSLAM.update_frame(f);
Frame &frame_current = gcFusion.gcSLAM.globalFrameList.back();
if (frame_current.tracking_success &&
!frame_current.is_keyframe) // if not keyframe
{
int keyframeIndex =
gcFusion.gcSLAM.GetKeyframeDataList().back().keyFrameIndex;
BasicAPI::refineKeyframesSIMD(
gcFusion.gcSLAM.globalFrameList[keyframeIndex], frame_current,
camera);
BasicAPI::refineNewframesSIMD(
gcFusion.gcSLAM.globalFrameList[keyframeIndex], frame_current,
camera);
}
BasicAPI::refineDepthUseNormalSIMD(
(float *)frame_current.normal_map.data,
(float *)frame_current.refined_depth.data, camera.c_fx, camera.c_fy,
camera.c_cx, camera.c_cy, camera.width, camera.height);
if (frame_current.is_keyframe) {
BasicAPI::checkColorQuality(frame_current.normal_map,
frame_current.colorValidFlag, camera.c_fx,
camera.c_fy, camera.c_cx, camera.c_cy);
BasicAPI::estimateColorQuality(
frame_current.refined_depth, frame_current.normal_map,
frame_current.observationQualityMap, frame_current.rgb, camera.c_fx,
camera.c_fy, camera.c_cx, camera.c_cy);
gcFusion.clearRedudentFrameMemory(integrateLocalFrameNum);
#if MULTI_THREAD
gcFusion.updateGlobalMap(gcFusion.gcSLAM.globalFrameList.size(),
gcFusion.gcSLAM.globalFrameList.size() - 1);
#else
gcFusion.tsdfFusion(gcFusion.gcSLAM.globalFrameList,
gcFusion.gcSLAM.globalFrameList.size() - 1,
gcFusion.gcSLAM.GetKeyframeDataList(),
gcFusion.gcSLAM.GetKeyframeDataList().size() - 2);
#endif
}
imageTexture.Upload(gcFusion.gcSLAM.globalFrameList.back().rgb.data,
GL_RGB, GL_UNSIGNED_BYTE);
// imageTexture显示实际看到的场景,非贴图
float memoryConsumption = 0;
for (int k = 0; k < gcFusion.gcSLAM.globalFrameList.size(); k++) {
memoryConsumption +=
gcFusion.gcSLAM.globalFrameList[k].GetOccupiedMemorySize();
}
// cout << "memory for frames: " << memoryConsumption / 1024 /
// 1024 << " " << memoryConsumption / 1024 / 1024
// /gcFusion.gcSLAM.globalFrameList.size() << endl;
TOCK("System::1::FrameTime");
if (frame_current.is_keyframe)
printf("frame %d is key frame!\n",
gcFusion.gcSLAM.globalFrameList.back().frame_index);
printf("frame %d time: %fms\r\n",
gcFusion.gcSLAM.globalFrameList.back().frame_index,
Stopwatch::getInstance().getTiming("System::1::FrameTime"));
processingTimePerFrame.push_back(
Stopwatch::getInstance().getTiming("System::1::FrameTime"));
}
TICK("System::2::GUI");
if (gui.followPose->Get()) {
Eigen::Matrix4f currPose;
if (gcFusion.gcSLAM.globalFrameList.back().tracking_success &&
gcFusion.gcSLAM.globalFrameList.back().origin_index == 0) {
currPose = gcFusion.gcSLAM.globalFrameList.back()
.pose_sophus[0]
.matrix()
.cast<float>();
}
gui.setModelView(currPose, camera.c_fy < 0);
}
gui.PreCall();
if (gui.drawGlobalModel->Get()) {
gcFusion.MobileShow(gui.s_cam.GetProjectionModelViewMatrix(),
VERTEX_WEIGHT_THRESHOLD, gui.drawUnstable->Get(),
gui.drawNormals->Get(), gui.drawColors->Get(),
gui.drawPoints->Get(), gui.drawOrigin->Get(),
gcFusion.gcSLAM.globalFrameList.size(), 1,
gcFusion.gcSLAM.globalFrameList);
}
// gui.DisplayImg(gui.RGB,&imageTexture);
gui.PostCall();
TOCK("System::2::GUI");
}
std::ofstream timeout("time.txt", std::ios::out | std::ios::trunc);
for (int i = 0; i < processingTimePerFrame.size(); i++) {
timeout << i << " " << processingTimePerFrame[i] << std::endl;
}
std::ofstream chunkout("chunk.txt", std::ios::out | std::ios::trunc);
for (int i = 0; i < gcFusion.chunk_log.size(); i++) {
chunkout << gcFusion.chunk_log[i] << " " << gcFusion.chunk_scale[i] << " "
<< gcFusion.chunk_complex[i] << std::endl;
}
std::ofstream statout("stat.txt", std::ios::out | std::ios::trunc);
statout
<< localization_time / gcFusion.gcSLAM.globalFrameList.size() << " "
<< gcFusion.integration_time /
gcFusion.gcSLAM.GetKeyframeDataList().size()
<< " "
<< gcFusion.meshing_time / gcFusion.gcSLAM.GetKeyframeDataList().size()
<< " " << gcFusion.mrf_time / gcFusion.gcSLAM.GetKeyframeDataList().size()
<< " "
<< gcFusion.adjustment_time / gcFusion.gcSLAM.GetKeyframeDataList().size()
<< " "
<< gcFusion.rendering_time / gcFusion.gcSLAM.GetKeyframeDataList().size()
<< " " << std::endl;
char fileName[2560];
memset(fileName, 0, 2560);
sprintf(fileName, "%s/trajectory.txt", basepath.c_str());
BasicAPI::saveTrajectoryFrameList(gcFusion.gcSLAM.globalFrameList, fileName);
float total_time = 0;
for (int i = 0; i < processingTimePerFrame.size(); i++) {
total_time += processingTimePerFrame[i];
}
cout << "average processing time per frame: "
<< total_time / processingTimePerFrame.size() << endl;
/*size_t pixel_num = 0;
for (auto patchit: gcFusion.chiselMap->atlas.allPatches)
{
pixel_num+=
patchit.second->boundingbox.height*patchit.second->boundingbox.width;
}
std::cout << "Memory Storage Stats:"
<< (gcFusion.tsdf_vertice_num*sizeof(float)*9 +
gcFusion.tsdf_indice_num*sizeof(unsigned int)
+ pixel_num*sizeof(unsigned char)*3)/1024.0f/1024
<<"/"<<
gcFusion.tsdf_indice_num*sizeof(float)*7/1024.0f/1024 << std::endl;
*/
memset(fileName, 0, 2560);
sprintf(fileName, "%s/OnlineModel_%dmm.ply", basepath.c_str(),
(int)(1000 * (gcFusion.GetVoxelResolution())));
cout << "saving online model to: " << fileName << endl;
gcFusion.chiselMap->SaveAllMeshesToPLY(fileName);
gcFusion.chiselMap->Reset();
cout << "saving textured model" << endl;
gcFusion.chiselMap->atlas.SaveTexturedModel("./output");
cout << "offline re-integrating all frames" << endl;
TICK("Final::IntegrateAllFrames");
for (int i = 0; i < gcFusion.gcSLAM.globalFrameList.size(); i++) {
gcFusion.IntegrateFrame(gcFusion.gcSLAM.globalFrameList[i]);
}
TOCK("Final::IntegrateAllFrames");
gcFusion.chiselMap->UpdateMeshes(gcFusion.cameraModel);
memset(fileName, 0, 2560);
sprintf(fileName, "%s/finalModelAllframes_%dmm.ply", basepath.c_str(),
(int)(1000 * (gcFusion.GetVoxelResolution())));
cout << "saving offline model to: " << fileName << endl;
gcFusion.chiselMap->SaveAllMeshesToPLY(fileName);
gcFusion.chiselMap->Reset();
BasicAPI::makeDir("texture");
for (int i = 0; i < gcFusion.gcSLAM.KeyframeDataList.size(); i++) {
MultiViewGeometry::KeyFrameDatabase kfd =
gcFusion.gcSLAM.KeyframeDataList[i];
Frame &f = gcFusion.gcSLAM.globalFrameList[kfd.keyFrameIndex];
if (!f.tracking_success) continue;
Eigen::MatrixXf transform = f.pose_sophus[0].matrix().cast<float>();
transform = transform.inverse();
Eigen::Matrix3f r = transform.block<3, 3>(0, 0);
Eigen::MatrixXf t = transform.block<3, 1>(0, 3);
memset(fileName, 0, 2560);
sprintf(fileName, "%s/texture/%06d.cam", basepath.c_str(), i);
FILE *fp = fopen(fileName, "w+");
fprintf(fp, "%f %f %f %f %f %f %f %f %f %f %f %f\r\n", t(0), t(1), t(2),
r(0, 0), r(0, 1), r(0, 2), r(1, 0), r(1, 1), r(1, 2), r(2, 0),
r(2, 1), r(2, 2));
fprintf(fp, "%f %f %f %f %f %f", camera.c_fx / camera.width, camera.d[0],
camera.d[1], camera.c_fx / camera.c_fy, camera.c_cx / camera.width,
camera.c_cy / camera.height);
fclose(fp);
memset(fileName, 0, 2560);
sprintf(fileName, "%s/texture/%06d.png", basepath.c_str(), i);
cv::cvtColor(f.rgb, f.rgb, cv::COLOR_RGB2BGR);
cv::imwrite(fileName, f.rgb);
}
Stopwatch::getInstance().printAll();
cout << "program finish" << endl;
gcFusion.is_stopped = true;
#if MULTI_THREAD
gcFusion.render_barr.wait();
map_thread.join();
#endif
}