MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk::vec4D Class Reference

Four-dimensional float vector used for homogeneous 3D coordinates. More...

#include <mxvk/include/mxvk/mxvk_math.h>

Public Member Functions

vec4D Build (const vec4D &from, const vec4D &to) const
 Build a direction vector from from to to with W set to 1.
vec4D Build (const vec4D &from, const vec4D &to) const
 Build a direction vector from from to to with W set to 0.
void Build (const vec4D &to)
 Replace this vector with the direction from this point to to.
void Build (const vec4D &to)
 Replace this vector with the direction from this point to to.
float Cos (const vec4D &v) const
 Compute the cosine of the angle between the 3D components of two vectors.
float Cos (const vec4D &v) const
 Compute the cosine of the angle between the 3D components of two vectors.
constexpr vec4D CrossProduct (const vec4D &v) const
 Compute the 3D cross product and return it with W set to 1.
vec4D CrossProduct (const vec4D &v) const
 Compute the 3D cross product and return it as a direction with W set to 0.
constexpr float DotProduct (const vec4D &v) const
 Compute the 3D dot product, ignoring the W component.
float DotProduct (const vec4D &v) const
 Compute the 3D dot product, ignoring the W component.
float Length () const
 Compute the 3D Euclidean length, ignoring the W component.
float Length () const
 Compute the 3D Euclidean length, ignoring the W component.
void Normalize ()
 Normalize the 3D components in place and reset W to 1.
void Normalize ()
 Normalize the 3D components in place while preserving W.
void Normalize (vec4D &v) const
 Write a normalized copy of this vector to v.
void Normalize (vec4D &v) const
 Write a normalized copy of this vector to v.
constexpr vec4D operator* (const vec4D &v) const
 Multiply two vectors component-wise.
vec4D operator* (const vec4D &v) const
 Multiply two vectors component-wise.
constexpr vec4D operator* (float k) const
 Scale this vector by a scalar.
vec4D operator* (float k) const
 Scale this vector by a scalar.
constexpr vec4D operator+ (const vec4D &v) const
 Add two vectors component-wise.
vec4D operator+ (const vec4D &v) const
 Add two vectors component-wise.
vec4D & operator+= (const vec4D &v)
 Add another vector to this vector.
vec4D & operator+= (const vec4D &v)
 Add another vector to this vector.
constexpr vec4D operator- (const vec4D &v) const
 Subtract two vectors component-wise.
vec4D operator- (const vec4D &v) const
 Subtract two vectors component-wise.
vec4D & operator-= (const vec4D &v)
 Subtract another vector from this vector.
vec4D & operator-= (const vec4D &v)
 Subtract another vector from this vector.
vec4D & operator= (const vec4D &)=default
vec4D & operator= (const vec4D &)=default
std::string Print (const std::string &name="v") const
 Format this vector as a named angle-bracket tuple.
std::string Print (const std::string &name="v") const
 Format this vector as a named angle-bracket tuple.
vec4D Scale (float k) const
 Return a scaled copy of this vector.
vec4D Scale (float k) const
 Return a scaled copy of this vector.
void ScaleThis (float k)
 Scale this vector in place.
void ScaleThis (float k)
 Scale this vector in place.
void Set (const vec4D &v)
 Copy coordinates from another vector.
void Set (const vec4D &v)
 Copy coordinates from another vector.
void Set (float x_value, float y_value, float z_value, float w_value=1.0f)
 Set all vector coordinates.
void Set (float x_value, float y_value, float z_value, float w_value=1.0f)
 Set all vector coordinates.
constexpr vec4D ()
 Construct the homogeneous origin.
constexpr vec4D ()
 Construct the homogeneous origin.
constexpr vec4D (float x_value, float y_value, float z_value, float w_value=1.0f)
 Construct a homogeneous vector from explicit coordinates.
constexpr vec4D (float x_value, float y_value, float z_value, float w_value=1.0f)
 Construct a homogeneous vector from explicit coordinates.

Public Attributes

float w = 1.0f
 Homogeneous W coordinate.
float x = 0.0f
 X coordinate.
float y = 0.0f
 Y coordinate.
float z = 0.0f
 Z coordinate.

Detailed Description

Four-dimensional float vector used for homogeneous 3D coordinates.

Definition at line 363 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ vec4D() [1/4]

mxvk::vec4D::vec4D ( )
inlineconstexpr

Construct the homogeneous origin.

Definition at line 378 of file mxvk_math.h.

378: x(0.0f), y(0.0f), z(0.0f), w(1.0f) {}
float y
Y coordinate.
Definition mxvk_math.h:369
float x
X coordinate.
Definition mxvk_math.h:366
float w
Homogeneous W coordinate.
Definition mxvk_math.h:375
float z
Z coordinate.
Definition mxvk_math.h:372

◆ vec4D() [2/4]

mxvk::vec4D::vec4D ( float x_value,
float y_value,
float z_value,
float w_value = 1.0f )
inlineconstexpr

Construct a homogeneous vector from explicit coordinates.

Definition at line 381 of file mxvk_math.h.

381: x(x_value), y(y_value), z(z_value), w(w_value) {}

◆ vec4D() [3/4]

mxvk::vec4D::vec4D ( )
inlineconstexpr

Construct the homogeneous origin.

Definition at line 394 of file mxvk_math_eigen.hpp.

394: x(0.0f), y(0.0f), z(0.0f), w(1.0f) {}

◆ vec4D() [4/4]

mxvk::vec4D::vec4D ( float x_value,
float y_value,
float z_value,
float w_value = 1.0f )
inlineconstexpr

Construct a homogeneous vector from explicit coordinates.

Definition at line 397 of file mxvk_math_eigen.hpp.

397: x(x_value), y(y_value), z(z_value), w(w_value) {}

Member Function Documentation

◆ Build() [1/4]

vec4D mxvk::vec4D::Build ( const vec4D & from,
const vec4D & to ) const
inlinenodiscard

Build a direction vector from from to to with W set to 1.

Definition at line 476 of file mxvk_math.h.

476{ return {to.x - from.x, to.y - from.y, to.z - from.z, 1.0f}; }

◆ Build() [2/4]

vec4D mxvk::vec4D::Build ( const vec4D & from,
const vec4D & to ) const
inlinenodiscard

Build a direction vector from from to to with W set to 0.

Definition at line 497 of file mxvk_math_eigen.hpp.

497{ return {to.x - from.x, to.y - from.y, to.z - from.z, 0.0f}; }

◆ Build() [3/4]

void mxvk::vec4D::Build ( const vec4D & to)
inline

Replace this vector with the direction from this point to to.

Definition at line 473 of file mxvk_math.h.

473{ *this = Build(*this, to); }
void Build(const vec4D &to)
Replace this vector with the direction from this point to to.
Definition mxvk_math.h:473

◆ Build() [4/4]

void mxvk::vec4D::Build ( const vec4D & to)
inline

Replace this vector with the direction from this point to to.

Definition at line 494 of file mxvk_math_eigen.hpp.

494{ *this = Build(*this, to); }

◆ Cos() [1/2]

float mxvk::vec4D::Cos ( const vec4D & v) const
inlinenodiscard

Compute the cosine of the angle between the 3D components of two vectors.

Definition at line 467 of file mxvk_math.h.

467 {
468 const float denom = Length() * v.Length();
469 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
470 }
float Length() const
Compute the 3D Euclidean length, ignoring the W component.
Definition mxvk_math.h:444
constexpr float DotProduct(const vec4D &v) const
Compute the 3D dot product, ignoring the W component.
Definition mxvk_math.h:438
constexpr float EPSILON
Default tolerance used for floating-point singularity and zero-length checks.
Definition mxvk_math.h:37

◆ Cos() [2/2]

float mxvk::vec4D::Cos ( const vec4D & v) const
inlinenodiscard

Compute the cosine of the angle between the 3D components of two vectors.

Definition at line 488 of file mxvk_math_eigen.hpp.

488 {
489 const float denom = Length() * v.Length();
490 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
491 }

◆ CrossProduct() [1/2]

vec4D mxvk::vec4D::CrossProduct ( const vec4D & v) const
inlinenodiscardconstexpr

Compute the 3D cross product and return it with W set to 1.

Definition at line 441 of file mxvk_math.h.

441{ return {y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x, 1.0f}; }

◆ CrossProduct() [2/2]

vec4D mxvk::vec4D::CrossProduct ( const vec4D & v) const
inlinenodiscard

Compute the 3D cross product and return it as a direction with W set to 0.

Definition at line 457 of file mxvk_math_eigen.hpp.

457 {
458 return {
459 y * v.z - z * v.y,
460 z * v.x - x * v.z,
461 x * v.y - y * v.x,
462 0.0f,
463 };
464 }

◆ DotProduct() [1/2]

float mxvk::vec4D::DotProduct ( const vec4D & v) const
inlinenodiscardconstexpr

Compute the 3D dot product, ignoring the W component.

Definition at line 438 of file mxvk_math.h.

438{ return x * v.x + y * v.y + z * v.z; }

◆ DotProduct() [2/2]

float mxvk::vec4D::DotProduct ( const vec4D & v) const
inlinenodiscard

Compute the 3D dot product, ignoring the W component.

Definition at line 454 of file mxvk_math_eigen.hpp.

454{ return x * v.x + y * v.y + z * v.z; }

◆ Length() [1/2]

float mxvk::vec4D::Length ( ) const
inlinenodiscard

Compute the 3D Euclidean length, ignoring the W component.

Definition at line 444 of file mxvk_math.h.

444{ return std::sqrt(DotProduct(*this)); }

◆ Length() [2/2]

float mxvk::vec4D::Length ( ) const
inlinenodiscard

Compute the 3D Euclidean length, ignoring the W component.

Definition at line 467 of file mxvk_math_eigen.hpp.

467{ return std::sqrt(DotProduct(*this)); }

◆ Normalize() [1/4]

void mxvk::vec4D::Normalize ( )
inline

Normalize the 3D components in place and reset W to 1.

Definition at line 447 of file mxvk_math.h.

447 {
448 const float len = Length();
449 if (len <= EPSILON) {
450 x = y = z = 0.0f;
451 w = 1.0f;
452 return;
453 }
454 x /= len;
455 y /= len;
456 z /= len;
457 w = 1.0f;
458 }

◆ Normalize() [2/4]

void mxvk::vec4D::Normalize ( )
inline

Normalize the 3D components in place while preserving W.

Definition at line 470 of file mxvk_math_eigen.hpp.

470 {
471 const float len = Length();
472 if (len <= EPSILON) {
473 x = y = z = 0.0f;
474 return;
475 }
476 x /= len;
477 y /= len;
478 z /= len;
479 }

◆ Normalize() [3/4]

void mxvk::vec4D::Normalize ( vec4D & v) const
inline

Write a normalized copy of this vector to v.

Definition at line 461 of file mxvk_math.h.

461 {
462 v = *this;
463 v.Normalize();
464 }

◆ Normalize() [4/4]

void mxvk::vec4D::Normalize ( vec4D & v) const
inline

Write a normalized copy of this vector to v.

Definition at line 482 of file mxvk_math_eigen.hpp.

482 {
483 v = *this;
484 v.Normalize();
485 }

◆ operator*() [1/4]

vec4D mxvk::vec4D::operator* ( const vec4D & v) const
inlinenodiscardconstexpr

Multiply two vectors component-wise.

Definition at line 424 of file mxvk_math.h.

424{ return {x * v.x, y * v.y, z * v.z, w * v.w}; }

◆ operator*() [2/4]

vec4D mxvk::vec4D::operator* ( const vec4D & v) const
inlinenodiscard

Multiply two vectors component-wise.

Definition at line 440 of file mxvk_math_eigen.hpp.

440{ return {x * v.x, y * v.y, z * v.z, w * v.w}; }

◆ operator*() [3/4]

vec4D mxvk::vec4D::operator* ( float k) const
inlinenodiscardconstexpr

Scale this vector by a scalar.

Definition at line 421 of file mxvk_math.h.

421{ return {x * k, y * k, z * k, w * k}; }

◆ operator*() [4/4]

vec4D mxvk::vec4D::operator* ( float k) const
inlinenodiscard

Scale this vector by a scalar.

Definition at line 437 of file mxvk_math_eigen.hpp.

437{ return {x * k, y * k, z * k, w * k}; }

◆ operator+() [1/2]

vec4D mxvk::vec4D::operator+ ( const vec4D & v) const
inlinenodiscardconstexpr

Add two vectors component-wise.

Definition at line 397 of file mxvk_math.h.

397{ return {x + v.x, y + v.y, z + v.z, w + v.w}; }

◆ operator+() [2/2]

vec4D mxvk::vec4D::operator+ ( const vec4D & v) const
inlinenodiscard

Add two vectors component-wise.

Definition at line 413 of file mxvk_math_eigen.hpp.

413{ return {x + v.x, y + v.y, z + v.z, w + v.w}; }

◆ operator+=() [1/2]

vec4D & mxvk::vec4D::operator+= ( const vec4D & v)
inline

Add another vector to this vector.

Definition at line 400 of file mxvk_math.h.

400 {
401 x += v.x;
402 y += v.y;
403 z += v.z;
404 w += v.w;
405 return *this;
406 }

◆ operator+=() [2/2]

vec4D & mxvk::vec4D::operator+= ( const vec4D & v)
inline

Add another vector to this vector.

Definition at line 416 of file mxvk_math_eigen.hpp.

416 {
417 x += v.x;
418 y += v.y;
419 z += v.z;
420 w += v.w;
421 return *this;
422 }

◆ operator-() [1/2]

vec4D mxvk::vec4D::operator- ( const vec4D & v) const
inlinenodiscardconstexpr

Subtract two vectors component-wise.

Definition at line 409 of file mxvk_math.h.

409{ return {x - v.x, y - v.y, z - v.z, w - v.w}; }

◆ operator-() [2/2]

vec4D mxvk::vec4D::operator- ( const vec4D & v) const
inlinenodiscard

Subtract two vectors component-wise.

Definition at line 425 of file mxvk_math_eigen.hpp.

425{ return {x - v.x, y - v.y, z - v.z, w - v.w}; }

◆ operator-=() [1/2]

vec4D & mxvk::vec4D::operator-= ( const vec4D & v)
inline

Subtract another vector from this vector.

Definition at line 412 of file mxvk_math.h.

412 {
413 x -= v.x;
414 y -= v.y;
415 z -= v.z;
416 w -= v.w;
417 return *this;
418 }

◆ operator-=() [2/2]

vec4D & mxvk::vec4D::operator-= ( const vec4D & v)
inline

Subtract another vector from this vector.

Definition at line 428 of file mxvk_math_eigen.hpp.

428 {
429 x -= v.x;
430 y -= v.y;
431 z -= v.z;
432 w -= v.w;
433 return *this;
434 }

◆ operator=() [1/2]

vec4D & mxvk::vec4D::operator= ( const vec4D & )
default

◆ operator=() [2/2]

vec4D & mxvk::vec4D::operator= ( const vec4D & )
default

◆ Print() [1/2]

std::string mxvk::vec4D::Print ( const std::string & name = "v") const
inlinenodiscard

Format this vector as a named angle-bracket tuple.

Definition at line 479 of file mxvk_math.h.

479 {
480 std::ostringstream out;
481 out << name << '<' << x << ',' << y << ',' << z << ',' << w << '>';
482 return out.str();
483 }

◆ Print() [2/2]

std::string mxvk::vec4D::Print ( const std::string & name = "v") const
inlinenodiscard

Format this vector as a named angle-bracket tuple.

Definition at line 500 of file mxvk_math_eigen.hpp.

500 {
501 std::ostringstream out;
502 out << name << '<' << x << ',' << y << ',' << z << ',' << w << '>';
503 return out.str();
504 }

◆ Scale() [1/2]

vec4D mxvk::vec4D::Scale ( float k) const
inlinenodiscard

Return a scaled copy of this vector.

Definition at line 427 of file mxvk_math.h.

427{ return *this * k; }

◆ Scale() [2/2]

vec4D mxvk::vec4D::Scale ( float k) const
inlinenodiscard

Return a scaled copy of this vector.

Definition at line 443 of file mxvk_math_eigen.hpp.

443{ return *this * k; }

◆ ScaleThis() [1/2]

void mxvk::vec4D::ScaleThis ( float k)
inline

Scale this vector in place.

Definition at line 430 of file mxvk_math.h.

430 {
431 x *= k;
432 y *= k;
433 z *= k;
434 w *= k;
435 }

◆ ScaleThis() [2/2]

void mxvk::vec4D::ScaleThis ( float k)
inline

Scale this vector in place.

Definition at line 446 of file mxvk_math_eigen.hpp.

446 {
447 x *= k;
448 y *= k;
449 z *= k;
450 w *= k;
451 }

◆ Set() [1/4]

void mxvk::vec4D::Set ( const vec4D & v)
inline

Copy coordinates from another vector.

Definition at line 392 of file mxvk_math.h.

392{ *this = v; }

◆ Set() [2/4]

void mxvk::vec4D::Set ( const vec4D & v)
inline

Copy coordinates from another vector.

Definition at line 408 of file mxvk_math_eigen.hpp.

408{ *this = v; }

◆ Set() [3/4]

void mxvk::vec4D::Set ( float x_value,
float y_value,
float z_value,
float w_value = 1.0f )
inline

Set all vector coordinates.

Definition at line 384 of file mxvk_math.h.

384 {
385 x = x_value;
386 y = y_value;
387 z = z_value;
388 w = w_value;
389 }

◆ Set() [4/4]

void mxvk::vec4D::Set ( float x_value,
float y_value,
float z_value,
float w_value = 1.0f )
inline

Set all vector coordinates.

Definition at line 400 of file mxvk_math_eigen.hpp.

400 {
401 x = x_value;
402 y = y_value;
403 z = z_value;
404 w = w_value;
405 }

Member Data Documentation

◆ w

float mxvk::vec4D::w = 1.0f

Homogeneous W coordinate.

Definition at line 375 of file mxvk_math.h.

◆ x

float mxvk::vec4D::x = 0.0f

X coordinate.

Definition at line 366 of file mxvk_math.h.

◆ y

float mxvk::vec4D::y = 0.0f

Y coordinate.

Definition at line 369 of file mxvk_math.h.

◆ z

float mxvk::vec4D::z = 0.0f

Z coordinate.

Definition at line 372 of file mxvk_math.h.


The documentation for this class was generated from the following files: