Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Infinity::Types::Math::t_Vector3< T > Struct Template Reference

Template structure representing a 3-component vector. More...

#include <Vector3.hpp>

Public Member Functions

constexpr t_Vector3 ()
 Default constructor.
 
constexpr t_Vector3 (const t_Vector3 &v)
 Copy constructor.
 
constexpr t_Vector3operator= (const t_Vector3 &)=default
 Copy assignment operator.
 
constexpr t_Vector3 (T x, T y, T z)
 Constructs a vector from component values.
 
template<typename R >
constexpr t_Vector3 (const t_Vector3< R > &v)
 Explicit type-converting constructor.
 
Toperator[] (size_t i)
 Array subscript operator (mutable).
 
T operator[] (size_t i) const
 Array subscript operator (const).
 
void set (T in_x, T in_y, T in_z)
 Sets all three components simultaneously.
 
Tdata ()
 Gets a mutable pointer to the underlying data.
 
const Tdata () const
 Gets a const pointer to the underlying data.
 
template<typename R >
t_Vector3operator= (const t_Vector3< R > &rhs)
 Type-converting assignment operator.
 
t_Vector3operator+= (const t_Vector3 &rhs)
 Component-wise addition assignment.
 
t_Vector3operator-= (const t_Vector3 &rhs)
 Component-wise subtraction assignment.
 
t_Vector3operator*= (T rhs)
 Scalar multiplication assignment.
 
t_Vector3operator*= (const t_Vector3 &rhs)
 Component-wise multiplication assignment.
 
t_Vector3operator/= (T rhs)
 Scalar division assignment.
 
 operator bool () const noexcept
 Boolean conversion operator.
 
t_Vector3< Tnormalized () const
 Computes the normalized version of this vector.
 
t_Vector3< Tcross (const t_Vector3< T > &other) const
 Computes the cross product with another vector.
 
float length () const
 Computes the length (magnitude) of this vector.
 
float magnitude () const
 Computes the magnitude of this vector.
 
float squaredMagnitude () const
 Computes the squared magnitude of this vector.
 
float distance (const t_Vector3< T > &other) const
 Computes the Euclidean distance to another vector.
 
float dot (const t_Vector3< T > &other) const
 Computes the dot product with another vector.
 
float angle (const t_Vector3< T > &other) const
 Computes the angle between this vector and another.
 
t_Vector3< Tabs () const
 Computes the component-wise absolute value.
 

Static Public Member Functions

static constexpr t_Vector3< Tzero ()
 Returns a zero vector (0, 0, 0).
 
static constexpr t_Vector3< Tidentity ()
 Returns an identity/zero vector (0, 0, 0).
 
static constexpr t_Vector3< Tleft ()
 Returns the left direction vector (-1, 0, 0).
 
static constexpr t_Vector3< Tright ()
 Returns the right direction vector (1, 0, 0).
 
static constexpr t_Vector3< Tup ()
 Returns the up direction vector (0, 1, 0).
 
static constexpr t_Vector3< Tdown ()
 Returns the down direction vector (0, -1, 0).
 
static constexpr t_Vector3< Tfront ()
 Returns the front/forward direction vector (0, 0, 1).
 
static constexpr t_Vector3< Tback ()
 Returns the back/backward direction vector (0, 0, -1).
 

Public Attributes

union { 
 
   T   value [3] 
 Array access to components. More...
 
   struct { 
 
      T   x 
 
      T   y 
 
      T   z 
 Cartesian coordinate interpretation. More...
 
   }  
 
   struct { 
 
      T   r 
 
      T   g 
 
      T   b 
 Color component interpretation. More...
 
   }  
 
   struct { 
 
      T   s 
 
      T   t 
 
      T   p 
 Texture coordinate interpretation. More...
 
   }  
 
};  
 Union providing multiple interpretations of the same data.
 

Detailed Description

template<typename T>
struct Infinity::Types::Math::t_Vector3< T >

Template structure representing a 3-component vector.

t_Vector3 provides a versatile 3D vector implementation with support for multiple interpretations of the same underlying data through union members:

  • Cartesian coordinates (x, y, z)
  • Color components (r, g, b)
  • Texture coordinates (s, t, p)
  • Array indexing (value[0], value[1], value[2])

This flexibility makes it suitable for various use cases in procedural generation:

  • 3D positions, directions, and offsets
  • 3D normals for lighting calculations
  • RGB color data
  • 3D velocity and acceleration vectors
  • Rotation axes
  • Scale factors
  • Procedural noise coordinates

The template parameter allows instantiation with different numeric types (float, double, int, etc.) to match precision and performance requirements. Common specializations are provided via type aliases (Vector3, Vector3i, etc.).

Vector operations include:

  • Arithmetic operations (+, -, *, /)
  • Vector math (dot product, cross product, normalization)
  • Magnitude and distance calculations
  • Angle computations
  • Component-wise operations
  • Static direction constants (up, right, forward, etc.)

Example usage:

// Create vectors
Vector3 position(10.0f, 20.0f, 5.0f);
Vector3 velocity(-5.0f, 3.0f, 0.0f);
// Arithmetic operations
Vector3 scaled = velocity * 2.0f;
// Vector operations
float dist = position.distance(newPosition);
float dotProd = position.dot(velocity);
// Static direction vectors
// Access via different names
float x = position.x;
float red = position.r; // Same as x, color interpretation
// Array access
for (size_t i = 0; i < 3; ++i) {
position[i] *= 2.0f;
}
Template structure representing a 3-component vector.
Definition Vector3.hpp:82
static constexpr t_Vector3< T > right()
Returns the right direction vector (1, 0, 0).
Definition Vector3.hpp:545
T x
Definition Vector3.hpp:97
t_Vector3< T > cross(const t_Vector3< T > &other) const
Computes the cross product with another vector.
Definition Vector3.hpp:370
t_Vector3< T > normalized() const
Computes the normalized version of this vector.
Definition Vector3.hpp:344
float distance(const t_Vector3< T > &other) const
Computes the Euclidean distance to another vector.
Definition Vector3.hpp:438
static constexpr t_Vector3< T > up()
Returns the up direction vector (0, 1, 0).
Definition Vector3.hpp:552
Template Parameters
TThe numeric type for vector components (float, double, int, etc.).
See also
Vector3, Vector3f, Vector3d, Vector3i
t_Vector2, t_Vector4

Constructor & Destructor Documentation

◆ t_Vector3() [1/4]

template<typename T >
constexpr Infinity::Types::Math::t_Vector3< T >::t_Vector3 ( )
inlineconstexpr

Default constructor.

Initializes all components to zero (or default value of T).

◆ t_Vector3() [2/4]

template<typename T >
constexpr Infinity::Types::Math::t_Vector3< T >::t_Vector3 ( const t_Vector3< T > &  v)
inlineconstexpr

Copy constructor.

Parameters
vVector to copy from.

◆ t_Vector3() [3/4]

template<typename T >
constexpr Infinity::Types::Math::t_Vector3< T >::t_Vector3 ( T  x,
T  y,
T  z 
)
inlineconstexpr

Constructs a vector from component values.

Parameters
xThe x component (or r, or s).
yThe y component (or g, or t).
zThe z component (or b, or p).
Vector3 pos(10.0f, 20.0f, 5.0f);
Vector3i voxel(100, 200, 50);
Vector3 color(1.0f, 0.5f, 0.0f); // RGB: orange

◆ t_Vector3() [4/4]

template<typename T >
template<typename R >
constexpr Infinity::Types::Math::t_Vector3< T >::t_Vector3 ( const t_Vector3< R > &  v)
inlineexplicitconstexpr

Explicit type-converting constructor.

Constructs a vector from another vector with a different component type, performing static_cast on each component.

Template Parameters
RThe source component type.
Parameters
vVector to convert from.
Vector3i intVec(10, 20, 30);
Vector3 floatVec(intVec); // Converts int to float

Member Function Documentation

◆ abs()

template<typename T >
t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::abs ( ) const
inline

Computes the component-wise absolute value.

Returns a vector where each component is the absolute value of the corresponding component in this vector.

Returns
Vector with absolute values of components.
Note
For unsigned types, returns a copy of the vector unchanged.
Vector3 v(-3.0f, -4.0f, 5.0f);
Vector3 absV = v.abs(); // Returns (3.0f, 4.0f, 5.0f)
t_Vector3< T > abs() const
Computes the component-wise absolute value.
Definition Vector3.hpp:508

◆ angle()

template<typename T >
float Infinity::Types::Math::t_Vector3< T >::angle ( const t_Vector3< T > &  other) const
inline

Computes the angle between this vector and another.

Returns the angle in radians between the two vectors.

Parameters
otherThe other vector.
Returns
Angle in radians (0 to π).
Note
Returns 0 if either vector has zero length.
Vector3 a(1, 0, 0);
Vector3 b(0, 1, 0);
float angle = a.angle(b); // Returns π/2 (90 degrees)
float angle(const t_Vector3< T > &other) const
Computes the angle between this vector and another.
Definition Vector3.hpp:485
T b
Color component interpretation.
Definition Vector3.hpp:101

◆ back()

template<typename T >
static constexpr t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::back ( )
inlinestaticconstexpr

Returns the back/backward direction vector (0, 0, -1).

Returns
Unit vector pointing backward along the negative Z axis.

◆ cross()

template<typename T >
t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::cross ( const t_Vector3< T > &  other) const
inline

Computes the cross product with another vector.

The cross product produces a vector perpendicular to both input vectors. The magnitude equals the area of the parallelogram formed by the vectors. The direction follows the right-hand rule.

Parameters
otherThe other vector.
Returns
Cross product vector perpendicular to both inputs.
Vector3 x_axis(1, 0, 0);
Vector3 y_axis(0, 1, 0);
Vector3 z_axis = x_axis.cross(y_axis); // (0, 0, 1)
// Compute surface normal from two edges
Vector3 normal = edge1.cross(edge2).normalized();

◆ data() [1/2]

template<typename T >
T * Infinity::Types::Math::t_Vector3< T >::data ( )
inline

Gets a mutable pointer to the underlying data.

Returns
Pointer to the first component.
Vector3 v(1, 2, 3);
float* ptr = v.data();
ptr[0] = 5.0f; // Modifies x

◆ data() [2/2]

template<typename T >
const T * Infinity::Types::Math::t_Vector3< T >::data ( ) const
inline

Gets a const pointer to the underlying data.

Returns
Const pointer to the first component.

◆ distance()

template<typename T >
float Infinity::Types::Math::t_Vector3< T >::distance ( const t_Vector3< T > &  other) const
inline

Computes the Euclidean distance to another vector.

Parameters
otherThe other vector.
Returns
The distance between this vector and other.
Vector3 a(0, 0, 0);
Vector3 b(3, 4, 0);
float dist = a.distance(b); // Returns 5.0f

◆ dot()

template<typename T >
float Infinity::Types::Math::t_Vector3< T >::dot ( const t_Vector3< T > &  other) const
inline

Computes the dot product with another vector.

The dot product measures how much two vectors point in the same direction. Returns positive if vectors point in similar directions, negative if opposite, and zero if perpendicular.

Parameters
otherThe other vector.
Returns
The dot product (x₁·x₂ + y₁·y₂ + z₁·z₂).
Vector3 forward(0, 0, 1);
Vector3 right(1, 0, 0);
float dot = forward.dot(right); // Returns 0 (perpendicular)
// Check if vectors point in same direction
if (v1.normalized().dot(v2.normalized()) > 0.9f) {
// Vectors are nearly parallel
}
float dot(const t_Vector3< T > &other) const
Computes the dot product with another vector.
Definition Vector3.hpp:464

◆ down()

template<typename T >
static constexpr t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::down ( )
inlinestaticconstexpr

Returns the down direction vector (0, -1, 0).

Returns
Unit vector pointing down along the negative Y axis.

◆ front()

template<typename T >
static constexpr t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::front ( )
inlinestaticconstexpr

Returns the front/forward direction vector (0, 0, 1).

Returns
Unit vector pointing forward along the positive Z axis.

◆ identity()

template<typename T >
static constexpr t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::identity ( )
inlinestaticconstexpr

Returns an identity/zero vector (0, 0, 0).

Alias for zero().

Returns
Vector with all components set to zero.

◆ left()

template<typename T >
static constexpr t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::left ( )
inlinestaticconstexpr

Returns the left direction vector (-1, 0, 0).

Returns
Unit vector pointing left along the negative X axis.

◆ length()

template<typename T >
float Infinity::Types::Math::t_Vector3< T >::length ( ) const
inline

Computes the length (magnitude) of this vector.

Returns
The Euclidean length of the vector.
Vector3 v(3.0f, 4.0f, 0.0f);
float len = v.length(); // Returns 5.0f

◆ magnitude()

template<typename T >
float Infinity::Types::Math::t_Vector3< T >::magnitude ( ) const
inline

Computes the magnitude of this vector.

Alias for length().

Returns
The Euclidean magnitude of the vector.

◆ normalized()

template<typename T >
t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::normalized ( ) const
inline

Computes the normalized version of this vector.

Returns a unit vector pointing in the same direction. If the vector is zero-length, returns a zero vector.

Returns
Normalized vector (length 1) or zero vector if original is zero.
Vector3 v(3.0f, 4.0f, 0.0f); // Length is 5
Vector3 unit = v.normalized(); // (0.6, 0.8, 0.0)

◆ operator bool()

template<typename T >
Infinity::Types::Math::t_Vector3< T >::operator bool ( ) const
inlineexplicitnoexcept

Boolean conversion operator.

Returns true if the vector is non-zero (at least one component is non-zero).

Returns
true if vector is non-zero, false if zero vector.
Vector3 v(0, 0, 0);
if (v) {
// This won't execute
}

◆ operator*=() [1/2]

Component-wise multiplication assignment.

Parameters
rhsVector to multiply by component-wise.
Returns
Reference to this vector.

◆ operator*=() [2/2]

Scalar multiplication assignment.

Parameters
rhsScalar to multiply by.
Returns
Reference to this vector.

◆ operator+=()

Component-wise addition assignment.

Parameters
rhsVector to add.
Returns
Reference to this vector.

◆ operator-=()

Component-wise subtraction assignment.

Parameters
rhsVector to subtract.
Returns
Reference to this vector.

◆ operator/=()

Scalar division assignment.

For floating-point types, multiplies by reciprocal for better performance. For integer types, performs standard division.

Parameters
rhsScalar to divide by.
Returns
Reference to this vector.
Warning
Division by zero is undefined behavior.

◆ operator=() [1/2]

Copy assignment operator.

◆ operator=() [2/2]

template<typename T >
template<typename R >
t_Vector3 & Infinity::Types::Math::t_Vector3< T >::operator= ( const t_Vector3< R > &  rhs)
inline

Type-converting assignment operator.

Assigns from a vector with a different component type, performing static_cast on each component.

Template Parameters
RThe source component type.
Parameters
rhsVector to assign from.
Returns
Reference to this vector.

◆ operator[]() [1/2]

Array subscript operator (mutable).

Parameters
iComponent index (0, 1, or 2).
Returns
Reference to the component at index i.

◆ operator[]() [2/2]

template<typename T >
T Infinity::Types::Math::t_Vector3< T >::operator[] ( size_t  i) const
inline

Array subscript operator (const).

Parameters
iComponent index (0, 1, or 2).
Returns
Value of the component at index i.

◆ right()

template<typename T >
static constexpr t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::right ( )
inlinestaticconstexpr

Returns the right direction vector (1, 0, 0).

Returns
Unit vector pointing right along the positive X axis.

◆ set()

template<typename T >
void Infinity::Types::Math::t_Vector3< T >::set ( T  in_x,
T  in_y,
T  in_z 
)
inline

Sets all three components simultaneously.

Parameters
in_xNew x component value.
in_yNew y component value.
in_zNew z component value.

◆ squaredMagnitude()

template<typename T >
float Infinity::Types::Math::t_Vector3< T >::squaredMagnitude ( ) const
inline

Computes the squared magnitude of this vector.

More efficient than length() when you only need to compare lengths or don't need the actual distance value, as it avoids the sqrt operation.

Returns
The squared length (x² + y² + z²).
// Efficient distance comparison without sqrt
if (v1.squaredMagnitude() < v2.squaredMagnitude()) {
// v1 is shorter than v2
}

◆ up()

template<typename T >
static constexpr t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::up ( )
inlinestaticconstexpr

Returns the up direction vector (0, 1, 0).

Returns
Unit vector pointing up along the positive Y axis.

◆ zero()

template<typename T >
static constexpr t_Vector3< T > Infinity::Types::Math::t_Vector3< T >::zero ( )
inlinestaticconstexpr

Returns a zero vector (0, 0, 0).

Returns
Vector with all components set to zero.

Member Data Documentation

◆ [union]

Union providing multiple interpretations of the same data.

Allows accessing the three components as:

  • value[3]: Array access
  • x, y, z: Cartesian coordinates
  • r, g, b: Color components (red, green, blue)
  • s, t, p: Texture coordinates (3D texture sampling)

◆ b

Color component interpretation.

◆ g

◆ p

Texture coordinate interpretation.

◆ r

◆ s

◆ t

◆ value

template<typename T >
T Infinity::Types::Math::t_Vector3< T >::value[3]

Array access to components.

◆ x

template<typename T >
T Infinity::Types::Math::t_Vector3< T >::x

◆ y

◆ z

Cartesian coordinate interpretation.