forked from augcog/OpenARK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandSlamDemo.cpp
182 lines (150 loc) · 6.34 KB
/
HandSlamDemo.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
#include "D435iCamera.h"
#include "OkvisSLAMSystem.h"
#include <iostream>
#include <thread>
#include "glfwManager.h"
#include "Core.h"
using namespace ark;
int main(int argc, char **argv)
{
if (argc >4 ) {
std::cerr << "Usage: ./" << argv[0] << " configuration-yaml-file [vocabulary-file] [skip-first-seconds]" << std::endl
<<"Args given: " << argc << std::endl;
return -1;
}
google::InitGoogleLogging(argv[0]);
okvis::Duration deltaT(0.0);
if (argc == 4) {
deltaT = okvis::Duration(atof(argv[3]));
}
// read configuration file
std::string configFilename;
if (argc > 1) configFilename = argv[1];
else configFilename = "../../config/d435i_intr.yaml";
std::string vocabFilename;
if (argc > 2) vocabFilename = argv[2];
else vocabFilename = "../../config/brisk_vocab.bn";
OkvisSLAMSystem slam(vocabFilename, configFilename);
//setup display
if (!MyGUI::Manager::init())
{
fprintf(stdout, "Failed to initialize GLFW\n");
return -1;
}
printf("Camera initialization started...\n");
fflush(stdout);
DetectionParams::Ptr hand_params(new DetectionParams());
D435iCamera camera;
PlaneDetector::Ptr planeDetector = std::make_shared<PlaneDetector>();
HandDetector::Ptr handDetector = std::make_shared<HandDetector>(planeDetector);
hand_params->handUseSVM = true;
hand_params->handRequireEdgeConnected = false;
hand_params->planeFloodFillThreshold = 0.06f;
planeDetector->setParams(hand_params);
handDetector->setParams(hand_params);
camera.start();
printf("Camera-IMU initialization complete\n");
fflush(stdout);
//Window for displaying the path
//PoseViewer poseViewer;
MyGUI::CameraWindow traj_win("Traj Viewer", 640*2,480*2);
MyGUI::ARCameraWindow ar_win("AR Viewer", 640*2.5,480*2.5, GL_LUMINANCE, GL_UNSIGNED_BYTE, 3.84299896e+02, 3.84299896e+02, 3.22548157e+02, 2.36944305e+02,0.01,100);
traj_win.set_pos(640*2.5,100);
ar_win.set_pos(0,100);
MyGUI::Path path1("path1", Eigen::Vector3d(1, 0, 0));
MyGUI::Axis axis1("axis1", .1);
MyGUI::Axis axis2("axis2", 1);
MyGUI::Grid grid1("grid1", 10, 1);
traj_win.add_object(&path1);
traj_win.add_object(&axis1);
traj_win.add_object(&axis2);
traj_win.add_object(&grid1);
ar_win.add_object(&axis1);
//Containers for information about cubes placed in the scene
std::vector<MyGUI::Object*> cubes;
std::vector<Eigen::Matrix4d> T_K_cubes;
std::vector<MapKeyFrame::Ptr> K_cubes;
//Recieves output from SLAM system and displays to the screen
FrameAvailableHandler handler([&path1, &axis2, &ar_win, &cubes, &T_K_cubes, &K_cubes](MultiCameraFrame::Ptr frame) {
Eigen::Affine3d transform(frame->T_WC(0));
path1.add_node(transform.translation());
axis2.set_transform(transform);
ar_win.set_camera(transform);
ar_win.set_image(frame->images_[0]);
//Place smaller cubes at fingertip positions
for(int i=0; i<frame->hands_.size();i++){
std::vector<cv::Vec3f> fingers = frame->hands_[i]->getFingers();
for(int j=0; j< fingers.size(); j++){
Eigen::Translation3d finger_pos(fingers[j][0],fingers[j][1],fingers[j][2]);
std::string cube_name = std::string("CubeNum")+std::to_string(cubes.size());
MyGUI::Object* obj = new MyGUI::Cube(cube_name, 0.01,0.01,0.01);
obj->set_transform(transform*finger_pos);
cubes.push_back(obj);
Eigen::Affine3d T_KS(frame->T_KS_);
T_K_cubes.push_back((T_KS*finger_pos).matrix());
K_cubes.push_back(frame->keyframe_);
std::cout << "Adding cube " << cube_name << std::endl;
ar_win.add_object(obj); //NOTE: this is bad, should change objects to shared_ptr
}
}
//If the AR window is clicked, add a larger cube to the scene
if(ar_win.clicked()){
std::string cube_name = std::string("CubeNum")+std::to_string(cubes.size());
MyGUI::Object* obj = new MyGUI::Cube(cube_name, 0.1,0.1,0.1);
obj->set_transform(transform);
cubes.push_back(obj);
T_K_cubes.push_back(frame->T_KS_);
K_cubes.push_back(frame->keyframe_);
std::cout << "Adding cube " << cube_name << std::endl;
ar_win.add_object(obj); //NOTE: this is bad, should change objects to shared_ptr
}
});
slam.AddFrameAvailableHandler(handler, "mapping");
KeyFrameAvailableHandler kfHandler([](MultiCameraFrame::Ptr frame){
frame->saveSimple("map_images/");
});
//slam.AddKeyFrameAvailableHandler(kfHandler, "saving");
LoopClosureDetectedHandler loopHandler([&slam, &path1, &cubes, &T_K_cubes, &K_cubes](void) {
std::vector<Eigen::Matrix4d> traj;
slam.getTrajectory(traj);
path1.clear();
for(size_t i=0; i<traj.size(); i++){
path1.add_node(traj[i].block<3,1>(0,3));
}
for(size_t i=0; i<cubes.size(); i++){
if(K_cubes[i]!=nullptr)
cubes[i]->set_transform(Eigen::Affine3d(K_cubes[i]->T_WS()*T_K_cubes[i]));
}
});
slam.AddLoopClosureDetectedHandler(loopHandler,"trajectoryUpdate");
//run until display is closed
okvis::Time start(0.0);
int id =0;
while (MyGUI::Manager::running()){
//Update the display
MyGUI::Manager::update();
//Get current camera frame
MultiCameraFrame::Ptr frame(new MultiCameraFrame);
camera.update(*frame);
cv::Mat xyzMap = frame->images_[2];
planeDetector->update(xyzMap);
frame->planes_ = planeDetector->getPlanes();
handDetector->update(xyzMap);
frame->hands_ = handDetector->getHands();
//Get or wait for IMU Data until current frame
//std::cout << "frame: " << frame.timestamp_ << std::endl;
std::vector<ImuPair> imuData;
camera.getImuToTime(frame->timestamp_,imuData);
//std::cout << "numimu: " << imuData.size() << std::endl;
//Add data to SLAM system
slam.PushIMU(imuData);
slam.PushFrame(frame);
int k = cv::waitKey(1);
if (k == 'q' || k == 'Q' || k == 27) break; // 27 is ESC
}
printf("\nTerminate...\n");
// Clean up
slam.ShutDown();
printf("\nExiting...\n");
return 0;
}