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

3D value type with common arithmetic helpers. More...

#include <spk_vector3.hpp>

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

Public Types

using value_type = TType
 Component type stored by the vector.

Public Member Functions

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

Static Public Member Functions

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

Public Attributes

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

Static Public Attributes

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

Friends

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

Detailed Description

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

3D value type with common arithmetic helpers.

Template Parameters
TTypearithmetic component type; must be constructible from input values and support +, -, * and / and self assignation operator, in addition to comparisons.
See also
Vector3, Vector3Int, Vector3UInt, IVector2
spk::Vector3 position{1.0f, 2.0f, 3.0f};
spk::Vector3 offset{0.5f, 0.0f, -1.0f};
const auto moved = position + offset;
const auto len = moved.length();
float length() const
Computes Euclidean length.
Definition spk_vector3.hpp:511

Constructor & Destructor Documentation

◆ IVector3() [1/4]

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

Builds a vector from three components.

Parameters
p_xX value.
p_yY value.
p_zZ value.

◆ IVector3() [2/4]

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

Builds a vector from a 2D vector and Z component.

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

◆ IVector3() [3/4]

template<typename TType>
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
spk::IVector3< TType >::IVector3 ( const IVector3< 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.

◆ IVector3() [4/4]

template<typename TType>
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
spk::IVector3< TType >::IVector3 ( 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>
IVector3 spk::IVector3< TType >::clamp ( const IVector3< TType > & p_value,
const IVector3< TType > & p_boundA,
const IVector3< 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>
IVector3 spk::IVector3< TType >::clamped ( const IVector3< TType > & p_boundA,
const IVector3< 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

◆ cross()

template<typename TType>
IVector3 spk::IVector3< TType >::cross ( const IVector3< TType > & p_other) const
inline

Computes 3D cross product.

Parameters
p_otherTarget vector.
Returns
Cross product vector.
See also
dot
Here is the caller graph for this function:

◆ distance()

template<typename TType>
float spk::IVector3< TType >::distance ( const IVector3< 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::IVector3< TType >::dot ( const IVector3< TType > & p_other) const
inline

Computes dot product with another vector.

Parameters
p_otherTarget vector.
Returns
Scalar dot product.
See also
cross
Here is the caller graph for this function:

◆ inverse()

template<typename TType>
IVector3 spk::IVector3< 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::IVector3< TType >::isBetween ( const IVector3< TType > & p_value,
const IVector3< TType > & p_boundA,
const IVector3< 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::IVector3< TType >::isZero ( ) const
inline

Checks if all components are zero.

Returns
true when x, y and z are zero.

◆ length()

template<typename TType>
float spk::IVector3< 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>
IVector3 spk::IVector3< TType >::lerp ( const IVector3< TType > & p_from,
const IVector3< 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.
See also
dot, cross
const auto a = spk::Vector3{0.f, 0.f, 0.f};
const auto b = spk::Vector3{1.f, 2.f, 3.f};
auto mid = spk::Vector3::lerp(a, b, 0.5f); // (0.5, 1, 1.5)
static IVector3 lerp(const IVector3 &p_from, const IVector3 &p_to, const TAlpha &p_alpha)
Definition spk_vector3.hpp:626

◆ max() [1/3]

template<typename TType>
IVector3 spk::IVector3< TType >::max ( const IVector3< TType > & p_a,
const IVector3< 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, IVector3> && ...))
IVector3 spk::IVector3< TType >::max ( const IVector3< TType > & p_a,
const IVector3< TType > & p_b,
const TOthers &... p_rest )
inlinestatic

Component-wise maximum of multiple vectors.

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

◆ max() [3/3]

template<typename TType>
IVector3 spk::IVector3< TType >::max ( std::initializer_list< IVector3< 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>
IVector3 spk::IVector3< TType >::min ( const IVector3< TType > & p_a,
const IVector3< 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, IVector3> && ...))
IVector3 spk::IVector3< TType >::min ( const IVector3< TType > & p_a,
const IVector3< TType > & p_b,
const TOthers &... p_rest )
inlinestatic

Component-wise minimum of multiple vectors.

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

◆ min() [3/3]

template<typename TType>
IVector3 spk::IVector3< TType >::min ( std::initializer_list< IVector3< 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::IVector3< TType >::normalize ( ) const
inline

Return the vector once normalized by its length.

Returns
The normalized vector.
See also
squaredLength
Here is the caller graph for this function:

◆ operator IVector3< TOther >()

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

Explicitly converts to another IVector3 component type.

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

◆ operator!=()

template<typename TType>
bool spk::IVector3< TType >::operator!= ( const IVector3< 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>
IVector3 spk::IVector3< TType >::operator* ( const IVector3< TType > & p_other) const
inline

Component-wise multiplication.

Parameters
p_otherVector to multiply.
Returns
Product of both vectors.

◆ operator*() [2/2]

template<typename TType>
IVector3 spk::IVector3< 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>
IVector3 & spk::IVector3< TType >::operator*= ( const IVector3< 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>
IVector3 & spk::IVector3< 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>
IVector3 spk::IVector3< TType >::operator+ ( const IVector3< TType > & p_other) const
inline

Component-wise addition.

Parameters
p_otherVector to add.
Returns
Sum of both vectors.

◆ operator+() [2/2]

template<typename TType>
IVector3 spk::IVector3< 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>
IVector3 & spk::IVector3< TType >::operator+= ( const IVector3< 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>
IVector3 & spk::IVector3< 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>
IVector3 spk::IVector3< TType >::operator- ( ) const
inline

Unary minus.

Returns
Vector with all components negated.

◆ operator-() [2/3]

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

Component-wise subtraction.

Parameters
p_otherVector to subtract.
Returns
Difference of both vectors.

◆ operator-() [3/3]

template<typename TType>
IVector3 spk::IVector3< 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>
IVector3 & spk::IVector3< TType >::operator-= ( const IVector3< 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>
IVector3 & spk::IVector3< 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>
IVector3 spk::IVector3< TType >::operator/ ( const IVector3< 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>
IVector3 spk::IVector3< 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>
IVector3 & spk::IVector3< TType >::operator/= ( const IVector3< 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>
IVector3 & spk::IVector3< 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::IVector3< TType >::operator== ( const IVector3< 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::IVector3< TType >::squaredDistance ( const IVector3< 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::IVector3< 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::IVector3< TType >::toString ( ) const
inline

Converts the vector to a string.

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

◆ toWstring()

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

Converts the vector to a wide string.

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

◆ xy()

template<typename TType>
IVector2< TType > spk::IVector3< TType >::xy ( ) const
inline

Projects this vector into 2D using x and y components.

Returns
2D vector copy.

◆ yz()

template<typename TType>
IVector2< TType > spk::IVector3< TType >::yz ( ) const
inline

Projects this vector into 2D using y and z components.

Returns
2D vector copy.

◆ operator*

template<typename TType>
IVector3 operator* ( const TType & p_scalar,
const IVector3< 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>
IVector3 operator+ ( const TType & p_scalar,
const IVector3< 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>
IVector3 operator- ( const TType & p_scalar,
const IVector3< 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>
IVector3 operator/ ( const TType & p_scalar,
const IVector3< 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 IVector3< 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 IVector3< 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: