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

Vector class and functions. More...

Detailed Description

Vector class and functions.

Classes

class  mathfu::VectorPacked< T, d >
 Packed N-dimensional vector. More...
 
class  mathfu::Vector< T, d >
 Vector of d elements with type T. More...
 

Functions

template<class T , int d>
bool mathfu::operator== (const Vector< T, d > &lhs, const Vector< T, d > &rhs)
 Compare 2 Vectors of the same size for equality. More...
 
template<class T , int d>
bool mathfu::operator!= (const Vector< T, d > &lhs, const Vector< T, d > &rhs)
 Compare 2 Vectors of the same size for inequality. More...
 
template<class T , int d>
Vector< T, d > mathfu::operator- (const Vector< T, d > &v)
 Negate all elements of the Vector. More...
 
template<class T , int d>
Vector< T, d > mathfu::operator* (const Vector< T, d > &lhs, const Vector< T, d > &rhs)
 Multiply a vector by another Vector. More...
 
template<class T , int d>
Vector< T, d > mathfu::operator/ (const Vector< T, d > &lhs, const Vector< T, d > &rhs)
 Divide a vector by another Vector. More...
 
template<class T , int d>
Vector< T, d > mathfu::operator+ (const Vector< T, d > &lhs, const Vector< T, d > &rhs)
 Add a vector with another Vector. More...
 
template<class T , int d>
Vector< T, d > mathfu::operator- (const Vector< T, d > &lhs, const Vector< T, d > &rhs)
 subtract a vector with another Vector. More...
 
template<class T , int d>
Vector< T, d > mathfu::operator* (const Vector< T, d > &v, const T &s)
 Multiply a vector with a scalar. More...
 
template<class T , int d>
Vector< T, d > mathfu::operator+ (const Vector< T, d > &v, const T &s)
 Add a scalar to all elements of a vector. More...
 
template<class T , int d>
Vector< T, d > mathfu::operator- (const Vector< T, d > &v, const T &s)
 Subtract a scalar from all elements of a vector. More...
 
template<class T , int d>
Vector< T, d > & mathfu::operator*= (Vector< T, d > &lhs, const Vector< T, d > &rhs)
 Multiply (in-place) a vector with another Vector. More...
 
template<class T , int d>
Vector< T, d > & mathfu::operator/= (Vector< T, d > &lhs, const Vector< T, d > &rhs)
 Divide (in-place) a vector by another Vector. More...
 
template<class T , int d>
Vector< T, d > & mathfu::operator+= (Vector< T, d > &lhs, const Vector< T, d > &rhs)
 Add (in-place) a vector with another Vector. More...
 
template<class T , int d>
Vector< T, d > & mathfu::operator-= (Vector< T, d > &lhs, const Vector< T, d > &rhs)
 Subtract (in-place) another Vector from a vector. More...
 
template<class T , int d>
Vector< T, d > & mathfu::operator*= (Vector< T, d > &v, const T &s)
 Multiply (in-place) each element of a vector with a scalar. More...
 
template<class T , int d>
Vector< T, d > & mathfu::operator/= (Vector< T, d > &v, const T &s)
 Divide (in-place) each element of a vector by a scalar. More...
 
template<class T , int d>
Vector< T, d > & mathfu::operator+= (Vector< T, d > &v, const T &s)
 Add (in-place) a scalar to each element of a vector. More...
 
template<class T , int d>
Vector< T, d > & mathfu::operator-= (Vector< T, d > &v, const T &s)
 Subtract (in-place) a scalar from each element of a vector. More...
 
template<class T , int d>
Vector< T, d > mathfu::HadamardProductHelper (const Vector< T, d > &v1, const Vector< T, d > &v2)
 Calculate the hadamard or componentwise product of two vectors. More...
 
template<class T >
Vector< T, 3 > mathfu::CrossProductHelper (const Vector< T, 3 > &v1, const Vector< T, 3 > &v2)
 Calculate the cross product of two vectors. More...
 
template<class T , int d>
mathfu::LengthSquaredHelper (const Vector< T, d > &v)
 Calculate the squared length of a vector. More...
 
template<class T , int d>
mathfu::LengthHelper (const Vector< T, d > &v)
 Calculate the length of a vector. More...
 
template<class T , int d>
mathfu::NormalizeHelper (Vector< T, d > &v)
 Normalize a vector in-place. More...
 
template<class T , int d>
Vector< T, d > mathfu::NormalizedHelper (const Vector< T, d > &v)
 Calculate the normalized version of a vector. More...
 
template<class T , int d>
Vector< T, d > mathfu::LerpHelper (const Vector< T, d > &v1, const Vector< T, d > &v2, const T percent)
 Linearly interpolate two vectors. More...
 
template<class T , int d>
Vector< T, d > mathfu::RandomInRangeHelper (const Vector< T, d > &min, const Vector< T, d > &max)
 Generates a random vector. More...
 
template<class T , int d>
Vector< T, d > mathfu::MaxHelper (const Vector< T, d > &v1, const Vector< T, d > &v2)
 Compare each component and returns max values. More...
 
template<class T , int d>
Vector< T, d > mathfu::MinHelper (const Vector< T, d > &v1, const Vector< T, d > &v2)
 Compare each component and returns min values. More...
 
template<class T >
bool mathfu::InRange2D (const mathfu::Vector< T, 2 > &val, const mathfu::Vector< T, 2 > &range_start, const mathfu::Vector< T, 2 > &range_end)
 Check if val is within [range_start..range_end), denoting a rectangular area. More...
 
template<class T , int d>
Vector< T, d > operator* (const T &s, const Vector< T, d > &v)
 Multiply a Vector by a scalar. More...
 
template<class T , int d>
Vector< T, d > operator/ (const Vector< T, d > &v, const T &s)
 Divide a Vector by a scalar. More...
 
template<class T , int d>
Vector< T, d > operator+ (const T &s, const Vector< T, d > &v)
 Add a scalar to each element of a Vector. More...
 
template<class T , int d>
Vector< T, d > operator- (const T &s, const Vector< T, d > &v)
 Subtract a scalar from each element of a Vector. More...
 

Function Documentation

template<class T >
Vector<T, 3> mathfu::CrossProductHelper ( const Vector< T, 3 > &  v1,
const Vector< T, 3 > &  v2 
)
inline

Calculate the cross product of two vectors.

Note that this function is only defined for 3-dimensional Vectors.

Parameters
v1First vector.
v2Second vector.
Returns
The cross product of v1 and v2.
template<class T , int d>
Vector<T, d> mathfu::HadamardProductHelper ( const Vector< T, d > &  v1,
const Vector< T, d > &  v2 
)
inline

Calculate the hadamard or componentwise product of two vectors.

Parameters
v1First vector.
v2Second vector.
Returns
The hadamard product of v1 and v2.
template<class T >
bool mathfu::InRange2D ( const mathfu::Vector< T, 2 > &  val,
const mathfu::Vector< T, 2 > &  range_start,
const mathfu::Vector< T, 2 > &  range_end 
)

Check if val is within [range_start..range_end), denoting a rectangular area.

Parameters
val2D vector 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 vector components to test.
template<class T , int d>
T mathfu::LengthHelper ( const Vector< T, d > &  v)
inline

Calculate the length of a vector.

Parameters
vVector to get the squared length of.
Returns
The length of the vector.
template<class T , int d>
T mathfu::LengthSquaredHelper ( const Vector< T, d > &  v)
inline

Calculate the squared length of a vector.

Parameters
vVector to get the squared length of.
Returns
The length of the vector squared.
template<class T , int d>
Vector<T, d> mathfu::LerpHelper ( const Vector< T, d > &  v1,
const Vector< T, d > &  v2,
const T  percent 
)
inline

Linearly interpolate two vectors.

Parameters
v1First vector.
v2Second vector.
percentPercentage from v1 to v2 in range 0.0...1.0.
Returns
The hadamard product of v1 and v2.
template<class T , int d>
Vector<T, d> mathfu::MaxHelper ( const Vector< T, d > &  v1,
const Vector< T, d > &  v2 
)
inline

Compare each component and returns max values.

Parameters
v1First vector.
v2Second vector.
Returns
Max value of v1 and v2.
template<class T , int d>
Vector<T, d> mathfu::MinHelper ( const Vector< T, d > &  v1,
const Vector< T, d > &  v2 
)
inline

Compare each component and returns min values.

Parameters
v1First vector.
v2Second vector.
Returns
Min value of v1 and v2.
template<class T , int d>
Vector<T, d> mathfu::NormalizedHelper ( const Vector< T, d > &  v)
inline

Calculate the normalized version of a vector.

Parameters
vVector to get the squared length of.
Returns
The normalized vector.
template<class T , int d>
T mathfu::NormalizeHelper ( Vector< T, d > &  v)
inline

Normalize a vector in-place.

Parameters
vVector to get the squared length of.
Returns
The length of the vector.
template<class T , int d>
bool mathfu::operator!= ( const Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

Compare 2 Vectors of the same size for inequality.

Returns
true if the elements of two vectors differ, false otherwise.
template<class T , int d>
Vector< T, d > operator* ( const T &  s,
const Vector< T, d > &  v 
)
related

Multiply a Vector by a scalar.

Multiplies each component of the specified Vector with a scalar.

Parameters
sscalar to multiply.
vVector to multiply.
Returns
Vector containing the result.
template<class T , int d>
Vector<T, d> mathfu::operator* ( const Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

Multiply a vector by another Vector.

In line with GLSL, this performs component-wise multiplication.

Parameters
lhsFirst vector to use as a starting point.
rhsSecond vector to multiply by.
Returns
A new Vector containing the result.
template<class T , int d>
Vector<T, d> mathfu::operator* ( const Vector< T, d > &  v,
const T &  s 
)
inline

Multiply a vector with a scalar.

Parameters
vVector for the operation.
sA scalar to multiply the vector with.
Returns
A new vector containing the result.
template<class T , int d>
Vector<T, d>& mathfu::operator*= ( Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

Multiply (in-place) a vector with another Vector.

In line with GLSL, this performs component-wise multiplication.

Parameters
lhsFirst vector to use as a starting point.
rhsSecond vector to multiply by.
Returns
A reference to the input v vector.
template<class T , int d>
Vector<T, d>& mathfu::operator*= ( Vector< T, d > &  v,
const T &  s 
)
inline

Multiply (in-place) each element of a vector with a scalar.

Parameters
vVector for the operation.
sA scalar to multiply the vector with.
Returns
A reference to the input v vector.
template<class T , int d>
Vector< T, d > operator+ ( const T &  s,
const Vector< T, d > &  v 
)
related

Add a scalar to each element of a Vector.

Parameters
sscalar to add to each element of a Vector.
vVector to add the scalar to.
Returns
Vector containing the result.
template<class T , int d>
Vector<T, d> mathfu::operator+ ( const Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

Add a vector with another Vector.

Parameters
lhsFirst vector to use as a starting point.
rhsSecond vector to add by.
Returns
A new vector containing the result.
template<class T , int d>
Vector<T, d> mathfu::operator+ ( const Vector< T, d > &  v,
const T &  s 
)
inline

Add a scalar to all elements of a vector.

Parameters
vVector for the operation.
sA scalar to add to the vector.
Returns
A new vector containing the result.
template<class T , int d>
Vector<T, d>& mathfu::operator+= ( Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

Add (in-place) a vector with another Vector.

Parameters
lhsFirst vector to use as a starting point.
rhsSecond vector to add.
Returns
A reference to the input v vector.
template<class T , int d>
Vector<T, d>& mathfu::operator+= ( Vector< T, d > &  v,
const T &  s 
)
inline

Add (in-place) a scalar to each element of a vector.

Parameters
vVector for the operation.
sA scalar to add the vector to.
Returns
A reference to the input v vector.
template<class T , int d>
Vector<T, d> mathfu::operator- ( const Vector< T, d > &  v)
inline

Negate all elements of the Vector.

Returns
A new Vector containing the result.
template<class T , int d>
Vector< T, d > operator- ( const T &  s,
const Vector< T, d > &  v 
)
related

Subtract a scalar from each element of a Vector.

Parameters
sscalar to subtract from each element of a Vector.
vVector to subtract the scalar from.
Returns
Vector containing the result.
template<class T , int d>
Vector<T, d> mathfu::operator- ( const Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

subtract a vector with another Vector.

Parameters
lhsFirst vector to use as a starting point.
rhsSecond vector to subtract by.
Returns
A new vector containing the result.
template<class T , int d>
Vector<T, d> mathfu::operator- ( const Vector< T, d > &  v,
const T &  s 
)
inline

Subtract a scalar from all elements of a vector.

Parameters
vVector for the operation.
sA scalar to subtract from a vector.
Returns
A new vector that stores the result.
template<class T , int d>
Vector<T, d>& mathfu::operator-= ( Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

Subtract (in-place) another Vector from a vector.

Parameters
lhsFirst vector to use as a starting point.
rhsSecond vector to subtract by.
Returns
A reference to the input v vector.
template<class T , int d>
Vector<T, d>& mathfu::operator-= ( Vector< T, d > &  v,
const T &  s 
)
inline

Subtract (in-place) a scalar from each element of a vector.

Parameters
vVector for the operation.
sA scalar to subtract from the vector.
Returns
A reference to the input v vector.
template<class T , int d>
Vector< T, d > operator/ ( const Vector< T, d > &  v,
const T &  s 
)
related

Divide a Vector by a scalar.

Divides each component of the specified Vector by a scalar.

Parameters
vVector to be divided.
sscalar to divide the vector by.
Returns
Vector containing the result.
template<class T , int d>
Vector<T, d> mathfu::operator/ ( const Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

Divide a vector by another Vector.

In line with GLSL, this performs component-wise division.

Parameters
lhsFirst vector to use as a starting point.
rhsSecond vector to divide by.
Returns
A new Vector containing the result.
template<class T , int d>
Vector<T, d>& mathfu::operator/= ( Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

Divide (in-place) a vector by another Vector.

In line with GLSL, this performs component-wise division.

Parameters
lhsFirst vector to use as a starting point.
rhsSecond vector to divide by.
Returns
A reference to the input v vector.
template<class T , int d>
Vector<T, d>& mathfu::operator/= ( Vector< T, d > &  v,
const T &  s 
)
inline

Divide (in-place) each element of a vector by a scalar.

Parameters
vVector for the operation.
sA scalar to divide the vector by.
Returns
A reference to the input v vector.
template<class T , int d>
bool mathfu::operator== ( const Vector< T, d > &  lhs,
const Vector< T, d > &  rhs 
)
inline

Compare 2 Vectors of the same size for equality.

Note
: The likelyhood of two float values being the same is very small. Instead consider comparing the difference between two float vectors using LengthSquared() with an epsilon value. For example, v1.LengthSquared(v2) < epsilon.
Returns
true if the 2 vectors contains the same value, false otherwise.
template<class T , int d>
Vector<T, d> mathfu::RandomInRangeHelper ( const Vector< T, d > &  min,
const Vector< T, d > &  max 
)
inline

Generates a random vector.

The range of each component is bounded by min and max.

Parameters
minMinimum value of the vector.
maxMaximum value of the vector.