#include <Magnum/Math/Vector.h>
template<std:: size_t size, class T>
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
Constructors, destructors, conversion operators
- Vector() constexpr noexcept
- Default constructor.
- Vector(ZeroInitT) explicit constexpr noexcept
- Construct a zero vector.
-
Vector(Magnum::
NoInitT) explicit noexcept - Construct a vector without initializing the contents.
-
template<class ... U>Vector(T first, U... next) constexpr noexcept
- Construct a vector from components.
-
template<std::Vector(const T(&data)[size_]) explicit constexpr noexcept new in Git master
size_t size_> - Construct a vector from a fixed-size array.
- Vector(T value) explicit constexpr noexcept
- Construct a vector with one value for all components.
-
template<class U>Vector(const Vector<size, U>& other) explicit constexpr noexcept
- Construct a vector from another of different type.
- Vector(const BitVector<size>& other) explicit constexpr noexcept new in Git master
- Construct a vector from a BitVector.
-
template<class U, class = decltype(Implementation::VectorConverter<size, T, U>::from(std::Vector(const U& other) explicit constexpr noexcept
declval<U>()))> - Construct a vector from external representation.
-
template<class U, class = decltype(Implementation::VectorConverter<size, T, U>::to(std::operator U() const explicit constexpr
declval<Vector<size, T>>()))> - Convert the 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 -> BitVector<size>
- Component-wise less than comparison.
- auto operator<=(const Vector<size, T>& other) const -> BitVector<size>
- Component-wise less than or equal comparison.
- auto operator>=(const Vector<size, T>& other) const -> BitVector<size>
- Component-wise greater than or equal comparison.
- auto operator>(const Vector<size, T>& other) const -> BitVector<size>
- Component-wise greater than comparison.
- auto isZero() const -> bool
- Whether the vector is zero.
- auto isNormalized() const -> bool
- Whether the vector is normalized.
- auto operator+() const -> Vector<size, T> constexpr new in Git master
- Promotion.
- auto operator-() const -> Vector<size, T> constexpr
- Negated vector.
- auto operator+=(const Vector<size, T>& other) -> Vector<size, T>&
- Add and assign a vector.
- auto operator+(const Vector<size, T>& other) const -> Vector<size, T> constexpr
- Add a vector.
- auto operator-=(const Vector<size, T>& other) -> Vector<size, T>&
- Subtract and assign a vector.
- auto operator-(const Vector<size, T>& other) const -> Vector<size, T> constexpr
- Subtract a vector.
- auto operator*=(T scalar) -> Vector<size, T>&
- Multiply with a scalar and assign.
- auto operator*(T scalar) const -> Vector<size, T> constexpr
- Multiply with a scalar.
-
template<class FloatingPoint>auto operator*=(FloatingPoint scalar) -> Vector<size, T>&
- Multiply an integral vector with a floating-point scalar and assign.
-
template<class FloatingPoint>auto operator*(FloatingPoint scalar) const -> Vector<size, T> constexpr
- Multiply an integral vector with a floating-point scalar.
- auto operator/=(T scalar) -> Vector<size, T>&
- Divide with a scalar and assign.
- auto operator/(T scalar) const -> Vector<size, T> constexpr
- Divide with a scalar.
-
template<class FloatingPoint>auto operator/=(FloatingPoint scalar) -> Vector<size, Integral>&
- Divide an integral vector with a floating-point scalar and assign.
-
template<class FloatingPoint>auto operator/(FloatingPoint scalar) const -> Vector<size, T> constexpr
- Divide an integral vector with a floating-point scalar.
- auto operator*=(const Vector<size, T>& other) -> Vector<size, T>&
- Multiply a vector component-wise and assign.
- auto operator*(const Vector<size, T>& other) const -> Vector<size, T> constexpr
- Multiply a vector component-wise.
-
template<class FloatingPoint>auto operator*=(const Vector<size, FloatingPoint>& other) -> Vector<size, T>&
- Multiply an integral vector with a floating-point vector component-wise and assign.
-
template<class FloatingPoint>auto operator*(const Vector<size, FloatingPoint>& other) const -> Vector<size, T> constexpr
- Multiply an integral vector with a floating-point vector component-wise.
-
template<class Integral>auto operator*(const Vector<size, Integral>& other) const -> Vector<size, Integral> constexpr
- Multiply a floating-point vector with an integral vector component-wise.
- auto operator/=(const Vector<size, T>& other) -> Vector<size, T>&
- Divide a vector component-wise and assign.
- auto operator/(const Vector<size, T>& other) const -> Vector<size, T> constexpr
- Divide a vector component-wise.
-
template<class FloatingPoint>auto operator/=(const Vector<size, FloatingPoint>& other) -> Vector<size, T>&
- Divide an integral vector with a floating-point vector component-wise and assign.
-
template<class FloatingPoint>auto operator/(const Vector<size, FloatingPoint>& other) const -> Vector<size, T> constexpr
- Divide an integral vector with a floating-point vector component-wise.
- auto operator%=(T scalar) -> Vector<size, T>&
- Do modulo of a vector and assign.
- auto operator%(T scalar) const -> Vector<size, T> constexpr
- Modulo of a vector.
- auto operator%=(const Vector<size, T>& other) -> Vector<size, T>&
- Do modulo of two vectors and assign.
- auto operator%(const Vector<size, T>& other) const -> Vector<size, T> constexpr
- Modulo of two vectors.
- auto operator~() const -> Vector<size, T> constexpr
- Bitwise NOT of a vector.
- auto operator&=(const Vector<size, T>& other) -> Vector<size, T>&
- Do bitwise AND of two vectors and assign.
- auto operator&(const Vector<size, T>& other) const -> Vector<size, T> constexpr
- Bitwise AND of two vectors.
- auto operator|=(const Vector<size, T>& other) -> Vector<size, T>&
- Do bitwise OR of two vectors and assign.
- auto operator|(const Vector<size, T>& other) const -> Vector<size, T> constexpr
- Bitwise OR of two vectors.
- auto operator^=(const Vector<size, T>& other) -> Vector<size, T>&
- Do bitwise XOR of two vectors and assign.
- auto operator^(const Vector<size, T>& other) const -> Vector<size, T> constexpr
- Bitwise XOR of two vectors.
- auto operator<<=(T shift) -> Vector<size, T>&
- Do bitwise left shift of a vector and assign.
- auto operator<<(T shift) const -> Vector<size, T> constexpr
- Bitwise left shift of a vector.
- auto operator>>=(T shift) -> Vector<size, T>&
- Do bitwise right shift of a vector and assign.
- auto operator>>(T shift) const -> Vector<size, T> constexpr
- Bitwise left shift of a vector.
- 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 a line.
- auto projectedOntoNormalized(const Vector<size, T>& line) const -> Vector<size, T>
- Vector projected onto a 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 -> Containers::
Pair<T, T> - Minimal and maximal value in the vector.
Friends
- auto operator*(T scalar, const Vector<size, T>& vector) -> Vector<size, T> constexpr
- Multiply a scalar with a vector.
-
template<class FloatingPoint>auto operator*(FloatingPoint scalar, const Vector<size, T>& vector) -> Vector<size, T> constexpr
- Multiply a floating-point scalar with an integral vector.
- auto operator/(T scalar, const Vector<size, T>& vector) -> Vector<size, T> constexpr
- Divide a vector with a scalar and invert.
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 an array.
Returns | Reference to the data as if it was Vector, thus doesn't perform any copying. |
---|
Use with caution, the function doesn't check whether the array is long enough. If possible, prefer to use the Vector(const T(&)[size_
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()) constexpr
Pad a 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() constexpr noexcept
Default constructor.
Equivalent to Vector(ZeroInitT).
template<std:: size_t size, class T>
Magnum:: Math:: Vector<size, T>:: Vector(ZeroInitT) explicit constexpr noexcept
Construct a zero vector.
template<std:: size_t size, class T>
template<std:: size_t size_>
Magnum:: Math:: Vector<size, T>:: Vector(const T(&data)[size_]) explicit constexpr noexcept new in Git master
Construct a vector from a fixed-size array.
Use from(const T*) to reinterpret an arbitrary pointer to a vector.
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 a vector from another of different type.
Performs only default casting on the values, no rounding or anything else. Example usage:
Vector4 floatingPoint{1.3f, 2.7f, -15.0f, 7.0f}; Vector4i integral{floatingPoint}; // {1, 2, -15, 7}
template<std:: size_t size, class T>
Magnum:: Math:: Vector<size, T>:: Vector(const BitVector<size>& other) explicit constexpr noexcept new in Git master
Construct a vector from a BitVector.
Bits that are unset are converted to 0
, set bits to 1
. If you need a different behavior, for example converting a bit mask to 0
or 255
for a color representation, use lerp(const Vector<size, T>&, const Vector<size, T>&, const BitVector<size>&) instead, for example:
BitVector3 mask = …; Vector3ub a = Math::lerp(Vector3ub{0}, Vector3ub{255}, mask);
template<std:: size_t size, class T>
T* Magnum:: Math:: Vector<size, T>:: data()
Raw data.
Contrary to what Doxygen shows, returns reference to an one-dimensional fixed-size array of size
elements, i.e. T(&)[size]
.
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>:: operator==(const Vector<size, T>& other) const
Equality comparison.
template<std:: size_t size, class T>
bool Magnum:: Math:: Vector<size, T>:: operator!=(const Vector<size, T>& other) const
Non-equality comparison.
template<std:: size_t size, class T>
bool Magnum:: Math:: Vector<size, T>:: isZero() const
Whether the vector is zero.
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:
template<std:: size_t size, class T>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator+() const constexpr new in Git master
Promotion.
Returns the value as-is.
template<std:: size_t size, class T>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator-() const constexpr
Negated vector.
Enabled only for signed types.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator+=(const Vector<size, T>& other)
Add and assign a vector.
The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator-=(const Vector<size, T>& other)
Subtract and assign a vector.
The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator*=(T scalar)
Multiply with a scalar and assign.
The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator*(T scalar) const constexpr
Multiply with a scalar.
template<std:: size_t size, class T>
template<class FloatingPoint>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator*=(FloatingPoint scalar)
Multiply an integral vector with a floating-point scalar and assign.
Similar to 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<class FloatingPoint>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator*(FloatingPoint scalar) const constexpr
Multiply an integral vector with a floating-point scalar.
Similar to operator*(T) const, except that the multiplication is done in floating-point.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator/=(T scalar)
Divide with a scalar and assign.
The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator/(T scalar) const constexpr
Divide with a scalar.
template<std:: size_t size, class T>
template<class FloatingPoint>
Vector<size, Integral>& Magnum:: Math:: Vector<size, T>:: operator/=(FloatingPoint scalar)
Divide an integral vector with a floating-point scalar and assign.
Similar to operator/
template<std:: size_t size, class T>
template<class FloatingPoint>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator/(FloatingPoint scalar) const constexpr
Divide an integral vector with a floating-point scalar.
Similar to operator/(T) const, except that the division is done in floating-point.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator*=(const Vector<size, T>& other)
Multiply a vector component-wise and assign.
The computation is done in-place.
template<std:: size_t size, class T>
template<class FloatingPoint>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator*=(const Vector<size, FloatingPoint>& other)
Multiply an integral vector with a floating-point vector component-wise and assign.
Similar to 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<class FloatingPoint>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator*(const Vector<size, FloatingPoint>& other) const constexpr
Multiply an integral vector with a floating-point vector component-wise.
Similar to operator*(const Vector<size, T>&) const, except that the multiplication is done in floating-point. The result is always an integral vector, convert both arguments to the same floating-point type to have a floating-point result.
template<std:: size_t size, class T>
template<class Integral>
Vector<size, Integral> Magnum:: Math:: Vector<size, T>:: operator*(const Vector<size, Integral>& other) const constexpr
Multiply a floating-point vector with an integral vector component-wise.
Same as operator*(const Vector<size, FloatingPoint>&) const.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator/=(const Vector<size, T>& other)
Divide a vector component-wise and assign.
The computation is done in-place.
template<std:: size_t size, class T>
template<class FloatingPoint>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator/=(const Vector<size, FloatingPoint>& other)
Divide an integral vector with a floating-point vector component-wise and assign.
Similar to 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<class FloatingPoint>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator/(const Vector<size, FloatingPoint>& other) const constexpr
Divide an integral vector with a floating-point vector component-wise.
Similar to Vector::
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator%=(T scalar)
Do modulo of a vector and assign.
Enabled only for integral types. The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator%(T scalar) const constexpr
Modulo of a vector.
Enabled only for integral types.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator%=(const Vector<size, T>& other)
Do modulo of two vectors and assign.
Enabled only for integral types. The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator~() const constexpr
Bitwise NOT of a vector.
Enabled only for integral types.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator&=(const Vector<size, T>& other)
Do bitwise AND of two vectors and assign.
Enabled only for integral types. The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator|=(const Vector<size, T>& other)
Do bitwise OR of two vectors and assign.
Enabled only for integral types. The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator^=(const Vector<size, T>& other)
Do bitwise XOR of two vectors and assign.
Enabled only for integral types. The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator<<=(T shift)
Do bitwise left shift of a vector and assign.
Enabled only for integral types. The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator<<(T shift) const constexpr
Bitwise left shift of a vector.
Enabled only for integral types.
template<std:: size_t size, class T>
Vector<size, T>& Magnum:: Math:: Vector<size, T>:: operator>>=(T shift)
Do bitwise right shift of a vector and assign.
Enabled only for integral types. The computation is done in-place.
template<std:: size_t size, class T>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: operator>>(T shift) const constexpr
Bitwise left shift of a vector.
Enabled only for integral types.
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.
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.
For integral types the result may be imprecise, to get a floating-point value of desired precision, cast to a floating-point vector first:
Vector2i a{25, -1}; Float length = Vector2{a}.length(); // ~25.099
A Manhattan length might be more suitable than length() in certain cases where the square root is undesirable — it's a sum of absolute values:
Int manhattanLength = Math::abs(a).sum(); // 26
template<std:: size_t size, class T>
T Magnum:: Math:: Vector<size, T>:: lengthInverted() const
Inverse vector length.
Enabled only for floating-point types.
template<std:: size_t size, class T>
Vector<size, T> Magnum:: Math:: Vector<size, T>:: normalized() const
Normalized vector (of unit length)
Enabled only for floating-point types.
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 a normalized() vector. Enabled only for floating-point types.
vec*(vec.lengthInverted()*length) // the parentheses are important
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 a normalized line.
Slightly faster alternative to projected(), expects line
to be normalized. Enabled only for floating-point types.
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. If you want to flip the vector direction instead, negate it.
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.
NaNs are ignored, unless the vector is all NaNs.
template<std:: size_t size, class T>
T Magnum:: Math:: Vector<size, T>:: max() const
Maximal value in the vector.
NaNs are ignored, unless the vector is all NaNs.
template<std:: size_t size, class T>
Containers:: Pair<T, T> Magnum:: Math:: Vector<size, T>:: minmax() const
Minimal and maximal value in the vector.
NaNs are ignored, unless the vector is all NaNs.
template<std:: size_t size, class T>
Vector<size, T> operator*(T scalar,
const Vector<size, T>& vector) constexpr
Multiply a scalar with a vector.
Same as operator*(T) const.
template<std:: size_t size, class T>
template<class FloatingPoint>
Vector<size, T> operator*(FloatingPoint scalar,
const Vector<size, T>& vector) constexpr
Multiply a floating-point scalar with an integral vector.
Same as operator*(FloatingPoint) const.
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.
template<std:: size_t size, class T>
template<std:: size_t size, class FloatingPoint>
Rad<FloatingPoint> angle(const Vector<size, FloatingPoint>& normalizedA,
const Vector<size, FloatingPoint>& normalizedB)
Angle between normalized vectors.
Expects that both vectors are normalized. Enabled only for floating-point types.
To avoid numerical issues when two vectors are very close to each other, the dot product is clamped to the range before being passed to .
template<std:: size_t size, class T>
template<std:: size_t size, class T>
BitVector<size> equal(const Vector<size, T>& a,
const Vector<size, T>& b) new in 2019.10
Component-wise equality comparison.
Unlike Vector::
template<std:: size_t size, class T>
template<std:: size_t size, class T>
BitVector<size> notEqual(const Vector<size, T>& a,
const Vector<size, T>& b) new in 2019.10
Component-wise non-equality comparison.
Unlike Vector::
template<std:: size_t size, class T>
template<std:: size_t size, class T>
Debug& operator<<(Debug& debug,
const Vector<size, T>& value)
Debug output operator.