implement CubeIterator (#309)

Co-authored-by: Ruling-0 <[email protected]>
Co-authored-by: Martin Robertz <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-03-30 00:03:20 +00:00
1 parent fbaa12a703
commit c802075ebd
2 files changed
+545

No files matched your search

@@ -0,0 +1,400 @@
package com.gtnewhorizon.gtnhlib.geometry;
import com.gtnewhorizon.gtnhlib.blockpos.BlockPos;
import com.gtnewhorizon.gtnhlib.util.CoordinatePacker;
/**
* An iterator through positions in 3D space, typically starting at relative <0,0,0> and going outward. To use, call
* {@link #next()} and then take the iterator's n, l, and m fields, using them as arbitrary coordinates. Offset
* coordinates can be optionally supplied; n is treated as x, l as y, and m as z in methods that use those. One reason
* n, l, and m are not internally x, y, and z is to highlight that their ordering is arbitrary; swap or invert them if
* you like. I initially named them n, l, and m as they increase similarly to the three electronic quantum numbers (you
* can think of the sign of n or l as the spin). In {@link com.gtnewhorizon.gtnhlib.geometry.CubeIterator}, from which
* this was adapted, aside from the symmetry transformations, m is bounded by l and l is bounded by n, similarly to
* electron orbitals, in order to get unique non-ordered {n,l,m} sets. <br>
* It is recommended to use direct field access, functions like {@link #getX()}, or a function such as
* {@link #nextNLMPacked()} to avoid object spam.
*
* @author __felix__
*/
public abstract class Abstract3DIterator {
/**
* One of the three coordinates ran through by an iterator. Default is X. Can be X, Y, or Z, depending on what
* arbitrary order you would like to prioritize. This should be the **first to be increased** value in implementing
* classes (after symmetry operations). <br>
* Inspired by the principal quantum number; it limits the values of l and m in CubeIterator.
*/
public int n;
/**
* One of the three coordinates ran through by an iterator. Default is Y. Can be X, Y, or Z, depending on what
* arbitrary order you would like to prioritize. This should be the **second to be increased** value in implementing
* classes (after symmetry operations). <br>
* Inspired by the azimuthal quantum number; it limits the value of m and can only go up to n in CubeIterator.
*/
public int l;
/**
* One of the three coordinates ran through by an iterator. Default is Z. Can be X, Y, or Z, depending on what
* arbitrary order you would like to prioritize. This should be the **last to be increased** value in implementing
* classes (after symmetry operations). <br>
* Inspired by the magnetic quantum number; it can only go up to l in CubeIterator.
*/
public int m;
/**
* The maximum distance or radius that the iterator can go through, used in {@link #hasNext()}. <br>
* It is encouraged for a negative range to mean the iterator is unbounded (so {@link #hasNext()} and the like would
* always return `true`), but it is not required since they can just be ignored.
*/
public int range;
/**
* Whether the iterator is not at or past the last value before it exceeds {@link #range}. The iterator should still
* faithfully go past the maximum value so that unbounded iterators can be made by setting a dummy range and
* ignoring this. It is also encouraged to have a check where, if the range is negative, return true. <br>
* If you are retreiving coordinates without progressing the iterator BEFORE calling next() (i.e. you want to
* include &lt;0,0,0&gt;, use {@link #hasNextFrom0()}, as it will allow the iterator to progress one more loop and
* not miss the last position.
*
* @return Whether the iterator is at or past the last value before it exceeds the provided range
*/
public abstract boolean hasNext();
/**
* Whether the iterator is not at or past the value where it exceeds {@link #range}. This is used for when you want
* to include <0,0,0> and not exclude the last point (which means you are calling {@link #next()} AFTER getting the
* coords). It's encouraged to return true for range &lt; 0. <br>
* Ex: `while(iter.hasNextFrom0()) { something(iter.n, iter.l, iter.m); next(); }` <br>
*
* @return Whether the iterator exceeds the provided range
*/
public abstract boolean hasNextFrom0();
// public abstract boolean hasPrevious();
/**
* The (optional) starting X coordinate, used with {@link #nextCoordTriple()} and {@link #nextBlockPos()}.
*/
public int startX;
/**
* The (optional) starting Y coordinate, used with {@link #nextCoordTriple()} and {@link #nextBlockPos()}.
*/
public int startY;
/**
* The (optional) starting Z coordinate, used with {@link #nextCoordTriple()} and {@link #nextBlockPos()}.
*/
public int startZ;
/**
* An {@link #Ordering} that applies to the order that values are increased. The default ordering is that X is the
* first value to be increased, and positive goes before negative. This changes the output of functions such as
* {@link #getX()} (anything that says "XYZ") If you want to ignore this, just use `iter.n`, `iter.l`, `iter.m` for
* whichever X, Y, Z coord you want. <br>
* Remember to use bitwise | to combine the directional and negational enums.
*/
public short order = Ordering.xyz;
/**
* An "enum" for the arbitrary ordering of X, Y, and Z coords. Use the bitwise | operator to combine the ordering
* and negation enums. Ordering of coordinates affects priority (for example, with range = 1 and Ordering.xyz it
* goes +x, -x, +y, -y, +z, -z, +x+y, +x-y, -x+y, etc.) This does not define a way to change the order in which the
* mirroring operation happens (so for example you can't do +x, -y, -x, +y), as that could mess up subclasses. (So
* you can do North-South-Down-Up-West-East, but not North-Up-South-East-Down-West). <br>
* The format is three nibbles stuck together, the first bit of each being the sign (1 for neg first, 0 for pos
* first) and the second three bits being which of n, l, m it uses (100 = n, 010 = l, 001 = m). This assumes that
* implementing classes have decreasing priority for n, l, and m.
*/
public static final class Ordering {
/**
* N, L, M, minus: ID's for the internals and sign; use bitwise & to test after shift <br>
* X, Y, Z: Offsets for the ordering data; right shift by them before testing
*/
public static final class Position {
public static final byte N = 0b100;
public static final byte L = 0b010;
public static final byte M = 0b001;
public static final byte minus = 8;
public static final byte X = 8;
public static final byte Y = 4;
public static final byte Z = 0;
}
public static final short xyz = 0b0100_0010_0001;
public static final short xzy = 0b0100_0001_0010;
public static final short yxz = 0b0010_0100_0001;
public static final short zxy = 0b0010_0001_0100;
public static final short yzx = 0b0001_0100_0010;
public static final short zyx = 0b0001_0010_0100;
public static final short NEG_X = 0b1000_0000_0000;
public static final short NEG_Y = 0b0000_1000_0000;
public static final short NEG_Z = 0b0000_0000_1000;
public static final short NEG_XY = 0b1000_1000_0000;
public static final short NEG_XZ = 0b1000_0000_1000;
public static final short NEG_YZ = 0b0000_1000_1000;
public static final short NEG_XYZ = 0b1000_1000_1000;
}
/**
* Initializes the iterator with only the max range. If you don't want a max range, put whatever here and ignore
* {@link #hasNext()}. Ordering defaults to X -> Y -> Z (pos -> neg for all). Some implementations may have a
* negative range mean unbounded.
*
* @param range the maximum radius the iterator goes up to, used in {@link #hasNext()}
*/
public Abstract3DIterator(int range) {
this.range = range;
}
/**
* Same as {@link #Abstract3DIterator(int)} except this sets {@link #order}.
*
* @param range the maximum radius the iterator goes up to, used in {@link #hasNext()}
* @param order an {@link #Ordering}; use bitwise | with a NEG_ enum to flip signs
*/
public Abstract3DIterator(int range, short order) {
this.range = range;
this.order = order;
}
/**
* Initializes the iterator with the max range and starting XYZ values. If you don't want a max range, put whatever
* here and ignore {@link #hasNext()}. The XYZ values are not factored into {@link #n}, {@link #l}, or {@link #m};
* those are only starting offsets. They are used in {@link #nextCoordTriple()} and {@link #nextBlockPos()}.
* Ordering defaults to X -> Y -> Z (pos -> neg for all).
*
* @param range the maximum radius the iterator goes up to, used in {@link #hasNext()}
* @param x the x offset used in {@link #nextCoordTriple()} and {@link #nextBlockPos()}
* @param y the y offset used in {@link #nextCoordTriple()} and {@link #nextBlockPos()}
* @param z the z offset used in {@link #nextCoordTriple()} and {@link #nextBlockPos()}
*/
public Abstract3DIterator(int range, int x, int y, int z) {
this.range = range;
startX = x;
startY = y;
startZ = z;
}
/**
* Same as {@link #Abstract3DIterator(int, int, int, int)} except this sets {@link #order}.
*
* @param range the maximum radius the iterator goes up to, used in {@link #hasNext()}
* @param order an {@link #Ordering}; use bitwise | with the NEG_ enums to flip signs
* @param x the x offset used in {@link #nextCoordTriple()} and {@link #nextBlockPos()}
* @param y the y offset used in {@link #nextCoordTriple()} and {@link #nextBlockPos()}
* @param z the z offset used in {@link #nextCoordTriple()} and {@link #nextBlockPos()}
*/
public Abstract3DIterator(int range, short order, int x, int y, int z) {
this.range = range;
this.order = order;
startX = x;
startY = y;
startZ = z;
}
/**
* Progresses the iterator ({@link n}, {@link l}, {@link m} values), but does not directly return them. This is
* intended so that you don't have to spin up an entire object if you don't want to
*/
public abstract void next();
/**
* Returns the next coords as an array, offset by the optional start values. The start values don't have to be set
* for this to be used (they default to 0). Using the fields directly or their getters is better than spinning up an
* array in most cases. Also note that this method progresses the iterator.
*
* @return an array containing the next absolute xyz coordinates
*/
public final int[] nextCoordTriple() {
next();
return new int[] { getX(), getY(), getZ() };
}
/**
* Returns the next coords as an array, not offset by the optional start values if they were set. Identical to
* {@link #nextCoordTriple()} if offsets weren't set. Using the n, l, m fields directly or their getters is better
* than spinning up an array in most cases. Also note that this method progresses the iterator.
*
* @return an array containing the next relative xyz coordinates
*/
public final int[] nextRelativeCoordTriple() {
next();
return new int[] { getRelativeX(), getRelativeY(), getRelativeZ() };
}
/**
* Returns the next coords as a {@link com.gtnewhorizon.gtnhlib.blockpos.BlockPos}, offset by the optional start
* values. The start values don't have to be set for this to be used (they default to 0). Using the fields directly
* or the XYZ getters is usually better than this. Also note that this method progresses the iterator.
*
* @return a BlockPos of the next absolute xyz coordinates
*/
public final BlockPos nextBlockPos() {
next();
return new BlockPos(getX(), getY(), getZ());
}
/**
* Returns the next coords as a {@link com.gtnewhorizon.gtnhlib.blockpos.BlockPos}, not offset by the optional start
* values. Only different if the startX, etc. fields were set. Using the n, l, m fields directly or the XYZ getters
* is usually better than this. Also note that this method progresses the iterator.
*
* @return a BlockPos of the next relative xyz coordinates, not offset even if offsets were provided in init
*/
public final BlockPos nextRelativeBlockPos() {
next();
return new BlockPos(getRelativeX(), getRelativeY(), getRelativeZ());
}
/**
* Returns the relative X coordinate according to the XYZ {@link #order}.
*
* @return the relative X coordinate according to the arbitrary ordering
*/
public final int getRelativeX() {
byte tmp = (byte) (order >> Ordering.Position.X);
int ret = (tmp & Ordering.Position.N) != 0 ? n : (tmp & Ordering.Position.L) != 0 ? l : m;
if ((tmp & Ordering.Position.minus) != 0) ret = -ret;
return ret;
}
/**
* Returns the relative Y coordinate according to the XYZ {@link #order}.
*
* @return the relative Y coordinate according to the arbitrary ordering
*/
public final int getRelativeY() {
byte tmp = (byte) (order >> Ordering.Position.Y);
int ret = (tmp & Ordering.Position.L) != 0 ? l : (tmp & Ordering.Position.N) != 0 ? n : m;
if ((tmp & Ordering.Position.minus) != 0) ret = -ret;
return ret;
}
/**
* Returns the relative Z coordinate according to the XYZ {@link #order}.
*
* @return the relative Z coordinate according to the arbitrary ordering
*/
public final int getRelativeZ() {
// If for whatever reason Ordering.Z changes to not be 0, update this
byte tmp = (byte) order;
int ret = (tmp & Ordering.Position.M) != 0 ? m : (tmp & Ordering.Position.L) != 0 ? l : n;
if ((tmp & Ordering.Position.minus) != 0) ret = -ret;
return ret;
}
/**
* Returns the absolute X coordinate according to the XYZ {@link #order}. Only different if {@link #startX} was set.
*
* @return the absolute X coordinate according to the arbitrary ordering
*/
public final int getX() {
return startX + getRelativeX();
}
/**
* Returns the absolute Y coordinate according to the XYZ {@link #order}. Only different if {@link #startY} was set.
*
* @return the absolute Y coordinate according to the arbitrary ordering
*/
public final int getY() {
return startY + getRelativeY();
}
/**
* Returns the absolute Z coordinate according to the XYZ {@link #order}. Only different if {@link #startZ} was set.
*
* @return the absolute Z coordinate according to the arbitrary ordering
*/
public final int getZ() {
return startZ + getRelativeZ();
}
/**
* Outputs the current iterator relative position as a string, for debugging.
*
* @return &quot;&lt;n,l,m&gt;&quot; with the ordering applied
*/
public String getCurrentRelativePos() {
return String.format("<%d,%d,%d>", getRelativeX(), getRelativeY(), getRelativeZ());
}
/**
* Outputs the current iterator absolute position as a string, for debugging.
*
* @return &quot;&lt;startX+n,startY+l,startZ+m&gt;&quot; with the ordering applied
*/
public String getCurrentAbsolutePos() {
return String.format("<%d,%d,%d>", getX(), getY(), getZ());
}
/**
* Outputs the current iterator internal values as a string, for debugging.
*
* @return &quot;&lt;n,l,m&gt;&quot; WITHOUT the ordering applied
*/
public String getCurrentNLM() {
return String.format("<%d,%d,%d>", n, l, m);
}
/**
* @hidden
*/
@Override
public String toString() {
return getClass().getName() + "@" + getCurrentRelativePos();
}
/**
* Progresses the iterator and gives the coordinates of n, l, and m, together, packed with
* {@link com.gtnewhorizon.gtnhlib.util.CoordinatePacker}
*
* @see com.gtnewhorizon.gtnhlib.util.CoordinatePacker#unpackX
* @see com.gtnewhorizon.gtnhlib.util.CoordinatePacker#unpackY
* @see com.gtnewhorizon.gtnhlib.util.CoordinatePacker#unpackZ
*
* @return n, l, and m, packed together into a long
*/
public final long nextNLMPacked() {
next();
return CoordinatePacker.pack(n, l, m);
}
/**
* Progresses the iterator and gives the coordinates of x, y, and z, together, packed with
* {@link com.gtnewhorizon.gtnhlib.util.CoordinatePacker}
*
* @see com.gtnewhorizon.gtnhlib.util.CoordinatePacker#unpackX
* @see com.gtnewhorizon.gtnhlib.util.CoordinatePacker#unpackY
* @see com.gtnewhorizon.gtnhlib.util.CoordinatePacker#unpackZ
*
* @return absolute x, y, and z, packed together into a long
*/
public final long nextXYZPacked() {
next();
return CoordinatePacker.pack(getX(), getY(), getZ());
}
/**
* Progresses the iterator and gives the relative coordinates of x, y, and z, together, packed with
* {@link com.gtnewhorizon.gtnhlib.util.CoordinatePacker}
*
* @see com.gtnewhorizon.gtnhlib.util.CoordinatePacker#unpackX
* @see com.gtnewhorizon.gtnhlib.util.CoordinatePacker#unpackY
* @see com.gtnewhorizon.gtnhlib.util.CoordinatePacker#unpackZ
*
* @return relative x, y, and z, packed together into a long
*/
public final long nextRelativeXYZPacked() {
next();
return CoordinatePacker.pack(getRelativeX(), getRelativeY(), getRelativeZ());
}
}
@@ -0,0 +1,145 @@
package com.gtnewhorizon.gtnhlib.geometry;
/**
* Abstract3DIterator that works outwards under the L infinity metric, forming a cube. (distance = max(x, y, z)) <br>
* Uses the manhattan metric secondarily to order triples in the same shell. (distance = x+y+z) <br>
* Use {@link com.gtnewhorizon.gtnhlib.geometry.Abstract3DIterator#getCurrentRelativePos} to debug.
*
* @author __felix__
*/
public class CubeIterator extends Abstract3DIterator {
/**
* Initializes without setting the offsets used by certain methods. <br>
* Set the range to a negative number for {@link #hasNext()} and the like to always return true.
*
* @see com.gtnewhorizon.gtnhlib.geometry.Abstract3DIterator#Abstract3DIterator(int)
*/
public CubeIterator(int range) {
super(range);
}
/**
* Initializes without setting the offsets used by certain methods and adds an ordering. <br>
* Set the range to a negative number for {@link #hasNext()} and the like to always return true.
*
* @see com.gtnewhorizon.gtnhlib.geometry.Abstract3DIterator#Abstract3DIterator(int, short)
*/
public CubeIterator(int range, short order) {
super(range, order);
}
/**
* Initializes with setting the offsets used by certain methods. <br>
* Set the range to a negative number for {@link #hasNext()} and the like to always return true.
*
* @see com.gtnewhorizon.gtnhlib.geometry.Abstract3DIterator#Abstract3DIterator(int,int,int,int)
*/
public CubeIterator(int range, int x, int y, int z) {
super(range, x, y, z);
}
/**
* Initializes with setting the offsets used by certain methods and adds an ordering. <br>
* Set the range to a negative number for {@link #hasNext()} and the like to always return true.
*
* @see com.gtnewhorizon.gtnhlib.geometry.Abstract3DIterator#Abstract3DIterator(int,short,int,int,int)
*/
public CubeIterator(int range, short order, int x, int y, int z) {
super(range, order, x, y, z);
}
/**
* Returns whether the iterator is at or past the end position. Use this if you use the values after or with
* {@link #next()} in your loop.
*
* @return whether the iterator is at or past the end position: n, l, m >= -range
*/
public boolean hasNext() {
return (range < 0 || -n < range || -l < range || -m < range) && hasNextFrom0();
}
/**
* Returns whether the iterator is past the end position, if you call {@link #next()} BEFORE using the coords in
* your loop. Basically, the iterator starts at &lt;0,0,0&gt;, so if you want to use that position and not have it
* end one iteration before the final corner, call this instead as the check in the loop.
*
* @return whether the iterator is past the end position
*/
public boolean hasNextFrom0() {
return range < 0 || (n >= -range && n <= range && l >= -range && l <= range && m >= -range && m <= range);
}
/**
* Returns whether the iterator is at its start position.
*
* @return whether n, l, and m are all 0 (meaning the center block)
*/
public boolean hasPrevious() {
return n != 0 || l != 0 || m != 0;
}
/**
* Progresses the CubeIterator forward by one step (the changes are in the n, l, and m fields). First inverts the
* coords in a binary-counter fashion, then once they are all negative swaps them around until they are back in
* descending order, then bumps m up unless it is equal to l, otherwise bumps l up and resets m, or if all three are
* equal bumps n and resets the other two. Uses the L infinity norm/metric, forming a cube.
*/
@Override
public void next() {
int g;
m = -m;
if (m < 0) return;
l = -l;
if (l < 0) return;
n = -n;
if (n < 0) return;
if (l >= n || m > n) {
if (m >= l) {
if (n <= l) {
if (m > n) {
g = m;
m = n;
n = g;
if (l > m) {
++m;
return;
}
m = 0;
++l;
return;
}
l = 0;
m = 0;
++n;
return;
}
g = l;
l = n;
n = g;
return;
}
if (n < m) {
g = m;
m = n;
n = g;
return;
}
g = l;
l = m;
m = g;
return;
}
if (l > m) {
g = l;
l = m;
m = g;
return;
}
g = l;
l = n;
n = g;
return;
}
}