MathFu
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups Pages
Utility Functions

Utility functions. More...

Detailed Description

Utility functions.

Macros

#define MATHFU_STATIC_ASSERT(x)   static_assert_util<(x)>()
 Compile time assert for pre-C++11 compilers. More...
 

Functions

template<class T >
mathfu::Clamp (const T &x, const T &lower, const T &upper)
 Clamp x within [lower, upper]. More...
 
template<class T , class T2 >
mathfu::Lerp (const T &range_start, const T &range_end, const T2 &percent)
 Linearly interpolate between range_start and range_end, based on percent. More...
 
template<class T >
mathfu::Lerp (const T &range_start, const T &range_end, const T &percent)
 Linearly interpolate between range_start and range_end, based on percent. More...
 
template<class T >
bool mathfu::InRange (T val, T range_start, T range_end)
 Check if val is within [range_start..range_end). More...
 
template<class T >
mathfu::Random ()
 Generate a random value of type T. More...
 
template<class T >
mathfu::RandomRange (T range)
 Generate a random value of type T in the range -range...+range. More...
 
template<class T >
mathfu::RandomInRange (T range_start, T range_end)
 Generate a random number between [range_start, range_end]. More...
 
template<class T >
mathfu::RoundUpToPowerOf2 (T x)
 Round a value up to the nearest power of 2. More...
 
template<>
int32_t mathfu::RoundUpToPowerOf2 (int32_t x)
 Specialized version of RoundUpToPowerOf2 for int32_t.
 
template<typename T >
uint32_t mathfu::RoundUpToTypeBoundary (uint32_t v)
 Round a value up to the type's size boundary. More...
 
template<typename T , int d>
Vector< T, d > mathfu::RoundUpToPowerOf2 (const Vector< T, d > &v)
 Specialized version of RoundUpToPowerOf2 for vector.
 

Macro Definition Documentation

#define MATHFU_STATIC_ASSERT (   x)    static_assert_util<(x)>()

Compile time assert for pre-C++11 compilers.

For example:

MATHFU_STATIC_ASSERT(0 == 1);

will result in a compile error.

Function Documentation

template<class T >
T mathfu::Clamp ( const T &  x,
const T &  lower,
const T &  upper 
)

Clamp x within [lower, upper].

Note
Results are undefined if lower > upper.
Parameters
xValue to clamp.
lowerLower value of the range.
upperUpper value of the range.
Returns
Clamped value.
template<class T >
bool mathfu::InRange ( val,
range_start,
range_end 
)

Check if val is within [range_start..range_end).

Parameters
valValue to be tested.
range_startStarting point of the range (inclusive).
range_endEnding point of the range (non-inclusive).
Returns
Bool indicating success.
Template Parameters
TType of values to test.
template<class T , class T2 >
T mathfu::Lerp ( const T &  range_start,
const T &  range_end,
const T2 &  percent 
)

Linearly interpolate between range_start and range_end, based on percent.

Parameters
range_startStart of the range.
range_endEnd of the range.
percentValue between 0.0 and 1.0 used to interpolate between range_start and range_end. Where a value of 0.0 results in a return value of range_start and 1.0 results in a return value of range_end.
Returns
Value between range_start and range_end.
Template Parameters
TType of the range to interpolate over.
T2Type of the value used to perform interpolation (e.g float or double).
template<class T >
T mathfu::Lerp ( const T &  range_start,
const T &  range_end,
const T &  percent 
)

Linearly interpolate between range_start and range_end, based on percent.

Parameters
range_startStart of the range.
range_endEnd of the range.
percentValue between 0.0 and 1.0 used to interpolate between range_start and range_end. Where a value of 0.0 results in a return value of range_start and 1.0 results in a return value of range_end.
Returns
Value between range_start and range_end.
Template Parameters
TType of the range to interpolate over.
template<class T >
T mathfu::Random ( )
inline

Generate a random value of type T.

This method generates a random value of type T, greater than or equal to 0.0 and less than 1.0.

This function uses the standard C library function rand() from math.h to generate the random number.

Returns
Random number greater than or equal to 0.0 and less than 1.0.
See Also
RandomRange()
RandomInRange()
template<class T >
T mathfu::RandomInRange ( range_start,
range_end 
)
inline

Generate a random number between [range_start, range_end].

This function uses the standard C library function rand() from math.h to generate the random number.

Parameters
range_startMinimum value.
range_endMaximum value.
Returns
Random value in the range [range_start, range_end].
See Also
Lerp()
Random()
template<class T >
T mathfu::RandomRange ( range)
inline

Generate a random value of type T in the range -range...+range.

This function uses the standard C library function rand() from math.h to generate the random number.

Parameters
rangeRange of the random value to generate.
Returns
Random value in the range -range to +range
See Also
Random()
template<class T >
T mathfu::RoundUpToPowerOf2 ( x)

Round a value up to the nearest power of 2.

Parameters
xValue to round up.
Returns
Value rounded up to the nearest power of 2.
template<typename T >
uint32_t mathfu::RoundUpToTypeBoundary ( uint32_t  v)

Round a value up to the type's size boundary.

Parameters
vValue to round up.
Returns
Value rounded up to the type's size boundary.