NVIDIA Iray: Math API Home  Up
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mi::math::Vector< T, DIM > Class Template Reference

Fixed-size math vector class template with generic operations. More...

Inheritance diagram for mi::math::Vector< T, DIM >:
Inheritance graph
[legend]

Public Types

typedef Vector_struct< T, DIM >  Pod_type
  POD class corresponding to this vector. More...
 
typedef Vector_struct< T, DIM >  storage_type
  Storage class used by this vector. More...
 
typedef T  value_type
  Element type. More...
 
typedef Size  size_type
  Size type, unsigned. More...
 
typedef Difference  difference_type
  Difference type, signed. More...
 
typedef T *  pointer
  Mutable pointer to element. More...
 
typedef const T *  const_pointer
  Const pointer to element. More...
 
typedef T &  reference
  Mutable reference to element. More...
 
typedef const T &  const_reference
  Const reference to element. More...
 

Public Member Functions

T *  begin ()
  Returns the pointer to the first vector element. More...
 
const T *  begin () const
  Returns the pointer to the first vector element. More...
 
T *  end ()
  Returns the past-the-end pointer. More...
 
const T *  end () const
  Returns the past-the-end pointer. More...
 
  Vector ()
  The default constructor leaves the vector elements uninitialized. More...
 
  Vector (const Vector_struct< T, DIM > &vec)
  Constructor from underlying storage type. More...
 
  Vector (T v)
  Constructor initializes all vector elements to the value v. More...
 
template<typename Iterator >
  Vector (From_iterator_tag, Iterator p)
  Constructor requires the mi::math::FROM_ITERATOR tag as first argument and initializes the vector elements with the first DIM elements from the sequence starting at the iterator p. More...
 
template<typename T2 >
  Vector (T2 const (&array)[DIM])
  Constructor initializes the vector elements from an array of dimension DIM. More...
 
template<typename T2 >
  Vector (const Vector< T2, DIM > &other)
  Template constructor that allows explicit conversions from other vectors with assignment compatible element value type. More...
 
  Vector (T v1, T v2)
  Dedicated constructor, for dimension 2 only, that initializes the vector elements from the two elements (v1,v2). More...
 
  Vector (T v1, T v2, T v3)
  Dedicated constructor, for dimension 3 only, that initializes the vector elements from the three elements (v1,v2,v3). More...
 
  Vector (T v1, const Vector< T, 2 > &v2)
  Dedicated constructor, for dimension 3 only, that initializes the vector elements from the three elements (v1,v2.x,v2.y). More...
 
  Vector (const Vector< T, 2 > &v1, T v2)
  Dedicated constructor, for dimension 3 only, that initializes the vector elements from the three elements (v1.x,v1.y,v2). More...
 
  Vector (T v1, T v2, T v3, T v4)
  Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1,v2,v3,v4). More...
 
  Vector (T v1, T v2, const Vector< T, 2 > &v3)
  Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1,v2,v3.x,v3.y). More...
 
  Vector (T v1, const Vector< T, 2 > &v2, T v3)
  Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1,v2.x,v2.y,v3). More...
 
  Vector (const Vector< T, 2 > &v1, T v2, T v3)
  Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1.x,v1.y,v2,v3). More...
 
  Vector (const Vector< T, 2 > &v1, const Vector< T, 2 > &v2)
  Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1.x,v1.y,v2.x,v2.y). More...
 
  Vector (T v1, const Vector< T, 3 > &v2)
  Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1,v2.x,v2.y,v2.z). More...
 
  Vector (const Vector< T, 3 > &v1, T v2)
  Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1.x,v1.y,v1.z,v2). More...
 
  Vector (const Color_struct &color)
  Dedicated constructor, for dimension 4 only, that initializes the vector elements from a color interpreted as a vector (r,g,b,a). More...
 
Vector operator= (const Vector &other)
  Assignment. More...
 
Vector operator= (T s)
  Assignment from a scalar, setting all elements to s. More...
 
Vector operator= (const Color_struct &color)
  Assignment, for dimension 4 only, that assigns color interpreted as a vector (r,g,b,a) to this vector. More...
 
T &  operator[] (Size i)
  Accesses the i-th vector element. More...
 
const T &  operator[] (Size i) const
  Accesses the i-th vector element. More...
 
const T &  get (Size i) const
  Returns the i-th vector element. More...
 
void  set (Size i, T value)
  Sets the i-th vector element to value. More...
 
bool  normalize ()
  Normalizes this vector to unit length. More...
 
bool  operator== (Vector_proxy_< T, DIM > rhs) const
  Returns true if lhs is elementwise equal to rhs. More...
 
bool  operator!= (Vector_proxy_< T, DIM > rhs) const
  Returns true if lhs is elementwise not equal to rhs. More...
 
bool  operator< (Vector_proxy_< T, DIM > rhs) const
  Returns true if lhs is lexicographically less than rhs. More...
 
bool  operator<= (Vector_proxy_< T, DIM > rhs) const
  Returns true if lhs is lexicographically less than or equal to rhs. More...
 
bool  operator> (Vector_proxy_< T, DIM > rhs) const
  Returns true if lhs is lexicographically greater than rhs. More...
 
bool  operator>= (Vector_proxy_< T, DIM > rhs) const
  Returns true if lhs is lexicographically greater than or equal to rhs. More...
 

Static Public Member Functions

static Size  size ()
  Constant size of the vector. More...
 
static Size  max_size ()
  Constant maximum size of the vector. More...
 

Public Attributes

elements [DIM]
  coordinates. More...
 

Static Public Attributes

static const Size  DIMENSION = DIM
  Constant dimension of the vector. More...
 
static const Size  SIZE = DIM
  Constant size of the vector. More...
 

Detailed Description

template<class T, Size DIM>
class mi::math::Vector< T, DIM >

Fixed-size math vector class template with generic operations.

This class template provides array-like storage for DIM many values of an arithmetic type T. Several functions and arithmetic operators support the work with vectors.

An instantiation of the vector class template is a model of the STL container concept. It provides random access to its elements and corresponding random access iterators.

The template parameters have the following requirements:

  • T: an arithmetic type supporting + - * / == != < > <= >= sqrt() .
  • DIM: a value > 0 of type mi::Size that defines the fixed dimension of the vector.

Depending on the dimension DIM, the mi::math::Vector class template offers element access through the conventional data members named x, y, z, and w. Assuming a vector vec of suitable dimension, the following expressions are valid

  • vec.x; equivalent to vec[0] and available if 1 <= DIM <= 4.
  • vec.y; equivalent to vec[1] and available if 2 <= DIM <= 4.
  • vec.z; equivalent to vec[2] and available if 3 <= DIM <= 4.
  • vec.w; equivalent to vec[3] and available if 4 <= DIM <= 4.

These data members allow users to access elements, as illustrated in the following example:

vec.x = 4.0;
See Also
For the free functions and operators available for vectors and vector-like classes see Math Vector Class.
The underlying POD type mi::math::Vector_struct.
Include File:
#include <mi/math/vector.h>

Member Typedef Documentation

template<class T, Size DIM>
typedef const T* mi::math::Vector< T, DIM >::const_pointer

Const pointer to element.

template<class T, Size DIM>
typedef const T& mi::math::Vector< T, DIM >::const_reference

Const reference to element.

template<class T, Size DIM>
typedef Difference mi::math::Vector< T, DIM >::difference_type

Difference type, signed.

template<class T, Size DIM>
typedef Vector_struct<T,DIM> mi::math::Vector< T, DIM >::Pod_type

POD class corresponding to this vector.

template<class T, Size DIM>
typedef T* mi::math::Vector< T, DIM >::pointer

Mutable pointer to element.

template<class T, Size DIM>
typedef T& mi::math::Vector< T, DIM >::reference

Mutable reference to element.

template<class T, Size DIM>
typedef Size mi::math::Vector< T, DIM >::size_type

Size type, unsigned.

template<class T, Size DIM>
typedef Vector_struct<T,DIM> mi::math::Vector< T, DIM >::storage_type

Storage class used by this vector.

template<class T, Size DIM>
typedef T mi::math::Vector< T, DIM >::value_type

Element type.

Constructor & Destructor Documentation

template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( )
inline

The default constructor leaves the vector elements uninitialized.

template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( const Vector_struct< T, DIM > &  vec)
inline

Constructor from underlying storage type.

template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( v)
inlineexplicit

Constructor initializes all vector elements to the value v.

template<class T, Size DIM>
template<typename Iterator >
mi::math::Vector< T, DIM >::Vector ( From_iterator_tag  ,
Iterator  p 
)
inline

Constructor requires the mi::math::FROM_ITERATOR tag as first argument and initializes the vector elements with the first DIM elements from the sequence starting at the iterator p.

Iterator must be a model of an input iterator. The value type of Iterator must be assignment compatible with the vector elements type T.

An example:

std::vector<int> data( 10, 42); // ten elements of value 42
template<class T, Size DIM>
template<typename T2 >
mi::math::Vector< T, DIM >::Vector ( T2 const (&)  array[DIM])
inlineexplicit

Constructor initializes the vector elements from an array of dimension DIM.

The value type T2 of the array must be assignment compatible with the vector elements type T.

An example:

int data[3] = { 1, 2, 4};
template<class T, Size DIM>
template<typename T2 >
mi::math::Vector< T, DIM >::Vector ( const Vector< T2, DIM > &  other)
inlineexplicit

Template constructor that allows explicit conversions from other vectors with assignment compatible element value type.

template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( v1,
v2 
)
inline

Dedicated constructor, for dimension 2 only, that initializes the vector elements from the two elements (v1,v2).

Precondition
DIM == 2
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( v1,
v2,
v3 
)
inline

Dedicated constructor, for dimension 3 only, that initializes the vector elements from the three elements (v1,v2,v3).

Precondition
DIM == 3
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( v1,
const Vector< T, 2 > &  v2 
)
inline

Dedicated constructor, for dimension 3 only, that initializes the vector elements from the three elements (v1,v2.x,v2.y).

Precondition
DIM == 3
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( const Vector< T, 2 > &  v1,
v2 
)
inline

Dedicated constructor, for dimension 3 only, that initializes the vector elements from the three elements (v1.x,v1.y,v2).

Precondition
DIM == 3
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( v1,
v2,
v3,
v4 
)
inline

Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1,v2,v3,v4).

Precondition
DIM == 4
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( v1,
v2,
const Vector< T, 2 > &  v3 
)
inline

Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1,v2,v3.x,v3.y).

Precondition
DIM == 4
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( v1,
const Vector< T, 2 > &  v2,
v3 
)
inline

Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1,v2.x,v2.y,v3).

Precondition
DIM == 4
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( const Vector< T, 2 > &  v1,
v2,
v3 
)
inline

Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1.x,v1.y,v2,v3).

Precondition
DIM == 4
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( const Vector< T, 2 > &  v1,
const Vector< T, 2 > &  v2 
)
inline

Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1.x,v1.y,v2.x,v2.y).

Precondition
DIM == 4
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( v1,
const Vector< T, 3 > &  v2 
)
inline

Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1,v2.x,v2.y,v2.z).

Precondition
DIM == 4
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( const Vector< T, 3 > &  v1,
v2 
)
inline

Dedicated constructor, for dimension 4 only, that initializes the vector elements from the four elements (v1.x,v1.y,v1.z,v2).

Precondition
DIM == 4
template<class T, Size DIM>
mi::math::Vector< T, DIM >::Vector ( const Color_struct color)
inlineexplicit

Dedicated constructor, for dimension 4 only, that initializes the vector elements from a color interpreted as a vector (r,g,b,a).

Precondition
DIM == 4

Member Function Documentation

template<class T, Size DIM>
T* mi::math::Vector< T, DIM >::begin ( )
inline

Returns the pointer to the first vector element.

template<class T, Size DIM>
const T* mi::math::Vector< T, DIM >::begin ( ) const
inline

Returns the pointer to the first vector element.

template<class T, Size DIM>
T* mi::math::Vector< T, DIM >::end ( )
inline

Returns the past-the-end pointer.

The range [begin(),end()) forms the range over all vector elements.

template<class T, Size DIM>
const T* mi::math::Vector< T, DIM >::end ( ) const
inline

Returns the past-the-end pointer.

The range [begin(),end()) forms the range over all vector elements.

template<class T, Size DIM>
const T& mi::math::Vector< T, DIM >::get ( Size  i) const
inline

Returns the i-th vector element.

Precondition
0 <= i < size()
template<class T, Size DIM>
static Size mi::math::Vector< T, DIM >::max_size ( )
inlinestatic

Constant maximum size of the vector.

template<class T, Size DIM>
bool mi::math::Vector< T, DIM >::normalize ( )
inline

Normalizes this vector to unit length.

Returns false if normalization fails because the vector norm is zero, and true otherwise. This vector remains unchanged if normalization failed.

Uses an unqualified call to sqrt(...) on the vector element type.

template<class T, Size DIM>
bool mi::math::Vector< T, DIM >::operator!= ( Vector_proxy_< T, DIM >  rhs) const
inline

Returns true if lhs is elementwise not equal to rhs.

template<class T, Size DIM>
bool mi::math::Vector< T, DIM >::operator< ( Vector_proxy_< T, DIM >  rhs) const
inline

Returns true if lhs is lexicographically less than rhs.

See Also
Definition: Lexicographic order and comparison
template<class T, Size DIM>
bool mi::math::Vector< T, DIM >::operator<= ( Vector_proxy_< T, DIM >  rhs) const
inline

Returns true if lhs is lexicographically less than or equal to rhs.

See Also
Definition: Lexicographic order and comparison
template<class T, Size DIM>
Vector& mi::math::Vector< T, DIM >::operator= ( const Vector< T, DIM > &  other)
inline

Assignment.

template<class T, Size DIM>
Vector& mi::math::Vector< T, DIM >::operator= ( s)
inline

Assignment from a scalar, setting all elements to s.

template<class T, Size DIM>
Vector& mi::math::Vector< T, DIM >::operator= ( const Color_struct color)
inline

Assignment, for dimension 4 only, that assigns color interpreted as a vector (r,g,b,a) to this vector.

Precondition
DIM == 4
template<class T, Size DIM>
bool mi::math::Vector< T, DIM >::operator== ( Vector_proxy_< T, DIM >  rhs) const
inline

Returns true if lhs is elementwise equal to rhs.

template<class T, Size DIM>
bool mi::math::Vector< T, DIM >::operator> ( Vector_proxy_< T, DIM >  rhs) const
inline

Returns true if lhs is lexicographically greater than rhs.

See Also
Definition: Lexicographic order and comparison
template<class T, Size DIM>
bool mi::math::Vector< T, DIM >::operator>= ( Vector_proxy_< T, DIM >  rhs) const
inline

Returns true if lhs is lexicographically greater than or equal to rhs.

See Also
Definition: Lexicographic order and comparison
template<class T, Size DIM>
T& mi::math::Vector< T, DIM >::operator[] ( Size  i)
inline

Accesses the i-th vector element.

Precondition
0 <= i < size()
template<class T, Size DIM>
const T& mi::math::Vector< T, DIM >::operator[] ( Size  i) const
inline

Accesses the i-th vector element.

Precondition
0 <= i < size()
template<class T, Size DIM>
void mi::math::Vector< T, DIM >::set ( Size  i,
value 
)
inline

Sets the i-th vector element to value.

Precondition
0 <= i < size()
template<class T, Size DIM>
static Size mi::math::Vector< T, DIM >::size ( )
inlinestatic

Constant size of the vector.

Member Data Documentation

template<class T, Size DIM>
const Size mi::math::Vector< T, DIM >::DIMENSION = DIM
static

Constant dimension of the vector.

template<typename T, Size DIM>
T mi::math::Vector_struct< T, DIM >::elements[DIM]
inherited

coordinates.

template<class T, Size DIM>
const Size mi::math::Vector< T, DIM >::SIZE = DIM
static

Constant size of the vector.