Package org.esa.snap.core.util.math
Class MathUtils
- java.lang.Object
-
- org.esa.snap.core.util.math.MathUtils
-
public class MathUtils extends Object
A utility class providing frequently used mathematical functions which are not found in thejava.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 thedouble
data type.static float
DTOR_F
Conversion factor for degrees to radians for thefloat
data type.static double
EPS
The epsilon value for thedouble
data type.static float
EPS_F
The epsilon value for thefloat
data type.static double
HALFPI
Pi halfstatic double
LOG10
The natural logarithm of 10 as given byMath.logging(10)
static double
RTOD
Conversion factor for radians to degrees for thedouble
data type.static float
RTOD_F
Conversion factor for radians to degrees for thefloat
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 from0
to1
.static byte
crop(byte val, byte min, byte max)
Crops the value to the rangemin
tomax
.static double
crop(double val, double min, double max)
Crops the value to the rangemin
tomax
.static float
crop(float val, float min, float max)
Crops the value to the rangemin
tomax
.static int
crop(int val, int min, int max)
Crops the value to the rangemin
tomax
.static long
crop(long val, long min, long max)
Crops the value to the rangemin
tomax
.static short
crop(short val, short min, short max)
Crops the value to the rangemin
tomax
.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 callsMath.floor
withx
and then crops the resulting value to the rangemin
tomax
.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 valuex
.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 callsMath.round
withvalue
and then crops the resulting value to the rangemin
tomax
.static int
roundAndCrop(float x, int min, int max)
First callsMath.round
withx
and then crops the resulting value to the rangemin
tomax
.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 thefloat
data type. The exact value of this constant is1.0E-6
.- See Also:
- Constant Field Values
-
EPS
public static final double EPS
The epsilon value for thedouble
data type. The exact value of this constant is1.0E-12
.- See Also:
- Constant Field Values
-
DTOR
public static final double DTOR
Conversion factor for degrees to radians for thedouble
data type.- See Also:
- Constant Field Values
-
RTOD
public static final double RTOD
Conversion factor for radians to degrees for thedouble
data type.- See Also:
- Constant Field Values
-
DTOR_F
public static final float DTOR_F
Conversion factor for degrees to radians for thefloat
data type.- See Also:
- Constant Field Values
-
RTOD_F
public static final float RTOD_F
Conversion factor for radians to degrees for thefloat
data type.- See Also:
- Constant Field Values
-
LOG10
public static final double LOG10
The natural logarithm of 10 as given byMath.logging(10)
-
HALFPI
public static final double HALFPI
Pi half- See Also:
- Constant Field Values
-
-
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 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.floor
withx
and then crops the resulting value to the rangemin
tomax
.
-
roundAndCrop
public static int roundAndCrop(float x, int min, int max)
First callsMath.round
withx
and then crops the resulting value to the rangemin
tomax
.
-
roundAndCrop
public static long roundAndCrop(double value, long min, long max)
First callsMath.round
withvalue
and then crops the resulting value to the rangemin
tomax
.- 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
- thedouble
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 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:
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 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(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 closestint
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 roundedroundFactor
- 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 closestint
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 roundedroundFactor
- 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 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 from0
to1
. 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/10
to10
, if1
then eachf[i] & 0xff
will 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 rangemin
tomax
.- 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 rangemin
tomax
.- 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 rangemin
tomax
.- 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 rangemin
tomax
.- 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 rangemin
tomax
.- 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 rangemin
tomax
.- Parameters:
val
- the value to cropmin
- the minimum crop limitmax
- 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 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
-
-