Move vectors to static creators, for caching

This commit is contained in:
Kenzie Togami
2018-10-19 13:13:32 -07:00
parent 399e0ad5fa
commit 2c8b2fe089
69 changed files with 366 additions and 334 deletions

View File

@ -55,6 +55,26 @@ public final class BlockVector2 {
.result();
};
public static BlockVector2 at(double x, double z) {
return at((int) Math.floor(x), (int) Math.floor(z));
}
public static BlockVector2 at(int x, int z) {
switch (x) {
case 0:
if (z == 0) {
return ZERO;
}
break;
case 1:
if (z == 1) {
return ONE;
}
break;
}
return new BlockVector2(x, z);
}
private final int x, z;
/**
@ -63,17 +83,7 @@ public final class BlockVector2 {
* @param x the X coordinate
* @param z the Z coordinate
*/
public BlockVector2(double x, double z) {
this((int) Math.floor(x), (int) Math.floor(z));
}
/**
* Construct an instance.
*
* @param x the X coordinate
* @param z the Z coordinate
*/
public BlockVector2(int x, int z) {
private BlockVector2(int x, int z) {
this.x = x;
this.z = z;
}
@ -103,7 +113,7 @@ public final class BlockVector2 {
* @return a new vector
*/
public BlockVector2 withX(int x) {
return new BlockVector2(x, z);
return BlockVector2.at(x, z);
}
/**
@ -131,7 +141,7 @@ public final class BlockVector2 {
* @return a new vector
*/
public BlockVector2 withZ(int z) {
return new BlockVector2(x, z);
return BlockVector2.at(x, z);
}
/**
@ -152,7 +162,7 @@ public final class BlockVector2 {
* @return a new vector
*/
public BlockVector2 add(int x, int z) {
return new BlockVector2(this.x + x, this.z + z);
return BlockVector2.at(this.x + x, this.z + z);
}
/**
@ -170,7 +180,7 @@ public final class BlockVector2 {
newZ += other.z;
}
return new BlockVector2(newX, newZ);
return BlockVector2.at(newX, newZ);
}
/**
@ -193,7 +203,7 @@ public final class BlockVector2 {
* @return a new vector
*/
public BlockVector2 subtract(int x, int z) {
return new BlockVector2(this.x - x, this.z - z);
return BlockVector2.at(this.x - x, this.z - z);
}
/**
@ -211,7 +221,7 @@ public final class BlockVector2 {
newZ -= other.z;
}
return new BlockVector2(newX, newZ);
return BlockVector2.at(newX, newZ);
}
/**
@ -232,7 +242,7 @@ public final class BlockVector2 {
* @return a new vector
*/
public BlockVector2 multiply(int x, int z) {
return new BlockVector2(this.x * x, this.z * z);
return BlockVector2.at(this.x * x, this.z * z);
}
/**
@ -249,7 +259,7 @@ public final class BlockVector2 {
newZ *= other.z;
}
return new BlockVector2(newX, newZ);
return BlockVector2.at(newX, newZ);
}
/**
@ -280,7 +290,7 @@ public final class BlockVector2 {
* @return a new vector
*/
public BlockVector2 divide(int x, int z) {
return new BlockVector2(this.x / x, this.z / z);
return BlockVector2.at(this.x / x, this.z / z);
}
/**
@ -343,7 +353,7 @@ public final class BlockVector2 {
double len = length();
double x = this.x / len;
double z = this.z / len;
return new BlockVector2(x, z);
return BlockVector2.at(x, z);
}
/**
@ -407,7 +417,7 @@ public final class BlockVector2 {
* @return a new vector
*/
public BlockVector2 abs() {
return new BlockVector2(Math.abs(x), Math.abs(z));
return BlockVector2.at(Math.abs(x), Math.abs(z));
}
/**
@ -429,7 +439,7 @@ public final class BlockVector2 {
double sin = Math.sin(angle);
double x2 = x * cos - z * sin;
double z2 = x * sin + z * cos;
return new BlockVector2(
return BlockVector2.at(
x2 + aboutX + translateX,
z2 + aboutZ + translateZ);
}
@ -461,7 +471,7 @@ public final class BlockVector2 {
}
public Vector2 toVector2() {
return new Vector2(x, z);
return Vector2.at(x, z);
}
/**
@ -480,7 +490,7 @@ public final class BlockVector2 {
* @return a new vector
*/
public Vector3 toVector3(double y) {
return new Vector3(x, y, z);
return Vector3.at(x, y, z);
}
/**
@ -499,7 +509,7 @@ public final class BlockVector2 {
* @return a new vector
*/
public BlockVector3 toBlockVector3(int y) {
return new BlockVector3(x, y, z);
return BlockVector3.at(x, y, z);
}
@Override

View File

@ -37,6 +37,28 @@ public final class BlockVector3 {
public static final BlockVector3 UNIT_Z = new BlockVector3(0, 0, 1);
public static final BlockVector3 ONE = new BlockVector3(1, 1, 1);
public static BlockVector3 at(double x, double y, double z) {
return at((int) Math.floor(x), (int) Math.floor(y), (int) Math.floor(z));
}
public static BlockVector3 at(int x, int y, int z) {
// switch for efficiency on typical cases
// in MC y is rarely 0/1 on selections
switch (y) {
case 0:
if (x == 0 && z == 0) {
return ZERO;
}
break;
case 1:
if (x == 1 && z == 1) {
return ONE;
}
break;
}
return new BlockVector3(x, y, z);
}
// thread-safe initialization idiom
private static final class YzxOrderComparator {
private static final Comparator<BlockVector3> YZX_ORDER = (a, b) -> {
@ -67,18 +89,7 @@ public final class BlockVector3 {
* @param y the Y coordinate
* @param z the Z coordinate
*/
public BlockVector3(double x, double y, double z) {
this((int) Math.floor(x), (int) Math.floor(y), (int) Math.floor(z));
}
/**
* Construct an instance.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
*/
public BlockVector3(int x, int y, int z) {
private BlockVector3(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
@ -109,7 +120,7 @@ public final class BlockVector3 {
* @return a new vector
*/
public BlockVector3 withX(int x) {
return new BlockVector3(x, y, z);
return BlockVector3.at(x, y, z);
}
/**
@ -137,7 +148,7 @@ public final class BlockVector3 {
* @return a new vector
*/
public BlockVector3 withY(int y) {
return new BlockVector3(x, y, z);
return BlockVector3.at(x, y, z);
}
/**
@ -165,7 +176,7 @@ public final class BlockVector3 {
* @return a new vector
*/
public BlockVector3 withZ(int z) {
return new BlockVector3(x, y, z);
return BlockVector3.at(x, y, z);
}
/**
@ -187,7 +198,7 @@ public final class BlockVector3 {
* @return a new vector
*/
public BlockVector3 add(int x, int y, int z) {
return new BlockVector3(this.x + x, this.y + y, this.z + z);
return BlockVector3.at(this.x + x, this.y + y, this.z + z);
}
/**
@ -206,7 +217,7 @@ public final class BlockVector3 {
newZ += other.z;
}
return new BlockVector3(newX, newY, newZ);
return BlockVector3.at(newX, newY, newZ);
}
/**
@ -230,7 +241,7 @@ public final class BlockVector3 {
* @return a new vector
*/
public BlockVector3 subtract(int x, int y, int z) {
return new BlockVector3(this.x - x, this.y - y, this.z - z);
return BlockVector3.at(this.x - x, this.y - y, this.z - z);
}
/**
@ -249,7 +260,7 @@ public final class BlockVector3 {
newZ -= other.z;
}
return new BlockVector3(newX, newY, newZ);
return BlockVector3.at(newX, newY, newZ);
}
/**
@ -271,7 +282,7 @@ public final class BlockVector3 {
* @return a new vector
*/
public BlockVector3 multiply(int x, int y, int z) {
return new BlockVector3(this.x * x, this.y * y, this.z * z);
return BlockVector3.at(this.x * x, this.y * y, this.z * z);
}
/**
@ -289,7 +300,7 @@ public final class BlockVector3 {
newZ *= other.z;
}
return new BlockVector3(newX, newY, newZ);
return BlockVector3.at(newX, newY, newZ);
}
/**
@ -321,7 +332,7 @@ public final class BlockVector3 {
* @return a new vector
*/
public BlockVector3 divide(int x, int y, int z) {
return new BlockVector3(this.x / x, this.y / y, this.z / z);
return BlockVector3.at(this.x / x, this.y / y, this.z / z);
}
/**
@ -386,7 +397,7 @@ public final class BlockVector3 {
double x = this.x / len;
double y = this.y / len;
double z = this.z / len;
return new BlockVector3(x, y, z);
return BlockVector3.at(x, y, z);
}
/**
@ -434,10 +445,10 @@ public final class BlockVector3 {
public BlockVector3 clampY(int min, int max) {
checkArgument(min <= max, "minimum cannot be greater than maximum");
if (y < min) {
return new BlockVector3(x, min, z);
return BlockVector3.at(x, min, z);
}
if (y > max) {
return new BlockVector3(x, max, z);
return BlockVector3.at(x, max, z);
}
return this;
}
@ -481,7 +492,7 @@ public final class BlockVector3 {
* @return a new vector
*/
public BlockVector3 abs() {
return new BlockVector3(Math.abs(x), Math.abs(y), Math.abs(z));
return BlockVector3.at(Math.abs(x), Math.abs(y), Math.abs(z));
}
/**
@ -504,7 +515,7 @@ public final class BlockVector3 {
double x2 = x * cos - z * sin;
double z2 = x * sin + z * cos;
return new BlockVector3(
return BlockVector3.at(
x2 + aboutX + translateX,
y,
z2 + aboutZ + translateZ
@ -579,11 +590,11 @@ public final class BlockVector3 {
* @return a new {@link BlockVector2}
*/
public BlockVector2 toBlockVector2() {
return new BlockVector2(x, z);
return BlockVector2.at(x, z);
}
public Vector3 toVector3() {
return new Vector3(x, y, z);
return Vector3.at(x, y, z);
}
@Override

View File

@ -25,12 +25,29 @@ import com.sk89q.worldedit.math.transform.AffineTransform;
* An immutable 2-dimensional vector.
*/
public final class Vector2 {
public static final Vector2 ZERO = new Vector2(0, 0);
public static final Vector2 UNIT_X = new Vector2(1, 0);
public static final Vector2 UNIT_Z = new Vector2(0, 1);
public static final Vector2 ONE = new Vector2(1, 1);
public static Vector2 at(double x, double z) {
int xTrunc = (int) x;
switch (xTrunc) {
case 0:
if (x == 0 && z == 0) {
return ZERO;
}
break;
case 1:
if (x == 1 && z == 1) {
return ONE;
}
break;
}
return new Vector2(x, z);
}
private final double x, z;
/**
@ -39,21 +56,11 @@ public final class Vector2 {
* @param x the X coordinate
* @param z the Z coordinate
*/
public Vector2(double x, double z) {
private Vector2(double x, double z) {
this.x = x;
this.z = z;
}
/**
* Copy another vector.
*
* @param other the other vector
*/
public Vector2(Vector2 other) {
this.x = other.x;
this.z = other.z;
}
/**
* Get the X coordinate.
*
@ -70,7 +77,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 withX(double x) {
return new Vector2(x, z);
return Vector2.at(x, z);
}
/**
@ -89,7 +96,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 withZ(double z) {
return new Vector2(x, z);
return Vector2.at(x, z);
}
/**
@ -110,7 +117,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 add(double x, double z) {
return new Vector2(this.x + x, this.z + z);
return Vector2.at(this.x + x, this.z + z);
}
/**
@ -128,7 +135,7 @@ public final class Vector2 {
newZ += other.z;
}
return new Vector2(newX, newZ);
return Vector2.at(newX, newZ);
}
/**
@ -151,7 +158,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 subtract(double x, double z) {
return new Vector2(this.x - x, this.z - z);
return Vector2.at(this.x - x, this.z - z);
}
/**
@ -169,7 +176,7 @@ public final class Vector2 {
newZ -= other.z;
}
return new Vector2(newX, newZ);
return Vector2.at(newX, newZ);
}
/**
@ -190,7 +197,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 multiply(double x, double z) {
return new Vector2(this.x * x, this.z * z);
return Vector2.at(this.x * x, this.z * z);
}
/**
@ -207,7 +214,7 @@ public final class Vector2 {
newZ *= other.z;
}
return new Vector2(newX, newZ);
return Vector2.at(newX, newZ);
}
/**
@ -238,7 +245,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 divide(double x, double z) {
return new Vector2(this.x / x, this.z / z);
return Vector2.at(this.x / x, this.z / z);
}
/**
@ -329,7 +336,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 floor() {
return new Vector2(Math.floor(x), Math.floor(z));
return Vector2.at(Math.floor(x), Math.floor(z));
}
/**
@ -338,7 +345,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 ceil() {
return new Vector2(Math.ceil(x), Math.ceil(z));
return Vector2.at(Math.ceil(x), Math.ceil(z));
}
/**
@ -349,7 +356,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 round() {
return new Vector2(Math.floor(x + 0.5), Math.floor(z + 0.5));
return Vector2.at(Math.floor(x + 0.5), Math.floor(z + 0.5));
}
/**
@ -359,7 +366,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector2 abs() {
return new Vector2(Math.abs(x), Math.abs(z));
return Vector2.at(Math.abs(x), Math.abs(z));
}
/**
@ -413,7 +420,7 @@ public final class Vector2 {
}
public static BlockVector2 toBlockPoint(double x, double z) {
return new BlockVector2(x, z);
return BlockVector2.at(x, z);
}
/**
@ -441,7 +448,7 @@ public final class Vector2 {
* @return a new vector
*/
public Vector3 toVector3(double y) {
return new Vector3(x, y, z);
return Vector3.at(x, y, z);
}
@Override

View File

@ -37,6 +37,25 @@ public final class Vector3 {
public static final Vector3 UNIT_Z = new Vector3(0, 0, 1);
public static final Vector3 ONE = new Vector3(1, 1, 1);
public static Vector3 at(double x, double y, double z) {
// switch for efficiency on typical cases
// in MC y is rarely 0/1 on selections
int yTrunc = (int) y;
switch (yTrunc) {
case 0:
if (x == 0 && y == 0 && z == 0) {
return ZERO;
}
break;
case 1:
if (x == 1 && y == 1 && z == 1) {
return ONE;
}
break;
}
return new Vector3(x, y, z);
}
// thread-safe initialization idiom
private static final class YzxOrderComparator {
private static final Comparator<Vector3> YZX_ORDER = (a, b) -> {
@ -67,23 +86,12 @@ public final class Vector3 {
* @param y the Y coordinate
* @param z the Z coordinate
*/
public Vector3(double x, double y, double z) {
private Vector3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Copy another vector.
*
* @param other another vector to make a copy of
*/
public Vector3(Vector3 other) {
this.x = other.x;
this.y = other.y;
this.z = other.z;
}
/**
* Get the X coordinate.
*
@ -100,7 +108,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 withX(double x) {
return new Vector3(x, y, z);
return Vector3.at(x, y, z);
}
/**
@ -119,7 +127,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 withY(double y) {
return new Vector3(x, y, z);
return Vector3.at(x, y, z);
}
/**
@ -138,7 +146,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 withZ(double z) {
return new Vector3(x, y, z);
return Vector3.at(x, y, z);
}
/**
@ -160,7 +168,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 add(double x, double y, double z) {
return new Vector3(this.x + x, this.y + y, this.z + z);
return Vector3.at(this.x + x, this.y + y, this.z + z);
}
/**
@ -179,7 +187,7 @@ public final class Vector3 {
newZ += other.z;
}
return new Vector3(newX, newY, newZ);
return Vector3.at(newX, newY, newZ);
}
/**
@ -203,7 +211,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 subtract(double x, double y, double z) {
return new Vector3(this.x - x, this.y - y, this.z - z);
return Vector3.at(this.x - x, this.y - y, this.z - z);
}
/**
@ -222,7 +230,7 @@ public final class Vector3 {
newZ -= other.z;
}
return new Vector3(newX, newY, newZ);
return Vector3.at(newX, newY, newZ);
}
/**
@ -244,7 +252,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 multiply(double x, double y, double z) {
return new Vector3(this.x * x, this.y * y, this.z * z);
return Vector3.at(this.x * x, this.y * y, this.z * z);
}
/**
@ -262,7 +270,7 @@ public final class Vector3 {
newZ *= other.z;
}
return new Vector3(newX, newY, newZ);
return Vector3.at(newX, newY, newZ);
}
/**
@ -294,7 +302,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 divide(double x, double y, double z) {
return new Vector3(this.x / x, this.y / y, this.z / z);
return Vector3.at(this.x / x, this.y / y, this.z / z);
}
/**
@ -403,10 +411,10 @@ public final class Vector3 {
public Vector3 clampY(int min, int max) {
checkArgument(min <= max, "minimum cannot be greater than maximum");
if (y < min) {
return new Vector3(x, min, z);
return Vector3.at(x, min, z);
}
if (y > max) {
return new Vector3(x, max, z);
return Vector3.at(x, max, z);
}
return this;
}
@ -417,7 +425,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 floor() {
return new Vector3(Math.floor(x), Math.floor(y), Math.floor(z));
return Vector3.at(Math.floor(x), Math.floor(y), Math.floor(z));
}
/**
@ -426,7 +434,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 ceil() {
return new Vector3(Math.ceil(x), Math.ceil(y), Math.ceil(z));
return Vector3.at(Math.ceil(x), Math.ceil(y), Math.ceil(z));
}
/**
@ -437,7 +445,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 round() {
return new Vector3(Math.floor(x + 0.5), Math.floor(y + 0.5), Math.floor(z + 0.5));
return Vector3.at(Math.floor(x + 0.5), Math.floor(y + 0.5), Math.floor(z + 0.5));
}
/**
@ -447,7 +455,7 @@ public final class Vector3 {
* @return a new vector
*/
public Vector3 abs() {
return new Vector3(Math.abs(x), Math.abs(y), Math.abs(z));
return Vector3.at(Math.abs(x), Math.abs(y), Math.abs(z));
}
/**
@ -548,7 +556,7 @@ public final class Vector3 {
* @return a new {@code BlockVector}
*/
public static BlockVector3 toBlockPoint(double x, double y, double z) {
return new BlockVector3(x, y, z);
return BlockVector3.at(x, y, z);
}
/**
@ -566,7 +574,7 @@ public final class Vector3 {
* @return a new {@link Vector2}
*/
public Vector2 toVector2() {
return new Vector2(x, z);
return Vector2.at(x, z);
}
@Override

View File

@ -134,17 +134,17 @@ public class HeightMap {
// Depending on growing or shrinking we need to start at the bottom or top
if (newHeight > curHeight) {
// Set the top block of the column to be the same type (this might go wrong with rounding)
BlockState existing = session.getBlock(new BlockVector3(xr, curHeight, zr));
BlockState existing = session.getBlock(BlockVector3.at(xr, curHeight, zr));
// Skip water/lava
if (existing.getBlockType() != BlockTypes.WATER && existing.getBlockType() != BlockTypes.LAVA) {
session.setBlock(new BlockVector3(xr, newHeight, zr), existing);
session.setBlock(BlockVector3.at(xr, newHeight, zr), existing);
++blocksChanged;
// Grow -- start from 1 below top replacing airblocks
for (int y = newHeight - 1 - originY; y >= 0; --y) {
int copyFrom = (int) (y * scale);
session.setBlock(new BlockVector3(xr, originY + y, zr), session.getBlock(new BlockVector3(xr, originY + copyFrom, zr)));
session.setBlock(BlockVector3.at(xr, originY + y, zr), session.getBlock(BlockVector3.at(xr, originY + copyFrom, zr)));
++blocksChanged;
}
}
@ -152,18 +152,18 @@ public class HeightMap {
// Shrink -- start from bottom
for (int y = 0; y < newHeight - originY; ++y) {
int copyFrom = (int) (y * scale);
session.setBlock(new BlockVector3(xr, originY + y, zr), session.getBlock(new BlockVector3(xr, originY + copyFrom, zr)));
session.setBlock(BlockVector3.at(xr, originY + y, zr), session.getBlock(BlockVector3.at(xr, originY + copyFrom, zr)));
++blocksChanged;
}
// Set the top block of the column to be the same type
// (this could otherwise go wrong with rounding)
session.setBlock(new BlockVector3(xr, newHeight, zr), session.getBlock(new BlockVector3(xr, curHeight, zr)));
session.setBlock(BlockVector3.at(xr, newHeight, zr), session.getBlock(BlockVector3.at(xr, curHeight, zr)));
++blocksChanged;
// Fill rest with air
for (int y = newHeight + 1; y <= curHeight; ++y) {
session.setBlock(new BlockVector3(xr, y, zr), fillerAir);
session.setBlock(BlockVector3.at(xr, y, zr), fillerAir);
++blocksChanged;
}
}

View File

@ -53,7 +53,7 @@ public final class Polygons {
final List<BlockVector2> points = new ArrayList<>(nPoints);
for (int i = 0; i < nPoints; ++i) {
double angle = i * (2.0 * Math.PI) / nPoints;
final Vector2 pos = new Vector2(Math.cos(angle), Math.sin(angle));
final Vector2 pos = Vector2.at(Math.cos(angle), Math.sin(angle));
final BlockVector2 blockVector2D = pos.multiply(radius).toBlockPoint().add(center);
points.add(blockVector2D);
}

View File

@ -38,7 +38,7 @@ public class Node {
private double continuity;
public Node() {
this(new Vector3(0, 0, 0));
this(Vector3.at(0, 0, 0));
}
public Node(Node other) {

View File

@ -293,7 +293,7 @@ public class AffineTransform implements Transform {
@Override
public Vector3 apply(Vector3 vector) {
return new Vector3(
return Vector3.at(
vector.getX() * m00 + vector.getY() * m01 + vector.getZ() * m02 + m03,
vector.getX() * m10 + vector.getY() * m11 + vector.getZ() * m12 + m13,
vector.getX() * m20 + vector.getY() * m21 + vector.getZ() * m22 + m23);