template<std::size_t size, class T>
Magnum::Math::Vector class

Vector.

Template parameters
size Vector size
T Underlying data type

See Operations with matrices and vectors for brief introduction.

Public types

enum (anonymous): std::size_t { Size = size }
using Type = T
Underlying data type.

Public static functions

static auto from(T* data) -> Vector<size, T>&
Vector from array.
static auto from(const T* data) -> const Vector<size, T>&
template<std::size_t otherSize>
static auto pad(const Vector<otherSize, T>& a, T value = T(0)) -> Vector<size, T> constexpr
Pad vector.

Constructors, destructors, conversion operators

Vector(ZeroInitT = ZeroInit) constexpr noexcept
Default constructor.
Vector(NoInitT) explicit noexcept
Construct vector without initializing the contents.
template<class ... U>
Vector(T first, U... next) constexpr noexcept
Construct vector from components.
Vector(T value) explicit constexpr noexcept
Construct vector with one value for all components.
template<class U>
Vector(const Vector<size, U>& other) explicit constexpr noexcept
Construct vector from another of different type.
template<class U, class V = decltype(Implementation::VectorConverter<size, T, U>::from(std::declval<U>()))>
Vector(const U& other) explicit constexpr noexcept
Construct vector from external representation.
Vector(const Vector<size, T>&) noexcept defaulted constexpr
Copy constructor.
template<class U, class V = decltype(Implementation::VectorConverter<size, T, U>::to(std::declval<Vector<size, T>>()))>
operator U() const explicit constexpr
Convert vector to external representation.

Public functions

auto data() -> T*
Raw data.
auto data() const -> const T* constexpr
auto operator[](std::size_t pos) -> T&
Value at given position.
auto operator[](std::size_t pos) const -> T constexpr
auto operator==(const Vector<size, T>& other) const -> bool
Equality comparison.
auto operator!=(const Vector<size, T>& other) const -> bool
Non-equality comparison.
auto operator<(const Vector<size, T>& other) const -> BoolVector<size>
Component-wise less than.
auto operator<=(const Vector<size, T>& other) const -> BoolVector<size>
Component-wise less than or equal.
auto operator>=(const Vector<size, T>& other) const -> BoolVector<size>
Component-wise greater than or equal.
auto operator>(const Vector<size, T>& other) const -> BoolVector<size>
Component-wise greater than.
auto isZero() const -> bool
Whether the vector is zero.
auto isNormalized() const -> bool
Whether the vector is normalized.
auto operator-() const -> Vector<size, T>
Negated vector.
auto operator+=(const Vector<size, T>& other) -> Vector<size, T>&
Add and assign vector.
auto operator+(const Vector<size, T>& other) const -> Vector<size, T>
Add vector.
auto operator-=(const Vector<size, T>& other) -> Vector<size, T>&
Subtract and assign vector.
auto operator-(const Vector<size, T>& other) const -> Vector<size, T>
Subtract vector.
auto operator*=(T number) -> Vector<size, T>&
Multiply vector with number and assign.
auto operator*(T number) const -> Vector<size, T>
Multiply vector with number.
auto operator/=(T number) -> Vector<size, T>&
Divide vector with number and assign.
auto operator/(T number) const -> Vector<size, T>
Divide vector with number.
auto operator*=(const Vector<size, T>& other) -> Vector<size, T>&
Multiply vector component-wise and assign.
auto operator*(const Vector<size, T>& other) const -> Vector<size, T>
Multiply vector component-wise.
auto operator/=(const Vector<size, T>& other) -> Vector<size, T>&
Divide vector component-wise and assign.
auto operator/(const Vector<size, T>& other) const -> Vector<size, T>
Divide vector component-wise.
auto dot() const -> T
Dot product of the vector.
auto length() const -> T
Vector length.
auto lengthInverted() const -> T
Inverse vector length.
auto normalized() const -> Vector<size, T>
Normalized vector (of unit length)
auto resized(T length) const -> Vector<size, T>
Resized vector.
auto projected(const Vector<size, T>& line) const -> Vector<size, T>
Vector projected onto line.
auto projectedOntoNormalized(const Vector<size, T>& line) const -> Vector<size, T>
Vector projected onto normalized line.
auto flipped() const -> Vector<size, T> constexpr
Flipped vector.
auto sum() const -> T
Sum of values in the vector.
auto product() const -> T
Product of values in the vector.
auto min() const -> T
Minimal value in the vector.
auto max() const -> T
Maximal value in the vector.
auto minmax() const -> std::pair<T, T>
Minimal and maximal value in the vector.

Enum documentation

template<std::size_t size, class T>
enum Magnum::Math::Vector<size, T>::(anonymous): std::size_t

Enumerators
Size

Vector size

Function documentation

template<std::size_t size, class T>
static Vector<size, T>& Magnum::Math::Vector<size, T>::from(T* data)

Vector from array.

Returns Reference to the data as if it was Vector, thus doesn't perform any copying.

template<std::size_t size, class T>
static const Vector<size, T>& Magnum::Math::Vector<size, T>::from(const T* data)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<std::size_t size, class T> template<std::size_t otherSize>
static Vector<size, T> Magnum::Math::Vector<size, T>::pad(const Vector<otherSize, T>& a, T value = T(0)) constexpr

Pad vector.

If size of a is smaller than Size, it is padded from right with value, otherwise it's cut.

template<std::size_t size, class T>
Magnum::Math::Vector<size, T>::Vector(ZeroInitT = ZeroInit) constexpr noexcept

Default constructor.

\[ \boldsymbol v = \boldsymbol 0 \]

template<std::size_t size, class T> template<class U>
Magnum::Math::Vector<size, T>::Vector(const Vector<size, U>& other) explicit constexpr noexcept

Construct vector from another of different type.

Performs only default casting on the values, no rounding or anything else. Example usage:

Vector<4, Float> floatingPoint(1.3f, 2.7f, -15.0f, 7.0f);
Vector<4, Byte> integral(floatingPoint);
// integral == {1, 2, -15, 7}

template<std::size_t size, class T>
T* Magnum::Math::Vector<size, T>::data()

Raw data.

Returns One-dimensional array of size length.

template<std::size_t size, class T>
const T* Magnum::Math::Vector<size, T>::data() const constexpr

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<std::size_t size, class T>
T& Magnum::Math::Vector<size, T>::operator[](std::size_t pos)

Value at given position.

template<std::size_t size, class T>
T Magnum::Math::Vector<size, T>::operator[](std::size_t pos) const constexpr

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<std::size_t size, class T>
bool Magnum::Math::Vector<size, T>::isZero() const

Whether the vector is zero.

\[ |\boldsymbol a \cdot \boldsymbol a - 0| < \epsilon^2 \cong \epsilon \]

template<std::size_t size, class T>
bool Magnum::Math::Vector<size, T>::isNormalized() const

Whether the vector is normalized.

The vector is normalized if it has unit length:

\[ |\boldsymbol a \cdot \boldsymbol a - 1| < 2 \epsilon + \epsilon^2 \cong 2 \epsilon \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::operator-() const

Negated vector.

\[ \boldsymbol b_i = -\boldsymbol a_i \]

template<std::size_t size, class T>
Vector<size, T>& Magnum::Math::Vector<size, T>::operator+=(const Vector<size, T>& other)

Add and assign vector.

The computation is done in-place.

\[ \boldsymbol a_i = \boldsymbol a_i + \boldsymbol b_i \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::operator+(const Vector<size, T>& other) const

Add vector.

template<std::size_t size, class T>
Vector<size, T>& Magnum::Math::Vector<size, T>::operator-=(const Vector<size, T>& other)

Subtract and assign vector.

The computation is done in-place.

\[ \boldsymbol a_i = \boldsymbol a_i - \boldsymbol b_i \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::operator-(const Vector<size, T>& other) const

Subtract vector.

template<std::size_t size, class T>
Vector<size, T>& Magnum::Math::Vector<size, T>::operator*=(T number)

Multiply vector with number and assign.

The computation is done in-place.

\[ \boldsymbol a_i = b \boldsymbol a_i \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::operator*(T number) const

Multiply vector with number.

template<std::size_t size, class T>
Vector<size, T>& Magnum::Math::Vector<size, T>::operator/=(T number)

Divide vector with number and assign.

The computation is done in-place.

\[ \boldsymbol a_i = \frac{\boldsymbol a_i} b \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::operator/(T number) const

Divide vector with number.

template<std::size_t size, class T>
Vector<size, T>& Magnum::Math::Vector<size, T>::operator*=(const Vector<size, T>& other)

Multiply vector component-wise and assign.

The computation is done in-place.

\[ \boldsymbol a_i = \boldsymbol a_i \boldsymbol b_i \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::operator*(const Vector<size, T>& other) const

Multiply vector component-wise.

template<std::size_t size, class T>
Vector<size, T>& Magnum::Math::Vector<size, T>::operator/=(const Vector<size, T>& other)

Divide vector component-wise and assign.

The computation is done in-place.

\[ \boldsymbol a_i = \frac{\boldsymbol a_i}{\boldsymbol b_i} \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::operator/(const Vector<size, T>& other) const

Divide vector component-wise.

template<std::size_t size, class T>
T Magnum::Math::Vector<size, T>::dot() const

Dot product of the vector.

Should be used instead of length() for comparing vector length with other values, because it doesn't compute the square root.

\[ \boldsymbol a \cdot \boldsymbol a = \sum_{i=0}^{n-1} \boldsymbol a_i^2 \]

template<std::size_t size, class T>
T Magnum::Math::Vector<size, T>::length() const

Vector length.

See also dot() const which is faster for comparing length with other values.

\[ |\boldsymbol a| = \sqrt{\boldsymbol a \cdot \boldsymbol a} \]

template<std::size_t size, class T>
T Magnum::Math::Vector<size, T>::lengthInverted() const

Inverse vector length.

\[ \frac{1}{|\boldsymbol a|} = \frac{1}{\sqrt{\boldsymbol a \cdot \boldsymbol a}} \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::normalized() const

Normalized vector (of unit length)

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::resized(T length) const

Resized vector.

Convenience equivalent to the following code. Due to operation order this function is faster than the obvious way of sizing normalized() vector.

vec*(vec.lengthInverted()*length) // the parentheses are important

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::projected(const Vector<size, T>& line) const

Vector projected onto line.

Returns vector projected onto line.

\[ \boldsymbol a_1 = \frac{\boldsymbol a \cdot \boldsymbol b}{\boldsymbol b \cdot \boldsymbol b} \boldsymbol b \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::projectedOntoNormalized(const Vector<size, T>& line) const

Vector projected onto normalized line.

Slightly faster alternative to projected(), expects line to be normalized.

\[ \boldsymbol a_1 = \frac{\boldsymbol a \cdot \boldsymbol b}{\boldsymbol b \cdot \boldsymbol b} \boldsymbol b = (\boldsymbol a \cdot \boldsymbol b) \boldsymbol b \]

template<std::size_t size, class T>
Vector<size, T> Magnum::Math::Vector<size, T>::flipped() const constexpr

Flipped vector.

Returns the vector with components in reverse order.

template<std::size_t size, class T>
T Magnum::Math::Vector<size, T>::sum() const

Sum of values in the vector.

template<std::size_t size, class T>
T Magnum::Math::Vector<size, T>::product() const

Product of values in the vector.

template<std::size_t size, class T>
T Magnum::Math::Vector<size, T>::min() const

Minimal value in the vector.

template<std::size_t size, class T>
T Magnum::Math::Vector<size, T>::max() const

Maximal value in the vector.

template<std::size_t size, class T>
std::pair<T, T> Magnum::Math::Vector<size, T>::minmax() const

Minimal and maximal value in the vector.

template<std::size_t size, class T> template<std::size_t size, class T>
T dot(const Vector<size, T>& a, const Vector<size, T>& b)

Dot product of two vectors.

Returns 0 when two vectors are perpendicular, > 0 when two vectors are in the same general direction, 1 when two normalized vectors are parallel, < 0 when two vectors are in opposite general direction and -1 when two normalized* vectors are antiparallel.

\[ \boldsymbol a \cdot \boldsymbol b = \sum_{i=0}^{n-1} \boldsymbol a_i \boldsymbol b_i \]

template<std::size_t size, class T> template<std::size_t size, class T>
Rad<T> angle(const Vector<size, T>& normalizedA, const Vector<size, T>& normalizedB)

Angle between normalized vectors.

Expects that both vectors are normalized.

\[ \theta = acos \left( \frac{\boldsymbol a \cdot \boldsymbol b}{|\boldsymbol a| |\boldsymbol b|} \right) = acos (\boldsymbol a \cdot \boldsymbol b) \]

template<std::size_t size, class T> template<std::size_t size, class T>
Vector<size, T> operator*(T number, const Vector<size, T>& vector)

Multiply number with vector.

Same as Vector::operator*(T) const.

template<std::size_t size, class T> template<std::size_t size, class T>
Vector<size, T> operator/(T number, const Vector<size, T>& vector)

Divide vector with number and invert.

\[ \boldsymbol c_i = \frac b {\boldsymbol a_i} \]

template<std::size_t size, class T> template<std::size_t size, class Integral>
Vector<size, Integral>& operator%=(Vector<size, Integral>& a, Integral b)

Do modulo of integral vector and assign.

The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral>
Vector<size, Integral>& operator%=(Vector<size, Integral>& a, const Vector<size, Integral>& b)

Do modulo of two integral vectors and assign.

The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral>
Vector<size, Integral>& operator&=(Vector<size, Integral>& a, const Vector<size, Integral>& b)

Do bitwise AND of two integral vectors and assign.

The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral>
Vector<size, Integral>& operator|=(Vector<size, Integral>& a, const Vector<size, Integral>& b)

Do bitwise OR of two integral vectors and assign.

The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral>
Vector<size, Integral>& operator^=(Vector<size, Integral>& a, const Vector<size, Integral>& b)

Do bitwise XOR of two integral vectors and assign.

The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral>
Vector<size, Integral>& operator<<=(Vector<size, Integral>& vector, Integral shift)

Do bitwise left shift of integral vector and assign.

The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral>
Vector<size, Integral>& operator>>=(Vector<size, Integral>& vector, Integral shift)

Do bitwise right shift of integral vector and assign.

The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral, class FloatingPoint>
Vector<size, Integral>& operator*=(Vector<size, Integral>& vector, FloatingPoint number)

Multiply integral vector with floating-point number and assign.

Similar to Vector::operator*=(T), except that the multiplication is done in floating-point. The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral, class FloatingPoint>
Vector<size, Integral> operator*(const Vector<size, Integral>& vector, FloatingPoint number)

Multiply integral vector with floating-point number.

Similar to Vector::operator*(T) const, except that the multiplication is done in floating-point.

template<std::size_t size, class T> template<std::size_t size, class FloatingPoint, class Integral>
Vector<size, Integral> operator*(FloatingPoint number, const Vector<size, Integral>& vector)

Multiply floating-point number with integral vector.

Same as operator*(const Vector<size, Integral>&, FloatingPoint).

template<std::size_t size, class T> template<std::size_t size, class Integral, class FloatingPoint>
Vector<size, Integral>& operator/=(Vector<size, Integral>& vector, FloatingPoint number)

Divide integral vector with floating-point number and assign.

Similar to Vector::operator/=(T), except that the division is done in floating-point. The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral, class FloatingPoint>
Vector<size, Integral> operator/(const Vector<size, Integral>& vector, FloatingPoint number)

Divide integral vector with floating-point number.

Similar to Vector::operator/(T) const, except that the division is done in floating-point.

template<std::size_t size, class T> template<std::size_t size, class Integral, class FloatingPoint>
Vector<size, Integral>& operator*=(Vector<size, Integral>& a, const Vector<size, FloatingPoint>& b)

Multiply integral vector with floating-point vector component-wise and assign.

Similar to Vector::operator*=(const Vector<size, T>&), except that the multiplication is done in floating-point. The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral, class FloatingPoint>
Vector<size, Integral> operator*(const Vector<size, Integral>& a, const Vector<size, FloatingPoint>& b)

Multiply integral vector with floating-point vector component-wise.

Similar to Vector::operator*(const Vector<size, T>&) const, except that the multiplication is done in floating-point. The result is always integral vector, convert both arguments to the same floating-point type to have floating-point result.

template<std::size_t size, class T> template<std::size_t size, class FloatingPoint, class Integral>
Vector<size, Integral> operator*(const Vector<size, FloatingPoint>& a, const Vector<size, Integral>& b)

Multiply floating-point vector with integral vector component-wise.

Same as operator*(const Vector<size, Integral>&, const Vector<size, FloatingPoint>&).

template<std::size_t size, class T> template<std::size_t size, class Integral, class FloatingPoint>
Vector<size, Integral>& operator/=(Vector<size, Integral>& a, const Vector<size, FloatingPoint>& b)

Divide integral vector with floating-point vector component-wise and assign.

Similar to Vector::operator/=(const Vector<size, T>&), except that the division is done in floating-point. The computation is done in-place.

template<std::size_t size, class T> template<std::size_t size, class Integral, class FloatingPoint>
Vector<size, Integral> operator/(const Vector<size, Integral>& a, const Vector<size, FloatingPoint>& b)

Divide integral vector with floating-point vector component-wise.

Similar to Vector::operator/(const Vector<size, T>&) const, except that the division is done in floating-point. The result is always integral vector, convert both arguments to the same floating-point type to have floating-point result.

template<std::size_t size, class T> template<std::size_t size, class T>
Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Vector<size, T>& value)

Debug output operator.