Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cubicchunks.worldgen.generator.custom.builder;
- import com.google.common.collect.AbstractIterator;
- import net.minecraft.util.math.MathHelper;
- import net.minecraft.util.math.Vec3i;
- import static java.lang.Math.max;
- import static java.lang.Math.min;
- class ScalingLerpIBuilderIterator extends AbstractIterator<IBuilder.IEntry> {
- // TODO: explain how it works
- private final int minX;
- private final int minY;
- private final int minZ;
- private final int maxX;
- private final int maxY;
- private final int maxZ;
- private final double xStep;
- private final double yStep;
- private final double zStep;
- private final int minGridX;
- private final int minGridY;
- private final int minGridZ;
- private final int maxGridX;
- private final int maxGridY;
- private final int maxGridZ;
- private final int scaleX;
- private final int scaleY;
- private final int scaleZ;
- private final IBuilder builder;
- private int nextGridX;
- private int nextGridY;
- private int nextGridZ;
- private int nextRelX;
- private int nextRelY;
- private int nextRelZ;
- private double v000, v001, v010, v011;
- private double vx00, vx01, vx10, vx11;
- private double vxy0, vxy1;
- private double vxyz;
- private double dx00, dx01, dx10, dx11;
- private double dxy0, dxy1;
- private double dxyz;
- ScalingLerpIBuilderIterator(IBuilder builder, Vec3i start, Vec3i end, Vec3i scale) {
- this.builder = builder;
- this.scaleX = scale.getX();
- this.scaleY = scale.getY();
- this.scaleZ = scale.getZ();
- minX = min(start.getX(), end.getX());
- minY = min(start.getY(), end.getY());
- minZ = min(start.getZ(), end.getZ());
- maxX = max(start.getX(), end.getX());
- maxY = max(start.getY(), end.getY());
- maxZ = max(start.getZ(), end.getZ());
- minGridX = MathHelper.intFloorDiv(minX, scaleX);
- minGridY = MathHelper.intFloorDiv(minY, scaleY);
- minGridZ = MathHelper.intFloorDiv(minZ, scaleZ);
- maxGridX = MathHelper.intFloorDiv(maxX, scaleX);
- maxGridY = MathHelper.intFloorDiv(maxY, scaleY);
- maxGridZ = MathHelper.intFloorDiv(maxZ, scaleZ);
- nextGridX = minGridX;
- nextGridY = minGridY;
- nextGridZ = minGridZ;
- xStep = 1.0/scaleX;
- yStep = 1.0/scaleY;
- zStep = 1.0/scaleZ;
- nextRelX = 0;
- nextRelY = 0;
- nextRelZ = 0;
- }
- @Override public IBuilder.IEntry computeNext() {
- // if out of bounds...
- if (nextGridX > maxGridX) {
- endOfData();
- return null;
- }
- if (nextRelZ == 0) {
- if (nextRelY == 0) {
- if (nextRelX == 0) {
- onResetX();
- }
- onResetY();
- }
- onResetZ();
- }
- vxyz += dxyz;
- IBuilder.IEntry entry = new IBuilder.ImmutbleEntry(
- global(nextGridX, scaleX, nextRelX),
- global(nextGridY, scaleY, nextRelY),
- global(nextGridZ, scaleZ, nextRelZ),
- vxyz
- );
- incrementPos();
- return entry;
- }
- private void onResetX() {
- nextRelX = boundClampRelPosMin(nextGridX, scaleX, minX);
- // get corners
- v000 = builder.get(nextGridX, nextGridY, nextGridZ);
- v001 = builder.get(nextGridX, nextGridY, nextGridZ + 1);
- v010 = builder.get(nextGridX, nextGridY + 1, nextGridZ);
- v011 = builder.get(nextGridX, nextGridY + 1, nextGridZ + 1);
- double v100 = builder.get(nextGridX + 1, nextGridY, nextGridZ);
- double v101 = builder.get(nextGridX + 1, nextGridY, nextGridZ + 1);
- double v110 = builder.get(nextGridX + 1, nextGridY + 1, nextGridZ);
- double v111 = builder.get(nextGridX + 1, nextGridY + 1, nextGridZ + 1);
- // step 1
- dx00 = (v100 - v000)*xStep;
- dx01 = (v101 - v001)*xStep;
- dx10 = (v110 - v010)*xStep;
- dx11 = (v111 - v011)*xStep;
- vx00 = v000 + dx00*nextRelX;
- vx01 = v001 + dx01*nextRelX;
- vx10 = v010 + dx10*nextRelX;
- vx11 = v011 + dx11*nextRelX;
- }
- private void onResetY() {
- nextRelY = boundClampRelPosMin(nextGridY, scaleY, minY);
- // step 2
- dxy0 = (vx10 - vx00)*yStep;
- dxy1 = (vx11 - vx01)*yStep;
- vxy0 = vx00 + dxy0*nextRelY;
- vxy1 = vx01 + dxy1*nextRelY;
- vx00 += dx00;
- vx01 += dx01;
- vx10 += dx10;
- vx11 += dx11;
- }
- private void onResetZ() {
- nextRelZ = boundClampRelPosMin(nextGridZ, scaleZ, minZ);
- dxyz = (vxy1 - vxy0)*zStep;
- vxyz = vxy0 + dxyz*nextRelZ - dxyz;
- vxy0 += dxy0;
- vxy1 += dxy1;
- }
- private void incrementPos() {
- nextRelZ++;
- if (nextRelZ >= scaleZ || global(nextGridZ, scaleZ, nextRelZ) > maxZ) {
- nextRelZ = 0;
- nextRelY++;
- if (nextRelY >= scaleY || global(nextGridY, scaleY, nextRelY) > maxY) {
- nextRelY = 0;
- nextRelX++;
- if (nextRelX >= scaleX || global(nextGridX, scaleX, nextRelX) > maxX) {
- nextRelX = 0;
- incrementGridPos();
- }
- }
- }
- }
- private void incrementGridPos() {
- nextGridZ++;
- if (nextGridZ > maxGridZ) {
- nextGridZ = 0;
- nextGridY++;
- if (nextGridY > maxGridY) {
- nextGridY = 0;
- nextGridX++;
- }
- }
- }
- private static int boundClampRelPosMin(int gridPos, int scale, int minPos) {
- int globPos = gridPos*scale;//+localPos
- if (globPos < minPos) {
- return minPos - globPos;
- }
- return 0;
- }
- private static int global(int gridPos, int scale, int relPos) {
- return gridPos*scale + relPos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement