Gap-free object rotation

- Rotations off the right angles fill every target cell from the nearest source block
- Source blocks that no target cell samples still place at their rotated position
- Right-angle rotations keep the direct transform
- Markers, canopy and tile data follow their resampled source blocks
- Datapack export bakes off-angle variants with the same resampler
- The unused rotateCopy paths are removed
- Placement goldens match the resampled output
This commit is contained in:
Brian Neumann-Fopiano committed 2026-10-31 19:23:25 -04:00
1 parent 2462330787
commit b0860f94fc
14 files changed
+874 -189

No files matched your search

@@ -6,6 +6,7 @@ import art.arcane.iris.pack.datapack.export.TemplateVolume;
import art.arcane.iris.pack.datapack.export.VanillaBlockState;
import art.arcane.iris.pack.datapack.export.VanillaResourceIdentifier;
import art.arcane.iris.pack.datapack.export.compile.WeightedState;
import art.arcane.iris.structure.object.RotationResampler;
import art.arcane.volmlib.util.nbt.tag.CompoundTag;
import java.util.ArrayList;
@@ -16,6 +17,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.ToIntFunction;
public final class ObjectVariantBaker {
private static final Set<String> AIR = Set.of("minecraft:air", "minecraft:cave_air", "minecraft:void_air");
@@ -35,6 +37,10 @@ public final class ObjectVariantBaker {
.comparingInt(Voxel::y)
.thenComparingInt(Voxel::x)
.thenComparingInt(Voxel::z);
private static final Comparator<Voxel> PLACEMENT_ORDER = Comparator
.comparingInt(Voxel::x)
.thenComparingInt(Voxel::y)
.thenComparingInt(Voxel::z);
public static String templateId(String namespace, String objectPath, VariantSpec spec, DataVersion target) {
VanillaResourceIdentifier.requireNamespace(namespace, "template namespace");
@@ -59,7 +65,7 @@ public final class ObjectVariantBaker {
sizeZ = scaledSize(sizeZ, spec.scale());
voxels = scale(voxels, spec.scale(), sizeX, sizeY, sizeZ);
}
voxels = anchor(voxels, spec.rotation(), sizeX / 2, sizeY / 2, sizeZ / 2);
voxels = anchor(templateId, voxels, spec.rotation(), sizeX / 2, sizeY / 2, sizeZ / 2);
if (spec.warp() != null) {
voxels = warp(voxels, spec.warp());
}
@@ -243,11 +249,19 @@ public final class ObjectVariantBaker {
return new ArrayList<>(scaled.values());
}
private static List<Voxel> anchor(List<Voxel> voxels, VariantSpec.Rotation rotation, int centerX, int centerY, int centerZ) {
private static List<Voxel> anchor(String templateId, List<Voxel> voxels, VariantSpec.Rotation rotation, int centerX,
int centerY, int centerZ) {
BlockStateRotation states = BlockStateRotation.of(rotation.x(), rotation.y(), rotation.z());
double[] lift = rotate(0, centerY, 0, rotation);
int rotatedCenterY = (int) Math.floor(lift[1]);
List<Voxel> sorted = new ArrayList<>(voxels);
if (!rotation.rightAngles()) {
sorted.sort(PLACEMENT_ORDER);
RotationResampler.Targets targets = RotationResampler.of(templateId, offsets(sorted, Voxel::x, centerX),
offsets(sorted, Voxel::y, centerY), offsets(sorted, Voxel::z, centerZ))
.resample(rotation.x(), rotation.y(), rotation.z());
return resampled(sorted, targets, states, rotatedCenterY);
}
sorted.sort(VOXEL_ORDER);
Map<Long, Voxel> anchored = new LinkedHashMap<>();
for (Voxel voxel : sorted) {
@@ -260,6 +274,35 @@ public final class ObjectVariantBaker {
return new ArrayList<>(anchored.values());
}
private static int[] offsets(List<Voxel> voxels, ToIntFunction<Voxel> axis, int center) {
int[] offsets = new int[voxels.size()];
for (int index = 0; index < offsets.length; index++) {
offsets[index] = axis.applyAsInt(voxels.get(index)) - center;
}
return offsets;
}
private static List<Voxel> resampled(List<Voxel> sources, RotationResampler.Targets targets, BlockStateRotation states,
int liftY) {
int[] x = targets.x();
int[] y = targets.y();
int[] z = targets.z();
int[] origins = targets.sources();
VanillaBlockState[] turned = new VanillaBlockState[sources.size()];
List<Voxel> anchored = new ArrayList<>(origins.length);
for (int index = 0; index < origins.length; index++) {
int origin = origins[index];
Voxel source = sources.get(origin);
VanillaBlockState state = turned[origin];
if (state == null) {
state = states.apply(source.state());
turned[origin] = state;
}
anchored.add(new Voxel(x[index], y[index] + liftY, z[index], state, source.nbt(), source.root()));
}
return anchored;
}
private static double[] rotate(double x, double y, double z, VariantSpec.Rotation rotation) {
double rx = x;
double ry = y;
@@ -44,6 +44,10 @@ public record VariantSpec(double scale, Rotation rotation, VariantWarp warp, dou
public boolean none() {
return x == 0 && y == 0 && z == 0;
}
public boolean rightAngles() {
return x % 90 == 0 && y % 90 == 0 && z % 90 == 0;
}
}
public static final class Builder {
@@ -386,10 +386,6 @@ public class IrisObject extends IrisRegistrant {
});
}
public IrisObject rotateCopy(IrisObjectRotation rt) {
return IrisObjectTransforms.rotateCopy(this, rt);
}
public IrisObject scaled(double scale, IrisObjectPlacementScaleInterpolator interpolation) {
return IrisObjectTransforms.scaled(this, scale, interpolation);
}
@@ -168,14 +168,6 @@ public class IrisObjectRotation {
return getRotation(spin, zAxis);
}
public IrisObject rotateCopy(IrisObject e) {
if (e == null) {
return null;
}
return e.rotateCopy(this);
}
public IrisBlockVector rotate(IrisBlockVector direction) {
return rotate(direction, 0, 0, 0);
}
@@ -43,72 +43,12 @@ import java.util.Map;
import java.util.Set;
/**
* Geometric transforms for {@link IrisObject}: rotation, scaling and the interpolated upscalers.
* Geometric transforms for {@link IrisObject}: scaling and the interpolated upscalers.
*/
final class IrisObjectTransforms {
private IrisObjectTransforms() {
}
static IrisObject rotateCopy(IrisObject self, IrisObjectRotation rt) {
IrisObject copy = self.copy();
rotate(copy, rt, 0, 0, 0);
return copy;
}
static void rotate(IrisObject self, IrisObjectRotation r, int spinx, int spiny, int spinz) {
self.writeLock.lock();
try {
VectorMap<NativeBlockState> d = new VectorMap<>();
VectorMap<TileData> dx = new VectorMap<>();
TreeGrounding grounding = self.getTreeGrounding();
Set<TreeBlockCanvas.Vec> canopy = grounding == null ? null : new HashSet<>();
for (Map.Entry<IrisBlockVector, NativeBlockState> entry : self.blocks) {
NativeBlockState rotated = r.rotate(entry.getValue(), spinx, spiny, spinz);
if (rotated == null) {
continue;
}
IrisBlockVector destination = r.rotate(entry.getKey(), spinx, spiny, spinz);
d.put(destination, rotated);
if (canopy != null) {
TreeBlockCanvas.Vec cell = new TreeBlockCanvas.Vec(destination.getBlockX(),
destination.getBlockY(), destination.getBlockZ());
IrisBlockVector source = entry.getKey();
if (grounding.isCanopy(source.getBlockX(), source.getBlockY(), source.getBlockZ())) {
canopy.add(cell);
} else {
canopy.remove(cell);
}
}
TileData tile = self.states.get(entry.getKey());
if (tile == null) {
dx.remove(destination);
} else {
dx.put(destination, tile);
}
}
self.blocks = d;
self.states = dx;
if (grounding != null) {
List<TreeGrounding.Root> roots = new ArrayList<>();
for (TreeGrounding.Root root : grounding.roots()) {
IrisBlockVector position = r.rotate(new IrisBlockVector(root.x(), root.y(), root.z()), spinx, spiny, spinz);
NativeBlockState state = d.get(position);
if (state != null) {
roots.add(new TreeGrounding.Root(position.getBlockX(), position.getBlockY(), position.getBlockZ(), state));
}
}
self.setTreeGrounding(TreeGrounding.create(roots, grounding.maxExtension(), canopy));
}
IrisObjectShaping.shrinkwrap(self);
self.surfaceSupportOffsets.reset();
self.floatingFootprint.reset();
} finally {
self.writeLock.unlock();
}
}
static IrisObject scaled(IrisObject self, double scale, IrisObjectPlacementScaleInterpolator interpolation) {
return scaled(self, scale, interpolation, ScaleOrigin.DEFAULT);
}
@@ -67,12 +67,12 @@ final class ObjectMarkerPlacement {
}
}
String[] resolved = new String[bake.count()];
String[] bySource = new String[bake.sourceCount()];
VectorMap<NativeBlockState>.Cursor cursor = blocks.cursor();
int index = 0;
while (index < resolved.length && cursor.next()) {
resolved[index++] = markers.get(cursor.key());
while (index < bySource.length && cursor.next()) {
bySource[index++] = markers.get(cursor.key());
}
return resolved;
return bake.markers(bySource);
}
}
@@ -8,6 +8,7 @@ import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@@ -21,15 +22,25 @@ final class ObjectPlacementBake {
private final int[] offsetY;
private final int[] offsetZ;
private final int[] layerY;
private final short[] paletteIndexes;
private final long[] canopy;
private final int[] tileBlocks;
private final TileData[] tileValues;
private final int[] sources;
private volatile double[] exact;
private ObjectPlacementBake(Layout layout, Key key, int[] offsetX, int[] offsetY, int[] offsetZ, int[] layerY) {
private ObjectPlacementBake(Layout layout, Key key, Cells cells) {
this.layout = layout;
this.key = key;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.offsetZ = offsetZ;
this.layerY = layerY;
this.offsetX = cells.offsetX();
this.offsetY = cells.offsetY();
this.offsetZ = cells.offsetZ();
this.layerY = cells.layerY();
this.paletteIndexes = cells.paletteIndexes();
this.canopy = cells.canopy();
this.tileBlocks = cells.tileBlocks();
this.tileValues = cells.tileValues();
this.sources = cells.sources();
}
static ObjectPlacementBake of(IrisObject object, SpinKernel spin, boolean ceilingHang, IrisBlockVector translateOffset) {
@@ -42,7 +53,10 @@ final class ObjectPlacementBake {
if (cached != null && cached.layout == layout) {
return cached;
}
ObjectPlacementBake built = bake(object, layout, key, spin, ceilingHang, translateOffset);
Cells cells = spin.axisAligned
? transformed(object, layout, spin, ceilingHang, translateOffset)
: resampled(object, layout, spin, ceilingHang, translateOffset);
ObjectPlacementBake built = new ObjectPlacementBake(layout, key, cells);
if (cached != null) {
variants.replace(key, cached, built);
} else if (variants.size() < MAXIMUM_VARIANTS_PER_OBJECT) {
@@ -54,8 +68,8 @@ final class ObjectPlacementBake {
}
}
private static ObjectPlacementBake bake(IrisObject object, Layout layout, Key key, SpinKernel spin, boolean ceilingHang,
IrisBlockVector translateOffset) {
private static Cells transformed(IrisObject object, Layout layout, SpinKernel spin, boolean ceilingHang,
IrisBlockVector translateOffset) {
int count = layout.count;
int[] x = new int[count];
int[] y = new int[count];
@@ -66,7 +80,7 @@ final class ObjectPlacementBake {
VectorMap<NativeBlockState>.Cursor cursor = object.blocks.cursor();
int index = 0;
while (cursor.next() && index < count) {
transform(cursor.key(), rotated, spin, ceilingHang, translateOffset);
RotationResampler.transform(cursor.key(), rotated, spin, ceilingHang, translateOffset);
int roundedY = (int) Math.round(rotated.getY());
int floorY = rotated.getBlockY();
x[index] = (int) Math.round(rotated.getX());
@@ -79,24 +93,46 @@ final class ObjectPlacementBake {
if (index != count) {
throw new IllegalStateException("Object " + object.getLoadKey() + " changed while its placement bake was built");
}
return new ObjectPlacementBake(layout, key, x, y, z, layerDiffers ? layer : null);
return new Cells(x, y, z, layerDiffers ? layer : null, layout.paletteIndexes, layout.canopy, layout.tileBlocks,
layout.tileValues, null);
}
private static void transform(IrisBlockVector source, IrisBlockVector target, SpinKernel spin, boolean ceilingHang,
IrisBlockVector translateOffset) {
target.setX(source.getX());
target.setY(source.getY());
target.setZ(source.getZ());
spin.rotate(target);
if (ceilingHang) {
target.setY(-target.getBlockY());
}
if (translateOffset != null) {
target.add(translateOffset);
private static Cells resampled(IrisObject object, Layout layout, SpinKernel spin, boolean ceilingHang,
IrisBlockVector translateOffset) {
RotationResampler.Targets targets = layout.resampler(object).resample(spin, ceilingHang, translateOffset);
return gather(layout, targets.x(), targets.y(), targets.z(), targets.sources());
}
private static Cells gather(Layout layout, int[] x, int[] y, int[] z, int[] sources) {
int count = sources.length;
short[] paletteIndexes = new short[count];
long[] canopy = layout.canopy == null ? null : new long[(count + 63) >>> 6];
IntArrayList tileBlocks = layout.tileBlocks == null ? null : new IntArrayList();
List<TileData> tileValues = layout.tileBlocks == null ? null : new ArrayList<>();
for (int index = 0; index < count; index++) {
int source = sources[index];
paletteIndexes[index] = layout.paletteIndexes[source];
if (canopy != null && (layout.canopy[source >>> 6] & (1L << (source & 63))) != 0L) {
canopy[index >>> 6] |= 1L << (index & 63);
}
if (tileBlocks != null) {
int slot = Arrays.binarySearch(layout.tileBlocks, source);
if (slot >= 0) {
tileBlocks.add(index);
tileValues.add(layout.tileValues[slot]);
}
}
}
boolean tiled = tileBlocks != null && !tileBlocks.isEmpty();
return new Cells(x, y, z, null, paletteIndexes, canopy, tiled ? tileBlocks.toIntArray() : null,
tiled ? tileValues.toArray(new TileData[0]) : null, sources);
}
int count() {
return offsetX.length;
}
int sourceCount() {
return layout.count;
}
@@ -117,7 +153,7 @@ final class ObjectPlacementBake {
}
int paletteIndex(int index) {
return layout.paletteIndexes[index];
return paletteIndexes[index];
}
NativeBlockState[] palette() {
@@ -125,32 +161,51 @@ final class ObjectPlacementBake {
}
int[] tileBlocks() {
return layout.tileBlocks;
return tileBlocks;
}
TileData[] tileValues() {
return layout.tileValues;
return tileValues;
}
boolean canopy(int index) {
long[] canopy = layout.canopy;
return canopy != null && (canopy[index >>> 6] & (1L << (index & 63))) != 0L;
}
String[] markers(String[] sourceMarkers) {
if (sources == null) {
return sourceMarkers;
}
String[] unclaimed = sourceMarkers.clone();
String[] resolved = new String[sources.length];
for (int index = 0; index < sources.length; index++) {
int source = sources[index];
resolved[index] = unclaimed[source];
unclaimed[source] = null;
}
return resolved;
}
double[] exact(IrisObject object, SpinKernel spin) {
double[] resolved = exact;
if (resolved != null) {
return resolved;
}
resolved = sources == null ? transformedExact(object, spin) : targetExact();
exact = resolved;
return resolved;
}
private double[] transformedExact(IrisObject object, SpinKernel spin) {
int count = layout.count;
object.readLock.lock();
try {
resolved = new double[count * 3];
double[] resolved = new double[count * 3];
IrisBlockVector rotated = new IrisBlockVector(0, 0, 0);
VectorMap<NativeBlockState>.Cursor cursor = object.blocks.cursor();
int index = 0;
while (cursor.next() && index < count) {
transform(cursor.key(), rotated, spin, key.ceilingHang(), key.translateOffset());
RotationResampler.transform(cursor.key(), rotated, spin, key.ceilingHang(), key.translateOffset());
resolved[index * 3] = rotated.getX();
resolved[index * 3 + 1] = rotated.getY();
resolved[index * 3 + 2] = rotated.getZ();
@@ -159,10 +214,19 @@ final class ObjectPlacementBake {
if (index != count) {
throw new IllegalStateException("Object " + object.getLoadKey() + " changed while its placement bake was read");
}
return resolved;
} finally {
object.readLock.unlock();
}
exact = resolved;
}
private double[] targetExact() {
double[] resolved = new double[offsetX.length * 3];
for (int index = 0; index < offsetX.length; index++) {
resolved[index * 3] = offsetX[index];
resolved[index * 3 + 1] = offsetY[index];
resolved[index * 3 + 2] = offsetZ[index];
}
return resolved;
}
@@ -176,6 +240,7 @@ final class ObjectPlacementBake {
private final int[] tileBlocks;
private final TileData[] tileValues;
private final long[] canopy;
private volatile RotationResampler resampler;
private Layout(long blocksRevision, long statesRevision, TreeGrounding grounding, int count, short[] paletteIndexes,
NativeBlockState[] palette, int[] tileBlocks, TileData[] tileValues, long[] canopy) {
@@ -270,6 +335,15 @@ final class ObjectPlacementBake {
&& object.states.modificationRevision() == statesRevision
&& object.getTreeGrounding() == grounding;
}
private RotationResampler resampler(IrisObject object) {
RotationResampler resolved = resampler;
if (resolved == null) {
resolved = RotationResampler.of(object, count);
resampler = resolved;
}
return resolved;
}
}
record Key(boolean rotates, int xMode, int yMode, int zMode, long xCos, long xSin, long yCos, long ySin,
@@ -291,4 +365,8 @@ final class ObjectPlacementBake {
Double.longBitsToDouble(translateY), Double.longBitsToDouble(translateZ)) : null;
}
}
private record Cells(int[] offsetX, int[] offsetY, int[] offsetZ, int[] layerY, short[] paletteIndexes, long[] canopy,
int[] tileBlocks, TileData[] tileValues, int[] sources) {
}
}
@@ -0,0 +1,205 @@
package art.arcane.iris.structure.object;
import art.arcane.iris.generation.block.VectorMap;
import art.arcane.iris.generation.geometry.IrisBlockVector;
import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
import it.unimi.dsi.fastutil.ints.IntArrayList;
public final class RotationResampler {
private final int[] sourceX;
private final int[] sourceY;
private final int[] sourceZ;
private final Box sourceBox;
private final int[] sourceCells;
private RotationResampler(int[] sourceX, int[] sourceY, int[] sourceZ, Box sourceBox, int[] sourceCells) {
this.sourceX = sourceX;
this.sourceY = sourceY;
this.sourceZ = sourceZ;
this.sourceBox = sourceBox;
this.sourceCells = sourceCells;
}
public static RotationResampler of(String owner, int[] x, int[] y, int[] z) {
int count = x.length;
if (y.length != count || z.length != count) {
throw new IllegalArgumentException(owner + " has " + x.length + "/" + y.length + "/" + z.length
+ " source coordinates on its x/y/z axes");
}
if (count == 0) {
return new RotationResampler(x, y, z, new Box(0, 0, 0, 0, 0, 0), new int[1]);
}
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 (int index = 0; index < count; index++) {
minX = Math.min(minX, x[index]);
minY = Math.min(minY, y[index]);
minZ = Math.min(minZ, z[index]);
maxX = Math.max(maxX, x[index]);
maxY = Math.max(maxY, y[index]);
maxZ = Math.max(maxZ, z[index]);
}
Box box = new Box(minX, maxX, minY, maxY, minZ, maxZ);
if (box.volume() > Integer.MAX_VALUE - 8) {
throw new IllegalStateException(owner + " spans " + (maxX - minX + 1) + "x" + (maxY - minY + 1) + "x"
+ (maxZ - minZ + 1) + " blocks, too large to resample for a non-right-angle rotation");
}
int[] cells = new int[(int) box.volume()];
for (int index = 0; index < count; index++) {
cells[(int) box.cell(x[index], y[index], z[index])] = index + 1;
}
return new RotationResampler(x, y, z, box, cells);
}
static RotationResampler of(IrisObject object, int count) {
int[] x = new int[count];
int[] y = new int[count];
int[] z = new int[count];
VectorMap<NativeBlockState>.Cursor cursor = object.blocks.cursor();
int index = 0;
while (index < count && cursor.next()) {
IrisBlockVector position = cursor.key();
x[index] = position.getBlockX();
y[index] = position.getBlockY();
z[index] = position.getBlockZ();
index++;
}
if (index != count) {
throw new IllegalStateException("Object " + object.getLoadKey() + " changed while its placement grid was built");
}
return of("Object " + object.getLoadKey(), x, y, z);
}
public Targets resample(double xDegrees, double yDegrees, double zDegrees) {
return resample(new SpinKernel(IrisObjectRotation.of(xDegrees, yDegrees, zDegrees), 0, 0, 0), false, null);
}
Targets resample(SpinKernel spin, boolean ceilingHang, IrisBlockVector translateOffset) {
int count = sourceX.length;
Box bounds = targetBounds(spin, ceilingHang, translateOffset);
long[] occupied = new long[(int) ((bounds.volume() + 63L) >>> 6)];
long[] sampled = new long[(count + 63) >>> 6];
IntArrayList x = new IntArrayList(count);
IntArrayList y = new IntArrayList(count);
IntArrayList z = new IntArrayList(count);
IntArrayList sources = new IntArrayList(count);
IrisBlockVector probe = new IrisBlockVector(0, 0, 0);
for (int targetX = bounds.minX(); targetX <= bounds.maxX(); targetX++) {
for (int targetY = bounds.minY(); targetY <= bounds.maxY(); targetY++) {
for (int targetZ = bounds.minZ(); targetZ <= bounds.maxZ(); targetZ++) {
probe.setX(targetX);
probe.setY(targetY);
probe.setZ(targetZ);
if (translateOffset != null) {
probe.subtract(translateOffset);
}
if (ceilingHang) {
probe.setY(-probe.getY());
}
spin.inverseRotate(probe);
int source = source((int) Math.round(probe.getX()), (int) Math.round(probe.getY()),
(int) Math.round(probe.getZ()));
if (source < 0) {
continue;
}
long cell = bounds.cell(targetX, targetY, targetZ);
occupied[(int) (cell >>> 6)] |= 1L << (cell & 63L);
sampled[source >>> 6] |= 1L << (source & 63);
x.add(targetX);
y.add(targetY);
z.add(targetZ);
sources.add(source);
}
}
}
for (int source = 0; source < count; source++) {
if ((sampled[source >>> 6] & (1L << (source & 63))) != 0L) {
continue;
}
probe.setX(sourceX[source]);
probe.setY(sourceY[source]);
probe.setZ(sourceZ[source]);
transform(probe, probe, spin, ceilingHang, translateOffset);
int targetX = Math.clamp(Math.round(probe.getX()), bounds.minX(), bounds.maxX());
int targetY = Math.clamp(Math.round(probe.getY()), bounds.minY(), bounds.maxY());
int targetZ = Math.clamp(Math.round(probe.getZ()), bounds.minZ(), bounds.maxZ());
long cell = bounds.cell(targetX, targetY, targetZ);
if ((occupied[(int) (cell >>> 6)] & (1L << (cell & 63L))) == 0L) {
occupied[(int) (cell >>> 6)] |= 1L << (cell & 63L);
x.add(targetX);
y.add(targetY);
z.add(targetZ);
sources.add(source);
}
}
return new Targets(x.toIntArray(), y.toIntArray(), z.toIntArray(), sources.toIntArray());
}
static void transform(IrisBlockVector source, IrisBlockVector target, SpinKernel spin, boolean ceilingHang,
IrisBlockVector translateOffset) {
target.setX(source.getX());
target.setY(source.getY());
target.setZ(source.getZ());
spin.rotate(target);
if (ceilingHang) {
target.setY(-target.getBlockY());
}
if (translateOffset != null) {
target.add(translateOffset);
}
}
private int source(int x, int y, int z) {
return sourceBox.contains(x, y, z) ? sourceCells[(int) sourceBox.cell(x, y, z)] - 1 : -1;
}
private Box targetBounds(SpinKernel spin, boolean ceilingHang, IrisBlockVector translateOffset) {
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;
IrisBlockVector corner = new IrisBlockVector(0, 0, 0);
for (int index = 0; index < 8; index++) {
corner.setX((index & 1) == 0 ? sourceBox.minX() : sourceBox.maxX());
corner.setY((index & 2) == 0 ? sourceBox.minY() : sourceBox.maxY());
corner.setZ((index & 4) == 0 ? sourceBox.minZ() : sourceBox.maxZ());
transform(corner, corner, spin, ceilingHang, translateOffset);
int x = (int) Math.round(corner.getX());
int y = (int) Math.round(corner.getY());
int z = (int) Math.round(corner.getZ());
minX = Math.min(minX, x);
minY = Math.min(minY, y);
minZ = Math.min(minZ, z);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
maxZ = Math.max(maxZ, z);
}
return new Box(minX, maxX, minY, maxY, minZ, maxZ);
}
public record Targets(int[] x, int[] y, int[] z, int[] sources) {
public int count() {
return sources.length;
}
}
private record Box(int minX, int maxX, int minY, int maxY, int minZ, int maxZ) {
private long volume() {
return (long) (maxX - minX + 1) * (maxY - minY + 1) * (maxZ - minZ + 1);
}
private boolean contains(int x, int y, int z) {
return x >= minX && x <= maxX && y >= minY && y <= maxY && z >= minZ && z <= maxZ;
}
private long cell(int x, int y, int z) {
return ((long) (x - minX) * (maxY - minY + 1) + (y - minY)) * (maxZ - minZ + 1) + (z - minZ);
}
}
}
@@ -17,8 +17,10 @@ final class SpinKernel {
static final int MODE_QUARTER = 2;
static final int MODE_THREE_QUARTER = 3;
static final int MODE_ANGLE = 4;
private static final double AXIS_ALIGNED_EPSILON = 1.0E-9D;
final boolean rotates;
final boolean axisAligned;
final int xMode;
final int yMode;
final int zMode;
@@ -44,6 +46,9 @@ final class SpinKernel {
zSin = Math.sin(zAngle);
yCos = Math.cos(yAngle);
ySin = Math.sin(yAngle);
axisAligned = Math.abs(xCos * xSin) < AXIS_ALIGNED_EPSILON
&& Math.abs(zCos * zSin) < AXIS_ALIGNED_EPSILON
&& Math.abs(yCos * ySin) < AXIS_ALIGNED_EPSILON;
}
private static int modeOf(IrisAxisRotationClamp clamp) {
@@ -149,4 +154,85 @@ final class SpinKernel {
}
}
}
void inverseRotate(IrisBlockVector v) {
if (!rotates) {
return;
}
switch (yMode) {
case MODE_FLIP -> {
v.setX(-v.getX());
v.setZ(-v.getZ());
}
case MODE_QUARTER -> {
double x = v.getX();
v.setX(-v.getZ());
v.setZ(x);
}
case MODE_THREE_QUARTER -> {
double x = v.getX();
v.setX(v.getZ());
v.setZ(-x);
}
case MODE_ANGLE -> {
double x = yCos * v.getX() - ySin * v.getZ();
double z = ySin * v.getX() + yCos * v.getZ();
v.setX(x);
v.setZ(z);
}
default -> {
}
}
switch (zMode) {
case MODE_FLIP -> {
v.setY(-v.getY());
v.setX(-v.getX());
}
case MODE_QUARTER -> {
double y = v.getY();
v.setY(-v.getX());
v.setX(y);
}
case MODE_THREE_QUARTER -> {
double y = v.getY();
v.setY(v.getX());
v.setX(-y);
}
case MODE_ANGLE -> {
double x = zCos * v.getX() + zSin * v.getY();
double y = -zSin * v.getX() + zCos * v.getY();
v.setX(x);
v.setY(y);
}
default -> {
}
}
switch (xMode) {
case MODE_FLIP -> {
v.setZ(-v.getZ());
v.setY(-v.getY());
}
case MODE_QUARTER -> {
double z = v.getZ();
v.setZ(-v.getY());
v.setY(z);
}
case MODE_THREE_QUARTER -> {
double z = v.getZ();
v.setZ(v.getY());
v.setY(-z);
}
case MODE_ANGLE -> {
double y = xCos * v.getY() + xSin * v.getZ();
double z = -xSin * v.getY() + xCos * v.getZ();
v.setY(y);
v.setZ(z);
}
default -> {
}
}
}
}