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::vec3D Class Reference

Three-dimensional float vector with arithmetic, dot, and cross-product helpers. More...

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

Public Member Functions

float Cos (const vec3D &v) const
 Compute the cosine of the angle between this vector and another vector.
float Cos (const vec3D &v) const
 Compute the cosine of the angle between this vector and another vector.
constexpr vec3D CrossProduct (const vec3D &v) const
 Compute the right-handed cross product with another vector.
vec3D CrossProduct (const vec3D &v) const
 Compute the right-handed cross product with another vector.
constexpr float DotProduct (const vec3D &v) const
 Compute the dot product with another vector.
float DotProduct (const vec3D &v) const
 Compute the dot product with another vector.
float Length () const
 Compute the Euclidean length of this vector.
float Length () const
 Compute the Euclidean length of this vector.
void Normalize ()
 Normalize this vector in place, or reset it to zero if it is too short.
void Normalize ()
 Normalize this vector in place, or reset it to zero if it is too short.
void Normalize (vec3D &v) const
 Write a normalized copy of this vector to v.
void Normalize (vec3D &v) const
 Write a normalized copy of this vector to v.
constexpr vec3D operator* (float k) const
 Scale this vector by a scalar.
vec3D operator* (float k) const
 Scale this vector by a scalar.
constexpr vec3D operator+ (const vec3D &v) const
 Add two vectors component-wise.
vec3D operator+ (const vec3D &v) const
 Add two vectors component-wise.
vec3Doperator+= (const vec3D &v)
 Add another vector to this vector.
vec3Doperator+= (const vec3D &v)
 Add another vector to this vector.
constexpr vec3D operator- (const vec3D &v) const
 Subtract two vectors component-wise.
vec3D operator- (const vec3D &v) const
 Subtract two vectors component-wise.
vec3Doperator-= (const vec3D &v)
 Subtract another vector from this vector.
vec3Doperator-= (const vec3D &v)
 Subtract another vector from this vector.
vec3Doperator= (const vec3D &)=default
vec3Doperator= (const vec3D &)=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.
vec3D Scale (float k) const
 Return a scaled copy of this vector.
vec3D 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 (float x_value, float y_value, float z_value)
 Set all vector coordinates.
void Set (float x_value, float y_value, float z_value)
 Set all vector coordinates.
constexpr vec3D ()
 Construct the zero vector.
constexpr vec3D ()
 Construct the zero vector.
constexpr vec3D (float x_value, float y_value, float z_value)
 Construct a vector from explicit coordinates.
constexpr vec3D (float x_value, float y_value, float z_value)
 Construct a vector from explicit coordinates.

Public Attributes

float x = 0.0f
 X coordinate.
float y = 0.0f
 Y coordinate.
float z = 0.0f
 Z coordinate.

Detailed Description

Three-dimensional float vector with arithmetic, dot, and cross-product helpers.

Definition at line 291 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ vec3D() [1/4]

mxvk::vec3D::vec3D ( )
inlineconstexpr

Construct the zero vector.

Definition at line 303 of file mxvk_math.h.

303: x(0.0f), y(0.0f), z(0.0f) {}
float z
Z coordinate.
Definition mxvk_math.h:300
float x
X coordinate.
Definition mxvk_math.h:294
float y
Y coordinate.
Definition mxvk_math.h:297

◆ vec3D() [2/4]

mxvk::vec3D::vec3D ( float x_value,
float y_value,
float z_value )
inlineconstexpr

Construct a vector from explicit coordinates.

Definition at line 306 of file mxvk_math.h.

306: x(x_value), y(y_value), z(z_value) {}

◆ vec3D() [3/4]

mxvk::vec3D::vec3D ( )
inlineconstexpr

Construct the zero vector.

Definition at line 328 of file mxvk_math_eigen.hpp.

328: x(0.0f), y(0.0f), z(0.0f) {}

◆ vec3D() [4/4]

mxvk::vec3D::vec3D ( float x_value,
float y_value,
float z_value )
inlineconstexpr

Construct a vector from explicit coordinates.

Definition at line 331 of file mxvk_math_eigen.hpp.

331: x(x_value), y(y_value), z(z_value) {}

Member Function Documentation

◆ Cos() [1/2]

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

Compute the cosine of the angle between this vector and another vector.

Definition at line 392 of file mxvk_math.h.

392 {
393 const float denom = Length() * v.Length();
394 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
395 }
constexpr float DotProduct(const vec3D &v) const
Compute the dot product with another vector.
Definition mxvk_math.h:361
float Length() const
Compute the Euclidean length of this vector.
Definition mxvk_math.h:371
constexpr float EPSILON
Default tolerance used for floating-point singularity and zero-length checks.
Definition mxvk_math.h:37

◆ Cos() [2/2]

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

Compute the cosine of the angle between this vector and another vector.

Definition at line 411 of file mxvk_math_eigen.hpp.

411 {
412 const float denom = Length() * v.Length();
413 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
414 }

◆ CrossProduct() [1/2]

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

Compute the right-handed cross product with another vector.

Definition at line 366 of file mxvk_math.h.

366 {
367 return {y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x};
368 }

◆ CrossProduct() [2/2]

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

Compute the right-handed cross product with another vector.

Definition at line 385 of file mxvk_math_eigen.hpp.

385 {
386 return FromEigen(ToEigen().cross(v.ToEigen()));
387 }

◆ DotProduct() [1/2]

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

Compute the dot product with another vector.

Definition at line 361 of file mxvk_math.h.

361 {
362 return x * v.x + y * v.y + z * v.z;
363 }

◆ DotProduct() [2/2]

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

Compute the dot product with another vector.

Definition at line 380 of file mxvk_math_eigen.hpp.

380 {
381 return ToEigen().dot(v.ToEigen());
382 }

◆ Length() [1/2]

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

Compute the Euclidean length of this vector.

Definition at line 371 of file mxvk_math.h.

371 {
372 return std::sqrt(DotProduct(*this));
373 }

◆ Length() [2/2]

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

Compute the Euclidean length of this vector.

Definition at line 390 of file mxvk_math_eigen.hpp.

390 {
391 return ToEigen().norm();
392 }

◆ Normalize() [1/4]

void mxvk::vec3D::Normalize ( )
inline

Normalize this vector in place, or reset it to zero if it is too short.

Definition at line 376 of file mxvk_math.h.

376 {
377 const float len = Length();
378 if (len <= EPSILON) {
379 x = y = z = 0.0f;
380 return;
381 }
382 ScaleThis(1.0f / len);
383 }
void ScaleThis(float k)
Scale this vector in place.
Definition mxvk_math.h:354

◆ Normalize() [2/4]

void mxvk::vec3D::Normalize ( )
inline

Normalize this vector in place, or reset it to zero if it is too short.

Definition at line 395 of file mxvk_math_eigen.hpp.

395 {
396 const float len = Length();
397 if (len <= EPSILON) {
398 FromEigen(Eigen::Vector3f::Zero(), *this);
399 return;
400 }
401 FromEigen(ToEigen().normalized(), *this);
402 }

◆ Normalize() [3/4]

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

Write a normalized copy of this vector to v.

Definition at line 386 of file mxvk_math.h.

386 {
387 v = *this;
388 v.Normalize();
389 }

◆ Normalize() [4/4]

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

Write a normalized copy of this vector to v.

Definition at line 405 of file mxvk_math_eigen.hpp.

405 {
406 v = *this;
407 v.Normalize();
408 }

◆ operator*() [1/2]

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

Scale this vector by a scalar.

Definition at line 344 of file mxvk_math.h.

344 {
345 return {x * k, y * k, z * k};
346 }

◆ operator*() [2/2]

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

Scale this vector by a scalar.

Definition at line 365 of file mxvk_math_eigen.hpp.

365 {
366 return FromEigen(ToEigen() * k);
367 }

◆ operator+() [1/2]

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

Add two vectors component-wise.

Definition at line 318 of file mxvk_math.h.

318 {
319 return {x + v.x, y + v.y, z + v.z};
320 }

◆ operator+() [2/2]

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

Add two vectors component-wise.

Definition at line 343 of file mxvk_math_eigen.hpp.

343 {
344 return FromEigen(ToEigen() + v.ToEigen());
345 }

◆ operator+=() [1/2]

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

Add another vector to this vector.

Definition at line 323 of file mxvk_math.h.

323 {
324 x += v.x;
325 y += v.y;
326 z += v.z;
327 return *this;
328 }

◆ operator+=() [2/2]

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

Add another vector to this vector.

Definition at line 348 of file mxvk_math_eigen.hpp.

348 {
349 FromEigen(ToEigen() + v.ToEigen(), *this);
350 return *this;
351 }

◆ operator-() [1/2]

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

Subtract two vectors component-wise.

Definition at line 331 of file mxvk_math.h.

331 {
332 return {x - v.x, y - v.y, z - v.z};
333 }

◆ operator-() [2/2]

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

Subtract two vectors component-wise.

Definition at line 354 of file mxvk_math_eigen.hpp.

354 {
355 return FromEigen(ToEigen() - v.ToEigen());
356 }

◆ operator-=() [1/2]

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

Subtract another vector from this vector.

Definition at line 336 of file mxvk_math.h.

336 {
337 x -= v.x;
338 y -= v.y;
339 z -= v.z;
340 return *this;
341 }

◆ operator-=() [2/2]

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

Subtract another vector from this vector.

Definition at line 359 of file mxvk_math_eigen.hpp.

359 {
360 FromEigen(ToEigen() - v.ToEigen(), *this);
361 return *this;
362 }

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ Print() [1/2]

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

Format this vector as a named angle-bracket tuple.

Definition at line 398 of file mxvk_math.h.

398 {
399 std::ostringstream out;
400 out << name << '<' << x << ',' << y << ',' << z << '>';
401 return out.str();
402 }

◆ Print() [2/2]

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

Format this vector as a named angle-bracket tuple.

Definition at line 417 of file mxvk_math_eigen.hpp.

417 {
418 std::ostringstream out;
419 out << name << '<' << x << ',' << y << ',' << z << '>';
420 return out.str();
421 }

◆ Scale() [1/2]

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

Return a scaled copy of this vector.

Definition at line 349 of file mxvk_math.h.

349 {
350 return *this * k;
351 }

◆ Scale() [2/2]

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

Return a scaled copy of this vector.

Definition at line 370 of file mxvk_math_eigen.hpp.

370 {
371 return *this * k;
372 }

◆ ScaleThis() [1/2]

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

Scale this vector in place.

Definition at line 354 of file mxvk_math.h.

354 {
355 x *= k;
356 y *= k;
357 z *= k;
358 }

◆ ScaleThis() [2/2]

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

Scale this vector in place.

Definition at line 375 of file mxvk_math_eigen.hpp.

375 {
376 FromEigen(ToEigen() * k, *this);
377 }

◆ Set() [1/2]

void mxvk::vec3D::Set ( float x_value,
float y_value,
float z_value )
inline

Set all vector coordinates.

Definition at line 309 of file mxvk_math.h.

309 {
310 x = x_value;
311 y = y_value;
312 z = z_value;
313 }

◆ Set() [2/2]

void mxvk::vec3D::Set ( float x_value,
float y_value,
float z_value )
inline

Set all vector coordinates.

Definition at line 334 of file mxvk_math_eigen.hpp.

334 {
335 x = x_value;
336 y = y_value;
337 z = z_value;
338 }

Member Data Documentation

◆ x

float mxvk::vec3D::x = 0.0f

X coordinate.

Definition at line 294 of file mxvk_math.h.

◆ y

float mxvk::vec3D::y = 0.0f

Y coordinate.

Definition at line 297 of file mxvk_math.h.

◆ z

float mxvk::vec3D::z = 0.0f

Z coordinate.

Definition at line 300 of file mxvk_math.h.


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