NASA World Wind

gov.nasa.worldwind.util
Class WWMath

java.lang.Object
  extended by gov.nasa.worldwind.util.WWMath

public class WWMath
extends java.lang.Object

A collection of useful math methods, all static.


Field Summary
static double HOUR_TO_MILLIS
           
static double METERS_TO_FEET
           
static double METERS_TO_KILOMETERS
           
static double METERS_TO_MILES
           
static double METERS_TO_NAUTICAL_MILES
           
static double METERS_TO_YARDS
           
static double MINUTE_TO_MILLIS
           
static double SECOND_TO_MILLIS
           
static double SQUARE_METERS_TO_ACRES
           
static double SQUARE_METERS_TO_HECTARES
           
static double SQUARE_METERS_TO_SQUARE_FEET
           
static double SQUARE_METERS_TO_SQUARE_KILOMETERS
           
static double SQUARE_METERS_TO_SQUARE_MILES
           
static double SQUARE_METERS_TO_SQUARE_YARDS
           
 
Constructor Summary
WWMath()
           
 
Method Summary
static double clamp(double v, double min, double max)
          Clamps a value to a given range.
static double computeDistanceFromEye(DrawContext dc, Extent extent)
          Returns the distance in model coordinates from the View eye point to the specified Extent.
static double computeInterpolationFactor(double v, double x, double y)
          Returns the interpolation factor for v given the specified range [x, y].
static double computePolygonArea(java.lang.Iterable<? extends LatLon> locations)
          Returns the area enclosed by the specified locations, in angular degrees.
static double computePolygonArea2(java.lang.Iterable<? extends Vec4> points)
          Returns the area enclosed by the specified (x, y) points (the z and w coordinates are ignored).
static java.lang.String computePolygonWindingOrder(java.lang.Iterable<? extends LatLon> locations)
          Returns the winding order of the polygon described by the specified locations, with respect an axis perpendicular to the (lat, lon) coordinates, and pointing in the direction of "positive elevation".
static java.lang.String computePolygonWindingOrder2(java.lang.Iterable<? extends Vec4> points)
          Returns the winding order of the 2D polygon described by the specified (x, y) points (z and w coordinates are ignored), with respect to the positive z axis.
static double computeSizeInWindowCoordinates(DrawContext dc, Extent extent)
          Returns the size in window coordinates of the specified Extent from the current View.
static Vec4 computeTriangleNormal(Vec4 a, Vec4 b, Vec4 c)
          Returns the normal vector corresponding to the triangle defined by three vertices (a, b, c).
static double convertHoursToMillis(double hours)
          Converts time in hours to time in milliseconds.
static double convertMetersToFeet(double meters)
          converts meters to feet.
static double convertMetersToMiles(double meters)
          converts meters to miles.
static double convertMillisToHours(double mills)
          Converts time in milliseconds to time in hours.
static double convertMillisToMinutes(double millis)
          Converts time in milliseconds to time in minutes.
static double convertMillisToSeconds(double millis)
          Converts time in milliseconds to time in seconds.
static double convertMinutesToMillis(double minutes)
          Converts time in minutes to time in milliseconds.
static double convertSecondsToMillis(double seconds)
          Converts time in seconds to time in milliseconds.
static boolean isLocationInside(LatLon location, java.lang.Iterable<? extends LatLon> locations)
          Determines whether a LatLon location is located inside a given polygon.
static boolean isPolygonClosed(java.lang.Iterable<? extends LatLon> locations)
          Returns whether the geographic polygon described by the specified locations defines a closed loop.
static boolean isPolygonClosed2(java.lang.Iterable<? extends Vec4> points)
          Returns whether the 2D polygon described by the specified (x, y) points defines a closed loop (z and w coordinates are ignored).
static boolean isPowerOfTwo(int value)
          Convenience method for testing whether a value is a power of two.
static double logBase2(double value)
          Convenience method to compute the log base 2 of a value.
static double mix(double a, double x, double y)
          Returns the linear interpolation of x and y according to the function: (1 - a) * x + a * y.
static double mixSmooth(double a, double x, double y)
          Returns the smooth hermite interpolation of x and y according to the function: (1 - t) * x + t * y, where t = a * a * (3 - 2 * a).
static int powerOfTwoCeiling(int reference)
          Returns the value that is the nearest power of 2 greater than or equal to the given value.
static int powerOfTwoFloor(int reference)
          Returns the value that is the nearest power of 2 less than or equal to the given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HOUR_TO_MILLIS

public static final double HOUR_TO_MILLIS
See Also:
Constant Field Values

METERS_TO_FEET

public static final double METERS_TO_FEET
See Also:
Constant Field Values

METERS_TO_KILOMETERS

public static final double METERS_TO_KILOMETERS
See Also:
Constant Field Values

METERS_TO_MILES

public static final double METERS_TO_MILES
See Also:
Constant Field Values

METERS_TO_NAUTICAL_MILES

public static final double METERS_TO_NAUTICAL_MILES
See Also:
Constant Field Values

METERS_TO_YARDS

public static final double METERS_TO_YARDS
See Also:
Constant Field Values

MINUTE_TO_MILLIS

public static final double MINUTE_TO_MILLIS
See Also:
Constant Field Values

SECOND_TO_MILLIS

public static final double SECOND_TO_MILLIS
See Also:
Constant Field Values

SQUARE_METERS_TO_ACRES

public static final double SQUARE_METERS_TO_ACRES
See Also:
Constant Field Values

SQUARE_METERS_TO_HECTARES

public static final double SQUARE_METERS_TO_HECTARES
See Also:
Constant Field Values

SQUARE_METERS_TO_SQUARE_FEET

public static final double SQUARE_METERS_TO_SQUARE_FEET
See Also:
Constant Field Values

SQUARE_METERS_TO_SQUARE_KILOMETERS

public static final double SQUARE_METERS_TO_SQUARE_KILOMETERS
See Also:
Constant Field Values

SQUARE_METERS_TO_SQUARE_MILES

public static final double SQUARE_METERS_TO_SQUARE_MILES
See Also:
Constant Field Values

SQUARE_METERS_TO_SQUARE_YARDS

public static final double SQUARE_METERS_TO_SQUARE_YARDS
See Also:
Constant Field Values
Constructor Detail

WWMath

public WWMath()
Method Detail

clamp

public static double clamp(double v,
                           double min,
                           double max)
Clamps a value to a given range.

Parameters:
v - the value to clamp.
min - the floor.
max - the ceiling
Returns:
the nearest value such that min <= v <= max.

computeDistanceFromEye

public static double computeDistanceFromEye(DrawContext dc,
                                            Extent extent)
Returns the distance in model coordinates from the View eye point to the specified Extent. If the View eye point is inside the extent, this returns 0.

Parameters:
dc - the DrawContext which the View eye point is obtained from.
extent - the extent to compute the distance from.
Returns:
the distance from the View eye point to the extent, in model coordinates.
Throws:
java.lang.IllegalArgumentException - if either the DrawContext or the extent is null.

computeInterpolationFactor

public static double computeInterpolationFactor(double v,
                                                double x,
                                                double y)
Returns the interpolation factor for v given the specified range [x, y]. The interpolation factor is a number between 0 and 1 (inclusive), representing the value's relative position between x and y. For example, 0 corresponds to x, 1 corresponds to y, and anything in between corresponds to a linear combination of x and y.

Parameters:
v - the value to compute the interpolation factor for.
x - the first value.
y - the second value.
Returns:
the interpolation factor for v given the specified range [x, y]

computePolygonArea

public static double computePolygonArea(java.lang.Iterable<? extends LatLon> locations)
Returns the area enclosed by the specified locations, in angular degrees. If the specified locations do not define a closed loop, then the loop is automatically closed by appending the first location to the last location.

Parameters:
locations - the locations which define the geographic polygon.
Returns:
the area enclosed by the specified coordinates.
Throws:
java.lang.IllegalArgumentException - if locations is null.

computePolygonArea2

public static double computePolygonArea2(java.lang.Iterable<? extends Vec4> points)
Returns the area enclosed by the specified (x, y) points (the z and w coordinates are ignored). If the specified points do not define a closed loop, then the loop is automatically closed by simulating appending the first point to the last point.

Parameters:
points - the (x, y) points which define the 2D polygon.
Returns:
the area enclosed by the specified coordinates.
Throws:
java.lang.IllegalArgumentException - if points is null.

computePolygonWindingOrder

public static java.lang.String computePolygonWindingOrder(java.lang.Iterable<? extends LatLon> locations)
Returns the winding order of the polygon described by the specified locations, with respect an axis perpendicular to the (lat, lon) coordinates, and pointing in the direction of "positive elevation".

Parameters:
locations - the locations which define the geographic polygon.
Returns:
AVKey.CLOCKWISE if the polygon has clockwise winding order about the positive z axis, and AVKey.COUNTER_CLOCKWISE otherwise.

computePolygonWindingOrder2

public static java.lang.String computePolygonWindingOrder2(java.lang.Iterable<? extends Vec4> points)
Returns the winding order of the 2D polygon described by the specified (x, y) points (z and w coordinates are ignored), with respect to the positive z axis.

Parameters:
points - the (x, y) points which define the 2D polygon.
Returns:
AVKey.CLOCKWISE if the polygon has clockwise winding order about the positive z axis, and AVKey.COUNTER_CLOCKWISE otherwise.

computeSizeInWindowCoordinates

public static double computeSizeInWindowCoordinates(DrawContext dc,
                                                    Extent extent)
Returns the size in window coordinates of the specified Extent from the current View. The returned size is an estimate of the Extent's diameter in window coordinates.

Parameters:
dc - the current draw context, from which the View is obtained from.
extent - the extent to compute the window size for.
Returns:
size of the specified Extent from the specified View, in window coordinates (screen pixels).
Throws:
java.lang.IllegalArgumentException - if either the DrawContext or the extent is null.

computeTriangleNormal

public static Vec4 computeTriangleNormal(Vec4 a,
                                         Vec4 b,
                                         Vec4 c)
Returns the normal vector corresponding to the triangle defined by three vertices (a, b, c).

Parameters:
a - the triangle's first vertex.
b - the triangle's second vertex.
c - the triangle's third vertex.
Returns:
the triangle's normal vector.
Throws:
java.lang.IllegalArgumentException - if any of the specified vertices are null.

convertHoursToMillis

public static double convertHoursToMillis(double hours)
Converts time in hours to time in milliseconds.

Parameters:
hours - time in hours.
Returns:
time in milliseconds.

convertMetersToFeet

public static double convertMetersToFeet(double meters)
converts meters to feet.

Parameters:
meters - the value in meters.
Returns:
the value converted to feet.

convertMetersToMiles

public static double convertMetersToMiles(double meters)
converts meters to miles.

Parameters:
meters - the value in meters.
Returns:
the value converted to miles.

convertMillisToHours

public static double convertMillisToHours(double mills)
Converts time in milliseconds to time in hours.

Parameters:
mills - time in milliseconds.
Returns:
time in hours.

convertMillisToMinutes

public static double convertMillisToMinutes(double millis)
Converts time in milliseconds to time in minutes.

Parameters:
millis - time in milliseconds.
Returns:
time in minutes.

convertMillisToSeconds

public static double convertMillisToSeconds(double millis)
Converts time in milliseconds to time in seconds.

Parameters:
millis - time in milliseconds.
Returns:
time in seconds.

convertMinutesToMillis

public static double convertMinutesToMillis(double minutes)
Converts time in minutes to time in milliseconds.

Parameters:
minutes - time in minutes.
Returns:
time in milliseconds.

convertSecondsToMillis

public static double convertSecondsToMillis(double seconds)
Converts time in seconds to time in milliseconds.

Parameters:
seconds - time in seconds.
Returns:
time in milliseconds.

isLocationInside

public static boolean isLocationInside(LatLon location,
                                       java.lang.Iterable<? extends LatLon> locations)
Determines whether a LatLon location is located inside a given polygon.

Parameters:
location - the location
locations - the list of positions describing the polygon. Last one should be the same as the first one.
Returns:
true if the location is inside the polygon.

isPolygonClosed

public static boolean isPolygonClosed(java.lang.Iterable<? extends LatLon> locations)
Returns whether the geographic polygon described by the specified locations defines a closed loop. If the iterable holds fewer than two points, this always returns false. Therefore a polygon consisting of a single point and the empty polygon are not considered closed loops.

Parameters:
locations - the locations which define the geographic polygon.
Returns:
true if the polygon defines a closed loop, and false otherwise.
Throws:
java.lang.IllegalArgumentException - if the locations are null.

isPolygonClosed2

public static boolean isPolygonClosed2(java.lang.Iterable<? extends Vec4> points)
Returns whether the 2D polygon described by the specified (x, y) points defines a closed loop (z and w coordinates are ignored). If the iterable holds fewer than two points, this always returns false. Therefore a polygon consisting of a single point and the empty polygon are not considered closed loops.

Parameters:
points - the (x, y) points which define the 2D polygon.
Returns:
true if the polygon defines a closed loop, and false otherwise.
Throws:
java.lang.IllegalArgumentException - if the points are null.

isPowerOfTwo

public static boolean isPowerOfTwo(int value)
Convenience method for testing whether a value is a power of two.

Parameters:
value - the value to test for power of 2
Returns:
true if power of 2, else false

logBase2

public static double logBase2(double value)
Convenience method to compute the log base 2 of a value.

Parameters:
value - the value to take the log of.
Returns:
the log base 2 of the specified value.

mix

public static double mix(double a,
                         double x,
                         double y)
Returns the linear interpolation of x and y according to the function: (1 - a) * x + a * y. The interpolation factor a defines the weight given to each value, and is clamped to the range [0, 1]. If a is 0 or less, this returns x. If a is 1 or more, this returns y. Otherwise, this returns the linear interpolation of x and y. For example, when a is 0.5 this returns (x + y)/2.

Parameters:
a - the interpolation factor.
x - the first value.
y - the second value.
Returns:
the linear interpolation of x and y.

mixSmooth

public static double mixSmooth(double a,
                               double x,
                               double y)
Returns the smooth hermite interpolation of x and y according to the function: (1 - t) * x + t * y, where t = a * a * (3 - 2 * a). The interpolation factor a defines the weight given to each value, and is clamped to the range [0, 1]. If a is 0 or less, this returns x. If a is 1 or more, this returns y. Otherwise, this returns the smooth hermite interpolation of x and y. Like the linear function mix(double, double, double), when a is 0.5 this returns (x + y)/2. But unlike the linear function, the hermite function's slope gradually increases when a is near 0, then gradually decreases when a is near 1. This is a useful property where a more gradual transition from x to y is desired.

Parameters:
a - the interpolation factor.
x - the first value.
y - the second value.
Returns:
the smooth hermite interpolation of x and y.

powerOfTwoCeiling

public static int powerOfTwoCeiling(int reference)
Returns the value that is the nearest power of 2 greater than or equal to the given value.

Parameters:
reference - the reference value. The power of 2 returned is greater than or equal to this value.
Returns:
the value that is the nearest power of 2 greater than or equal to the reference value

powerOfTwoFloor

public static int powerOfTwoFloor(int reference)
Returns the value that is the nearest power of 2 less than or equal to the given value.

Parameters:
reference - the reference value. The power of 2 returned is less than or equal to this value.
Returns:
the value that is the nearest power of 2 less than or equal to the reference value

NASA World Wind