/
registrationCuda.cu
405 lines (364 loc) · 14.8 KB
/
registrationCuda.cu
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "..\registrationCuda.h"
const int limit = 512 * 424;
//const int size_color = 1920 * 1080;
//const int size_filter_map = size_color + 1920 * 2;
//const int offset_filter_map = 1920;
int *d_map_dist;
int *d_map_yi;
float *d_map_x;
float *d_color_shift_m;
float *d_color_fx;
float *d_color_cx;
double *d_t;
unsigned int *d_rgb_data;
int *d_map_c_off;
__global__ void Apply(int *d_map_dist, float *d_depth_data, float *d_map_x, float *d_color_shift_m, float *d_color_fx, float *d_color_cx, int *d_map_yi, unsigned int *d_rgb_data, double *d_t, float *d_p_filter_map, pcl::cuda::PointXYZRGB *points, unsigned int *d_count){
int id = blockIdx.x*blockDim.x + threadIdx.x;
if (id<limit){
// getting index of distorted depth pixel
const int index = d_map_dist[id];
// check if distorted depth pixel is outside of the depth image
if (index > 0){
// getting depth value for current pixel
const float z = d_depth_data[index];
// checking for invalid depth value
if (z > 0.0f){
// calculating x offset for rgb image based on depth value
const float rx = (d_map_x[id]+ ((*d_color_shift_m) / z)) * (*d_color_fx) + (*d_color_cx);
const int cx = rx; // same as round for positive numbers (0.5f was already added to color_cx)
// getting y offset for depth image
const int cy = d_map_yi[id];
// combining offsets
const int c_off = cx + cy * 1920;
// check if c_off is outside of rgb image
// checking rx/cx is not needed because the color image is much wider then the depth image
if (c_off >= 0 && c_off < (1920 * 1080)){
// setting a window around the filter map pixel corresponding to the color pixel with the current z value
int yi = (cy - 1) * 1920 + cx - 2; // index of first pixel to set
for (int r = -1; r <= 1; ++r, yi += 1920) // index increased by a full row each iteration
{
float *it = d_p_filter_map + yi;
for (int c = -2; c <= 2; ++c, ++it)
{
// only set if the current z is smaller
if (z < *it)
*it = z;
}
}
const float min_z = d_p_filter_map[c_off];
unsigned int color = (z - min_z) / z > 0.01f ? 0 : *(d_rgb_data + c_off);
const float depth_value = z / 1000.0f;
int y = id / 512;
int x = id % 512;
double p[4] = { x*depth_value, y*depth_value, 1.0*depth_value, 1.0 };
double cor[4] = { d_t[0] * p[0] + d_t[4] * p[1] + d_t[8] * p[2] + d_t[12] * p[3],
d_t[1] * p[0] + d_t[5] * p[1] + d_t[9] * p[2] + d_t[13] * p[3],
d_t[2] * p[0] + d_t[6] * p[1] + d_t[10] * p[2] + d_t[14] * p[3],
d_t[3] * p[0] + d_t[7] * p[1] + d_t[11] * p[2] + d_t[15] * p[3] };
pcl::cuda::PointXYZRGB itP;
itP.z = depth_value;
itP.x = cor[0];
itP.y = cor[1];
itP.rgb.r = ((color >> 16) & 0xff);
itP.rgb.g = ((color >> 8) & 0xff);
itP.rgb.b = (color & 0xff);
unsigned int temp=atomicAdd(d_count, 1);
points[temp] = itP;
}
}
}
}
}
__global__ void Apply1(int *d_map_dist, unsigned short *d_depth_data, float *d_map_x, float *d_color_shift_m, float *d_color_fx, float *d_color_cx, int *d_map_yi, unsigned int *d_p_filter_map, int *d_map_c_off){
int id = blockIdx.x*blockDim.x + threadIdx.x;
if (id<limit){
// getting index of distorted depth pixel
const int index = d_map_dist[id];
// check if distorted depth pixel is outside of the depth image
if (index >= 0){
// getting depth value for current pixel
const float z = d_depth_data[index];
// checking for invalid depth value
if (z > 0.0f){
// calculating x offset for rgb image based on depth value
const float rx = (d_map_x[id] + ((*d_color_shift_m) / z)) * (*d_color_fx) + (*d_color_cx);
const int cx = rx; // same as round for positive numbers (0.5f was already added to color_cx)
// getting y offset for depth image
const int cy = d_map_yi[id];
// combining offsets
const int c_off = cx + cy * 1920;
// check if c_off is outside of rgb image
// checking rx/cx is not needed because the color image is much wider then the depth image
if (c_off >= 0 && c_off < (1920 * 1080)){
d_map_c_off[id] = c_off;
// setting a window around the filter map pixel corresponding to the color pixel with the current z value
int yi = (cy - 1) * 1920 + cx - 2; // index of first pixel to set
unsigned int zTemp = z * 100;
for (int r = -1; r <= 1; ++r, yi += 1920) // index increased by a full row each iteration
{
unsigned int *it = d_p_filter_map + yi;
for (int c = -2; c <= 2; ++c, ++it)
{
atomicMin(it, zTemp);
}
}
}
else
{
d_map_c_off[id] = -1;
}
}
else
{
d_map_c_off[id] = -1;
}
}
else
{
d_map_c_off[id] = -1;
}
}
}
__global__ void Apply3(int *d_map_dist, unsigned short *d_depth_data, float *d_map_x, float *d_color_shift_m, float *d_color_fx, float *d_color_cx, int *d_map_yi, unsigned int *d_p_filter_map, int *d_map_c_off, unsigned char *d_body_index_data){
int id = blockIdx.x*blockDim.x + threadIdx.x;
if (id<limit){
// getting index of distorted depth pixel
const int index = d_map_dist[id];
// check if distorted depth pixel is outside of the depth image
if (index >= 0){
// getting depth value for current pixel
const float z = d_depth_data[index];
unsigned char pix=d_body_index_data[index];
// checking for invalid depth value
if (z > 0.0f && pix != 0xff){
// calculating x offset for rgb image based on depth value
const float rx = (d_map_x[id] + ((*d_color_shift_m) / z)) * (*d_color_fx) + (*d_color_cx);
const int cx = rx; // same as round for positive numbers (0.5f was already added to color_cx)
// getting y offset for depth image
const int cy = d_map_yi[id];
// combining offsets
const int c_off = cx + cy * 1920;
// check if c_off is outside of rgb image
// checking rx/cx is not needed because the color image is much wider then the depth image
if (c_off >= 0 && c_off < (1920 * 1080)){
d_map_c_off[id] = c_off;
// setting a window around the filter map pixel corresponding to the color pixel with the current z value
int yi = (cy - 1) * 1920 + cx - 2; // index of first pixel to set
unsigned int zTemp = z * 100;
for (int r = -1; r <= 1; ++r, yi += 1920) // index increased by a full row each iteration
{
unsigned int *it = d_p_filter_map + yi;
for (int c = -2; c <= 2; ++c, ++it)
{
atomicMin(it, zTemp);
}
}
}
else
{
d_map_c_off[id] = -1;
}
}
else
{
d_map_c_off[id] = -1;
}
}
else
{
d_map_c_off[id] = -1;
}
}
}
__global__ void Apply2(int *d_map_dist, unsigned short *d_depth_data, unsigned char *d_rgb_data, double *d_t, unsigned int *d_p_filter_map, pcl::cuda::PointXYZRGB *points, unsigned int *d_count, int *d_map_c_off){
int id = blockIdx.x*blockDim.x + threadIdx.x;
if (id < limit){
int c_off = d_map_c_off[id];
if (c_off>=0){
const float min_z = d_p_filter_map[c_off]/100;
int index=d_map_dist[id];
unsigned short z = d_depth_data[index];
if ((z - min_z) / z <= 0.01f){
unsigned char* color=(d_rgb_data + c_off * 4);
const float depth_value = z / 1000.0f;
int y = id / 512;
int x = id % 512;
double p[4] = { x*depth_value, y*depth_value, 1.0*depth_value, 1.0 };
double cor[4] = { d_t[0] * p[0] + d_t[4] * p[1] + d_t[8] * p[2] + d_t[12] * p[3],
d_t[1] * p[0] + d_t[5] * p[1] + d_t[9] * p[2] + d_t[13] * p[3],
d_t[2] * p[0] + d_t[6] * p[1] + d_t[10] * p[2] + d_t[14] * p[3],
d_t[3] * p[0] + d_t[7] * p[1] + d_t[11] * p[2] + d_t[15] * p[3] };
pcl::cuda::PointXYZRGB itP;
itP.z = depth_value;
itP.x = cor[0];
itP.y = cor[1];
itP.rgb.r = *color;// *(d_rgb_data + c_off * 4);
itP.rgb.g = *(color+1);//*(d_rgb_data + c_off*4+1);
itP.rgb.b = *(color + 2);//*(d_rgb_data + c_off*4+2);
unsigned int temp = atomicAdd(d_count, 1);
points[temp] = itP;
}
}
}
}
__global__ void Apply21(int *d_map_dist, unsigned short *d_depth_data, unsigned char *d_rgb_data, double *d_t, unsigned int *d_p_filter_map, PointXYZRGBNew *points, unsigned int *d_count, int *d_map_c_off){
int id = blockIdx.x*blockDim.x + threadIdx.x;
if (id < limit){
int c_off = d_map_c_off[id];
if (c_off >= 0){
const float min_z = d_p_filter_map[c_off] / 100;
int index = d_map_dist[id];
unsigned short z = d_depth_data[index];
if ((z - min_z) / z <= 0.01f){
unsigned char* color = (d_rgb_data + c_off * 4);
const float depth_value = z / 1000.0f;
int y = id / 512;
int x = id % 512;
double p[4] = { x*depth_value, y*depth_value, 1.0*depth_value, 1.0 };
double cor[4] = { d_t[0] * p[0] + d_t[4] * p[1] + d_t[8] * p[2] + d_t[12] * p[3],
d_t[1] * p[0] + d_t[5] * p[1] + d_t[9] * p[2] + d_t[13] * p[3],
d_t[2] * p[0] + d_t[6] * p[1] + d_t[10] * p[2] + d_t[14] * p[3],
d_t[3] * p[0] + d_t[7] * p[1] + d_t[11] * p[2] + d_t[15] * p[3] };
PointXYZRGBNew itP;
itP.z = depth_value;
itP.x = cor[0];
itP.y = cor[1];
itP.r = *color;// *(d_rgb_data + c_off * 4);
itP.g = *(color + 1);//*(d_rgb_data + c_off*4+1);
itP.b = *(color + 2);//*(d_rgb_data + c_off*4+2);
unsigned int temp = atomicAdd(d_count, 1);
points[temp] = itP;
}
}
}
}
bool init(int *h_map_dist, float *h_map_x, int *h_map_yi, float *h_color_shift_m, float *h_color_fx, float *h_color_cx, double *h_t){
//allocate memory in device
if (cudaMalloc(&d_map_dist, sizeof(int)*limit) != cudaSuccess){
return false;
}
if (cudaMalloc(&d_map_x, sizeof(float)*limit) != cudaSuccess){
cudaFree(d_map_dist);
return false;
}
if (cudaMalloc(&d_map_yi, sizeof(int)*limit) != cudaSuccess){
cudaFree(d_map_dist);
cudaFree(d_map_x);
return false;
}
if (cudaMalloc(&d_color_shift_m, sizeof(float)) != cudaSuccess){
cudaFree(d_map_dist);
cudaFree(d_map_x);
cudaFree(d_map_yi);
return false;
}
if (cudaMalloc(&d_color_fx, sizeof(float)) != cudaSuccess){
cudaFree(d_map_dist);
cudaFree(d_map_x);
cudaFree(d_map_yi);
cudaFree(d_color_shift_m);
return false;
}
if (cudaMalloc(&d_color_cx, sizeof(float)) != cudaSuccess){
cudaFree(d_map_dist);
cudaFree(d_map_x);
cudaFree(d_map_yi);
cudaFree(d_color_shift_m);
cudaFree(d_color_fx);
return false;
}
if (cudaMalloc(&d_t, sizeof(double)*16) != cudaSuccess){
cudaFree(d_map_dist);
cudaFree(d_map_x);
cudaFree(d_map_yi);
cudaFree(d_color_shift_m);
cudaFree(d_color_fx);
cudaFree(d_color_cx);
return false;
}
if (cudaMalloc(&d_map_c_off, sizeof(int) * limit) != cudaSuccess){
cudaFree(d_map_dist);
cudaFree(d_map_x);
cudaFree(d_map_yi);
cudaFree(d_color_shift_m);
cudaFree(d_color_fx);
cudaFree(d_color_cx);
cudaFree(d_t);
return false;
}
//copy memory to device
if (cudaMemcpy(d_map_dist, h_map_dist, sizeof(int)*limit,cudaMemcpyHostToDevice) != cudaSuccess){
freeCudaMem();
return false;
}
if (cudaMemcpy(d_map_x, h_map_x, sizeof(float)*limit, cudaMemcpyHostToDevice) != cudaSuccess){
freeCudaMem();
return false;
}
if (cudaMemcpy(d_map_yi, h_map_yi, sizeof(int)*limit, cudaMemcpyHostToDevice) != cudaSuccess){
freeCudaMem();
return false;
}
if (cudaMemcpy(d_color_shift_m, h_color_shift_m, sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess){
freeCudaMem();
return false;
}
if (cudaMemcpy(d_color_fx, h_color_fx, sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess){
freeCudaMem();
return false;
}
if (cudaMemcpy(d_color_cx, h_color_cx, sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess){
freeCudaMem();
return false;
}
if (cudaMemcpy(d_t,h_t, sizeof(double)*16, cudaMemcpyHostToDevice) != cudaSuccess){
freeCudaMem();
return false;
}
return true;
}
//free memory in device
void freeCudaMem(){
cudaFree(d_map_dist);
cudaFree(d_map_x);
cudaFree(d_map_yi);
cudaFree(d_color_shift_m);
cudaFree(d_color_fx);
cudaFree(d_color_cx);
cudaFree(d_t);
cudaFree(d_map_c_off);
}
bool applyRegistrationCuda(unsigned char *d_rgb_data, unsigned short *d_depth_data, pcl::cuda::PointXYZRGB *points, unsigned int *d_p_filter_map, unsigned int *d_count){
//Apply << <(limit / 424) + 1, 424 >> >(d_map_dist, d_depth_data, d_map_x, d_color_shift_m, d_color_fx, d_color_cx, d_map_yi, d_rgb_data, d_t, d_p_filter_map, points, d_count);
Apply1 << <(limit / 424) + 1, 424 >> >(d_map_dist, d_depth_data, d_map_x, d_color_shift_m, d_color_fx, d_color_cx, d_map_yi, d_p_filter_map, d_map_c_off);
cudaDeviceSynchronize();
Apply2 << <(limit / 424) + 1, 424 >> >(d_map_dist,d_depth_data, d_rgb_data, d_t, d_p_filter_map, points, d_count, d_map_c_off);
cudaDeviceSynchronize();
return true;
}
bool applyRegistrationCuda2(unsigned char *d_rgb_data, unsigned short *d_depth_data, pcl::cuda::PointXYZRGB *points, unsigned int *d_p_filter_map, unsigned int *d_count, double *h_t){
if (cudaMemcpy(d_t, h_t, sizeof(double) * 16, cudaMemcpyHostToDevice) != cudaSuccess){
freeCudaMem();
return false;
}
Apply1 << <(limit / 424) + 1, 424 >> >(d_map_dist, d_depth_data, d_map_x, d_color_shift_m, d_color_fx, d_color_cx, d_map_yi, d_p_filter_map, d_map_c_off);
cudaDeviceSynchronize();
Apply2 << <(limit / 424) + 1, 424 >> >(d_map_dist, d_depth_data, d_rgb_data, d_t, d_p_filter_map, points, d_count, d_map_c_off);
cudaDeviceSynchronize();
return true;
}
bool applyRegistrationCuda3(unsigned char *d_rgb_data, unsigned short *d_depth_data, PointXYZRGBNew *points, unsigned int *d_p_filter_map, unsigned int *d_count, unsigned char *d_body_index_data, int nthreads){
Apply3 << <(limit / nthreads), nthreads >> >(d_map_dist, d_depth_data, d_map_x, d_color_shift_m, d_color_fx, d_color_cx, d_map_yi, d_p_filter_map, d_map_c_off, d_body_index_data);
cudaDeviceSynchronize();
Apply21 << <(limit / nthreads), nthreads >> >(d_map_dist, d_depth_data, d_rgb_data, d_t, d_p_filter_map, points, d_count, d_map_c_off);
cudaDeviceSynchronize();
return true;
}
bool applyRegistrationCuda1(unsigned char *d_rgb_data, unsigned short *d_depth_data, PointXYZRGBNew *points, unsigned int *d_p_filter_map, unsigned int *d_count, int nthreads){
//Apply << <(limit / 424) + 1, 424 >> >(d_map_dist, d_depth_data, d_map_x, d_color_shift_m, d_color_fx, d_color_cx, d_map_yi, d_rgb_data, d_t, d_p_filter_map, points, d_count);
Apply1 << <(limit / nthreads), nthreads >> >(d_map_dist, d_depth_data, d_map_x, d_color_shift_m, d_color_fx, d_color_cx, d_map_yi, d_p_filter_map, d_map_c_off);
cudaDeviceSynchronize();
Apply21 << <(limit / nthreads), nthreads >> >(d_map_dist, d_depth_data, d_rgb_data, d_t, d_p_filter_map, points, d_count, d_map_c_off);
cudaDeviceSynchronize();
return true;
}