pbpt
|
A template class for N-dimensional mathematical vectors. More...
#include <vector.hpp>
Public Member Functions | |
constexpr | Vector () noexcept=default |
Default constructor. Initializes all components to zero. | |
template<std::convertible_to< T >... Args> requires (sizeof...(Args) == N) | |
constexpr | Vector (Args &&... args) noexcept |
Constructs a vector from a list of individual components. | |
constexpr T & | x () noexcept |
Accesses the first component (x-axis). | |
constexpr T & | y () noexcept |
Accesses the second component (y-axis). | |
constexpr T & | z () noexcept |
Accesses the third component (z-axis). | |
constexpr T & | w () noexcept |
Accesses the fourth component (w-axis). | |
constexpr const T & | x () const noexcept |
Const access to the first component (x-axis). | |
constexpr const T & | y () const noexcept |
Const access to the second component (y-axis). | |
constexpr const T & | z () const noexcept |
Const access to the third component (z-axis). | |
constexpr const T & | w () const noexcept |
Const access to the fourth component (w-axis). | |
constexpr int | dims () const noexcept |
Returns the number of dimensions of the vector. | |
constexpr const T & | operator[] (int index) const |
Provides const access to the vector's components by index. | |
constexpr T & | operator[] (int index) |
Provides mutable access to the vector's components by index. | |
constexpr const T & | at (int index) const |
Provides const access to the vector's components by index. | |
constexpr Vector | operator- () const noexcept |
Negates the vector. | |
constexpr Vector & | operator+= (const Vector &rhs) noexcept |
Adds another vector to this one component-wise. | |
constexpr Vector & | operator-= (const Vector &rhs) noexcept |
Subtracts another vector from this one component-wise. | |
constexpr Vector & | operator*= (const T &rhs) noexcept |
Multiplies this vector by a scalar. | |
constexpr bool | operator== (const Vector &rhs) const noexcept |
operator== ,compare two vector component by component | |
constexpr bool | operator!= (const Vector &rhs) const noexcept |
operator!= | |
constexpr bool | is_normalized () const |
Check if the vector is normalized. | |
constexpr bool | is_zero () const |
Check if all components of the vector are zero. | |
constexpr bool | has_nan () const |
Check if any component of the vector is NaN (Not a Number). | |
template<std::convertible_to< T > U> | |
constexpr Vector | operator/= (const U &value) const noexcept |
Divides this vector by a scalar. | |
constexpr T | length_squared () const noexcept |
Calculates the squared length (magnitude) of the vector. | |
constexpr T | length () const |
Calculates the length (Euclidean norm) of the vector. | |
constexpr Vector | normalized () const |
Returns a new vector that is a normalized version of this one. | |
constexpr Vector & | normalize () |
Normalizes this vector in-place, making its length 1. | |
constexpr T | dot (const Vector &rhs) const noexcept |
Calculates the dot product of this vector and another. | |
constexpr Vector | cross (const Vector &rhs) const noexcept |
Calculates the cross product of this vector and another. | |
constexpr T | product () const noexcept |
void | apply (const std::function< void(T &, int)> &func) |
Applies a function to each element of the vector. | |
constexpr Vector | operator+ (const Vector< T, N > &rhs) const noexcept |
Adds two vectors component-wise. | |
constexpr Vector | operator- (const Vector< T, N > &rhs) const noexcept |
Subtracts one vector from another component-wise. | |
template<std::convertible_to< T > U> | |
constexpr Vector | operator* (U value) const noexcept |
Multiplies a vector by a scalar. | |
constexpr Vector | operator* (const Vector< T, N > &rhs) const noexcept |
Multiplies two vectors component-wise (Hadamard product). | |
template<std::convertible_to< T > U> | |
constexpr Vector | operator/ (U rhs) const |
Divides a vector by a scalar. | |
constexpr int | max_dim () const |
Returns the dimension with the maximum value. | |
constexpr T | max () const |
Returns the maximum value in the vector. | |
constexpr int | min_dim () const |
Returns the dimension with the minimum value. | |
constexpr T | min () const |
Returns the minimum value in the vector. | |
template<typename ... Args> requires (sizeof...(Args) == N) | |
constexpr Vector | permuted (Args ...args) const |
Returns a new vector with the specified dimensions permuted. | |
template<typename ... Args> requires (sizeof...(Args) == N) | |
constexpr Vector & | permute (Args ...args) |
Permutes the dimensions of the vector. |
Static Public Member Functions | |
static constexpr Vector | filled (T value) noexcept |
Creates a vector with all components set to a single scalar value. | |
static constexpr Vector | zeros () noexcept |
Creates a vector with all components set to zero. | |
static constexpr Vector | ones () noexcept |
Creates a vector with all components set to one. |
Friends | |
template<std::convertible_to< T > U> | |
constexpr Vector | operator* (U lhs, const Vector< T, N > &rhs) noexcept |
Multiplies a scalar by a vector. | |
std::ostream & | operator<< (std::ostream &os, const Vector &vec) |
Stream insertion operator for printing the vector. |
A template class for N-dimensional mathematical vectors.
This class provides a generic, fixed-size vector implementation suitable for various mathematical and geometric calculations. It is designed to be highly performant and flexible, with extensive use of constexpr for compile-time computations. The vector's underlying type is constrained to floating-point types, and its dimension must be positive.
T | The underlying floating-point type of the vector's components (e.g., float, double). |
N | The number of dimensions of the vector. |
|
inlineexplicitconstexprnoexcept |
Constructs a vector from a list of individual components.
This constructor allows for initialization like Vec<float, 3>(1.0f, 2.0f, 3.0f). The number of arguments must exactly match the vector's dimension N.
Args | A parameter pack of types convertible to T. |
args | The component values. |
|
inline |
Applies a function to each element of the vector.
func | The function to apply. |
|
inlineconstexpr |
Provides const access to the vector's components by index.
index | The zero-based index of the component to access. |
std::out_of_range | If index is out of bounds [0, N-1] at runtime. |
|
inlineconstexprnoexcept |
Calculates the cross product of this vector and another.
rhs | The other vector. |
|
inlineconstexprnoexcept |
Calculates the dot product of this vector and another.
rhs | The other vector. |
|
inlinestaticconstexprnoexcept |
Creates a vector with all components set to a single scalar value.
value | The value to assign to all components. |
|
inlineconstexpr |
Check if any component of the vector is NaN (Not a Number).
|
inlineconstexpr |
Check if the vector is normalized.
A normalized vector has a length of 1.0.
|
inlineconstexpr |
Check if all components of the vector are zero.
|
inlineconstexpr |
Calculates the length (Euclidean norm) of the vector.
|
inlineconstexprnoexcept |
Calculates the squared length (magnitude) of the vector.
This is faster than length() as it avoids a square root. It is often sufficient for comparing vector lengths.
|
inlineconstexpr |
Returns the maximum value in the vector.
|
inlineconstexpr |
Returns the dimension with the maximum value.
|
inlineconstexpr |
Returns the minimum value in the vector.
|
inlineconstexpr |
Returns the dimension with the minimum value.
|
inlineconstexpr |
Normalizes this vector in-place, making its length 1.
std::runtime_error | If the vector's length is zero. |
|
inlineconstexpr |
Returns a new vector that is a normalized version of this one.
The new vector will have a length of 1.
std::runtime_error | If the vector's length is zero. |
|
inlinestaticconstexprnoexcept |
Creates a vector with all components set to one.
|
inlineconstexprnoexcept |
Multiplies two vectors component-wise (Hadamard product).
*
|
inlineconstexprnoexcept |
Negates the vector.
|
inlineconstexpr |
Provides mutable access to the vector's components by index.
index | The zero-based index of the component to access. |
std::out_of_range | If index is out of bounds [0, N-1] at runtime. |
|
inlineconstexpr |
Provides const access to the vector's components by index.
index | The zero-based index of the component to access. |
std::out_of_range | If index is out of bounds [0, N-1] at runtime. |
|
inlineconstexpr |
Permutes the dimensions of the vector.
The dimensions are specified by their indices, starting from 0.
Args | The dimension indices to permute. |
|
inlineconstexpr |
Returns a new vector with the specified dimensions permuted.
The dimensions are specified by their indices, starting from 0.
Args | The dimension indices to permute. |
|
inlineconstexprnoexcept |
Const access to the fourth component (w-axis).
|
inlineconstexprnoexcept |
Accesses the fourth component (w-axis).
|
inlineconstexprnoexcept |
Const access to the first component (x-axis).
|
inlineconstexprnoexcept |
Accesses the first component (x-axis).
|
inlineconstexprnoexcept |
Const access to the second component (y-axis).
|
inlineconstexprnoexcept |
Accesses the second component (y-axis).
|
inlineconstexprnoexcept |
Const access to the third component (z-axis).
|
inlineconstexprnoexcept |
Accesses the third component (z-axis).
|
inlinestaticconstexprnoexcept |
Creates a vector with all components set to zero.
|
friend |
Stream insertion operator for printing the vector.
Prints the vector in the format (c1, c2, ..., cN).
os | The output stream. |
vec | The vector to print. |