I created this method to trim 3D arrays to use in Octrees. For some weird reason that I cant figure out after returning the trimmed array its just full of zeros. When it should be filled with ones.
Here is the code to better explain:
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)
The print matrix method (its used to only print the zeros to check if there are any) (there shouldnt be any):
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(" } ------------------");
}
The trim method (x,z,y is there to say where to start trimming so you could trim the 3D array into 8 parts for the Octree) (Note: suicide never happens meaning the voxels array never has zeros):
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)
}
Now I am not a Java newbie for any stretch of the imagination but I can not figure out why this is acting up.