mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-21 09:31:20 -08:00
Merge remote-tracking branch 'gui/main' into merge-gui-and-server
This commit is contained in:
commit
ae94de39b1
430 files changed
+44297
-50
No files matched your search
@@ -0,0 +1,672 @@
|
||||
package io.eiren.math;
|
||||
|
||||
import com.jme3.math.FastMath;
|
||||
import com.jme3.math.Vector3f;
|
||||
import com.jme3.math.Vector4f;
|
||||
|
||||
|
||||
public class FloatMath {
|
||||
|
||||
public static final float PI = (float) Math.PI;
|
||||
public static final float TWO_PI = (float) (Math.PI * 2);
|
||||
public static final float ANGLE_EPSILON = 0.028f; // in degrees (float
|
||||
// epsilon for sin/cos)
|
||||
public static final float ANGLE_EPSILON_RAD = toRad(ANGLE_EPSILON);
|
||||
|
||||
public static final float ZERO_TOLERANCE_F = FastMath.ZERO_TOLERANCE;
|
||||
public static final double ZERO_TOLERANCE_D = 0.0001d;
|
||||
|
||||
public static final float SQRT_TWO = (float) Math.sqrt(2f);
|
||||
public static final float INV_SQRT_TWO = 1f / SQRT_TWO;
|
||||
public static final float SQRT_THREE = (float) Math.sqrt(3f);
|
||||
public static final float INV_SQRT_THREE = 1f / SQRT_THREE;
|
||||
public static final float TWO_FPI = PI * 2;
|
||||
|
||||
public static final float SIN_75_DEG = 0.965926f;
|
||||
public static final float SIN_60_DEG = 0.866025f;
|
||||
public static final float SIN_45_DEG = 0.707107f;
|
||||
public static final float SIN_30_DEG = 0.5f;
|
||||
public static final float SIN_15_DEG = 0.258819f;
|
||||
|
||||
public static final float COS_75_DEG = 0.258819f;
|
||||
public static final float COS_60_DEG = 0.5f;
|
||||
public static final float COS_45_DEG = 0.707107f;
|
||||
public static final float COS_30_DEG = 0.866025f;
|
||||
public static final float COS_15_DEG = 0.965926f;
|
||||
|
||||
public static final int TEN_BITS = ~(~0 << 10);
|
||||
public static final int TENTH_BIT = 1 << 10;
|
||||
public static final int TEN_BITS_MAX = ~(~0 << 9);
|
||||
public static final int TEN_BITS_MAX_UNSIGNED = ~(~0 << 10);
|
||||
public static final int TWO_BITS = ~(~0 << 2);
|
||||
public static final int SECOND_BIT = 1 << 2;
|
||||
public static final int TWO_BITS_MAX = ~(~0 << 1);
|
||||
public static final int TWO_BITS_MAX_UNSIGNED = ~(~0 << 2);
|
||||
|
||||
public static float roundIfZero(float x) {
|
||||
return Math.abs(x) < ZERO_TOLERANCE_F ? 0.0f : x;
|
||||
}
|
||||
|
||||
public static boolean equalsToZero(float x) {
|
||||
return Math.abs(x) < ZERO_TOLERANCE_F;
|
||||
}
|
||||
|
||||
public static boolean lessThanZero(float x) {
|
||||
return (x < -ZERO_TOLERANCE_F);
|
||||
}
|
||||
|
||||
public static boolean lessOrEqualsToZero(float x) {
|
||||
return (x < ZERO_TOLERANCE_F);
|
||||
}
|
||||
|
||||
public static boolean greaterThanZero(float x) {
|
||||
return (x > ZERO_TOLERANCE_F);
|
||||
}
|
||||
|
||||
public static boolean greaterOrEqualsToZero(float x) {
|
||||
return (x > -ZERO_TOLERANCE_F);
|
||||
}
|
||||
|
||||
public static boolean equalsToZero(float x, float epsilon) {
|
||||
return Math.abs(x) < epsilon;
|
||||
}
|
||||
|
||||
public static boolean equalsWithEpsilon(float x, float y) {
|
||||
return Math.abs(x - y) < ZERO_TOLERANCE_F;
|
||||
}
|
||||
|
||||
public static boolean equalsWithEpsilon(float x, float y, float epsilon) {
|
||||
return Math.abs(x - y) < epsilon;
|
||||
}
|
||||
|
||||
public static boolean lessWithEpsilon(float x, float y) {
|
||||
return (x < y - ZERO_TOLERANCE_F);
|
||||
}
|
||||
|
||||
public static boolean lessOrEqualsWithEpsilon(float x, float y) {
|
||||
return (x < y + ZERO_TOLERANCE_F);
|
||||
}
|
||||
|
||||
public static boolean lessWithEpsilon(float x, float y, float epsilon) {
|
||||
return (x < y - epsilon);
|
||||
}
|
||||
|
||||
public static boolean lessOrEqualsWithEpsilon(float x, float y, float epsilon) {
|
||||
return (x < y + epsilon);
|
||||
}
|
||||
|
||||
public static boolean greaterWithEpsilon(float x, float y) {
|
||||
return (x > y + ZERO_TOLERANCE_F);
|
||||
}
|
||||
|
||||
public static boolean greaterOrEqualsWithEpsilon(float x, float y) {
|
||||
return (x > y - ZERO_TOLERANCE_F);
|
||||
}
|
||||
|
||||
public static boolean greaterWithEpsilon(float x, float y, float epsilon) {
|
||||
return (x > y + epsilon);
|
||||
}
|
||||
|
||||
public static boolean greaterOrEqualsWithEpsilon(float x, float y, float epsilon) {
|
||||
return (x > y - epsilon);
|
||||
}
|
||||
|
||||
public static double roundIfZero(double x) {
|
||||
return Math.abs(x) < ZERO_TOLERANCE_D ? 0.0d : x;
|
||||
}
|
||||
|
||||
public static boolean equalsToZero(double x) {
|
||||
return Math.abs(x) < ZERO_TOLERANCE_D;
|
||||
}
|
||||
|
||||
public static boolean equalsWithEpsilon(double x, double y) {
|
||||
return Math.abs(x - y) < ZERO_TOLERANCE_D;
|
||||
}
|
||||
|
||||
public static boolean lessWithEpsilon(double x, double y) {
|
||||
return (x < y - ZERO_TOLERANCE_D);
|
||||
}
|
||||
|
||||
public static boolean lessOrEqualsWithEpsilon(double x, double y) {
|
||||
return (x < y + ZERO_TOLERANCE_D);
|
||||
}
|
||||
|
||||
public static boolean greaterWithEpsilon(double x, double y) {
|
||||
return (x > y + ZERO_TOLERANCE_D);
|
||||
}
|
||||
|
||||
public static boolean greaterOrEqualsWithEpsilon(double x, double y) {
|
||||
return (x > y - ZERO_TOLERANCE_D);
|
||||
}
|
||||
|
||||
public static float toDegrees(float angrad) {
|
||||
return angrad * 180.0f / PI;
|
||||
}
|
||||
|
||||
public static float toRad(float deg) {
|
||||
return deg / 180.0f * PI;
|
||||
}
|
||||
|
||||
public static boolean radEqual(float angle1, float angle2) {
|
||||
float diff = clampRad(angle1 - angle2);
|
||||
return Math.abs(diff) < ANGLE_EPSILON_RAD;
|
||||
}
|
||||
|
||||
public static boolean degreesEqual(float angle1, float angle2) {
|
||||
float diff = clampDegrees(angle1 - angle2);
|
||||
return Math.abs(diff) < ANGLE_EPSILON;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #normalizeRad(float)}
|
||||
*/
|
||||
@Deprecated
|
||||
public static float clampRad(float angle) {
|
||||
return normalizeRad(angle);
|
||||
}
|
||||
|
||||
public static float normalizeRad(float angle) {
|
||||
return FastMath.normalize(angle, -FastMath.PI, FastMath.PI);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #normalizeDegrees(float)}
|
||||
*/
|
||||
@Deprecated
|
||||
public static float clampDegrees(float angle) {
|
||||
return normalizeDegrees(angle);
|
||||
}
|
||||
|
||||
public static float normalizeDegrees(float angle) {
|
||||
return FastMath.normalize(angle, -180f, 180f);
|
||||
}
|
||||
|
||||
public static float animateEase(float t) {
|
||||
// Special case of Bezier interpolation (p0 = p1 = 0, p2 = p3 = 1)
|
||||
return (3.0f - 2.0f * t) * t * t;
|
||||
}
|
||||
|
||||
public static float animateEaseIn(float t) {
|
||||
return t * t;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lineary remaps value from the source interval to the target interval.
|
||||
* <a href="https://en.wikipedia.org/wiki/Linear_interpolation">details</a>
|
||||
*/
|
||||
public static float mapValue(
|
||||
float value,
|
||||
float sourceStart,
|
||||
float sourceEnd,
|
||||
float targetStart,
|
||||
float targetEnd
|
||||
) {
|
||||
return targetStart
|
||||
+ (value - sourceStart) * (targetEnd - targetStart) / (sourceEnd - sourceStart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamps the given value and remaps to the target interval.
|
||||
* <p>
|
||||
* Note the source interval values should be sorted.
|
||||
*/
|
||||
public static float mapValueWithClampBefore(
|
||||
float value,
|
||||
float sourceBottom,
|
||||
float sourceTop,
|
||||
float targetBottom,
|
||||
float targetTop
|
||||
) {
|
||||
return mapValue(
|
||||
clamp(value, sourceBottom, sourceTop),
|
||||
sourceBottom,
|
||||
sourceTop,
|
||||
targetBottom,
|
||||
targetTop
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remaps the given value to the target interval and clamps.
|
||||
* <p>
|
||||
* Note the target interval values should be sorted.
|
||||
*/
|
||||
public static float mapValueWithClampAfter(
|
||||
float value,
|
||||
float sourceBottom,
|
||||
float sourceTop,
|
||||
float targetBottom,
|
||||
float targetTop
|
||||
) {
|
||||
return clamp(
|
||||
mapValue(value, sourceBottom, sourceTop, targetBottom, targetTop),
|
||||
targetBottom,
|
||||
targetTop
|
||||
);
|
||||
}
|
||||
|
||||
public static float smoothstep(float edge0, float edge1, float x) {
|
||||
// Scale, bias and saturate x to 0..1 range
|
||||
x = FastMath.clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
|
||||
// Evaluate polynomial
|
||||
return x * x * (3f - 2f * x);
|
||||
}
|
||||
|
||||
public static float smootherstep(float edge0, float edge1, float x) {
|
||||
// Scale, and clamp x to 0..1 range
|
||||
x = FastMath.clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
|
||||
// Evaluate polynomial
|
||||
return x * x * x * (x * (x * 6f - 15f) + 10f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies linear contrast (with clamping).
|
||||
*
|
||||
* @param t - input value in range (0..1)
|
||||
* @param k - contrast factor in range (-1..1):
|
||||
* <ul>
|
||||
* <li>1.0 - maximal contrast</li>
|
||||
* <li><b>0.0</b> - bypass (returns input value)</li>
|
||||
* <li>-1.0 - minimal contrast (returns 0.5f for any input)</li>
|
||||
* </ul>
|
||||
* @return contrasted value in range (0..1)
|
||||
*/
|
||||
public static float contrastLinear(float t, float k) {
|
||||
float x = 2f * t - 1f; // -1..1
|
||||
float gamma = (1f + k) / (1f - k);
|
||||
float f = FastMath.clamp(gamma * x, -1f, 1f); // -1..1
|
||||
return 0.5f * (f + 1f); // 0..1
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies non-linear contrast by power function.
|
||||
*
|
||||
* @param t - input value in range (0..1)
|
||||
* @param k - contrast factor in range (-1..1) exclusive:
|
||||
* <ul>
|
||||
* <li>0.999 - maximal contrast</li>
|
||||
* <li>0.0 - bypass (returns input value)</li>
|
||||
* <li>-0.999 - minimal contrast</li>
|
||||
* </ul>
|
||||
* @return contrasted value in range (0..1)
|
||||
*/
|
||||
public static float contrastPower(float t, float k) {
|
||||
float x = 2f * t - 1f; // -1..1
|
||||
float gamma = (1f - k) / (1f + k);
|
||||
float f = FastMath.sign(x) * FastMath.pow(FastMath.abs(x), gamma); // -1..1
|
||||
return 0.5f * (f + 1f); // 0..1
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies non-linear contrast by square splines.
|
||||
*
|
||||
* @param t - input value in range (0..1)
|
||||
* @param k - contrast factor in range (-1..1):
|
||||
* <ul>
|
||||
* <li>1.0 - maximal contrast</li>
|
||||
* <li>0.0 - bypass (returns input value)</li>
|
||||
* <li>-1.0 - minimal contrast</li>
|
||||
* </ul>
|
||||
* @return contrasted value in range (0..1)
|
||||
*/
|
||||
public static float contrastQuadricSpline(float t, float k) {
|
||||
float x = 2f * t - 1f; // -1..1
|
||||
float f = x * (1f + k * (1f - FastMath.abs(x))); // -1..1
|
||||
return 0.5f * (f + 1f); // 0..1
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies non-linear contrast by square splines inverted function.
|
||||
*
|
||||
* @param t - input value in range (0..1)
|
||||
* @param k - contrast factor in range (-2..2):
|
||||
* <ul>
|
||||
* <li>2.0 - maximal contrast</li>
|
||||
* <li>0.0 - bypass (returns input value)</li>
|
||||
* <li>-2.0 - minimal contrast</li>
|
||||
* </ul>
|
||||
* @return contrasted value in range (0..1)
|
||||
*/
|
||||
public static float contrastInvertQuadricSpline(float t, float k) {
|
||||
float x = 2f * t - 1f; // -1..1
|
||||
float g;
|
||||
if (k > 0) {
|
||||
g = FastMath.sign(x) * FastMath.sqrt(FastMath.abs(x)) - 2f * x;
|
||||
} else {
|
||||
g = FastMath.sign(x) * (FastMath.sqrt(1f - FastMath.abs(x)) - 1f);
|
||||
}
|
||||
float f = (1f + k) * x + k * g; // -1..1
|
||||
return 0.5f * (f + 1f); // 0..1
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies non-linear contrast by cubic splines.
|
||||
*
|
||||
* @param t - input value in range (0..1)
|
||||
* @param k - contrast factor in range (-1..1):
|
||||
* <ul>
|
||||
* <li>1.0 - maximal contrast</li>
|
||||
* <li>0.0 - bypass (returns input value)</li>
|
||||
* <li>-1.0 - minimal contrast</li>
|
||||
* </ul>
|
||||
* @return contrasted value in range (0..1)
|
||||
*/
|
||||
public static float contrastCubicSpline(float t, float k) {
|
||||
float x = 2f * t - 1f; // -1..1
|
||||
float f = x * (1f + FastMath.abs(k) * (x * x - 1f));
|
||||
if (k < 0)
|
||||
f -= x * 3f * k * (1f - FastMath.abs(x));
|
||||
return 0.5f * (f + 1f); // 0..1
|
||||
}
|
||||
|
||||
public static float fraction(float f) {
|
||||
return f - (int) f;
|
||||
}
|
||||
|
||||
public static double fraction(double d) {
|
||||
return d - (long) d;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Do not copy {@link Math} methods.
|
||||
*/
|
||||
@Deprecated
|
||||
public static float min(float a, float b) {
|
||||
return a > b ? b : a;
|
||||
}
|
||||
|
||||
public static float min(float a, float b, float c) {
|
||||
return Math.min(Math.min(a, b), c);
|
||||
}
|
||||
|
||||
public static float min(float a, float b, float c, float d) {
|
||||
return Math.min(Math.min(a, b), Math.min(c, d));
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Do not copy {@link Math} methods.
|
||||
*/
|
||||
@Deprecated
|
||||
public static float max(float a, float b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
public static float max(float a, float b, float c) {
|
||||
return Math.max(Math.max(a, b), c);
|
||||
}
|
||||
|
||||
public static float max(float a, float b, float c, float d) {
|
||||
return Math.max(Math.max(a, b), Math.max(c, d));
|
||||
}
|
||||
|
||||
public static float cos(float value) {
|
||||
return (float) Math.cos(value);
|
||||
}
|
||||
|
||||
public static float sin(float value) {
|
||||
return (float) Math.sin(value);
|
||||
}
|
||||
|
||||
public static float ceil(float value) {
|
||||
return (float) Math.ceil(value);
|
||||
}
|
||||
|
||||
public static float floor(float value) {
|
||||
return (float) Math.floor(value);
|
||||
}
|
||||
|
||||
public static float pow(float value, float power) {
|
||||
return (float) Math.pow(value, power);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Do not copy {@link Math} methods.
|
||||
*/
|
||||
@Deprecated
|
||||
public static float abs(float value) {
|
||||
return (float) Math.abs(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Do not copy {@link Math} methods.
|
||||
*/
|
||||
@Deprecated
|
||||
public static float round(float value) {
|
||||
return (float) Math.round(value);
|
||||
}
|
||||
|
||||
public static float sqrt(float value) {
|
||||
return (float) Math.sqrt(value);
|
||||
}
|
||||
|
||||
public static float distance(float x0, float y0, float z0, float x1, float y1, float z1) {
|
||||
return distance(x1 - x0, y1 - y0, z1 - z0);
|
||||
}
|
||||
|
||||
public static float distance(float x, float y, float z) {
|
||||
return sqrt(sqrDistance(x, y, z));
|
||||
}
|
||||
|
||||
public static float sqrDistance(float x, float y, float z) {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
public static float distance(float x, float y) {
|
||||
return sqrt(sqrDistance(x, y));
|
||||
}
|
||||
|
||||
public static float sqrDistance(float x, float y) {
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
public static float sqrDistance(Vector3f v, float x1, float y1, float z1) {
|
||||
return sqrDistance(x1 - v.x, y1 - v.y, z1 - v.z);
|
||||
}
|
||||
|
||||
public static float sqrDistance(float x0, float y0, float z0, float x1, float y1, float z1) {
|
||||
return sqrDistance(x1 - x0, y1 - y0, z1 - z0);
|
||||
}
|
||||
|
||||
public static float hypot(float x, float y) {
|
||||
return FastMath.sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
public static float hypot(float x, float y, float z) {
|
||||
return FastMath.sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
/**
|
||||
* The same as FastMath.clamp
|
||||
*/
|
||||
public static float clamp(float value, float min, float max) {
|
||||
if (value <= min)
|
||||
return min;
|
||||
if (value >= max)
|
||||
return max;
|
||||
return value;
|
||||
}
|
||||
|
||||
public static Vector3f int2101010RevToFloats(int packedValue, Vector3f store) {
|
||||
if (store == null)
|
||||
store = new Vector3f();
|
||||
store.x = packedValue & TEN_BITS_MAX;
|
||||
if ((packedValue & TENTH_BIT) != 0)
|
||||
store.x *= -1;
|
||||
store.y = (packedValue >>> 10) & TEN_BITS_MAX;
|
||||
if ((packedValue & (TENTH_BIT << 10)) != 0)
|
||||
store.y *= -1;
|
||||
store.z = (packedValue >>> 20) & TEN_BITS_MAX;
|
||||
if ((packedValue & (TENTH_BIT << 20)) != 0)
|
||||
store.z *= -1;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static int floatToInt210101Rev(Vector3f values) {
|
||||
int store = 0;
|
||||
store |= ((int) values.x) & TEN_BITS_MAX;
|
||||
if (values.x < 0)
|
||||
store |= TENTH_BIT;
|
||||
store |= (((int) values.y) & TEN_BITS_MAX) << 10;
|
||||
if (values.y < 0)
|
||||
store |= TENTH_BIT << 10;
|
||||
store |= (((int) values.z) & TEN_BITS_MAX) << 20;
|
||||
if (values.z < 0)
|
||||
store |= TENTH_BIT << 20;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static int floatToInt210101RevNormalized(Vector3f values) {
|
||||
int store = 0;
|
||||
store |= ((int) (values.x * TEN_BITS)) & TEN_BITS_MAX;
|
||||
if (values.x < 0)
|
||||
store |= TENTH_BIT;
|
||||
store |= (((int) (values.y * TEN_BITS)) & TEN_BITS_MAX) << 10;
|
||||
if (values.y < 0)
|
||||
store |= TENTH_BIT << 10;
|
||||
store |= (((int) (values.z * TEN_BITS)) & TEN_BITS_MAX) << 20;
|
||||
if (values.z < 0)
|
||||
store |= TENTH_BIT << 20;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static int floatToUnsignedInt210101Rev(Vector3f values) {
|
||||
int store = 0;
|
||||
store |= ((int) values.x) & TEN_BITS;
|
||||
store |= (((int) values.y) & TEN_BITS) << 10;
|
||||
store |= (((int) values.z) & TEN_BITS) << 20;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static int floatToUnsignedInt210101RevNormalized(Vector3f values) {
|
||||
int store = 0;
|
||||
store |= ((int) (values.x * TEN_BITS)) & TEN_BITS;
|
||||
store |= (((int) (values.y * TEN_BITS)) & TEN_BITS) << 10;
|
||||
store |= (((int) (values.z * TEN_BITS)) & TEN_BITS) << 20;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static int floatToInt210101Rev(float x, float y, float z) {
|
||||
int store = 0;
|
||||
store |= ((int) x) & TEN_BITS_MAX;
|
||||
if (x < 0)
|
||||
store |= TENTH_BIT;
|
||||
store |= (((int) y) & TEN_BITS_MAX) << 10;
|
||||
if (y < 0)
|
||||
store |= TENTH_BIT << 10;
|
||||
store |= (((int) z) & TEN_BITS_MAX) << 20;
|
||||
if (z < 0)
|
||||
store |= TENTH_BIT << 20;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static int floatToUnsignedInt210101Rev(float x, float y, float z) {
|
||||
int store = 0;
|
||||
store |= ((int) x) & TEN_BITS;
|
||||
store |= (((int) y) & TEN_BITS) << 10;
|
||||
store |= (((int) z) & TEN_BITS) << 20;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static Vector4f int2101010RevToFloats(int packedValue, Vector4f store) {
|
||||
if (store == null)
|
||||
store = new Vector4f();
|
||||
store.x = packedValue & TEN_BITS_MAX;
|
||||
if ((packedValue & TENTH_BIT) != 0)
|
||||
store.x *= -1;
|
||||
store.y = (packedValue >>> 10) & TEN_BITS_MAX;
|
||||
if ((packedValue & (TENTH_BIT << 10)) != 0)
|
||||
store.y *= -1;
|
||||
store.z = (packedValue >>> 20) & TEN_BITS_MAX;
|
||||
if ((packedValue & (TENTH_BIT << 20)) != 0)
|
||||
store.z *= -1;
|
||||
store.w = (packedValue >>> 30) & TWO_BITS_MAX;
|
||||
if ((packedValue & (SECOND_BIT << 30)) != 0)
|
||||
store.w *= -1;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static int floatToInt210101Rev(Vector4f values) {
|
||||
int store = 0;
|
||||
store |= ((int) values.x) & TEN_BITS_MAX;
|
||||
if (values.x < 0)
|
||||
store |= TENTH_BIT;
|
||||
store |= (((int) values.y) & TEN_BITS_MAX) << 10;
|
||||
if (values.y < 0)
|
||||
store |= TENTH_BIT << 10;
|
||||
store |= (((int) values.z) & TEN_BITS_MAX) << 20;
|
||||
if (values.z < 0)
|
||||
store |= TENTH_BIT << 20;
|
||||
store |= (((int) values.z) & TWO_BITS_MAX) << 30;
|
||||
if (values.w < 0)
|
||||
store |= SECOND_BIT << 30;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static int floatToUnsignedInt210101Rev(Vector4f values) {
|
||||
int store = 0;
|
||||
store |= ((int) values.x) & TEN_BITS;
|
||||
store |= (((int) values.y) & TEN_BITS) << 10;
|
||||
store |= (((int) values.z) & TEN_BITS) << 20;
|
||||
store |= (((int) values.z) & TWO_BITS) << 30;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static Vector3f unsignedInt2101010RevToFloats(int packedValue, Vector3f store) {
|
||||
if (store == null)
|
||||
store = new Vector3f();
|
||||
store.x = packedValue & TEN_BITS;
|
||||
store.y = (packedValue >>> 10) & TEN_BITS;
|
||||
store.z = (packedValue >>> 20) & TEN_BITS;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static Vector4f unsignedInt2101010RevToFloats(int packedValue, Vector4f store) {
|
||||
if (store == null)
|
||||
store = new Vector4f();
|
||||
store.x = packedValue & TEN_BITS;
|
||||
store.y = (packedValue >>> 10) & TEN_BITS;
|
||||
store.z = (packedValue >>> 20) & TEN_BITS;
|
||||
store.w = (packedValue >>> 30) & TWO_BITS;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static Vector3f int2101010RevNormalizedToFloats(int packedValue, Vector3f store) {
|
||||
store = int2101010RevToFloats(packedValue, store);
|
||||
store.x /= TEN_BITS_MAX;
|
||||
store.y /= TEN_BITS_MAX;
|
||||
store.z /= TEN_BITS_MAX;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static Vector4f int2101010RevNormalizedToFloats(int packedValue, Vector4f store) {
|
||||
store = int2101010RevToFloats(packedValue, store);
|
||||
store.x /= TEN_BITS_MAX;
|
||||
store.y /= TEN_BITS_MAX;
|
||||
store.z /= TEN_BITS_MAX;
|
||||
store.w /= TWO_BITS_MAX;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static Vector3f unsignedInt2101010RevNormalizedToFloats(
|
||||
int packedValue,
|
||||
Vector3f store
|
||||
) {
|
||||
store = unsignedInt2101010RevToFloats(packedValue, store);
|
||||
store.x /= TEN_BITS;
|
||||
store.y /= TEN_BITS;
|
||||
store.z /= TEN_BITS;
|
||||
return store;
|
||||
}
|
||||
|
||||
public static Vector4f unsignedInt2101010RevNormalizedToFloats(
|
||||
int packedValue,
|
||||
Vector4f store
|
||||
) {
|
||||
store = unsignedInt2101010RevToFloats(packedValue, store);
|
||||
store.x /= TEN_BITS;
|
||||
store.y /= TEN_BITS;
|
||||
store.z /= TEN_BITS;
|
||||
store.w /= TWO_BITS;
|
||||
return store;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
package io.eiren.math;
|
||||
|
||||
import com.jme3.math.Vector3f;
|
||||
|
||||
|
||||
public class Vector3d implements Cloneable {
|
||||
|
||||
public double x;
|
||||
public double y;
|
||||
public double z;
|
||||
|
||||
public Vector3d() {
|
||||
}
|
||||
|
||||
public Vector3d(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public Vector3d(double x1, double y1, double z1, double x2, double y2, double z2) {
|
||||
this.x = x2 - x1;
|
||||
this.y = y2 - y1;
|
||||
this.z = z2 - z1;
|
||||
}
|
||||
|
||||
public Vector3d(Vector3f src) {
|
||||
this(src.x, src.y, src.z);
|
||||
}
|
||||
|
||||
public Vector3d set(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d set(Vector3d v) {
|
||||
this.x = v.x;
|
||||
this.y = v.y;
|
||||
this.z = v.z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d add(double addX, double addY, double addZ) {
|
||||
return new Vector3d(this.x + addX, this.y + addY, this.z + addZ);
|
||||
}
|
||||
|
||||
public Vector3d addLocal(Vector3d vec) {
|
||||
return addLocal(vec.x, vec.y, vec.z);
|
||||
}
|
||||
|
||||
public Vector3d addLocal(double addX, double addY, double addZ) {
|
||||
x += addX;
|
||||
y += addY;
|
||||
z += addZ;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d substract(double subX, double subY, double subZ) {
|
||||
return new Vector3d(this.x - subX, this.y - subY, this.z - subZ);
|
||||
}
|
||||
|
||||
public Vector3d substractLocal(Vector3d vec) {
|
||||
if (null == vec) {
|
||||
return null;
|
||||
}
|
||||
x -= vec.x;
|
||||
y -= vec.y;
|
||||
z -= vec.z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d substractLocal(double subX, double subY, double subZ) {
|
||||
x -= subX;
|
||||
y -= subY;
|
||||
z -= subZ;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d negate() {
|
||||
return new Vector3d(-x, -y, -z);
|
||||
}
|
||||
|
||||
public Vector3d negateLocal() {
|
||||
x = -x;
|
||||
y = -y;
|
||||
z = -z;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d mult(double scalar) {
|
||||
return new Vector3d(x * scalar, y * scalar, z * scalar);
|
||||
}
|
||||
|
||||
public Vector3d multLocal(double scalar) {
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
z *= scalar;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d divide(double scalar) {
|
||||
return new Vector3d(x / scalar, y / scalar, z / scalar);
|
||||
}
|
||||
|
||||
public Vector3d divideLocal(double scalar) {
|
||||
x /= scalar;
|
||||
y /= scalar;
|
||||
z /= scalar;
|
||||
return this;
|
||||
}
|
||||
|
||||
public double dot(Vector3d v) {
|
||||
return x * v.x + y * v.y + z * v.z;
|
||||
}
|
||||
|
||||
public double dot(double vx, double vy, double vz) {
|
||||
return x * vx + y * vy + z * vz;
|
||||
}
|
||||
|
||||
public Vector3d cross(Vector3d other, Vector3d result) {
|
||||
if (result == null)
|
||||
result = new Vector3d();
|
||||
double resX = ((y * other.z) - (z * other.y));
|
||||
double resY = ((z * other.x) - (x * other.z));
|
||||
double resZ = ((x * other.y) - (y * other.x));
|
||||
result.set(resX, resY, resZ);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector3d clone() {
|
||||
return new Vector3d(this.x, this.y, this.z);
|
||||
}
|
||||
|
||||
public Vector3d normalize() {
|
||||
double length = x * x + y * y + z * z;
|
||||
if (length != 1.0 && length != 0.0) {
|
||||
double invLength = 1.0 / Math.sqrt(length);
|
||||
return mult(invLength);
|
||||
}
|
||||
return clone();
|
||||
}
|
||||
|
||||
public Vector3d normalizeLocal() {
|
||||
double length = x * x + y * y + z * z;
|
||||
if (length != 1.0 && length != 0.0) {
|
||||
length = Math.sqrt(length);
|
||||
double invLength = 1.0 / length;
|
||||
x *= invLength;
|
||||
z *= invLength;
|
||||
y *= invLength;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3f toVector3f() {
|
||||
return new Vector3f((float) x, (float) y, (float) z);
|
||||
}
|
||||
|
||||
public double length() {
|
||||
return Math.sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
public double lengthSquared() {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringBuilder("Vector3D{")
|
||||
.append(x)
|
||||
.append(',')
|
||||
.append(y)
|
||||
.append(',')
|
||||
.append(z)
|
||||
.append('}')
|
||||
.toString();
|
||||
}
|
||||
|
||||
public void rotateAroundX(float f) {
|
||||
double f1 = Math.cos(f);
|
||||
double f2 = Math.sin(f);
|
||||
double d = x;
|
||||
double d1 = y * f1 + z * f2;
|
||||
double d2 = z * f1 - y * f2;
|
||||
x = (float) d;
|
||||
y = (float) d1;
|
||||
z = (float) d2;
|
||||
}
|
||||
|
||||
public void rotateAroundY(float f) {
|
||||
double f1 = Math.cos(f);
|
||||
double f2 = Math.sin(f);
|
||||
double d = x * f1 + z * f2;
|
||||
double d1 = y;
|
||||
double d2 = z * f1 - x * f2;
|
||||
x = (float) d;
|
||||
y = (float) d1;
|
||||
z = (float) d2;
|
||||
}
|
||||
|
||||
public double distanceTo(Vector3d vec3d) {
|
||||
return Math.sqrt(squaredDistance(vec3d));
|
||||
}
|
||||
|
||||
public double squaredDistance(Vector3d point) {
|
||||
return squaredDistance(point.x, point.y, point.z);
|
||||
}
|
||||
|
||||
public double squaredDistance(double toX, double toY, double toZ) {
|
||||
return (this.x - toX) * (this.x - toX)
|
||||
+ (this.y - toY) * (this.y - toY)
|
||||
+ (this.z - toZ) * (this.z - toZ);
|
||||
}
|
||||
|
||||
public Vector3d add(Vector3d dir) {
|
||||
return add(dir.x, dir.y, dir.z);
|
||||
}
|
||||
|
||||
public Vector3d substract(Vector3d dir) {
|
||||
return substract(dir.x, dir.y, dir.z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(x);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(y);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(z);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Vector3d other = (Vector3d) obj;
|
||||
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(z) != Double.doubleToLongBits(other.z))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user