minor tweak for mutable vectors

This commit is contained in:
Jesse Boyd
2019-04-01 03:09:20 +11:00
parent 2165bb127f
commit 92a7bd5e44
120 changed files with 923 additions and 2426 deletions

View File

@ -60,22 +60,12 @@ public class BlockVector2 {
}
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;
protected int x, z;
protected BlockVector2(){}
/**
* Construct an instance.
@ -83,11 +73,31 @@ public class BlockVector2 {
* @param x the X coordinate
* @param z the Z coordinate
*/
private BlockVector2(int x, int z) {
protected BlockVector2(int x, int z) {
this.x = x;
this.z = z;
}
public MutableBlockVector2 setComponents(int x, int z) {
return new MutableBlockVector2(x, z);
}
public MutableBlockVector2 mutX(double x) {
return new MutableBlockVector2((int) x, z);
}
public MutableBlockVector2 mutZ(double z) {
return new MutableBlockVector2(x, (int) z);
}
public MutableBlockVector2 mutX(int x) {
return new MutableBlockVector2(x, z);
}
public MutableBlockVector2 mutZ(int z) {
return new MutableBlockVector2(x, z);
}
/**
* Get the X coordinate.
*
@ -144,6 +154,35 @@ public class BlockVector2 {
return BlockVector2.at(x, z);
}
public MutableBlockVector2 nextPosition() {
int absX = Math.abs(x);
int absY = Math.abs(z);
if (absX > absY) {
if (x > 0) {
return setComponents(x, z + 1);
} else {
return setComponents(x, z - 1);
}
} else if (absY > absX) {
if (z > 0) {
return setComponents(x - 1, z);
} else {
return setComponents(x + 1, z);
}
} else {
if (x == z && x > 0) {
return setComponents(x, z + 1);
}
if (x == absX) {
return setComponents(x, z + 1);
}
if (z == absY) {
return setComponents(x, z - 1);
}
return setComponents(x + 1, z);
}
}
/**
* Add another vector to this vector and return the result as a new vector.
*

View File

@ -42,20 +42,6 @@ public class BlockVector3 {
}
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);
}
@ -80,7 +66,9 @@ public class BlockVector3 {
return YzxOrderComparator.YZX_ORDER;
}
private final int x, y, z;
protected int x, y, z;
protected BlockVector3(){}
/**
* Construct an instance.
@ -89,12 +77,44 @@ public class BlockVector3 {
* @param y the Y coordinate
* @param z the Z coordinate
*/
private BlockVector3(int x, int y, int z) {
protected BlockVector3(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public MutableBlockVector3 setComponents(double x, double y, double z) {
return new MutableBlockVector3((int) x, (int) y, (int) z);
}
public MutableBlockVector3 setComponents(int x, int y, int z) {
return new MutableBlockVector3(x, y, z);
}
public MutableBlockVector3 mutX(double x) {
return new MutableBlockVector3((int) x, y, z);
}
public MutableBlockVector3 mutY(double y) {
return new MutableBlockVector3(x, (int) y, z);
}
public MutableBlockVector3 mutZ(double z) {
return new MutableBlockVector3(x, y, (int) z);
}
public MutableBlockVector3 mutX(int x) {
return new MutableBlockVector3(x, y, z);
}
public MutableBlockVector3 mutY(int y) {
return new MutableBlockVector3(x, y, z);
}
public MutableBlockVector3 mutZ(int z) {
return new MutableBlockVector3(x, y, z);
}
/**
* Get the X coordinate.
*

View File

@ -1,629 +0,0 @@
package com.sk89q.worldedit.math;
import com.boydti.fawe.util.MathMan;
import com.google.common.collect.ComparisonChain;
import com.sk89q.worldedit.math.transform.AffineTransform;
import static com.google.common.base.Preconditions.checkArgument;
import java.io.IOException;
import java.io.Serializable;
import java.util.Comparator;
/**
* A mutable rendition of WorldEdit's BlockVector3 class.
* This class should ONLY be used locally for efficient vector manipulation.
*/
public class MutableBlockVector implements Serializable {
private transient int x, y, z;
private static ThreadLocal<MutableBlockVector> MUTABLE_CACHE = new ThreadLocal<MutableBlockVector>() {
@Override
protected MutableBlockVector initialValue() {
return new MutableBlockVector();
}
};
public static MutableBlockVector get(int x, int y, int z) {
return MUTABLE_CACHE.get().setComponents(x, y, z);
}
public MutableBlockVector(BlockVector3 v) {
this(v.getBlockX(), v.getBlockY(), v.getBlockZ());
}
public MutableBlockVector(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public MutableBlockVector() {
this(0, 0, 0);
}
public static final MutableBlockVector ZERO = new MutableBlockVector(0, 0, 0);
public static final MutableBlockVector UNIT_X = new MutableBlockVector(1, 0, 0);
public static final MutableBlockVector UNIT_Y = new MutableBlockVector(0, 1, 0);
public static final MutableBlockVector UNIT_Z = new MutableBlockVector(0, 0, 1);
public static final MutableBlockVector ONE = new MutableBlockVector(1, 1, 1);
// thread-safe initialization idiom
private static final class YzxOrderComparator {
private static final Comparator<MutableBlockVector> YZX_ORDER = (a, b) -> {
return ComparisonChain.start()
.compare(a.y, b.y)
.compare(a.z, b.z)
.compare(a.x, b.x)
.result();
};
}
/**
* Returns a comparator that sorts vectors first by Y, then Z, then X.
*
* <p>
* Useful for sorting by chunk block storage order.
*/
public static Comparator<MutableBlockVector> sortByCoordsYzx() {
return YzxOrderComparator.YZX_ORDER;
}
public MutableBlockVector setComponents(BlockVector3 other) {
return setComponents(other.getBlockX(), other.getBlockY(), other.getBlockZ());
}
public MutableBlockVector setComponents(double x, double y, double z) {
return this.setComponents((int) x, (int) y, (int) z);
}
public MutableBlockVector setComponents(int x, int y, int z) {
this.mutX(x);
this.mutY(y);
this.mutZ(z);
return this;
}
public final void mutX(double x) {
this.x = MathMan.roundInt(x);
}
public final void mutY(double y) {
this.y = MathMan.roundInt(y);
}
public final void mutZ(double z) {
this.z = MathMan.roundInt(z);
}
public final void mutX(int x) {
this.x = x;
}
public final void mutY(int y) {
this.y = y;
}
public final void mutZ(int z) {
this.z = z;
}
/**
* Get the X coordinate.
*
* @return the x coordinate
*/
public int getX() {
return x;
}
/**
* Get the X coordinate.
*
* @return the x coordinate
*/
public int getBlockX() {
return x;
}
/**
* Get the Y coordinate.
*
* @return the y coordinate
*/
public int getY() {
return y;
}
/**
* Get the Y coordinate.
*
* @return the y coordinate
*/
public int getBlockY() {
return y;
}
/**
* Get the Z coordinate.
*
* @return the z coordinate
*/
public int getZ() {
return z;
}
/**
* Get the Z coordinate.
*
* @return the z coordinate
*/
public int getBlockZ() {
return z;
}
/**
* Add another vector to this vector.
*
* @param other the other vector
* @return a new vector
*/
public MutableBlockVector add(MutableBlockVector other) {
return add(other.x, other.y, other.z);
}
/**
* Add another vector to this vector.
*
* @param x the value to add
* @param y the value to add
* @param z the value to add
* @return a new vector
*/
public MutableBlockVector add(int x, int y, int z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
/**
* Add a list of vectors to this vector and return the
* result as a new vector.
*
* @param others an array of vectors
* @return a new vector
*/
public MutableBlockVector add(MutableBlockVector... others) {
for (MutableBlockVector other : others) {
x += other.x;
y += other.y;
z += other.z;
}
return this;
}
/**
* Subtract another vector from this vector and return the result
* as a new vector.
*
* @param other the other vector
* @return a new vector
*/
public MutableBlockVector subtract(MutableBlockVector other) {
return subtract(other.x, other.y, other.z);
}
/**
* Subtract another vector from this vector and return the result
* as a new vector.
*
* @param x the value to subtract
* @param y the value to subtract
* @param z the value to subtract
* @return a new vector
*/
public MutableBlockVector subtract(int x, int y, int z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
/**
* Subtract a list of vectors from this vector and return the result
* as a new vector.
*
* @param others an array of vectors
* @return a new vector
*/
public MutableBlockVector subtract(MutableBlockVector... others) {
for (MutableBlockVector other : others) {
x -= other.x;
y -= other.y;
z -= other.z;
}
return this;
}
/**
* Multiply this vector by another vector on each component.
*
* @param other the other vector
* @return a new vector
*/
public MutableBlockVector multiply(MutableBlockVector other) {
return multiply(other.x, other.y, other.z);
}
/**
* Multiply this vector by another vector on each component.
*
* @param x the value to multiply
* @param y the value to multiply
* @param z the value to multiply
* @return a new vector
*/
public MutableBlockVector multiply(int x, int y, int z) {
this.x *= x;
this.y *= y;
this.z *= z;
return this;
}
/**
* Multiply this vector by zero or more vectors on each component.
*
* @param others an array of vectors
* @return a new vector
*/
public MutableBlockVector multiply(MutableBlockVector... others) {
for (MutableBlockVector other : others) {
x *= other.x;
y *= other.y;
z *= other.z;
}
return this;
}
/**
* Perform scalar multiplication and return a new vector.
*
* @param n the value to multiply
* @return a new vector
*/
public MutableBlockVector multiply(int n) {
return multiply(n, n, n);
}
/**
* Divide this vector by another vector on each component.
*
* @param other the other vector
* @return a new vector
*/
public MutableBlockVector divide(MutableBlockVector other) {
return divide(other.x, other.y, other.z);
}
/**
* Divide this vector by another vector on each component.
*
* @param x the value to divide by
* @param y the value to divide by
* @param z the value to divide by
* @return a new vector
*/
public MutableBlockVector divide(int x, int y, int z) {
this.x /= x;
this.y /= y;
this.z /= z;
return this;
}
/**
* Perform scalar division and return a new vector.
*
* @param n the value to divide by
* @return a new vector
*/
public MutableBlockVector divide(int n) {
return divide(n, n, n);
}
/**
* Get the length of the vector.
*
* @return length
*/
public double length() {
return Math.sqrt(lengthSq());
}
/**
* Get the length, squared, of the vector.
*
* @return length, squared
*/
public int lengthSq() {
return x * x + y * y + z * z;
}
/**
* Get the distance between this vector and another vector.
*
* @param other the other vector
* @return distance
*/
public double distance(MutableBlockVector other) {
return Math.sqrt(distanceSq(other));
}
/**
* Get the distance between this vector and another vector, squared.
*
* @param other the other vector
* @return distance
*/
public int distanceSq(MutableBlockVector other) {
int dx = other.x - x;
int dy = other.y - y;
int dz = other.z - z;
return dx * dx + dy * dy + dz * dz;
}
/**
* Get the normalized vector, which is the vector divided by its
* length, as a new vector.
*
* @return a new vector
*/
public MutableBlockVector normalize() {
double len = length();
x /= len;
y /= len;
z /= len;
return this;
}
/**
* Gets the dot product of this and another vector.
*
* @param other the other vector
* @return the dot product of this and the other vector
*/
public double dot(MutableBlockVector other) {
return x * other.x + y * other.y + z * other.z;
}
/**
* Gets the cross product of this and another vector.
*
* @param other the other vector
* @return the cross product of this and the other vector
*/
public MutableBlockVector cross(MutableBlockVector other) {
x = y * other.z - z * other.y;
y = z * other.x - x * other.z;
z = x * other.y - y * other.x;
return this;
}
/**
* Checks to see if a vector is contained with another.
*
* @param min the minimum point (X, Y, and Z are the lowest)
* @param max the maximum point (X, Y, and Z are the lowest)
* @return true if the vector is contained
*/
public boolean containedWithin(MutableBlockVector min, MutableBlockVector max) {
return x >= min.x && x <= max.x && y >= min.y && y <= max.y && z >= min.z && z <= max.z;
}
/**
* Clamp the Y component.
*
* @param min the minimum value
* @param max the maximum value
* @return a new vector
*/
public MutableBlockVector clampY(int min, int max) {
checkArgument(min <= max, "minimum cannot be greater than maximum");
if (y < min) {
y = min;
}
if (y > max) {
y = max;
}
return this;
}
/**
* Floors the values of all components.
*
* @return a new vector
*/
public MutableBlockVector floor() {
// already floored, kept for feature parity with Vector3
return this;
}
/**
* Rounds all components up.
*
* @return a new vector
*/
public MutableBlockVector ceil() {
// already raised, kept for feature parity with Vector3
return this;
}
/**
* Rounds all components to the closest integer.
*
* <p>Components &lt; 0.5 are rounded down, otherwise up.</p>
*
* @return a new vector
*/
public MutableBlockVector round() {
// already rounded, kept for feature parity with Vector3
return this;
}
/**
* Returns a vector with the absolute values of the components of
* this vector.
*
* @return a new vector
*/
public MutableBlockVector abs() {
x = Math.abs(x);
y = Math.abs(y);
z = Math.abs(z);
return this;
}
/**
* Perform a 2D transformation on this vector and return a new one.
*
* @param angle in degrees
* @param aboutX about which x coordinate to rotate
* @param aboutZ about which z coordinate to rotate
* @param translateX what to add after rotation
* @param translateZ what to add after rotation
* @return a new vector
* @see AffineTransform another method to transform vectors
*/
public MutableBlockVector transform2D(double angle, double aboutX, double aboutZ, double translateX, double translateZ) {
angle = Math.toRadians(angle);
double x = this.x - aboutX;
double z = this.z - aboutZ;
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double x2 = x * cos - z * sin;
double z2 = x * sin + z * cos;
this.x = (int) Math.floor(x2 + aboutX + translateX);
this.z = (int) Math.floor(z2 + aboutZ + translateZ);
return this;
}
/**
* Get this vector's pitch as used within the game.
*
* @return pitch in radians
*/
public double toPitch() {
double x = getX();
double z = getZ();
if (x == 0 && z == 0) {
return getY() > 0 ? -90 : 90;
} else {
double x2 = x * x;
double z2 = z * z;
double xz = Math.sqrt(x2 + z2);
return Math.toDegrees(Math.atan(-getY() / xz));
}
}
/**
* Get this vector's yaw as used within the game.
*
* @return yaw in radians
*/
public double toYaw() {
double x = getX();
double z = getZ();
double t = Math.atan2(-x, z);
double tau = 2 * Math.PI;
return Math.toDegrees(((t + tau) % tau));
}
/**
* Gets the minimum components of two vectors.
*
* @param v2 the second vector
* @return minimum
*/
public MutableBlockVector getMinimum(MutableBlockVector v2) {
x = Math.min(x, v2.x);
y = Math.min(y, v2.y);
z = Math.min(z, v2.z);
return this;
}
/**
* Gets the maximum components of two vectors.
*
* @param v2 the second vector
* @return maximum
*/
public MutableBlockVector getMaximum(MutableBlockVector v2) {
x = Math.max(x, v2.x);
y = Math.max(y, v2.y);
z = Math.max(z, v2.z);
return this;
}
/**
* Creates a 2D vector by dropping the Y component from this vector.
*
* @return a new {@link BlockVector2}
*/
public BlockVector2 toBlockVector2() {
return BlockVector2.at(x, z);
}
public Vector3 toVector3() {
return Vector3.at(x, y, z);
}
public BlockVector3 toBlockVector3() {
return BlockVector3.at(x, y, z);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof MutableBlockVector)) {
return false;
}
MutableBlockVector other = (MutableBlockVector) obj;
return other.x == this.x && other.y == this.y && other.z == this.z;
}
@Override
public int hashCode() {
int hash = 17;
hash = 31 * hash + Integer.hashCode(x);
hash = 31 * hash + Integer.hashCode(y);
hash = 31 * hash + Integer.hashCode(z);
return hash;
}
@Override
public String toString() {
return "Mutable (" + x + ", " + y + ", " + z + ")";
}
private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
stream.writeInt(x);
stream.writeByte((byte) y);
stream.writeInt(z);
}
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
this.x = stream.readInt();
this.y = stream.readByte() & 0xFF;
this.z = stream.readInt();
}
}

View File

@ -0,0 +1,52 @@
package com.sk89q.worldedit.math;
public class MutableBlockVector2 extends BlockVector2 {
private static ThreadLocal<MutableBlockVector2> MUTABLE_CACHE = new ThreadLocal<MutableBlockVector2>() {
@Override
protected MutableBlockVector2 initialValue() {
return new MutableBlockVector2();
}
};
public static MutableBlockVector2 get(int x, int z) {
return MUTABLE_CACHE.get().setComponents(x, z);
}
public MutableBlockVector2() {}
public MutableBlockVector2(int x, int z) {
super(x, z);
}
@Override
public MutableBlockVector2 setComponents(int x, int z) {
this.x = x;
this.z = z;
return this;
}
@Override
public MutableBlockVector2 mutX(double x) {
this.x = (int) x;
return this;
}
@Override
public MutableBlockVector2 mutZ(double z) {
this.z = (int) z;
return this;
}
@Override
public MutableBlockVector2 mutX(int x) {
this.x = x;
return this;
}
@Override
public MutableBlockVector2 mutZ(int z) {
this.z = z;
return this;
}
}

View File

@ -1,594 +0,0 @@
package com.sk89q.worldedit.math;
import java.io.IOException;
import java.io.Serializable;
import java.util.Comparator;
import com.google.common.collect.ComparisonChain;
import com.sk89q.worldedit.math.transform.AffineTransform;
/**
* A mutable rendition of WorldEdit's BlockVector2 class.
* This class should ONLY be used locally for efficient vector manipulation.
*/
public final class MutableBlockVector2D implements Serializable {
private static ThreadLocal<MutableBlockVector2D> MUTABLE_CACHE = new ThreadLocal<MutableBlockVector2D>() {
@Override
protected MutableBlockVector2D initialValue() {
return new MutableBlockVector2D();
}
};
public static MutableBlockVector2D get(int x, int z) {
return MUTABLE_CACHE.get().setComponents(x, z);
}
private transient int x, z;
public MutableBlockVector2D() {
this(0, 0);
}
public static final MutableBlockVector2D ZERO = new MutableBlockVector2D(0, 0);
public static final MutableBlockVector2D UNIT_X = new MutableBlockVector2D(1, 0);
public static final MutableBlockVector2D UNIT_Z = new MutableBlockVector2D(0, 1);
public static final MutableBlockVector2D ONE = new MutableBlockVector2D(1, 1);
/**
* A comparator for MutableBlockVector2Ds that orders the vectors by rows, with x as the
* column and z as the row.
*
* For example, if x is the horizontal axis and z is the vertical axis, it
* sorts like so:
*
* <pre>
* 0123
* 4567
* 90ab
* cdef
* </pre>
*/
public static final Comparator<MutableBlockVector2D> COMPARING_GRID_ARRANGEMENT = (a, b) -> {
return ComparisonChain.start()
.compare(a.getBlockZ(), b.getBlockZ())
.compare(a.getBlockX(), b.getBlockX())
.result();
};
/**
* Construct an instance.
*
* @param vector
*/
public MutableBlockVector2D(BlockVector2 vector) {
this(vector.getBlockX(), vector.getBlockZ());
}
/**
* Construct an instance.
*
* @param x the X coordinate
* @param z the Z coordinate
*/
public MutableBlockVector2D(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 MutableBlockVector2D(int x, int z) {
this.x = x;
this.z = z;
}
/**
* Get the X coordinate.
*
* @return the x coordinate
*/
public int getX() {
return x;
}
/**
* Get the X coordinate.
*
* @return the x coordinate
*/
public int getBlockX() {
return x;
}
/**
* Get the Z coordinate.
*
* @return the z coordinate
*/
public int getZ() {
return z;
}
/**
* Get the Z coordinate.
*
* @return the z coordinate
*/
public int getBlockZ() {
return z;
}
/**
* Add another vector to this vector.
*
* @param other the other vector
* @return a new vector
*/
public MutableBlockVector2D add(MutableBlockVector2D other) {
return add(other.x, other.z);
}
/**
* Add another vector to this vector.
*
* @param x the value to add
* @param z the value to add
* @return a new vector
*/
public MutableBlockVector2D add(int x, int z) {
this.x += x;
this.z += z;
return this;
}
/**
* Add a list of vectors to this vector.
*
* @param others an array of vectors
* @return a new vector
*/
public MutableBlockVector2D add(MutableBlockVector2D... others) {
for (MutableBlockVector2D other : others) {
x += other.x;
x += other.z;
}
return this;
}
/**
* Subtract another vector from this vector.
*
* @param other the other vector
* @return a new vector
*/
public MutableBlockVector2D subtract(MutableBlockVector2D other) {
return subtract(other.x, other.z);
}
/**
* Subtract another vector from this vector.
*
* @param x the value to subtract
* @param z the value to subtract
* @return a new vector
*/
public MutableBlockVector2D subtract(int x, int z) {
this.x -= x;
this.z -= z;
return this;
}
/**
* Subtract a list of vectors from this vector.
*
* @param others an array of vectors
* @return a new vector
*/
public MutableBlockVector2D subtract(MutableBlockVector2D... others) {
for (MutableBlockVector2D other : others) {
x -= other.x;
z -= other.z;
}
return this;
}
/**
* Multiply this vector by another vector on each component.
*
* @param other the other vector
* @return a new vector
*/
public MutableBlockVector2D multiply(MutableBlockVector2D other) {
return multiply(other.x, other.z);
}
/**
* Multiply this vector by another vector on each component.
*
* @param x the value to multiply
* @param z the value to multiply
* @return a new vector
*/
public MutableBlockVector2D multiply(int x, int z) {
this.x *= x;
this.z *= z;
return this;
}
/**
* Multiply this vector by zero or more vectors on each component.
*
* @param others an array of vectors
* @return a new vector
*/
public MutableBlockVector2D multiply(MutableBlockVector2D... others) {
for (MutableBlockVector2D other : others) {
x *= other.x;
z *= other.z;
}
return this;
}
/**
* Perform scalar multiplication.
*
* @param n the value to multiply
* @return a new vector
*/
public MutableBlockVector2D multiply(int n) {
return multiply(n, n);
}
/**
* Divide this vector by another vector on each component.
*
* @param other the other vector
* @return a new vector
*/
public MutableBlockVector2D divide(MutableBlockVector2D other) {
return divide(other.x, other.z);
}
/**
* Divide this vector by another vector on each component.
*
* @param x the value to divide by
* @param z the value to divide by
* @return a new vector
*/
public MutableBlockVector2D divide(int x, int z) {
this.x /= x;
this.z /= z;
return this;
}
/**
* Perform scalar division.
*
* @param n the value to divide by
* @return a new vector
*/
public MutableBlockVector2D divide(int n) {
return divide(n, n);
}
/**
* Get the length of the vector.
*
* @return length
*/
public double length() {
return Math.sqrt(lengthSq());
}
/**
* Get the length, squared, of the vector.
*
* @return length, squared
*/
public int lengthSq() {
return x * x + z * z;
}
/**
* Get the distance between this vector and another vector.
*
* @param other the other vector
* @return distance
*/
public double distance(MutableBlockVector2D other) {
return Math.sqrt(distanceSq(other));
}
/**
* Get the distance between this vector and another vector, squared.
*
* @param other the other vector
* @return distance
*/
public int distanceSq(MutableBlockVector2D other) {
int dx = other.x - x;
int dz = other.z - z;
return dx * dx + dz * dz;
}
/**
* Get the normalized vector, which is the vector divided by its
* length.
*
* @return a new vector
*/
public MutableBlockVector2D normalize() {
double len = length();
this.x /= len;
this.z /= len;
return this;
}
/**
* Gets the dot product of this and another vector.
*
* @param other the other vector
* @return the dot product of this and the other vector
*/
public int dot(MutableBlockVector2D other) {
return x * other.x + z * other.z;
}
/**
* Checks to see if a vector is contained with another.
*
* @param min the minimum point (X, Y, and Z are the lowest)
* @param max the maximum point (X, Y, and Z are the lowest)
* @return true if the vector is contained
*/
public boolean containedWithin(MutableBlockVector2D min, MutableBlockVector2D max) {
return x >= min.x && x <= max.x
&& z >= min.z && z <= max.z;
}
/**
* Floors the values of all components.
*
* @return a new vector
*/
public MutableBlockVector2D floor() {
// already floored, kept for feature parity with Vector2
return this;
}
/**
* Rounds all components up.
*
* @return a new vector
*/
public MutableBlockVector2D ceil() {
// already raised, kept for feature parity with Vector2
return this;
}
/**
* Rounds all components to the closest integer.
*
* <p>Components &lt; 0.5 are rounded down, otherwise up.</p>
*
* @return a new vector
*/
public MutableBlockVector2D round() {
// already rounded, kept for feature parity with Vector2
return this;
}
/**
* Returns a vector with the absolute values of the components of
* this vector.
*
* @return a new vector
*/
public MutableBlockVector2D abs() {
x = Math.abs(x);
z = Math.abs(z);
return this;
}
/**
* Perform a 2D transformation on this vector.
*
* @param angle in degrees
* @param aboutX about which x coordinate to rotate
* @param aboutZ about which z coordinate to rotate
* @param translateX what to add after rotation
* @param translateZ what to add after rotation
* @return a new vector
* @see AffineTransform another method to transform vectors
*/
public MutableBlockVector2D transform2D(double angle, double aboutX, double aboutZ, double translateX, double translateZ) {
angle = Math.toRadians(angle);
double x = this.x - aboutX;
double z = this.z - aboutZ;
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double x2 = x * cos - z * sin;
double z2 = x * sin + z * cos;
this.x = (int) Math.floor(x2 + aboutX + translateX);
this.z = (int) Math.floor(z2 + aboutZ + translateZ);
return this;
}
/**
* Gets the minimum components of two vectors.
*
* @param v2 the second vector
* @return minimum
*/
public MutableBlockVector2D getMinimum(MutableBlockVector2D v2) {
x = Math.min(x, v2.x);
z = Math.min(z, v2.z);
return this;
}
/**
* Gets the maximum components of two vectors.
*
* @param v2 the second vector
* @return maximum
*/
public MutableBlockVector2D getMaximum(MutableBlockVector2D v2) {
x = Math.max(x, v2.x);
z = Math.max(z, v2.z);
return this;
}
/**
* Creates a 2D vector from this vector.
*
* @return a new vector
*/
public Vector2 toVector2() {
return Vector2.at(x, z);
}
/**
* Creates a 3D vector by adding a zero Y component to this vector.
*
* @return a new vector
*/
public Vector3 toVector3() {
return toVector3(0);
}
/**
* Creates a 3D vector by adding the specified Y component to this vector.
*
* @param y the Y component
* @return a new vector
*/
public Vector3 toVector3(double y) {
return Vector3.at(x, y, z);
}
/**
* Creates a 3D vector by adding a zero Y component to this vector.
*
* @return a new vector
*/
public BlockVector3 toBlockVector3() {
return toBlockVector3(0);
}
/**
* Creates a 3D vector by adding the specified Y component to this vector.
*
* @param y the Y component
* @return a new vector
*/
public BlockVector3 toBlockVector3(int y) {
return BlockVector3.at(x, y, z);
}
/**
* Creates a 2D vector from this vector.
*
* @return a new vector
*/
public BlockVector2 toBlockVector2() {
return BlockVector2.at(x, z);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof MutableBlockVector2D)) {
return false;
}
MutableBlockVector2D other = (MutableBlockVector2D) obj;
return other.x == this.x && other.z == this.z;
}
@Override
public int hashCode() {
int hash = 17;
hash = 31 * hash + Integer.hashCode(x);
hash = 31 * hash + Integer.hashCode(z);
return hash;
}
@Override
public String toString() {
return "Mutable (" + x + ", " + z + ")";
}
public MutableBlockVector2D setComponents(int x, int z) {
this.x = x;
this.z = z;
return this;
}
public MutableBlockVector2D setComponents(double x, double z) {
return setComponents((int) x, (int) z);
}
public final void mutX(int x) {
this.x = x;
}
public void mutZ(int z) {
this.z = z;
}
public final void mutX(double x) {
this.x = (int) x;
}
public void mutZ(double z) {
this.z = (int) z;
}
private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
stream.writeInt(x);
stream.writeInt(z);
}
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
this.x = stream.readInt();
this.z = stream.readInt();
}
public MutableBlockVector2D nextPosition() {
int absX = Math.abs(x);
int absY = Math.abs(z);
if (absX > absY) {
if (x > 0) {
return setComponents(x, z + 1);
} else {
return setComponents(x, z - 1);
}
} else if (absY > absX) {
if (z > 0) {
return setComponents(x - 1, z);
} else {
return setComponents(x + 1, z);
}
} else {
if (x == z && x > 0) {
return setComponents(x, z + 1);
}
if (x == absX) {
return setComponents(x, z + 1);
}
if (z == absY) {
return setComponents(x, z - 1);
}
return setComponents(x + 1, z);
}
}
}

View File

@ -0,0 +1,73 @@
package com.sk89q.worldedit.math;
public class MutableBlockVector3 extends BlockVector3 {
private static ThreadLocal<MutableBlockVector3> MUTABLE_CACHE = new ThreadLocal<MutableBlockVector3>() {
@Override
protected MutableBlockVector3 initialValue() {
return new MutableBlockVector3();
}
};
public static MutableBlockVector3 get(int x, int y, int z) {
return MUTABLE_CACHE.get().setComponents(x, y, z);
}
public MutableBlockVector3() {}
public MutableBlockVector3(BlockVector3 other) {
super(other.getX(), other.getY(), other.getZ());
}
public MutableBlockVector3 setComponents(BlockVector3 other) {
return setComponents(other.getBlockX(), other.getBlockY(), other.getBlockZ());
}
public MutableBlockVector3(int x, int y, int z) {
super(x, y, z);
}
@Override
public MutableBlockVector3 setComponents(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
@Override
public MutableBlockVector3 mutX(double x) {
this.x = (int) x;
return this;
}
@Override
public MutableBlockVector3 mutY(double y) {
this.y = (int) y;
return this;
}
@Override
public MutableBlockVector3 mutZ(double z) {
this.z = (int) z;
return this;
}
@Override
public final MutableBlockVector3 mutX(int x) {
this.x = x;
return this;
}
@Override
public final MutableBlockVector3 mutY(int y) {
this.y = y;
return this;
}
@Override
public final MutableBlockVector3 mutZ(int z) {
this.z = z;
return this;
}
}

View File

@ -1,581 +0,0 @@
package com.sk89q.worldedit.math;
import com.sk89q.worldedit.math.transform.AffineTransform;
import static com.google.common.base.Preconditions.checkArgument;
import java.io.IOException;
import java.io.Serializable;
/**
* A mutable rendition of WorldEdit's Vector3 class.
* This class should ONLY be used locally for efficient vector manipulation.
*/
public class MutableVector implements Serializable {
private transient double x, y, z;
private static ThreadLocal<MutableVector> MUTABLE_CACHE = new ThreadLocal<MutableVector>() {
@Override
protected MutableVector initialValue() {
return new MutableVector();
}
};
public static MutableVector get(double x, double y, double z) {
return MUTABLE_CACHE.get().setComponents(x, y, z);
}
public MutableVector(Vector3 v) {
this(v.getX(), v.getY(), v.getZ());
}
public MutableVector(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public MutableVector() {
this(0, 0, 0);
}
public MutableVector setComponents(Vector3 other) {
return setComponents(other.getX(), other.getY(), other.getZ());
}
public MutableVector setComponents(MutableVector mutable) {
return setComponents(mutable.getX(), mutable.getY(), mutable.getZ());
}
public MutableVector setComponents(int x, int y, int z) {
return this.setComponents((double) x, (double) y, (double) z);
}
public MutableVector setComponents(double x, double y, double z) {
this.mutX(x);
this.mutY(y);
this.mutZ(z);
return this;
}
public final void mutX(double x) {
this.x = y;
}
public final void mutY(double y) {
this.y = y;
}
public final void mutZ(double z) {
this.z = y;
}
public final void mutX(int x) {
this.x = (double)x;
}
public final void mutY(int y) {
this.y = (double)y;
}
public final void mutZ(int z) {
this.z = (double)z;
}
public final double getX() {
return x;
}
public final double getY() {
return y;
}
public final double getZ() {
return z;
}
/**
* Add another vector to this vector and return the result as a new vector.
*
* @param other the other vector
* @return a new vector
*/
public MutableVector add(MutableVector other) {
return add(other.x, other.y, other.z);
}
/**
* Add another vector to this vector and return the result as a new vector.
*
* @param x the value to add
* @param y the value to add
* @param z the value to add
* @return a new vector
*/
public MutableVector add(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
/**
* Add a list of vectors to this vector and return the
* result as a new vector.
*
* @param others an array of vectors
* @return a new vector
*/
public MutableVector add(MutableVector... others) {
for (MutableVector other : others) {
x += other.x;
y += other.y;
z += other.z;
}
return this;
}
/**
* Subtract another vector from this vector and return the result
* as a new vector.
*
* @param other the other vector
* @return a new vector
*/
public MutableVector subtract(MutableVector other) {
return subtract(other.x, other.y, other.z);
}
/**
* Subtract another vector from this vector and return the result
* as a new vector.
*
* @param x the value to subtract
* @param y the value to subtract
* @param z the value to subtract
* @return a new vector
*/
public MutableVector subtract(double x, double y, double z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
/**
* Subtract a list of vectors from this vector and return the result
* as a new vector.
*
* @param others an array of vectors
* @return a new vector
*/
public MutableVector subtract(MutableVector... others) {
for (MutableVector other : others) {
x -= other.x;
y -= other.y;
z -= other.z;
}
return this;
}
/**
* Multiply this vector by another vector on each component.
*
* @param other the other vector
* @return a new vector
*/
public MutableVector multiply(MutableVector other) {
return multiply(other.x, other.y, other.z);
}
/**
* Multiply this vector by another vector on each component.
*
* @param x the value to multiply
* @param y the value to multiply
* @param z the value to multiply
* @return a new vector
*/
public MutableVector multiply(double x, double y, double z) {
this.x *= x;
this.y *= y;
this.z *= z;
return this;
}
/**
* Multiply this vector by zero or more vectors on each component.
*
* @param others an array of vectors
* @return a new vector
*/
public MutableVector multiply(MutableVector... others) {
for (MutableVector other : others) {
x *= other.x;
y *= other.y;
z *= other.z;
}
return this;
}
/**
* Perform scalar multiplication and return a new vector.
*
* @param n the value to multiply
* @return a new vector
*/
public MutableVector multiply(double n) {
return multiply(n, n, n);
}
/**
* Divide this vector by another vector on each component.
*
* @param other the other vector
* @return a new vector
*/
public MutableVector divide(MutableVector other) {
return divide(other.x, other.y, other.z);
}
/**
* Divide this vector by another vector on each component.
*
* @param x the value to divide by
* @param y the value to divide by
* @param z the value to divide by
* @return a new vector
*/
public MutableVector divide(double x, double y, double z) {
this.x /= x;
this.y /= y;
this.z /= z;
return this;
}
/**
* Perform scalar division and return a new vector.
*
* @param n the value to divide by
* @return a new vector
*/
public MutableVector divide(double n) {
return divide(n, n, n);
}
/**
* Get the length of the vector.
*
* @return length
*/
public double length() {
return Math.sqrt(lengthSq());
}
/**
* Get the length, squared, of the vector.
*
* @return length, squared
*/
public double lengthSq() {
return x * x + y * y + z * z;
}
/**
* Get the distance between this vector and another vector.
*
* @param other the other vector
* @return distance
*/
public double distance(Vector3 other) {
return Math.sqrt(distanceSq(other));
}
/**
* Get the distance between this vector and another vector, squared.
*
* @param other the other vector
* @return distance
*/
public double distanceSq(Vector3 other) {
double dx = other.getX() - x;
double dy = other.getY() - y;
double dz = other.getZ() - z;
return dx * dx + dy * dy + dz * dz;
}
/**
* Get the normalized vector, which is the vector divided by its
* length, as a new vector.
*
* @return a new vector
*/
public MutableVector normalize() {
return divide(length());
}
/**
* Gets the dot product of this and another vector.
*
* @param other the other vector
* @return the dot product of this and the other vector
*/
public double dot(MutableVector other) {
return x * other.x + y * other.y + z * other.z;
}
/**
* Gets the cross product of this and another vector.
*
* @param other the other vector
* @return the cross product of this and the other vector
*/
public Vector3 cross(MutableVector other) {
return Vector3.at(
y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x
);
}
/**
* Checks to see if a vector is contained with another.
*
* @param min the minimum point (X, Y, and Z are the lowest)
* @param max the maximum point (X, Y, and Z are the lowest)
* @return true if the vector is contained
*/
public boolean containedWithin(MutableVector min, MutableVector max) {
return x >= min.x && x <= max.x && y >= min.y && y <= max.y && z >= min.z && z <= max.z;
}
/**
* Clamp the Y component.
*
* @param min the minimum value
* @param max the maximum value
* @return a new vector
*/
public MutableVector clampY(int min, int max) {
checkArgument(min <= max, "minimum cannot be greater than maximum");
if (y < min) {
y = min;
}
if (y > max) {
y = max;
}
return this;
}
/**
* Floors the values of all components.
*
* @return a new vector
*/
public MutableVector floor() {
x = Math.floor(x);
y = Math.floor(y);
z = Math.floor(z);
return this;
}
/**
* Rounds all components up.
*
* @return a new vector
*/
public MutableVector ceil() {
x = Math.ceil(x);
y = Math.ceil(y);
z = Math.ceil(z);
return this;
}
/**
* Rounds all components to the closest integer.
*
* <p>Components &lt; 0.5 are rounded down, otherwise up.</p>
*
* @return a new vector
*/
public MutableVector round() {
x = Math.floor(x + 0.5);
y = Math.floor(y + 0.5);
z = Math.floor(z + 0.5);
return this;
}
/**
* Returns a vector with the absolute values of the components of
* this vector.
*
* @return a new vector
*/
public MutableVector abs() {
x = Math.abs(x);
y = Math.abs(y);
z = Math.abs(z);
return this;
}
/**
* Perform a 2D transformation on this vector and return a new one.
*
* @param angle in degrees
* @param aboutX about which x coordinate to rotate
* @param aboutZ about which z coordinate to rotate
* @param translateX what to add after rotation
* @param translateZ what to add after rotation
* @return a new vector
* @see AffineTransform another method to transform vectors
*/
public MutableVector transform2D(double angle, double aboutX, double aboutZ, double translateX, double translateZ) {
angle = Math.toRadians(angle);
double x = this.x - aboutX;
double z = this.z - aboutZ;
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double x2 = x * cos - z * sin;
double z2 = x * sin + z * cos;
this.x = x2 + aboutX + translateX;
this.z = z2 + aboutZ + translateZ;
return this;
}
/**
* Get this vector's pitch as used within the game.
*
* @return pitch in radians
*/
public double toPitch() {
double x = getX();
double z = getZ();
if (x == 0 && z == 0) {
return getY() > 0 ? -90 : 90;
} else {
double x2 = x * x;
double z2 = z * z;
double xz = Math.sqrt(x2 + z2);
return Math.toDegrees(Math.atan(-getY() / xz));
}
}
/**
* Get this vector's yaw as used within the game.
*
* @return yaw in radians
*/
public double toYaw() {
double x = getX();
double z = getZ();
double t = Math.atan2(-x, z);
double tau = 2 * Math.PI;
return Math.toDegrees(((t + tau) % tau));
}
/**
* Gets the minimum components of two vectors.
*
* @param v2 the second vector
* @return minimum
*/
public MutableVector getMinimum(MutableVector v2) {
x = Math.min(x, v2.x);
y = Math.min(y, v2.y);
z = Math.min(z, v2.z);
return this;
}
/**
* Gets the maximum components of two vectors.
*
* @param v2 the second vector
* @return maximum
*/
public MutableVector getMaximum(MutableVector v2) {
x = Math.max(x, v2.x);
y = Math.max(y, v2.y);
z = Math.max(z, v2.z);
return this;
}
/**
* Create a new {@code BlockVector} using the given components.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return a new {@code BlockVector}
*/
public static BlockVector3 toBlockPoint(double x, double y, double z) {
return BlockVector3.at(x, y, z);
}
/**
* Create a new {@code BlockVector} from this vector.
*
* @return a new {@code BlockVector}
*/
public BlockVector3 toBlockPoint() {
return toBlockPoint(x, y, z);
}
/**
* Creates a 2D vector by dropping the Y component from this vector.
*
* @return a new {@link Vector2}
*/
public Vector2 toVector2() {
return Vector2.at(x, z);
}
public Vector3 toVector3() {
return Vector3.at(x, y, z);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof MutableVector)) {
return false;
}
MutableVector other = (MutableVector) obj;
return other.x == this.x && other.y == this.y && other.z == this.z;
}
@Override
public int hashCode() {
int hash = 17;
hash = 31 * hash + Double.hashCode(x);
hash = 31 * hash + Double.hashCode(y);
hash = 31 * hash + Double.hashCode(z);
return hash;
}
@Override
public String toString() {
return "Mutable (" + x + ", " + y + ", " + z + ")";
}
private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
stream.writeDouble(x);
stream.writeByte((byte) y);
stream.writeDouble(z);
}
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
this.x = stream.readDouble();
this.y = stream.readByte() & 0xFF;
this.z = stream.readDouble();
}
}

View File

@ -0,0 +1,70 @@
package com.sk89q.worldedit.math;
import java.io.Serializable;
public class MutableVector2 extends Vector2 {
public MutableVector2() {}
/**
* Construct an instance.
*
* @param x the X coordinate
* @param z the Z coordinate
*/
public MutableVector2(double x, double z) {
super(x, z);
}
/**
* Construct an instance.
*
* @param x the X coordinate
* @param z the Z coordinate
*/
public MutableVector2(float x, float z) {
super(x, z);
}
/**
* Copy another vector.
*
* @param other the other vector
*/
public MutableVector2(Vector2 other) {
super(other);
}
@Override
public MutableVector2 setComponents(int x, int z) {
this.x = x;
this.z = z;
return this;
}
@Override
public MutableVector2 setComponents(double x, double z) {
this.x = x;
this.z = z;
return this;
}
@Override
public MutableVector2 mutX(int x) {
this.x = x;
return this;
}
@Override
public MutableVector2 mutZ(int z) {
this.z = z;
return this;
}
@Override
public MutableVector2 mutX(double x) {
this.x = x;
return this;
}
@Override
public MutableVector2 mutZ(double z) {
this.z = z;
return this;
}
}

View File

@ -0,0 +1,82 @@
package com.sk89q.worldedit.math;
import javax.annotation.Nullable;
public class MutableVector3 extends Vector3 {
public MutableVector3() {}
public MutableVector3(double x, double y, double z) {
super(x, y, z);
}
public MutableVector3(float x, float y, float z) {
super(x, y, z);
}
public MutableVector3(Vector3 other) {
super(other);
}
@Override
public MutableVector3 setComponents(Vector3 other) {
this.x = other.x;
this.y = other.y;
this.z = other.z;
return this;
}
@Override
public MutableVector3 setComponents(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
@Override
public MutableVector3 setComponents(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
@Override
public MutableVector3 mutX(int x) {
this.x = x;
return this;
}
@Override
public MutableVector3 mutZ(int z) {
this.z = z;
return this;
}
@Override
public MutableVector3 mutX(double x) {
this.x = x;
return this;
}
@Override
public MutableVector3 mutZ(double z) {
this.z = z;
return this;
}
@Override
public MutableVector3 mutY(int y) {
this.y = y;
return this;
}
@Override
public MutableVector3 mutY(double y) {
this.y = y;
return this;
}
public double getY() {
return y;
}
}

View File

@ -19,12 +19,13 @@
package com.sk89q.worldedit.math;
import com.boydti.fawe.util.MathMan;
import com.sk89q.worldedit.math.transform.AffineTransform;
/**
* An immutable 2-dimensional vector.
*/
public final class Vector2 {
public class Vector2 {
public static final Vector2 ZERO = new Vector2(0, 0);
public static final Vector2 UNIT_X = new Vector2(1, 0);
@ -32,23 +33,12 @@ public final class Vector2 {
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;
protected double x, z;
protected Vector2(){}
/**
* Construct an instance.
@ -56,11 +46,48 @@ public final class Vector2 {
* @param x the X coordinate
* @param z the Z coordinate
*/
private Vector2(double x, double z) {
protected Vector2(double x, double z) {
this.x = x;
this.z = z;
}
protected Vector2(Vector2 other) {
this.x = other.x;
this.z = other.z;
}
public int getBlockX() {
return MathMan.roundInt(getX());
}
public int getBlockZ() {
return MathMan.roundInt(getZ());
}
public MutableVector2 setComponents(int x, int z) {
return new MutableVector2(x, z);
}
public MutableVector2 setComponents(double x, double z) {
return new MutableVector2(x, z);
}
public MutableVector2 mutX(int x) {
return new MutableVector2(x, z);
}
public MutableVector2 mutZ(int z) {
return new MutableVector2(x, z);
}
public MutableVector2 mutX(double x) {
return new MutableVector2(x, z);
}
public MutableVector2 mutZ(double z) {
return new MutableVector2(x, z);
}
/**
* Get the X coordinate.
*
@ -458,21 +485,18 @@ public final class Vector2 {
}
Vector2 other = (Vector2) obj;
return other.x == this.x && other.z == this.z;
return other.getX() == this.getX() && other.getZ() == this.getZ();
}
@Override
public int hashCode() {
int hash = 17;
hash = 31 * hash + Double.hashCode(x);
hash = 31 * hash + Double.hashCode(z);
return hash;
return ((int) getX() ^ ((int) getZ() << 16));
}
@Override
public String toString() {
return "(" + x + ", " + z + ")";
return "(" + getX() + ", " + getZ() + ")";
}
}

View File

@ -21,9 +21,11 @@ package com.sk89q.worldedit.math;
import static com.google.common.base.Preconditions.checkArgument;
import com.boydti.fawe.util.MathMan;
import com.google.common.collect.ComparisonChain;
import com.sk89q.worldedit.math.transform.AffineTransform;
import javax.annotation.Nullable;
import java.util.Comparator;
/**
@ -38,21 +40,6 @@ public class Vector3 {
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);
}
@ -77,7 +64,7 @@ public class Vector3 {
return YzxOrderComparator.YZX_ORDER;
}
private final double x, y, z;
protected double x, y, z;
/**
* Construct an instance.
@ -86,12 +73,68 @@ public class Vector3 {
* @param y the Y coordinate
* @param z the Z coordinate
*/
private Vector3(double x, double y, double z) {
protected Vector3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
protected Vector3() {}
protected Vector3(Vector3 other) {
this.x = other.x;
this.y = other.y;
this.z = other.z;
}
public int getBlockX() {
return MathMan.roundInt(getX());
}
public int getBlockY() {
return MathMan.roundInt(getY());
}
public int getBlockZ() {
return MathMan.roundInt(getZ());
}
public MutableVector3 setComponents(Vector3 other) {
return new MutableVector3(other);
}
public MutableVector3 setComponents(int x, int y, int z) {
return new MutableVector3(x, y, z);
}
public MutableVector3 setComponents(double x, double y, double z) {
return new MutableVector3(x, y, z);
}
public MutableVector3 mutX(int x) {
return new MutableVector3(x, y, getZ());
}
public MutableVector3 mutX(double x) {
return new MutableVector3(x, y, getZ());
}
public MutableVector3 mutY(int y) {
return new MutableVector3(getX(), y, getZ());
}
public MutableVector3 mutY(double y) {
return new MutableVector3(getX(), y, getZ());
}
public MutableVector3 mutZ(int z) {
return new MutableVector3(getX(), y, z);
}
public MutableVector3 mutZ(double z) {
return new MutableVector3(getX(), y, z);
}
/**
* Get the X coordinate.
*
@ -579,25 +622,25 @@ public class Vector3 {
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof Vector3)) {
return false;
}
Vector3 other = (Vector3) obj;
return other.x == this.x && other.y == this.y && other.z == this.z;
return other.getX() == this.getX() && other.getZ() == this.getZ() && other.getY() == this.getY();
}
@Override
public int hashCode() {
int hash = 17;
hash = 31 * hash + Double.hashCode(x);
hash = 31 * hash + Double.hashCode(y);
hash = 31 * hash + Double.hashCode(z);
return hash;
return ((int) getX() ^ ((int) getZ() << 16)) ^ ((int) getY() << 30);
}
@Override
public String toString() {
String x = (getX() == getBlockX() ? "" + getBlockX() : "" + getX());
String y = (getY() == getBlockY() ? "" + getBlockY() : "" + getY());
String z = (getZ() == getBlockZ() ? "" + getBlockZ() : "" + getZ());
return "(" + x + ", " + y + ", " + z + ")";
}

View File

@ -23,7 +23,7 @@ package com.sk89q.worldedit.math.interpolation;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.math.MutableBlockVector;
import com.sk89q.worldedit.math.MutableBlockVector3;
import com.sk89q.worldedit.math.Vector3;
import java.util.Collections;
@ -139,7 +139,7 @@ public class KochanekBartelsInterpolation implements Interpolation {
return nodes.get(index).getPosition();
}
private MutableBlockVector mutable = new MutableBlockVector();
private MutableBlockVector3 mutable = new MutableBlockVector3();
@Override
public Vector3 getPosition(double position) {

View File

@ -5,7 +5,7 @@ import java.io.Serializable;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.MathUtils;
import com.sk89q.worldedit.math.MutableBlockVector;
import com.sk89q.worldedit.math.MutableBlockVector3;
import com.sk89q.worldedit.math.Vector3;
/**
@ -17,7 +17,7 @@ import com.sk89q.worldedit.math.Vector3;
*/
public class AffineTransform implements Transform, Serializable {
private transient MutableBlockVector mutable = new MutableBlockVector();
private transient MutableBlockVector3 mutable = new MutableBlockVector3();
/**
* coefficients for x coordinate.
@ -324,7 +324,7 @@ public class AffineTransform implements Transform, Serializable {
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
mutable = new MutableBlockVector();
mutable = new MutableBlockVector3();
}

View File

@ -5,7 +5,7 @@ import com.sk89q.worldedit.math.Vector3;
public class RoundedTransform implements Transform{
private final Transform transform;
// private MutableBlockVector mutable = new MutableBlockVector();
// private MutableBlockVector3 mutable = new MutableBlockVector3();
public RoundedTransform(Transform transform) {
this.transform = transform;

View File

@ -43,7 +43,7 @@ public final class Transforms {
public static Location transform(Location location, Transform transform) {
checkNotNull(location);
checkNotNull(transform);
return new Location(location.getExtent(), transform.apply(location.toVector()), location.getDirection());
return new Location(location.getExtent(), transform.apply(location), location.getDirection());
}
}