public final class FastMath
extends java.lang.Object
FastMath
provides 'fast' math approximations and float equivalents of Math
functions. These are all used as static values and functions.Modifier and Type | Field and Description |
---|---|
static double |
DBL_EPSILON
A "close to zero" double epsilon value for use
|
static float |
DEG_TO_RAD
A value to multiply a degree value by, to convert it to radians.
|
static float |
FLT_EPSILON
A "close to zero" float epsilon value for use
|
static float |
HALF_PI
The value PI/2 as a float.
|
static float |
INV_PI
The value 1/PI as a float.
|
static float |
INV_TWO_PI
The value 1/(2PI) as a float.
|
static float |
ONE_THIRD
The value 1/3, as a float.
|
static float |
PI
The value PI as a float.
|
static float |
QUARTER_PI
The value PI/4 as a float.
|
static float |
RAD_TO_DEG
A value to multiply a radian value by, to convert it to degrees.
|
static java.util.Random |
rand
A precreated random object for random numbers.
|
static float |
TWO_PI
The value 2PI as a float.
|
static float |
ZERO_TOLERANCE
A "close to zero" float epsilon value for use
|
Modifier and Type | Method and Description |
---|---|
static float |
abs(float fValue)
Returns Absolute value of a float.
|
static float |
acos(float fValue)
Returns the arc cosine of a value.
Special cases: If fValue is smaller than -1, then the result is PI. |
static boolean |
approximateEquals(float a,
float b)
Determine if two floats are approximately equal.
|
static float |
asin(float fValue)
Returns the arc sine of a value.
Special cases: If fValue is smaller than -1, then the result is -HALF_PI. |
static float |
atan(float fValue)
Returns the arc tangent of an angle given in radians.
|
static float |
atan2(float fY,
float fX)
A direct call to Math.atan2.
|
static Vector3f |
cartesianToSpherical(Vector3f cartCoords,
Vector3f store)
Converts a point from Cartesian coordinates (using positive Y as up) to
Spherical and stores the results in the store var.
|
static Vector3f |
cartesianZToSpherical(Vector3f cartCoords,
Vector3f store)
Converts a point from Cartesian coordinates (using positive Z as up) to
Spherical and stores the results in the store var.
|
static float |
ceil(float fValue)
Rounds a fValue up.
|
static float |
clamp(float input,
float min,
float max)
Take a float input and clamp it between min and max.
|
static Vector3f |
computeNormal(Vector3f v1,
Vector3f v2,
Vector3f v3)
A method that computes normal for a triangle defined by three vertices.
|
static short |
convertFloatToHalf(float flt)
Convert a single-precision (32-bit) floating-point value
to half precision.
|
static float |
convertHalfToFloat(short half)
Converts a single precision (32 bit) floating point value
into half precision (16 bit).
|
static float |
copysign(float x,
float y) |
static float |
cos(float v)
Returns cosine of an angle.
|
static int |
counterClockwise(Vector2f p0,
Vector2f p1,
Vector2f p2)
Given 3 points in a 2d plane, this function computes if the points going from A-B-C
are moving counter clock wise.
|
static float |
determinant(double m00,
double m01,
double m02,
double m03,
double m10,
double m11,
double m12,
double m13,
double m20,
double m21,
double m22,
double m23,
double m30,
double m31,
double m32,
double m33)
Returns the determinant of a 4x4 matrix.
|
static float |
exp(float fValue)
Returns E^fValue
|
static float |
extrapolateLinear(float scale,
float startValue,
float endValue)
Linear extrapolation from startValue to endValue by the given scale.
|
static Vector3f |
extrapolateLinear(float scale,
Vector3f startValue,
Vector3f endValue)
Linear extrapolation from startValue to endValue by the given scale.
|
static Vector3f |
extrapolateLinear(float scale,
Vector3f startValue,
Vector3f endValue,
Vector3f store)
Linear extrapolation from startValue to endValue by the given scale.
|
static float |
fastInvSqrt(float x)
Quickly estimate 1/sqrt(fValue).
|
static float |
floor(float fValue)
Returns a number rounded down.
|
static float |
getBezierP1toP2Length(Vector3f p0,
Vector3f p1,
Vector3f p2,
Vector3f p3)
Compute the length on a Bezier spline between control points 1 and 2.
|
static float |
getCatmullRomP1toP2Length(Vector3f p0,
Vector3f p1,
Vector3f p2,
Vector3f p3,
float startRange,
float endRange,
float curveTension)
Compute the length of a Catmull–Rom spline between control points 1 and 2
|
static float |
interpolateBezier(float u,
float p0,
float p1,
float p2,
float p3)
Interpolate a spline between at least 4 control points following the Bezier equation.
|
static Vector3f |
interpolateBezier(float u,
Vector3f p0,
Vector3f p1,
Vector3f p2,
Vector3f p3)
Interpolate a spline between at least 4 control points following the Bezier equation.
|
static Vector3f |
interpolateBezier(float u,
Vector3f p0,
Vector3f p1,
Vector3f p2,
Vector3f p3,
Vector3f store)
Interpolate a spline between at least 4 control points following the Bezier equation.
|
static float |
interpolateCatmullRom(float u,
float T,
float p0,
float p1,
float p2,
float p3)
Interpolate a spline between at least 4 control points following the Catmull-Rom equation.
|
static Vector3f |
interpolateCatmullRom(float u,
float T,
Vector3f p0,
Vector3f p1,
Vector3f p2,
Vector3f p3)
Interpolate a spline between at least 4 control points using the
Catmull-Rom equation.
|
static Vector3f |
interpolateCatmullRom(float u,
float T,
Vector3f p0,
Vector3f p1,
Vector3f p2,
Vector3f p3,
Vector3f store)
Interpolate a spline between at least 4 control points following the Catmull-Rom equation.
|
static float |
interpolateLinear(float scale,
float startValue,
float endValue)
Linear interpolation from startValue to endValue by the given percent.
|
static Vector3f |
interpolateLinear(float scale,
Vector3f startValue,
Vector3f endValue)
Linear interpolation from startValue to endValue by the given percent.
|
static Vector3f |
interpolateLinear(float scale,
Vector3f startValue,
Vector3f endValue,
Vector3f store)
Linear interpolation from startValue to endValue by the given percent.
|
static float |
invSqrt(float fValue)
Returns 1/sqrt(fValue)
|
static boolean |
isPowerOfTwo(int number)
Returns true if the number is a power of 2 (2,4,8,16...)
A good implementation found on the Java boards.
|
static float |
log(float fValue)
Returns the log base E of a value.
|
static float |
log(float value,
float base)
Returns the logarithm of value with given base, calculated as log(value)/log(base),
so that pow(base, return)==value (contributed by vear)
|
static int |
nearestPowerOfTwo(int number)
Get the next power of two of the given number.
|
static float |
nextRandomFloat()
Returns a random float between 0 and 1.
|
static int |
nextRandomInt()
Choose a pseudo-random, uniformly-distributed integer value from
the shared generator.
|
static int |
nextRandomInt(int min,
int max)
Returns a random integer between min and max.
|
static float |
normalize(float val,
float min,
float max)
Takes a value and expresses it in terms of min to max.
|
static int |
pointInsideTriangle(Vector2f t0,
Vector2f t1,
Vector2f t2,
Vector2f p)
Test if a point is inside a triangle.
|
static float |
pow(float fBase,
float fExponent)
Returns a number raised to an exponent power.
|
static float |
saturate(float input)
Clamps the given float to be between 0 and 1.
|
static float |
sign(float fValue)
Returns 1 if the number is positive, -1 if the number is negative, and 0 otherwise
|
static int |
sign(int iValue)
Returns 1 if the number is positive, -1 if the number is negative, and 0 otherwise
|
static float |
sin(float v)
Returns the sine of an angle.
|
static Vector3f |
sphericalToCartesian(Vector3f sphereCoords,
Vector3f store)
Converts a point from Spherical coordinates to Cartesian (using positive
Y as up) and stores the results in the store var.
|
static Vector3f |
sphericalToCartesianZ(Vector3f sphereCoords,
Vector3f store)
Converts a point from Spherical coordinates to Cartesian (using positive
Z as up) and stores the results in the store var.
|
static float |
sqr(float fValue)
Returns the value squared.
|
static float |
sqrt(float fValue)
Returns the square root of a given value.
|
static float |
tan(float fValue)
Returns the tangent of the specified angle.
|
static float |
unInterpolateLinear(float value,
float min,
float max)
Converts a range of min/max to a 0-1 range.
|
public static final double DBL_EPSILON
public static final float FLT_EPSILON
public static final float ZERO_TOLERANCE
public static final float ONE_THIRD
public static final float PI
public static final float TWO_PI
public static final float HALF_PI
public static final float QUARTER_PI
public static final float INV_PI
public static final float INV_TWO_PI
public static final float DEG_TO_RAD
public static final float RAD_TO_DEG
public static final java.util.Random rand
public static boolean isPowerOfTwo(int number)
number
- The number to test.public static int nearestPowerOfTwo(int number)
number
- The number to obtain the POT for.public static float interpolateLinear(float scale, float startValue, float endValue)
scale
- scale value to use. if 1, use endValue, if 0, use startValue.startValue
- Beginning value. 0% of fendValue
- ending value. 100% of fpublic static Vector3f interpolateLinear(float scale, Vector3f startValue, Vector3f endValue, Vector3f store)
scale
- scale value to use. if 1, use endValue, if 0, use startValue.startValue
- Beginning value. 0% of fendValue
- ending value. 100% of fstore
- a vector3f to store the resultpublic static Vector3f interpolateLinear(float scale, Vector3f startValue, Vector3f endValue)
scale
- scale value to use. if 1, use endValue, if 0, use startValue.startValue
- Beginning value. 0% of fendValue
- ending value. 100% of fpublic static float extrapolateLinear(float scale, float startValue, float endValue)
scale
- the scale for extrapolationstartValue
- the starting value (scale = 0)endValue
- the end value (scale = 1)public static Vector3f extrapolateLinear(float scale, Vector3f startValue, Vector3f endValue, Vector3f store)
scale
- the scale for extrapolationstartValue
- the starting value (scale = 0)endValue
- the end value (scale = 1)store
- an initialized vector to store the return valuepublic static Vector3f extrapolateLinear(float scale, Vector3f startValue, Vector3f endValue)
scale
- the scale for extrapolationstartValue
- the starting value (scale = 0)endValue
- the end value (scale = 1)public static float interpolateCatmullRom(float u, float T, float p0, float p1, float p2, float p3)
u
- value from 0 to 1T
- The tension of the curvep0
- control point 0p1
- control point 1p2
- control point 2p3
- control point 3public static Vector3f interpolateCatmullRom(float u, float T, Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3, Vector3f store)
u
- value from 0 to 1T
- The tension of the curvep0
- control point 0p1
- control point 1p2
- control point 2p3
- control point 3store
- a Vector3f to store the resultpublic static Vector3f interpolateCatmullRom(float u, float T, Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3)
u
- value from 0 to 1T
- The tension of the curvep0
- control point 0p1
- control point 1p2
- control point 2p3
- control point 3public static float interpolateBezier(float u, float p0, float p1, float p2, float p3)
u
- value from 0 to 1p0
- control point 0p1
- control point 1p2
- control point 2p3
- control point 3public static Vector3f interpolateBezier(float u, Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3, Vector3f store)
u
- value from 0 to 1p0
- control point 0p1
- control point 1p2
- control point 2p3
- control point 3store
- a Vector3f to store the resultpublic static Vector3f interpolateBezier(float u, Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3)
u
- value from 0 to 1p0
- control point 0p1
- control point 1p2
- control point 2p3
- control point 3public static float getCatmullRomP1toP2Length(Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3, float startRange, float endRange, float curveTension)
p0
- control point 0p1
- control point 1p2
- control point 2p3
- control point 3startRange
- the starting range on the segment (use 0)endRange
- the end range on the segment (use 1)curveTension
- the curve tensionpublic static float getBezierP1toP2Length(Vector3f p0, Vector3f p1, Vector3f p2, Vector3f p3)
p0
- control point 0p1
- control point 1p2
- control point 2p3
- control point 3public static float acos(float fValue)
fValue
- The value to arc cosine.Math.acos(double)
public static float asin(float fValue)
fValue
- The value to arc sine.Math.asin(double)
public static float atan(float fValue)
fValue
- The angle, in radians.Math.atan(double)
public static float atan2(float fY, float fX)
fY
- fX
- Math.atan2(double, double)
public static float ceil(float fValue)
fValue
- The value.Math.ceil(double)
public static float cos(float v)
v
- The angle to cosine.Math.cos(double)
public static float sin(float v)
v
- The angle to sine.Math.sin(double)
public static float exp(float fValue)
fValue
- Value to raise to a power.Math.exp(double)
public static float abs(float fValue)
fValue
- The value to abs.Math.abs(float)
public static float floor(float fValue)
fValue
- The value to roundMath.floor(double)
public static float invSqrt(float fValue)
fValue
- The value to process.Math.sqrt(double)
public static float fastInvSqrt(float x)
x
- the input value (≥0)public static float log(float fValue)
fValue
- The value to log.Math.log(double)
public static float log(float value, float base)
value
- The value to log.base
- Base of logarithm.public static float pow(float fBase, float fExponent)
fBase
- The base value (IE 2)fExponent
- The exponent value (IE 3)Math.pow(double, double)
public static float sqr(float fValue)
fValue
- The value to square.public static float sqrt(float fValue)
fValue
- The value to sqrt.Math.sqrt(double)
public static float tan(float fValue)
fValue
- The value to tangent, in radians.Math.tan(double)
public static int sign(int iValue)
iValue
- The integer to examine.public static float sign(float fValue)
fValue
- The float to examine.public static int counterClockwise(Vector2f p0, Vector2f p1, Vector2f p2)
p0
- Point 0.p1
- Point 1.p2
- Point 2.public static int pointInsideTriangle(Vector2f t0, Vector2f t1, Vector2f t2, Vector2f p)
t0
- First point of the triangle.t1
- Second point of the triangle.t2
- Third point of the triangle.p
- The point to test.public static Vector3f computeNormal(Vector3f v1, Vector3f v2, Vector3f v3)
v1
- first vertexv2
- second vertexv3
- third vertexpublic static float determinant(double m00, double m01, double m02, double m03, double m10, double m11, double m12, double m13, double m20, double m21, double m22, double m23, double m30, double m31, double m32, double m33)
m00
- the element in row 0, column 0 of the matrixm01
- the element in row 0, column 1 of the matrixm02
- the element in row 0, column 2 of the matrixm03
- the element in row 0, column 3 of the matrixm10
- the element in row 1, column 0 of the matrixm11
- the element in row 1, column 1 of the matrixm12
- the element in row 1, column 2 of the matrixm13
- the element in row 1, column 3 of the matrixm20
- the element in row 2, column 0 of the matrixm21
- the element in row 2, column 1 of the matrixm22
- the element in row 2, column 2 of the matrixm23
- the element in row 2, column 3 of the matrixm30
- the element in row 3, column 0 of the matrixm31
- the element in row 3, column 1 of the matrixm32
- the element in row 3, column 2 of the matrixm33
- the element in row 3, column 3 of the matrixpublic static float nextRandomFloat()
public static int nextRandomInt(int min, int max)
min
- the desired minimum valuemax
- the desired maximum valuepublic static int nextRandomInt()
public static Vector3f sphericalToCartesian(Vector3f sphereCoords, Vector3f store)
sphereCoords
- the input spherical coordinates: x=distance from
origin, y=longitude in radians, z=latitude in radians (not null,
unaffected)store
- storage for the result (modified if not null)public static Vector3f cartesianToSpherical(Vector3f cartCoords, Vector3f store)
cartCoords
- the input Cartesian coordinates (not null, unaffected)store
- storage for the result (modified if not null)public static Vector3f sphericalToCartesianZ(Vector3f sphereCoords, Vector3f store)
sphereCoords
- the input spherical coordinates: x=distance from
origin, y=longitude in radians, z=latitude in radians (not null,
unaffected)store
- storage for the result (modified if not null)public static Vector3f cartesianZToSpherical(Vector3f cartCoords, Vector3f store)
cartCoords
- the input Cartesian coordinates (not null, unaffected)store
- storage for the result (modified if not null)public static float normalize(float val, float min, float max)
val
- -
the angle to normalize (in radians)min
- the lower limit of the rangemax
- the upper limit of the rangepublic static float copysign(float x, float y)
x
- the value whose sign is to be adjusted.y
- the value whose sign is to be used.public static float clamp(float input, float min, float max)
input
- min
- max
- public static float saturate(float input)
input
- public static boolean approximateEquals(float a, float b)
a
- The first float to compareb
- The second float to comparepublic static float convertHalfToFloat(short half)
Source: ftp://www.fox-toolkit.org/pub/fasthalffloatconversion.pdf
half
- The half floating point value as a short.public static short convertFloatToHalf(float flt)
flt
- the input value (not a NaN)java.lang.UnsupportedOperationException
- if flt is a NaNpublic static float unInterpolateLinear(float value, float min, float max)
value
- the value between min-max (inclusive).min
- the minimum of the range.max
- the maximum of the range.