more BitRegion math added

This commit is contained in:
BuildTools 2017-01-23 22:18:03 -05:00
parent 68b965590b
commit a21af80204
8 changed files with 389 additions and 444 deletions

View file

@ -2,64 +2,65 @@ package regions;
import java.util.BitSet;
public class BitRegion implements UtilBitSet
public class BitRegion
{
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
CONSTRUCTORS
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/*
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; }
protected int[] sides; public int[] getSides() { return sides; }
public final boolean is3D;
protected final BitSet blocks;
/**
*
*
* @param minX
* @param minZ
*/
public BitRegion(int minX, int minZ)
protected BitRegion(int xSideLength, int zSideLength)
{
this.min = new int[] { minX, minZ };
this.is3D = false;
this.blocks = new BitSet();
sides = new int[] { xSideLength, zSideLength };
is3D = false;
blocks = new BitSet();
}
/**
*
*
* @param minX
* @param minZ
* @param minY
*/
public BitRegion(int minX, int minZ, int minY)
protected BitRegion(int xSideLength, int zSideLength, int ySideLength)
{
this.min = new int[] { minX, minZ, minY };
this.is3D = true;
this.blocks = new BitSet();
sides = new int[] { xSideLength, zSideLength, ySideLength };
is3D = true;
blocks = new BitSet();
}
public static BitRegion new2DBitRegion()
{
return new BitRegion(0, 0);
}
public static BitRegion new3DBitRegion()
{
return new BitRegion(0, 0, 0);
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
GET VALUE
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/*
GET VALUE
*/
/**
*
*
* @return
* Get number of blocks contained in the region
*/
public int getVolume()
{
@ -68,52 +69,170 @@ public class BitRegion implements UtilBitSet
/**
*
*
* @return
* Get number of blocks contained in the region's bounding box
*/
public int getBoundsVolume()
{
if (max == null) return 0;
return is3D ?
(sides[0]) * (sides[1]) * (sides[2]) :
(sides[0]) * (sides[1]);
}
/**
* Get coordinates for the block at the given index in the BitSet
*
* @param index
* @return
*/
protected int[] getCoords(int index)
{
if (++index > getBoundsVolume()) return null;
return is3D ?
(max[0] - min[0]) * (max[1] - min[1]) * (max[2] - min[2]) :
(max[0] - min[0]) * (max[1] - min[1]);
getCoords3D(index) :
getCoords2D(index);
}
/**
*
* @param coordinate
* @return
*/
public int getIndex(int x, int z)
{
return (x - min[0]) + (z - min[1]);
}
/**
*
* @param coordinate
* @param index
* @return
*/
public int getIndex(int x, int z, int y)
protected int[] getCoords3D(int index)
{
return (x - min[0]) + (z - min[1]) + (y - min[2]);
}
if (index == 0) return new int[] {0, 0, 0};
int[] xzy = new int[3];
int crossSec = sides[0] * sides[1];
xzy[2] = index / crossSec - ((index = index % crossSec) == 0 ? 1 : 0); index = (index == 0 ? crossSec : index);
xzy[1] = index / sides[0] - ((index = index % sides[0]) == 0 ? 1 : 0); index = (index == 0 ? sides[0] : index);
xzy[0] = index - 1;
return xzy;
}
/**
*
* @param index
* @return
*/
public int getCoords(int index)
protected int[] getCoords2D(int index)
{
return is3D ?
:
;
if (index == 0) return new int[] {0, 0};
int[] xz = new int[2];
xz[1] = index / sides[0] - ((index = index % sides[0]) == 0 ? 1 : 0); index = (index == 0 ? sides[0] : index);
xz[0] = index - 1;
return xz;
}
/*-------------------------------------
OVERLOADS : getIndex()
-------------------------------------*/
/**
*
* @param coordinate
* @return
*/
public int getIndex(int x, int z)//TODO return -1 if outside bounds
{
return (z * sides[0]) + x;
}
/**
*
* @param coordinate
* @return
*/
public int getIndex(int x, int z, int y)//TODO return -1 if outside bounds
{
return (y * sides[0] * sides[1]) + (z * sides[0]) + x;
}
/*-------------------------------------
OVERLOADS : blockAt()
-------------------------------------*/
/**
*
* @param x
* @param z
* @return
*/
public boolean blockAt(int x, int z)//TODO return false if outside bounds
{
return blocks.get(getIndex(x,z));
}
/**
*
* @param x
* @param z
* @param y
* @return
*/
public boolean blockAt(int x, int z, int y)//TODO return false if outside bounds
{
return blocks.get(getIndex(x, z, y));
}
/**
*
* @param index
* @return
*/
public boolean blockAt(int index)
{
return blocks.get(index);
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
OVERLOADS : shift()
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/**
*
* @param shift
* @param fromIndex
*/
public void bitshift(int shift, int fromIndex)
{
if (shift < 1) return;
int toAboveThisIndex = fromIndex + shift - 1;
for (int i = blocks.size() + shift; i > toAboveThisIndex; i++)
{
blocks.set(i - shift, blocks.get(i));
}
blocks.clear(fromIndex, toAboveThisIndex);
}
public void shift(int xShift, int zShift)
{
}
public void shift(int xShift, int zShift, int yShift)
{
}
@ -124,6 +243,7 @@ public class BitRegion implements UtilBitSet
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/**
*
*/
@ -132,6 +252,30 @@ public class BitRegion implements UtilBitSet
blocks.set(0, getBoundsVolume() - 1);
}
/**
*
* @param x
* @param z
*/
public void fill(int x, int z)
{
blocks.set(getIndex(x, z));
}
/**
*
* @param x
* @param z
* @param y
*/
public void fill(int x, int z, int y)
{
blocks.set(getIndex(x, z, y));
}
/**
*
*
@ -157,6 +301,7 @@ public class BitRegion implements UtilBitSet
}
}
/**
*
*
@ -186,6 +331,7 @@ public class BitRegion implements UtilBitSet
}
}
/**
*
*
@ -213,6 +359,7 @@ public class BitRegion implements UtilBitSet
}
}
/**
*
*
@ -247,11 +394,14 @@ public class BitRegion implements UtilBitSet
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
OVERLOADS : clear()
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/**
*
*/
@ -260,6 +410,30 @@ public class BitRegion implements UtilBitSet
blocks.clear();
}
/**
*
* @param x
* @param z
*/
public void clear(int x, int z)
{
blocks.clear(getIndex(x, z));
}
/**
*
* @param x
* @param z
* @param y
*/
public void clear(int x, int z, int y)
{
blocks.clear(getIndex(x, z, y));
}
/**
*
*
@ -285,6 +459,7 @@ public class BitRegion implements UtilBitSet
}
}
/**
*
*
@ -314,6 +489,7 @@ public class BitRegion implements UtilBitSet
}
}
/**
*
*
@ -341,6 +517,7 @@ public class BitRegion implements UtilBitSet
}
}
/**
*
*

View file

@ -0,0 +1,7 @@
package regions;
public class CoordRegion
{
// 2896 is the maximum side length for a 3D, square, resolution 1 region with height 256
// 128 is the coarsest resolution for a 3D region
}

View file

@ -7,15 +7,6 @@ import java.io.OutputStream;
public class Octree extends Tree
{
/*
BYTE CONVERSION
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
FROM BYTES
@ -32,6 +23,7 @@ public class Octree extends Tree
b = input.read();
return a == -1 || b == -1 ?
new Node( false ) :
new Node( new Node[] { parseBytes(input, (a >>> 6 & 3)),
parseBytes(input, (a >>> 4 & 3)),
@ -52,9 +44,7 @@ public class Octree extends Tree
----------------------------------------------------------------------------*/
@Override
public void writeBytes(Node node, OutputStream output)
{
try
public void writeBytes(Node node, OutputStream output) throws IOException
{
output.write( getByte( node.children[0],
node.children[1],
@ -67,51 +57,41 @@ public class Octree extends Tree
node.children[6],
node.children[7]
));
}
catch (IOException e) { e.printStackTrace(); }
for (Node child : node.children)
if (child.children.length > 0)
writeBytes(child, output);
}
/*
CONSTRUCTORS
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
CONSTRUCTORS
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
public Octree(int[][] bounds, File file, byte[] bytes)
public Octree(File file) throws IOException
{
super(bounds, file);
super(file);
}
/*-------------------------------------
Methods used by constructors.
-------------------------------------*/
@Override
protected final TreeEditor<Octree> newEditor()
{
return new TreeEditor<Octree>(this, min[0], min[1], min[2]);
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
Methods used by constructors
CALCULATIONS
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
@Override
protected final OctreeEditor newEditor()
{
return new OctreeEditor(this);
}
/*
CALCULATIONS
*/
@Override
public Node[] getNodes(Node parentNode, int[][] regionBounds)
{

View file

@ -7,15 +7,6 @@ import java.io.OutputStream;
public class Quadtree extends Tree
{
/*
BYTE CONVERSION
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
FROM BYTES
@ -31,6 +22,7 @@ public class Quadtree extends Tree
int a = input.read(); //returns -1 if there are no more bytes
return a == -1 ?
new Node( false ) :
new Node( new Node[] { parseBytes(input, (a >>> 6 & 3)),
parseBytes(input, (a >>> 4 & 3)),
@ -46,17 +38,13 @@ public class Quadtree extends Tree
----------------------------------------------------------------------------*/
@Override
public void writeBytes(Node node, OutputStream output)
{
try
public void writeBytes(Node node, OutputStream output) throws IOException
{
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)
if (child.children.length > 0)
@ -64,42 +52,34 @@ public class Quadtree extends Tree
}
/*
CONSTRUCTORS
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
CONSTRUCTORS
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
public Quadtree(File file)
public Quadtree(File file) throws IOException
{
super(file);
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
Methods used by constructors
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
/*-------------------------------------
Methods used by constructors.
-------------------------------------*/
@Override
final QuadtreeEditor newEditor()
protected final TreeEditor<Quadtree> newEditor()
{
return new QuadtreeEditor(this);
return new TreeEditor<Quadtree>(this, min[0], min[1]);
}
/*
CALCULATIONS
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
CALCULATIONS
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
@Override
public Node[] getNodes(Node parentNode, int[][] regionBounds)

View file

@ -1,7 +1,6 @@
package regions;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
@ -58,13 +57,13 @@ public abstract class Tree
/*
BYTE CONVERSION
*/
*/
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
@ -97,11 +96,11 @@ public abstract class Tree
*
* @param input DataInputStream of source bytes
* @return a new Node
* @throws IOException
*/
public Node parseBytes(DataInputStream input)
public Node parseBytes(DataInputStream input) throws IOException
{
try { return parseBytes(input, 0); }
catch (IOException e) { return new Node(false); }
return parseBytes(input, 0);
}
@ -112,29 +111,17 @@ public abstract class Tree
*
* @param file binary file to read bytes from
* @return a new Node
* @throws IOException
*/
public Node parseBytes(File file)
public Node parseBytes(File file) throws IOException
{
if (file.length() == 0) { return new Node(false); }
else { return parseBytes(file, 1); }
}
if (file.length() == 0) return new Node(false);
private Node parseBytes(File file, int attempt)
{
try
{
DataInputStream input = new DataInputStream( new BufferedInputStream( new FileInputStream(file) ));
Node node = parseBytes(input);
input.close();
return node;
}
catch (IOException e)
{
e.printStackTrace();
return attempt < 4 ? parseBytes(file, ++attempt) : new Node(false);
}
}
@ -183,79 +170,17 @@ public abstract class Tree
/**
* Parses the tree rooted at 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>
*
* NOTE: assumes an OutputStream that appends with each write.
* Assumes an OutputStream that appends with each write.
*
* @param node the node to be parsed
* @return a byte array representing the node and all its child nodes
* @throws IOException
*/
public abstract void writeBytes(Node node, OutputStream output);
/*-------------------------------------
OVERLOADS : getBytes()
-------------------------------------*/
/**
* Parses the tree from the root, 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>
*
* @return byte array representing the root node and all its child nodes
*/
public byte[] getBytes()
{
ByteArrayOutputStream output = new ByteArrayOutputStream();
writeBytes(root, output);
return output.toByteArray();
}
/**
* 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 given node and all its child nodes
*/
public byte[] getBytes(Node node)
{
ByteArrayOutputStream output = new ByteArrayOutputStream();
writeBytes(node, output);
return output.toByteArray();
}
/**
* Parses the tree from the root, 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 OutputStream, does not close the stream.
*
* @param output the ByteArrayOutputStream to write to
*/
public void getBytes(OutputStream output)
{
writeBytes(root, output);
}
/**
* 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 OutputStream, does not close the stream.
*
* @param node the node to parse from
* @param output the ByteArrayOutputStream to write to
*/
public void getBytes(Node node, OutputStream output)
{
writeBytes(node, output);
}
public abstract void writeBytes(Node node, OutputStream output) throws IOException;
@ -263,105 +188,93 @@ public abstract class Tree
OVERLOADS : saveToFile()
-------------------------------------*/
/**
* Performs <tt>{@link #writeBytes(Node, OutputStream)}</tt> from the root node, using a
* FileOutputStream of the source file as the OutputStream argument.
* Performs <tt>{@link #writeBytes(Node, OutputStream)}</tt> from the root node,
* using a FileOutputStream of the source file as the OutputStream argument.
*
* @param node the root node of the tree to be parsed
* @param destination the file to save to
* @throws IOException
*/
public void saveToFile()//TODO erase existing file contents before writing
public void saveToFile() throws IOException
{
try
{
FileOutputStream output = new FileOutputStream (file, true);
FileOutputStream output = new FileOutputStream (new File(file.getAbsolutePath()), true);
writeBytes(root, output);
output.close();
}
catch (IOException e) { e.printStackTrace(); }
}
/**
* Performs <tt>{@link #writeBytes(Node, OutputStream)}</tt> from the given node, using a
* FileOutputStream of the source file as the OutputStream argument.
* Performs <tt>{@link #writeBytes(Node, OutputStream)}</tt> from the given node,
* using a FileOutputStream of the source file as the OutputStream argument.
*
* @param node the root node of the tree to be parsed
* @param destination the file to save to
* @throws IOException
*/
public void saveToFile(Node node)//TODO erase existing file contents before writing
public void saveToFile(Node node) throws IOException
{
try
{
FileOutputStream output = new FileOutputStream (file, true);
FileOutputStream output = new FileOutputStream (new File(file.getAbsolutePath()), true);
writeBytes(node, output);
output.close();
}
catch (IOException e) { e.printStackTrace(); }
}
/**
* Performs <tt>{@link #writeBytes(Node, OutputStream)}</tt> from the root node, using a
* FileOutputStream of the given file as the OutputStream argument.
* Performs <tt>{@link #writeBytes(Node, OutputStream)}</tt> from the root node,
* using a FileOutputStream of the given file as the OutputStream argument.
*
* @param node the root node of the tree to be parsed
* @param destination the file to save to
* @throws IOException
*/
public void saveToFile(File destination)//TODO erase existing file contents before writing
public void saveToFile(File destination) throws IOException
{
try
{
FileOutputStream output = new FileOutputStream (destination, true);
FileOutputStream output = new FileOutputStream (new File(destination.getAbsolutePath()), true);
writeBytes(root, output);
output.close();
}
catch (IOException e) { e.printStackTrace(); }
}
/**
* Performs <tt>{@link #writeBytes(Node, OutputStream)}</tt> from the given node, using a
* FileOutputStream of the given file as the OutputStream argument.
* Performs <tt>{@link #writeBytes(Node, OutputStream)}</tt> from the given node,
* using a FileOutputStream of the given file as the OutputStream argument.
*
* @param node the root node of the tree to be parsed
* @param destination the file to save to
* @throws IOException
*/
public void saveToFile(Node node, File destination)//TODO erase existing file contents before writing
public void saveToFile(Node node, File destination) throws IOException
{
try
{
FileOutputStream output = new FileOutputStream (destination, true);
FileOutputStream output = new FileOutputStream (new File(destination.getAbsolutePath()), true);
writeBytes(node, output);
output.close();
}
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; }
protected int[] min; public int[] getMin() { return min; }
protected int[] max; public int[] getMax() { return max; }
protected int[] minTrue; public int[] getMinTrue() { return minTrue; }
protected int[] maxTrue; public int[] getMaxTrue() { return maxTrue; }
public final File file;
public final Node root;
public final TreeEditor<?> editor;
public final TreeEditor<? extends Tree> editor;
/**
* Create a Tree from the given binary file. Invokes <tt>parseBytes()</tt>
* Create a Tree from the given binary file. Invokes {@link #parseBytes(File)}
*
* @param file The source file, and save destination, for this Tree.
* @see {@link #parseBytes(File)}
* @throws IOException
*/
public Tree(File file)
public Tree(File file) throws IOException
{
setBoundsFromFilename(file);
@ -397,21 +310,27 @@ public abstract class Tree
*
* @return a new TreeEditor
*/
protected abstract TreeEditor<? extends TreeEditor.Edit> newEditor();
abstract TreeEditor<? extends Tree> newEditor();
/*
CALCULATIONS
*/
*/
/**
*
* @param parentNode
* @param regionBounds
* @return
*/
public abstract Node[] getNodes(Node parentNode, int[][] regionBounds);
/**
@ -423,13 +342,4 @@ public abstract class Tree
{
return getNodes(root, regionBounds);
}
/**
*
* @param parentNode
* @param regionBounds
* @return
*/
public abstract Node[] getNodes(Node parentNode, int[][] regionBounds);
}

View file

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

View file

@ -1,74 +1,18 @@
package regions;
/**
*
*
* @author Kevin Mathewson
*
*/
public abstract class TreeEditor<T extends TreeEditor.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;
}
}
/**
* 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>
public class TreeEditor<T extends Tree>
{
private final TreeEditor editor;
private volatile Edit superEdit;
EditQueue(TreeEditor editor, Edit superEdit)
public TreeEditor(Octree tree, int minX, int minZ, int minY)
{
this.editor = editor;
this.superEdit = superEdit;
}
public synchronized T merge(T edit1, T edit2)
public TreeEditor(Quadtree tree, int minX, int minZ)
{
return null;
}
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
CONSTRUCTOR
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
private final EditQueue changeQueue;
public TreeEditor(EditQueue changeQueue)
{
this.changeQueue = changeQueue;
}
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
METHODS
------------------------------------------------------------------------------
----------------------------------------------------------------------------*/
abstract void addEdit(Edit edit);
}

View file

@ -1,51 +0,0 @@
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)
{
}
}