Class MathUtils


  • public class MathUtils
    extends Object
    A utility class providing frequently used mathematical functions which are not found in the java.lang.Math class.

    All functions have been implemented with extreme caution in order to provide a maximum performance.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double DTOR
      Conversion factor for degrees to radians for the double data type.
      static float DTOR_F
      Conversion factor for degrees to radians for the float data type.
      static double EPS
      The epsilon value for the double data type.
      static float EPS_F
      The epsilon value for the float data type.
      static double HALFPI
      Pi half
      static double LOG10
      The natural logarithm of 10 as given by Math.logging(10)
      static double RTOD
      Conversion factor for radians to degrees for the double data type.
      static float RTOD_F
      Conversion factor for radians to degrees for the float data type.
    • Constructor Summary

      Constructors 
      Constructor Description
      MathUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int ceilInt​(double x)
      Returns (int) Math.ceil(x).
      static long ceilLong​(double x)
      Returns (long) Math.ceil(x).
      static double computeRoundFactor​(double min, double max, int numDigits)
      Computes a rounding factor suitable for the given value range and number of significant digits after the decimal point.
      static float computeRoundFactor​(float min, float max, int numDigits)
      Computes a rounding factor suitable for the given value range and number of significant digits.
      static byte[] createGammaCurve​(double gamma, byte[] f)
      Creates a quantized gamma (correction) curve for 256 samples in range from 0 to 1.
      static byte crop​(byte val, byte min, byte max)
      Crops the value to the range min to max.
      static double crop​(double val, double min, double max)
      Crops the value to the range min to max.
      static float crop​(float val, float min, float max)
      Crops the value to the range min to max.
      static int crop​(int val, int min, int max)
      Crops the value to the range min to max.
      static long crop​(long val, long min, long max)
      Crops the value to the range min to max.
      static short crop​(short val, short min, short max)
      Crops the value to the range min to max.
      static boolean equalValues​(double x1, double x2)
      Compares two double values for equality within the fixed epsilon.
      static boolean equalValues​(double x1, double x2, double eps)
      Compares two double values for equality within the given epsilon.
      static boolean equalValues​(float x1, float x2)
      Compares two float values for equality within the fixed epsilon.
      static boolean equalValues​(float x1, float x2, float eps)
      Compares two float values for equality within the given epsilon.
      static Dimension fitDimension​(int n, double a, double b)
      Computes an integer dimension for a given integer area that best fits the rectangle given by floating point width and height.
      static int floorAndCrop​(double x, int min, int max)
      First calls Math.floor with x and then crops the resulting value to the range min to max.
      static int floorInt​(double value)
      Returns (int) Math.floor(value).
      static long floorLong​(double x)
      Returns (long) Math.floor(x).
      static double getOrderOfMagnitude​(double x)
      Returns the order of magnitude for the value x.
      static double interpolate2D​(double wi, double wj, double x00, double x10, double x01, double x11)
      Performs a fast linear interpolation in two dimensions i and j.
      static float interpolate2D​(float wi, float wj, float x00, float x10, float x01, float x11)
      Performs a fast linear interpolation in two dimensions i and j.
      static double log10​(double x)
      Computes the common logarithm (to the base 10).
      static double round​(double x, double roundFactor)
      Computes a rounded value for the given rounding factor.
      static float round​(float x, float roundFactor)
      Computes a rounded value for the given rounding factor.
      static long roundAndCrop​(double value, long min, long max)
      First calls Math.round with value and then crops the resulting value to the range min to max.
      static int roundAndCrop​(float x, int min, int max)
      First calls Math.round with x and then crops the resulting value to the range min to max.
      static double sphereDistance​(double sphereRadius, double lambda1_rad, double phi1_rad, double lambda2_rad, double phi2_rad)  
      static double sphereDistanceDeg​(double sphereRadius, double lambda1_deg, double phi1_deg, double lambda2_deg, double phi2_deg)  
      static Rectangle[] subdivideRectangle​(int width, int height, int numTilesX, int numTilesY, int extraBorder)
      Subdivides a rectangle into tiles.
    • Field Detail

      • EPS_F

        public static final float EPS_F
        The epsilon value for the float data type. The exact value of this constant is 1.0E-6.
        See Also:
        Constant Field Values
      • EPS

        public static final double EPS
        The epsilon value for the double data type. The exact value of this constant is 1.0E-12.
        See Also:
        Constant Field Values
      • DTOR

        public static final double DTOR
        Conversion factor for degrees to radians for the double data type.
        See Also:
        Constant Field Values
      • RTOD

        public static final double RTOD
        Conversion factor for radians to degrees for the double data type.
        See Also:
        Constant Field Values
      • DTOR_F

        public static final float DTOR_F
        Conversion factor for degrees to radians for the float data type.
        See Also:
        Constant Field Values
      • RTOD_F

        public static final float RTOD_F
        Conversion factor for radians to degrees for the float data type.
        See Also:
        Constant Field Values
      • LOG10

        public static final double LOG10
        The natural logarithm of 10 as given by Math.logging(10)
    • Constructor Detail

      • MathUtils

        public MathUtils()
    • Method Detail

      • equalValues

        public static boolean equalValues​(float x1,
                                          float x2)
        Compares two float values for equality within the fixed epsilon.
        Parameters:
        x1 - the first value
        x2 - the second value
      • equalValues

        public static boolean equalValues​(double x1,
                                          double x2)
        Compares two double values for equality within the fixed epsilon.
        Parameters:
        x1 - the first value
        x2 - the second value
      • equalValues

        public static boolean equalValues​(float x1,
                                          float x2,
                                          float eps)
        Compares two float values for equality within the given epsilon.
        Parameters:
        x1 - the first value
        x2 - the second value
        eps - the maximum allowed difference
      • equalValues

        public static boolean equalValues​(double x1,
                                          double x2,
                                          double eps)
        Compares two double values for equality within the given epsilon.
        Parameters:
        x1 - the first value
        x2 - the second value
        eps - the maximum allowed difference
      • interpolate2D

        public static float interpolate2D​(float wi,
                                          float wj,
                                          float x00,
                                          float x10,
                                          float x01,
                                          float x11)
        Performs a fast linear interpolation in two dimensions i and j.
        Parameters:
        wi - weight in i-direction, a weight of 0.0 corresponds to i, 1.0 to i+1
        wj - weight in j-direction, a weight of 0.0 corresponds to j, 1.0 to j+1
        x00 - first anchor point located at (i,j)
        x10 - second anchor point located at (i+1,j)
        x01 - third anchor point located at (i,j+1)
        x11 - forth anchor point located at (i+1,j+1)
        Returns:
        the interpolated value
      • interpolate2D

        public static double interpolate2D​(double wi,
                                           double wj,
                                           double x00,
                                           double x10,
                                           double x01,
                                           double x11)
        Performs a fast linear interpolation in two dimensions i and j.
        Parameters:
        wi - weight in i-direction, a weight of 0.0 corresponds to i, 1.0 to i+1
        wj - weight in j-direction, a weight of 0.0 corresponds to j, 1.0 to j+1
        x00 - first anchor point located at (i,j)
        x10 - second anchor point located at (i+1,j)
        x01 - third anchor point located at (i,j+1)
        x11 - forth anchor point located at (i+1,j+1)
        Returns:
        the interpolated value
      • floorAndCrop

        public static int floorAndCrop​(double x,
                                       int min,
                                       int max)
        First calls Math.floor with x and then crops the resulting value to the range min to max.
      • roundAndCrop

        public static int roundAndCrop​(float x,
                                       int min,
                                       int max)
        First calls Math.round with x and then crops the resulting value to the range min to max.
      • roundAndCrop

        public static long roundAndCrop​(double value,
                                        long min,
                                        long max)
        First calls Math.round with value and then crops the resulting value to the range min to max.
        Parameters:
        value - the value to round and crop
        min - the minimum value of the crop range
        max - the maximum value of the crop range
        Returns:
        the rounded and cropped value
      • floorInt

        public static int floorInt​(double value)
        Returns (int) Math.floor(value).
        Parameters:
        value - the double value to be converted
        Returns:
        the integer value corresponding to the floor of value
      • floorLong

        public static long floorLong​(double x)
        Returns (long) Math.floor(x).
        Parameters:
        x - the value to be converted
        Returns:
        the long integer value corresponding to the floor of x
      • ceilInt

        public static int ceilInt​(double x)
        Returns (int) Math.ceil(x).
        Parameters:
        x - the value to be converted
        Returns:
        the integer value corresponding to the ceil of x
      • ceilLong

        public static long ceilLong​(double x)
        Returns (long) Math.ceil(x).
        Parameters:
        x - the value to be converted
        Returns:
        the long value corresponding to the ceil of x
      • computeRoundFactor

        public static float computeRoundFactor​(float min,
                                               float max,
                                               int numDigits)
        Computes a rounding factor suitable for the given value range and number of significant digits.
        Parameters:
        min - the minimum value of the range
        max - the maximum value of the range
        numDigits - the number of significant digits, must be =0
        Returns:
        the rounded value, always a power to the base 10
        See Also:
        round(float, float)
      • computeRoundFactor

        public static double computeRoundFactor​(double min,
                                                double max,
                                                int numDigits)
        Computes a rounding factor suitable for the given value range and number of significant digits after the decimal point.
        Parameters:
        min - the minimum value of the range
        max - the maximum value of the range
        numDigits - the number of significant digits after the decimal point, must be =0
        Returns:
        the rounded value, always a power to the base 10
        See Also:
        round(double, double)
      • round

        public static float round​(float x,
                                  float roundFactor)
        Computes a rounded value for the given rounding factor. The given value is pre-multiplied with the rounding factor, then rounded to the closest int and then again divided by the the rounding factor.

        The rounding factor can be computed for a given value range and accuracy with the computeRoundFactor method.

        Parameters:
        x - the value to be rounded
        roundFactor - the rounding factor specifying the accuracy, should always be a power to the base 10
        Returns:
        the rounded value
        See Also:
        computeRoundFactor(float, float, int)
      • round

        public static double round​(double x,
                                   double roundFactor)
        Computes a rounded value for the given rounding factor. The given value is pre-multiplied with the rounding factor, then rounded to the closest int and then again divided by the the rounding factor.

        The rounding factor can be computed for a given value range and accuracy with the computeRoundFactor method.

        Parameters:
        x - the value to be rounded
        roundFactor - the rounding factor specifying the accuracy, should always be a power to the base 10
        Returns:
        the rounded value
        See Also:
        computeRoundFactor(double, double, int)
      • getOrderOfMagnitude

        public static double getOrderOfMagnitude​(double x)
        Returns the order of magnitude for the value x.
        Parameters:
        x - the input
        Returns:
        the order of magnitude: Math.floor(log10(x))
      • log10

        public static double log10​(double x)
        Computes the common logarithm (to the base 10).
        Parameters:
        x - the input
        Returns:
        the common logarithm: Math.logging(x)/LOG10
      • createGammaCurve

        public static byte[] createGammaCurve​(double gamma,
                                              byte[] f)
        Creates a quantized gamma (correction) curve for 256 samples in range from 0 to 1. The array returned for can be used as a lookup table for gamma transformations. In order to interpret the value correctly use the following code snippet:
            byte[] f = MathUtils.createGammaCurve(gamma, null);
            for (int i = 0; i < 256; i++) {
                 // transform i --> j
                 j = f[i] & 0xff;
                 // now use j instead of i
            }
         
        Parameters:
        gamma - the gamma value, reasonable range is 1/10 to 10, if 1 then each f[i] & 0xff will be i.
        f - the curve as an array of length 256. If not null, the method used this array as returen value after values have been written into it. If null, the method creates a new array and returns it.
        Returns:
        a quantized gamma (correction) curve for 256 samples
      • crop

        public static byte crop​(byte val,
                                byte min,
                                byte max)
        Crops the value to the range min to max.
        Parameters:
        val - the value to crop
        min - the minimum crop limit
        max - the maximum crop limit
      • crop

        public static short crop​(short val,
                                 short min,
                                 short max)
        Crops the value to the range min to max.
        Parameters:
        val - the value to crop
        min - the minimum crop limit
        max - the maximum crop limit
      • crop

        public static int crop​(int val,
                               int min,
                               int max)
        Crops the value to the range min to max.
        Parameters:
        val - the value to crop
        min - the minimum crop limit
        max - the maximum crop limit
      • crop

        public static long crop​(long val,
                                long min,
                                long max)
        Crops the value to the range min to max.
        Parameters:
        val - the value to crop
        min - the minimum crop limit
        max - the maximum crop limit
      • crop

        public static float crop​(float val,
                                 float min,
                                 float max)
        Crops the value to the range min to max.
        Parameters:
        val - the value to crop
        min - the minimum crop limit
        max - the maximum crop limit
      • crop

        public static double crop​(double val,
                                  double min,
                                  double max)
        Crops the value to the range min to max.
        Parameters:
        val - the value to crop
        min - the minimum crop limit
        max - the maximum crop limit
      • fitDimension

        public static Dimension fitDimension​(int n,
                                             double a,
                                             double b)
        Computes an integer dimension for a given integer area that best fits the rectangle given by floating point width and height.
        Parameters:
        n - the integer area
        a - the rectangle's width
        b - the rectangle's height
        Returns:
        an integer dimension, never null
      • subdivideRectangle

        public static Rectangle[] subdivideRectangle​(int width,
                                                     int height,
                                                     int numTilesX,
                                                     int numTilesY,
                                                     int extraBorder)
        Subdivides a rectangle into tiles. The coordinates of each returned tile rectangle are guaranteed to be within the given rectangle.
        Parameters:
        width - the rectangle's width
        height - the rectangle's height
        numTilesX - the number of tiles in X direction
        numTilesY - the number of tiles in Y direction
        extraBorder - an extra border size to extend each tile
        Returns:
        the tile coordinates as rectangles
      • sphereDistanceDeg

        public static final double sphereDistanceDeg​(double sphereRadius,
                                                     double lambda1_deg,
                                                     double phi1_deg,
                                                     double lambda2_deg,
                                                     double phi2_deg)
        Parameters:
        sphereRadius - the radius of the sphere
        lambda1_deg - the lambda angle of point one, in degree
        phi1_deg - the phi angle of point one, in degree
        lambda2_deg - the lambda angle of point two, in degree
        phi2_deg - the phi angle of point one, in degree
        Returns:
        the distance described by the two given points (lambda/phi) on the top of sphere described by the given radius
      • sphereDistance

        public static final double sphereDistance​(double sphereRadius,
                                                  double lambda1_rad,
                                                  double phi1_rad,
                                                  double lambda2_rad,
                                                  double phi2_rad)
        Parameters:
        sphereRadius - the radius of the sphere
        lambda1_rad - the lambda angle of point one, in radians
        phi1_rad - the phi angle of point one, in radians
        lambda2_rad - the lambda angle of point two, in radians
        phi2_rad - the phi angle of point one, in radians
        Returns:
        the distance described by the two given points (lambda/phi) on the top of sphere described by the given radius