update, TreeBuilder, BitRegion, and UtilBitSet

still writing some of the methods in BitRegion
This commit is contained in:
BuildTools 2017-01-16 16:53:56 -05:00
parent 744d206a6a
commit 35fa13229a
9 changed files with 649 additions and 110 deletions

300
src/regions/BitRegion.java Normal file
View file

@ -0,0 +1,300 @@
package regions;
import java.util.BitSet;
public class BitRegion implements UtilBitSet
{
private int[] min; public int[] getMin() { return min; }
private int[] max; public int[] getMax() { return max; }
private int[] minTrue; public int[] getMinTrue() { return minTrue; }
private int[] maxTrue; public int[] getMaxTrue() { return maxTrue; }
public final boolean is3D;
protected final BitSet blocks;
/**
*
*
* @param min
*/
public BitRegion(int minX, int minZ)
{
this.min = new int[] { minX, minZ };
this.is3D = false;
this.blocks = new BitSet();
}
/**
*
*
* @param min
*/
public BitRegion(int minX, int minZ, int minY)
{
this.min = new int[] { minX, minZ, minY };
this.is3D = true;
this.blocks = new BitSet();
}
/*
CALCULATIONS
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
GETTERS
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/**
*
* @return
*/
public int getBoxVolume()
{
if (max == null) return 0;
return is3D ?
(max[0] - min[0]) * (max[1] - min[1]) * (max[2] - min[2]) :
(max[0] - min[0]) * (max[1] - min[1]);
}
/**
*
* @return
*/
public int getVolume()
{
return blocks.cardinality();
}
/*-------------------------------------
OVERLOADS : getIndex()
-------------------------------------*/
/**
*
* @param coordinate
* @return
*/
public int getIndex(int x, int z)
{
return getIndex(x, z, min[0], min[1]);
}
/**
*
* @param coordinate
* @return
*/
public int getIndex(int x, int z, int y)
{
return getIndex(x, z, y, min[0], min[1], min[2]);
}
/**
*
* @param coordinate
* @param blocks
* @param minX
* @param maxX
* @param minZ
* @param maxZ
* @return
*/
public static int getIndex(int x, int z, int minX, int minZ)
{
return (x - minX) + (z - minZ);
}
/**
*
* @param coordinate
* @param blocks
* @param minX
* @param maxX
* @param minZ
* @param maxZ
* @param minY
* @param maxY
* @return
*/
public static int getIndex(int x, int z, int y, int minX, int minZ, int minY)
{
return (x - minX) + (z - minZ) + (y - minY);
}
/*-------------------------------------
OVERLOADS : getCoords()
-------------------------------------*/
/**
*
* @param coordinate
* @return
*/
public int getCoords(int index)
{
return is3D ?
getIndex(index, min[0], min[1], min[2]) :
getIndex(index, min[0], min[1]);
}
/**
*
* @param coordinate
* @param blocks
* @param minX
* @param maxX
* @param minZ
* @param maxZ
* @return
*/
public static int getCoords(int index, int minX, int minZ)
{
return (x - minX) + (z - minZ);
}
/**
*
* @param coordinate
* @param blocks
* @param minX
* @param maxX
* @param minZ
* @param maxZ
* @param minY
* @param maxY
* @return
*/
public static int getCoords(int index, int minX, int minZ, int minY)
{
return (x - minX) + (z - minZ) + (y - minY);
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
fill() and clear()
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/*-------------------------------------
OVERLOADS : fill()
-------------------------------------*/
/**
*
*/
public void fill()
{
blocks.set(0, getBoxVolume() - 1);
}
/**
*
*
* @param min
* @param max
*/
public void fill(int[] min, int[] max)
{
}
/**
*
*
* @param min
* @param max
* @param blocks
*/
public void fill(int minX, int minZ, int maxX, int maxZ, BitSet blocksToFill)
{
BitSet row;
}
/**
*
*
* @param min
* @param max
* @param blocks
*/
public void fill(int minX, int minZ, int minY, int maxX, int maxZ, int maxY, BitSet blocksToFill)
{
BitSet row;
}
/*-------------------------------------
OVERLOADS : clear()
-------------------------------------*/
/**
*
*/
public void clear()
{
blocks.clear();
}
/**
*
*
* @param min
* @param max
*/
public void clear(int[] min, int[] max)
{
}
/**
*
*
* @param min
* @param max
* @param blocks
*/
public void clear(int minX, int minZ, int maxX, int maxZ, BitSet blocksToClear)
{
BitSet row;
}
/**
*
*
* @param min
* @param max
* @param blocks
*/
public void clear(int minX, int minZ, int minY, int maxX, int maxZ, int maxY, BitSet blocksToClear)
{
BitSet row;
int index = 0;
for (int y = minY; y <= maxY; y++)
{
for (int z = minZ; z <= maxZ; z++)
{
for (int x = minX; x <= maxX; x++)
{
}
}
}
}
}

View file

@ -7,18 +7,28 @@ import java.io.OutputStream;
public class Octree extends Tree
{
/*
BYTE CONVERSION
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
FROM BYTES
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
@Override
public Node parseBytes(DataInputStream input, int parentByte) throws IOException
{
if (parentByte == 0b00000010) return new Node(true);
if (parentByte == 0b00000001) return new Node(false);
int a = input.read(),
int a = input.read(), //returns -1 if there are no more bytes
b = input.read();
return a == -1 || b == -1 ?
@ -41,6 +51,7 @@ public class Octree extends Tree
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
@Override
public void writeBytes(Node node, OutputStream output)
{
try
@ -64,18 +75,47 @@ public class Octree extends Tree
writeBytes(child, output);
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
CONSTRUCTORS
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/*
CONSTRUCTORS
*/
public Octree(int[][] bounds, File file, byte[] bytes)
{
super(bounds, file);
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
Methods used by constructors
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
@Override
OctreeEditor newEditor()
{
return new OctreeEditor(this);
}
/*
CALCULATIONS
*/
@Override
public Node[] getNodes(Node parentNode, int[][] regionBounds)
{
// TODO Auto-generated method stub
return null;
}
}

View file

@ -1,11 +0,0 @@
package regions;
public class OctreeEditor implements TreeEditor
{
private final Octree tree;
OctreeEditor(Octree tree)
{
this.tree = tree;
}
}

View file

@ -7,18 +7,28 @@ import java.io.OutputStream;
public class Quadtree extends Tree
{
/*
BYTE CONVERSION
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
FROM BYTES
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
@Override
public Node parseBytes(DataInputStream input, int parentByte) throws IOException
{
if (parentByte == 0b00000010) return new Node(true);
if (parentByte == 0b00000001) return new Node(false);
int a = input.read();
int a = input.read(); //returns -1 if there are no more bytes
return a == -1 ?
new Node( false ) :
@ -35,6 +45,7 @@ public class Quadtree extends Tree
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
@Override
public void writeBytes(Node node, OutputStream output)
{
try
@ -52,18 +63,48 @@ public class Quadtree extends Tree
writeBytes(child, output);
}
/*
CONSTRUCTORS
*/
public Quadtree(File file)
{
super(file);
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
CONSTRUCTORS
Methods used by constructors
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
public Quadtree(int[][] bounds, File file)
{
super(bounds, file);
}
public QuadtreeEditor newEditor()
@Override
final QuadtreeEditor newEditor()
{
return new QuadtreeEditor(this);
}
/*
CALCULATIONS
*/
@Override
public Node[] getNodes(Node parentNode, int[][] regionBounds)
{
// TODO Auto-generated method stub
return null;
}
}

View file

@ -1,11 +0,0 @@
package regions;
public class QuadtreeEditor implements TreeEditor
{
private final Quadtree tree;
QuadtreeEditor(Quadtree tree)
{
this.tree = tree;
}
}

View file

@ -5,7 +5,6 @@ import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@ -54,16 +53,19 @@ public abstract class Tree
this.full = false;
this.children = nodes;
}
public Node()
{
this.full = false;
this.children = new Node[0];
}
}
/*
BYTE CONVERSION
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
FROM BYTES
@ -72,13 +74,12 @@ public abstract class Tree
/**
* An abstract method, implemented slightly differently by octrees and quadtrees because
* octrees have 8 branches per node and quadtrees only 4.<p>
*
* Interprets each byte as four 2-bit values, where 00 = partial, 01 = empty (<tt>false</tt>)
* Reads each byte as four 2-bit values, where 00 = partial, 01 = empty (<tt>false</tt>)
* and 10 = full (<tt>true</tt>). Octree nodes use 2 bytes each, Quadtree nodes use 1 byte each.
* Assumes a depth-first arrangement.<p>
*
* Does not close the input stream.
*
* @param input DataInputStream of source bytes
* @param parentBits the 2-bit value (00, 01, or 10)
* @return a new Node
@ -88,22 +89,24 @@ public abstract class Tree
/**
* Interprets each byte as four 2-bit values, where 00 = partial, 01 = empty (<tt>false</tt>)
* Reads each byte as four 2-bit values, where 00 = partial, 01 = empty (<tt>false</tt>)
* and 10 = full (<tt>true</tt>). Octree nodes use 2 bytes each, Quadtree nodes use 1 byte each.
* Assumes a depth-first arrangement.<p>
*
* @param input DataInputStream containing source bytes
* Does not close the input stream.
*
* @param input DataInputStream of source bytes
* @return a new Node
*/
public Node parseBytes(DataInputStream input)
{
try { return parseBytes(input, 0); }
catch (IOException e) { return new Node(); }
catch (IOException e) { return new Node(false); }
}
/**
* Interprets each byte as four 2-bit values, where 00 = partial, 01 = empty (<tt>false</tt>)
* Reads each byte as four 2-bit values, where 00 = partial, 01 = empty (<tt>false</tt>)
* and 10 = full (<tt>true</tt>). Octree nodes use 2 bytes each, Quadtree nodes use 1 byte each.
* Assumes a depth-first arrangement.<p>
*
@ -112,17 +115,22 @@ public abstract class Tree
*/
public Node parseBytes(File file)
{
if (file.getUsableSpace() == 0)
if (file.length() == 0)
{
return new Node();
return new Node(false);
}
try
{
return parseBytes(new DataInputStream(new BufferedInputStream(new FileInputStream(file))));
DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
Node node = parseBytes(input);
input.close();
return node;
}
catch (FileNotFoundException e)
catch (IOException e)
{
return new Node();
return new Node(false);
}
}
@ -173,9 +181,6 @@ public abstract class Tree
/**
* 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
* <tt>{@link #getByte(Node, Node, Node, Node) getByte(children)}</tt> for each encountered
* node in the tree, skipping childless nodes.<p>
@ -207,12 +212,12 @@ public abstract class Tree
}
/**
* Parses the tree from this node, appending in depth-first order the result of invoking
* Parses the tree below this node, appending in depth-first order the result of invoking
* <tt>{@link #getByte(Node, Node, Node, Node) getByte(children)}</tt> for each encountered
* node in the tree, skipping childless nodes.<p>
*
* @param node the node to parse from
* @return byte array representing the root node and all its child nodes
* @return byte array representing the given node and all its child nodes
*/
public byte[] getBytes(Node node)
{
@ -226,32 +231,28 @@ public abstract class Tree
* <tt>{@link #getByte(Node, Node, Node, Node) getByte(children)}</tt> for each encountered
* node in the tree, skipping childless nodes.<p>
*
* Writes to the given ByteArrayOutputStream.
* Writes to the given OutputStream, does not close the stream.
*
* @param output the ByteArrayOutputStream to write to
* @return byte array representing the root node and all its child nodes
*/
public byte[] getBytes(ByteArrayOutputStream output)
public void getBytes(OutputStream output)
{
writeBytes(root, output);
return output.toByteArray();
}
/**
* Parses the tree from this node, appending in depth-first order the result of invoking
* Parses the tree below this node, appending in depth-first order the result of invoking
* <tt>{@link #getByte(Node, Node, Node, Node) getByte(children)}</tt> for each encountered
* node in the tree, skipping childless nodes.<p>
*
* Writes to the given ByteArrayOutputStream.
* Writes to the given OutputStream, does not close the stream.
*
* @param node the node to parse from
* @param output the ByteArrayOutputStream to write to
* @return byte array representing the root node and all its child nodes
*/
public byte[] getBytes(Node node, ByteArrayOutputStream output)
public void getBytes(Node node, OutputStream output)
{
writeBytes(node, output);
return output.toByteArray();
}
@ -266,7 +267,7 @@ public abstract class Tree
* @param node the root node of the tree to be parsed
* @param destination the file to save to
*/
public void saveToFile()
public void saveToFile()//TODO erase existing file contents before writing
{
try
{
@ -274,8 +275,7 @@ public abstract class Tree
writeBytes(root, output);
output.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
}
/**
@ -285,7 +285,7 @@ public abstract class Tree
* @param node the root node of the tree to be parsed
* @param destination the file to save to
*/
public void saveToFile(Node node)
public void saveToFile(Node node)//TODO erase existing file contents before writing
{
try
{
@ -293,8 +293,7 @@ public abstract class Tree
writeBytes(node, output);
output.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
}
/**
@ -304,7 +303,7 @@ public abstract class Tree
* @param node the root node of the tree to be parsed
* @param destination the file to save to
*/
public void saveToFile(File destination)
public void saveToFile(File destination)//TODO erase existing file contents before writing
{
try
{
@ -312,8 +311,7 @@ public abstract class Tree
writeBytes(root, output);
output.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
}
/**
@ -323,7 +321,7 @@ public abstract class Tree
* @param node the root node of the tree to be parsed
* @param destination the file to save to
*/
public void saveToFile(Node node, File destination)
public void saveToFile(Node node, File destination)//TODO erase existing file contents before writing
{
try
{
@ -331,48 +329,105 @@ public abstract class Tree
writeBytes(node, output);
output.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
}
/*
CONSTRUCTORS
*/
private int[] min; public int[] getMin() { return min; }
private int[] max; public int[] getMax() { return max; }
private int[] minTrue; public int[] getMinTrue() { return minTrue; }
private int[] maxTrue; public int[] getMaxTrue() { return maxTrue; }
public final File file;
public final Node root;
public final TreeEditor<?> editor;
/**
* Create a Tree from the given binary file. Invokes <tt>parseBytes()</tt>
*
* @param file The source file, and save destination, for this Tree.
* @see {@link #parseBytes(File)}
*/
public Tree(File file)
{
setBoundsFromFilename(file);
this.file = file;
this.root = parseBytes(file);
this.editor = newEditor();
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
CONSTRUCTOR
Methods used by constructors
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/**
* Minimum and maximum bounds [ [min] [max] ].
* Quadtrees store [x,z], Octrees store [x,z,y].
*/
public int[][] bounds;
/**
* Minimum and maximum bounds [ [min] [max] ].
* Quadtrees store [x,z], Octrees store [x,z,y].<p>
/**
* Set bounds of this tree from values specified
* in the filename of the given source file.
*
* <b>Bounds of the shape itself,
* not including empty nodes.
* @param file the source file to examine
*/
public int[][] boundsTrue;
public final File file;
public final Node root;
public final TreeEditor editor;
public Tree(int[][] bounds, File file)
private static void setBoundsFromFilename(File file)
{
this.bounds = bounds;
this.file = file;
this.root = parseBytes(file);
this.editor = newEditor();
//TODO finish setBoundsFromFilename() method
}
/**
* An abstract method, returning a new subclass-specific
* instance implementing the interface TreeEditor
* @return a new TreeEditor
* Abstract method, returns a new object
* extending the abstract class TreeEditor
*
* @return a new TreeEditor
*/
abstract TreeEditor newEditor();
abstract TreeEditor<? extends TreeEditor.Edit> newEditor();
/*
CALCULATIONS
*/
/**
*
* @param parentNode
* @param regionBounds
* @return
*/
public abstract Node[] getNodes(Node parentNode, int[][] regionBounds);
/**
*
* @param regionBounds
* @return
*/
public Node[] getNodes(int[][] regionBounds)
{
return getNodes(root, regionBounds);
}
}

View file

@ -0,0 +1,16 @@
package regions;
import java.util.BitSet;
public class TreeBuilder<T extends Tree> extends BitRegion
{
public TreeBuilder(int minX, int minZ)
{
super(minX, minZ);
}
public TreeBuilder(int minX, int minZ, int minY)
{
super(minX, minZ, minY);
}
}

View file

@ -6,11 +6,69 @@ package regions;
* @author Kevin Mathewson
*
*/
public interface TreeEditor
public abstract class TreeEditor<T extends TreeEditor.Edit>
{
public abstract class ChangeQueue<T extends Edit>{}
/**
* Abstract class describing an edit to a Tree object. In general, defines a selection
* of blocks, specifying <tt>true</tt>, <tt>false</tt>, or <tt>no change</tt> for each.
* Dimensions are square or cubic, for quadtrees and octrees respectively.
*/
static abstract class Edit
{
/**
* Minimum and maximum bounds [ [min] [max] ].
* Quadtree edits have bounds [x,z], Octree edits have bounds [x,z,y].
*/
private final int[][] bounds;
private int[][] boundsTrue;
private boolean containsTrue = false;
Edit(int[][] bounds)
{
this.bounds = bounds;
this.boundsTrue = this.bounds;
}
}
public abstract class Edit{}
/**
* Aggregator for all edits waiting to be added. Merges all incoming edits
* into a growing 'super-edit' that is periodically written to the tree and flushed.
*/
static class EditQueue<T extends Edit>
{
private final TreeEditor editor;
private volatile Edit superEdit;
EditQueue(TreeEditor editor, Edit superEdit)
{
this.editor = editor;
this.superEdit = superEdit;
}
public synchronized T merge(T edit1, T edit2)
{
return null;
}
}
public void edit(Edit edit);
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
CONSTRUCTOR
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
private final EditQueue changeQueue;
public TreeEditor(EditQueue changeQueue)
{
this.changeQueue = changeQueue;
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
METHODS
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
abstract void addEdit(Edit edit);
}

View file

@ -0,0 +1,51 @@
package regions;
import java.util.BitSet;
public interface UtilBitSet
{
/*-------------------------------------
OVERLOADS : shift()
-------------------------------------*/
/**
*
* @param bitset
* @param shift
* @return
*/
default BitSet shift(BitSet bitset, int shift)
{
if (shift == 0) return bitset;
if (shift > 0)
{
for (int i = bitset.length() - 1; i > -1; i--)
{
bitset.set(i + shift, bitset.get(i));
}
return bitset;
}
else
{
int length = bitset.length();
for (int i = -shift; i < length; i++)
{
bitset.set(i + shift, bitset.get(i));
}
bitset.clear(length + shift, length);
return bitset;
}
}
/**
*
* @param bitset
* @param shift
* @param fromIndex
* @param toIndex
* @return
*/
default BitSet shift(BitSet bitset, int shift, int fromIndex, int toIndex)
{
}
}