Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. public static List<Box> listBox(boolean[][][] immutableBlocks, int width, int height, int depth)
  2. {
  3. List<Box> list = Lists.newArrayList();
  4.  
  5. boolean[][][] clone = immutableBlocks.clone();
  6.  
  7. int i = 0;
  8.  
  9. Box next;
  10. while ((next = nextBox(clone, width, height, depth)) != null)
  11. {
  12. list.add(next);
  13. i ++;
  14. if (1024 <= i) throw new RuntimeException("Over capacity: Detected too many boxes. May be bug?");
  15. }
  16.  
  17. return list;
  18. }
  19.  
  20. public static Box nextBox(boolean[][][] mutableBlocks, int width, int height, int depth)
  21. {
  22. for (int x=0; x<width; x++)
  23. {
  24. for (int y=0; y<height; y++)
  25. {
  26. for (int z=0; z<depth; z++)
  27. {
  28. if (mutableBlocks[x][y][z])
  29. {
  30. Box box = nextBoxAt(mutableBlocks, x, y, z, width, height, depth);
  31.  
  32. int edge = box.width();
  33.  
  34. for (int dx=0; dx<edge; dx++)
  35. for (int dy=0; dy<edge; dy++)
  36. for (int dz=0; dz<edge; dz++)
  37. mutableBlocks[x +dx][y +dy][z +dz] = false; // TODO: Edge Decisionに統合する余地あり
  38.  
  39. return box;
  40. }
  41. }
  42. }
  43. }
  44.  
  45. return null;
  46. }
  47.  
  48. public static Box nextBoxAt(boolean[][][] immutableBlocks, int x, int y, int z, int width, int height, int depth) // 現在立方体 直方体にしよう
  49. {
  50. // 座標 0,0,0 はtrueでなければなりません
  51. if (! immutableBlocks[x][y][z]) throw new IllegalArgumentException("始点座標 x,y,z はfalseにできません");
  52.  
  53. int maxEdge = Math.min(Math.min(width -x, height -y), depth -z);
  54.  
  55. // ここでは、最も外側の薄皮にブロックが満たされているか検査しています
  56. int edge;
  57. edge: for (edge=0; edge<maxEdge;)
  58. {
  59. edge ++;
  60.  
  61. // XZ_Check 内外のXZ平面を検査
  62. for (int ex=0; ex<edge; ex++)
  63. for (int ez=0; ez<edge; ez++)
  64. {
  65. if (! immutableBlocks[x +ex][y][z +ez]) // TODO: 検査箇所が重複するので非効率 原点方向については最適化の余地あり
  66. break edge;
  67. if (! immutableBlocks[x +ex][y +edge -1][z +ez])
  68. break edge;
  69. }
  70.  
  71. // XY_Check 内外のXY平面を検査
  72. for (int ex=0; ex<edge; ex++)
  73. for (int ey=0; ey<edge; ey++)
  74. {
  75. if (! immutableBlocks[x +ex][y +ey][z])
  76. break edge;
  77. if (! immutableBlocks[x +ex][y +ey][z +edge -1])
  78. break edge;
  79. }
  80.  
  81. // YZ_Check 内外のYZ平面を検査
  82. for (int ey=0; ey<edge; ey++)
  83. for (int ez=0; ez<edge; ez++)
  84. {
  85. if (! immutableBlocks[x][y +ey][z +ez])
  86. break edge;
  87. if (! immutableBlocks[x +edge -1][y +ey][z +ez])
  88. break edge;
  89. }
  90. } // End of Edge Decision Block
  91.  
  92. System.out.println("Edge len: " + edge);
  93.  
  94. return new Box(x, y, z, x +edge-1, y +edge-1, z +edge-1);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement