pbpt
Loading...
Searching...
No Matches
pbpt::math::Vector< T, N > Class Template Reference

A template class for N-dimensional mathematical vectors. More...

#include <vector.hpp>

Inheritance diagram for pbpt::math::Vector< T, N >:
pbpt::math::Normal< Float, 2 > pbpt::math::Normal< Float, 3 > pbpt::math::Normal< Float, 4 > pbpt::math::Normal< T, N >

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 Vectoroperator+= (const Vector &rhs) noexcept
 Adds another vector to this one component-wise.
constexpr Vectoroperator-= (const Vector &rhs) noexcept
 Subtracts another vector from this one component-wise.
constexpr Vectoroperator*= (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 Vectornormalize ()
 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 Vectorpermute (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.

Detailed Description

template<typename T, int N>
requires (N > 0) && std::is_floating_point_v<T>
class pbpt::math::Vector< T, N >

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.

Template Parameters
TThe underlying floating-point type of the vector's components (e.g., float, double).
NThe number of dimensions of the vector.

Constructor & Destructor Documentation

◆ Vector()

template<typename T, int N>
template<std::convertible_to< T >... Args>
requires (sizeof...(Args) == N)
pbpt::math::Vector< T, N >::Vector ( Args &&... args)
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.

Template Parameters
ArgsA parameter pack of types convertible to T.
Parameters
argsThe component values.
Note
The number of arguments sizeof...(args) must be equal to N.

Member Function Documentation

◆ apply()

template<typename T, int N>
void pbpt::math::Vector< T, N >::apply ( const std::function< void(T &, int)> & func)
inline

Applies a function to each element of the vector.

Parameters
funcThe function to apply.

◆ at()

template<typename T, int N>
const T & pbpt::math::Vector< T, N >::at ( int index) const
inlineconstexpr

Provides const access to the vector's components by index.

Parameters
indexThe zero-based index of the component to access.
Returns
A const reference to the component.
Exceptions
std::out_of_rangeIf index is out of bounds [0, N-1] at runtime.
Note
In a constexpr context, out-of-bounds access will result in a compile-time error.

◆ cross()

template<typename T, int N>
Vector pbpt::math::Vector< T, N >::cross ( const Vector< T, N > & rhs) const
inlineconstexprnoexcept

Calculates the cross product of this vector and another.

Parameters
rhsThe other vector.
Returns
The resulting vector perpendicular to the two input vectors.
Note
This operation is only defined for 3D vectors (N == 3).

◆ dot()

template<typename T, int N>
T pbpt::math::Vector< T, N >::dot ( const Vector< T, N > & rhs) const
inlineconstexprnoexcept

Calculates the dot product of this vector and another.

Parameters
rhsThe other vector.
Returns
The scalar dot product.

◆ filled()

template<typename T, int N>
constexpr Vector pbpt::math::Vector< T, N >::filled ( T value)
inlinestaticconstexprnoexcept

Creates a vector with all components set to a single scalar value.

Parameters
valueThe value to assign to all components.

◆ has_nan()

template<typename T, int N>
bool pbpt::math::Vector< T, N >::has_nan ( ) const
inlineconstexpr

Check if any component of the vector is NaN (Not a Number).

Returns
true If any component is NaN.
false If no component is NaN.

◆ is_normalized()

template<typename T, int N>
bool pbpt::math::Vector< T, N >::is_normalized ( ) const
inlineconstexpr

Check if the vector is normalized.

A normalized vector has a length of 1.0.

Returns
true If the vector is normalized.
false If the vector is not normalized.

◆ is_zero()

template<typename T, int N>
bool pbpt::math::Vector< T, N >::is_zero ( ) const
inlineconstexpr

Check if all components of the vector are zero.

Returns
true If all components are zero.
false If any component is non-zero.

◆ length()

template<typename T, int N>
T pbpt::math::Vector< T, N >::length ( ) const
inlineconstexpr

Calculates the length (Euclidean norm) of the vector.

See also
length_squared()
Returns
The length of the vector.

◆ length_squared()

template<typename T, int N>
T pbpt::math::Vector< T, N >::length_squared ( ) const
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.

Returns
The squared length of the vector.

◆ max()

template<typename T, int N>
T pbpt::math::Vector< T, N >::max ( ) const
inlineconstexpr

Returns the maximum value in the vector.

Returns
The maximum value.

◆ max_dim()

template<typename T, int N>
int pbpt::math::Vector< T, N >::max_dim ( ) const
inlineconstexpr

Returns the dimension with the maximum value.

Returns
The dimension index with the maximum value.

◆ min()

template<typename T, int N>
T pbpt::math::Vector< T, N >::min ( ) const
inlineconstexpr

Returns the minimum value in the vector.

Returns
The minimum value.

◆ min_dim()

template<typename T, int N>
int pbpt::math::Vector< T, N >::min_dim ( ) const
inlineconstexpr

Returns the dimension with the minimum value.

Returns
The dimension index with the minimum value.

◆ normalize()

template<typename T, int N>
Vector & pbpt::math::Vector< T, N >::normalize ( )
inlineconstexpr

Normalizes this vector in-place, making its length 1.

Returns
A reference to this modified vector.
Exceptions
std::runtime_errorIf the vector's length is zero.
Note
In a constexpr context, normalizing a zero vector will result in a compile-time error.

◆ normalized()

template<typename T, int N>
Vector pbpt::math::Vector< T, N >::normalized ( ) const
inlineconstexpr

Returns a new vector that is a normalized version of this one.

The new vector will have a length of 1.

Returns
A new unit vector.
Exceptions
std::runtime_errorIf the vector's length is zero.
Note
In a constexpr context, normalizing a zero vector will result in a compile-time error.

◆ ones()

template<typename T, int N>
constexpr Vector pbpt::math::Vector< T, N >::ones ( )
inlinestaticconstexprnoexcept

Creates a vector with all components set to one.

Returns
A new Vec instance with all components initialized to 1.0.

◆ operator*()

template<typename T, int N>
Vector pbpt::math::Vector< T, N >::operator* ( const Vector< T, N > & rhs) const
inlineconstexprnoexcept

Multiplies two vectors component-wise (Hadamard product).

*

Warning
This is NOT a dot product or cross product.

◆ operator-()

template<typename T, int N>
Vector pbpt::math::Vector< T, N >::operator- ( ) const
inlineconstexprnoexcept

Negates the vector.

Returns
A new vector where each component is the negation of the original.

◆ operator[]() [1/2]

template<typename T, int N>
T & pbpt::math::Vector< T, N >::operator[] ( int index)
inlineconstexpr

Provides mutable access to the vector's components by index.

Parameters
indexThe zero-based index of the component to access.
Returns
A mutable reference to the component.
Exceptions
std::out_of_rangeIf index is out of bounds [0, N-1] at runtime.
Note
In a constexpr context, out-of-bounds access will result in a compile-time error.

◆ operator[]() [2/2]

template<typename T, int N>
const T & pbpt::math::Vector< T, N >::operator[] ( int index) const
inlineconstexpr

Provides const access to the vector's components by index.

Parameters
indexThe zero-based index of the component to access.
Returns
A const reference to the component.
Exceptions
std::out_of_rangeIf index is out of bounds [0, N-1] at runtime.
Note
In a constexpr context, out-of-bounds access will result in a compile-time error.

◆ permute()

template<typename T, int N>
template<typename ... Args>
requires (sizeof...(Args) == N)
Vector & pbpt::math::Vector< T, N >::permute ( Args ... args)
inlineconstexpr

Permutes the dimensions of the vector.

The dimensions are specified by their indices, starting from 0.

Template Parameters
ArgsThe dimension indices to permute.
Returns
A reference to this vector with the dimensions permuted.

◆ permuted()

template<typename T, int N>
template<typename ... Args>
requires (sizeof...(Args) == N)
Vector pbpt::math::Vector< T, N >::permuted ( Args ... args) const
inlineconstexpr

Returns a new vector with the specified dimensions permuted.

The dimensions are specified by their indices, starting from 0.

Template Parameters
ArgsThe dimension indices to permute.
Returns
A new vector with the specified dimensions permuted.

◆ w() [1/2]

template<typename T, int N>
const T & pbpt::math::Vector< T, N >::w ( ) const
inlineconstexprnoexcept

Const access to the fourth component (w-axis).

Note
N > 3.

◆ w() [2/2]

template<typename T, int N>
T & pbpt::math::Vector< T, N >::w ( )
inlineconstexprnoexcept

Accesses the fourth component (w-axis).

Note
N > 3.

◆ x() [1/2]

template<typename T, int N>
const T & pbpt::math::Vector< T, N >::x ( ) const
inlineconstexprnoexcept

Const access to the first component (x-axis).

Note
N > 0.

◆ x() [2/2]

template<typename T, int N>
T & pbpt::math::Vector< T, N >::x ( )
inlineconstexprnoexcept

Accesses the first component (x-axis).

Note
N > 0.

◆ y() [1/2]

template<typename T, int N>
const T & pbpt::math::Vector< T, N >::y ( ) const
inlineconstexprnoexcept

Const access to the second component (y-axis).

Note
N > 1.

◆ y() [2/2]

template<typename T, int N>
T & pbpt::math::Vector< T, N >::y ( )
inlineconstexprnoexcept

Accesses the second component (y-axis).

Note
N > 1.

◆ z() [1/2]

template<typename T, int N>
const T & pbpt::math::Vector< T, N >::z ( ) const
inlineconstexprnoexcept

Const access to the third component (z-axis).

Note
N > 2.

◆ z() [2/2]

template<typename T, int N>
T & pbpt::math::Vector< T, N >::z ( )
inlineconstexprnoexcept

Accesses the third component (z-axis).

Note
N > 2.

◆ zeros()

template<typename T, int N>
constexpr Vector pbpt::math::Vector< T, N >::zeros ( )
inlinestaticconstexprnoexcept

Creates a vector with all components set to zero.

Returns
A new Vec instance with all components initialized to 0.0.

◆ operator<<

template<typename T, int N>
std::ostream & operator<< ( std::ostream & os,
const Vector< T, N > & vec )
friend

Stream insertion operator for printing the vector.

Prints the vector in the format (c1, c2, ..., cN).

Parameters
osThe output stream.
vecThe vector to print.
Returns
A reference to the output stream.

The documentation for this class was generated from the following file:
  • /Users/jinceyang/Desktop/codebase/pbpt/engine/math/vector.hpp