Saturday, 15 February 2014

algorithm - Indexing a 3d array into a 1d array - neighbor finding -


Then I have a 3D array that represents a bounding volume (uniform grid of "cell") which is 1D I have flattened array so that I can use it in CUDA.

The problem I am facing is that I should be able to take any "cell" in my uniform grid and get all its neighbors. It's easy when you have 3d array (it's just 3 to 3 loops for 1 and 1 is correctly indexing in 3D array), but in a version which works on the 1D array I'm terrible in trying to break it down. There should be a simple formula that will calculate it, but I can not understand it.

Also, the edges should not be loop, which means that the cell should not be in the lower left hand corner, the "neighbor" of the cell in the bottom right corner.

Any help is appreciated. Thank you.

You can place 3 for the loop. Just check the boundaries in each dimension and calculate the index by nested extra / multiply. ( i1 and i {For (int i3 = -1; i3 & lt; = 1; ++ i3) {const int x1 = p1 + i1; Const int x2 = p2 + i2; Const int x3 = p3 + i3; If (x1> = 0 & amp; x1 & lt; n1 and x2 & gt; = 0 & amp; amp; x2 & lt; n2 & amp; x3 & gt; = 0 & amp; amp; And & x3 & lt; n3) {neighboring = array [(x1 * n2 + x2) * n3 + x3]; }}}}


No comments:

Post a Comment