com.threerings.math
Class FloatMath

java.lang.Object
  extended by com.threerings.math.FloatMath

public class FloatMath
extends Object

Utility methods and constants for single-precision floating point math.


Field Summary
static float E
          The base value of the natural logarithm.
static float EPSILON
          A small number.
static float HALF_PI
          Pi times one half.
static float PI
          The ratio of a circle's circumference to its diameter.
static float TWO_PI
          The circle constant, tau (τ) http://tauday.com/
 
Constructor Summary
FloatMath()
           
 
Method Summary
static float acos(float a)
          Computes and returns the arc cosine of the given value.
static float asin(float a)
          Computes and returns the arc sine of the given value.
static float atan(float a)
          Computes and returns the arc tangent of the given value.
static float atan2(float y, float x)
          Computes and returns the arc tangent of the given values.
static float cbrt(float v)
          Returns the cube root of the supplied value.
static float ceil(float v)
          Returns the ceiling of v.
static float clamp(float v, float lower, float upper)
          Clamps a value to the range [lower, upper].
static float cos(float a)
          Computes and returns the cosine of the given angle.
static boolean epsilonEquals(float v1, float v2)
          Determines whether two values are "close enough" to equal.
static float exp(float v)
          Returns e to the power of the supplied value.
static float exponential(float mean)
          Returns a random value according to the exponential distribution with the provided mean.
static float floor(float v)
          Returns the floor of v.
static float getAngularDifference(float a1, float a2)
          Returns the (shortest) difference between two angles, assuming that both angles are in [-pi, +pi].
static float getAngularDistance(float a1, float a2)
          Returns the (shortest) distance between two angles, assuming that both angles are in [-pi, +pi].
static float hypot(float x, float y)
          Computes and returns sqrt(x*x + y*y).
static int iceil(float v)
          Returns the ceiling of v as an integer without calling the relatively expensive Math.ceil(double).
static float IEEEremainder(float f1, float f2)
          Returns the remainder when f1 is divided by f2.
static int ifloor(float v)
          Returns the floor of v as an integer without calling the relatively expensive Math.floor(double).
static boolean isWithin(float v, float lower, float upper)
          Checks whether the value supplied is in [lower, upper].
static float lerp(float v1, float v2, float t)
          Linearly interpolates between v1 and v2 by the parameter t.
static float lerpa(float a1, float a2, float t)
          Linearly interpolates between two angles, taking the shortest path around the circle.
static float log(float v)
          Returns the natural logarithm of the supplied value.
static float log10(float v)
          Returns the base 10 logarithm of the supplied value.
static float mirrorAngle(float a)
          Returns the mirror angle of the specified angle (assumed to be in [-pi, +pi]).
static float normal()
          Returns a random value according to the standard normal distribution.
static float normal(float mean, float stddev)
          Returns a random value according to the normal distribution with the provided mean and standard deviation.
static float normalizeAngle(float a)
          Returns an angle in the range [-pi, pi].
static float normalizeAnglePositive(float a)
          Returns an angle in the range [0, 2pi].
static float pow(float v, float e)
          Returns v to the power of e.
static float random()
          Returns a uniformly distributed random floating point value in [0, 1).
static float random(float lower, float upper)
          Returns a uniformly distributed random floating point value in [lower, upper).
static Vector3f reflect(Vector3f i, Vector3f n)
          Computes the reflection of a vector.
static Vector3f reflect(Vector3f i, Vector3f n, Vector3f result)
          Computes the reflection of a vector and stores it in the provided vector.
static Vector3f refract(Vector3f i, Vector3f n, float eta)
          Computes the refraction of a vector.
static Vector3f refract(Vector3f i, Vector3f n, float eta, Vector3f result)
          Computes the refraction of a vector, placing the result in the provided vector.
static int round(float v)
          A cheaper version of Math.round(float) that doesn't handle the special cases.
static float roundNearest(float v, float target)
          Rounds a value to the nearest multiple of a target.
static float sin(float a)
          Computes and returns the sine of the given angle.
static float sqrt(float v)
          Returns the square root of the supplied value.
static float tan(float a)
          Computes and returns the tangent of the given angle.
static float toDegrees(float a)
          Converts from radians to degrees.
static float toRadians(float a)
          Converts from degrees to radians.
static Vector3f updateClosest(Vector3f origin, Vector3f result, Vector3f closest)
          Updates the value of the closest point and returns a new result vector reference.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PI

public static final float PI
The ratio of a circle's circumference to its diameter.

See Also:
Constant Field Values

TWO_PI

public static final float TWO_PI
The circle constant, tau (τ) http://tauday.com/

See Also:
Constant Field Values

HALF_PI

public static final float HALF_PI
Pi times one half.

See Also:
Constant Field Values

E

public static final float E
The base value of the natural logarithm.

See Also:
Constant Field Values

EPSILON

public static final float EPSILON
A small number.

See Also:
Constant Field Values
Constructor Detail

FloatMath

public FloatMath()
Method Detail

sin

public static float sin(float a)
Computes and returns the sine of the given angle.

See Also:
Math.sin(double)

cos

public static float cos(float a)
Computes and returns the cosine of the given angle.

See Also:
Math.cos(double)

tan

public static float tan(float a)
Computes and returns the tangent of the given angle.

See Also:
Math.tan(double)

asin

public static float asin(float a)
Computes and returns the arc sine of the given value.

See Also:
Math.asin(double)

acos

public static float acos(float a)
Computes and returns the arc cosine of the given value.

See Also:
Math.acos(double)

atan

public static float atan(float a)
Computes and returns the arc tangent of the given value.

See Also:
Math.atan(double)

atan2

public static float atan2(float y,
                          float x)
Computes and returns the arc tangent of the given values.

See Also:
Math.atan2(double, double)

toDegrees

public static float toDegrees(float a)
Converts from radians to degrees.

See Also:
Math.toDegrees(double)

toRadians

public static float toRadians(float a)
Converts from degrees to radians.

See Also:
Math.toRadians(double)

sqrt

public static float sqrt(float v)
Returns the square root of the supplied value.

See Also:
Math.sqrt(double)

cbrt

public static float cbrt(float v)
Returns the cube root of the supplied value.

See Also:
Math.cbrt(double)

hypot

public static float hypot(float x,
                          float y)
Computes and returns sqrt(x*x + y*y).

See Also:
Math.hypot(double, double)

exp

public static float exp(float v)
Returns e to the power of the supplied value.

See Also:
Math.exp(double)

log

public static float log(float v)
Returns the natural logarithm of the supplied value.

See Also:
Math.log(double)

log10

public static float log10(float v)
Returns the base 10 logarithm of the supplied value.

See Also:
Math.log10(double)

pow

public static float pow(float v,
                        float e)
Returns v to the power of e.

See Also:
Math.pow(double, double)

floor

public static float floor(float v)
Returns the floor of v.

See Also:
Math.floor(double)

round

public static int round(float v)
A cheaper version of Math.round(float) that doesn't handle the special cases.


ifloor

public static int ifloor(float v)
Returns the floor of v as an integer without calling the relatively expensive Math.floor(double).


ceil

public static float ceil(float v)
Returns the ceiling of v.

See Also:
Math.ceil(double)

iceil

public static int iceil(float v)
Returns the ceiling of v as an integer without calling the relatively expensive Math.ceil(double).


IEEEremainder

public static float IEEEremainder(float f1,
                                  float f2)
Returns the remainder when f1 is divided by f2.

See Also:
Math.IEEEremainder(double, double)

clamp

public static float clamp(float v,
                          float lower,
                          float upper)
Clamps a value to the range [lower, upper].


roundNearest

public static float roundNearest(float v,
                                 float target)
Rounds a value to the nearest multiple of a target.


isWithin

public static boolean isWithin(float v,
                               float lower,
                               float upper)
Checks whether the value supplied is in [lower, upper].


random

public static float random(float lower,
                           float upper)
Returns a uniformly distributed random floating point value in [lower, upper).


random

public static float random()
Returns a uniformly distributed random floating point value in [0, 1).


normal

public static float normal(float mean,
                           float stddev)
Returns a random value according to the normal distribution with the provided mean and standard deviation.


normal

public static float normal()
Returns a random value according to the standard normal distribution.


exponential

public static float exponential(float mean)
Returns a random value according to the exponential distribution with the provided mean.


lerpa

public static float lerpa(float a1,
                          float a2,
                          float t)
Linearly interpolates between two angles, taking the shortest path around the circle. This assumes that both angles are in [-pi, +pi].


lerp

public static float lerp(float v1,
                         float v2,
                         float t)
Linearly interpolates between v1 and v2 by the parameter t.


epsilonEquals

public static boolean epsilonEquals(float v1,
                                    float v2)
Determines whether two values are "close enough" to equal.


getAngularDistance

public static float getAngularDistance(float a1,
                                       float a2)
Returns the (shortest) distance between two angles, assuming that both angles are in [-pi, +pi].


getAngularDifference

public static float getAngularDifference(float a1,
                                         float a2)
Returns the (shortest) difference between two angles, assuming that both angles are in [-pi, +pi].


normalizeAngle

public static float normalizeAngle(float a)
Returns an angle in the range [-pi, pi].


normalizeAnglePositive

public static float normalizeAnglePositive(float a)
Returns an angle in the range [0, 2pi].


mirrorAngle

public static float mirrorAngle(float a)
Returns the mirror angle of the specified angle (assumed to be in [-pi, +pi]).


reflect

public static Vector3f reflect(Vector3f i,
                               Vector3f n)
Computes the reflection of a vector. The formula comes from the GLSL specification.

Returns:
a new vector containing the result.

reflect

public static Vector3f reflect(Vector3f i,
                               Vector3f n,
                               Vector3f result)
Computes the reflection of a vector and stores it in the provided vector.

Returns:
a reference to the result, for chaining.

refract

public static Vector3f refract(Vector3f i,
                               Vector3f n,
                               float eta)
Computes the refraction of a vector. The formula comes from the GLSL specification.

Returns:
a new vector containing the result.

refract

public static Vector3f refract(Vector3f i,
                               Vector3f n,
                               float eta,
                               Vector3f result)
Computes the refraction of a vector, placing the result in the provided vector.

Returns:
a reference to the result, for chaining.

updateClosest

public static Vector3f updateClosest(Vector3f origin,
                                     Vector3f result,
                                     Vector3f closest)
Updates the value of the closest point and returns a new result vector reference. This is used to minimize garbage creation when searching for the closet point using, for example, the following pattern:

 Vector3f closest = result;
 for (Model model : models) {
     if (model.getIntersection(ray, result)) {
         result = FloatMath.updateClosest(ray.getOrigin(), result, closest);
     }
 }
 // if result != closest, then we hit something
 



Copyright © 2011. All Rights Reserved.