finished tree-to-bytes conversion

This commit is contained in:
BuildTools 2017-01-09 00:46:19 -05:00
parent dc570f5c7f
commit f7698ab16f
3 changed files with 83 additions and 24 deletions

View file

@ -1,9 +1,15 @@
package regions; package regions;
import java.io.IOException;
import java.io.OutputStream;
public class Octree extends Tree public class Octree extends Tree
{ {
//LOADING AND SAVING /*----------------------------------------------------
//================================================================== ------------------------------------------------------
FROM BYTES
------------------------------------------------------
----------------------------------------------------*/
Node parseBytes(IntReference index, byte[] bytes, int parentByte) 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) public Octree(Owner owner, byte[] bytes)
{ {

View file

@ -1,9 +1,15 @@
package regions; package regions;
import java.io.IOException;
import java.io.OutputStream;
public class Quadtree extends Tree public class Quadtree extends Tree
{ {
//LOADING AND SAVING /*----------------------------------------------------
//================================================================== ------------------------------------------------------
FROM BYTES
------------------------------------------------------
----------------------------------------------------*/
Node parseBytes(IntReference index, byte[] bytes, int parentByte) 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) public Quadtree(Owner owner, byte[] bytes)
{ {

View file

@ -171,12 +171,15 @@ public abstract class Tree
return (byte) ( getBits(a) << 6 | return (byte) ( getBits(a) << 6 |
getBits(b) << 4 | getBits(b) << 4 |
getBits(c) << 2 | 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.<p>
*
* Parses the tree rooted at this node, appending in depth-first order the result of invoking * Parses the tree rooted at this node, appending in depth-first order the result of invoking
* <tt>{@link #getByte(Node, Node, Node, Node) getByte(children)}</tt> for each encountered * <tt>{@link #getByte(Node, Node, Node, Node) getByte(children)}</tt> for each encountered
* node in the tree, skipping childless nodes.<p> * node in the tree, skipping childless nodes.<p>
@ -186,19 +189,7 @@ public abstract class Tree
* @param node the node to be parsed * @param node the node to be parsed
* @return a byte array representing the node and all its child nodes * @return a byte array representing the node and all its child nodes
*/ */
public static void writeBytes(Node node, OutputStream output) public abstract 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);
}
/** /**
@ -211,7 +202,7 @@ public abstract class Tree
* @param node the root node of the tree to be parsed * @param node the root node of the tree to be parsed
* @return a byte array representing the root node and all its child nodes * @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(); ByteArrayOutputStream output = new ByteArrayOutputStream();
writeBytes(node, output); writeBytes(node, output);
@ -226,7 +217,7 @@ public abstract class Tree
* @param node the root node of the tree to be parsed * @param node the root node of the tree to be parsed
* @param destination the file to save to * @param destination the file to save to
*/ */
public static void saveToFile(Node node, File destination) public void saveToFile(Node node, File destination)
{ {
try try
{ {
@ -241,7 +232,9 @@ public abstract class Tree
/*---------------------------------------------------- /*----------------------------------------------------
------------------------------------------------------
CONSTRUCTOR CONSTRUCTOR
------------------------------------------------------
----------------------------------------------------*/ ----------------------------------------------------*/