diff --git a/src/regions/Octree.java b/src/regions/Octree.java index 051c452..8b261ce 100644 --- a/src/regions/Octree.java +++ b/src/regions/Octree.java @@ -1,9 +1,15 @@ package regions; +import java.io.IOException; +import java.io.OutputStream; + public class Octree extends Tree { - //LOADING AND SAVING - //================================================================== + /*---------------------------------------------------- + ------------------------------------------------------ + FROM BYTES + ------------------------------------------------------ + ----------------------------------------------------*/ Node parseBytes(IntReference index, byte[] bytes, int parentByte) { @@ -30,8 +36,38 @@ public class Octree extends Tree }); } - //CONSTRUCTOR - //================================================================== + /*---------------------------------------------------- + ------------------------------------------------------ + TO BYTES + ------------------------------------------------------ + ----------------------------------------------------*/ + + public void writeBytes(Node node, OutputStream output) + { + try + { + output.write( getByte( node.children[0], + node.children[1], + node.children[2], + node.children[3] + )); + + output.write( getByte( node.children[4], + node.children[5], + node.children[6], + node.children[7] + )); + } + catch (IOException e) { e.printStackTrace(); } + for (Node child : node.children) + writeBytes(child, output); + } + + /*---------------------------------------------------- + ------------------------------------------------------ + CONSTRUCTORS + ------------------------------------------------------ + ----------------------------------------------------*/ public Octree(Owner owner, byte[] bytes) { diff --git a/src/regions/Quadtree.java b/src/regions/Quadtree.java index 0ecc300..b1458da 100644 --- a/src/regions/Quadtree.java +++ b/src/regions/Quadtree.java @@ -1,9 +1,15 @@ package regions; +import java.io.IOException; +import java.io.OutputStream; + public class Quadtree extends Tree { - //LOADING AND SAVING - //================================================================== + /*---------------------------------------------------- + ------------------------------------------------------ + FROM BYTES + ------------------------------------------------------ + ----------------------------------------------------*/ Node parseBytes(IntReference index, byte[] bytes, int parentByte) { @@ -24,8 +30,32 @@ public class Quadtree extends Tree }); } - //CONSTRUCTOR - //================================================================== + /*---------------------------------------------------- + ------------------------------------------------------ + TO BYTES + ------------------------------------------------------ + ----------------------------------------------------*/ + + public void writeBytes(Node node, OutputStream output) + { + try + { + output.write( getByte( node.children[0], + node.children[1], + node.children[2], + node.children[3] + )); + } + catch (IOException e) { e.printStackTrace(); } + for (Node child : node.children) + writeBytes(child, output); + } + + /*---------------------------------------------------- + ------------------------------------------------------ + CONSTRUCTORS + ------------------------------------------------------ + ----------------------------------------------------*/ public Quadtree(Owner owner, byte[] bytes) { diff --git a/src/regions/Tree.java b/src/regions/Tree.java index 04730be..523aa86 100644 --- a/src/regions/Tree.java +++ b/src/regions/Tree.java @@ -171,12 +171,15 @@ public abstract class Tree return (byte) ( getBits(a) << 6 | getBits(b) << 4 | getBits(c) << 2 | - getBits(d) + getBits(d) ); } /** + * An abstract method, implemented slightly differently by octrees and quadtrees because + * octrees have 8 branches per node and quadtrees only 4.

+ * * Parses the tree rooted at this node, appending in depth-first order the result of invoking * {@link #getByte(Node, Node, Node, Node) getByte(children)} for each encountered * node in the tree, skipping childless nodes.

@@ -186,19 +189,7 @@ public abstract class Tree * @param node the node to be parsed * @return a byte array representing the node and all its child nodes */ - public static void writeBytes(Node node, OutputStream output) - { - try - { - output.write( getByte( node.children[0], - node.children[1], - node.children[2], - node.children[3] )); - } - catch (IOException e) { e.printStackTrace(); } - for (Node child : node.children) - writeBytes(child, output); - } + public abstract void writeBytes(Node node, OutputStream output); /** @@ -211,7 +202,7 @@ public abstract class Tree * @param node the root node of the tree to be parsed * @return a byte array representing the root node and all its child nodes */ - public static byte[] getBytes(Node node) + public byte[] getBytes(Node node) { ByteArrayOutputStream output = new ByteArrayOutputStream(); writeBytes(node, output); @@ -226,7 +217,7 @@ public abstract class Tree * @param node the root node of the tree to be parsed * @param destination the file to save to */ - public static void saveToFile(Node node, File destination) + public void saveToFile(Node node, File destination) { try { @@ -241,7 +232,9 @@ public abstract class Tree /*---------------------------------------------------- + ------------------------------------------------------ CONSTRUCTOR + ------------------------------------------------------ ----------------------------------------------------*/