#include <muu/vector.h>
template <typename Scalar, size_t Dimensions>
vector struct
An N-dimensional vector.
Template parameters | |
---|---|
Scalar | The vector's scalar component type. |
Dimensions | The number of dimensions. |
Public types
-
using const_iterator = const scalar_
type* - A const LegacyRandomAccessIterator for the scalar components in the vector.
-
using constants = muu::
constants<vector> - Compile-time constants for this vector type.
-
using delta_scalar_type = std::
conditional_t<is_ integral<scalar_ type>, double, scalar_ type> - The scalar type used for length, distance, blend factors, etc. Always floating-point.
-
using delta_type = vector<delta_
scalar_ type, dimensions> - The vector type with scalar_
type == delta_ scalar_ type and the same number of dimensions as this one. -
using iterator = scalar_
type* - A LegacyRandomAccessIterator for the scalar components in the vector.
-
using product_scalar_type = std::
conditional_t<is_ integral<scalar_ type>, impl::highest_ranked<make_ signed<scalar_ type>, int>, scalar_ type> - The scalar type used for products (dot, cross, etc.). Always signed.
-
using product_type = vector<product_
scalar_ type, dimensions> - The vector type with scalar_
type == product_ scalar_ type and the same number of dimensions as this one. - using scalar_type = Scalar
- The type of each scalar component stored in this vector.
Public static variables
- static size_t dimensions constexpr
- The number of scalar components stored in this vector.
Public static functions
- static auto abs(const vector& v) -> vector constexpr noexcept
- Returns a vector with all scalar components set to their absolute values.
-
static auto angle(const vector& v1,
const vector& v2) -> delta_
scalar_ type constexpr noexcept - Calculates the angle between two vectors.
- static auto ceil(const vector& v) -> vector constexpr noexcept
- Returns a vector with all scalar components set to the lowest integer not less than their original values.
- static auto floor(const vector& v) -> vector constexpr noexcept
- Returns a vector with all scalar components set to the highest integer not greater than their original values.
-
static auto lerp(const vector& start,
const vector& finish,
delta_
scalar_ type alpha) -> vector constexpr noexcept - Performs a linear interpolation between two vectors.
Constructors, destructors, conversion operators
- vector() defaulted noexcept
- Default constructor. Scalar components are not initialized.
- vector(const vector&) defaulted constexpr noexcept
- Copy constructor.
-
vector(scalar_
type x, scalar_ type y) constexpr noexcept - Constructs a vector from two scalar values.
-
vector(scalar_
type x, scalar_ type y, scalar_ type z = scalar_ type{}) constexpr noexcept - Constructs a vector from three scalar values.
-
vector(scalar_
type x, scalar_ type y, scalar_ type z = scalar_ type{}, scalar_ type w = scalar_ type{}) constexpr noexcept - Constructs a vector from four scalar values.
-
vector(scalar_
type x, scalar_ type y, scalar_ type z, scalar_ type w, const T&... vals) constexpr noexcept - Constructs a vector from five or more scalar values.
-
vector(scalar_
type broadcast) explicit constexpr noexcept - Constructs a vector by broadcasting a scalar value to all components.
-
template <size_t N>vector(const scalar_
type(&arr)[N]) explicit constexpr noexcept - Constructs a vector from a raw array of scalars.
-
template <typename T, size_t N>vector(const T(&arr)[N]) explicit constexpr noexcept
- Constructs a vector from a raw array of scalars.
-
template <size_t N>vector(const std::
array<scalar_ type, N>& arr) explicit constexpr noexcept - Constructs a vector from a std::
array of scalars. -
template <typename T, size_t N>vector(const std::
array<T, N>& arr) explicit constexpr noexcept - Constructs a vector from a std::
array of scalars. -
template <typename T>vector(const T& tuple_or_bitcastable) explicit constexpr noexcept
- Constructs a vector from any tuple-like or implicitly bit-castable type.
-
template <typename S, size_t D>vector(const vector<S, D>& vec) explicit constexpr noexcept
- Enlarging/truncating/converting constructor.
- vector(const vector<S1, D1>& vec1, const vector<S2, D2>& vec2) constexpr noexcept
- Concatenating constructor.
-
template <typename S, size_t D, typename... T>vector(const vector<S, D>& vec, const T&... vals) constexpr noexcept
- Appending constructor.
-
template <typename T>vector(const T* vals, size_t num) noexcept
- Constructs a vector from a pointer to scalars.
-
template <typename T>vector(const T* vals) explicit constexpr noexcept
- Constructs a vector from a pointer to scalars.
-
template <typename T, size_t N>vector(const muu::
span<T, N>& vals) explicit constexpr noexcept - Constructs a vector from a statically-sized muu::
span. -
template <typename T>vector(const muu::
span<T>& vals) explicit constexpr noexcept - Constructs a vector from a dynamically-sized muu::
span.
Public functions
- auto abs() const -> vector constexpr noexcept
- Returns a copy of this vector with all scalar components set to their absolute values.
-
auto angle(const vector& v) const -> delta_
scalar_ type constexpr noexcept - Calculates the angle between this vector and another.
- auto ceil() const -> vector constexpr noexcept
- Returns a copy of the vector with all scalar components set to the lowest integer not less than their original values.
- auto floor() const -> vector constexpr noexcept
- Returns a copy of the vector with all scalar components set to the highest integer not greater than their original values.
-
auto lerp(const vector& target,
delta_
scalar_ type alpha) -> vector& constexpr noexcept - Linearly interpolates this vector towards another (in-place).
- auto operator=(const vector&) -> vector& defaulted constexpr noexcept
- Copy-assigment operator.
Public variables
-
scalar_
type x - The vector's 0th scalar component (when dimensions <= 4).
-
scalar_
type y - The vector's 1st scalar component (when dimensions == 2, 3 or 4).
-
scalar_
type z - The vector's 2nd scalar component (when dimensions == 3 or 4).
-
scalar_
type w - The vector's 3rd scalar component (when dimensions == 4).
-
scalar_
type values - The vector's scalar component array (when dimensions >= 5).
Addition
-
static auto sum(const vector& v) -> scalar_
type constexpr noexcept - Returns the sum of all the scalar components in a vector.
- auto operator+() const -> vector constexpr noexcept
- Returns a componentwise copy of a vector.
- auto operator+=(const vector& rhs) -> vector& constexpr noexcept
- Componentwise adds another vector to this one.
-
auto sum() const -> scalar_
type constexpr noexcept - Returns the sum of all the scalar components in the vector.
- auto operator+(const vector& lhs, const vector& rhs) -> vector constexpr noexcept
- Returns the componentwise addition of two vectors.
Bitwise shifts
-
auto operator<<=(product_
scalar_ type rhs) -> vector& constexpr noexcept - Componentwise left-shifts each scalar component in the vector by the given number of bits.
-
auto operator>>=(product_
scalar_ type rhs) -> vector& constexpr noexcept - Componentwise right-shifts each scalar component in the vector by the given number of bits.
-
auto operator<<(const vector& lhs,
product_
scalar_ type rhs) -> vector constexpr noexcept - Returns a vector with each scalar component left-shifted the given number of bits.
-
auto operator>>(const vector& lhs,
product_
scalar_ type rhs) -> vector constexpr noexcept - Returns a vector with each scalar component right-shifted the given number of bits.
Cross product
-
static auto cross(const vector& v1,
const vector& v2) -> vector<product_
scalar_ type, 3> constexpr noexcept - Returns the cross product of two vectors.
-
template <size_t Dimension>static auto cross(const vector& v, index_
tag<Dimension>) -> vector<product_ scalar_ type, 3> constexpr noexcept - Returns the cross product of a vector and a principal axis.
-
template <size_t Dimension>static auto cross(index_
tag<Dimension>, const vector& v) -> vector<product_ scalar_ type, 3> constexpr noexcept - Returns the cross product of a principal axis and a vector.
-
static auto orthogonal(const vector& v) -> vector<product_
scalar_ type, 3> constexpr noexcept - Returns a vector orthogonal to another.
-
auto cross(const vector& v) const -> vector<product_
scalar_ type, 3> constexpr noexcept - Returns the cross product of this vector and another.
-
template <size_t Dimension>auto cross(index_
tag<Dimension>) const -> vector<product_ scalar_ type, 3> constexpr noexcept - Returns the cross product of this vector and a principal axis.
-
auto orthogonal() const -> vector<product_
scalar_ type, 3> constexpr noexcept - Returns a vector orthogonal to this one.
Direction
-
static auto direction(const vector& from,
const vector& to,
delta_
scalar_ type& distance_out) -> delta_ type constexpr noexcept - Returns the normalized direction vector from one position to another.
-
static auto direction(const vector& from,
const vector& to) -> delta_
type constexpr noexcept - Returns the normalized direction vector from one position to another.
-
auto direction(const vector& to,
delta_
scalar_ type& distance_out) const -> delta_ type constexpr noexcept - Returns the normalized direction vector from this position to another.
-
auto direction(const vector& to) const -> delta_
type constexpr noexcept - Returns the normalized direction vector from this position to another.
Division
- auto operator/=(const vector& rhs) -> vector& constexpr noexcept
- Componentwise divides this vector by another.
-
auto operator/=(scalar_
type rhs) -> vector& constexpr noexcept - Componentwise divides this vector by a scalar.
- auto operator/(const vector& lhs, const vector& rhs) -> vector constexpr noexcept
- Returns the componentwise division of one vector by another.
-
auto operator/(const vector& lhs,
scalar_
type rhs) -> vector constexpr noexcept - Returns the componentwise division of a vector and a scalar.
Dot product
-
static auto dot(const vector& v1,
const vector& v2) -> product_
scalar_ type constexpr noexcept - Returns the dot product of two vectors.
-
template <size_t Dimension>static auto dot(const vector& v, index_
tag<Dimension>) -> product_ scalar_ type constexpr noexcept - Returns the dot product of a vector and a principal axis.
-
template <size_t Dimension>static auto dot(index_
tag<Dimension>, const vector& v) -> product_ scalar_ type constexpr noexcept - Returns the dot product of a vector and a principal axis.
-
auto dot(const vector& v) const -> product_
scalar_ type constexpr noexcept - Returns the dot product of this and another vector.
-
template <size_t Dimension>auto dot(index_
tag<Dimension>) const -> product_ scalar_ type constexpr noexcept - Returns the dot product of the vector and a principal axis.
Equality (approximate)
-
template <typename T>static auto approx_equal(const vector& v1, const vector<T, dimensions>& v2, epsilon_
type<scalar_ type, T> epsilon = default_ epsilon<scalar_ type, T>) -> bool constexpr noexcept - Returns true if two vectors are approximately equal.
-
static auto approx_zero(const vector& v,
scalar_
type epsilon = default_ epsilon<scalar_ type>) -> bool constexpr noexcept - Returns true if all the scalar components in a vector are approximately equal to zero.
-
template <typename T>auto approx_equal(const vector<T, dimensions>& v, epsilon_
type<scalar_ type, T> epsilon = default_ epsilon<scalar_ type, T>) const -> bool constexpr noexcept - Returns true if the vector is approximately equal to another.
-
auto approx_zero(scalar_
type epsilon = default_ epsilon<scalar_ type>) const -> bool constexpr noexcept - Returns true if all the scalar components in the vector are approximately equal to zero.
Equality (exact)
- static auto infinity_or_nan(const vector& v) -> bool constexpr noexcept
- Returns true if any of the scalar components of a vector are infinity or NaN.
- static auto zero(const vector& v) -> bool constexpr noexcept
- Returns true if all the scalar components of a vector are exactly zero.
- auto infinity_or_nan() const -> bool constexpr noexcept
- Returns true if any of the scalar components of the vector are infinity or NaN.
- auto zero() const -> bool constexpr noexcept
- Returns true if all the scalar components of the vector are exactly zero.
-
template <typename T>auto operator!=(const vector& lhs, const vector<T, dimensions>& rhs) -> bool constexpr noexcept
- Returns true if two vectors are not exactly equal.
-
template <typename T>auto operator==(const vector& lhs, const vector<T, dimensions>& rhs) -> bool constexpr noexcept
- Returns true if two vectors are exactly equal.
Iterators
- auto begin() -> iterator constexpr noexcept
- Returns an iterator to the first scalar component in the vector.
-
auto begin() const -> const_
iterator constexpr noexcept - Returns a const iterator to the first scalar component in the vector.
-
auto cbegin() const -> const_
iterator constexpr noexcept - Returns a const iterator to the first scalar component in the vector.
-
auto cend() const -> const_
iterator constexpr noexcept - Returns a const iterator to the one-past-the-last scalar component in the vector.
- auto end() -> iterator constexpr noexcept
- Returns an iterator to the one-past-the-last scalar component in the vector.
-
auto end() const -> const_
iterator constexpr noexcept - Returns a const iterator to the one-past-the-last scalar component in the vector.
Iterators (ADL)
- auto begin(vector& v) -> iterator constexpr noexcept
- Returns an iterator to the first scalar component in a vector.
-
auto begin(const vector& v) -> const_
iterator constexpr noexcept - Returns a const iterator to the first scalar component in a vector.
-
auto cbegin(const vector& v) -> const_
iterator constexpr noexcept - Returns a const iterator to the first scalar component in a vector.
-
auto cend(const vector& v) -> const_
iterator constexpr noexcept - Returns a const iterator to the one-past-the-last scalar component in a vector.
- auto end(vector& v) -> iterator constexpr noexcept
- Returns an iterator to the one-past-the-last scalar component in a vector.
-
auto end(const vector& v) -> const_
iterator constexpr noexcept - Returns a const iterator to the one-past-the-last scalar component in a vector.
Length and Distance
-
static auto consteval_distance(const vector& p1,
const vector& p2) -> delta_
scalar_ type consteval noexcept - Returns the distance between two points.
-
static auto consteval_length(const vector& v) -> delta_
scalar_ type consteval noexcept - Returns the length (magnitude) of a vector.
-
static auto distance(const vector& p1,
const vector& p2) -> delta_
scalar_ type constexpr noexcept - Returns the distance between two points.
-
static auto distance_squared(const vector& p1,
const vector& p2) -> delta_
scalar_ type constexpr noexcept - Returns the squared distance between two points.
-
static auto length(const vector& v) -> delta_
scalar_ type constexpr noexcept - Returns the length (magnitude) of a vector.
-
static auto length_squared(const vector& v) -> delta_
scalar_ type constexpr noexcept - Returns the squared length (magnitude) of a vector.
-
auto distance(const vector& p) const -> delta_
scalar_ type constexpr noexcept - Returns the distance between this and another point vector.
-
auto distance_squared(const vector& p) const -> delta_
scalar_ type constexpr noexcept - Returns the squared distance between this and another point vector.
-
auto length() const -> delta_
scalar_ type constexpr noexcept - Returns the length (magnitude) of the vector.
-
auto length_squared() const -> delta_
scalar_ type constexpr noexcept - Returns the squared length (magnitude) of the vector.
Min, Max and Clamp
- static auto clamp(const vector& v, const vector& low, const vector& high) -> vector constexpr noexcept
- Componentwise clamps a vector between two others.
-
template <typename... T>static auto max(const vector& v1, const vector& v2, const T&... vecs) -> vector constexpr noexcept
- Returns the componentwise maximum of two or more vectors.
- static auto max(const vector* begin_, const vector* end_) -> vector constexpr noexcept
- Returns the componentwise maximum of a range of vectors.
-
static auto max(std::
initializer_list<vector> vecs) -> vector constexpr noexcept - Returns the componentwise maximum of a list of vectors.
-
template <typename... T>static auto min(const vector& v1, const vector& v2, const T&... vecs) -> vector constexpr noexcept
- Returns the componentwise minimum of two or more vectors.
- static auto min(const vector* begin_, const vector* end_) -> vector constexpr noexcept
- Returns the componentwise minimum of a range of vectors.
-
static auto min(std::
initializer_list<vector> vecs) -> vector constexpr noexcept - Returns the componentwise minimum of a list of vectors.
- auto clamp(const vector& low, const vector& high) -> vector& constexpr noexcept
- Componentwise clamps the vector between two others (in-place).
Modulo
- auto operator%=(const vector& rhs) -> vector& constexpr noexcept
- Assigns the result of componentwise dividing this vector by another.
-
auto operator%=(scalar_
type rhs) -> vector& constexpr noexcept - Assigns the result of componentwise dividing this vector by a scalar.
- auto operator%(const vector& lhs, const vector& rhs) -> vector constexpr noexcept
- Returns the remainder of componentwise dividing of one vector by another.
-
auto operator%(const vector& lhs,
scalar_
type rhs) -> vector constexpr noexcept - Returns the remainder of componentwise dividing vector by a scalar.
Multiplication
-
static auto product(const vector& v) -> scalar_
type constexpr noexcept - Returns the product of all the scalar components in a vector.
- auto operator*=(const vector& rhs) -> vector& constexpr noexcept
- Componentwise multiplies this vector by another.
-
auto operator*=(scalar_
type rhs) -> vector& constexpr noexcept - Componentwise multiplies this vector by a scalar.
-
auto product() const -> scalar_
type constexpr noexcept - Returns the product of all the scalar components in the vector.
- auto operator*(const vector& lhs, const vector& rhs) -> vector constexpr noexcept
- Returns the componentwise multiplication of two vectors.
-
auto operator*(const vector& lhs,
scalar_
type rhs) -> vector constexpr noexcept - Returns the componentwise multiplication of a vector and a scalar.
-
auto operator*(scalar_
type lhs, const vector& rhs) -> vector constexpr noexcept - Returns the componentwise multiplication of a vector and a scalar.
Normalization
- static auto consteval_normalize(const vector& v) -> vector consteval noexcept
- Normalizes a vector.
-
static auto normalize(const vector& v,
delta_
scalar_ type& length_out) -> vector constexpr noexcept - Normalizes a vector.
- static auto normalize(const vector& v) -> vector constexpr noexcept
- Normalizes a vector.
-
static auto normalize_lensq(const vector& v,
delta_
scalar_ type v_lensq) -> vector constexpr noexcept - Normalizes a vector using a pre-calculated squared-length.
- static auto normalized(const vector& v) -> bool constexpr noexcept
- Returns true if a vector is normalized (i.e. has a length of 1).
-
auto normalize(delta_
scalar_ type& length_out) -> vector& constexpr noexcept - Normalizes the vector (in-place).
- auto normalize() -> vector& constexpr noexcept
- Normalizes the vector (in-place).
-
auto normalize_lensq(delta_
scalar_ type lensq) -> vector& constexpr noexcept - Normalizes the vector using a pre-calculated squared-length (in-place).
- auto normalized() const -> bool constexpr noexcept
- Returns true if the vector is normalized (i.e. has a length of 1).
Scalar accessors
-
auto data() const -> constscalar_
type* constexpr noexcept - Returns a pointer to the first scalar component in the vector.
-
auto data() -> scalar_
type* constexpr noexcept - Returns a pointer to the first scalar component in the vector.
-
template <size_t Dimension>auto get() const -> constscalar_
type& constexpr noexcept - Gets a reference to the scalar component at a specific index.
-
template <size_t Dimension>auto get() -> scalar_
type& constexpr noexcept - Gets a reference to the scalar component at a specific index.
-
auto operator[](size_t dim) const -> constscalar_
type& constexpr noexcept - Gets a reference to the Nth scalar component.
-
auto operator[](size_t dim) -> scalar_
type& constexpr noexcept - Gets a reference to the Nth scalar component.
Subtraction
- auto operator-() const -> vector constexpr noexcept
- Returns the componentwise negation of a vector.
- auto operator-=(const vector& rhs) -> vector& constexpr noexcept
- Componentwise subtracts another vector from this one.
- auto operator-(const vector& lhs, const vector& rhs) -> vector constexpr noexcept
- Returns the componentwise subtraction of one vector from another.
Swizzles
-
template <size_t... Indices>static auto swizzle(const vector& v) -> vector<scalar_
type, sizeof...(Indices)> constexpr noexcept - Creates a vector by selecting and re-packing scalar components from another vector in an abitrary order.
-
template <size_t... Indices>auto swizzle() const -> vector<scalar_
type, sizeof...(Indices)> constexpr noexcept - Creates a vector by selecting and re-packing the scalar components from this vector in an abitrary order.
-
auto xy() const -> vector<scalar_
type, 2> constexpr noexcept - Returns a two-dimensional vector containing
{ x, y }
. -
auto xyz() const -> vector<scalar_
type, 3> constexpr noexcept - Returns a three-dimensional vector containing
{ x, y, z }
. -
auto xyz0() const -> vector<scalar_
type, 4> constexpr noexcept - Returns a four-dimensional vector containing
{ x, y, z, 0 }
. -
auto xyz1() const -> vector<scalar_
type, 4> constexpr noexcept - Returns a four-dimensional vector containing
{ x, y, z, 1 }
. -
auto xz() const -> vector<scalar_
type, 2> constexpr noexcept - Returns a two-dimensional vector containing
{ x, z }
. -
auto yx() const -> vector<scalar_
type, 2> constexpr noexcept - Returns a two-dimensional vector containing
{ y, x }
.
Friends
-
template <typename Char, typename Traits>auto operator<<(std::
basic_ostream<Char, Traits>& os, const vector& v) -> std:: basic_ostream<Char, Traits>& - Writes a vector out to a text stream.
Function documentation
template <typename Scalar, size_t Dimensions>
static delta_ scalar_ type muu::vector::angle(const vector& v1,
const vector& v2) constexpr noexcept
Calculates the angle between two vectors.
Parameters | |
---|---|
v1 | A vector. |
v2 | A vector. |
Returns | The angle between v1 and v2 (in radians). |
template <typename Scalar, size_t Dimensions>
static vector muu::vector::lerp(const vector& start,
const vector& finish,
delta_ scalar_ type alpha) constexpr noexcept
Performs a linear interpolation between two vectors.
Parameters | |
---|---|
start | The value at the start of the interpolation range. |
finish | The value at the end of the interpolation range. |
alpha | The blend factor. |
Returns | A vector with values derived from a linear interpolation from start to finish . |
template <typename Scalar, size_t Dimensions>
muu::vector::vector(scalar_ type x,
scalar_ type y) constexpr noexcept
Constructs a vector from two scalar values.
Parameters | |
---|---|
x | Initial value for the vector's first scalar component. |
y | Initial value for the vector's second scalar component. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
template <typename Scalar, size_t Dimensions>
muu::vector::vector(scalar_ type x,
scalar_ type y,
scalar_ type z = scalar_ type{}) constexpr noexcept
Constructs a vector from three scalar values.
Parameters | |
---|---|
x | Initial value for the vector's first scalar component. |
y | Initial value for the vector's second scalar component. |
z | Initial value for the vector's third scalar component. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
template <typename Scalar, size_t Dimensions>
muu::vector::vector(scalar_ type x,
scalar_ type y,
scalar_ type z = scalar_ type{},
scalar_ type w = scalar_ type{}) constexpr noexcept
Constructs a vector from four scalar values.
Parameters | |
---|---|
x | Initial value for the vector's first scalar component. |
y | Initial value for the vector's second scalar component. |
z | Initial value for the vector's third scalar component. |
w | Initial value for the vector's fourth scalar component. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
template <typename Scalar, size_t Dimensions>
muu::vector::vector(scalar_ type x,
scalar_ type y,
scalar_ type z,
scalar_ type w,
const T&... vals) constexpr noexcept
Constructs a vector from five or more scalar values.
Parameters | |
---|---|
x | Initial value for the vector's first scalar component. |
y | Initial value for the vector's second scalar component. |
z | Initial value for the vector's third scalar component. |
w | Initial value for the vector's fourth scalar component. |
vals | Initial values for the vector's remaining scalar components. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
template <typename Scalar, size_t Dimensions>
muu::vector::vector(scalar_ type broadcast) explicit constexpr noexcept
Constructs a vector by broadcasting a scalar value to all components.
Parameters | |
---|---|
broadcast | The scalar value used to initialize each of the vector's scalar components. |
muu::vector::vector(const scalar_ type(&arr)[N]) explicit constexpr noexcept
Constructs a vector from a raw array of scalars.
Template parameters | |
---|---|
N | The number of elements in the array. |
Parameters | |
arr | Array of values used to initialize the vector's scalar components. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
muu::vector::vector(const T(&arr)[N]) explicit constexpr noexcept
Constructs a vector from a raw array of scalars.
Template parameters | |
---|---|
T | A type convertible to scalar_ |
N | The number of elements in the array. |
Parameters | |
arr | Array of values used to initialize the vector's scalar components. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
muu::vector::vector(const std:: array<scalar_ type, N>& arr) explicit constexpr noexcept
Constructs a vector from a std::
Template parameters | |
---|---|
N | The number of elements in the array. |
Parameters | |
arr | Array of values used to initialize the vector's scalar components. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
muu::vector::vector(const std:: array<T, N>& arr) explicit constexpr noexcept
Constructs a vector from a std::
Template parameters | |
---|---|
T | A type convertible to scalar_ |
N | The number of elements in the array. |
Parameters | |
arr | Array of values used to initialize the vector's scalar components. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
muu::vector::vector(const T& tuple_or_bitcastable) explicit constexpr noexcept
Constructs a vector from any tuple-like or implicitly bit-castable type.
Template parameters | |
---|---|
T | A tuple-like or bit-castable type. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
muu::vector::vector(const vector<S, D>& vec) explicit constexpr noexcept
Enlarging/truncating/converting constructor.
Template parameters | |
---|---|
S | Source vector's scalar_ |
D | Source vector's dimensions. |
Parameters | |
vec | Source vector. |
Copies source vector's scalar components, casting if necessary:
vector<float, 3> xyz = { 1, 2, 3 }; vector<float, 2> xy = { xyz }; // { 1, 2 } vector<float, 4> xyzw = { xyz }; // { 1, 2, 3, 0 } auto rect = vector{ point1, point2 }; // { 1, 2, 10, 15 }
Any scalar components not covered by the constructor's parameters are initialized to zero.
template <typename Scalar, size_t Dimensions>
template <typename S1, size_t D1, typename S2, size_t D2>
muu::vector::vector(const vector<S1, D1>& vec1,
const vector<S2, D2>& vec2) constexpr noexcept
template <typename S1, size_t D1, typename S2, size_t D2>
Concatenating constructor.
Template parameters | |
---|---|
S1 | Vector 1's scalar_ |
D1 | Vector 1's dimensions. |
S2 | Vector 2's scalar_ |
D2 | Vector 2's dimensions. |
Parameters | |
vec1 | Vector 1. |
vec2 | Vector 2. |
Copies the scalar components from vector 1 and then vector 2 contiguously into the new vector:
vector<float, 2> point1 = { 1, 2 }; vector<float, 2> point2 = { 10, 15 }; vector<float, 4> rect = { point1, point2 }; // { 1, 2, 10, 15 }
Any scalar components not covered by the constructor's parameters are initialized to zero.
muu::vector::vector(const vector<S, D>& vec, const T&... vals) constexpr noexcept
Appending constructor.
Template parameters | |
---|---|
S | Vector's scalar_ |
D | Vector's dimensions. |
T | Types convertible to scalar_ |
Parameters | |
vec | A vector. |
vals | Scalar values. |
Copies the scalar components from the vector and then the list of scalars contiguously into the new vector:
vector<float, 2> point = { 1, 2 }; vector<float, 4> rect = { point, 10, 15 }; // { 1, 2, 10, 15 }
Any scalar components not covered by the constructor's parameters are initialized to zero.
muu::vector::vector(const T* vals, size_t num) noexcept
Constructs a vector from a pointer to scalars.
Template parameters | |
---|---|
T | Type convertible to scalar_ |
Parameters | |
vals | Pointer to values to copy. |
num | Number of values to copy. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
muu::vector::vector(const T* vals) explicit constexpr noexcept
Constructs a vector from a pointer to scalars.
Template parameters | |
---|---|
T | Type convertible to scalar_ |
Parameters | |
vals | Pointer to values to copy. |
muu::vector::vector(const muu:: span<T, N>& vals) explicit constexpr noexcept
Constructs a vector from a statically-sized muu::
Template parameters | |
---|---|
T | Type convertible to scalar_ |
N | The number of elements covered by the span. |
Parameters | |
vals | A span representing the values to copy. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
muu::vector::vector(const muu:: span<T>& vals) explicit constexpr noexcept
Constructs a vector from a dynamically-sized muu::
Template parameters | |
---|---|
T | Type convertible to scalar_ |
Parameters | |
vals | A span representing the values to copy. |
Any scalar components not covered by the constructor's parameters are initialized to zero.
template <typename Scalar, size_t Dimensions>
delta_ scalar_ type muu::vector::angle(const vector& v) const constexpr noexcept
Calculates the angle between this vector and another.
Parameters | |
---|---|
v | A vector. |
Returns | The angle between this vector and v (in radians). |
template <typename Scalar, size_t Dimensions>
vector& muu::vector::lerp(const vector& target,
delta_ scalar_ type alpha) constexpr noexcept
Linearly interpolates this vector towards another (in-place).
Parameters | |
---|---|
target | The 'target' value for the interpolation. |
alpha | The blend factor. |
Returns | A reference to the vector. |
template <typename Scalar, size_t Dimensions>
vector& muu::vector::operator<<=(product_ scalar_ type rhs) constexpr noexcept
Componentwise left-shifts each scalar component in the vector by the given number of bits.
template <typename Scalar, size_t Dimensions>
vector& muu::vector::operator>>=(product_ scalar_ type rhs) constexpr noexcept
Componentwise right-shifts each scalar component in the vector by the given number of bits.
template <typename Scalar, size_t Dimensions>
vector muu::vector::operator<<(const vector& lhs,
product_ scalar_ type rhs) constexpr noexcept
Returns a vector with each scalar component left-shifted the given number of bits.
template <typename Scalar, size_t Dimensions>
vector muu::vector::operator>>(const vector& lhs,
product_ scalar_ type rhs) constexpr noexcept
Returns a vector with each scalar component right-shifted the given number of bits.
template <typename Scalar, size_t Dimensions>
static vector<product_ scalar_ type, 3> muu::vector::orthogonal(const vector& v) constexpr noexcept
Returns a vector orthogonal to another.
template <typename Scalar, size_t Dimensions>
vector<product_ scalar_ type, 3> muu::vector::cross(const vector& v) const constexpr noexcept
Returns the cross product of this vector and another.
template <typename Scalar, size_t Dimensions>
vector<product_ scalar_ type, 3> muu::vector::orthogonal() const constexpr noexcept
Returns a vector orthogonal to this one.
template <typename Scalar, size_t Dimensions>
static delta_ type muu::vector::direction(const vector& from,
const vector& to,
delta_ scalar_ type& distance_out) constexpr noexcept
Returns the normalized direction vector from one position to another.
Parameters | |
---|---|
from | The start position. |
to | The end position. |
distance_out | An output param to receive the distance between the two points. |
Returns | A normalized direction vector pointing from the start position to the end position. |
template <typename Scalar, size_t Dimensions>
static delta_ type muu::vector::direction(const vector& from,
const vector& to) constexpr noexcept
Returns the normalized direction vector from one position to another.
Parameters | |
---|---|
from | The start position. |
to | The end position. |
Returns | A normalized direction vector pointing from the start position to the end position. |
template <typename Scalar, size_t Dimensions>
delta_ type muu::vector::direction(const vector& to,
delta_ scalar_ type& distance_out) const constexpr noexcept
Returns the normalized direction vector from this position to another.
Parameters | |
---|---|
to | The end position. |
distance_out | An output param to receive the distance between the two points. |
template <typename Scalar, size_t Dimensions>
delta_ type muu::vector::direction(const vector& to) const constexpr noexcept
Returns the normalized direction vector from this position to another.
Parameters | |
---|---|
to | The end position. |
static bool muu::vector::approx_equal(const vector& v1,
const vector<T, dimensions>& v2,
epsilon_ type<scalar_ type, T> epsilon = default_ epsilon<scalar_ type, T>) constexpr noexcept
Returns true if two vectors are approximately equal.
template <typename Scalar, size_t Dimensions>
static bool muu::vector::approx_zero(const vector& v,
scalar_ type epsilon = default_ epsilon<scalar_ type>) constexpr noexcept
Returns true if all the scalar components in a vector are approximately equal to zero.
bool muu::vector::approx_equal(const vector<T, dimensions>& v,
epsilon_ type<scalar_ type, T> epsilon = default_ epsilon<scalar_ type, T>) const constexpr noexcept
Returns true if the vector is approximately equal to another.
template <typename Scalar, size_t Dimensions>
bool muu::vector::approx_zero(scalar_ type epsilon = default_ epsilon<scalar_ type>) const constexpr noexcept
Returns true if all the scalar components in the vector are approximately equal to zero.
bool muu::vector::operator!=(const vector& lhs, const vector<T, dimensions>& rhs) constexpr noexcept
Returns true if two vectors are not exactly equal.
bool muu::vector::operator==(const vector& lhs, const vector<T, dimensions>& rhs) constexpr noexcept
Returns true if two vectors are exactly equal.
template <typename Scalar, size_t Dimensions>
static delta_ scalar_ type muu::vector::consteval_distance(const vector& p1,
const vector& p2) consteval noexcept
Returns the distance between two points.
template <typename Scalar, size_t Dimensions>
static delta_ scalar_ type muu::vector::consteval_length(const vector& v) consteval noexcept
Returns the length (magnitude) of a vector.
template <typename Scalar, size_t Dimensions>
static vector muu::vector::clamp(const vector& v,
const vector& low,
const vector& high) constexpr noexcept
Componentwise clamps a vector between two others.
Parameters | |
---|---|
v | The vector being clamped. |
low | The low bound of the clamp operation. |
high | The high bound of the clamp operation. |
Returns | A vector containing the scalar components from v clamped inside the given bounds. |
template <typename Scalar, size_t Dimensions>
vector& muu::vector::clamp(const vector& low,
const vector& high) constexpr noexcept
Componentwise clamps the vector between two others (in-place).
Parameters | |
---|---|
low | The low bound of the clamp operation. |
high | The high bound of the clamp operation. |
Returns | A reference to the vector. |
template <typename Scalar, size_t Dimensions>
static vector muu::vector::consteval_normalize(const vector& v) consteval noexcept
Normalizes a vector.
Parameters | |
---|---|
v | The vector to normalize. |
Returns | A normalized copy of the input vector. |
template <typename Scalar, size_t Dimensions>
static vector muu::vector::normalize(const vector& v,
delta_ scalar_ type& length_out) constexpr noexcept
Normalizes a vector.
Parameters | |
---|---|
v | The vector to normalize. |
length_out | An output param to receive the length of the vector pre-normalization. |
Returns | A normalized copy of the input vector. |
template <typename Scalar, size_t Dimensions>
static vector muu::vector::normalize_lensq(const vector& v,
delta_ scalar_ type v_lensq) constexpr noexcept
Normalizes a vector using a pre-calculated squared-length.
Parameters | |
---|---|
v | The vector to normalize. |
v_lensq | The pre-calculated squared-length of v . |
Returns | A normalized copy of the input vector. |
template <typename Scalar, size_t Dimensions>
vector& muu::vector::normalize(delta_ scalar_ type& length_out) constexpr noexcept
Normalizes the vector (in-place).
Parameters | |
---|---|
length_out | An output param to receive the length of the vector pre-normalization. |
Returns | A reference to the vector. |
template <typename Scalar, size_t Dimensions>
vector& muu::vector::normalize_lensq(delta_ scalar_ type lensq) constexpr noexcept
Normalizes the vector using a pre-calculated squared-length (in-place).
Parameters | |
---|---|
lensq | The pre-calculated squared-length of the vector. |
Returns | A reference to the vector. |
constscalar_ type& muu::vector::get() const constexpr noexcept
Gets a reference to the scalar component at a specific index.
Template parameters | |
---|---|
Dimension | The index of the dimension to retrieve, where X == 0, Y == 1, etc. |
Returns | A reference to the selected scalar component. |
scalar_ type& muu::vector::get() constexpr noexcept
Gets a reference to the scalar component at a specific index.
Template parameters | |
---|---|
Dimension | The index of the dimension to retrieve, where X == 0, Y == 1, etc. |
Returns | A reference to the selected scalar component. |
template <typename Scalar, size_t Dimensions>
constscalar_ type& muu::vector::operator[](size_t dim) const constexpr noexcept
Gets a reference to the Nth scalar component.
Returns | A reference to the selected scalar component. |
---|
template <typename Scalar, size_t Dimensions>
scalar_ type& muu::vector::operator[](size_t dim) constexpr noexcept
Gets a reference to the Nth scalar component.
Returns | A reference to the selected scalar component. |
---|
static vector<scalar_ type, sizeof...(Indices)> muu::vector::swizzle(const vector& v) constexpr noexcept
Creates a vector by selecting and re-packing scalar components from another vector in an abitrary order.
Template parameters | |
---|---|
Indices | Indices of the scalar components from the source vector in the order they're to be re-packed. |
Parameters | |
v | The source vector. |
Returns | A vector composed from the desired 'swizzle' of the source vector. |
auto v = vector{ 10, 7, 5, 9 }; // ^ ^ ^ ^ // indices: 0 1 2 3 using vec4 = decltype(v); std::cout << "swizzle<0>: " << vec4::swizzle<0>(v) << "\n"; std::cout << "swizzle<1, 0>: " << vec4::swizzle<1, 0>(v) << "\n"; std::cout << "swizzle<3, 2, 3>: " << vec4::swizzle<3, 2, 3>(v) << "\n"; std::cout << "swizzle<0, 1, 0, 1>: " << vec4::swizzle<0, 1, 0, 1>(v) << "\n";
swizzle<0>: { 10 } swizzle<1, 0>: { 7, 10 } swizzle<3, 2, 3>: { 9, 5, 9 } swizzle<0, 1, 0, 1>: { 10, 7, 10, 7 }
vector<scalar_ type, sizeof...(Indices)> muu::vector::swizzle() const constexpr noexcept
Creates a vector by selecting and re-packing the scalar components from this vector in an abitrary order.
Template parameters | |
---|---|
Indices | Indices of the scalar components in the order they're to be re-packed. |
Returns | A vector composed from the desired 'swizzle' of this one. |
auto v = vector{ 10, 7, 5, 9 }; // ^ ^ ^ ^ // indices: 0 1 2 3 std::cout << "swizzle<0>: " << v.swizzle<0>(v) << "\n"; std::cout << "swizzle<1, 0>: " << v.swizzle<1, 0>(v) << "\n"; std::cout << "swizzle<3, 2, 3>: " << v.swizzle<3, 2, 3>(v) << "\n"; std::cout << "swizzle<0, 1, 0, 1>: " << v.swizzle<0, 1, 0, 1>(v) << "\n";
swizzle<0>: { 10 } swizzle<1, 0>: { 7, 10 } swizzle<3, 2, 3>: { 9, 5, 9 } swizzle<0, 1, 0, 1>: { 10, 7, 10, 7 }
delta_ scalar_ type consteval_distance(const vector<S, D>& p1,
const vector<S, D>& p2) consteval noexcept
Returns the distance between two points.
delta_ scalar_ type consteval_length(const vector<S, D>& v) consteval noexcept
Returns the length (magnitude) of a vector.
vector<S, D> consteval_normalize(const vector<S, D>& v) consteval noexcept
Normalizes a vector.
Parameters | |
---|---|
v | The vector to normalize. |
Returns | A normalized copy of the input vector. |
delta_ type direction(const vector<S, D>& from,
const vector<S, D>& to,
delta_ scalar_ type& distance_out) constexpr noexcept
Returns the normalized direction vector from one position to another.
Parameters | |
---|---|
from | The start position. |
to | The end position. |
distance_out | An output param to receive the distance between the two points. |
Returns | A normalized direction vector pointing from the start position to the end position. |
delta_ type direction(const vector<S, D>& from,
const vector<S, D>& to) constexpr noexcept
Returns the normalized direction vector from one position to another.
Parameters | |
---|---|
from | The start position. |
to | The end position. |
Returns | A normalized direction vector pointing from the start position to the end position. |
vector<S, D> normalize(const vector<S, D>& v,
delta_ scalar_ type& length_out) constexpr noexcept
Normalizes a vector.
Parameters | |
---|---|
v | The vector to normalize. |
length_out | An output param to receive the length of the vector pre-normalization. |
Returns | A normalized copy of the input vector. |