Class SphericalCoordinates
- java.lang.Object
-
- org.apache.commons.geometry.euclidean.threed.SphericalCoordinates
-
- All Implemented Interfaces:
Spatial
public final class SphericalCoordinates extends java.lang.Object implements Spatial
Class representing spherical coordinates in 3 dimensional Euclidean space.Spherical coordinates for a point are defined by three values:
- Radius - The distance from the point to a fixed referenced point.
- Azimuth angle - The angle measured from a fixed reference direction in a plane to the orthogonal projection of the point on that plane.
- Polar angle - The angle measured from a fixed zenith direction to the point. The zenith direction must be orthogonal to the reference plane.
x = r cos(θ) sin(Φ) y = r sin(θ) sin(Φ) z = r cos(Φ) r = √(x^2 + y^2 + z^2) θ = atan2(y, x) Φ = acos(z/r)
where r is the radius, θ is the azimuth angle, and Φ is the polar angle of the spherical coordinates.There are numerous, competing conventions for the symbols used to represent spherical coordinate values. For example, the mathematical convention is to use (r, θ, Φ) to represent radius, azimuth angle, and polar angle, whereas the physics convention flips the angle values and uses (r, Φ, θ). As such, this class avoids the use of these symbols altogether in favor of the less ambiguous formal names of the values, e.g.
radius,azimuth, andpolar.In order to ensure the uniqueness of coordinate sets, coordinate values are normalized so that
radiusis in the range[0, +Infinity),azimuthis in the range[0, 2pi), andpolaris in the range[0, pi].- See Also:
- Spherical Coordinate System
-
-
Constructor Summary
Constructors Modifier Constructor Description privateSphericalCoordinates(double radius, double azimuth, double polar)Simple constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object other)Test for the equality of two sets of spherical coordinates.static SphericalCoordinatesfromCartesian(double x, double y, double z)Convert the given set of Cartesian coordinates to spherical coordinates.static SphericalCoordinatesfromCartesian(Vector3D vec)Convert the given set of Cartesian coordinates to spherical coordinates.doublegetAzimuth()Return the azimuth angle in radians.intgetDimension()Returns the number of dimensions in the space that this element belongs to.doublegetPolar()Return the polar angle in radians.doublegetRadius()Return the radius value.inthashCode()Get a hashCode for this set of spherical coordinates.booleanisFinite()Returns true if all values in this element are finite, meaning they are not NaN or infinite.booleanisInfinite()Returns true if any value in this element is infinite and none are NaN; otherwise, returns false.booleanisNaN()Returns true if any value in this element is NaN; otherwise returns false.static doublenormalizeAzimuth(double azimuth)Normalize an azimuth value to be within the range[0, 2pi).static doublenormalizePolar(double polar)Normalize a polar value to be within the range[0, +pi].static SphericalCoordinatesof(double radius, double azimuth, double polar)Return a new instance with the given spherical coordinate values.static SphericalCoordinatesparse(java.lang.String input)Parse the given string and return a newSphericalCoordinatesinstance.static Vector3DtoCartesian(double radius, double azimuth, double polar)Convert the given set of spherical coordinates to Cartesian coordinates.java.lang.StringtoString()Vector3DtoVector()Convert this set of spherical coordinates to a Cartesian form.
-
-
-
Method Detail
-
getRadius
public double getRadius()
Return the radius value. The value is in the range[0, +Infinity).- Returns:
- the radius value
-
getAzimuth
public double getAzimuth()
Return the azimuth angle in radians. This is the angle in the x-y plane measured counter-clockwise from the positive x axis. The angle is in the range[0, 2pi).- Returns:
- the azimuth angle in radians
-
getPolar
public double getPolar()
Return the polar angle in radians. This is the angle the coordinate ray makes with the positive z axis. The angle is in the range[0, pi].- Returns:
- the polar angle in radians
-
getDimension
public int getDimension()
Returns the number of dimensions in the space that this element belongs to.- Specified by:
getDimensionin interfaceSpatial- Returns:
- the number of dimensions in the element's space
-
isNaN
public boolean isNaN()
Returns true if any value in this element is NaN; otherwise returns false.
-
isInfinite
public boolean isInfinite()
Returns true if any value in this element is infinite and none are NaN; otherwise, returns false.- Specified by:
isInfinitein interfaceSpatial- Returns:
- true if any value in this element is infinite and none are NaN
-
isFinite
public boolean isFinite()
Returns true if all values in this element are finite, meaning they are not NaN or infinite.
-
toVector
public Vector3D toVector()
Convert this set of spherical coordinates to a Cartesian form.- Returns:
- A 3-dimensional vector with an equivalent set of Cartesian coordinates.
-
hashCode
public int hashCode()
Get a hashCode for this set of spherical coordinates.All NaN values have the same hash code.
- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code value for this object
-
equals
public boolean equals(java.lang.Object other)
Test for the equality of two sets of spherical coordinates.If all values of two sets of coordinates are exactly the same, and none are
Double.NaN, the two sets are considered to be equal.NaNvalues are considered to globally affect the coordinates and be equal to each other - i.e, if any (or all) values of the coordinate set are equal toDouble.NaN, the set as a whole is considered to equal NaN.- Overrides:
equalsin classjava.lang.Object- Parameters:
other- Object to test for equality to this- Returns:
- true if two SphericalCoordinates objects are equal, false if object is null, not an instance of SphericalCoordinates, or not equal to this SphericalCoordinates instance
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
of
public static SphericalCoordinates of(double radius, double azimuth, double polar)
Return a new instance with the given spherical coordinate values. The values are normalized so thatradiuslies in the range[0, +Infinity),azimuthlies in the range[0, 2pi), andpolarlies in the range[0, +pi].- Parameters:
radius- the length of the line segment from the origin to the coordinate point.azimuth- the angle in the x-y plane, measured in radians counter-clockwise from the positive x-axis.polar- the angle in radians between the positive z-axis and the ray from the origin to the coordinate point.- Returns:
- a new
SphericalCoordinatesinstance representing the same point as the given set of spherical coordinates.
-
fromCartesian
public static SphericalCoordinates fromCartesian(double x, double y, double z)
Convert the given set of Cartesian coordinates to spherical coordinates.- Parameters:
x- X coordinate valuey- Y coordinate valuez- Z coordinate value- Returns:
- a set of spherical coordinates equivalent to the given Cartesian coordinates
-
fromCartesian
public static SphericalCoordinates fromCartesian(Vector3D vec)
Convert the given set of Cartesian coordinates to spherical coordinates.- Parameters:
vec- vector containing Cartesian coordinates to convert- Returns:
- a set of spherical coordinates equivalent to the given Cartesian coordinates
-
toCartesian
public static Vector3D toCartesian(double radius, double azimuth, double polar)
Convert the given set of spherical coordinates to Cartesian coordinates.- Parameters:
radius- The spherical radius value.azimuth- The spherical azimuth angle in radians.polar- The spherical polar angle in radians.- Returns:
- A 3-dimensional vector with an equivalent set of Cartesian coordinates.
-
parse
public static SphericalCoordinates parse(java.lang.String input)
Parse the given string and return a newSphericalCoordinatesinstance. The parsed coordinate values are normalized as in theof(double, double, double)method. The expected string format is the same as that returned bytoString().- Parameters:
input- the string to parse- Returns:
- new
SphericalCoordinatesinstance - Throws:
java.lang.IllegalArgumentException- if the string format is invalid.
-
normalizeAzimuth
public static double normalizeAzimuth(double azimuth)
Normalize an azimuth value to be within the range[0, 2pi). This is exactly equivalent toPolarCoordinates.normalizeAzimuth(double).- Parameters:
azimuth- azimuth value in radians- Returns:
- equivalent azimuth value in the range
[0, 2pi). - See Also:
PolarCoordinates.normalizeAzimuth(double)
-
normalizePolar
public static double normalizePolar(double polar)
Normalize a polar value to be within the range[0, +pi]. Since the polar angle is the angle between two vectors (the zenith direction and the point vector), the sign of the angle is not significant as in the azimuth angle. For example, a polar angle of-pi/2and one of+pi/2will both normalize topi/2.- Parameters:
polar- polar value in radians- Returns:
- equivalent polar value in the range
[0, +pi]
-
-