/** * Returns a new packed short[] containing the Hilbert distances in hilbert as "on" cells, and all other cells "off" * @param hilbert a vararg or array of Hilbert distances that will be encoded as "on" * @return the points given to this encoded as "on" in a packed short array */ public static short[] packSeveral(int... hilbert) { if(hilbert == null || hilbert.length == 0) return ALL_WALL; Arrays.sort(hilbert); ShortVLA vla = new ShortVLA(128); int current, past = hilbert[0], skip = 0; vla.add((short)hilbert[0]); for (int i = 1; i < hilbert.length; i++) { current = hilbert[i]; if (current - past > 1) { vla.add((short) (skip+1)); skip = 0; vla.add((short)(current - past - 1)); } else if(current != past) skip++; past = current; } vla.add((short)(skip+1)); return vla.toArray(); }
/** Returns a new array containing the remaining keys. */ public ShortVLA toArray () { ShortVLA array = new ShortVLA(true, set.size); while (hasNext) array.add(next()); return array; } }
x = clamp(hilbertX[i] + xMove, 0, width); y = clamp(hilbertY[i] + yMove, 0, height); vla.add(hilbertDistances[x + (y << 8)]); int current, past = indices[0], skip = 0; vla.add((short)indices[0]); for (int i = 1; i < indices.length; i++) { current = indices[i]; if (current - past > 1) vla.add((short) (skip+1)); skip = 0; vla.add((short)(current - past - 1)); past = current; vla.add((short)(skip+1));
private static void modifiedShadowFOV(int expansion, int viewerX, int viewerY, Radius metric, ShortSet bounds, ShortSet storedSet, ShortVLA vla) { if(expansion < 1) return; short start = hilbertDistances[viewerX + (viewerY << 8)]; if(storedSet.add(start)) vla.add(start); for (Direction d : Direction.DIAGONALS) { modifiedShadowCast(expansion, 1, 1.0, 0.0, 0, d.deltaX, d.deltaY, 0, viewerX, viewerY, metric, bounds, storedSet, vla); modifiedShadowCast(expansion, 1, 1.0, 0.0, d.deltaX, 0, 0, d.deltaY, viewerX, viewerY, metric, bounds, storedSet, vla); } }
/** * Given the zones resulting from this class' calculate method and a Coord to check, finds the indices of all * influencing groups in zones that have the Coord in their area, and returns all such indices as an int array. * @param zones a short[][] returned by calculate; not a multi-packed short[][] from CoordPacker ! * @param point the Coord to test * @return an int[] where each element is the index of an influencing group in zones */ public int[] nearestInfluences(short[][] zones, Coord point) { ShortVLA found = new ShortVLA(4); for (short i = 0; i < zones.length; i++) { if(CoordPacker.queryPacked(zones[i], point.x, point.y)) found.add(i); } return found.asInts(); }
/** * Given the zones resulting from this class' calculate method and a Coord to check, finds the indices of all * influencing groups in zones that have the Coord in their area, and returns all such indices as an int array. * @param zones a short[][] returned by calculate; not a multi-packed short[][] from CoordPacker ! * @param point the Coord to test * @return an int[] where each element is the index of an influencing group in zones */ public int[] nearestInfluences(short[][] zones, Coord point) { ShortVLA found = new ShortVLA(4); for (short i = 0; i < zones.length; i++) { if(CoordPacker.queryPacked(zones[i], point.x, point.y)) found.add(i); } return found.asInts(); } /**
/** * This can be given a Coord to check in the results of the latest calculate() call. Finds the indices of all * influencing groups in zones that have the Coord in their area, and returns all such indices as an int array. * @param point the Coord to test * @return an int[] where each element is the index of an influencing group in zones */ public int[] nearestInfluences(Coord point) { if(!completed) return new int[0]; ShortVLA found = new ShortVLA(4); for (short i = 0; i < packedGroups.length; i++) { if(CoordPacker.queryPacked(packedGroups[i], point.x, point.y)) found.add(i); } return found.asInts(); }
vla.add((short) i);
/** * Gets the positions that are "on" in the given packed array, without unpacking it, repeatedly goes through a * number of "on" cells equal to fraction and stores one of those cells as a Coord, and returns the accumulated * portion of positions as an array of Hilbert Curve indices. * <br> * For purposes of finding mostly cells with a similar distance to each other but without obvious patterns, a value * of 5, 6, or 7 for fraction works well. * @param packed a short[] returned by pack() or one of the sub-arrays in what is returned by packMulti(); must * not be null (this method does not check due to very tight performance constraints). * @param fraction the approximate fraction of "on" cells to use * @return a Hilbert Curve index array corresponding to a fraction of the "on" cells in packed. */ public static short[] fractionPackedHilbert(short[] packed, int fraction) { if(fraction <= 1) return allPackedHilbert(packed); ShortVLA vla = new ShortVLA(64); boolean on = false; int idx = 0, ctr = 0; for(int p = 0; p < packed.length; p++, on = !on) { if (on) { for (int i = idx; i < idx + (packed[p] & 0xffff); i++, ctr = (ctr + 1) % fraction) { if(ctr == 0) vla.add((short)i); } } idx += packed[p] & 0xffff; } return vla.toArray(); }
vla.add((short) i);
vla.add((short) i);
vla.add((short) i);
for (int i = idx; i < idx + (packed[p] & 0xffff); i++, ctr = (ctr + 1) % fraction) { if(ctr == 0) vla.add((short)i);
for (int i = idx; i < idx + (packed[p] & 0xffff); i++) { if(rng.nextDouble() < fraction) vla.add((short)i);
vla.add((short)i);