MXVK Vulkan Framework 0.24.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.
vec4Doperator+= (const vec4D &v)
 Add another vector to this vector.
vec4Doperator+= (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.
vec4Doperator-= (const vec4D &v)
 Subtract another vector from this vector.
vec4Doperator-= (const vec4D &v)
 Subtract another vector from this vector.
vec4Doperator= (const vec4D &)=default
vec4Doperator= (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 416 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ vec4D() [1/4]

mxvk::vec4D::vec4D ( )
inlineconstexpr

Construct the homogeneous origin.

Definition at line 431 of file mxvk_math.h.

431: x(0.0f), y(0.0f), z(0.0f), w(1.0f) {}
float y
Y coordinate.
Definition mxvk_math.h:422
float x
X coordinate.
Definition mxvk_math.h:419
float w
Homogeneous W coordinate.
Definition mxvk_math.h:428
float z
Z coordinate.
Definition mxvk_math.h:425

◆ 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 434 of file mxvk_math.h.

434: 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 463 of file mxvk_math_eigen.hpp.

463: 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 466 of file mxvk_math_eigen.hpp.

466: 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 549 of file mxvk_math.h.

549 {
550 return {to.x - from.x, to.y - from.y, to.z - from.z, 1.0f};
551 }

◆ 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 584 of file mxvk_math_eigen.hpp.

584 {
585 return {to.x - from.x, to.y - from.y, to.z - from.z, 0.0f};
586 }

◆ 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 544 of file mxvk_math.h.

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

◆ 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 579 of file mxvk_math_eigen.hpp.

579 {
580 *this = Build(*this, to);
581 }

◆ 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 538 of file mxvk_math.h.

538 {
539 const float denom = Length() * v.Length();
540 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
541 }
float Length() const
Compute the 3D Euclidean length, ignoring the W component.
Definition mxvk_math.h:513
constexpr float DotProduct(const vec4D &v) const
Compute the 3D dot product, ignoring the W component.
Definition mxvk_math.h:503
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 573 of file mxvk_math_eigen.hpp.

573 {
574 const float denom = Length() * v.Length();
575 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
576 }

◆ 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 508 of file mxvk_math.h.

508 {
509 return {y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x, 1.0f};
510 }

◆ 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 540 of file mxvk_math_eigen.hpp.

540 {
541 return {
542 y * v.z - z * v.y,
543 z * v.x - x * v.z,
544 x * v.y - y * v.x,
545 0.0f,
546 };
547 }

◆ DotProduct() [1/2]

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

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

Definition at line 503 of file mxvk_math.h.

503 {
504 return x * v.x + y * v.y + z * v.z;
505 }

◆ DotProduct() [2/2]

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

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

Definition at line 535 of file mxvk_math_eigen.hpp.

535 {
536 return x * v.x + y * v.y + z * v.z;
537 }

◆ Length() [1/2]

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

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

Definition at line 513 of file mxvk_math.h.

513 {
514 return std::sqrt(DotProduct(*this));
515 }

◆ Length() [2/2]

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

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

Definition at line 550 of file mxvk_math_eigen.hpp.

550 {
551 return std::sqrt(DotProduct(*this));
552 }

◆ Normalize() [1/4]

void mxvk::vec4D::Normalize ( )
inline

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

Definition at line 518 of file mxvk_math.h.

518 {
519 const float len = Length();
520 if (len <= EPSILON) {
521 x = y = z = 0.0f;
522 w = 1.0f;
523 return;
524 }
525 x /= len;
526 y /= len;
527 z /= len;
528 w = 1.0f;
529 }

◆ Normalize() [2/4]

void mxvk::vec4D::Normalize ( )
inline

Normalize the 3D components in place while preserving W.

Definition at line 555 of file mxvk_math_eigen.hpp.

555 {
556 const float len = Length();
557 if (len <= EPSILON) {
558 x = y = z = 0.0f;
559 return;
560 }
561 x /= len;
562 y /= len;
563 z /= len;
564 }

◆ Normalize() [3/4]

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

Write a normalized copy of this vector to v.

Definition at line 532 of file mxvk_math.h.

532 {
533 v = *this;
534 v.Normalize();
535 }

◆ Normalize() [4/4]

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

Write a normalized copy of this vector to v.

Definition at line 567 of file mxvk_math_eigen.hpp.

567 {
568 v = *this;
569 v.Normalize();
570 }

◆ operator*() [1/4]

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

Multiply two vectors component-wise.

Definition at line 485 of file mxvk_math.h.

485 {
486 return {x * v.x, y * v.y, z * v.z, w * v.w};
487 }

◆ operator*() [2/4]

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

Multiply two vectors component-wise.

Definition at line 517 of file mxvk_math_eigen.hpp.

517 {
518 return {x * v.x, y * v.y, z * v.z, w * v.w};
519 }

◆ operator*() [3/4]

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

Scale this vector by a scalar.

Definition at line 480 of file mxvk_math.h.

480 {
481 return {x * k, y * k, z * k, w * k};
482 }

◆ operator*() [4/4]

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

Scale this vector by a scalar.

Definition at line 512 of file mxvk_math_eigen.hpp.

512 {
513 return {x * k, y * k, z * k, w * k};
514 }

◆ operator+() [1/2]

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

Add two vectors component-wise.

Definition at line 452 of file mxvk_math.h.

452 {
453 return {x + v.x, y + v.y, z + v.z, w + v.w};
454 }

◆ operator+() [2/2]

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

Add two vectors component-wise.

Definition at line 484 of file mxvk_math_eigen.hpp.

484 {
485 return {x + v.x, y + v.y, z + v.z, w + v.w};
486 }

◆ operator+=() [1/2]

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

Add another vector to this vector.

Definition at line 457 of file mxvk_math.h.

457 {
458 x += v.x;
459 y += v.y;
460 z += v.z;
461 w += v.w;
462 return *this;
463 }

◆ operator+=() [2/2]

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

Add another vector to this vector.

Definition at line 489 of file mxvk_math_eigen.hpp.

489 {
490 x += v.x;
491 y += v.y;
492 z += v.z;
493 w += v.w;
494 return *this;
495 }

◆ operator-() [1/2]

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

Subtract two vectors component-wise.

Definition at line 466 of file mxvk_math.h.

466 {
467 return {x - v.x, y - v.y, z - v.z, w - v.w};
468 }

◆ operator-() [2/2]

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

Subtract two vectors component-wise.

Definition at line 498 of file mxvk_math_eigen.hpp.

498 {
499 return {x - v.x, y - v.y, z - v.z, w - v.w};
500 }

◆ operator-=() [1/2]

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

Subtract another vector from this vector.

Definition at line 471 of file mxvk_math.h.

471 {
472 x -= v.x;
473 y -= v.y;
474 z -= v.z;
475 w -= v.w;
476 return *this;
477 }

◆ operator-=() [2/2]

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

Subtract another vector from this vector.

Definition at line 503 of file mxvk_math_eigen.hpp.

503 {
504 x -= v.x;
505 y -= v.y;
506 z -= v.z;
507 w -= v.w;
508 return *this;
509 }

◆ 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 554 of file mxvk_math.h.

554 {
555 std::ostringstream out;
556 out << name << '<' << x << ',' << y << ',' << z << ',' << w << '>';
557 return out.str();
558 }

◆ 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 589 of file mxvk_math_eigen.hpp.

589 {
590 std::ostringstream out;
591 out << name << '<' << x << ',' << y << ',' << z << ',' << w << '>';
592 return out.str();
593 }

◆ Scale() [1/2]

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

Return a scaled copy of this vector.

Definition at line 490 of file mxvk_math.h.

490 {
491 return *this * k;
492 }

◆ Scale() [2/2]

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

Return a scaled copy of this vector.

Definition at line 522 of file mxvk_math_eigen.hpp.

522 {
523 return *this * k;
524 }

◆ ScaleThis() [1/2]

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

Scale this vector in place.

Definition at line 495 of file mxvk_math.h.

495 {
496 x *= k;
497 y *= k;
498 z *= k;
499 w *= k;
500 }

◆ ScaleThis() [2/2]

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

Scale this vector in place.

Definition at line 527 of file mxvk_math_eigen.hpp.

527 {
528 x *= k;
529 y *= k;
530 z *= k;
531 w *= k;
532 }

◆ Set() [1/4]

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

Copy coordinates from another vector.

Definition at line 445 of file mxvk_math.h.

445 {
446 *this = v;
447 }

◆ Set() [2/4]

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

Copy coordinates from another vector.

Definition at line 477 of file mxvk_math_eigen.hpp.

477 {
478 *this = v;
479 }

◆ 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 437 of file mxvk_math.h.

437 {
438 x = x_value;
439 y = y_value;
440 z = z_value;
441 w = w_value;
442 }

◆ 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 469 of file mxvk_math_eigen.hpp.

469 {
470 x = x_value;
471 y = y_value;
472 z = z_value;
473 w = w_value;
474 }

Member Data Documentation

◆ w

float mxvk::vec4D::w = 1.0f

Homogeneous W coordinate.

Definition at line 428 of file mxvk_math.h.

◆ x

float mxvk::vec4D::x = 0.0f

X coordinate.

Definition at line 419 of file mxvk_math.h.

◆ y

float mxvk::vec4D::y = 0.0f

Y coordinate.

Definition at line 422 of file mxvk_math.h.

◆ z

float mxvk::vec4D::z = 0.0f

Z coordinate.

Definition at line 425 of file mxvk_math.h.


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