Class MyMath

java.lang.Object
jme3utilities.math.MyMath

public class MyMath extends Object
Mathematical utility methods. TODO method to combine 2 transforms
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Pi/2
    static final Logger
    message logger for this class
    static final float
    golden ratio = 1.618...
    static final float
    square root of 2
    static final float
    square root of 1/2
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    area(com.jme3.math.Triangle triangle)
    Determine the area of the specified triangle.
    static boolean
    areWithinTolerance(float a, float b, float relativeTolerance)
    Test whether 2 float values are within the specified relative tolerance of one another.
    static double
    circle(double abscissa)
    Compute the circle function sqrt(1 - x^2) for a double-precision value.
    static float
    circle(float abscissa)
    Compute the circle function sqrt(1 - x^2) for a single-precision value.
    static double
    clamp(double dValue, double maxMagnitude)
    Clamp the magnitude of a double-precision value.
    static double
    clamp(double dValue, double min, double max)
    Clamp a double-precision value between 2 limits.
    static float
    clamp(float fValue, float maxMagnitude)
    Clamp the magnitude of a single-precision value.
    static int
    clamp(int iValue, int min, int max)
    Clamp an integer value between 2 limits.
    static float
    cube(float fValue)
    Cube the specified single-precision value.
    static float
    cubeRoot(float fValue)
    Extract the cube root of a single-precision value.
    static double
    discriminant(double a, double b, double c)
    Compute the discriminant (b^2 - 4*a*c) of a quadratic equation in standard form (a*x^2 + b*x + c).
    static float
    easeInQuartic(float time, float start, float end, float duration)
    Quartic easing function for animation: slow at the start, fast at the end.
    static float
    easeOutQuartic(float time, float start, float end, float duration)
    Quartic easing function for animation: fast at the start, slow at the end.
    static float
    fade(float t)
    Fade polynomial for Perlin noise.
    static double
    fourthRoot(double dValue)
    Extract the 4th root of a double-precision value.
    static float
    hypotenuse(float... fValues)
    Determine the root sum of squares of some single-precision values.
    static double
    hypotenuseDouble(double... dValues)
    Determine the root sum of squares of some double-precision values.
    static boolean
    isBetween(double a, double b, double c)
    Test whether b is between a and c.
    static boolean
    isBetween(float a, float b, float c)
    Test whether b is between a and c.
    static boolean
    isBetween(int a, int b, int c)
    Test whether b is between a and c.
    static boolean
    isIdentity(com.jme3.math.Transform transform)
    Test the specified transform for exact identity.
    static boolean
    isOdd(int iValue)
    Test whether an integer value is odd.
    static float
    lerp(float t, float y0, float y1)
    Interpolate between (or extrapolate from) 2 single-precision values using linear (Lerp) *polation.
    static float
    lerp3(float t1, float t2, float y0, float y1, float y2)
    Interpolate between (or extrapolate from) 3 single-precision values using linear (Lerp) *polation.
    static float
    max(float... fValues)
    Find the maximum of some single-precision values.
    static double
    maxDouble(double... dValues)
    Find the maximum of some double-precision values.
    static int
    maxInt(int... iValues)
    Find the maximum of some int values.
    static double
    mid(double a, double b, double c)
    Find the median of 3 double-precision values.
    static float
    mid(float a, float b, float c)
    Find the median of 3 single-precision values.
    static float
    min(float... fValues)
    Find the minimum of some single-precision values.
    static double
    minDouble(double... dValues)
    Find the minimum of some double-precision values.
    static double
    modulo(double dValue, double modulus)
    Compute the least non-negative value congruent with a double-precision value with respect to the specified modulus.
    static float
    modulo(float fValue, float modulus)
    Compute the least non-negative value congruent with a single-precision value with respect to the specified modulus.
    static int
    modulo(int iValue, int modulus)
    Compute the least non-negative value congruent with an integer value with respect to the specified modulus.
    static com.jme3.math.Transform
    slerp(float t, com.jme3.math.Transform t0, com.jme3.math.Transform t1, com.jme3.math.Transform storeResult)
    Interpolate between 2 transforms using spherical linear (Slerp) interpolation.
    static double
    sqr(double dValue)
    Square the specified double-precision value.
    static float
    standardize(float fValue)
    Standardize a single-precision value in preparation for hashing.
    static float
    standardizeAngle(float angle)
    Standardize a rotation angle to the range [-Pi, Pi).
    static double
    sumOfSquares(float... fValues)
    Compute the sum of squares of some single-precision values.
    static float
    toDegrees(float radians)
    Convert an angle from radians to degrees.
    static float
    toRadians(float degrees)
    Convert an angle from degrees to radians.
    static com.jme3.math.Triangle
    transformInverse(com.jme3.math.Transform transform, com.jme3.math.Triangle input, com.jme3.math.Triangle storeResult)
    Apply the inverse of the specified transform to each vertex of a Triangle.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • halfPi

      public static final double halfPi
      Pi/2
      See Also:
    • phi

      public static final float phi
      golden ratio = 1.618...
    • root2

      public static final float root2
      square root of 2
    • rootHalf

      public static final float rootHalf
      square root of 1/2
    • logger

      public static final Logger logger
      message logger for this class
  • Method Details

    • area

      public static double area(com.jme3.math.Triangle triangle)
      Determine the area of the specified triangle.
      Parameters:
      triangle - (not null, unaffected)
      Returns:
      the area (≥0)
    • areWithinTolerance

      public static boolean areWithinTolerance(float a, float b, float relativeTolerance)
      Test whether 2 float values are within the specified relative tolerance of one another.
      Parameters:
      a - the first input value
      b - the 2nd input value
      relativeTolerance - the relative tolerance (≥0, 0.02 → 2 percent)
      Returns:
      true if a and b differ by less than relativeTolerance * max(abs(a), abs(b)), otherwise false
    • circle

      public static double circle(double abscissa)
      Compute the circle function sqrt(1 - x^2) for a double-precision value.
      Parameters:
      abscissa - input (≤1, ≥-1)
      Returns:
      positive ordinate of the unit circle at the abscissa (≤1, ≥0)
    • circle

      public static float circle(float abscissa)
      Compute the circle function sqrt(1 - x^2) for a single-precision value. Double-precision arithmetic is used to reduce the risk of overflow.
      Parameters:
      abscissa - input (≤1, ≥-1)
      Returns:
      positive ordinate of the unit circle at the abscissa (≤1, ≥0)
    • clamp

      public static float clamp(float fValue, float maxMagnitude)
      Clamp the magnitude of a single-precision value.
      Parameters:
      fValue - input value to be clamped
      maxMagnitude - limit of the clamp (≥0)
      Returns:
      value between -maxMagnitude and +maxMagnitude inclusive which is closest to fValue
      See Also:
      • FastMath.clamp(float,float,float)
    • clamp

      public static double clamp(double dValue, double maxMagnitude)
      Clamp the magnitude of a double-precision value.
      Parameters:
      dValue - input value to be clamped
      maxMagnitude - limit of the clamp (≥0)
      Returns:
      value between -maxMagnitude and +maxMagnitude inclusive which is closest to fValue
      See Also:
      • FastMath.clamp(float,float,float)
    • clamp

      public static double clamp(double dValue, double min, double max)
      Clamp a double-precision value between 2 limits.
      Parameters:
      dValue - input value to be clamped
      min - lower limit of the clamp
      max - upper limit of the clamp
      Returns:
      the value between min and max inclusive that is closest to fValue
      See Also:
      • FastMath.clamp(float,float,float)
    • clamp

      public static int clamp(int iValue, int min, int max)
      Clamp an integer value between 2 limits.
      Parameters:
      iValue - input value to be clamped
      min - the lower limit
      max - the upper limit
      Returns:
      the value between min and max inclusive that is closest to iValue
    • cube

      public static float cube(float fValue)
      Cube the specified single-precision value. Logs a warning in case of overflow.
      Parameters:
      fValue - input value to be cubed
      Returns:
      fValue raised to the third power
      See Also:
    • cubeRoot

      public static float cubeRoot(float fValue)
      Extract the cube root of a single-precision value. Unlike FastMath.pow(float,float), this method works on negative values.
      Parameters:
      fValue - input cube to be extracted (may be negative)
      Returns:
      cube root of fValue
      See Also:
    • discriminant

      public static double discriminant(double a, double b, double c)
      Compute the discriminant (b^2 - 4*a*c) of a quadratic equation in standard form (a*x^2 + b*x + c).
      Parameters:
      a - coefficient of the square term
      b - coefficient of the linear term
      c - constant term
      Returns:
      discriminant
    • easeInQuartic

      public static float easeInQuartic(float time, float start, float end, float duration)
      Quartic easing function for animation: slow at the start, fast at the end.
      Parameters:
      time - the elapsed time since the start of the animation (≥0, ≤duration)
      start - the starting value
      end - the ending value
      duration - the duration of the animation (>0)
      Returns:
      the current value
    • easeOutQuartic

      public static float easeOutQuartic(float time, float start, float end, float duration)
      Quartic easing function for animation: fast at the start, slow at the end.
      Parameters:
      time - the elapsed time since the start of the animation (≥0, ≤duration)
      start - the starting value
      end - the ending value
      duration - the duration of the animation (>0)
      Returns:
      the current value
    • fade

      public static float fade(float t)
      Fade polynomial for Perlin noise. Double-precision arithmetic is used to reduce rounding error.
      Parameters:
      t - input value (≤1, ≥0)
      Returns:
      6*t^5 - 15*t^4 + 10*t^3 (≤1, ≥0)
    • fourthRoot

      public static double fourthRoot(double dValue)
      Extract the 4th root of a double-precision value. This method is faster than Math.pow(d, 0.25).
      Parameters:
      dValue - input 4th power to be extracted (≥0)
      Returns:
      the positive 4th root of dValue (≥0)
      See Also:
    • hypotenuse

      public static float hypotenuse(float... fValues)
      Determine the root sum of squares of some single-precision values. Double-precision arithmetic is used to reduce the risk of overflow.
      Parameters:
      fValues - the input values
      Returns:
      the positive square root of the sum of squares (≥0)
    • hypotenuseDouble

      public static double hypotenuseDouble(double... dValues)
      Determine the root sum of squares of some double-precision values.
      Parameters:
      dValues - the input values
      Returns:
      the positive square root of the sum of squares (≥0)
    • isBetween

      public static boolean isBetween(int a, int b, int c)
      Test whether b is between a and c.
      Parameters:
      a - the first input value
      b - the 2nd input value
      c - the 3rd input value
      Returns:
      true if b is between a and c (inclusive), otherwise false
    • isBetween

      public static boolean isBetween(float a, float b, float c)
      Test whether b is between a and c.
      Parameters:
      a - the first input value
      b - the 2nd input value
      c - the 3rd input value
      Returns:
      true if b is between a and c (inclusive), otherwise false
    • isBetween

      public static boolean isBetween(double a, double b, double c)
      Test whether b is between a and c.
      Parameters:
      a - the first input value
      b - the 2nd input value
      c - the 3rd input value
      Returns:
      true if b is between a and c (inclusive), otherwise false
    • isIdentity

      public static boolean isIdentity(com.jme3.math.Transform transform)
      Test the specified transform for exact identity.
      Parameters:
      transform - which transform to test (not null, unaffected)
      Returns:
      true if exact identity, otherwise false
    • isOdd

      public static boolean isOdd(int iValue)
      Test whether an integer value is odd.
      Parameters:
      iValue - input value to be tested
      Returns:
      true if x is odd, false if it's even
    • lerp

      public static float lerp(float t, float y0, float y1)
      Interpolate between (or extrapolate from) 2 single-precision values using linear (Lerp) *polation. Unlike FastMath.interpolateLinear(float, float, float), no rounding error is introduced when y0==y1.
      Parameters:
      t - descaled parameter value (0→y0, 1→y1)
      y0 - function value at t=0
      y1 - function value at t=1
      Returns:
      an interpolated function value
    • lerp3

      public static float lerp3(float t1, float t2, float y0, float y1, float y2)
      Interpolate between (or extrapolate from) 3 single-precision values using linear (Lerp) *polation.
      Parameters:
      t1 - the descaled parameter value from y0 to y1
      t2 - the descaled parameter value from y0 to y2
      y0 - function value at t1=0, t2=0
      y1 - function value at t1=1, t2=0
      y2 - function value at t1=0, t2=1
      Returns:
      an interpolated function value
    • max

      public static float max(float... fValues)
      Find the maximum of some single-precision values.
      Parameters:
      fValues - the input values
      Returns:
      the most positive value
      See Also:
    • maxDouble

      public static double maxDouble(double... dValues)
      Find the maximum of some double-precision values.
      Parameters:
      dValues - the input values
      Returns:
      the most positive value
      See Also:
    • maxInt

      public static int maxInt(int... iValues)
      Find the maximum of some int values.
      Parameters:
      iValues - the input values
      Returns:
      the most positive value
      See Also:
    • mid

      public static float mid(float a, float b, float c)
      Find the median of 3 single-precision values.
      Parameters:
      a - the first input value
      b - the 2nd input value
      c - the 3rd input value
      Returns:
      the median of the 3 values
    • mid

      public static double mid(double a, double b, double c)
      Find the median of 3 double-precision values.
      Parameters:
      a - the first input value
      b - the 2nd input value
      c - the 3rd input value
      Returns:
      the median of the 3 values
    • min

      public static float min(float... fValues)
      Find the minimum of some single-precision values.
      Parameters:
      fValues - the input values
      Returns:
      the most negative value
      See Also:
    • minDouble

      public static double minDouble(double... dValues)
      Find the minimum of some double-precision values.
      Parameters:
      dValues - the input values
      Returns:
      the most negative value
      See Also:
    • modulo

      public static int modulo(int iValue, int modulus)
      Compute the least non-negative value congruent with an integer value with respect to the specified modulus. modulo() differs from remainder for negative values of the first argument. For instance, modulo(-1, 4) == 3, while -1 % 4 == -1.
      Parameters:
      iValue - input value
      modulus - (>0)
      Returns:
      iValue MOD modulus (<modulus, ≥0)
    • modulo

      public static float modulo(float fValue, float modulus)
      Compute the least non-negative value congruent with a single-precision value with respect to the specified modulus. modulo() differs from remainder for negative values of the first argument. For instance, modulo(-1f, 4f) == 3f, while -1f % 4f == -1f.
      Parameters:
      fValue - input value
      modulus - (>0)
      Returns:
      fValue MOD modulus (<modulus, ≥0)
    • modulo

      public static double modulo(double dValue, double modulus)
      Compute the least non-negative value congruent with a double-precision value with respect to the specified modulus. modulo() differs from remainder for negative values of the first argument. For instance, modulo(-1, 4) == 3, while -1 % 4 == -1.
      Parameters:
      dValue - input value
      modulus - (>0)
      Returns:
      dValue MOD modulus (<modulus, ≥0)
    • slerp

      public static com.jme3.math.Transform slerp(float t, com.jme3.math.Transform t0, com.jme3.math.Transform t1, com.jme3.math.Transform storeResult)
      Interpolate between 2 transforms using spherical linear (Slerp) interpolation. This method is slower (but more accurate) than Transform.interpolateTransforms(com.jme3.math.Transform, com.jme3.math.Transform, float) and doesn't trash t1. The caller is responsible for flipping quaternion signs when it's appropriate to do so.
      Parameters:
      t - descaled parameter value (≥0, ≤1)
      t0 - function value at t=0 (not null, unaffected unless it's also storeResult)
      t1 - function value at t=1 (not null, unaffected unless it's also storeResult)
      storeResult - storage for the result (modified if not null, may be t0 or t1)
      Returns:
      an interpolated transform (either storeResult or a new instance)
    • sqr

      public static double sqr(double dValue)
      Square the specified double-precision value. Logs a warning in case of overflow.
      Parameters:
      dValue - input value to be squared
      Returns:
      dValue squared (≥0)
      See Also:
    • standardize

      public static float standardize(float fValue)
      Standardize a single-precision value in preparation for hashing.
      Parameters:
      fValue - input value
      Returns:
      an equivalent value that's not -0
    • standardizeAngle

      public static float standardizeAngle(float angle)
      Standardize a rotation angle to the range [-Pi, Pi).
      Parameters:
      angle - input (in radians)
      Returns:
      standardized angle (in radians, <Pi, ≥-Pi)
    • sumOfSquares

      public static double sumOfSquares(float... fValues)
      Compute the sum of squares of some single-precision values. Double-precision arithmetic is used to reduce the risk of overflow.
      Parameters:
      fValues - the input values
      Returns:
      the sum of squares (≥0)
    • toDegrees

      public static float toDegrees(float radians)
      Convert an angle from radians to degrees.
      Parameters:
      radians - input angle
      Returns:
      equivalent in degrees
      See Also:
    • toRadians

      public static float toRadians(float degrees)
      Convert an angle from degrees to radians.
      Parameters:
      degrees - input angle
      Returns:
      equivalent in radians
      See Also:
    • transformInverse

      public static com.jme3.math.Triangle transformInverse(com.jme3.math.Transform transform, com.jme3.math.Triangle input, com.jme3.math.Triangle storeResult)
      Apply the inverse of the specified transform to each vertex of a Triangle.
      Parameters:
      transform - the transform to use (not null, unaffected)
      input - the input triangle (not null, unaffected unless it's storeResult
      storeResult - storage for the result (modified if not null)
      Returns:
      the transformed triangle (either storeResult or a new instance)