MathCommon.h File Reference

Defines and includes common Math data types and functions. More...

#include <cmath>
#include <algorithm>
#include <vector>
#include <limits>
#include <float.h>
#include "Math/Point.h"
#include "Math/Vector.h"
#include "Math/Matrix.h"

Classes

struct  Rect_t< T >
 A 2D rectangle. More...
struct  Line
 A 2D Line segment. More...
class  Ray
 A 2D Ray. More...
class  AABB
 2D Axis Aligned Bounding Box. More...

Defines

#define BIT(x)   (1 << (x))
 Constructs an integer with one bit set.

Typedefs

typedef Rect_t< float > Rectf
 A Template instantiation of the Rect_t template using the float data type.
typedef Rect_t< double > Rectd
 A Template instantiation of the Rect_t template using the double data type.
typedef Rect_t< int > Recti
 A Template instantiation of the Rect_t template using the int data type.

Functions

float ClosestPoints (const Line &l1, const Line &l2, Point2f &out1, Point2f &out2)
 Computes the closest points between the two given lines.
int Intersect (const Ray &r, const Circle &c, float &t1, float &t2)
 Intersects a Ray with a Circle and finds the enter and exit time.
int Intersect (const Line &l, const Circle &c, float &t1, float &t2)
 Intersects a Line with a Circle and finds the enter and exit time.
template<class T , class U >
lerp (const T &t1, const T &t2, U t)
 Linear interpolates between two objects of type T.
template<class T >
AABB RectToAABB (const Rect_t< T > &r)
 Converts a Rect to an AABB.
template<class T >
Rect_t< T > AABBToRect (const AABB &aabb)
 Converts an AABB to a Rect.
template<class T >
Rect_t< T > ExpandRect (const Rect_t< T > &r, float dx, float dy)
 Expands (or contracts) the size of the given rectangle about the center.
template<class T >
bool InsideRect (const Rect_t< T > &r, const Point2< T > &p)
 Tests if the given point is inside the given rect.
template<class T >
void ClampInPlace (T &val, const T &low, const T &high)
 Clamps a given value into a given range.
template<class T >
Clamp (const T &val, const T &low, const T &high)
 Clamps a given value into a given range.
template<class T >
void ClampLow (T &val, const T &low)
 Clamps a given value into a given range.
template<class T >
void ClampHigh (T &val, const T &high)
 Clamps a given value into a given range.
template<class T >
Sign (const T &val)
 Determines the sign of the given value.
template<class T >
SignZero (const T &val)
 Determines the sign of the given value.
template<class T >
Minimum (const T &v1, const T &v2)
 Determines the minimum of two elements.
template<class T >
Maximum (const T &v1, const T &v2)
 Determines the maximum of two elements.
template<class T >
void Swap (T &v1, T &v2)
 Swaps the given two values.
float RadToDeg (float a)
 Converts an angle in Radians to Degrees.
float DegToRad (float a)
 Converts an angle in Degrees to Radians.
bool Close (float a, float b, float tol=0.00001f)
 Tests if two floating point values are within a given tolerance.
float RandomFloat (float min, float max)
 Computes a random float in a given range.
uint TimeSeed ()
 Computes a pretty good seed for a random number generator using the system time.
void Transform (Poly &p, const Matrixf &m)
 Transforms the given polygon.
void Transform (Point2f *p, uint n, const Matrixf &m)
 Transforms the given points.
Point2f ScreenToNormCoords (const Point2f &p)
 Converts a point in absolute Screen coordinates to normalized device coordinates.
Point2f NormToScreenCoords (const Point2f &p)
 Converts a point in normalized device coordinates to absolute Screen coordinates.
AABB ScreenToNormCoords (const AABB &a)
 Converts an AABB in normalized device coordinates to absolute Screen coordinates.
AABB NormToScreenCoords (const AABB &a)
 Converts an AABB in normalized device coordinates to absolute Screen coordinates.

Variables

const float PI = 3.141592653589793f
const float PI_2 = PI / 2.0f
const float PI_4 = PI / 4.0f
const float _2PI = 2.0f * PI
const float INV_PI = 1.0f / PI
const float E = 2.718281828459045f
const float SQRT2 = 1.414213562373095f
const float INV_SQRT2 = 1.0f / SQRT2
const float SQRT2_2 = 0.707106781186547f
const float RAD_TO_DEG = 180.0f / PI
const float DEG_TO_RAD = PI / 180.0f
const float EPSILON = 0.0001f

Detailed Description

Defines and includes common Math data types and functions.

Author:
Joe Forte
Date:
3/27/2008

Define Documentation

#define BIT (  )     (1 << (x))

Constructs an integer with one bit set.

Parameters:
[in] x the xth bit in the integer to set.

Function Documentation

template<class T >
Rect_t<T> AABBToRect ( const AABB aabb  )  [inline]

Converts an AABB to a Rect.

Template Parameters:
T the data type of the Rect. If T is not float data may be truncated.
Parameters:
[in] aabb The AABB to convert.
See also:
RectToAABB
Returns:
A Rect equivalent to aabb.
template<class T >
T Clamp ( const T &  val,
const T &  low,
const T &  high 
) [inline]

Clamps a given value into a given range.

Template Parameters:
T the data type of used.
Parameters:
[in] val the value to clamp.
[in] low the low end of the range.
[in] high the hight end of the range.
See also:
ClampLow
ClampHigh
Returns:
The result of clamping val into the range [low,high]
template<class T >
void ClampHigh ( T &  val,
const T &  high 
) [inline]

Clamps a given value into a given range.

Template Parameters:
T the data type of used.
Parameters:
[in,out] val the value to clamp. Will be set to be in the range (infinity,high]
[in] high the hight end of the range.
See also:
Clamp
ClampLow
template<class T >
void ClampInPlace ( T &  val,
const T &  low,
const T &  high 
) [inline]

Clamps a given value into a given range.

Template Parameters:
T the data type of used.
Parameters:
[in,out] val the value to clamp. Will be set to be in the range [low,high]
[in] low the low end of the range.
[in] high the hight end of the range.
See also:
ClampLow
ClampHigh
template<class T >
void ClampLow ( T &  val,
const T &  low 
) [inline]

Clamps a given value into a given range.

Template Parameters:
T the data type of used.
Parameters:
[in,out] val the value to clamp. Will be set to be in the range [low,infinity)
[in] low the low end of the range.
See also:
Clamp
ClampHigh
bool Close ( float  a,
float  b,
float  tol = 0.00001f 
) [inline]

Tests if two floating point values are within a given tolerance.

Parameters:
[in] a the first value.
[in] b the second value.
[in] tol the tolerance. Defaults to 0.00001f.
Returns:
true if the given values are within the given tolerance from each other.
float ClosestPoints ( const Line l1,
const Line l2,
Point2f out1,
Point2f out2 
)

Computes the closest points between the two given lines.

Parameters:
[in] l1 the first line.
[in] l2 the second line.
[out] out1 the closest point on the first line.
[out] out2 the closest point on the second line.
Returns:
The distance between out1 and out2.

pg 149

float DegToRad ( float  a  )  [inline]

Converts an angle in Degrees to Radians.

Parameters:
[in] a the angle in Degrees to convert.
See also:
RadToDeg
Returns:
The angle a in Radians.
template<class T >
Rect_t<T> ExpandRect ( const Rect_t< T > &  r,
float  dx,
float  dy 
) [inline]

Expands (or contracts) the size of the given rectangle about the center.

Template Parameters:
T the data type of the Rect. If T is not float data may be truncated.
Parameters:
[in] r the Rect to expand.
[in] dx the amount to expand in the x direction.
[in] dy the amount to expand in the y direction.
Returns:
The expanded Rect.
template<class T >
bool InsideRect ( const Rect_t< T > &  r,
const Point2< T > &  p 
) [inline]

Tests if the given point is inside the given rect.

Template Parameters:
T the data type of the Rect and the Point2
Parameters:
[in] r the Rect to check against.
[in] p the point to test.
Returns:
True if the point p is inside Rect r.
int Intersect ( const Line l,
const Circle &  c,
float &  t1,
float &  t2 
)

Intersects a Line with a Circle and finds the enter and exit time.

Parameters:
[in] l the Line.
[in] c the circle.
[out] t1 the time the Line l enters the Circle c.
[out] t2 the time the Line l exits the Circle c.
Returns:
The number of intersections the Line l has with the Circle c. If 0 is returned there where was no intersection. If 1 is returned then there was one intersection and t1 holds the time. If 2 is returned then there were two intersections and the times are in t1 and t2 where t1 < t2.
int Intersect ( const Ray r,
const Circle &  c,
float &  t1,
float &  t2 
)

Intersects a Ray with a Circle and finds the enter and exit time.

Parameters:
[in] r the Ray.
[in] c the circle.
[out] t1 the time the Ray r enters the Circle c.
[out] t2 the time the Ray r exits the Circle c.
Returns:
The number of intersections the Ray r has with the Circle c. If 0 is returned there where was no intersection. If 1 is returned then there was one intersection and t1 holds the time. If 2 is returned then there were two intersections and the times are in t1 and t2 where t1 < t2.

pg 178

template<class T , class U >
T lerp ( const T &  t1,
const T &  t2,
t 
) [inline]

Linear interpolates between two objects of type T.

Template Parameters:
T the data type to linearly interpolate between. The type must support the following operators:

  1. T operator+(const &T) const
  2. T operator-(const &T) const
  3. T operator*(const &U) const
U the data type time takes. Usually float or double.
Parameters:
[in] t1 the starting point to interpolate from.
[in] t2 the ending point to interpolate to.
[in] t the time to interpolate to.
Returns:
The interpolated position between t1 and t2 such that t1 is returned when t = 0 and t2 is returned when t = 1.
template<class T >
T Maximum ( const T &  v1,
const T &  v2 
) [inline]

Determines the maximum of two elements.

Template Parameters:
T the data type of used.
Parameters:
[in] v1 the first value.
[in] v2 the second value.
Returns:
v1 if v1 >= v2, or v2 if v1 < v2.
template<class T >
T Minimum ( const T &  v1,
const T &  v2 
) [inline]

Determines the minimum of two elements.

Template Parameters:
T the data type of used.
Parameters:
[in] v1 the first value.
[in] v2 the second value.
Returns:
v1 if v1 <= v2, or v2 if v1 > v2.
AABB NormToScreenCoords ( const AABB a  )  [inline]

Converts an AABB in normalized device coordinates to absolute Screen coordinates.

Parameters:
[in] a the AABB to convert.
Returns:
The AABB a in absolute Screen coordinates.
Point2f NormToScreenCoords ( const Point2f p  ) 

Converts a point in normalized device coordinates to absolute Screen coordinates.

Parameters:
[in] p the point to convert.
See also:
ScreenToNormCoords
Returns:
The point p in absolute Screen coordinates.
float RadToDeg ( float  a  )  [inline]

Converts an angle in Radians to Degrees.

Parameters:
[in] a the angle in Radians to convert.
See also:
DegToRad
Returns:
The angle a in Degrees.
float RandomFloat ( float  min,
float  max 
) [inline]

Computes a random float in a given range.

Parameters:
[in] min the minimum end of the range.
[in] max the maximum end of the range.
Returns:
A random float in the range [min,max]
template<class T >
AABB RectToAABB ( const Rect_t< T > &  r  )  [inline]

Converts a Rect to an AABB.

Template Parameters:
T the data type of the Rect. If T is not float data may be truncated.
Parameters:
[in] r The Rect to convert.
See also:
AABBToRect
Returns:
An AABB equivalent to r.
AABB ScreenToNormCoords ( const AABB a  )  [inline]

Converts an AABB in normalized device coordinates to absolute Screen coordinates.

Parameters:
[in] a the AABB to convert.
Returns:
The AABB a in absolute Screen coordinates.
Point2f ScreenToNormCoords ( const Point2f p  ) 

Converts a point in absolute Screen coordinates to normalized device coordinates.

Parameters:
[in] p the point to convert.
See also:
NormToScreenCoords
Returns:
The point p in normalized device coordinates.
template<class T >
T Sign ( const T &  val  )  [inline]

Determines the sign of the given value.

Template Parameters:
T the data type of used.
Parameters:
[in] val the value to determine the sign of.
See also:
SignZero
Returns:
-1 if val < 0 or 1 if val >= 0.
template<class T >
T SignZero ( const T &  val  )  [inline]

Determines the sign of the given value.

Template Parameters:
T the data type of used.
Parameters:
[in] val the value to determine the sign of.
See also:
Sign
Returns:
-1 if val < 0, 0 if val == 0 or 1 if val > 0.
template<class T >
void Swap ( T &  v1,
T &  v2 
) [inline]

Swaps the given two values.

Template Parameters:
T the data type of the values to Swap.
Parameters:
[in,out] v1 will be set to v2.
[in,out] v2 will be set to v1.
uint TimeSeed (  ) 

Computes a pretty good seed for a random number generator using the system time.

Returns:
A seed value.
void Transform ( Point2f p,
uint  n,
const Matrixf m 
) [inline]

Transforms the given points.

Parameters:
[in,out] p the points to be transformed.
[in] n the number of points.
[in] m the transformation matrix.
void Transform ( Poly &  p,
const Matrixf m 
) [inline]

Transforms the given polygon.

Parameters:
[in,out] p the polygon to be transformed.
[in] m the transformation matrix.

Variable Documentation

const float _2PI = 2.0f * PI

$2\pi$

const float DEG_TO_RAD = PI / 180.0f

Degrees to Radians constant $\frac{\pi}{180}$

const float E = 2.718281828459045f

$e$

const float EPSILON = 0.0001f

Small floating point value.

const float INV_PI = 1.0f / PI

$\frac{1}{\pi}$

const float INV_SQRT2 = 1.0f / SQRT2

$\frac{1}\sqrt{2}$

const float PI = 3.141592653589793f

$\pi$

const float PI_2 = PI / 2.0f

$\frac{\pi}{2}$

const float PI_4 = PI / 4.0f

$\frac{\pi}{4}$

const float RAD_TO_DEG = 180.0f / PI

Radians to Degrees constant $\frac{180}{\pi}$

const float SQRT2 = 1.414213562373095f

$\sqrt{2}$

const float SQRT2_2 = 0.707106781186547f

$\frac{\sqrt{2}}{2}$

Generated on Mon Apr 5 19:08:04 2010 for BrickOut |ReDuX| by  doxygen 1.6.3