Class GeoMaths

java.lang.Object
net.buildtheearth.terracore.geo.GeoMaths

public final class GeoMaths extends Object
Miscellaneous mathematical utilities for geographic and spherical calculations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Square root of 3
    static final double
    Two times pi
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[]
    cartesian2Spherical(double[] cartesian)
    Converts Cartesian coordinates to spherical coordinates on a sphere of radius 1.
    static double
    clamp(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 double
    lerp(double a, double min, double max)
    Linearly interpolates between min and max.
    static double[]
    matVecProdD(double[][] matrix, double[] vector)
    Multiples the given matrix with the given vector.
    static float
    normalizeAzimuth(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 by a about the z-axis, followed by a rotation by b about the y-axis, followed by a rotation by c about the z-axis.
    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.
    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 void
    toDegrees(double[] arr)
    Converts all values in a double array from radians to degrees.
    static void
    toRadians(double[] arr)
    Converts all values in a double array from degrees to radians.

    Methods inherited from class java.lang.Object

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

    • ROOT3

      public static final double ROOT3
      Square root of 3
    • TAU

      public static final double TAU
      Two 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 by a about the z-axis, followed by a rotation by b about the y-axis, followed by a rotation by c about the z-axis.
      Parameters:
      a - the first rotation angle about the z-axis, in radians
      b - the rotation angle about the y-axis, in radians
      c - 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 array
      vector - - 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 value
      shift - 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 clamp
      min - the minimum allowed value
      max - the maximum allowed value
      Returns:
      num if it lies within [min, max], otherwise the nearest bound
    • lerp

      public static double lerp(double a, double min, double max)
      Linearly interpolates between min and max.
      Parameters:
      a - the interpolation factor: 0 returns min, 1 returns max; values outside [0, 1] extrapolate
      min - the value returned when a is 0
      max - the value returned when a is 1
      Returns:
      the interpolated value
    • normalizeAzimuth

      public static float normalizeAzimuth(float azimuth, @NonNull @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.
      Parameters:
      azimuth - the azimuth angle, expressed in the given unit
      unit - the unit azimuth is 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 finite
      NullPointerException - if the unit is null