template <typename Scalar, size_t Dimensions>
muu::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.
template <typename S1, size_t D1, typename S2, size_t D2>
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.

template <typename Scalar, size_t Dimensions>
template <size_t N>
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.

template <typename Scalar, size_t Dimensions>
template <typename T, size_t N>
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_type.
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.

template <typename Scalar, size_t Dimensions>
template <size_t N>
muu::vector::vector(const std::array<scalar_type, N>& arr) explicit constexpr noexcept

Constructs a vector from a std::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.

template <typename Scalar, size_t Dimensions>
template <typename T, size_t N>
muu::vector::vector(const std::array<T, N>& arr) explicit constexpr noexcept

Constructs a vector from a std::array of scalars.

Template parameters
T A type convertible to scalar_type.
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.

template <typename Scalar, size_t Dimensions>
template <typename T>
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.

template <typename Scalar, size_t Dimensions>
template <typename S, size_t D>
muu::vector::vector(const vector<S, D>& vec) explicit constexpr noexcept

Enlarging/truncating/converting constructor.

Template parameters
S Source vector's scalar_type.
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

Concatenating constructor.

Template parameters
S1 Vector 1's scalar_type.
D1 Vector 1's dimensions.
S2 Vector 2's scalar_type.
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.

template <typename Scalar, size_t Dimensions>
template <typename S, size_t D, typename... T>
muu::vector::vector(const vector<S, D>& vec, const T&... vals) constexpr noexcept

Appending constructor.

Template parameters
S Vector's scalar_type.
D Vector's dimensions.
T Types convertible to scalar_type.
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.

template <typename Scalar, size_t Dimensions>
template <typename T>
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_type.
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.

template <typename Scalar, size_t Dimensions>
template <typename T>
muu::vector::vector(const T* vals) explicit constexpr noexcept

Constructs a vector from a pointer to scalars.

Template parameters
T Type convertible to scalar_type.
Parameters
vals Pointer to values to copy.

template <typename Scalar, size_t Dimensions>
template <typename T, size_t N>
muu::vector::vector(const muu::span<T, N>& vals) explicit constexpr noexcept

Constructs a vector from a statically-sized muu::span.

Template parameters
T Type convertible to scalar_type.
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.

template <typename Scalar, size_t Dimensions>
template <typename T>
muu::vector::vector(const muu::span<T>& vals) explicit constexpr noexcept

Constructs a vector from a dynamically-sized muu::span.

Template parameters
T Type convertible to scalar_type.
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::cross(const vector& v1, const vector& v2) constexpr noexcept

Returns the cross product of two vectors.

template <typename Scalar, size_t Dimensions>
template <size_t Dimension>
static vector<product_scalar_type, 3> muu::vector::cross(const vector& v, index_tag<Dimension>) constexpr noexcept

Returns the cross product of a vector and a principal axis.

template <typename Scalar, size_t Dimensions>
template <size_t Dimension>
static vector<product_scalar_type, 3> muu::vector::cross(index_tag<Dimension>, const vector& v) constexpr noexcept

Returns the cross product of a principal axis and a vector.

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>
template <size_t Dimension>
vector<product_scalar_type, 3> muu::vector::cross(index_tag<Dimension>) const constexpr noexcept

Returns the cross product of this vector and a principal axis.

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.

template <typename Scalar, size_t Dimensions>
template <typename T>
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.

template <typename Scalar, size_t Dimensions>
template <typename T>
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.

template <typename Scalar, size_t Dimensions>
static bool muu::vector::zero(const vector& v) constexpr noexcept

Returns true if all the scalar components of a vector are exactly zero.

template <typename Scalar, size_t Dimensions>
bool muu::vector::zero() const constexpr noexcept

Returns true if all the scalar components of the vector are exactly zero.

template <typename Scalar, size_t Dimensions>
template <typename T>
bool muu::vector::operator!=(const vector& lhs, const vector<T, dimensions>& rhs) constexpr noexcept

Returns true if two vectors are not exactly equal.

template <typename Scalar, size_t Dimensions>
template <typename T>
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(const vector& v) constexpr 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_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() constexpr noexcept

Normalizes the vector (in-place).

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.

template <typename Scalar, size_t Dimensions>
template <size_t Dimension>
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.

template <typename Scalar, size_t Dimensions>
template <size_t Dimension>
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.

template <typename Scalar, size_t Dimensions>
template <size_t... Indices>
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 }

template <typename Scalar, size_t Dimensions>
template <size_t... Indices>
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 }

template <typename Scalar, size_t Dimensions>
template <typename S, size_t D>
delta_scalar_type angle(const vector<S, D>& v1, const vector<S, D>& 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>
template <typename S, size_t D>
delta_scalar_type consteval_distance(const vector<S, D>& p1, const vector<S, D>& p2) consteval noexcept

Returns the distance between two points.

template <typename Scalar, size_t Dimensions>
template <typename S, size_t D>
delta_scalar_type consteval_length(const vector<S, D>& v) consteval noexcept

Returns the length (magnitude) of a vector.

template <typename Scalar, size_t Dimensions>
template <typename S, size_t D>
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.

template <typename Scalar, size_t Dimensions>
template <typename S, size_t D>
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.

template <typename Scalar, size_t Dimensions>
template <typename S, size_t D>
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.

template <typename Scalar, size_t Dimensions>
template <typename S, size_t D>
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.

template <typename Scalar, size_t Dimensions>
template <typename S, size_t D>
vector<S, D> normalize(const vector<S, D>& v) constexpr noexcept

Normalizes a vector.

Parameters
v The vector to normalize.
Returns A normalized copy of the input vector.