DirectZ
Loading...
Searching...
No Matches
dz::vec< T, N > Struct Template Reference

Template struct representing an N-dimensional vector of type T. More...

#include <math.hpp>

Inheritance diagram for dz::vec< T, N >:
dz::color_vec< T, N >

Public Member Functions

template<typename... Args>
 vec (Args... args)
 Variadic constructor to initialize vector components.
template<typename OT, size_t ON>
 vec (const vec< OT, ON > &other)
 Copy constructor to convert from vectors of different types and/or sizes.
vecoperator= (const vec &other)
 Assignment operator for component-wise copy.
Toperator[] (size_t i)
 Index operator for non-const access to vector components.
const Toperator[] (size_t i) const
 Index operator for const access to vector components.
template<typename O>
vecoperator*= (const O &other)
 Compound multiplication-assignment operator.
template<typename O>
vecoperator/= (const O &other)
 Compound division-assignment operator.
template<typename O>
vecoperator+= (const O &other)
 Compound addition-assignment operator.
template<typename O>
vecoperator-= (const O &other)
 Compound subtraction-assignment operator.
template<typename O>
vec operator* (const O &other) const
 Multiplication operator returning a new vector.
template<typename O>
vec operator/ (const O &other) const
 Division operator returning a new vector.
template<typename O>
vec operator+ (const O &other) const
 Addition operator returning a new vector.
template<typename O>
vec operator- (const O &other) const
 Subtraction operator returning a new vector.
vec operator- () const
 Unary negation operator.
T length () const
 Computes the Euclidean length (magnitude) of the vector.
vec normalize () const
 Returns a normalized (unit length) copy of the vector.

Public Attributes

T data [N]
 Underlying array storing vector components.

Detailed Description

template<typename T, size_t N>
struct dz::vec< T, N >

Template struct representing an N-dimensional vector of type T.

This class supports construction from multiple arguments or a single scalar to fill all components, copy construction from vectors of different types and sizes, element access, and arithmetic operations (component-wise or scalar). It also supports vector length computation and normalization.

Template Parameters
TThe scalar type of the vector components.
NThe dimensionality of the vector.

Constructor & Destructor Documentation

◆ vec() [1/2]

template<typename T, size_t N>
template<typename... Args>
dz::vec< T, N >::vec ( Args... args)
inline

Variadic constructor to initialize vector components.

Behaves differently depending on the number of arguments passed:

  • If no arguments are provided, initializes all components to T() (default constructed).
  • If one argument is provided, initializes all components to that value.
  • If multiple arguments are provided, initializes each component to the corresponding argument.
Template Parameters
ArgsVariadic template arguments representing component values.
Parameters
argsValues used to initialize vector components.

◆ vec() [2/2]

template<typename T, size_t N>
template<typename OT, size_t ON>
dz::vec< T, N >::vec ( const vec< OT, ON > & other)
inline

Copy constructor to convert from vectors of different types and/or sizes.

Copies component-wise up to the minimum of source and destination size, zero-initializes remaining components if destination vector is larger.

Template Parameters
OTThe source vector component type.
ONThe source vector dimension.
Parameters
otherThe vector to copy from.

Member Function Documentation

◆ length()

template<typename T, size_t N>
T dz::vec< T, N >::length ( ) const
inline

Computes the Euclidean length (magnitude) of the vector.

Calculates the square root of the sum of squares of all components.

Returns
The length of the vector as type T.

◆ normalize()

template<typename T, size_t N>
vec dz::vec< T, N >::normalize ( ) const
inline

Returns a normalized (unit length) copy of the vector.

Divides all components by the vector length.

Returns
A new vector normalized to length 1.

◆ operator*()

template<typename T, size_t N>
template<typename O>
vec dz::vec< T, N >::operator* ( const O & other) const
inline

Multiplication operator returning a new vector.

Supports multiplication by a scalar or component-wise multiplication by another vector.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to multiply by.
Returns
A new vector resulting from the multiplication.

◆ operator*=()

template<typename T, size_t N>
template<typename O>
vec & dz::vec< T, N >::operator*= ( const O & other)
inline

Compound multiplication-assignment operator.

Supports multiplication by a scalar or component-wise multiplication by another vector. Throws std::runtime_error if the operand type is unsupported.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to multiply by.
Returns
Reference to this vector after multiplication.
Exceptions
std::runtime_errorif unsupported operand type is used.

◆ operator+()

template<typename T, size_t N>
template<typename O>
vec dz::vec< T, N >::operator+ ( const O & other) const
inline

Addition operator returning a new vector.

Supports addition by a scalar or component-wise addition by another vector.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to add.
Returns
A new vector resulting from the addition.

◆ operator+=()

template<typename T, size_t N>
template<typename O>
vec & dz::vec< T, N >::operator+= ( const O & other)
inline

Compound addition-assignment operator.

Supports addition by a scalar or component-wise addition by another vector. Throws std::runtime_error if the operand type is unsupported.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to add.
Returns
Reference to this vector after addition.
Exceptions
std::runtime_errorif unsupported operand type is used.

◆ operator-() [1/2]

template<typename T, size_t N>
vec dz::vec< T, N >::operator- ( ) const
inline

Unary negation operator.

Returns a new vector with all components negated.

Returns
A new vector with negated components.

◆ operator-() [2/2]

template<typename T, size_t N>
template<typename O>
vec dz::vec< T, N >::operator- ( const O & other) const
inline

Subtraction operator returning a new vector.

Supports subtraction by a scalar or component-wise subtraction by another vector.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to subtract.
Returns
A new vector resulting from the subtraction.

◆ operator-=()

template<typename T, size_t N>
template<typename O>
vec & dz::vec< T, N >::operator-= ( const O & other)
inline

Compound subtraction-assignment operator.

Supports subtraction by a scalar or component-wise subtraction by another vector. Throws std::runtime_error if the operand type is unsupported.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to subtract.
Returns
Reference to this vector after subtraction.
Exceptions
std::runtime_errorif unsupported operand type is used.

◆ operator/()

template<typename T, size_t N>
template<typename O>
vec dz::vec< T, N >::operator/ ( const O & other) const
inline

Division operator returning a new vector.

Supports division by a scalar or component-wise division by another vector.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to divide by.
Returns
A new vector resulting from the division.

◆ operator/=()

template<typename T, size_t N>
template<typename O>
vec & dz::vec< T, N >::operator/= ( const O & other)
inline

Compound division-assignment operator.

Supports division by a scalar or component-wise division by another vector. Throws std::runtime_error if the operand type is unsupported.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to divide by.
Returns
Reference to this vector after division.
Exceptions
std::runtime_errorif unsupported operand type is used.

◆ operator=()

template<typename T, size_t N>
vec & dz::vec< T, N >::operator= ( const vec< T, N > & other)
inline

Assignment operator for component-wise copy.

Copies all components from the other vector using memcpy for efficiency.

Parameters
otherThe vector to copy from.
Returns
Reference to this vector after assignment.

◆ operator[]() [1/2]

template<typename T, size_t N>
T & dz::vec< T, N >::operator[] ( size_t i)
inline

Index operator for non-const access to vector components.

No bounds checking is performed.

Parameters
iThe component index to access.
Returns
Reference to the component at index i.

◆ operator[]() [2/2]

template<typename T, size_t N>
const T & dz::vec< T, N >::operator[] ( size_t i) const
inline

Index operator for const access to vector components.

No bounds checking is performed.

Parameters
iThe component index to access.
Returns
Const reference to the component at index i.

Member Data Documentation

◆ data

template<typename T, size_t N>
T dz::vec< T, N >::data[N]

Underlying array storing vector components.


The documentation for this struct was generated from the following file: