|
|
|
@ -41,33 +41,56 @@ import java.util.Random; |
|
|
|
|
* @version $Id: FastMath.java,v 1.45 2007/08/26 08:44:20 irrisor Exp $ |
|
|
|
|
*/ |
|
|
|
|
final public class FastMath { |
|
|
|
|
|
|
|
|
|
private FastMath() { |
|
|
|
|
} |
|
|
|
|
/** A "close to zero" double epsilon value for use*/ |
|
|
|
|
/** |
|
|
|
|
* A "close to zero" double epsilon value for use |
|
|
|
|
*/ |
|
|
|
|
public static final double DBL_EPSILON = 2.220446049250313E-16d; |
|
|
|
|
/** A "close to zero" float epsilon value for use*/ |
|
|
|
|
/** |
|
|
|
|
* A "close to zero" float epsilon value for use |
|
|
|
|
*/ |
|
|
|
|
public static final float FLT_EPSILON = 1.1920928955078125E-7f; |
|
|
|
|
/** A "close to zero" float epsilon value for use*/ |
|
|
|
|
/** |
|
|
|
|
* A "close to zero" float epsilon value for use |
|
|
|
|
*/ |
|
|
|
|
public static final float ZERO_TOLERANCE = 0.0001f; |
|
|
|
|
public static final float ONE_THIRD = 1f / 3f; |
|
|
|
|
/** The value PI as a float. (180 degrees) */ |
|
|
|
|
/** |
|
|
|
|
* The value PI as a float. (180 degrees) |
|
|
|
|
*/ |
|
|
|
|
public static final float PI = (float) Math.PI; |
|
|
|
|
/** The value 2PI as a float. (360 degrees) */ |
|
|
|
|
/** |
|
|
|
|
* The value 2PI as a float. (360 degrees) |
|
|
|
|
*/ |
|
|
|
|
public static final float TWO_PI = 2.0f * PI; |
|
|
|
|
/** The value PI/2 as a float. (90 degrees) */ |
|
|
|
|
/** |
|
|
|
|
* The value PI/2 as a float. (90 degrees) |
|
|
|
|
*/ |
|
|
|
|
public static final float HALF_PI = 0.5f * PI; |
|
|
|
|
/** The value PI/4 as a float. (45 degrees) */ |
|
|
|
|
/** |
|
|
|
|
* The value PI/4 as a float. (45 degrees) |
|
|
|
|
*/ |
|
|
|
|
public static final float QUARTER_PI = 0.25f * PI; |
|
|
|
|
/** The value 1/PI as a float. */ |
|
|
|
|
/** |
|
|
|
|
* The value 1/PI as a float. |
|
|
|
|
*/ |
|
|
|
|
public static final float INV_PI = 1.0f / PI; |
|
|
|
|
/** The value 1/(2PI) as a float. */ |
|
|
|
|
/** |
|
|
|
|
* The value 1/(2PI) as a float. |
|
|
|
|
*/ |
|
|
|
|
public static final float INV_TWO_PI = 1.0f / TWO_PI; |
|
|
|
|
/** A value to multiply a degree value by, to convert it to radians. */ |
|
|
|
|
/** |
|
|
|
|
* A value to multiply a degree value by, to convert it to radians. |
|
|
|
|
*/ |
|
|
|
|
public static final float DEG_TO_RAD = PI / 180.0f; |
|
|
|
|
/** A value to multiply a radian value by, to convert it to degrees. */ |
|
|
|
|
/** |
|
|
|
|
* A value to multiply a radian value by, to convert it to degrees. |
|
|
|
|
*/ |
|
|
|
|
public static final float RAD_TO_DEG = 180.0f / PI; |
|
|
|
|
/** A precreated random object for random numbers. */ |
|
|
|
|
/** |
|
|
|
|
* A precreated random object for random numbers. |
|
|
|
|
*/ |
|
|
|
|
public static final Random rand = new Random(System.currentTimeMillis()); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -146,7 +169,8 @@ final public class FastMath { |
|
|
|
|
* @param store a vector3f to store the result |
|
|
|
|
* @return The interpolated value between startValue and endValue. |
|
|
|
|
*/ |
|
|
|
|
public static Vector3f interpolateLinear(float scale, Vector3f startValue, Vector3f endValue, Vector3f store) { |
|
|
|
|
public static Vector3f interpolateLinear(float scale, Vector3f startValue, |
|
|
|
|
Vector3f endValue, Vector3f store) { |
|
|
|
|
if (store == null) { |
|
|
|
|
store = new Vector3f(); |
|
|
|
|
} |
|
|
|
@ -177,6 +201,7 @@ final public class FastMath { |
|
|
|
|
* if scale is between 0 and 1 this method returns the same result as interpolateLinear |
|
|
|
|
* if the scale is over 1 the value is linearly extrapolated. |
|
|
|
|
* Note that the end value is the value for a scale of 1. |
|
|
|
|
* |
|
|
|
|
* @param scale the scale for extrapolation |
|
|
|
|
* @param startValue the starting value (scale = 0) |
|
|
|
|
* @param endValue the end value (scale = 1) |
|
|
|
@ -194,13 +219,15 @@ final public class FastMath { |
|
|
|
|
* if scale is between 0 and 1 this method returns the same result as interpolateLinear |
|
|
|
|
* if the scale is over 1 the value is linearly extrapolated. |
|
|
|
|
* Note that the end value is the value for a scale of 1. |
|
|
|
|
* |
|
|
|
|
* @param scale the scale for extrapolation |
|
|
|
|
* @param startValue the starting value (scale = 0) |
|
|
|
|
* @param endValue the end value (scale = 1) |
|
|
|
|
* @param store an initialized vector to store the return value |
|
|
|
|
* @return an extrapolation for the given parameters |
|
|
|
|
*/ |
|
|
|
|
public static Vector3f extrapolateLinear(float scale, Vector3f startValue, Vector3f endValue, Vector3f store) { |
|
|
|
|
public static Vector3f extrapolateLinear(float scale, Vector3f startValue, |
|
|
|
|
Vector3f endValue, Vector3f store) { |
|
|
|
|
if (store == null) { |
|
|
|
|
store = new Vector3f(); |
|
|
|
|
} |
|
|
|
@ -218,6 +245,7 @@ final public class FastMath { |
|
|
|
|
* if scale is between 0 and 1 this method returns the same result as interpolateLinear |
|
|
|
|
* if the scale is over 1 the value is linearly extrapolated. |
|
|
|
|
* Note that the end value is the value for a scale of 1. |
|
|
|
|
* |
|
|
|
|
* @param scale the scale for extrapolation |
|
|
|
|
* @param startValue the starting value (scale = 0) |
|
|
|
|
* @param endValue the end value (scale = 1) |
|
|
|
@ -227,7 +255,8 @@ final public class FastMath { |
|
|
|
|
return extrapolateLinear(scale, startValue, endValue, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**Interpolate a spline between at least 4 control points following the Catmull-Rom equation. |
|
|
|
|
/** |
|
|
|
|
* Interpolate a spline between at least 4 control points following the Catmull-Rom equation. |
|
|
|
|
* here is the interpolation matrix |
|
|
|
|
* m = [ 0.0 1.0 0.0 0.0 ] |
|
|
|
|
* [-T 0.0 T 0.0 ] |
|
|
|
@ -253,7 +282,8 @@ final public class FastMath { |
|
|
|
|
return ((c4 * u + c3) * u + c2) * u + c1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**Interpolate a spline between at least 4 control points following the Catmull-Rom equation. |
|
|
|
|
/** |
|
|
|
|
* Interpolate a spline between at least 4 control points following the Catmull-Rom equation. |
|
|
|
|
* here is the interpolation matrix |
|
|
|
|
* m = [ 0.0 1.0 0.0 0.0 ] |
|
|
|
|
* [-T 0.0 T 0.0 ] |
|
|
|
@ -270,7 +300,8 @@ final public class FastMath { |
|
|
|
|
* @param store a Vector3f to store the result |
|
|
|
|
* @return Catmull–Rom interpolation |
|
|
|
|
*/ |
|
|
|
|
public static Vector3f interpolateCatmullRom(float u, float T, Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3, Vector3f store) { |
|
|
|
|
public static Vector3f interpolateCatmullRom(float u, float T, Vector3f p0, |
|
|
|
|
Vector3f p1, Vector3f p2, Vector3f p3, Vector3f store) { |
|
|
|
|
if (store == null) { |
|
|
|
|
store = new Vector3f(); |
|
|
|
|
} |
|
|
|
@ -297,7 +328,8 @@ final public class FastMath { |
|
|
|
|
* @param p3 control point 3 |
|
|
|
|
* @return Catmull–Rom interpolation |
|
|
|
|
*/ |
|
|
|
|
public static Vector3f interpolateCatmullRom(float u, float T, Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3) { |
|
|
|
|
public static Vector3f interpolateCatmullRom(float u, float T, Vector3f p0, |
|
|
|
|
Vector3f p1, Vector3f p2, Vector3f p3) { |
|
|
|
|
return interpolateCatmullRom(u, T, p0, p1, p2, p3, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -326,7 +358,8 @@ final public class FastMath { |
|
|
|
|
+ p3 * u2 * u; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**Interpolate a spline between at least 4 control points following the Bezier equation. |
|
|
|
|
/** |
|
|
|
|
* Interpolate a spline between at least 4 control points following the Bezier equation. |
|
|
|
|
* here is the interpolation matrix |
|
|
|
|
* m = [ -1.0 3.0 -3.0 1.0 ] |
|
|
|
|
* [ 3.0 -6.0 3.0 0.0 ] |
|
|
|
@ -342,7 +375,8 @@ final public class FastMath { |
|
|
|
|
* @param store a Vector3f to store the result |
|
|
|
|
* @return Bezier interpolation |
|
|
|
|
*/ |
|
|
|
|
public static Vector3f interpolateBezier(float u, Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3, Vector3f store) { |
|
|
|
|
public static Vector3f interpolateBezier(float u, Vector3f p0, Vector3f p1, |
|
|
|
|
Vector3f p2, Vector3f p3, Vector3f store) { |
|
|
|
|
if (store == null) { |
|
|
|
|
store = new Vector3f(); |
|
|
|
|
} |
|
|
|
@ -352,7 +386,8 @@ final public class FastMath { |
|
|
|
|
return store; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**Interpolate a spline between at least 4 control points following the Bezier equation. |
|
|
|
|
/** |
|
|
|
|
* Interpolate a spline between at least 4 control points following the Bezier equation. |
|
|
|
|
* here is the interpolation matrix |
|
|
|
|
* m = [ -1.0 3.0 -3.0 1.0 ] |
|
|
|
|
* [ 3.0 -6.0 3.0 0.0 ] |
|
|
|
@ -373,6 +408,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Compute the length of a Catmull–Rom spline between control points 1 and 2 |
|
|
|
|
* |
|
|
|
|
* @param p0 control point 0 |
|
|
|
|
* @param p1 control point 1 |
|
|
|
|
* @param p2 control point 2 |
|
|
|
@ -382,7 +418,8 @@ final public class FastMath { |
|
|
|
|
* @param curveTension the curve tension |
|
|
|
|
* @return the length of the segment |
|
|
|
|
*/ |
|
|
|
|
public static float getCatmullRomP1toP2Length(Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3, float startRange, float endRange, float curveTension) { |
|
|
|
|
public static float getCatmullRomP1toP2Length(Vector3f p0, Vector3f p1, |
|
|
|
|
Vector3f p2, Vector3f p3, float startRange, float endRange, float curveTension) { |
|
|
|
|
|
|
|
|
|
float epsilon = 0.001f; |
|
|
|
|
float middleValue = (startRange + endRange) * 0.5f; |
|
|
|
@ -409,6 +446,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Compute the length on a Bezier spline between control points 1 and 2. |
|
|
|
|
* |
|
|
|
|
* @param p0 control point 0 |
|
|
|
|
* @param p1 control point 1 |
|
|
|
|
* @param p2 control point 2 |
|
|
|
@ -432,6 +470,7 @@ final public class FastMath { |
|
|
|
|
* Special cases: |
|
|
|
|
* <ul><li>If fValue is smaller than -1, then the result is PI. |
|
|
|
|
* <li>If the argument is greater than 1, then the result is 0.</ul> |
|
|
|
|
* |
|
|
|
|
* @param fValue The value to arc cosine. |
|
|
|
|
* @return The angle, in radians. |
|
|
|
|
* @see java.lang.Math#acos(double) |
|
|
|
@ -453,6 +492,7 @@ final public class FastMath { |
|
|
|
|
* Special cases: |
|
|
|
|
* <ul><li>If fValue is smaller than -1, then the result is -HALF_PI. |
|
|
|
|
* <li>If the argument is greater than 1, then the result is HALF_PI.</ul> |
|
|
|
|
* |
|
|
|
|
* @param fValue The value to arc sine. |
|
|
|
|
* @return the angle in radians. |
|
|
|
|
* @see java.lang.Math#asin(double) |
|
|
|
@ -471,6 +511,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the arc tangent of an angle given in radians.<br> |
|
|
|
|
* |
|
|
|
|
* @param fValue The angle, in radians. |
|
|
|
|
* @return fValue's atan |
|
|
|
|
* @see java.lang.Math#atan(double) |
|
|
|
@ -481,6 +522,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* A direct call to Math.atan2. |
|
|
|
|
* |
|
|
|
|
* @param fY |
|
|
|
|
* @param fX |
|
|
|
|
* @return Math.atan2(fY,fX) |
|
|
|
@ -492,6 +534,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Rounds a fValue up. A call to Math.ceil |
|
|
|
|
* |
|
|
|
|
* @param fValue The value. |
|
|
|
|
* @return The fValue rounded up |
|
|
|
|
* @see java.lang.Math#ceil(double) |
|
|
|
@ -502,6 +545,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns cosine of an angle. Direct call to java.lang.Math |
|
|
|
|
* |
|
|
|
|
* @see Math#cos(double) |
|
|
|
|
* @param v The angle to cosine. |
|
|
|
|
* @return the cosine of the angle. |
|
|
|
@ -512,6 +556,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the sine of an angle. Direct call to java.lang.Math |
|
|
|
|
* |
|
|
|
|
* @see Math#sin(double) |
|
|
|
|
* @param v The angle to sine. |
|
|
|
|
* @return the sine of the angle. |
|
|
|
@ -522,6 +567,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns E^fValue |
|
|
|
|
* |
|
|
|
|
* @param fValue Value to raise to a power. |
|
|
|
|
* @return The value E^fValue |
|
|
|
|
* @see java.lang.Math#exp(double) |
|
|
|
@ -532,6 +578,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns Absolute value of a float. |
|
|
|
|
* |
|
|
|
|
* @param fValue The value to abs. |
|
|
|
|
* @return The abs of the value. |
|
|
|
|
* @see java.lang.Math#abs(float) |
|
|
|
@ -545,6 +592,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns a number rounded down. |
|
|
|
|
* |
|
|
|
|
* @param fValue The value to round |
|
|
|
|
* @return The given number rounded down |
|
|
|
|
* @see java.lang.Math#floor(double) |
|
|
|
@ -555,6 +603,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns 1/sqrt(fValue) |
|
|
|
|
* |
|
|
|
|
* @param fValue The value to process. |
|
|
|
|
* @return 1/sqrt(fValue) |
|
|
|
|
* @see java.lang.Math#sqrt(double) |
|
|
|
@ -574,6 +623,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the log base E of a value. |
|
|
|
|
* |
|
|
|
|
* @param fValue The value to log. |
|
|
|
|
* @return The log of fValue base E |
|
|
|
|
* @see java.lang.Math#log(double) |
|
|
|
@ -585,6 +635,7 @@ final public class FastMath { |
|
|
|
|
/** |
|
|
|
|
* Returns the logarithm of value with given base, calculated as log(value)/log(base), |
|
|
|
|
* so that pow(base, return)==value (contributed by vear) |
|
|
|
|
* |
|
|
|
|
* @param value The value to log. |
|
|
|
|
* @param base Base of logarithm. |
|
|
|
|
* @return The logarithm of value with given base |
|
|
|
@ -595,6 +646,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns a number raised to an exponent power. fBase^fExponent |
|
|
|
|
* |
|
|
|
|
* @param fBase The base value (IE 2) |
|
|
|
|
* @param fExponent The exponent value (IE 3) |
|
|
|
|
* @return base raised to exponent (IE 8) |
|
|
|
@ -606,6 +658,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the value squared. fValue ^ 2 |
|
|
|
|
* |
|
|
|
|
* @param fValue The value to square. |
|
|
|
|
* @return The square of the given value. |
|
|
|
|
*/ |
|
|
|
@ -615,6 +668,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the square root of a given value. |
|
|
|
|
* |
|
|
|
|
* @param fValue The value to sqrt. |
|
|
|
|
* @return The square root of the given value. |
|
|
|
|
* @see java.lang.Math#sqrt(double) |
|
|
|
@ -626,6 +680,7 @@ final public class FastMath { |
|
|
|
|
/** |
|
|
|
|
* Returns the tangent of a value. If USE_FAST_TRIG is enabled, an approximate value |
|
|
|
|
* is returned. Otherwise, a direct value is used. |
|
|
|
|
* |
|
|
|
|
* @param fValue The value to tangent, in radians. |
|
|
|
|
* @return The tangent of fValue. |
|
|
|
|
* @see java.lang.Math#tan(double) |
|
|
|
@ -636,6 +691,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns 1 if the number is positive, -1 if the number is negative, and 0 otherwise |
|
|
|
|
* |
|
|
|
|
* @param iValue The integer to examine. |
|
|
|
|
* @return The integer's sign. |
|
|
|
|
*/ |
|
|
|
@ -651,6 +707,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns 1 if the number is positive, -1 if the number is negative, and 0 otherwise |
|
|
|
|
* |
|
|
|
|
* @param fValue The float to examine. |
|
|
|
|
* @return The float's sign. |
|
|
|
|
*/ |
|
|
|
@ -661,6 +718,7 @@ final public class FastMath { |
|
|
|
|
/** |
|
|
|
|
* Given 3 points in a 2d plane, this function computes if the points going from A-B-C |
|
|
|
|
* are moving counter clock wise. |
|
|
|
|
* |
|
|
|
|
* @param p0 Point 0. |
|
|
|
|
* @param p1 Point 1. |
|
|
|
|
* @param p2 Point 2. |
|
|
|
@ -690,6 +748,7 @@ final public class FastMath { |
|
|
|
|
/** |
|
|
|
|
* Test if a point is inside a triangle. 1 if the point is on the ccw side, |
|
|
|
|
* -1 if the point is on the cw side, and 0 if it is on neither. |
|
|
|
|
* |
|
|
|
|
* @param t0 First point of the triangle. |
|
|
|
|
* @param t1 Second point of the triangle. |
|
|
|
|
* @param t2 Third point of the triangle. |
|
|
|
@ -720,6 +779,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* A method that computes normal for a triangle defined by three vertices. |
|
|
|
|
* |
|
|
|
|
* @param v1 first vertex |
|
|
|
|
* @param v2 second vertex |
|
|
|
|
* @param v3 third vertex |
|
|
|
@ -996,6 +1056,7 @@ final public class FastMath { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Converts a range of min/max to a 0-1 range. |
|
|
|
|
* |
|
|
|
|
* @param value the value between min-max (inclusive). |
|
|
|
|
* @param min the minimum of the range. |
|
|
|
|
* @param max the maximum of the range. |
|
|
|
@ -1004,5 +1065,4 @@ final public class FastMath { |
|
|
|
|
public static float unInterpolateLinear(float value, float min, float max) { |
|
|
|
|
return (value - min) / (max - min); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|