Java - 3D数组修剪方法起作用



我创建了这个方法来修剪3D数组,以便在八叉树中使用。由于一些奇怪的原因,我不能弄清楚后返回修剪数组,它只是充满了零。

下面的代码可以更好地解释:

    int[][][] arr = new int[128][128][128];//Create a 3D array of size 128*128*128
    for (int y = 0; y < arr[0][0].length; y++)
        for (int x = 0; x < arr.length; x++)
            for (int z = 0; z < arr[0].length; z++)
                arr[x][z][y] = 1; //Fill the array with ones
    printMatrix(arr); //PRINT "ERROR" IF A ZERO IS FOUND (Note at this call no zeros are found)
    int[][][] arrTrim = trimMatrix(arr,0,0,0,128); //Trim the array using the method (Note in octrees I would use this method multiple times)
    printMatrix(arrTrim); //Check if there are zeros in the new trimmed array (Note: at this call the array is full of zeros)

打印矩阵方法(它只用于打印零来检查是否有)(不应该有):

    private static void printMatrix(int[][][] arr) {
    System.out.println("Arr ------------------ {");
    for (int y = 0; y < arr[0][0].length; y++)
        for (int x = 0; x < arr.length; x++)
            for (int z = 0; z < arr[0].length; z++)
                if (arr[x][z][y] == 0)
                    System.out.print(" ERROR | "+arr[x][z][y]+" at "+x+","+z+","+y+" | ");
    System.out.println(" } ------------------");
}

修剪方法(x,z,y表示从哪里开始修剪,以便您可以将3D数组修剪为八叉树的8个部分)(注意:自杀永远不会发生,意味着体素数组永远不会有零):

private static int[][][] trimMatrix(int[][][] voxel, float x, float z, float y, float size) {
    int[][][] temp = new int[voxel.length/2][voxel[0].length/2][voxel[0][0].length/2]; //Create a new temp array of half size (64)
    float varx = x * ( size/2); //Determine where to start (x can be 0 or 1)
    float vary = y * (size/2); //Determine where to start (y can be 0 or 1)
    float varz = z * (size/2); //Determine where to start (z can be 0 or 1)
    int incx = 0 , incy = 0 , incz = 0; //Increments for the temp array
    for (int yy = (int) vary; incy < temp[0][0].length; yy++, incy++) {
        for (int xx = (int) varx; incx < temp.length; xx++, incx++) {
            for (int zz = (int) varz; incz < temp[0].length; zz++, incz++) {
                temp[incx][incz][incy] = voxel[xx][zz][yy];//Set in temp the value of voxel which should be 1 (it is one)
                if (voxel[xx][zz][yy] == 0) { //if its zero kill the program (This never happens)
                    System.out.println("Suicide at "+xx+":"+incx);
                    System.exit(-1);
                }
            }
        }
    }
    return temp; //Return the new trimmed array full of ones (which turns out is full of zeros)
}

我不是一个Java新手,但是我不明白为什么这个会出错

您的内环测试是incz < temp[0].length。但你永远不会将incz重置为零。您可以在开始时将其设置为零,然后不再设置。

所以你实际上是在一维中经过一个向量,而所有下一个向量都被跳过了

坦率地说,一个简单的会话与调试器应该发现这个

最新更新