Use a primitive long set for MantleWriter hollowing and mask lookups

Hollowing allocated six IrisPosition probes per voxel and mask expansion one per candidate voxel. Both now look membership up in an origin-relative long key set, falling back to position equality for sets too wide to pack.

Upstream: 79af11c43
This commit is contained in:
piexel committed 2026-11-10 01:18:10 +01:00
1 parent 8748d85e37
commit c77a9fb012
2 files changed
+257 -18

No files matched your search

@@ -25,6 +25,7 @@ import art.arcane.iris.spi.IrisLogging;
import art.arcane.iris.util.project.matter.TileWrapper;
import com.google.common.collect.ImmutableList;
import com.volmit.generation.GenerationFailures;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import art.arcane.iris.core.IrisSettings;
import art.arcane.iris.core.link.Identifier;
import art.arcane.iris.core.tools.WorldMaintenance;
@@ -131,7 +132,7 @@ public class MantleWriter implements IObjectPlacer, AutoCloseable {
return Math.max(1, availableProcessors / 2);
}
private static Set<IrisPosition> getBallooned(Set<IrisPosition> vset, double radius) {
static Set<IrisPosition> getBallooned(Set<IrisPosition> vset, double radius) {
Set<IrisPosition> returnset = new HashSet<>();
int ceilrad = (int) Math.ceil(radius);
double r2 = Math.pow(radius, 2);
@@ -154,30 +155,83 @@ public class MantleWriter implements IObjectPlacer, AutoCloseable {
return returnset;
}
private static Set<IrisPosition> getHollowed(Set<IrisPosition> vset) {
static Set<IrisPosition> getHollowed(Set<IrisPosition> vset) {
PositionLookup lookup = PositionLookup.of(vset);
Set<IrisPosition> returnset = new KSet<>();
for (IrisPosition v : vset) {
double x = v.getX();
double y = v.getY();
double z = v.getZ();
if (!(vset.contains(new IrisPosition(x + 1, y, z))
&& vset.contains(new IrisPosition(x - 1, y, z))
&& vset.contains(new IrisPosition(x, y + 1, z))
&& vset.contains(new IrisPosition(x, y - 1, z))
&& vset.contains(new IrisPosition(x, y, z + 1))
&& vset.contains(new IrisPosition(x, y, z - 1)))) {
int x = v.getX();
int y = v.getY();
int z = v.getZ();
if (!(lookup.contains(x + 1, y, z)
&& lookup.contains(x - 1, y, z)
&& lookup.contains(x, y + 1, z)
&& lookup.contains(x, y - 1, z)
&& lookup.contains(x, y, z + 1)
&& lookup.contains(x, y, z - 1))) {
returnset.add(v);
}
}
return returnset;
}
private static double hypot(double... pars) {
double sum = 0;
for (double d : pars) {
sum += Math.pow(d, 2);
private static double hypot(double x, double y, double z) {
return Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2);
}
record PositionLookup(LongOpenHashSet keys, Set<IrisPosition> positions, int originX, int originY, int originZ) {
private static final int AXIS_BITS = 21;
private static final long AXIS_LIMIT = 1L << AXIS_BITS;
static PositionLookup of(Set<IrisPosition> positions) {
if (positions.isEmpty()) {
return new PositionLookup(new LongOpenHashSet(), positions, 0, 0, 0);
}
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
int minZ = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int maxY = Integer.MIN_VALUE;
int maxZ = Integer.MIN_VALUE;
for (IrisPosition position : positions) {
minX = Math.min(minX, position.getX());
minY = Math.min(minY, position.getY());
minZ = Math.min(minZ, position.getZ());
maxX = Math.max(maxX, position.getX());
maxY = Math.max(maxY, position.getY());
maxZ = Math.max(maxZ, position.getZ());
}
if ((long) maxX - minX >= AXIS_LIMIT - 2L
|| (long) maxY - minY >= AXIS_LIMIT - 2L
|| (long) maxZ - minZ >= AXIS_LIMIT - 2L) {
return new PositionLookup(null, positions, 0, 0, 0);
}
LongOpenHashSet keys = new LongOpenHashSet(positions.size());
for (IrisPosition position : positions) {
keys.add(key(position.getX() - minX, position.getY() - minY, position.getZ() - minZ));
}
return new PositionLookup(keys, positions, minX, minY, minZ);
}
boolean contains(int x, int y, int z) {
if (keys == null) {
return positions.contains(new IrisPosition(x, y, z));
}
long dx = (long) x - originX;
long dy = (long) y - originY;
long dz = (long) z - originZ;
if ((dx | dy | dz) < 0L || dx >= AXIS_LIMIT || dy >= AXIS_LIMIT || dz >= AXIS_LIMIT) {
return false;
}
return keys.contains(key(dx, dy, dz));
}
private static long key(long x, long y, long z) {
return (x << (AXIS_BITS * 2)) | (y << AXIS_BITS) | z;
}
return sum;
}
private static double lengthSq(double x, double y, double z) {
@@ -796,10 +850,11 @@ public class MantleWriter implements IObjectPlacer, AutoCloseable {
setConsumer(vset, data);
}
private static Set<IrisPosition> getMasked(Set<IrisPosition> vectors, Set<IrisPosition> masks, double radius) {
static Set<IrisPosition> getMasked(Set<IrisPosition> vectors, Set<IrisPosition> masks, double radius) {
Set<IrisPosition> vset = new KSet<>();
int ceil = (int) Math.ceil(radius);
double r2 = Math.pow(radius, 2);
PositionLookup maskLookup = PositionLookup.of(masks);
for (IrisPosition v : vectors) {
int tipX = v.getX();
@@ -809,7 +864,7 @@ public class MantleWriter implements IObjectPlacer, AutoCloseable {
for (int x = -ceil; x <= ceil; x++) {
for (int y = -ceil; y <= ceil; y++) {
for (int z = -ceil; z <= ceil; z++) {
if (hypot(x, y, z) > r2 || !masks.contains(new IrisPosition(x, y, z)))
if (hypot(x, y, z) > r2 || !maskLookup.contains(x, y, z))
continue;
vset.add(new IrisPosition(tipX + x, tipY + y, tipZ + z));
}
@@ -0,0 +1,184 @@
package art.arcane.iris.engine.mantle;
import art.arcane.iris.engine.object.value.IrisPosition;
import org.junit.Test;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class MantleWriterNeighborhoodTest {
@Test
public void balloonedVoxelsMatchTheReferenceSphereForFractionalRadii() {
for (double radius : new double[]{0.0D, 0.5D, 1.0D, 1.5D, 2.0D, 3.7D, 5.0D}) {
Set<IrisPosition> seeds = line(-3, 60, 7, 4, 63, 9);
assertEquals("radius " + radius, referenceBallooned(seeds, radius), MantleWriter.getBallooned(seeds, radius));
}
}
@Test
public void hollowedVoxelsMatchTheReferenceSurfaceExtraction() {
Set<IrisPosition> ballooned = MantleWriter.getBallooned(line(0, 40, 0, 6, 44, 3), 4.0D);
assertEquals(referenceHollowed(ballooned), MantleWriter.getHollowed(ballooned));
}
@Test
public void hollowingASolidBlockKeepsOnlyItsShell() {
Set<IrisPosition> solid = new LinkedHashSet<>();
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 5; y++) {
for (int z = 0; z < 5; z++) {
solid.add(new IrisPosition(x, y, z));
}
}
}
Set<IrisPosition> hollow = MantleWriter.getHollowed(solid);
assertEquals(98, hollow.size());
assertFalse(hollow.contains(new IrisPosition(2, 2, 2)));
assertTrue(hollow.contains(new IrisPosition(0, 2, 2)));
}
@Test
public void aSetTooWideToPackFallsBackToPositionEqualityWithoutChangingTheResult() {
Set<IrisPosition> spread = new LinkedHashSet<>();
spread.add(new IrisPosition(-20_000_000, 60, 0));
spread.add(new IrisPosition(20_000_000, 60, 0));
spread.add(new IrisPosition(20_000_001, 60, 0));
assertEquals(referenceHollowed(spread), MantleWriter.getHollowed(spread));
assertEquals(spread, MantleWriter.getHollowed(spread));
}
@Test
public void maskedVoxelsMatchTheReferenceMaskExpansion() {
Set<IrisPosition> seeds = line(0, 70, 0, 3, 70, 4);
Set<IrisPosition> masks = new LinkedHashSet<>();
masks.add(new IrisPosition(0, 0, 0));
masks.add(new IrisPosition(1, 0, 0));
masks.add(new IrisPosition(0, 1, 0));
masks.add(new IrisPosition(-1, 0, 1));
masks.add(new IrisPosition(0, 0, -2));
assertEquals(referenceMasked(seeds, masks, 3.0D), MantleWriter.getMasked(seeds, masks, 3.0D));
}
@Test
public void anEmptyMaskProducesNoVoxels() {
assertTrue(MantleWriter.getMasked(line(0, 70, 0, 2, 70, 2), new LinkedHashSet<>(), 2.0D).isEmpty());
}
@Test
public void aPositionLookupReportsMembershipOutsideItsBoundingBoxAsAbsent() {
Set<IrisPosition> positions = new LinkedHashSet<>();
positions.add(new IrisPosition(10, 20, 30));
MantleWriter.PositionLookup lookup = MantleWriter.PositionLookup.of(positions);
assertTrue(lookup.contains(10, 20, 30));
assertFalse(lookup.contains(9, 20, 30));
assertFalse(lookup.contains(10, 20, 31));
assertFalse(lookup.contains(Integer.MIN_VALUE, 20, 30));
assertFalse(lookup.contains(Integer.MAX_VALUE, 20, 30));
}
@Test
public void anEmptyPositionLookupContainsNothing() {
MantleWriter.PositionLookup lookup = MantleWriter.PositionLookup.of(new LinkedHashSet<>());
assertFalse(lookup.contains(0, 0, 0));
assertFalse(lookup.contains(-1, -1, -1));
}
private static Set<IrisPosition> line(int x1, int y1, int z1, int x2, int y2, int z2) {
Set<IrisPosition> positions = new LinkedHashSet<>();
int steps = Math.max(Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1)), Math.abs(z2 - z1));
for (int step = 0; step <= steps; step++) {
double t = steps == 0 ? 0D : (double) step / steps;
positions.add(new IrisPosition(
(int) Math.round(x1 + t * (x2 - x1)),
(int) Math.round(y1 + t * (y2 - y1)),
(int) Math.round(z1 + t * (z2 - z1))));
}
return positions;
}
static Set<IrisPosition> referenceBallooned(Set<IrisPosition> vset, double radius) {
Set<IrisPosition> returnset = new HashSet<>();
int ceilrad = (int) Math.ceil(radius);
double r2 = Math.pow(radius, 2);
for (IrisPosition v : vset) {
int tipx = v.getX();
int tipy = v.getY();
int tipz = v.getZ();
for (int loopx = tipx - ceilrad; loopx <= tipx + ceilrad; loopx++) {
for (int loopy = tipy - ceilrad; loopy <= tipy + ceilrad; loopy++) {
for (int loopz = tipz - ceilrad; loopz <= tipz + ceilrad; loopz++) {
if (referenceHypot(loopx - tipx, loopy - tipy, loopz - tipz) <= r2) {
returnset.add(new IrisPosition(loopx, loopy, loopz));
}
}
}
}
}
return returnset;
}
static Set<IrisPosition> referenceHollowed(Set<IrisPosition> vset) {
Set<IrisPosition> returnset = new LinkedHashSet<>();
for (IrisPosition v : vset) {
double x = v.getX();
double y = v.getY();
double z = v.getZ();
if (!(vset.contains(new IrisPosition(x + 1, y, z))
&& vset.contains(new IrisPosition(x - 1, y, z))
&& vset.contains(new IrisPosition(x, y + 1, z))
&& vset.contains(new IrisPosition(x, y - 1, z))
&& vset.contains(new IrisPosition(x, y, z + 1))
&& vset.contains(new IrisPosition(x, y, z - 1)))) {
returnset.add(v);
}
}
return returnset;
}
static Set<IrisPosition> referenceMasked(Set<IrisPosition> vectors, Set<IrisPosition> masks, double radius) {
Set<IrisPosition> vset = new LinkedHashSet<>();
int ceil = (int) Math.ceil(radius);
double r2 = Math.pow(radius, 2);
for (IrisPosition v : vectors) {
int tipX = v.getX();
int tipY = v.getY();
int tipZ = v.getZ();
for (int x = -ceil; x <= ceil; x++) {
for (int y = -ceil; y <= ceil; y++) {
for (int z = -ceil; z <= ceil; z++) {
if (referenceHypot(x, y, z) > r2 || !masks.contains(new IrisPosition(x, y, z))) {
continue;
}
vset.add(new IrisPosition(tipX + x, tipY + y, tipZ + z));
}
}
}
}
return vset;
}
private static double referenceHypot(double... pars) {
double sum = 0;
for (double d : pars) {
sum += Math.pow(d, 2);
}
return sum;
}
}