make Vector3 immutable by default

This commit is contained in:
dfsek
2021-12-20 00:37:54 -07:00
parent 961a42d1cb
commit b9965bdbc5
14 changed files with 343 additions and 235 deletions

View File

@@ -13,10 +13,18 @@ public interface ReadableWorld extends World {
return getBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ());
}
default BlockState getBlockState(Vector3.Mutable position) {
return getBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ());
}
default BlockState getBlockState(Vector3Int position) {
return getBlockState(position.getX(), position.getY(), position.getZ());
}
default BlockState getBlockState(Vector3Int.Mutable position) {
return getBlockState(position.getX(), position.getY(), position.getZ());
}
BlockEntity getBlockEntity(int x, int y, int z);
default BlockEntity getBlockEntity(Vector3 position) {

View File

@@ -12,18 +12,34 @@ public interface WritableWorld extends ReadableWorld {
setBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ(), data, physics);
}
default void setBlockState(Vector3.Mutable position, BlockState data, boolean physics) {
setBlockState(position.getBlockX(), position.getBlockY(), position.getBlockZ(), data, physics);
}
default void setBlockState(Vector3Int position, BlockState data, boolean physics) {
setBlockState(position.getX(), position.getY(), position.getZ(), data, physics);
}
default void setBlockState(Vector3Int.Mutable position, BlockState data, boolean physics) {
setBlockState(position.getX(), position.getY(), position.getZ(), data, physics);
}
default void setBlockState(Vector3 position, BlockState data) {
setBlockState(position, data, false);
}
default void setBlockState(Vector3.Mutable position, BlockState data) {
setBlockState(position, data, false);
}
default void setBlockState(Vector3Int position, BlockState data) {
setBlockState(position, data, false);
}
default void setBlockState(Vector3Int.Mutable position, BlockState data) {
setBlockState(position, data, false);
}
default void setBlockState(int x, int y, int z, BlockState data) {
setBlockState(x, y, z, data, false);
}

View File

@@ -15,10 +15,10 @@ import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.util.MathUtil;
public class Vector3 implements Cloneable {
private double x;
private double y;
private double z;
public class Vector3 {
private final double x;
private final double y;
private final double z;
private Vector3(double x, double y, double z) {
this.x = x;
@@ -30,40 +30,6 @@ public class Vector3 implements Cloneable {
return new Vector3(x, y, z);
}
public Vector3 multiply(double m) {
x *= m;
y *= m;
z *= m;
return this;
}
public Vector3 add(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
public Vector3 add(Vector3 other) {
this.x += other.getX();
this.y += other.getY();
this.z += other.getZ();
return this;
}
public Vector3 add(Vector3Int other) {
this.x += other.getX();
this.y += other.getY();
this.z += other.getZ();
return this;
}
public Vector3 add(Vector2 other) {
this.x += other.getX();
this.z += other.getZ();
return this;
}
public double lengthSquared() {
return x * x + y * y + z * z;
}
@@ -76,75 +42,6 @@ public class Vector3 implements Cloneable {
return FastMath.invSqrtQuick(lengthSquared());
}
/**
* Rotates the vector around the x axis.
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
*
* @return the same vector
*/
@NotNull
public Vector3 rotateAroundX(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double y = angleCos * getY() - angleSin * getZ();
double z = angleSin * getY() + angleCos * getZ();
return setY(y).setZ(z);
}
/**
* Rotates the vector around the y axis.
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
*
* @return the same vector
*/
@NotNull
public Vector3 rotateAroundY(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double x = angleCos * getX() + angleSin * getZ();
double z = -angleSin * getX() + angleCos * getZ();
return setX(x).setZ(z);
}
/**
* Rotates the vector around the z axis
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
*
* @return the same vector
*/
@NotNull
public Vector3 rotateAroundZ(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double x = angleCos * getX() - angleSin * getY();
double y = angleSin * getX() + angleCos * getY();
return setX(x).setY(y);
}
/**
* Get the distance between this vector and another. The value of this
* method is not cached and uses a costly square-root function, so do not
@@ -171,74 +68,6 @@ public class Vector3 implements Cloneable {
return FastMath.pow2(x - o.getX()) + FastMath.pow2(y - o.getY()) + FastMath.pow2(z - o.getZ());
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
*
* <p>
* Rotation will follow the general Right-Hand-Rule, which means rotation
* will be counterclockwise when the axis is pointing towards the observer.
* <p>
* This method will always make sure the provided axis is a unit vector, to
* not modify the length of the vector when rotating.
*
* @param axis the axis to rotate the vector around. If the passed vector is
* not of length 1, it gets copied and normalized before using it for the
* rotation. Please use {@link Vector3#normalize()} on the instance before
* passing it to this method
* @param angle the angle to rotate the vector around the axis
*
* @return the same vector
*
* @throws IllegalArgumentException if the provided axis vector instance is
* null
*/
@NotNull
public Vector3 rotateAroundAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
return rotateAroundNonUnitAxis(axis.isNormalized() ? axis : axis.clone().normalize(), angle);
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
*
* <p>
* Rotation will follow the general Right-Hand-Rule, which means rotation
* will be counterclockwise when the axis is pointing towards the observer.
* <p>
* Note that the vector length will change accordingly to the axis vector
* length. If the provided axis is not a unit vector, the rotated vector
* will not have its previous length. The scaled length of the resulting
* vector will be related to the axis vector.
*
* @param axis the axis to rotate the vector around.
* @param angle the angle to rotate the vector around the axis
*
* @return the same vector
*
* @throws IllegalArgumentException if the provided axis vector instance is
* null
*/
@NotNull
public Vector3 rotateAroundNonUnitAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
double x = getX(), y = getY(), z = getZ();
double x2 = axis.getX(), y2 = axis.getY(), z2 = axis.getZ();
double cosTheta = Math.cos(angle);
double sinTheta = Math.sin(angle);
double dotProduct = this.dot(axis);
double xPrime = x2 * dotProduct * (1d - cosTheta)
+ x * cosTheta
+ (-z2 * y + y2 * z) * sinTheta;
double yPrime = y2 * dotProduct * (1d - cosTheta)
+ y * cosTheta
+ (z2 * x - x2 * z) * sinTheta;
double zPrime = z2 * dotProduct * (1d - cosTheta)
+ z * cosTheta
+ (-y2 * x + x2 * y) * sinTheta;
return setX(xPrime).setY(yPrime).setZ(zPrime);
}
/**
* Calculates the dot product of this vector with another. The dot product
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
@@ -251,50 +80,20 @@ public class Vector3 implements Cloneable {
return x * other.getX() + y * other.getY() + z * other.getZ();
}
public Vector3 normalize() {
return this.multiply(this.inverseLength());
}
public Vector3 subtract(int x, int y, int z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
public Vector3 subtract(Vector3 end) {
x -= end.getX();
y -= end.getY();
z -= end.getZ();
return this;
}
public double getZ() {
return z;
}
public Vector3 setZ(double z) {
this.z = z;
return this;
}
public double getX() {
return x;
}
public Vector3 setX(double x) {
this.x = x;
return this;
}
public double getY() {
return y;
}
public Vector3 setY(double y) {
this.y = y;
return this;
}
public int getBlockX() {
return FastMath.floorToInt(x);
@@ -345,11 +144,287 @@ public class Vector3 implements Cloneable {
return MathUtil.equals(x, other.getX()) && MathUtil.equals(y, other.getY()) && MathUtil.equals(z, other.getZ());
}
public Vector3 clone() {
try {
return (Vector3) super.clone();
} catch(CloneNotSupportedException e) {
throw new Error(e);
public Mutable mutable() {
return new Mutable(x, y, z);
}
public static class Mutable {
private double x, y, z;
private Mutable(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public static Mutable of(double x, double y, double z) {
return new Mutable(x, y, z);
}
public Vector3 immutable() {
return Vector3.of(x, y, z);
}
public double getZ() {
return z;
}
public Mutable setZ(double z) {
this.z = z;
return this;
}
public double getX() {
return x;
}
public Mutable setX(double x) {
this.x = x;
return this;
}
public double getY() {
return y;
}
public Mutable setY(double y) {
this.y = y;
return this;
}
public double lengthSquared() {
return x * x + y * y + z * z;
}
public double length() {
return FastMath.sqrt(lengthSquared());
}
public double inverseLength() {
return FastMath.invSqrtQuick(lengthSquared());
}
public Mutable normalize() {
return this.multiply(this.inverseLength());
}
public Mutable subtract(int x, int y, int z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
/**
* Calculates the dot product of this vector with another. The dot product
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
*
* @param other The other vector
*
* @return dot product
*/
public double dot(@NotNull Vector3 other) {
return x * other.getX() + y * other.getY() + z * other.getZ();
}
public Mutable subtract(Vector3 end) {
x -= end.getX();
y -= end.getY();
z -= end.getZ();
return this;
}
public Mutable multiply(double m) {
x *= m;
y *= m;
z *= m;
return this;
}
public Mutable add(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
public Mutable add(Vector3 other) {
this.x += other.getX();
this.y += other.getY();
this.z += other.getZ();
return this;
}
public Mutable add(Vector3Int other) {
this.x += other.getX();
this.y += other.getY();
this.z += other.getZ();
return this;
}
public Mutable add(Vector2 other) {
this.x += other.getX();
this.z += other.getZ();
return this;
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
*
* <p>
* Rotation will follow the general Right-Hand-Rule, which means rotation
* will be counterclockwise when the axis is pointing towards the observer.
* <p>
* This method will always make sure the provided axis is a unit vector, to
* not modify the length of the vector when rotating.
*
* @param axis the axis to rotate the vector around. If the passed vector is
* not of length 1, it gets copied and normalized before using it for the
* rotation. Please use {@link Mutable#normalize()} on the instance before
* passing it to this method
* @param angle the angle to rotate the vector around the axis
*
* @return the same vector
*
* @throws IllegalArgumentException if the provided axis vector instance is
* null
*/
@NotNull
public Mutable rotateAroundAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
return rotateAroundNonUnitAxis(axis.isNormalized() ? axis : axis.mutable().normalize().immutable(), angle);
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
*
* <p>
* Rotation will follow the general Right-Hand-Rule, which means rotation
* will be counterclockwise when the axis is pointing towards the observer.
* <p>
* Note that the vector length will change accordingly to the axis vector
* length. If the provided axis is not a unit vector, the rotated vector
* will not have its previous length. The scaled length of the resulting
* vector will be related to the axis vector.
*
* @param axis the axis to rotate the vector around.
* @param angle the angle to rotate the vector around the axis
*
* @return the same vector
*
* @throws IllegalArgumentException if the provided axis vector instance is
* null
*/
@NotNull
public Mutable rotateAroundNonUnitAxis(@NotNull Vector3 axis, double angle) throws IllegalArgumentException {
double x = getX(), y = getY(), z = getZ();
double x2 = axis.getX(), y2 = axis.getY(), z2 = axis.getZ();
double cosTheta = Math.cos(angle);
double sinTheta = Math.sin(angle);
double dotProduct = this.dot(axis);
double xPrime = x2 * dotProduct * (1d - cosTheta)
+ x * cosTheta
+ (-z2 * y + y2 * z) * sinTheta;
double yPrime = y2 * dotProduct * (1d - cosTheta)
+ y * cosTheta
+ (z2 * x - x2 * z) * sinTheta;
double zPrime = z2 * dotProduct * (1d - cosTheta)
+ z * cosTheta
+ (-y2 * x + x2 * y) * sinTheta;
return setX(xPrime).setY(yPrime).setZ(zPrime);
}
/**
* Rotates the vector around the x axis.
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
*
* @return the same vector
*/
@NotNull
public Mutable rotateAroundX(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double y = angleCos * getY() - angleSin * getZ();
double z = angleSin * getY() + angleCos * getZ();
return setY(y).setZ(z);
}
/**
* Rotates the vector around the y axis.
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
*
* @return the same vector
*/
@NotNull
public Mutable rotateAroundY(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double x = angleCos * getX() + angleSin * getZ();
double z = -angleSin * getX() + angleCos * getZ();
return setX(x).setZ(z);
}
/**
* Rotates the vector around the z axis
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
*
* @return the same vector
*/
@NotNull
public Mutable rotateAroundZ(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double x = angleCos * getX() - angleSin * getY();
double y = angleSin * getX() + angleCos * getY();
return setX(x).setY(y);
}
@Override
public int hashCode() {
int hash = 13;
hash = 79 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
hash = 79 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
hash = 79 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
return hash;
}
public int getBlockX() {
return FastMath.floorToInt(x);
}
public int getBlockY() {
return FastMath.floorToInt(y);
}
public int getBlockZ() {
return FastMath.floorToInt(z);
}
}

View File

@@ -46,6 +46,10 @@ public class Vector3Int {
return Vector3.of(x, y, z);
}
public Vector3.Mutable toVector3Mutable() {
return Vector3.Mutable.of(x, y, z);
}
public static class Mutable {
private int x, y, z;