Three-dimensional (1D flat) indexing

I use the coordinate system x (width), y (height), z (depth)

Just to clear the confusion, if there is x and y is a flat plane, and I use Z as the height.

I'm going to access the array millions of times per second, and benchmarking shows that a 1D array using an index is faster, and I would like to compress as much efficiency as possible so that other things can use this time

For example, creating a 2D β†’ 1D array is easy

Object[] oneDArray = new Object[width * height]

and to index the array, I can simply use the following.

Object obj = oneDArray[x + y * width]

I found the following in stackoverflow, but I'm not quite sure which one is correct. How do I smooth the "or" index "of a 3D array in a 1D array?

The answer "Correct" says to index the array as follows

Object[] oneDArray = new Object[width * height * depth]
Object obj = oneDArray[x + WIDTH * (y + DEPTH * z)]

, ""

Object[] oneDArray = new Object[width * height * depth]
Object obj = oneDArray[x + HEIGHT* (y + WIDTH* z)]

3D-?

+4
3

, 3D- 1D, : Z, Y, X, 2x2x2 :

index 0: [z=0,y=0,x=0]
index 1: [z=0,y=0,x=1]
index 2: [z=0,y=1,x=0]
index 3: [z=0,y=1,x=1]
index 4: [z=1,y=0,x=0]
index 5: [z=1,y=0,x=1]
index 6: [z=1,y=1,x=0]
index 7: [z=1,y=1,x=1]

DEPTH z, HEIGHT - y WIDTH - x

: index = HEIGHT*WIDTH*z + WIDTH*y + x.

x , x .

Y-, WIDTH, 2, , 1, z = 0, y = 0 x = 1, WIDTH = 2 , 3. y 1.

z = 0 z = 1, 4 ( ), HEIGHT * WIDTH ( 2 * 2).

, 3D- z, y, x, , . :

int z = 1, y=1, x=0;
int index = HEIGHT*WIDTH*z + WIDTH*y;
int data;

for(x=0;x<WIDTH;x++)
{
    Object obj = oneDArray[index+x];
}

, , oneDArray. , .

+5

index = x*height*depth + y*depth + z java-.

public class FlattenedArray {
    public static void main(String[] args) {
        int width = 2;
        int height = 3;
        int depth = 4;
        int[][][] d3 = new int[width][height][depth];
        int[] d1 = new int[width*height*depth];

        //3D Array :
        int w=0;
        for(int i=0;i<width;i++) 
            for(int j=0;j<height;j++)
                for(int k=0;k<depth;k++) {
                    d3[i][j][k] = ++w;
                    System.out.print(d3[i][j][k] + " ");
                }
        System.out.println();

        //1D Array :
        w=0;
        for(int i=0;i<width;i++) 
            for(int j=0;j<height;j++)
                for(int k=0;k<depth;k++) {
                    int index = i*height*depth + j*depth + k;
                    d1[index] = ++w;
                }

        for(int i=0;i<width*height*depth;i++) {
            System.out.print(d1[i] + " ");
        }
    }
}
0

Java, :

  • 3D 1D
  • 1D 3D

, 1D- 50% , / , .

​​ , 3D-, :

2 Examples of 3D matrices

:

public int to1D( int x, int y, int z ) {
    return (z * xMax * yMax) + (y * xMax) + x;
}

public int[] to3D( int idx ) {
    final int z = idx / (xMax * yMax);
    idx -= (z * xMax * yMax);
    final int y = idx / xMax;
    final int x = idx % xMax;
    return new int[]{ x, y, z };
}

, , , , , 2 ;)

0

All Articles