Package org.esa.snap.core.util.math
Class MathUtils
java.lang.Object
org.esa.snap.core.util.math.MathUtils
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
FieldsModifier and TypeFieldDescriptionstatic final doubleConversion factor for degrees to radians for thedoubledata type.static final floatConversion factor for degrees to radians for thefloatdata type.static final doubleThe epsilon value for thedoubledata type.static final floatThe epsilon value for thefloatdata type.static final doublePi halfstatic final doubleThe natural logarithm of 10 as given byMath.logging(10)static final doubleConversion factor for radians to degrees for thedoubledata type.static final floatConversion factor for radians to degrees for thefloatdata type. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intceilInt(double x) Returns(int) Math.ceil(x).static longceilLong(double x) Returns(long) Math.ceil(x).static doublecomputeRoundFactor(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 floatcomputeRoundFactor(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 from0to1.static bytecrop(byte val, byte min, byte max) Crops the value to the rangemintomax.static doublecrop(double val, double min, double max) Crops the value to the rangemintomax.static floatcrop(float val, float min, float max) Crops the value to the rangemintomax.static intcrop(int val, int min, int max) Crops the value to the rangemintomax.static longcrop(long val, long min, long max) Crops the value to the rangemintomax.static shortcrop(short val, short min, short max) Crops the value to the rangemintomax.static booleanequalValues(double x1, double x2) Compares two double values for equality within the fixed epsilon.static booleanequalValues(double x1, double x2, double eps) Compares two double values for equality within the given epsilon.static booleanequalValues(float x1, float x2) Compares two float values for equality within the fixed epsilon.static booleanequalValues(float x1, float x2, float eps) Compares two float values for equality within the given epsilon.static DimensionfitDimension(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 intfloorAndCrop(double x, int min, int max) First callsMath.floorwithxand then crops the resulting value to the rangemintomax.static intfloorInt(double value) Returns(int) Math.floor(value).static longfloorLong(double x) Returns(long) Math.floor(x).static doublegetOrderOfMagnitude(double x) Returns the order of magnitude for the valuex.static doubleinterpolate2D(double wi, double wj, double x00, double x10, double x01, double x11) Performs a fast linear interpolation in two dimensions i and j.static floatinterpolate2D(float wi, float wj, float x00, float x10, float x01, float x11) Performs a fast linear interpolation in two dimensions i and j.static doublelog10(double x) Computes the common logarithm (to the base 10).static doubleround(double x, double roundFactor) Computes a rounded value for the given rounding factor.static floatround(float x, float roundFactor) Computes a rounded value for the given rounding factor.static longroundAndCrop(double value, long min, long max) First callsMath.roundwithvalueand then crops the resulting value to the rangemintomax.static introundAndCrop(float x, int min, int max) First callsMath.roundwithxand then crops the resulting value to the rangemintomax.static final doublesphereDistance(double sphereRadius, double lambda1_rad, double phi1_rad, double lambda2_rad, double phi2_rad) static final doublesphereDistanceDeg(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 Details
-
EPS_F
public static final float EPS_FThe epsilon value for thefloatdata type. The exact value of this constant is1.0E-6.- See Also:
-
EPS
public static final double EPSThe epsilon value for thedoubledata type. The exact value of this constant is1.0E-12.- See Also:
-
DTOR
public static final double DTORConversion factor for degrees to radians for thedoubledata type.- See Also:
-
RTOD
public static final double RTODConversion factor for radians to degrees for thedoubledata type.- See Also:
-
DTOR_F
public static final float DTOR_FConversion factor for degrees to radians for thefloatdata type.- See Also:
-
RTOD_F
public static final float RTOD_FConversion factor for radians to degrees for thefloatdata type.- See Also:
-
LOG10
public static final double LOG10The natural logarithm of 10 as given byMath.logging(10) -
HALFPI
public static final double HALFPIPi half- See Also:
-
-
Constructor Details
-
MathUtils
public MathUtils()
-
-
Method Details
-
equalValues
public static boolean equalValues(float x1, float x2) Compares two float values for equality within the fixed epsilon.- Parameters:
x1- the first valuex2- 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 valuex2- 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 valuex2- the second valueeps- 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 valuex2- the second valueeps- 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+1wj- weight in j-direction, a weight of 0.0 corresponds to j, 1.0 to j+1x00- 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+1wj- weight in j-direction, a weight of 0.0 corresponds to j, 1.0 to j+1x00- 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 callsMath.floorwithxand then crops the resulting value to the rangemintomax. -
roundAndCrop
public static int roundAndCrop(float x, int min, int max) First callsMath.roundwithxand then crops the resulting value to the rangemintomax. -
roundAndCrop
public static long roundAndCrop(double value, long min, long max) First callsMath.roundwithvalueand then crops the resulting value to the rangemintomax.- Parameters:
value- the value to round and cropmin- the minimum value of the crop rangemax- 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- thedoublevalue 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 rangemax- the maximum value of the rangenumDigits- the number of significant digits, must be=0- Returns:
- the rounded value, always a power to the base 10
- See Also:
-
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 rangemax- the maximum value of the rangenumDigits- 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
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 closestintand then again divided by the the rounding factor.The rounding factor can be computed for a given value range and accuracy with the
computeRoundFactormethod.- Parameters:
x- the value to be roundedroundFactor- the rounding factor specifying the accuracy, should always be a power to the base 10- Returns:
- the rounded value
- See Also:
-
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 closestintand then again divided by the the rounding factor.The rounding factor can be computed for a given value range and accuracy with the
computeRoundFactormethod.- Parameters:
x- the value to be roundedroundFactor- the rounding factor specifying the accuracy, should always be a power to the base 10- Returns:
- the rounded value
- See Also:
-
getOrderOfMagnitude
public static double getOrderOfMagnitude(double x) Returns the order of magnitude for the valuex.- 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 from0to1. 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 is1/10to10, if1then eachf[i] & 0xffwill bei.f- the curve as an array of length 256. If notnull, the method used this array as returen value after values have been written into it. Ifnull, 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 rangemintomax.- Parameters:
val- the value to cropmin- the minimum crop limitmax- the maximum crop limit
-
crop
public static short crop(short val, short min, short max) Crops the value to the rangemintomax.- Parameters:
val- the value to cropmin- the minimum crop limitmax- the maximum crop limit
-
crop
public static int crop(int val, int min, int max) Crops the value to the rangemintomax.- Parameters:
val- the value to cropmin- the minimum crop limitmax- the maximum crop limit
-
crop
public static long crop(long val, long min, long max) Crops the value to the rangemintomax.- Parameters:
val- the value to cropmin- the minimum crop limitmax- the maximum crop limit
-
crop
public static float crop(float val, float min, float max) Crops the value to the rangemintomax.- Parameters:
val- the value to cropmin- the minimum crop limitmax- the maximum crop limit
-
crop
public static double crop(double val, double min, double max) Crops the value to the rangemintomax.- Parameters:
val- the value to cropmin- the minimum crop limitmax- the maximum crop limit
-
fitDimension
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 areaa- the rectangle's widthb- 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 widthheight- the rectangle's heightnumTilesX- the number of tiles in X directionnumTilesY- the number of tiles in Y directionextraBorder- 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 spherelambda1_deg- the lambda angle of point one, in degreephi1_deg- the phi angle of point one, in degreelambda2_deg- the lambda angle of point two, in degreephi2_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 spherelambda1_rad- the lambda angle of point one, in radiansphi1_rad- the phi angle of point one, in radianslambda2_rad- the lambda angle of point two, in radiansphi2_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
-