オクトリーで使用する 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 配列を Octree の 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の初心者ではありませんが、なぜこれが機能しているのか理解できません。