Vector Listing#

template<typename Scalar, int64_t Dims = 3>
class GenericVector

The implementation for the Vector class. It is capable of representing an n-dimensional vector with any data type and storage type. By default, the storage type is a Vc Vector, but can be replaced with custom types for different functionality.

Template Parameters
  • Scalar – The type of each element of the vector

  • Dims – The number of dimensions of the vector

  • StorageType – The type of the storage for the vector

Public Types

using StorageType = Scalar[Dims]

Public Functions

GenericVector() = default

Default constructor.

explicit GenericVector(const StorageType &arr)

Create a Vector object from a StorageType object

Parameters

arr – The StorageType object to construct from

template<typename S, int64_t D>
GenericVector(const GenericVector<S, D> &other)

Construct a Vector from another Vector with potentially different dimensions, scalar type and storage type

Template Parameters
  • S – The scalar type of the other vector

  • D – The number of dimensions of

Parameters

other – The other vector to construct from

template<typename ...Args, std::enable_if_t<sizeof...(Args) == Dims, int> = 0>
GenericVector(Args... args)

Construct a Vector object from n values, where n is the number of dimensions of the vector

Template Parameters

Args – Parameter pack template type

Parameters

args – The values to construct the vector from

template<typename ...Args, int64_t size = sizeof...(Args), typename std::enable_if_t<size != Dims, int> = 0>
GenericVector(Args... args)

Construct a Vector object from an arbitrary number of arguments. See other vector constructors for more information

Template Parameters
  • Args – Parameter pack template type

  • size – Number of arguments passed

Parameters

args – Values

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector(const std::initializer_list<T> &list)

Construct a Vector object from an std::initializer_list

Template Parameters

T – The type of each element of the initializer list

Parameters

list – The initializer list to construct from

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector(const std::vector<T> &list)

Construct a Vector object from an std::vector

Template Parameters

T – The type of each element of the vector

Parameters

list – The vector to construct from

GenericVector(const GenericVector &other) = default

Create a Vector from another vector instance

Parameters

other – Vector to copy values from

GenericVector(GenericVector &&other) noexcept = default

Move constructor for Vector objects

Parameters

other – Vector to move

GenericVector &operator=(const GenericVector &other) = default

Assignment operator for Vector objects

Parameters

other – Vector to copy values from

Returns

Reference to this

GenericVector &operator=(GenericVector &&other) noexcept = default

Assignment move constructor for Vector objects

Parameters

other – Vector to move

Returns

Reference to this

const Scalar &operator[](int64_t index) const

Access a specific element of the vector

Parameters

index – The index of the element to access

Returns

Reference to the element

Scalar &operator[](int64_t index)

Access a specific element of the vector

Parameters

index – The index of the element to access

Returns

Reference to the element

template<typename T, int64_t d>
GenericVector &operator+=(const GenericVector<T, d> &other)

Add a vector to this vector, element-by-element

Parameters

other – The vector to add

Returns

Reference to this

template<typename T, int64_t d>
GenericVector &operator-=(const GenericVector<T, d> &other)

Subtract a vector from this vector, element-by-element

Parameters

other – The vector to subtract

Returns

Reference to this

template<typename T, int64_t d>
GenericVector &operator*=(const GenericVector<T, d> &other)

Multiply this vector by another vector, element-by-element

Parameters

other – The vector to multiply by

Returns

Reference to this

template<typename T, int64_t d>
GenericVector &operator/=(const GenericVector<T, d> &other)

Divide this vector by another vector, element-by-element

Parameters

other – The vector to divide by

Returns

Reference to this

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector &operator+=(const T &value)

Add a scalar to this vector, element-by-element

Parameters

other – The scalar to add

Returns

Reference to this

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector &operator-=(const T &value)

Subtract a scalar from this vector, element-by-element

Parameters

other – The scalar to subtract

Returns

Reference to this

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector &operator*=(const T &value)

Multiply this vector by a scalar, element-by-element

Parameters

other – The scalar to multiply by

Returns

Reference to this

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector &operator/=(const T &value)

Divide this vector by a scalar, element-by-element

Parameters

other – The scalar to divide by

Returns

Reference to this

GenericVector operator-() const

Negate this vector

Returns

Vector with all elements negated

template<typename T, int64_t d>
GenericVector cmp(const GenericVector<T, d> &other, const char *mode) const

Compare two vectors for equality. Available modes are:

  • ”eq” - Check for equality

  • ”ne” - Check for inequality

  • ”lt” - Check if each element is less than the corresponding element in the other

  • ”le” - Check if each element is less than or equal to the corresponding element in the other

  • ”gt” - Check if each element is greater than the corresponding element in the other

  • ”ge” - Check if each element is greater than or equal to the corresponding element in the other

Parameters
  • other – The vector to compare to

  • mode – The comparison mode

Returns

Vector with each element set to 1 if the comparison is true, 0 otherwise

template<typename T>
GenericVector cmp(const T &value, const char *mode) const

Compare a vector and a scalar for equality. Available modes are:

  • ”eq” - Check for equality

  • ”ne” - Check for inequality

  • ”lt” - Check if each element is less than the scalar

  • ”le” - Check if each element is less than or equal to the scalar

  • ”gt” - Check if each element is greater than the scalar

  • ”ge” - Check if each element is greater than or equal to the scalar

Parameters
  • value – The scalar to compare to

  • mode – The comparison mode

Returns

Vector with each element set to 1 if the comparison is true, 0 otherwise

template<typename T, int64_t d>
GenericVector operator<(const GenericVector<T, d> &other) const

Equivalent to calling cmp(other, “lt”)

See also

cmp()

Parameters

other – The vector to compare to

Returns

See cmp()

template<typename T, int64_t d>
GenericVector operator<=(const GenericVector<T, d> &other) const

Equivalent to calling cmp(other, “le”)

See also

cmp()

Parameters

other – The vector to compare to

Returns

See cmp()

template<typename T, int64_t d>
GenericVector operator>(const GenericVector<T, d> &other) const

Equivalent to calling cmp(other, “gt”)

See also

cmp()

Parameters

other – The vector to compare to

Returns

See cmp()

template<typename T, int64_t d>
GenericVector operator>=(const GenericVector<T, d> &other) const

Equivalent to calling cmp(other, “ge”)

See also

cmp()

Parameters

other – The vector to compare to

Returns

See cmp()

template<typename T, int64_t d>
GenericVector operator==(const GenericVector<T, d> &other) const

Equivalent to calling cmp(other, “eq”)

See also

cmp()

Parameters

other – The vector to compare to

Returns

See cmp()

template<typename T, int64_t d>
GenericVector operator!=(const GenericVector<T, d> &other) const

Equivalent to calling cmp(other, “ne”)

See also

cmp()

Parameters

other – The vector to compare to

Returns

See cmp()

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector operator<(const T &other) const

Equivalent to calling cmp(other, “lt”)

See also

cmp()

Parameters

value – The scalar to compare to

Returns

See cmp()

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector operator<=(const T &other) const

Equivalent to calling cmp(other, “le”)

See also

cmp()

Parameters

value – The scalar to compare to

Returns

See cmp()

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector operator>(const T &other) const

Equivalent to calling cmp(other, “gt”)

See also

cmp()

Parameters

value – The scalar to compare to

Returns

See cmp()

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector operator>=(const T &other) const

Equivalent to calling cmp(other, “ge”)

See also

cmp()

Parameters

value – The scalar to compare to

Returns

See cmp()

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector operator==(const T &other) const

Equivalent to calling cmp(other, “eq”)

See also

cmp()

Parameters

value – The scalar to compare to

Returns

See cmp()

template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int> = 0>
GenericVector operator!=(const T &other) const

Equivalent to calling cmp(other, “ne”)

See also

cmp()

Parameters

value – The scalar to compare to

Returns

See cmp()

Scalar mag2() const

Calculate the magnitude of this vector squared

Returns

The magnitude squared

Scalar mag() const

Calculate the magnitude of this vector

Returns

The magnitude

inline Scalar invMag() const

Calculate 1/mag(this)

Returns

1/mag(this)

GenericVector norm() const

Calculate the normalized version of this vector

Returns

The normalized vector

Scalar dot(const GenericVector &other) const

Calculate the dot product of this vector and another

Parameters

other – The other vector

Returns

The dot product

GenericVector cross(const GenericVector &other) const

Calculate the cross product of this vector and another

Parameters

other – The other vector

Returns

The cross product

GenericVector proj(const GenericVector &other) const

Project vector other onto this vector and return the result.

Perform vector projection using the formula: \( \operatorname{proj}_a(\vec{b})=rac{\vec{b} \cdot \vec{a}}{|\vec{a}|^2} \cdot \vec{a} \)

Parameters

other – The vector to project

Returns

The projection of other onto this vector

explicit operator bool() const

Cast this vector to a boolean. This is equivalent to calling mag2() != 0

Returns

True if the magnitude of this vector is not 0, false otherwise

Scalar x() const

Access the x component of this vector

Returns

The x component of this vector

Scalar y() const

Access the y component of this vector

Returns

The y component of this vector

Scalar z() const

Access the z component of this vector

Returns

The z component of this vector

Scalar w() const

Access the w component of this vector

Returns

The w component of this vector

template<size_t... Indices>
GenericVector<Scalar, sizeof...(Indices)> swizzle() const

Vector swizzle.

Create a new vector with \( m \) dimensions, where \( m \leq n \), where \( n \) is the dimension of this vector. The new vector is created by selecting the elements of this vector at the indices specified by Indices.

Template Parameters

Indices – The indices to select

Returns

A new vector with the selected elements

GenericVector<Scalar, 2> xy() const
GenericVector<Scalar, 2> yx() const
GenericVector<Scalar, 2> xz() const
GenericVector<Scalar, 2> zx() const
GenericVector<Scalar, 2> yz() const
GenericVector<Scalar, 2> zy() const
GenericVector<Scalar, 3> xyz() const
GenericVector<Scalar, 3> xzy() const
GenericVector<Scalar, 3> yxz() const
GenericVector<Scalar, 3> yzx() const
GenericVector<Scalar, 3> zxy() const
GenericVector<Scalar, 3> zyx() const
GenericVector<Scalar, 3> xyw() const
GenericVector<Scalar, 3> xwy() const
GenericVector<Scalar, 3> yxw() const
GenericVector<Scalar, 3> ywx() const
GenericVector<Scalar, 3> wxy() const
GenericVector<Scalar, 3> wyx() const
GenericVector<Scalar, 3> xzw() const
GenericVector<Scalar, 3> xwz() const
GenericVector<Scalar, 3> zxw() const
GenericVector<Scalar, 3> zwx() const
GenericVector<Scalar, 3> wxz() const
GenericVector<Scalar, 3> wzx() const
GenericVector<Scalar, 3> yzw() const
GenericVector<Scalar, 3> ywz() const
GenericVector<Scalar, 3> zyw() const
GenericVector<Scalar, 3> zwy() const
GenericVector<Scalar, 3> wyz() const
GenericVector<Scalar, 3> wzy() const
GenericVector<Scalar, 4> xyzw() const
GenericVector<Scalar, 4> xywz() const
GenericVector<Scalar, 4> xzyw() const
GenericVector<Scalar, 4> xzwy() const
GenericVector<Scalar, 4> xwyz() const
GenericVector<Scalar, 4> xwzy() const
GenericVector<Scalar, 4> yxzw() const
GenericVector<Scalar, 4> yxwz() const
GenericVector<Scalar, 4> yzxw() const
GenericVector<Scalar, 4> yzwx() const
GenericVector<Scalar, 4> ywxz() const
GenericVector<Scalar, 4> ywzx() const
GenericVector<Scalar, 4> zxyw() const
GenericVector<Scalar, 4> zxwy() const
GenericVector<Scalar, 4> zyxw() const
GenericVector<Scalar, 4> zywx() const
GenericVector<Scalar, 4> zwxy() const
GenericVector<Scalar, 4> zwyx() const
GenericVector<Scalar, 4> wxyz() const
GenericVector<Scalar, 4> wxzy() const
GenericVector<Scalar, 4> wyxz() const
GenericVector<Scalar, 4> wyzx() const
GenericVector<Scalar, 4> wzxy() const
GenericVector<Scalar, 4> wzyx() const
void x(Scalar val)

Set the x component of this vector

Parameters

val – The new value of the x component

void y(Scalar val)

Set the y component of this vector

Parameters

val – The new value of the y component

void z(Scalar val)

Set the z component of this vector

Parameters

val – The new value of the z component

void w(Scalar val)

Set the w component of this vector

Parameters

val – The new value of the w component

void xy(const GenericVector<Scalar, 2> &v)
void yx(const GenericVector<Scalar, 2> &v)
void xz(const GenericVector<Scalar, 2> &v)
void zx(const GenericVector<Scalar, 2> &v)
void yz(const GenericVector<Scalar, 2> &v)
void zy(const GenericVector<Scalar, 2> &v)
void xyz(const GenericVector<Scalar, 3> &v)
void xzy(const GenericVector<Scalar, 3> &v)
void yxz(const GenericVector<Scalar, 3> &v)
void yzx(const GenericVector<Scalar, 3> &v)
void zxy(const GenericVector<Scalar, 3> &v)
void zyx(const GenericVector<Scalar, 3> &v)
void xyw(const GenericVector<Scalar, 3> &v)
void xwy(const GenericVector<Scalar, 3> &v)
void yxw(const GenericVector<Scalar, 3> &v)
void ywx(const GenericVector<Scalar, 3> &v)
void wxy(const GenericVector<Scalar, 3> &v)
void wyx(const GenericVector<Scalar, 3> &v)
void xzw(const GenericVector<Scalar, 3> &v)
void xwz(const GenericVector<Scalar, 3> &v)
void zxw(const GenericVector<Scalar, 3> &v)
void zwx(const GenericVector<Scalar, 3> &v)
void wxz(const GenericVector<Scalar, 3> &v)
void wzx(const GenericVector<Scalar, 3> &v)
void yzw(const GenericVector<Scalar, 3> &v)
void ywz(const GenericVector<Scalar, 3> &v)
void zyw(const GenericVector<Scalar, 3> &v)
void zwy(const GenericVector<Scalar, 3> &v)
void wyz(const GenericVector<Scalar, 3> &v)
void wzy(const GenericVector<Scalar, 3> &v)
void xyzw(const GenericVector<Scalar, 4> &v)
void xywz(const GenericVector<Scalar, 4> &v)
void xzyw(const GenericVector<Scalar, 4> &v)
void xzwy(const GenericVector<Scalar, 4> &v)
void xwyz(const GenericVector<Scalar, 4> &v)
void xwzy(const GenericVector<Scalar, 4> &v)
void yxzw(const GenericVector<Scalar, 4> &v)
void yxwz(const GenericVector<Scalar, 4> &v)
void yzxw(const GenericVector<Scalar, 4> &v)
void yzwx(const GenericVector<Scalar, 4> &v)
void ywxz(const GenericVector<Scalar, 4> &v)
void ywzx(const GenericVector<Scalar, 4> &v)
void zxyw(const GenericVector<Scalar, 4> &v)
void zxwy(const GenericVector<Scalar, 4> &v)
void zyxw(const GenericVector<Scalar, 4> &v)
void zywx(const GenericVector<Scalar, 4> &v)
void zwxy(const GenericVector<Scalar, 4> &v)
void zwyx(const GenericVector<Scalar, 4> &v)
void wxyz(const GenericVector<Scalar, 4> &v)
void wxzy(const GenericVector<Scalar, 4> &v)
void wyxz(const GenericVector<Scalar, 4> &v)
void wyzx(const GenericVector<Scalar, 4> &v)
void wzxy(const GenericVector<Scalar, 4> &v)
void wzyx(const GenericVector<Scalar, 4> &v)
const StorageType &data() const

Return the underlying storage type

Returns

The underlying storage type

StorageType &data()

Return the underlying storage type

Returns

The underlying storage type

std::string str(const std::string &formatString = "{}") const

Convert a vector into a string representation &#8212; “(x, y, z, w, …)”

Parameters

formatString – The format string to use for each component

Returns

A string representation of this vector

template<typename T, int64_t d>
auto operator+=(const GenericVector<T, d> &other) -> GenericVector&
template<typename T, int64_t d>
auto operator-=(const GenericVector<T, d> &other) -> GenericVector&
template<typename T, int64_t d>
auto operator*=(const GenericVector<T, d> &other) -> GenericVector&
template<typename T, int64_t d>
auto operator/=(const GenericVector<T, d> &other) -> GenericVector&
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator+=(const T &value) -> GenericVector&
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator-=(const T &value) -> GenericVector&
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator*=(const T &value) -> GenericVector&
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator/=(const T &value) -> GenericVector&
template<typename T, int64_t d>
auto cmp(const GenericVector<T, d> &other, const char *mode) const -> GenericVector
template<typename T>
auto cmp(const T &value, const char *mode) const -> GenericVector
template<typename T, int64_t d>
auto operator<(const GenericVector<T, d> &other) const -> GenericVector
template<typename T, int64_t d>
auto operator<=(const GenericVector<T, d> &other) const -> GenericVector
template<typename T, int64_t d>
auto operator>(const GenericVector<T, d> &other) const -> GenericVector
template<typename T, int64_t d>
auto operator>=(const GenericVector<T, d> &other) const -> GenericVector
template<typename T, int64_t d>
auto operator==(const GenericVector<T, d> &other) const -> GenericVector
template<typename T, int64_t d>
auto operator!=(const GenericVector<T, d> &other) const -> GenericVector
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator<(const T &other) const -> GenericVector
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator<=(const T &other) const -> GenericVector
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator>(const T &other) const -> GenericVector
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator>=(const T &other) const -> GenericVector
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator==(const T &other) const -> GenericVector
template<typename T, std::enable_if_t<std::is_convertible_v<T, Scalar>, int>>
auto operator!=(const T &other) const -> GenericVector
template<size_t... Indices>
auto swizzle() const -> GenericVector<Scalar, sizeof...(Indices)>

Protected Attributes

StorageType m_data = {}