Gavin
2021-02-04 4e5aaefc7162b700b95c750caeff35e6323631d3
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
 
//#include <pcl/point_types.h>
//#include <pcl/gpu/containers/device_array.h>
 
// CUDA runtime
#include <cuda_runtime.h>
 
// helper functions and utilities to work with CUDA
#include <helper_cuda.h>
#include <helper_functions.h>
 
#include "kdTree.h"
 
#include <stack>
#include <stdio.h>
#include <iostream>
#include <vector>
 
 
__device__ double dev_distance(coordinate a, coordinate b) {
    double tmp = (a.x - b.x) * (a.x - b.x)
        + (a.y - b.y) * (a.y - b.y)
        + (a.z - b.z) * (a.z - b.z);
    return sqrt(tmp);
}
 
__device__ void dev_search_nearest(
    const TreeElement* treeArray,
    const int size, 
    coordinate target, 
    coordinate &nearestpoint, 
    double & distance)
{
    
    //std::stack<int> search_path;
 
    int search_path[40];
 
    int path_index = -1;
    int index = 0;
    TreeElement pSearch = treeArray[index];
 
    coordinate nearest;
    double dist;
 
    while (!pSearch.isEmpty)
    {
        path_index += 1;
        search_path[path_index] = index;
 
        if ((2 * index + 1) >= size)
        {
            break;
        }
 
        switch (pSearch.split)
        {
        case 0:
            if (target.x <= pSearch.dom_elt.x) /* Èç¹ûСÓھͽøÈë×ó×ÓÊ÷ */
            {
                index = 2 * index + 1;
                pSearch = treeArray[index];
            }
            else
            {
                index = 2 * index + 2;
                pSearch = treeArray[index];
            }
            break;
        case 1:
            if (target.y <= pSearch.dom_elt.y) /* Èç¹ûСÓھͽøÈë×ó×ÓÊ÷ */
            {
                index = 2 * index + 1;
                pSearch = treeArray[index];
            }
            else
            {
                index = 2 * index + 2;
                pSearch = treeArray[index];
            }
            break;
        default:
            if (target.z <= pSearch.dom_elt.z) /* Èç¹ûСÓھͽøÈë×ó×ÓÊ÷ */
            {
                index = 2 * index + 1;
                pSearch = treeArray[index];
            }
            else
            {
                index = 2 * index + 2;
                pSearch = treeArray[index];
            }
            break;
        }
    }
    //È¡³ösearch_path×îºóÒ»¸ö¸³¸ønearest
    index = search_path[path_index];
    nearest.x = treeArray[index].dom_elt.x;
    nearest.y = treeArray[index].dom_elt.y;
    nearest.z = treeArray[index].dom_elt.z;
    path_index -= 1;
    dist = dev_distance(nearest, target);
 
    //3. »ØËÝËÑË÷·¾¶
    TreeElement pBack;
 
    while (path_index != -1)
    {
        //È¡³ösearch_path×îºóÒ»¸ö½áµã¸³¸øpBack
        index = search_path[path_index];
        pBack = treeArray[index];
        path_index -= 1;
 
        if ((2 * index + 1) >= size) /* Èç¹ûpBackΪҶ×Ó½áµã */
        {
            if (dev_distance(nearest, target) > dev_distance(pBack.dom_elt, target))
            {
                nearest = pBack.dom_elt;
                dist = dev_distance(pBack.dom_elt, target);
            }
        }
        else if (treeArray[2 * index + 1].isEmpty == true && treeArray[2 * index + 2].isEmpty == true) /* Èç¹ûpBackΪҶ×Ó½áµã */
        {
            if (dev_distance(nearest, target) > dev_distance(pBack.dom_elt, target))
            {
                nearest = pBack.dom_elt;
                dist = dev_distance(pBack.dom_elt, target);
            }
        }
        else
        {
            UL s = pBack.split;
            switch (s)
            {
            case 0:
                if (fabs(pBack.dom_elt.x - target.x) < dist) /* Èç¹ûÒÔtargetΪÖÐÐĵÄÔ²£¨Çò»ò³¬Çò£©£¬°ë¾¶ÎªdistµÄÔ²Óë·Ö¸î³¬Æ½ÃæÏཻ£¬ ÄÇô¾ÍÒªÌøµ½ÁíÒ»±ßµÄ×Ó¿Õ¼äÈ¥ËÑË÷ */
                {
                    if (dev_distance(nearest, target) > dev_distance(pBack.dom_elt, target))
                    {
                        nearest = pBack.dom_elt;
                        dist = dev_distance(pBack.dom_elt, target);
                    }
 
                    if (target.x <= pBack.dom_elt.x) /* Èç¹ûtargetλÓÚpBackµÄ×ó×ӿռ䣬ÄÇô¾ÍÒªÌøµ½ÓÒ×Ó¿Õ¼äÈ¥ËÑË÷ */
                    {
                        index = 2 * index + 2;
                        pSearch = treeArray[index];
                    }
                    else                            /* Èç¹ûtargetλÓÚpBackµÄÓÒ×ӿռ䣬ÄÇô¾ÍÒªÌøµ½×ó×Ó¿Õ¼äÈ¥ËÑË÷ */
                    {
                        index = 2 * index + 1;
                        pSearch = treeArray[index];
                    }
                    if (!pSearch.isEmpty)       //pSearch¼ÓÈëµ½search_pathÖÐ
                    {
                        path_index += 1;
                        search_path[path_index] = index;
                    }
                }
                break;
            case 1:
                if (fabs(pBack.dom_elt.y - target.y) < dist) /* Èç¹ûÒÔtargetΪÖÐÐĵÄÔ²£¨Çò»ò³¬Çò£©£¬°ë¾¶ÎªdistµÄÔ²Óë·Ö¸î³¬Æ½ÃæÏཻ£¬ ÄÇô¾ÍÒªÌøµ½ÁíÒ»±ßµÄ×Ó¿Õ¼äÈ¥ËÑË÷ */
                {
                    if (dev_distance(nearest, target) > dev_distance(pBack.dom_elt, target))
                    {
                        nearest = pBack.dom_elt;
                        dist = dev_distance(pBack.dom_elt, target);
                    }
                    if (target.y <= pBack.dom_elt.y) /* Èç¹ûtargetλÓÚpBackµÄ×ó×ӿռ䣬ÄÇô¾ÍÒªÌøµ½ÓÒ×Ó¿Õ¼äÈ¥ËÑË÷ */
                    {
                        index = 2 * index + 2;
                        pSearch = treeArray[index];
                    }
                    else                             /* Èç¹ûtargetλÓÚpBackµÄÓÒ×ӿռ䣬ÄÇô¾ÍÒªÌøµ½×ó×Ó¿Õ¼äÈ¥ËÑË÷ */
                    {
                        index = 2 * index + 1;
                        pSearch = treeArray[index];
                    }
                    if (!pSearch.isEmpty)       //pSearch¼ÓÈëµ½search_pathÖÐ
                    {
                        path_index += 1;
                        search_path[path_index] = index;
                    }
                }
                break;
            default:
                if (fabs(pBack.dom_elt.z - target.z) < dist) /* Èç¹ûÒÔtargetΪÖÐÐĵÄÔ²£¨Çò»ò³¬Çò£©£¬°ë¾¶ÎªdistµÄÔ²Óë·Ö¸î³¬Æ½ÃæÏཻ£¬ ÄÇô¾ÍÒªÌøµ½ÁíÒ»±ßµÄ×Ó¿Õ¼äÈ¥ËÑË÷ */
                {
                    if (dev_distance(nearest, target) > dev_distance(pBack.dom_elt, target))
                    {
                        nearest = pBack.dom_elt;
                        dist = dev_distance(pBack.dom_elt, target);
                    }
                    if (target.z <= pBack.dom_elt.z) /* Èç¹ûtargetλÓÚpBackµÄ×ó×ӿռ䣬ÄÇô¾ÍÒªÌøµ½ÓÒ×Ó¿Õ¼äÈ¥ËÑË÷ */
                    {
                        index = 2 * index + 2;
                        pSearch = treeArray[index];
                    }
                    else                            /* Èç¹ûtargetλÓÚpBackµÄÓÒ×ӿռ䣬ÄÇô¾ÍÒªÌøµ½×ó×Ó¿Õ¼äÈ¥ËÑË÷ */
                    {
                        index = 2 * index + 1;
                        pSearch = treeArray[index];
                    }
                    if (!pSearch.isEmpty)       //pSearch¼ÓÈëµ½search_pathÖÐ
                    {
                        path_index += 1;
                        search_path[path_index] = index;
                    }
                }
                break;
            }
        }
 
    }
 
    nearestpoint.x = nearest.x;
    nearestpoint.y = nearest.y;
    nearestpoint.z = nearest.z;
    distance = dist;
}
 
__global__ void search_all_nearest(
    TreeElement *tree_array, 
    const int *size, 
    const coordinate *points, 
    const coordinate *normals,
    coordinate *nearest, 
    double *distance)
{
    int Id = blockIdx.x * 4 + threadIdx.x;
    //printf("Block id:%d,Thread id:%d,points:%f  \n", blockIdx.x, threadIdx.x, points[Id].x);
 
    dev_search_nearest(tree_array, *size, points[Id], nearest[Id], distance[Id]);
    double delta_x = points[Id].x - nearest[Id].x;
    double delta_y = points[Id].y - nearest[Id].y;
    double delta_z = points[Id].z - nearest[Id].z;
    distance[Id] = 
        normals[Id].x * delta_x +
        normals[Id].y * delta_y +
        normals[Id].z * delta_z;
}
 
__global__ void search_all_nearest_icp(
    TreeElement *tree_array,
    const int *size,
    coordinate *points,
    const coordinate *normals,
    coordinate *nearest,
    double *distance)
{
    int Id = blockIdx.x * 1000 + threadIdx.x;
    
    dev_search_nearest(tree_array, *size, points[Id], nearest[Id], distance[Id]);
    /*double delta_x = points[Id].x - nearest[Id].x;
    double delta_y = points[Id].y - nearest[Id].y;
    double delta_z = points[Id].z - nearest[Id].z;
    distance[Id] =
        normals[Id].x * delta_x +
        normals[Id].y * delta_y +
        normals[Id].z * delta_z;*/
 
}
 
__global__ void rotate_points(
    coordinate *points,
    double *rotate_matrix)
{
    int Id = blockIdx.x * 1000 + threadIdx.x;
    //printf("Block id:%d,Thread id:%d,points:%f  \n", blockIdx.x, threadIdx.x, points[Id].x);
 
    points[Id].x = points[Id].x * rotate_matrix[0] + points[Id].y * rotate_matrix[1] + points[Id].z * rotate_matrix[2];
    points[Id].y = points[Id].x * rotate_matrix[3] + points[Id].y * rotate_matrix[4] + points[Id].z * rotate_matrix[5];
    points[Id].z = points[Id].x * rotate_matrix[6] + points[Id].y * rotate_matrix[7] + points[Id].z * rotate_matrix[8];
}
 
__global__ void transfer_points(
    coordinate *points,
    coordinate *dev_transfer_vector)
{
    int Id = blockIdx.x * 1000 + threadIdx.x;
 
    points[Id].x += dev_transfer_vector->x;
    points[Id].y += dev_transfer_vector->y;
    points[Id].z += dev_transfer_vector->z;
}
 
extern "C" bool get_distance(
    TreeElement *tree_array,
    const int *size,
    const coordinate *points,
    const coordinate *normals,
    coordinate *nearest,
    double *distance)
{
    //dim3 cudaBlockSize(256, 1, 1);
    //dim3 cudaGridSize((1000 + cudaBlockSize.x - 1) / cudaBlockSize.x, 1, 1);
    search_all_nearest << <50000, 4 >> > (
        tree_array, 
        size, 
        points, 
        normals, 
        nearest, 
        distance);
    return true;
}
 
extern "C" bool get_distance_icp(
    TreeElement *tree_array,
    const int *size,
    coordinate *points,
    const coordinate *normals,
    coordinate *nearest,
    double *distance)
{
    search_all_nearest_icp << <20, 1000 >> > (
        tree_array, 
        size, 
        points, 
        normals, 
        nearest, 
        distance);
    return true;
}
 
extern "C" bool rotate_points_host(
    coordinate *points,
    double *rotate_matrix)
{
    rotate_points << <20, 1000 >> > (points, rotate_matrix);
    return true;
}
 
extern "C" bool transfer_points_host(
    coordinate *points,
    coordinate *dev_transfer_vector)
{
    transfer_points << <20, 1000 >> > (points, dev_transfer_vector);
    return true;
}