From 6c307d16f40be3205f34f64ec9037cf717942b8f Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Sat, 18 Jun 2016 15:28:54 -0500 Subject: [PATCH] Made Quats proper value type --- .../botania/common/core/helper/Quat.java | 74 ++++++++----------- 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/src/main/java/vazkii/botania/common/core/helper/Quat.java b/src/main/java/vazkii/botania/common/core/helper/Quat.java index 765d50912..9bc002343 100644 --- a/src/main/java/vazkii/botania/common/core/helper/Quat.java +++ b/src/main/java/vazkii/botania/common/core/helper/Quat.java @@ -10,42 +10,26 @@ */ package vazkii.botania.common.core.helper; +import java.util.Arrays; import java.util.Formatter; import java.util.Locale; +import java.util.Objects; -public class Quat { +public final class Quat { - public double x; - public double y; - public double z; - public double s; - - public Quat() { - s = 1.0D; - x = 0.0D; - y = 0.0D; - z = 0.0D; - } - - public Quat(Quat quat) { - x = quat.x; - y = quat.y; - z = quat.z; - s = quat.s; - } + public static final Quat IDENTITY = new Quat(0, 0, 0, 1); + public final double x; + public final double y; + public final double z; + public final double s; + private final int hashCode; public Quat(double d, double d1, double d2, double d3) { x = d1; y = d2; z = d3; s = d; - } - - public void set(Quat quat) { - x = quat.x; - y = quat.y; - z = quat.z; - s = quat.s; + this.hashCode = Arrays.hashCode(new double[] { d, d1, d2, d3 } ); } public static Quat aroundAxis(double ax, double ay, double az, double angle) { @@ -54,43 +38,33 @@ public class Quat { return new Quat(Math.cos(angle), ax * d4, ay * d4, az * d4); } - public void multiply(Quat quat) { + public Quat multiply(Quat quat) { double d = s * quat.s - x * quat.x - y * quat.y - z * quat.z; double d1 = s * quat.x + x * quat.s - y * quat.z + z * quat.y; double d2 = s * quat.y + x * quat.z + y * quat.s - z * quat.x; double d3 = s * quat.z - x * quat.y + y * quat.x + z * quat.s; - s = d; - x = d1; - y = d2; - z = d3; + return new Quat(d1, d2, d3, d); } - public void rightMultiply(Quat quat) { + public Quat rightMultiply(Quat quat) { double d = s * quat.s - x * quat.x - y * quat.y - z * quat.z; double d1 = s * quat.x + x * quat.s + y * quat.z - z * quat.y; double d2 = s * quat.y - x * quat.z + y * quat.s + z * quat.x; double d3 = s * quat.z + x * quat.y - y * quat.x + z * quat.s; - s = d; - x = d1; - y = d2; - z = d3; + return new Quat(d1, d2, d3, d); } public double mag() { return Math.sqrt(x * x + y * y + z * z + s * s); } - public void normalize() { + public Quat normalize() { double d = mag(); if (d == 0.0D) { - return; + return this; } else { d = 1.0D / d; - x *= d; - y *= d; - z *= d; - s *= d; - return; + return new Quat(x * d, y * d, z * d, s * d); } } @@ -104,6 +78,20 @@ public class Quat { vec.z = d3 * s - d * z - d1 * y + d2 * x; } + @Override + public int hashCode() { + return hashCode; + } + + @Override + public boolean equals(Object o) { + return o instanceof Quat + && ((Quat) o).x == this.x + && ((Quat) o).y == this.y + && ((Quat) o).z == this.z + && ((Quat) o).s == this.s; + } + @Override public String toString() { StringBuilder stringbuilder = new StringBuilder();