forked from GerhardR/kfusion
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.cpp
263 lines (210 loc) · 7.52 KB
/
test.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
/*
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 <iostream>
#include <sstream>
#include <iomanip>
#ifdef __APPLE__
#include <GLUT/glut.h>
#elif defined(WIN32)
#define GLUT_NO_LIB_PRAGMA
#include <GL/glut.h>
#else
#include <GL/glut.h>
#endif
#include "perfstats.h"
using namespace std;
using namespace TooN;
SE3<float> preTrans(makeVector(0.0, 0, -0.9, 0, 0, 0));
SE3<float> rot(makeVector(0.0, 0, 0, 0, 0, 0));
SE3<float> trans(makeVector(0.5, 0.5, 0.5, 0, 0, 0));
KFusion kfusion;
Volume reference;
Image<float3, HostDevice> vertex, normal;
Image<float, HostDevice> depth;
Image<uchar4, HostDevice> rgb;
int counter = 0;
bool benchmark = false;
void display(void) {
static bool integrate = true;
const uint2 imageSize = kfusion.configuration.inputSize;
const double start = Stats.start();
renderInput(vertex.getDeviceImage(), normal.getDeviceImage(), depth.getDeviceImage(), reference, toMatrix4( trans * rot * preTrans ) * getInverseCameraMatrix(kfusion.configuration.camera), kfusion.configuration.nearPlane, kfusion.configuration.farPlane, kfusion.configuration.stepSize(), 0.01 );
cudaDeviceSynchronize();
Stats.sample("ground raycast");
Stats.sample("ground copy");
glRasterPos2i(0,0);
glDrawPixels(vertex);
glRasterPos2i(imageSize.x, 0);
glDrawPixels(normal);
glRasterPos2i(imageSize.x * 2, 0);
glDrawPixels(depth);
Stats.sample("ground draw");
kfusion.setDepth( depth.getDeviceImage() );
cudaDeviceSynchronize();
const double track_start = Stats.sample("process depth");
if(counter > 1){
integrate = kfusion.Track();
cudaDeviceSynchronize();
Stats.sample("track");
}
renderTrackResult(rgb.getDeviceImage(), kfusion.reduction);
cudaDeviceSynchronize();
Stats.sample("track render");
Stats.sample("track copy");
if(integrate){
kfusion.Integrate();
cudaDeviceSynchronize();
Stats.sample("integration");
kfusion.Raycast();
cudaDeviceSynchronize();
Stats.sample("raycast");
vertex = kfusion.vertex;
normal = kfusion.normal;
Stats.sample("raycast get");
}
glRasterPos2i(0,imageSize.y * 1);
glDrawPixels(vertex);
glRasterPos2i(imageSize.x, imageSize.y * 1);
glDrawPixels(normal);
glRasterPos2i(2 * imageSize.x, imageSize.y * 1);
glDrawPixels(rgb);
Stats.sample("track draw");
Stats.sample("total track", Stats.get_time() - track_start, PerfStats::TIME);
renderInput(vertex.getDeviceImage(), normal.getDeviceImage(), depth.getDeviceImage(), kfusion.integration, kfusion.pose * getInverseCameraMatrix(kfusion.configuration.camera), kfusion.configuration.nearPlane, kfusion.configuration.farPlane, kfusion.configuration.stepSize(), 0.7 * kfusion.configuration.mu );
cudaDeviceSynchronize();
Stats.sample("view raycast");
Stats.sample("view copy");
glRasterPos2i(0,imageSize.y * 2);
glDrawPixels(vertex);
glRasterPos2i(imageSize.x, imageSize.y * 2);
glDrawPixels(normal);
glRasterPos2i(imageSize.x * 2, imageSize.y * 2);
glDrawPixels(depth);
Stats.sample("view draw");
Stats.sample("events");
Stats.sample("total all", Stats.get_time() - start, PerfStats::TIME);
if(counter % 30 == 0){
Stats.print();
Stats.reset();
cout << endl;
}
++counter;
printCUDAError();
glutSwapBuffers();
}
void keys(unsigned char key, int x, int y) {
switch(key){
case 'r':
kfusion.setPose( toMatrix4( trans * rot * preTrans ));
break;
case 'c':
kfusion.Reset();
kfusion.setPose( toMatrix4( trans * rot * preTrans ));
break;
case 'd':
cout << kfusion.pose << endl;
break;
case 'q':
exit(0);
break;
}
glutPostRedisplay();
}
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));
break;
case GLUT_KEY_RIGHT:
rot *= SE3<float>(makeVector(0.0, 0, 0, 0, -0.1, 0));
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;
}
glutPostRedisplay();
}
void idle(void) {
if(counter > 100 && benchmark)
exit(0);
if(benchmark)
rot *= SE3<float>(makeVector(0.0, 0, 0, 0, 0.02, 0));
glutPostRedisplay();
}
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);
}
int main(int argc, char ** argv) {
benchmark = argc > 1 && string(argv[1]) == "-b";
KFusionConfig config;
config.volumeSize = make_uint3(128);
config.combinedTrackAndReduce = false;
config.iterations[0] = 10;
config.iterations[1] = 5;
config.iterations[2] = 5;
config.inputSize = make_uint2(320, 240);
config.camera = make_float4(100, 100, 160, 120);
config.nearPlane = 0.001;
config.maxweight = 100;
config.mu = 0.1;
config.dist_threshold = 0.2f;
config.normal_threshold = 0.8f;
kfusion.Init(config);
if(printCUDAError()){
cudaDeviceReset();
exit(1);
}
reference.init(config.volumeSize, config.volumeDimensions);
initVolumeWrap(reference, 1.0f);
setBoxWrap(reference, make_float3(0.1f,0.1f,0.8f), make_float3(0.9f, 0.9f, 0.9f), -1.0f);
setBoxWrap(reference, make_float3(0.1f,0.8f,0.1f), make_float3(0.9f, 0.9f, 0.9f), -1.0f);
setBoxWrap(reference, make_float3(0.8f,0.1f,0.1f), make_float3(0.9f, 0.9f, 0.9f), -1.0f);
setSphereWrap(reference, make_float3(0.5f), 0.2f, -1.0f);
kfusion.setPose( toMatrix4( trans * rot * preTrans ));
vertex.alloc(config.inputSize);
normal.alloc(config.inputSize);
depth.alloc(config.inputSize);
rgb.alloc(config.inputSize);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
glutInitWindowSize(config.inputSize.x * 3, config.inputSize.y * 3);
glutCreateWindow("kfusion test");
glutDisplayFunc(display);
glutKeyboardFunc(keys);
glutSpecialFunc(specials);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMainLoop();
cudaDeviceReset();
return 0;
}