refactor: efficiency improvements to some editsession methods (#2304)

This commit is contained in:
Jordan 2023-06-27 17:36:54 +01:00 committed by GitHub
parent 97ee71bbd0
commit 476ba4ab41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2161,10 +2161,12 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
final int ceilRadiusX = (int) Math.ceil(radiusX); final int ceilRadiusX = (int) Math.ceil(radiusX);
final int ceilRadiusZ = (int) Math.ceil(radiusZ); final int ceilRadiusZ = (int) Math.ceil(radiusZ);
double xSqr; double xSqr, zSqr, distanceSq;
double zSqr; double xn, zn;
double distanceSq; double dx2, dz2;
double nextXn = 0; double nextXn = 0;
double nextZn, nextMinZn;
int xx, x_x, zz, z_z, yy;
if (thickness != 0) { if (thickness != 0) {
double nextMinXn = 0; double nextMinXn = 0;
@ -2172,19 +2174,18 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
final double minInvRadiusZ = 1 / (radiusZ - thickness); final double minInvRadiusZ = 1 / (radiusZ - thickness);
forX: forX:
for (int x = 0; x <= ceilRadiusX; ++x) { for (int x = 0; x <= ceilRadiusX; ++x) {
final double xn = nextXn; xn = nextXn;
double dx2 = nextMinXn * nextMinXn; dx2 = nextMinXn * nextMinXn;
nextXn = (x + 1) * invRadiusX; nextXn = (x + 1) * invRadiusX;
nextMinXn = (x + 1) * minInvRadiusX; nextMinXn = (x + 1) * minInvRadiusX;
double nextZn = 0; nextZn = 0;
double nextMinZn = 0; nextMinZn = 0;
xSqr = xn * xn; xSqr = xn * xn;
xx = px + x;
x_x = px - x;
forZ: forZ:
for (int z = 0; z <= ceilRadiusZ; ++z) { for (int z = 0; z <= ceilRadiusZ; ++z) {
final double zn = nextZn; zn = nextZn;
double dz2 = nextMinZn * nextMinZn;
nextZn = (z + 1) * invRadiusZ;
nextMinZn = (z + 1) * minInvRadiusZ;
zSqr = zn * zn; zSqr = zn * zn;
distanceSq = xSqr + zSqr; distanceSq = xSqr + zSqr;
if (distanceSq > 1) { if (distanceSq > 1) {
@ -2193,16 +2194,23 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
} }
break forZ; break forZ;
} }
dz2 = nextMinZn * nextMinZn;
nextZn = (z + 1) * invRadiusZ;
nextMinZn = (z + 1) * minInvRadiusZ;
if ((dz2 + nextMinXn * nextMinXn <= 1) && (nextMinZn * nextMinZn + dx2 <= 1)) { if ((dz2 + nextMinXn * nextMinXn <= 1) && (nextMinZn * nextMinZn + dx2 <= 1)) {
continue; continue;
} }
zz = pz + z;
z_z = pz - z;
for (int y = 0; y < height; ++y) { for (int y = 0; y < height; ++y) {
this.setBlock(mutableBlockVector3.setComponents(px + x, py + y, pz + z), block); yy = py + y;
this.setBlock(mutableBlockVector3.setComponents(px - x, py + y, pz + z), block); this.setBlock(xx, yy, zz, block);
this.setBlock(mutableBlockVector3.setComponents(px + x, py + y, pz - z), block); this.setBlock(x_x, yy, zz, block);
this.setBlock(mutableBlockVector3.setComponents(px - x, py + y, pz - z), block); this.setBlock(xx, yy, z_z, block);
this.setBlock(x_x, yy, z_z, block);
} }
} }
} }
@ -2210,14 +2218,17 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
//FAWE end //FAWE end
forX: forX:
for (int x = 0; x <= ceilRadiusX; ++x) { for (int x = 0; x <= ceilRadiusX; ++x) {
final double xn = nextXn; xn = nextXn;
nextXn = (x + 1) * invRadiusX; nextXn = (x + 1) * invRadiusX;
double nextZn = 0; nextZn = 0;
xSqr = xn * xn; xSqr = xn * xn;
// FAWE start
xx = px + x;
x_x = px - x;
//FAWE end
forZ: forZ:
for (int z = 0; z <= ceilRadiusZ; ++z) { for (int z = 0; z <= ceilRadiusZ; ++z) {
final double zn = nextZn; zn = nextZn;
nextZn = (z + 1) * invRadiusZ;
zSqr = zn * zn; zSqr = zn * zn;
distanceSq = xSqr + zSqr; distanceSq = xSqr + zSqr;
if (distanceSq > 1) { if (distanceSq > 1) {
@ -2227,18 +2238,27 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
break forZ; break forZ;
} }
// FAWE start
nextZn = (z + 1) * invRadiusZ;
//FAWE end
if (!filled) { if (!filled) {
if ((zSqr + nextXn * nextXn <= 1) && (nextZn * nextZn + xSqr <= 1)) { if ((zSqr + nextXn * nextXn <= 1) && (nextZn * nextZn + xSqr <= 1)) {
continue; continue;
} }
} }
//FAWE start
zz = pz + z;
z_z = pz - z;
//FAWE end
for (int y = 0; y < height; ++y) { for (int y = 0; y < height; ++y) {
//FAWE start - mutable //FAWE start
this.setBlock(mutableBlockVector3.setComponents(px + x, py + y, pz + z), block); yy = py + y;
this.setBlock(mutableBlockVector3.setComponents(px - x, py + y, pz + z), block); this.setBlock(xx, yy, zz, block);
this.setBlock(mutableBlockVector3.setComponents(px + x, py + y, pz - z), block); this.setBlock(x_x, yy, zz, block);
this.setBlock(mutableBlockVector3.setComponents(px - x, py + y, pz - z), block); this.setBlock(xx, yy, z_z, block);
this.setBlock(x_x, yy, z_z, block);
//FAWE end //FAWE end
} }
} }
@ -2293,7 +2313,6 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
int px = pos.getBlockX(); int px = pos.getBlockX();
int py = pos.getBlockY(); int py = pos.getBlockY();
int pz = pos.getBlockZ(); int pz = pos.getBlockZ();
MutableBlockVector3 mutable = new MutableBlockVector3();
final int ceilRadiusX = (int) Math.ceil(radiusX); final int ceilRadiusX = (int) Math.ceil(radiusX);
final int ceilRadiusY = (int) Math.ceil(radiusY); final int ceilRadiusY = (int) Math.ceil(radiusY);
@ -2301,31 +2320,43 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
double threshold = 0.5; double threshold = 0.5;
double dx, dy, dz, dxy, dxz, dyz, dxyz;
int xx, x_x, yy, y_y, zz, z_z;
double xnx, yny, znz;
double nextXn = 0; double nextXn = 0;
double dx; double nextYn, nextZn;
double dy; double nextXnSq, nextYnSq, nextZnSq;
double dz; double xn, yn, zn;
double dxy;
double dxyz;
forX: forX:
for (int x = 0; x <= ceilRadiusX; ++x) { for (int x = 0; x <= ceilRadiusX; ++x) {
final double xn = nextXn; xn = nextXn;
dx = xn * xn; dx = xn * xn;
nextXn = (x + 1) * invRadiusX; nextXn = (x + 1) * invRadiusX;
double nextYn = 0; nextXnSq = nextXn * nextXn;
nextYn = 0;
xx = px + x;
x_x = px - x;
xnx = x * nx;
forY: forY:
for (int y = 0; y <= ceilRadiusY; ++y) { for (int y = 0; y <= ceilRadiusY; ++y) {
final double yn = nextYn; yn = nextYn;
dy = yn * yn; dy = yn * yn;
dxy = dx + dy; dxy = dx + dy;
nextYn = (y + 1) * invRadiusY; nextYn = (y + 1) * invRadiusY;
double nextZn = 0; nextYnSq = nextYn * nextYn;
nextZn = 0;
yy = py + y;
y_y = py - y;
yny = y * ny;
forZ: forZ:
for (int z = 0; z <= ceilRadiusZ; ++z) { for (int z = 0; z <= ceilRadiusZ; ++z) {
final double zn = nextZn; zn = nextZn;
dz = zn * zn; dz = zn * zn;
dxyz = dxy + dz; dxyz = dxy + dz;
dxz = dx + dz;
dyz = dy + dz;
nextZn = (z + 1) * invRadiusZ; nextZn = (z + 1) * invRadiusZ;
nextZnSq = nextZn * nextZn;
if (dxyz > 1) { if (dxyz > 1) {
if (z == 0) { if (z == 0) {
if (y == 0) { if (y == 0) {
@ -2336,34 +2367,37 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
break forZ; break forZ;
} }
if (!filled) { if (!filled) {
if (nextXn * nextXn + dy + dz <= 1 && nextYn * nextYn + dx + dz <= 1 && nextZn * nextZn + dx + dy <= 1) { if (nextXnSq + dyz <= 1 && nextYnSq + dxz <= 1 && nextZnSq + dxy <= 1) {
continue; continue;
} }
} }
zz = pz + z;
z_z = pz - z;
znz = z * nz;
if (Math.abs((x) * nx + (y) * ny + (z) * nz) < threshold) { if (Math.abs(xnx + yny + znz) < threshold) {
setBlock(mutable.setComponents(px + x, py + y, pz + z), block); setBlock(xx, yy, zz, block);
} }
if (Math.abs((-x) * nx + (y) * ny + (z) * nz) < threshold) { if (Math.abs(-xnx + yny + znz) < threshold) {
setBlock(mutable.setComponents(px - x, py + y, pz + z), block); setBlock(x_x, yy, zz, block);
} }
if (Math.abs((x) * nx + (-y) * ny + (z) * nz) < threshold) { if (Math.abs(xnx - yny + znz) < threshold) {
setBlock(mutable.setComponents(px + x, py - y, pz + z), block); setBlock(xx, y_y, zz, block);
} }
if (Math.abs((x) * nx + (y) * ny + (-z) * nz) < threshold) { if (Math.abs(xnx + yny - znz) < threshold) {
setBlock(mutable.setComponents(px + x, py + y, pz - z), block); setBlock(xx, yy, z_z, block);
} }
if (Math.abs((-x) * nx + (-y) * ny + (z) * nz) < threshold) { if (Math.abs(-xnx - yny + znz) < threshold) {
setBlock(mutable.setComponents(px - x, py - y, pz + z), block); setBlock(x_x, y_y, zz, block);
} }
if (Math.abs((x) * nx + (-y) * ny + (-z) * nz) < threshold) { if (Math.abs(xnx - yny - znz) < threshold) {
setBlock(mutable.setComponents(px + x, py - y, pz - z), block); setBlock(xx, y_y, z_z, block);
} }
if (Math.abs((-x) * nx + (y) * ny + (-z) * nz) < threshold) { if (Math.abs(-xnx + yny - znz) < threshold) {
setBlock(mutable.setComponents(px - x, py + y, pz - z), block); setBlock(x_x, yy, z_z, block);
} }
if (Math.abs((-x) * nx + (-y) * ny + (-z) * nz) < threshold) { if (Math.abs(-xnx - yny - znz) < threshold) {
setBlock(mutable.setComponents(px - x, py - y, pz - z), block); setBlock(x_x, y_y, z_z, block);
} }
} }
} }
@ -2418,29 +2452,38 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
final int ceilRadiusZ = (int) Math.ceil(radiusZ); final int ceilRadiusZ = (int) Math.ceil(radiusZ);
//FAWE start //FAWE start
int yy;
//FAWE end
double nextXn = 0; double nextXn = 0;
double nextYn, nextZn;
double nextXnSq, nextYnSq, nextZnSq;
double xn, yn, zn, dx, dy, dz;
double dxy, dxz, dyz, dxyz;
int xx, x_x, yy, zz, z_z;
forX: forX:
for (int x = 0; x <= ceilRadiusX; ++x) { for (int x = 0; x <= ceilRadiusX; ++x) {
final double xn = nextXn; xn = nextXn;
double dx = xn * xn; dx = xn * xn;
nextXn = (x + 1) * invRadiusX; nextXn = (x + 1) * invRadiusX;
double nextZn = 0; nextXnSq = nextXn * nextXn;
xx = px + x;
x_x = px - x;
nextZn = 0;
forZ: forZ:
for (int z = 0; z <= ceilRadiusZ; ++z) { for (int z = 0; z <= ceilRadiusZ; ++z) {
final double zn = nextZn; zn = nextZn;
double dz = zn * zn; dz = zn * zn;
double dxz = dx + dz; dxz = dx + dz;
nextZn = (z + 1) * invRadiusZ; nextZn = (z + 1) * invRadiusZ;
double nextYn = 0; nextZnSq = nextZn * nextZn;
zz = pz + z;
z_z = pz - z;
nextYn = 0;
forY: forY:
for (int y = 0; y <= ceilRadiusY; ++y) { for (int y = 0; y <= ceilRadiusY; ++y) {
final double yn = nextYn; yn = nextYn;
double dy = yn * yn; dy = yn * yn;
double dxyz = dxz + dy; dxyz = dxz + dy;
nextYn = (y + 1) * invRadiusY; nextYn = (y + 1) * invRadiusY;
if (dxyz > 1) { if (dxyz > 1) {
@ -2453,40 +2496,45 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
break forY; break forY;
} }
nextYnSq = nextYn * nextYn;
dxy = dx + dy;
dyz = dy + dz;
if (!filled) { if (!filled) {
if (nextXn * nextXn + dy + dz <= 1 && nextYn * nextYn + dx + dz <= 1 && nextZn * nextZn + dx + dy <= 1) { if (nextXnSq + dyz <= 1 && nextYnSq + dxz <= 1 && nextZnSq + dxy <= 1) {
continue; continue;
} }
} }
//FAWE start //FAWE start
yy = py + y; yy = py + y;
if (yy <= maxY) { if (yy <= maxY) {
this.setBlock(px + x, py + y, pz + z, block); this.setBlock(xx, yy, zz, block);
if (x != 0) { if (x != 0) {
this.setBlock(px - x, py + y, pz + z, block); this.setBlock(x_x, yy, zz, block);
} }
if (z != 0) { if (z != 0) {
this.setBlock(px + x, py + y, pz - z, block); this.setBlock(xx, yy, z_z, block);
if (x != 0) { if (x != 0) {
this.setBlock(px - x, py + y, pz - z, block); this.setBlock(x_x, yy, z_z, block);
} }
} }
} }
if (y != 0 && (yy = py - y) >= minY) { if (y != 0 && (yy = py - y) >= minY) {
this.setBlock(px + x, yy, pz + z, block); this.setBlock(xx, yy, zz, block);
if (x != 0) { if (x != 0) {
this.setBlock(px - x, yy, pz + z, block); this.setBlock(x_x, yy, zz, block);
} }
if (z != 0) { if (z != 0) {
this.setBlock(px + x, yy, pz - z, block); this.setBlock(xx, yy, z_z, block);
if (x != 0) { if (x != 0) {
this.setBlock(px - x, yy, pz - z, block); this.setBlock(x_x, yy, z_z, block);
} }
} }
} }
} }
} }
} }
//FAWE end
return changes; return changes;
//FAWE end //FAWE end
@ -2509,17 +2557,22 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
int bz = position.getZ(); int bz = position.getZ();
int height = size; int height = size;
int yy, xx, x_x, zz, z_z;
for (int y = 0; y <= height; ++y) { for (int y = 0; y <= height; ++y) {
size--; size--;
yy = y + by;
for (int x = 0; x <= size; ++x) { for (int x = 0; x <= size; ++x) {
xx = bx + x;
x_x = bx - x;
for (int z = 0; z <= size; ++z) { for (int z = 0; z <= size; ++z) {
zz = bz + z;
z_z = bz - z;
if ((filled && z <= size && x <= size) || z == size || x == size) { if ((filled && z <= size && x <= size) || z == size || x == size) {
setBlock(x + bx, y + by, z + bz, block); setBlock(xx, yy, zz, block);
setBlock(-x + bx, y + by, z + bz, block); setBlock(x_x, yy, zz, block);
setBlock(x + bx, y + by, -z + bz, block); setBlock(xx, yy, z_z, block);
setBlock(-x + bx, y + by, -z + bz, block); setBlock(x_x, yy, z_z, block);
} }
} }
} }
@ -3774,19 +3827,28 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
int radiusSqr = (int) (size * size); int radiusSqr = (int) (size * size);
int sizeInt = (int) size * 2; int sizeInt = (int) size * 2;
int xx, yy, zz;
double distance;
double noise;
if (sphericity == 1) { if (sphericity == 1) {
double nx, ny, nz;
double d1, d2;
for (int x = -sizeInt; x <= sizeInt; x++) { for (int x = -sizeInt; x <= sizeInt; x++) {
double nx = seedX + x * distort; nx = seedX + x * distort;
double d1 = x * x * modX; d1 = x * x * modX;
xx = px + x;
for (int y = -sizeInt; y <= sizeInt; y++) { for (int y = -sizeInt; y <= sizeInt; y++) {
double d2 = d1 + y * y * modY; d2 = d1 + y * y * modY;
double ny = seedY + y * distort; ny = seedY + y * distort;
yy = py + y;
for (int z = -sizeInt; z <= sizeInt; z++) { for (int z = -sizeInt; z <= sizeInt; z++) {
double nz = seedZ + z * distort; nz = seedZ + z * distort;
double distance = d2 + z * z * modZ; distance = d2 + z * z * modZ;
double noise = amplitude * SimplexNoise.noise(nx, ny, nz); zz = pz + z;
noise = amplitude * SimplexNoise.noise(nx, ny, nz);
if (distance + distance * noise < radiusSqr) { if (distance + distance * noise < radiusSqr) {
setBlock(px + x, py + y, pz + z, pattern); setBlock(xx, yy, zz, pattern);
} }
} }
} }
@ -3803,39 +3865,48 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
MutableVector3 mutable = new MutableVector3(); MutableVector3 mutable = new MutableVector3();
double roughness = 1 - sphericity; double roughness = 1 - sphericity;
int x;
int y;
int z;
double xScaled;
double yScaled;
double zScaled;
double manDist;
double distSqr;
for (int xr = -sizeInt; xr <= sizeInt; xr++) { for (int xr = -sizeInt; xr <= sizeInt; xr++) {
xx = px + xr;
for (int yr = -sizeInt; yr <= sizeInt; yr++) { for (int yr = -sizeInt; yr <= sizeInt; yr++) {
yy = py + yr;
for (int zr = -sizeInt; zr <= sizeInt; zr++) { for (int zr = -sizeInt; zr <= sizeInt; zr++) {
zz = pz + zr;
// pt == mutable as it's a MutableVector3 // pt == mutable as it's a MutableVector3
// so it must be set each time // so it must be set each time
mutable.mutX(xr); mutable.setComponents(xr, yr, zr);
mutable.mutY(yr);
mutable.mutZ(zr);
Vector3 pt = transform.apply(mutable); Vector3 pt = transform.apply(mutable);
int x = MathMan.roundInt(pt.getX()); x = MathMan.roundInt(pt.getX());
int y = MathMan.roundInt(pt.getY()); y = MathMan.roundInt(pt.getY());
int z = MathMan.roundInt(pt.getZ()); z = MathMan.roundInt(pt.getZ());
double xScaled = Math.abs(x) * modX; xScaled = Math.abs(x) * modX;
double yScaled = Math.abs(y) * modY; yScaled = Math.abs(y) * modY;
double zScaled = Math.abs(z) * modZ; zScaled = Math.abs(z) * modZ;
double manDist = xScaled + yScaled + zScaled; manDist = xScaled + yScaled + zScaled;
double distSqr = x * x * modX + z * z * modZ + y * y * modY; distSqr = x * x * modX + z * z * modZ + y * y * modY;
double distance = Math.sqrt(distSqr) * sphericity + MathMan.max( distance = Math.sqrt(distSqr) * sphericity + MathMan.max(
manDist, manDist,
xScaled * manScaleX, xScaled * manScaleX,
yScaled * manScaleY, yScaled * manScaleY,
zScaled * manScaleZ zScaled * manScaleZ
) * roughness; ) * roughness;
double noise = amplitude * SimplexNoise.noise( noise = amplitude * SimplexNoise.noise(
seedX + x * distort, seedX + x * distort,
seedZ + z * distort, seedZ + z * distort,
seedZ + z * distort seedZ + z * distort
); );
if (distance + distance * noise < r) { if (distance + distance * noise < r) {
setBlock(px + xr, py + yr, pz + zr, pattern); setBlock(xx, yy, zz, pattern);
} }
} }
} }