MathFu
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups Pages
mathfu::Vector< T, d > Class Template Reference

Vector of d elements with type T. More...

#include "mathfu/vector.h"

Detailed Description

template<class T, int d>
class mathfu::Vector< T, d >

Vector of d elements with type T.

Vector stores d elements of type T and provides a set functions to perform operations on the set of elements.

Template Parameters
Ttype of Vector elements.
ddimensions (number of elements) in the Vector structure.

Public Types

typedef T Scalar
 Element type to enable reference by other classes.
 

Public Member Functions

 Vector ()
 Create an uninitialized Vector.
 
 Vector (const Vector< T, d > &v)
 Create a vector from another vector copying each element. More...
 
template<typename U >
 Vector (const Vector< U, d > &v)
 Create a vector from another vector of a different type. More...
 
 Vector (const T &s)
 Create a vector from a single float. More...
 
 Vector (const T *a)
 Create a vector form the first d elements of an array. More...
 
 Vector (const T &s1, const T &s2)
 Create a vector from two values. More...
 
 Vector (const T &s1, const T &s2, const T &s3)
 Create a vector from three values. More...
 
 Vector (const Vector< T, 2 > &v12, const T &s3)
 Create a vector from a 2 component vector and a third value. More...
 
 Vector (const T &s1, const T &s2, const T &s3, const T &s4)
 Create a vector from four values. More...
 
 Vector (const Vector< T, 3 > &vector3, const T &value)
 Create a 4-dimensional vector from a Vector<T, 3>. More...
 
 Vector (const Vector< T, 2 > &v12, const Vector< T, 2 > &v34)
 Create a vector from two 2 component vectors. More...
 
 Vector (const VectorPacked< T, d > &vector)
 Create a vector from packed vector (VectorPacked). More...
 
T & operator() (const int i)
 Access an element of the vector. More...
 
const T & operator() (const int i) const
 Access an element of the vector. More...
 
T & operator[] (const int i)
 Access an element of the vector. More...
 
const T & operator[] (const int i) const
 Access an element of the vector. More...
 
Vector< T, 3 > xyz ()
 GLSL style 3 element accessor. More...
 
const Vector< T, 3 > xyz () const
 GLSL style 3 element accessor. More...
 
Vector< T, 2 > xy ()
 GLSL style 2 element accessor. More...
 
const Vector< T, 2 > xy () const
 GLSL style 2 element accessor. More...
 
Vector< T, 2 > zw ()
 GLSL style 2 element accessor. More...
 
const Vector< T, 2 > zw () const
 GLSL style 2 element accessor. More...
 
void Pack (VectorPacked< T, d > *const vector) const
 Pack a Vector to a packed "d" element vector structure. More...
 
LengthSquared () const
 Calculate the squared length of this vector. More...
 
Length () const
 Calculate the length of this vector. More...
 
Normalize ()
 Normalize this vector in-place. More...
 
Vector< T, d > Normalized () const
 Calculate the normalized version of this vector. More...
 

Static Public Member Functions

template<typename CompatibleT >
static Vector< T, d > FromType (const CompatibleT &compatible)
 Load from any type that is some formulation of a length d array of type T. More...
 
template<typename CompatibleT >
static CompatibleT ToType (const Vector< T, d > &v)
 Load into any type that is some formulation of a length d array of type T. More...
 
static T DotProduct (const Vector< T, d > &v1, const Vector< T, d > &v2)
 Calculate the dot product of two vectors. More...
 
static Vector< T, d > HadamardProduct (const Vector< T, d > &v1, const Vector< T, d > &v2)
 Calculate the hadamard or componentwise product of two vectors. More...
 
static Vector< T, 3 > CrossProduct (const Vector< T, 3 > &v1, const Vector< T, 3 > &v2)
 Calculate the cross product of two vectors. More...
 
static Vector< T, d > Lerp (const Vector< T, d > &v1, const Vector< T, d > &v2, const T percent)
 Linearly interpolate two vectors. More...
 
static Vector< T, d > RandomInRange (const Vector< T, d > &min, const Vector< T, d > &max)
 Generates a random vector. More...
 
static Vector< T, d > Max (const Vector< T, d > &v1, const Vector< T, d > &v2)
 Compare each component and returns max values. More...
 
static Vector< T, d > Min (const Vector< T, d > &v1, const Vector< T, d > &v2)
 Compare each component and returns min values. More...
 

Public Attributes

MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE
data_ [d]
 Elements of the vector.
 

Related Functions

(Note that these are not member functions.)

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...
 

Constructor & Destructor Documentation

template<class T, int d>
mathfu::Vector< T, d >::Vector ( const Vector< T, d > &  v)
inline

Create a vector from another vector copying each element.

Parameters
vVector that the data will be copied from.
template<class T, int d>
template<typename U >
mathfu::Vector< T, d >::Vector ( const Vector< U, d > &  v)
inlineexplicit

Create a vector from another vector of a different type.

This copies each element of a Vector which makes it possible to between vectors of different types, for example float/double/int vectors.

Parameters
vVector that the data will be copied from.
Template Parameters
Utype of Vector elements to copy.
template<class T, int d>
mathfu::Vector< T, d >::Vector ( const T &  s)
inlineexplicit

Create a vector from a single float.

Each elements is set to be equal to the value given.

Parameters
sScalar value that the vector will be initialized to.
template<class T, int d>
mathfu::Vector< T, d >::Vector ( const T *  a)
inlineexplicit

Create a vector form the first d elements of an array.

Parameters
aArray of values that the vector will be iniitlized to.
template<class T, int d>
mathfu::Vector< T, d >::Vector ( const T &  s1,
const T &  s2 
)
inline

Create a vector from two values.

Note
This method only works when the vector is of size two.
Parameters
s1Scalar value for the first element of the vector.
s2Scalar value for the second element of the vector.
template<class T, int d>
mathfu::Vector< T, d >::Vector ( const T &  s1,
const T &  s2,
const T &  s3 
)
inline

Create a vector from three values.

Note
This method only works when the vector is of size three.
Parameters
s1Scalar value for the first element of the vector.
s2Scalar value for the second element of the vector.
s3Scalar value for the third element of the vector.
template<class T, int d>
mathfu::Vector< T, d >::Vector ( const Vector< T, 2 > &  v12,
const T &  s3 
)
inline

Create a vector from a 2 component vector and a third value.

Note
This method only works when the vector is of size three.
Parameters
v12Vector containing the first 2 values.
s3Scalar value for the third element of the vector.
template<class T, int d>
mathfu::Vector< T, d >::Vector ( const T &  s1,
const T &  s2,
const T &  s3,
const T &  s4 
)
inline

Create a vector from four values.

Note
This method only works when the vector is of size four.
Parameters
s1Scalar value for the first element of the vector.
s2Scalar value for the second element of the vector.
s3Scalar value for the third element of the vector.
s4Scalar value for the forth element of the vector.
template<class T, int d>
mathfu::Vector< T, d >::Vector ( const Vector< T, 3 > &  vector3,
const T &  value 
)
inline

Create a 4-dimensional vector from a Vector<T, 3>.

The last element is initialized to the specified value.

Note
This method only works with 4 element vectors.
Parameters
vector3Vector used to initialize the first 3 elements.
valueValue used to set the last element of the vector.
template<class T, int d>
mathfu::Vector< T, d >::Vector ( const Vector< T, 2 > &  v12,
const Vector< T, 2 > &  v34 
)
inline

Create a vector from two 2 component vectors.

Note
This method only works when the vector is of size four.
Parameters
v12Vector containing the first 2 values.
v34Vector containing the last 2 values.
template<class T, int d>
mathfu::Vector< T, d >::Vector ( const VectorPacked< T, d > &  vector)
inlineexplicit

Create a vector from packed vector (VectorPacked).

Parameters
vectorPacked vector used to initialize an unpacked.

Member Function Documentation

template<class T, int d>
static Vector<T, 3> mathfu::Vector< T, d >::CrossProduct ( const Vector< T, 3 > &  v1,
const Vector< T, 3 > &  v2 
)
inlinestatic

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>
static T mathfu::Vector< T, d >::DotProduct ( const Vector< T, d > &  v1,
const Vector< T, d > &  v2 
)
inlinestatic

Calculate the dot product of two vectors.

Parameters
v1First vector.
v2Second vector.
Returns
The dot product of v1 and v2.
template<class T, int d>
template<typename CompatibleT >
static Vector<T, d> mathfu::Vector< T, d >::FromType ( const CompatibleT &  compatible)
inlinestatic

Load from any type that is some formulation of a length d array of type T.

Essentially this is just a type cast and a load, but it happens safely so that we avoid aliasing bugs.

Returns
compatible cast to Vector<T,d> and dereferenced.
template<class T, int d>
static Vector<T, d> mathfu::Vector< T, d >::HadamardProduct ( const Vector< T, d > &  v1,
const Vector< T, d > &  v2 
)
inlinestatic

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, int d>
T mathfu::Vector< T, d >::Length ( ) const
inline

Calculate the length of this vector.

Returns
The length of this vector.
template<class T, int d>
T mathfu::Vector< T, d >::LengthSquared ( ) const
inline

Calculate the squared length of this vector.

Returns
The length of this vector squared.
template<class T, int d>
static Vector<T, d> mathfu::Vector< T, d >::Lerp ( const Vector< T, d > &  v1,
const Vector< T, d > &  v2,
const T  percent 
)
inlinestatic

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>
static Vector<T, d> mathfu::Vector< T, d >::Max ( const Vector< T, d > &  v1,
const Vector< T, d > &  v2 
)
inlinestatic

Compare each component and returns max values.

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

Compare each component and returns min values.

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

Normalize this vector in-place.

Returns
The length of this vector.
template<class T, int d>
Vector<T, d> mathfu::Vector< T, d >::Normalized ( ) const
inline

Calculate the normalized version of this vector.

Returns
The normalized vector.
template<class T, int d>
T& mathfu::Vector< T, d >::operator() ( const int  i)
inline

Access an element of the vector.

Parameters
iIndex of the element to access.
Returns
A reference to the accessed data that can be modified by the caller.
template<class T, int d>
const T& mathfu::Vector< T, d >::operator() ( const int  i) const
inline

Access an element of the vector.

Parameters
iIndex of the element to access.
Returns
A reference to the accessed data.
template<class T, int d>
T& mathfu::Vector< T, d >::operator[] ( const int  i)
inline

Access an element of the vector.

Parameters
iIndex of the element to access.
Returns
A reference to the accessed data that can be modified by the caller.
template<class T, int d>
const T& mathfu::Vector< T, d >::operator[] ( const int  i) const
inline

Access an element of the vector.

Parameters
iIndex of the element to access.
Returns
A const reference to the accessed.
template<class T, int d>
void mathfu::Vector< T, d >::Pack ( VectorPacked< T, d > *const  vector) const
inline

Pack a Vector to a packed "d" element vector structure.

Parameters
vectorPacked "d" element vector to write to.
template<class T, int d>
static Vector<T, d> mathfu::Vector< T, d >::RandomInRange ( const Vector< T, d > &  min,
const Vector< T, d > &  max 
)
inlinestatic

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.
template<class T, int d>
template<typename CompatibleT >
static CompatibleT mathfu::Vector< T, d >::ToType ( const Vector< T, d > &  v)
inlinestatic

Load into any type that is some formulation of a length d array of type T.

Essentially this is just a type cast and a load, but it happens safely so that we avoid aliasing bugs.

Returns
v cast to CompatibleT and dereferenced.
template<class T, int d>
Vector<T, 2> mathfu::Vector< T, d >::xy ( )
inline

GLSL style 2 element accessor.

This only works with vectors that contain more than 2 elements.

Returns
A 2-dimensional Vector with the first 2 elements of this Vector.
template<class T, int d>
const Vector<T, 2> mathfu::Vector< T, d >::xy ( ) const
inline

GLSL style 2 element accessor.

This only works with vectors that contain more than 2 elements.

Returns
A 2-dimensional Vector with the first 2 elements of this Vector.
template<class T, int d>
Vector<T, 3> mathfu::Vector< T, d >::xyz ( )
inline

GLSL style 3 element accessor.

This only works with vectors that contain more than 3 elements.

Returns
A 3-dimensional Vector containing the first 3 elements of
template<class T, int d>
const Vector<T, 3> mathfu::Vector< T, d >::xyz ( ) const
inline

GLSL style 3 element accessor.

This only works with vectors that contain more than 3 elements.

Returns
A 3-dimensional Vector containing the first 3 elements of
template<class T, int d>
Vector<T, 2> mathfu::Vector< T, d >::zw ( )
inline

GLSL style 2 element accessor.

This only works with vectors that contain 4 elements.

Returns
A 2-dimensional Vector with the last 2 elements of this Vector.
template<class T, int d>
const Vector<T, 2> mathfu::Vector< T, d >::zw ( ) const
inline

GLSL style 2 element accessor.

This only works with vectors that contain 4 elements.

Returns
A 2-dimensional Vector with the last 2 elements of this Vector.

The documentation for this class was generated from the following file: