我在两组3D点之间找到最近邻。
近邻: 对于数据单中的每一点(x,y,z),我必须找到该模型中最接近的一点。 (Ax-Bx)^2 + (Ay-By)^2 + (Az-Bz)^2
Here what I ve done so far:
struct point {
int x;
int y;
int z;
};
__kernel void
nearest_neighbour(__global struct point *model,
__global struct point *dataset,
__global int *nearest,
const unsigned int model_size)
{
int g_dataset_id = get_global_id(0);
int dmin = -1;
int d, dx, dy, dz;
for (int i=0; i<model_size; ++i) {
dx = model[i].x - dataset[g_dataset_id].x;
dx = dx * dx;
dy = model[i].y - dataset[g_dataset_id].y;
dy = dy * dy;
dz = model[i].z - dataset[g_dataset_id].z;
dz = dz * dz;
d = dx + dy + dz;
if(dmin == -1 || d < dmin)
{
nearest[g_dataset_id] = i;
dmin = d;
}
}
}
The code seems to work, but I m sure that it can be optimized. I would like to know how can I take advantage of the local memory to make it better.
增 编
P.S. I know that there are other (better) methods to find nearest neighbour, like kd-tree, but for now I would like to do the easy one.