Example #1
0
  @Test
  public void test1() {
    Random random = new Random(228);

    Node root = EmptyNode.instance;
    for (int i = 0; i < 10; i++) {
      root = root.merge(new NotEmptyNode(i, random.nextInt()));

      System.out.println(root.size());
    }

    Node.SplitResult split = root.split(5);
  }
Example #2
0
  /**
   * Set the voxel type at the given coordinates.
   *
   * @param type The new voxel type to be set
   * @param x
   * @param y
   * @param z
   */
  public synchronized void set(int type, int x, int y, int z) {
    Node node = root;
    int parentLvl = depth - 1;
    int level = parentLvl;
    for (int i = depth - 1; i >= 0; --i) {
      level = i;
      parents[i] = node;

      if (node.type == type) {
        return;
      } else if (node.children == null) {
        node.subdivide();
        parentLvl = i;
      }

      int xbit = 1 & (x >> i);
      int ybit = 1 & (y >> i);
      int zbit = 1 & (z >> i);
      node = node.children[(xbit << 2) | (ybit << 1) | zbit];
    }
    node.type = type;

    // merge nodes where all children have been set to the same type
    for (int i = level; i <= parentLvl; ++i) {
      Node parent = parents[i];

      boolean allSame = true;
      for (Node child : parent.children) {
        if (child.type != node.type) {
          allSame = false;
          break;
        }
      }

      if (allSame) {
        parent.merge(node.type);
        cacheLevel = FastMath.max(i, cacheLevel);
      } else {
        break;
      }
    }
  }