forked from GerhardR/kfusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kinect.cpp
282 lines (230 loc) · 8.68 KB
/
kinect.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
/*
Copyright (c) 2011-2013 Gerhard Reitmayr, TU Graz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "kfusion.h"
#include "helpers.h"
#include "interface.h"
#include "perfstats.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#ifdef __APPLE__
#include <GLUT/glut.h>
#elif defined(WIN32)
#define GLUT_NO_LIB_PRAGMA
#include <glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
using namespace TooN;
KFusion kfusion;
Image<uchar4, HostDevice> lightScene, trackModel, lightModel, texModel;
Image<uint16_t, HostDevice> depthImage[2];
Image<uchar3, HostDevice> rgbImage;
const float3 light = make_float3(1, 1, -1.0);
const float3 ambient = make_float3(0.1, 0.1, 0.1);
SE3<float> initPose;
int counter = 0;
int integration_rate = 2;
bool reset = true;
bool should_integrate = true;
bool render_texture = false;
Image<float3, Device> pos, normals;
Image<float, Device> dep;
SE3<float> preTrans, trans, rot(makeVector(0.0, 0, 0, 0, 0, 0));
bool redraw_big_view = false;
void display(void){
const uint2 imageSize = kfusion.configuration.inputSize;
static bool integrate = true;
glClear( GL_COLOR_BUFFER_BIT );
const double startFrame = Stats.start();
const double startProcessing = Stats.sample("kinect");
kfusion.setKinectDeviceDepth(depthImage[GetKinectFrame()].getDeviceImage());
Stats.sample("raw to cooked");
integrate = kfusion.Track();
Stats.sample("track");
if((should_integrate && integrate && ((counter % integration_rate) == 0)) || reset){
kfusion.Integrate();
kfusion.Raycast();
Stats.sample("integrate");
if(counter > 2) // use the first two frames to initialize
reset = false;
}
renderLight( lightScene.getDeviceImage(), kfusion.inputVertex[0], kfusion.inputNormal[0], light, ambient );
renderLight( lightModel.getDeviceImage(), kfusion.vertex, kfusion.normal, light, ambient);
renderTrackResult(trackModel.getDeviceImage(), kfusion.reduction);
static int count = 4;
if(count > 3 || redraw_big_view){
renderInput( pos, normals, dep, kfusion.integration, toMatrix4( trans * rot * preTrans ) * getInverseCameraMatrix(kfusion.configuration.camera * 2), kfusion.configuration.nearPlane, kfusion.configuration.farPlane, kfusion.configuration.stepSize(), 0.75 * kfusion.configuration.mu);
count = 0;
redraw_big_view = false;
} else
count++;
if(render_texture)
renderTexture( texModel.getDeviceImage(), pos, normals, rgbImage.getDeviceImage(), getCameraMatrix(2*kfusion.configuration.camera) * inverse(kfusion.pose), light);
else
renderLight( texModel.getDeviceImage(), pos, normals, light, ambient);
cudaDeviceSynchronize();
Stats.sample("render");
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(lightScene);
glRasterPos2i(0, 240);
glPixelZoom(0.5, -0.5);
glDrawPixels(rgbImage);
glPixelZoom(1,-1);
glRasterPos2i(320,0);
glDrawPixels(lightModel);
glRasterPos2i(320,240);
glDrawPixels(trackModel);
glRasterPos2i(640, 0);
glDrawPixels(texModel);
const double endProcessing = Stats.sample("draw");
Stats.sample("total", endProcessing - startFrame, PerfStats::TIME);
Stats.sample("total_proc", endProcessing - startProcessing, PerfStats::TIME);
if(printCUDAError())
exit(1);
++counter;
if(counter % 50 == 0){
Stats.print();
Stats.reset();
cout << endl;
}
glutSwapBuffers();
}
void idle(void){
if(KinectFrameAvailable())
glutPostRedisplay();
}
void keys(unsigned char key, int x, int y){
switch(key){
case 'c':
kfusion.Reset();
kfusion.setPose(toMatrix4(initPose));
reset = true;
break;
case 'q':
exit(0);
break;
case 'i':
should_integrate = !should_integrate;
break;
case 't':
render_texture = !render_texture;
break;
}
}
void specials(int key, int x, int y){
switch(key){
case GLUT_KEY_LEFT:
rot = SE3<float>(makeVector(0.0, 0, 0, 0, 0.1, 0)) * rot;
break;
case GLUT_KEY_RIGHT:
rot = SE3<float>(makeVector(0.0, 0, 0, 0, -0.1, 0)) * rot;
break;
case GLUT_KEY_UP:
rot *= SE3<float>(makeVector(0.0, 0, 0, -0.1, 0, 0));
break;
case GLUT_KEY_DOWN:
rot *= SE3<float>(makeVector(0.0, 0, 0, 0.1, 0, 0));
break;
}
redraw_big_view = true;
}
void reshape(int width, int height){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(1.0f,1.0f,1.0f);
glRasterPos2f(-1, 1);
glOrtho(-0.375, width-0.375, height-0.375, -0.375, -1 , 1); //offsets to make (0,0) the top left pixel (rather than off the display)
glPixelZoom(1,-1);
}
void exitFunc(void){
CloseKinect();
kfusion.Clear();
cudaDeviceReset();
}
int main(int argc, char ** argv) {
const float size = (argc > 1) ? atof(argv[1]) : 2.f;
KFusionConfig config;
// it is enough now to set the volume resolution once.
// everything else is derived from that.
// config.volumeSize = make_uint3(64);
// config.volumeSize = make_uint3(128);
config.volumeSize = make_uint3(256);
// these are physical dimensions in meters
config.volumeDimensions = make_float3(size);
config.nearPlane = 0.4f;
config.farPlane = 5.0f;
config.mu = 0.1;
config.combinedTrackAndReduce = false;
// change the following parameters for using 640 x 480 input images
config.inputSize = make_uint2(320,240);
config.camera = make_float4(531.15/2, 531.15/2, 640/4, 480/4);
// config.iterations is a vector<int>, the length determines
// the number of levels to be used in tracking
// push back more then 3 iteraton numbers to get more levels.
config.iterations[0] = 10;
config.iterations[1] = 5;
config.iterations[2] = 4;
config.dist_threshold = (argc > 2 ) ? atof(argv[2]) : config.dist_threshold;
config.normal_threshold = (argc > 3 ) ? atof(argv[3]) : config.normal_threshold;
initPose = SE3<float>(makeVector(size/2, size/2, 0, 0, 0, 0));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize(config.inputSize.x * 2 + 640, max(config.inputSize.y * 2, 480));
glutCreateWindow("kfusion");
kfusion.Init(config);
// input buffers
depthImage[0].alloc(make_uint2(640, 480));
depthImage[1].alloc(make_uint2(640, 480));
rgbImage.alloc(make_uint2(640, 480));
// render buffers
lightScene.alloc(config.inputSize), trackModel.alloc(config.inputSize), lightModel.alloc(config.inputSize);
pos.alloc(make_uint2(640, 480)), normals.alloc(make_uint2(640, 480)), dep.alloc(make_uint2(640, 480)), texModel.alloc(make_uint2(640, 480));
if(printCUDAError()) {
cudaDeviceReset();
return 1;
}
memset(depthImage[0].data(), 0, depthImage[0].size.x*depthImage[0].size.y * sizeof(uint16_t));
memset(depthImage[1].data(), 0, depthImage[1].size.x*depthImage[1].size.y * sizeof(uint16_t));
memset(rgbImage.data(), 0, rgbImage.size.x*rgbImage.size.y * sizeof(uchar3));
uint16_t * buffers[2] = {depthImage[0].data(), depthImage[1].data()};
if(InitKinect(buffers, (unsigned char *)rgbImage.data())){
cudaDeviceReset();
return 1;
}
kfusion.setPose(toMatrix4(initPose));
// model rendering parameters
preTrans = SE3<float>::exp(makeVector(0.0, 0, -size, 0, 0, 0));
trans = SE3<float>::exp(makeVector(0.5, 0.5, 0.5, 0, 0, 0) * size);
atexit(exitFunc);
glutDisplayFunc(display);
glutKeyboardFunc(keys);
glutSpecialFunc(specials);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMainLoop();
CloseKinect();
return 0;
}