Package net.buildtheearth.terracore.geo
Class GeoMaths
java.lang.Object
net.buildtheearth.terracore.geo.GeoMaths
Miscellaneous mathematical utilities for geographic and spherical calculations.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic double[]cartesian2Spherical(double[] cartesian) Converts Cartesian coordinates to spherical coordinates on a sphere of radius 1.static doubleclamp(double num, double min, double max) Clamps a value to the given inclusive range.static double[]geo2Spherical(double[] geo) Converts geographic latitude and longitude coordinates to spherical coordinates on a sphere of radius 1.static doublelerp(double a, double min, double max) Linearly interpolates betweenminandmax.static double[]matVecProdD(double[][] matrix, double[] vector) Multiples the given matrix with the given vector.static floatnormalizeAzimuth(float azimuth, @NonNull AngleUnit unit) Normalizes an azimuth angle to the range of one full turn: [0, 360) degrees or [0, 2π) radians, expressed in the given unit.static double[][]produceZYZRotationMatrix(double a, double b, double c) Produces the 3x3 rotation matrix for a Z-Y-Z Euler rotation: a rotation byaabout the z-axis, followed by a rotation bybabout the y-axis, followed by a rotation bycabout the z-axis.static intsafeDirectionalShift(int val, int shift) Right-shifts the given value by the given number of bits, safely handling negative shifts and checking for overflow.static double[]spherical2Cartesian(double[] spherical) Converts spherical coordinates to Cartesian coordinates on a sphere of radius 1.static double[]spherical2Geo(double[] spherical) Converts spherical coordinates to geographic coordinates on a sphere of radius 1.static voidtoDegrees(double[] arr) Converts all values in a double array from radians to degrees.static voidtoRadians(double[] arr) Converts all values in a double array from degrees to radians.
-
Field Details
-
ROOT3
public static final double ROOT3Square root of 3 -
TAU
public static final double TAUTwo times pi- See Also:
-
-
Method Details
-
geo2Spherical
public static double[] geo2Spherical(double[] geo) Converts geographic latitude and longitude coordinates to spherical coordinates on a sphere of radius 1.- Parameters:
geo- - geographic coordinates as a double array of length 2, {longitude, latitude}, in degrees- Returns:
- the corresponding spherical coordinates in radians: {longitude, colatitude}
-
spherical2Geo
public static double[] spherical2Geo(double[] spherical) Converts spherical coordinates to geographic coordinates on a sphere of radius 1.- Parameters:
spherical- - spherical coordinates in radians as a double array of length 2: {longitude, colatitude}- Returns:
- the corresponding geographic coordinates in degrees: {longitude, latitude}
-
spherical2Cartesian
public static double[] spherical2Cartesian(double[] spherical) Converts spherical coordinates to Cartesian coordinates on a sphere of radius 1.- Parameters:
spherical- - spherical coordinates in radians as a double array of length 2: {longitude, colatitude}- Returns:
- the corresponding Cartesian coordinates: {x, y, z}
-
cartesian2Spherical
public static double[] cartesian2Spherical(double[] cartesian) Converts Cartesian coordinates to spherical coordinates on a sphere of radius 1.- Parameters:
cartesian- - Cartesian coordinates as a double array of length 3: {x, y, z}- Returns:
- the spherical coordinates of the corresponding normalized vector
-
produceZYZRotationMatrix
public static double[][] produceZYZRotationMatrix(double a, double b, double c) Produces the 3x3 rotation matrix for a Z-Y-Z Euler rotation: a rotation byaabout the z-axis, followed by a rotation bybabout the y-axis, followed by a rotation bycabout the z-axis.- Parameters:
a- the first rotation angle about the z-axis, in radiansb- the rotation angle about the y-axis, in radiansc- the second rotation angle about the z-axis, in radians- Returns:
- the resulting 3x3 rotation matrix
-
matVecProdD
public static double[] matVecProdD(double[][] matrix, double[] vector) Multiples the given matrix with the given vector. The matrix is assumed to be square and the vector is assumed to be of the same dimension as the matrix.- Parameters:
matrix- - the matrix as a n*n double arrayvector- - the vector as double array of length n- Returns:
- the result of the multiplication as an array of double on length n
-
toRadians
public static void toRadians(double[] arr) Converts all values in a double array from degrees to radians. The array is converted in place.- Parameters:
arr- - array to work on
-
toDegrees
public static void toDegrees(double[] arr) Converts all values in a double array from radians to degrees. The array is converted in place.- Parameters:
arr- - array to work on
-
safeDirectionalShift
public static int safeDirectionalShift(int val, int shift) Right-shifts the given value by the given number of bits, safely handling negative shifts and checking for overflow.- Parameters:
val- the valueshift- the number of bits to shift by- Returns:
- the shifted value
-
clamp
public static double clamp(double num, double min, double max) Clamps a value to the given inclusive range.- Parameters:
num- the value to clampmin- the minimum allowed valuemax- the maximum allowed value- Returns:
numif it lies within [min, max], otherwise the nearest bound
-
lerp
public static double lerp(double a, double min, double max) Linearly interpolates betweenminandmax.- Parameters:
a- the interpolation factor: 0 returnsmin, 1 returnsmax; values outside [0, 1] extrapolatemin- the value returned whenais 0max- the value returned whenais 1- Returns:
- the interpolated value
-
normalizeAzimuth
Normalizes an azimuth angle to the range of one full turn: [0, 360) degrees or [0, 2π) radians, expressed in the given unit.- Parameters:
azimuth- the azimuth angle, expressed in the given unitunit- the unitazimuthis expressed in and the result is returned in- Returns:
- the equivalent azimuth in the range [0, 360) degrees / [0, 2π) radians, in the given unit
- Throws:
IllegalArgumentException- if azimuth is not finiteNullPointerException- if the unit is null
-