Sparkle 0.0.1
Loading...
Searching...
No Matches
spk::IVector4< TType > Struct Template Reference

4D value type with arithmetic helpers and swizzles. More...

#include <spk_vector4.hpp>

Collaboration diagram for spk::IVector4< TType >:
Collaboration graph

Public Types

using value_type = TType
 Component type stored by the vector.

Public Member Functions

constexpr IVector4 ()=default
 Builds a zero vector.
template<typename TX, typename TY, typename TZ, typename TW>
requires (std::is_constructible_v<TType, TX> && std::is_constructible_v<TType, TY> && std::is_constructible_v<TType, TZ> && std::is_constructible_v<TType, TW>)
constexpr IVector4 (const TX &p_x, const TY &p_y, const TZ &p_z, const TW &p_w)
 Builds a vector from four components.
template<typename TOtherVector, typename TZ, typename TW>
requires (std::is_constructible_v<TType, TOtherVector> && std::is_constructible_v<TType, TZ> && std::is_constructible_v<TType, TW>)
constexpr IVector4 (const IVector2< TOtherVector > &p_other, const TZ &p_z, const TW &p_w)
 Builds a vector from a 2D vector plus Z and W components.
template<typename TOtherVector, typename TW>
requires (std::is_constructible_v<TType, TOtherVector> && std::is_constructible_v<TType, TW>)
constexpr IVector4 (const IVector3< TOtherVector > &p_other, const TW &p_w)
 Builds a vector from a 3D vector and W component.
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
constexpr IVector4 (const IVector4< TOther > &p_other)
 Converting constructor from another component type.
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
constexpr IVector4 (const TOther &p_value)
 Fills all components with the same value.
std::string toString () const
 Converts the vector to a string.
std::wstring toWstring () const
 Converts the vector to a wide string.
bool operator== (const IVector4 &p_other) const
 Checks component-wise equality using SafeComparand.
bool operator!= (const IVector4 &p_other) const
 Negation of equality.
IVector4 operator+ (const IVector4 &p_other) const
 Component-wise addition.
IVector4 operator- (const IVector4 &p_other) const
 Component-wise subtraction.
IVector4 operator* (const IVector4 &p_other) const
 Component-wise multiplication.
IVector4 operator/ (const IVector4 &p_other) const
 Component-wise division.
IVector4 operator+ (const TType &p_scalar) const
 Adds a scalar to each component.
IVector4 operator- (const TType &p_scalar) const
 Subtracts a scalar from each component.
IVector4 operator* (const TType &p_scalar) const
 Multiplies each component by a scalar.
IVector4 operator/ (const TType &p_scalar) const
 Divides each component by a scalar.
IVector4 inverse () const
 Return the inverse vector.
IVector4 operator- () const
 Unary minus.
IVector4operator+= (const IVector4 &p_other)
 In-place component-wise addition.
IVector4operator-= (const IVector4 &p_other)
 In-place component-wise subtraction.
IVector4operator*= (const IVector4 &p_other)
 In-place component-wise multiplication.
IVector4operator/= (const IVector4 &p_other)
 In-place component-wise division.
IVector4operator+= (const TType &p_scalar)
 In-place scalar addition.
IVector4operator-= (const TType &p_scalar)
 In-place scalar subtraction.
IVector4operator*= (const TType &p_scalar)
 In-place scalar multiplication.
IVector4operator/= (const TType &p_scalar)
 In-place scalar division.
bool isZero () const
 Checks if all components are zero.
float squaredLength () const
 Computes squared vector length.
IVector3< float > normalize () const
 Return the vector once normalized by its length.
float length () const
 Computes Euclidean length.
float squaredDistance (const IVector4 &p_other) const
 Computes squared distance to another vector.
float distance (const IVector4 &p_other) const
 Computes Euclidean distance to another vector.
float dot (const IVector4 &p_other) const
 Computes dot product with another vector.
IVector3< TType > xyz () const
 Projects this vector into 3D using x, y, z components.
IVector4 clamped (const IVector4 &p_boundA, const IVector4 &p_boundB) const
 Returns a clamped copy of this vector.
template<typename TOther>
requires std::is_constructible_v<TOther, TType>
constexpr operator IVector4< TOther > () const
 Explicitly converts to another IVector4 component type.

Static Public Member Functions

template<typename TAlpha>
requires std::is_arithmetic_v<TAlpha>
static IVector4 lerp (const IVector4 &p_from, const IVector4 &p_to, const TAlpha &p_alpha)
 Linearly interpolates between two vectors.
static IVector4 min (const IVector4 &p_a, const IVector4 &p_b)
 Component-wise minimum of two vectors.
template<typename... TOthers>
requires (sizeof...(TOthers) > 0 && (std::is_same_v<TOthers, IVector4> && ...))
static IVector4 min (const IVector4 &p_a, const IVector4 &p_b, const TOthers &...p_rest)
 Component-wise minimum of multiple vectors.
static IVector4 min (std::initializer_list< IVector4 > p_values)
 Component-wise minimum across an initializer list.
static IVector4 max (const IVector4 &p_a, const IVector4 &p_b)
 Component-wise maximum of two vectors.
template<typename... TOthers>
requires (sizeof...(TOthers) > 0 && (std::is_same_v<TOthers, IVector4> && ...))
static IVector4 max (const IVector4 &p_a, const IVector4 &p_b, const TOthers &...p_rest)
 Component-wise maximum of multiple vectors.
static IVector4 max (std::initializer_list< IVector4 > p_values)
 Component-wise maximum across an initializer list.
static IVector4 clamp (const IVector4 &p_value, const IVector4 &p_boundA, const IVector4 &p_boundB)
 Clamps each component of a vector between two bounds.
static bool isBetween (const IVector4 &p_value, const IVector4 &p_boundA, const IVector4 &p_boundB)
 Checks whether a vector lies within inclusive bounds.

Public Attributes

TType x {}
 X component.
TType y {}
 Y component.
TType z {}
 Z component.
TType w {}
 W component.

Static Public Attributes

static const IVector4 Zero
 Predefined zero vector.
static const IVector4 One
 Predefined unit vector (1, 1, 1, 1).

Friends

std::ostream & operator<< (std::ostream &p_stream, const IVector4 &p_value)
 Streams the vector to an output stream.
std::wostream & operator<< (std::wostream &p_stream, const IVector4 &p_value)
 Streams the vector to a wide output stream.
IVector4 operator+ (const TType &p_scalar, const IVector4 &p_vector)
 Adds a scalar to a vector (scalar on the left side).
IVector4 operator- (const TType &p_scalar, const IVector4 &p_vector)
 Subtracts vector components from a scalar (scalar on the left side).
IVector4 operator* (const TType &p_scalar, const IVector4 &p_vector)
 Multiplies a scalar by a vector (scalar on the left side).
IVector4 operator/ (const TType &p_scalar, const IVector4 &p_vector)
 Divides a scalar by each vector component (scalar on the left side).

Detailed Description

template<typename TType>
struct spk::IVector4< TType >

4D value type with arithmetic helpers and swizzles.

Template Parameters
TTypearithmetic component type; must be constructible from input values and support basic arithmetic and comparisons.
See also
Vector4, Vector3, Vector2
spk::Vector4 color{1.f, 0.5f, 0.25f, 1.f};
spk::Vector4 tint{0.1f, 0.f, 0.f, 0.f};
const auto mixed = color + tint;
const auto len = mixed.length();
float length() const
Computes Euclidean length.
Definition spk_vector4.hpp:570

Constructor & Destructor Documentation

◆ IVector4() [1/5]

template<typename TType>
template<typename TX, typename TY, typename TZ, typename TW>
requires (std::is_constructible_v<TType, TX> && std::is_constructible_v<TType, TY> && std::is_constructible_v<TType, TZ> && std::is_constructible_v<TType, TW>)
spk::IVector4< TType >::IVector4 ( const TX & p_x,
const TY & p_y,
const TZ & p_z,
const TW & p_w )
inlineconstexpr

Builds a vector from four components.

Parameters
p_xX value.
p_yY value.
p_zZ value.
p_wW value.

◆ IVector4() [2/5]

template<typename TType>
template<typename TOtherVector, typename TZ, typename TW>
requires (std::is_constructible_v<TType, TOtherVector> && std::is_constructible_v<TType, TZ> && std::is_constructible_v<TType, TW>)
spk::IVector4< TType >::IVector4 ( const IVector2< TOtherVector > & p_other,
const TZ & p_z,
const TW & p_w )
inlineconstexpr

Builds a vector from a 2D vector plus Z and W components.

Template Parameters
TOtherVector2D vector component type.
TZZ component type.
TWW component type.
Parameters
p_otherSource 2D vector.
p_zZ value.
p_wW value.

◆ IVector4() [3/5]

template<typename TType>
template<typename TOtherVector, typename TW>
requires (std::is_constructible_v<TType, TOtherVector> && std::is_constructible_v<TType, TW>)
spk::IVector4< TType >::IVector4 ( const IVector3< TOtherVector > & p_other,
const TW & p_w )
inlineconstexpr

Builds a vector from a 3D vector and W component.

Template Parameters
TOtherVector3D vector component type.
TWW component type.
Parameters
p_otherSource 3D vector.
p_wW value.

◆ IVector4() [4/5]

template<typename TType>
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
spk::IVector4< TType >::IVector4 ( const IVector4< TOther > & p_other)
inlineexplicitconstexpr

Converting constructor from another component type.

Template Parameters
TOthersource component type; must be constructible into TType.
Parameters
p_otherSource vector to copy and cast from.

◆ IVector4() [5/5]

template<typename TType>
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
spk::IVector4< TType >::IVector4 ( const TOther & p_value)
inlineexplicitconstexpr

Fills all components with the same value.

Parameters
p_valueComponent value to duplicate.

Member Function Documentation

◆ clamp()

template<typename TType>
IVector4 spk::IVector4< TType >::clamp ( const IVector4< TType > & p_value,
const IVector4< TType > & p_boundA,
const IVector4< TType > & p_boundB )
inlinestatic

Clamps each component of a vector between two bounds.

Parameters
p_valueValue to clamp.
p_boundAFirst bound.
p_boundBSecond bound.
Returns
Vector with each component clamped between the provided bounds.
See also
clamped, min, max
Here is the caller graph for this function:

◆ clamped()

template<typename TType>
IVector4 spk::IVector4< TType >::clamped ( const IVector4< TType > & p_boundA,
const IVector4< TType > & p_boundB ) const
inline

Returns a clamped copy of this vector.

Parameters
p_boundAFirst bound.
p_boundBSecond bound.
Returns
Clamped vector.
See also
clamp

◆ distance()

template<typename TType>
float spk::IVector4< TType >::distance ( const IVector4< TType > & p_other) const
inline

Computes Euclidean distance to another vector.

Parameters
p_otherTarget vector.
Returns
Distance between the two points.
See also
squaredDistance

◆ dot()

template<typename TType>
float spk::IVector4< TType >::dot ( const IVector4< TType > & p_other) const
inline

Computes dot product with another vector.

Parameters
p_otherTarget vector.
Returns
Scalar dot product.

◆ inverse()

template<typename TType>
IVector4 spk::IVector4< TType >::inverse ( ) const
inline

Return the inverse vector.

Returns
Vector with all components negated.
Here is the caller graph for this function:

◆ isBetween()

template<typename TType>
bool spk::IVector4< TType >::isBetween ( const IVector4< TType > & p_value,
const IVector4< TType > & p_boundA,
const IVector4< TType > & p_boundB )
inlinestatic

Checks whether a vector lies within inclusive bounds.

Parameters
p_valueValue to test.
p_boundAFirst bound.
p_boundBSecond bound.
Returns
true when all components are between the corresponding bounds.
See also
clamp

◆ isZero()

template<typename TType>
bool spk::IVector4< TType >::isZero ( ) const
inline

Checks if all components are zero.

Returns
true when x, y, z and w are zero.

◆ length()

template<typename TType>
float spk::IVector4< TType >::length ( ) const
inline

Computes Euclidean length.

Returns
Magnitude of the vector.
See also
squaredLength

◆ lerp()

template<typename TType>
template<typename TAlpha>
requires std::is_arithmetic_v<TAlpha>
IVector4 spk::IVector4< TType >::lerp ( const IVector4< TType > & p_from,
const IVector4< TType > & p_to,
const TAlpha & p_alpha )
inlinestatic

Linearly interpolates between two vectors.

Template Parameters
TAlphaArithmetic interpolation type.
Parameters
p_fromStart vector.
p_toEnd vector.
p_alphaInterpolation factor (0 -> start, 1 -> end); not clamped.
Returns
Interpolated vector.
const auto a = spk::Vector4{0.f, 0.f, 0.f, 0.f};
const auto b = spk::Vector4{1.f, 2.f, 3.f, 4.f};
auto mid = spk::Vector4::lerp(a, b, 0.5f); // (0.5, 1, 1.5, 2)
static IVector4 lerp(const IVector4 &p_from, const IVector4 &p_to, const TAlpha &p_alpha)
Definition spk_vector4.hpp:639

◆ max() [1/3]

template<typename TType>
IVector4 spk::IVector4< TType >::max ( const IVector4< TType > & p_a,
const IVector4< TType > & p_b )
inlinestatic

Component-wise maximum of two vectors.

Parameters
p_aFirst vector.
p_bSecond vector.
Returns
Vector with maximum for each component.
See also
min
Here is the caller graph for this function:

◆ max() [2/3]

template<typename TType>
template<typename... TOthers>
requires (sizeof...(TOthers) > 0 && (std::is_same_v<TOthers, IVector4> && ...))
IVector4 spk::IVector4< TType >::max ( const IVector4< TType > & p_a,
const IVector4< TType > & p_b,
const TOthers &... p_rest )
inlinestatic

Component-wise maximum of multiple vectors.

Template Parameters
TOthersAdditional vector types (must be IVector4).
Parameters
p_aFirst vector.
p_bSecond vector.
p_restRemaining vectors.
Returns
Vector with maximum for each component.

◆ max() [3/3]

template<typename TType>
IVector4 spk::IVector4< TType >::max ( std::initializer_list< IVector4< TType > > p_values)
inlinestatic

Component-wise maximum across an initializer list.

Parameters
p_valuesCollection of vectors (must contain at least one element).
Returns
Vector with maximum for each component.
Exceptions
std::invalid_argumentwhen the list is empty.

◆ min() [1/3]

template<typename TType>
IVector4 spk::IVector4< TType >::min ( const IVector4< TType > & p_a,
const IVector4< TType > & p_b )
inlinestatic

Component-wise minimum of two vectors.

Parameters
p_aFirst vector.
p_bSecond vector.
Returns
Vector with minimum for each component.
See also
max
Here is the caller graph for this function:

◆ min() [2/3]

template<typename TType>
template<typename... TOthers>
requires (sizeof...(TOthers) > 0 && (std::is_same_v<TOthers, IVector4> && ...))
IVector4 spk::IVector4< TType >::min ( const IVector4< TType > & p_a,
const IVector4< TType > & p_b,
const TOthers &... p_rest )
inlinestatic

Component-wise minimum of multiple vectors.

Template Parameters
TOthersAdditional vector types (must be IVector4).
Parameters
p_aFirst vector.
p_bSecond vector.
p_restRemaining vectors.
Returns
Vector with minimum for each component.
See also
min(std::initializer_list<IVector4>)

◆ min() [3/3]

template<typename TType>
IVector4 spk::IVector4< TType >::min ( std::initializer_list< IVector4< TType > > p_values)
inlinestatic

Component-wise minimum across an initializer list.

Parameters
p_valuesCollection of vectors (must contain at least one element).
Returns
Vector with minimum for each component.
Exceptions
std::invalid_argumentwhen the list is empty.

◆ normalize()

template<typename TType>
IVector3< float > spk::IVector4< TType >::normalize ( ) const
inline

Return the vector once normalized by its length.

Returns
The normalized vector.
See also
squaredLength

◆ operator IVector4< TOther >()

template<typename TType>
template<typename TOther>
requires std::is_constructible_v<TOther, TType>
spk::IVector4< TType >::operator IVector4< TOther > ( ) const
inlineexplicitconstexpr

Explicitly converts to another IVector4 component type.

Template Parameters
TOtherTarget component type; must be constructible from TType.
Returns
Converted vector.

◆ operator!=()

template<typename TType>
bool spk::IVector4< TType >::operator!= ( const IVector4< TType > & p_other) const
inline

Negation of equality.

Parameters
p_otherVector to compare against.
Returns
true when any component differs.
See also
operator==

◆ operator*() [1/2]

template<typename TType>
IVector4 spk::IVector4< TType >::operator* ( const IVector4< TType > & p_other) const
inline

Component-wise multiplication.

Parameters
p_otherVector to multiply.
Returns
Product of both vectors.

◆ operator*() [2/2]

template<typename TType>
IVector4 spk::IVector4< TType >::operator* ( const TType & p_scalar) const
inline

Multiplies each component by a scalar.

Parameters
p_scalarScalar factor.
Returns
Scaled vector.

◆ operator*=() [1/2]

template<typename TType>
IVector4 & spk::IVector4< TType >::operator*= ( const IVector4< TType > & p_other)
inline

In-place component-wise multiplication.

Parameters
p_otherVector multiplier.
Returns
Reference to this vector after the operation.
See also
operator*

◆ operator*=() [2/2]

template<typename TType>
IVector4 & spk::IVector4< TType >::operator*= ( const TType & p_scalar)
inline

In-place scalar multiplication.

Parameters
p_scalarScalar factor.
Returns
Reference to this vector after the operation.
See also
operator*(const TType&) const

◆ operator+() [1/2]

template<typename TType>
IVector4 spk::IVector4< TType >::operator+ ( const IVector4< TType > & p_other) const
inline

Component-wise addition.

Parameters
p_otherVector to add.
Returns
Sum of both vectors.

◆ operator+() [2/2]

template<typename TType>
IVector4 spk::IVector4< TType >::operator+ ( const TType & p_scalar) const
inline

Adds a scalar to each component.

Parameters
p_scalarScalar value to add.
Returns
Offset vector.

◆ operator+=() [1/2]

template<typename TType>
IVector4 & spk::IVector4< TType >::operator+= ( const IVector4< TType > & p_other)
inline

In-place component-wise addition.

Parameters
p_otherVector to add.
Returns
Reference to this vector after the operation.
See also
operator+

◆ operator+=() [2/2]

template<typename TType>
IVector4 & spk::IVector4< TType >::operator+= ( const TType & p_scalar)
inline

In-place scalar addition.

Parameters
p_scalarScalar to add.
Returns
Reference to this vector after the operation.
See also
operator+(const TType&) const

◆ operator-() [1/3]

template<typename TType>
IVector4 spk::IVector4< TType >::operator- ( ) const
inline

Unary minus.

Returns
Vector with all components negated.

◆ operator-() [2/3]

template<typename TType>
IVector4 spk::IVector4< TType >::operator- ( const IVector4< TType > & p_other) const
inline

Component-wise subtraction.

Parameters
p_otherVector to subtract.
Returns
Difference of both vectors.

◆ operator-() [3/3]

template<typename TType>
IVector4 spk::IVector4< TType >::operator- ( const TType & p_scalar) const
inline

Subtracts a scalar from each component.

Parameters
p_scalarScalar value to subtract.
Returns
Offset vector.

◆ operator-=() [1/2]

template<typename TType>
IVector4 & spk::IVector4< TType >::operator-= ( const IVector4< TType > & p_other)
inline

In-place component-wise subtraction.

Parameters
p_otherVector to subtract.
Returns
Reference to this vector after the operation.
See also
operator-

◆ operator-=() [2/2]

template<typename TType>
IVector4 & spk::IVector4< TType >::operator-= ( const TType & p_scalar)
inline

In-place scalar subtraction.

Parameters
p_scalarScalar to subtract.
Returns
Reference to this vector after the operation.
See also
operator-(const TType&) const

◆ operator/() [1/2]

template<typename TType>
IVector4 spk::IVector4< TType >::operator/ ( const IVector4< TType > & p_other) const
inline

Component-wise division.

Parameters
p_otherVector to divide by.
Returns
Quotient of both vectors.
Exceptions
std::invalid_argumentwhen any divisor component is zero.

◆ operator/() [2/2]

template<typename TType>
IVector4 spk::IVector4< TType >::operator/ ( const TType & p_scalar) const
inline

Divides each component by a scalar.

Parameters
p_scalarScalar divisor.
Returns
Scaled vector.
Exceptions
std::invalid_argumentwhen divisor is zero.

◆ operator/=() [1/2]

template<typename TType>
IVector4 & spk::IVector4< TType >::operator/= ( const IVector4< TType > & p_other)
inline

In-place component-wise division.

Parameters
p_otherVector divisor.
Returns
Reference to this vector after the operation.
Exceptions
std::invalid_argumentwhen any divisor component is zero.
See also
operator/

◆ operator/=() [2/2]

template<typename TType>
IVector4 & spk::IVector4< TType >::operator/= ( const TType & p_scalar)
inline

In-place scalar division.

Parameters
p_scalarScalar divisor.
Returns
Reference to this vector after the operation.
Exceptions
std::invalid_argumentwhen divisor is zero.
See also
operator/(const TType&) const

◆ operator==()

template<typename TType>
bool spk::IVector4< TType >::operator== ( const IVector4< TType > & p_other) const
inline

Checks component-wise equality using SafeComparand.

Parameters
p_otherVector to compare against.
Returns
true if all components are equal.

◆ squaredDistance()

template<typename TType>
float spk::IVector4< TType >::squaredDistance ( const IVector4< TType > & p_other) const
inline

Computes squared distance to another vector.

Parameters
p_otherTarget vector.
Returns
Squared distance.
See also
distance

◆ squaredLength()

template<typename TType>
float spk::IVector4< TType >::squaredLength ( ) const
inline

Computes squared vector length.

Returns
Sum of squared components.
See also
length
Here is the caller graph for this function:

◆ toString()

template<typename TType>
std::string spk::IVector4< TType >::toString ( ) const
inline

Converts the vector to a string.

Returns
Text form "(x, y, z, w)".

◆ toWstring()

template<typename TType>
std::wstring spk::IVector4< TType >::toWstring ( ) const
inline

Converts the vector to a wide string.

Returns
Text form "(x, y, z, w)" using wide characters.

◆ xyz()

template<typename TType>
IVector3< TType > spk::IVector4< TType >::xyz ( ) const
inline

Projects this vector into 3D using x, y, z components.

Returns
3D vector copy.
Here is the caller graph for this function:

◆ operator*

template<typename TType>
IVector4 operator* ( const TType & p_scalar,
const IVector4< TType > & p_vector )
friend

Multiplies a scalar by a vector (scalar on the left side).

Parameters
p_scalarScalar factor.
p_vectorVector value.
Returns
Scaled vector.
See also
operator*(const TType&) const

◆ operator+

template<typename TType>
IVector4 operator+ ( const TType & p_scalar,
const IVector4< TType > & p_vector )
friend

Adds a scalar to a vector (scalar on the left side).

Parameters
p_scalarScalar value.
p_vectorVector value.
Returns
Sum of scalar and vector.
See also
operator+(const TType&) const

◆ operator-

template<typename TType>
IVector4 operator- ( const TType & p_scalar,
const IVector4< TType > & p_vector )
friend

Subtracts vector components from a scalar (scalar on the left side).

Parameters
p_scalarScalar value.
p_vectorVector value.
Returns
Component-wise (scalar - vector).

◆ operator/

template<typename TType>
IVector4 operator/ ( const TType & p_scalar,
const IVector4< TType > & p_vector )
friend

Divides a scalar by each vector component (scalar on the left side).

Parameters
p_scalarScalar numerator.
p_vectorVector divisor.
Returns
Component-wise division.
Exceptions
std::invalid_argumentwhen any divisor component is zero.

◆ operator<< [1/2]

template<typename TType>
std::ostream & operator<< ( std::ostream & p_stream,
const IVector4< TType > & p_value )
friend

Streams the vector to an output stream.

Parameters
p_streamDestination stream.
p_valueVector to print.
Returns
Reference to the destination stream.

◆ operator<< [2/2]

template<typename TType>
std::wostream & operator<< ( std::wostream & p_stream,
const IVector4< TType > & p_value )
friend

Streams the vector to a wide output stream.

Parameters
p_streamDestination stream.
p_valueVector to print.
Returns
Reference to the destination stream.

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