public class FloatUtil extends Object
Implementation assumes linear matrix layout in column-major order matching OpenGL's implementation, illustration:
Row-Major Column-Major (OpenGL): | 0 1 2 3 | | 0 4 8 12 | | | | | | 4 5 6 7 | | 1 5 9 13 | M = | | M = | | | 8 9 10 11 | | 2 6 10 14 | | | | | | 12 13 14 15 | | 3 7 11 15 |
See Matrix-FAQ
Derived from ProjectFloat.java - Created 11-jan-2004
Modifier and Type | Field and Description |
---|---|
static boolean |
DEBUG |
static float |
E |
static float |
EPSILON
Epsilon for floating point 1.1920928955078125E-7f, as once computed via
getMachineEpsilon() on an AMD-64 CPU. |
static float |
HALF_PI
The value PI/2, i.e.
|
static float |
PI
The value PI, i.e.
|
static float |
QUARTER_PI
The value PI/4, i.e.
|
static float |
SQUARED_PI
The value PI^2.
|
static float |
TWO_PI
The value 2PI, i.e.
|
Constructor and Description |
---|
FloatUtil() |
Modifier and Type | Method and Description |
---|---|
static float |
abs(float a) |
static float |
acos(float a) |
static float |
asin(float a) |
static float |
atan(float a) |
static float |
atan2(float y,
float x) |
static int |
compare(float a,
float b)
Return true if both values are equal w/o regarding an epsilon.
|
static int |
compare(float a,
float b,
float epsilon)
Return true if both values are equal, i.e.
|
static void |
copyMatrixColumn(float[] m_in,
int m_in_off,
int column,
float[] v_out,
int v_out_off)
Copy the named column of the given column-major matrix to v_out.
|
static void |
copyMatrixRow(float[] m_in,
int m_in_off,
int row,
float[] v_out,
int v_out_off)
Copy the named row of the given column-major matrix to v_out.
|
static float |
cos(float a) |
static float |
getMachineEpsilon()
Return computed machine Epsilon value.
|
static float |
getOrthoWinZ(float orthoZ,
float zNear,
float zFar)
Returns orthogonal distance
(1f/zNear-1f/orthoDist)/(1f/zNear-1f/zFar);
|
static float |
getZBufferEpsilon(int zBits,
float z,
float zNear)
Returns resolution of Z buffer of given parameter,
see Love Your Z-Buffer.
|
static int |
getZBufferValue(int zBits,
float z,
float zNear,
float zFar)
Returns Z buffer value of given parameter,
see Love Your Z-Buffer.
|
static boolean |
isEqual(float a,
float b)
Return true if both values are equal w/o regarding an epsilon.
|
static boolean |
isEqual(float a,
float b,
float epsilon)
Return true if both values are equal, i.e.
|
static boolean |
isZero(float a,
float epsilon)
Return true if value is zero, i.e.
|
static float[] |
makeFrustumf(float[] a,
int a_off,
boolean initA,
float left,
float right,
float bottom,
float top,
float zNear,
float zFar)
Make given matrix the frustum matrix based on given parameters.
|
static void |
makeIdentityf(float[] m,
int offset)
Make matrix an identity matrix
|
static void |
makeIdentityf(FloatBuffer m)
Make matrix an identity matrix
|
static float[] |
makeOrthof(float[] a,
int a_off,
boolean initA,
float left,
float right,
float bottom,
float top,
float zNear,
float zFar)
Make given matrix the orthogonal matrix based on given parameters.
|
static float[] |
makePerspective(float[] a,
int a_off,
boolean initA,
float fovy,
float aspect,
float zNear,
float zFar)
Make given matrix the perspective matrix based on given parameters.
|
static void |
makeRotationAxis(float angrad,
float x,
float y,
float z,
float[] mat,
int mat_offset,
float[] tmpVec3f)
Make a rotation matrix from the given axis and angle in radians.
|
static void |
makeRotationEuler(float bankX,
float headingY,
float attitudeZ,
float[] mat,
int mat_offset)
Make a concatenated rotation matrix in column-major order from the given Euler rotation angles in radians.
|
static void |
makeZero(float[] m,
int offset)
Make matrix an zero matrix
|
static void |
makeZero(FloatBuffer m)
Make matrix an zero matrix
|
static StringBuilder |
matrixRowToString(StringBuilder sb,
String f,
float[] a,
int aOffset,
int rows,
int columns,
boolean rowMajorOrder,
int row) |
static StringBuilder |
matrixRowToString(StringBuilder sb,
String f,
FloatBuffer a,
int aOffset,
int rows,
int columns,
boolean rowMajorOrder,
int row) |
static StringBuilder |
matrixToString(StringBuilder sb,
String rowPrefix,
String f,
float[] a,
int aOffset,
float[] b,
int bOffset,
int rows,
int columns,
boolean rowMajorOrder) |
static StringBuilder |
matrixToString(StringBuilder sb,
String rowPrefix,
String f,
float[] a,
int aOffset,
int rows,
int columns,
boolean rowMajorOrder) |
static StringBuilder |
matrixToString(StringBuilder sb,
String rowPrefix,
String f,
FloatBuffer a,
int aOffset,
FloatBuffer b,
int bOffset,
int rows,
int columns,
boolean rowMajorOrder) |
static StringBuilder |
matrixToString(StringBuilder sb,
String rowPrefix,
String f,
FloatBuffer a,
int aOffset,
int rows,
int columns,
boolean rowMajorOrder) |
static void |
multMatrixf(float[] a,
int a_off,
float[] b,
int b_off)
Multiply matrix: [a] = [a] x [b]
|
static void |
multMatrixf(float[] a,
int a_off,
float[] b,
int b_off,
float[] d,
int d_off)
Multiply matrix: [d] = [a] x [b]
|
static void |
multMatrixf(float[] a,
int a_off,
float[] b,
int b_off,
FloatBuffer d)
Multiply matrix: [d] = [a] x [b]
|
static void |
multMatrixf(FloatBuffer a,
float[] b,
int b_off)
Multiply matrix: [a] = [a] x [b]
|
static void |
multMatrixf(FloatBuffer a,
float[] b,
int b_off,
FloatBuffer d)
Multiply matrix: [d] = [a] x [b]
|
static void |
multMatrixf(FloatBuffer a,
FloatBuffer b)
Multiply matrix: [a] = [a] x [b]
|
static void |
multMatrixf(FloatBuffer a,
FloatBuffer b,
float[] d,
int d_off)
Multiply matrix: [d] = [a] x [b]
|
static void |
multMatrixf(FloatBuffer a,
FloatBuffer b,
FloatBuffer d)
Multiply matrix: [d] = [a] x [b]
|
static void |
multMatrixVecf(float[] m_in,
float[] v_in,
float[] v_out) |
static void |
multMatrixVecf(float[] m_in,
int m_in_off,
float[] v_in,
int v_in_off,
float[] v_out,
int v_out_off) |
static void |
multMatrixVecf(FloatBuffer m_in,
float[] v_in,
float[] v_out) |
static void |
multMatrixVecf(FloatBuffer m_in,
float[] v_in,
int v_in_off,
float[] v_out,
int v_out_off) |
static void |
multMatrixVecf(FloatBuffer m_in,
FloatBuffer v_in,
FloatBuffer v_out) |
static float |
pow(float a,
float b) |
static float |
sin(float a) |
static float |
sqrt(float a) |
static float |
tan(float a) |
public static final boolean DEBUG
public static final float E
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 SQUARED_PI
public static final float EPSILON
getMachineEpsilon()
on an AMD-64 CPU.
Definition of machine epsilon guarantees that:
1.0f + EPSILON != 1.0fIn other words: machEps is the maximum relative error of the chosen rounding procedure.
A number can be considered zero if it is in the range (or in the set):
MaybeZeroSet e ]-machEps .. machEps[ (exclusive)While comparing floating point values, machEps allows to clip the relative error:
boolean isZero = afloat < EPSILON; boolean isNotZero = afloat >= EPSILON; boolean isEqual = abs(bfloat - afloat) < EPSILON; boolean isNotEqual = abs(bfloat - afloat) >= EPSILON;
public static final void makeIdentityf(float[] m, int offset)
public static final void makeIdentityf(FloatBuffer m)
public static final void makeZero(float[] m, int offset)
public static final void makeZero(FloatBuffer m)
public static final void makeRotationAxis(float angrad, float x, float y, float z, float[] mat, int mat_offset, float[] tmpVec3f)
public static final void makeRotationEuler(float bankX, float headingY, float attitudeZ, float[] mat, int mat_offset)
The rotations are applied in the given order:
bankX
- the Euler pitch angle in radians. (rotation about the X axis)headingY
- the Euler yaw angle in radians. (rotation about the Y axis)attitudeZ
- the Euler roll angle in radians. (rotation about the Z axis)
Implementation does not use Quaternion and hence is exposed to Gimbal-Lock
public static final float[] makeOrthof(float[] a, int a_off, boolean initA, float left, float right, float bottom, float top, float zNear, float zFar)
a
- 4x4 matrix in column-major order (also result)a_off
- initA
- if true, given matrix will be initialized w/ identity matrix.left
- right
- bottom
- top
- zNear
- zFar
- public static final float[] makeFrustumf(float[] a, int a_off, boolean initA, float left, float right, float bottom, float top, float zNear, float zFar)
a
- 4x4 matrix in column-major order (also result)a_off
- initA
- if true, given matrix will be initialized w/ identity matrix.left
- right
- bottom
- top
- zNear
- zFar
- public static final float[] makePerspective(float[] a, int a_off, boolean initA, float fovy, float aspect, float zNear, float zFar)
a
- 4x4 matrix in column-major order (also result)a_off
- initA
- if true, given matrix will be initialized w/ identity matrix.fovy
- angle in radiansaspect
- zNear
- zFar
- public static final void multMatrixf(float[] a, int a_off, float[] b, int b_off, float[] d, int d_off)
a
- 4x4 matrix in column-major orderb
- 4x4 matrix in column-major orderd
- result a*b in column-major orderpublic static final void multMatrixf(float[] a, int a_off, float[] b, int b_off)
a
- 4x4 matrix in column-major order (also result)b
- 4x4 matrix in column-major orderpublic static final void multMatrixf(float[] a, int a_off, float[] b, int b_off, FloatBuffer d)
a
- 4x4 matrix in column-major orderb
- 4x4 matrix in column-major orderd
- result a*b in column-major orderpublic static final void multMatrixf(FloatBuffer a, float[] b, int b_off, FloatBuffer d)
a
- 4x4 matrix in column-major orderb
- 4x4 matrix in column-major orderd
- result a*b in column-major orderpublic static final void multMatrixf(FloatBuffer a, float[] b, int b_off)
a
- 4x4 matrix in column-major order (also result)b
- 4x4 matrix in column-major orderpublic static final void multMatrixf(FloatBuffer a, FloatBuffer b, FloatBuffer d)
a
- 4x4 matrix in column-major orderb
- 4x4 matrix in column-major orderd
- result a*b in column-major orderpublic static final void multMatrixf(FloatBuffer a, FloatBuffer b)
a
- 4x4 matrix in column-major order (also result)b
- 4x4 matrix in column-major orderpublic static final void multMatrixf(FloatBuffer a, FloatBuffer b, float[] d, int d_off)
a
- 4x4 matrix in column-major orderb
- 4x4 matrix in column-major orderd
- result a*b in column-major orderpublic static final void multMatrixVecf(float[] m_in, int m_in_off, float[] v_in, int v_in_off, float[] v_out, int v_out_off)
m_in
- 4x4 matrix in column-major orderm_in_off
- v_in
- 4-component column-vectorv_out
- m_in * v_inpublic static final void multMatrixVecf(float[] m_in, float[] v_in, float[] v_out)
m_in
- 4x4 matrix in column-major orderm_in_off
- v_in
- 4-component column-vectorv_out
- m_in * v_inpublic static final void multMatrixVecf(FloatBuffer m_in, float[] v_in, int v_in_off, float[] v_out, int v_out_off)
m_in
- 4x4 matrix in column-major orderv_in
- 4-component column-vectorv_out
- m_in * v_inpublic static final void multMatrixVecf(FloatBuffer m_in, float[] v_in, float[] v_out)
m_in
- 4x4 matrix in column-major orderv_in
- 4-component column-vectorv_out
- m_in * v_inpublic static final void multMatrixVecf(FloatBuffer m_in, FloatBuffer v_in, FloatBuffer v_out)
m_in
- 4x4 matrix in column-major orderv_in
- 4-component column-vectorv_out
- m_in * v_inpublic static final void copyMatrixColumn(float[] m_in, int m_in_off, int column, float[] v_out, int v_out_off)
v_out may be 3 or 4 components long, hence the 4th row may not be stored.
m_in
- input column-major matrixm_in_off
- offset to input matrixcolumn
- named column to copyv_out
- the column-vector storage, at least 3 components longv_out_off
- offset to storagepublic static final void copyMatrixRow(float[] m_in, int m_in_off, int row, float[] v_out, int v_out_off)
v_out may be 3 or 4 components long, hence the 4th column may not be stored.
m_in
- input column-major matrixm_in_off
- offset to input matrixrow
- named row to copyv_out
- the row-vector storage, at least 3 components longv_out_off
- offset to storagepublic static StringBuilder matrixRowToString(StringBuilder sb, String f, FloatBuffer a, int aOffset, int rows, int columns, boolean rowMajorOrder, int row)
sb
- optional passed StringBuilder instance to be usedf
- the format string of one floating point, i.e. "%10.5f", see Formatter
a
- mxn matrix (rows x columns)aOffset
- offset to a
's current positionrows
- columns
- rowMajorOrder
- if true floats are layed out in row-major-order, otherwise column-major-order (OpenGL)row
- row number to printpublic static StringBuilder matrixRowToString(StringBuilder sb, String f, float[] a, int aOffset, int rows, int columns, boolean rowMajorOrder, int row)
sb
- optional passed StringBuilder instance to be usedf
- the format string of one floating point, i.e. "%10.5f", see Formatter
a
- mxn matrix (rows x columns)aOffset
- offset to a
's current positionrows
- columns
- rowMajorOrder
- if true floats are layed out in row-major-order, otherwise column-major-order (OpenGL)row
- row number to printpublic static StringBuilder matrixToString(StringBuilder sb, String rowPrefix, String f, FloatBuffer a, int aOffset, int rows, int columns, boolean rowMajorOrder)
sb
- optional passed StringBuilder instance to be usedrowPrefix
- optional prefix for each rowf
- the format string of one floating point, i.e. "%10.5f", see Formatter
a
- mxn matrix (rows x columns)aOffset
- offset to a
's current positionrows
- columns
- rowMajorOrder
- if true floats are layed out in row-major-order, otherwise column-major-order (OpenGL)public static StringBuilder matrixToString(StringBuilder sb, String rowPrefix, String f, float[] a, int aOffset, int rows, int columns, boolean rowMajorOrder)
sb
- optional passed StringBuilder instance to be usedrowPrefix
- optional prefix for each rowf
- the format string of one floating point, i.e. "%10.5f", see Formatter
a
- mxn matrix (rows x columns)aOffset
- offset to a
's current positionrows
- columns
- rowMajorOrder
- if true floats are layed out in row-major-order, otherwise column-major-order (OpenGL)public static StringBuilder matrixToString(StringBuilder sb, String rowPrefix, String f, FloatBuffer a, int aOffset, FloatBuffer b, int bOffset, int rows, int columns, boolean rowMajorOrder)
sb
- optional passed StringBuilder instance to be usedrowPrefix
- optional prefix for each rowf
- the format string of one floating point, i.e. "%10.5f", see Formatter
a
- 4x4 matrix in column major order (OpenGL)aOffset
- offset to a
's current positionb
- 4x4 matrix in column major order (OpenGL)bOffset
- offset to a
's current positionrows
- columns
- rowMajorOrder
- if true floats are layed out in row-major-order, otherwise column-major-order (OpenGL)public static StringBuilder matrixToString(StringBuilder sb, String rowPrefix, String f, float[] a, int aOffset, float[] b, int bOffset, int rows, int columns, boolean rowMajorOrder)
sb
- optional passed StringBuilder instance to be usedrowPrefix
- optional prefix for each rowf
- the format string of one floating point, i.e. "%10.5f", see Formatter
a
- 4x4 matrix in column major order (OpenGL)aOffset
- offset to a
's current positionb
- 4x4 matrix in column major order (OpenGL)bOffset
- offset to a
's current positionrows
- columns
- rowMajorOrder
- if true floats are layed out in row-major-order, otherwise column-major-order (OpenGL)public static float getMachineEpsilon()
The machine Epsilon value is computed once.
On a reference machine the result was EPSILON
in 23 iterations.
EPSILON
public static boolean isEqual(float a, float b)
Implementation considers following corner cases:
isEqual(float, float, float)
public static boolean isEqual(float a, float b, float epsilon)
epsilon
.
Implementation considers following corner cases:
EPSILON
public static int compare(float a, float b)
Implementation considers following corner cases:
compare(float, float, float)
public static int compare(float a, float b, float epsilon)
epsilon
.
Implementation considers following corner cases:
EPSILON
public static boolean isZero(float a, float epsilon)
epsilon
.EPSILON
public static float abs(float a)
public static float pow(float a, float b)
public static float sin(float a)
public static float asin(float a)
public static float cos(float a)
public static float acos(float a)
public static float tan(float a)
public static float atan(float a)
public static float atan2(float y, float x)
public static float sqrt(float a)
public static float getZBufferEpsilon(int zBits, float z, float zNear)
return z * z / ( zNear * (1<<zBits) - z )
zBits
- number of bits of Z precision, i.e. z-buffer depthz
- distance from the eye to the objectzNear
- distance from eye to near clip planepublic static int getZBufferValue(int zBits, float z, float zNear, float zFar)
float a = zFar / ( zFar - zNear ) float b = zFar * zNear / ( zNear - zFar ) return (int) ( (1<<zBits) * ( a + b / z ) )
zBits
- number of bits of Z precision, i.e. z-buffer depthz
- distance from the eye to the objectzNear
- distance from eye to near clip planezFar
- distance from eye to far clip planepublic static float getOrthoWinZ(float orthoZ, float zNear, float zFar)
Copyright 2010 JogAmp Community.