0

我创建了这个方法来修剪 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(" } ------------------");
}

trim 方法(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新手,但我不知道为什么会这样。

4

1 回答 1

1

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

所以你实际上是在一维中通过一个向量,并且所有下一个向量都被跳过。

坦率地说,与调试器的简单会话应该已经发现了这一点。

于 2013-06-11T11:14:02.290 回答