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::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.
vec3D & operator+= (const vec3D &v)
 Add another vector to this vector.
vec3D & operator+= (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.
vec3D & operator-= (const vec3D &v)
 Subtract another vector from this vector.
vec3D & operator-= (const vec3D &v)
 Subtract another vector from this vector.
vec3D & operator= (const vec3D &)=default
vec3D & operator= (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 256 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ vec3D() [1/4]

mxvk::vec3D::vec3D ( )
inlineconstexpr

Construct the zero vector.

Definition at line 268 of file mxvk_math.h.

268: x(0.0f), y(0.0f), z(0.0f) {}
float z
Z coordinate.
Definition mxvk_math.h:265
float x
X coordinate.
Definition mxvk_math.h:259
float y
Y coordinate.
Definition mxvk_math.h:262

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

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

◆ vec3D() [3/4]

mxvk::vec3D::vec3D ( )
inlineconstexpr

Construct the zero vector.

Definition at line 285 of file mxvk_math_eigen.hpp.

285: 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 288 of file mxvk_math_eigen.hpp.

288: 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 343 of file mxvk_math.h.

343 {
344 const float denom = Length() * v.Length();
345 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
346 }
constexpr float DotProduct(const vec3D &v) const
Compute the dot product with another vector.
Definition mxvk_math.h:318
float Length() const
Compute the Euclidean length of this vector.
Definition mxvk_math.h:324
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 352 of file mxvk_math_eigen.hpp.

352 {
353 const float denom = Length() * v.Length();
354 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
355 }

◆ CrossProduct() [1/2]

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

Compute the right-handed cross product with another vector.

Definition at line 321 of file mxvk_math.h.

321{ return {y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x}; }

◆ CrossProduct() [2/2]

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

Compute the right-handed cross product with another vector.

Definition at line 330 of file mxvk_math_eigen.hpp.

330{ return FromEigen(ToEigen().cross(v.ToEigen())); }

◆ DotProduct() [1/2]

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

Compute the dot product with another vector.

Definition at line 318 of file mxvk_math.h.

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

◆ DotProduct() [2/2]

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

Compute the dot product with another vector.

Definition at line 327 of file mxvk_math_eigen.hpp.

327{ return ToEigen().dot(v.ToEigen()); }

◆ Length() [1/2]

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

Compute the Euclidean length of this vector.

Definition at line 324 of file mxvk_math.h.

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

◆ Length() [2/2]

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

Compute the Euclidean length of this vector.

Definition at line 333 of file mxvk_math_eigen.hpp.

333{ return ToEigen().norm(); }

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

327 {
328 const float len = Length();
329 if (len <= EPSILON) {
330 x = y = z = 0.0f;
331 return;
332 }
333 ScaleThis(1.0f / len);
334 }
void ScaleThis(float k)
Scale this vector in place.
Definition mxvk_math.h:311

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

336 {
337 const float len = Length();
338 if (len <= EPSILON) {
339 FromEigen(Eigen::Vector3f::Zero(), *this);
340 return;
341 }
342 FromEigen(ToEigen().normalized(), *this);
343 }

◆ Normalize() [3/4]

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

Write a normalized copy of this vector to v.

Definition at line 337 of file mxvk_math.h.

337 {
338 v = *this;
339 v.Normalize();
340 }

◆ Normalize() [4/4]

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

Write a normalized copy of this vector to v.

Definition at line 346 of file mxvk_math_eigen.hpp.

346 {
347 v = *this;
348 v.Normalize();
349 }

◆ operator*() [1/2]

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

Scale this vector by a scalar.

Definition at line 305 of file mxvk_math.h.

305{ return {x * k, y * k, z * k}; }

◆ operator*() [2/2]

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

Scale this vector by a scalar.

Definition at line 318 of file mxvk_math_eigen.hpp.

318{ return FromEigen(ToEigen() * k); }

◆ operator+() [1/2]

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

Add two vectors component-wise.

Definition at line 283 of file mxvk_math.h.

283{ return {x + v.x, y + v.y, z + v.z}; }

◆ operator+() [2/2]

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

Add two vectors component-wise.

Definition at line 300 of file mxvk_math_eigen.hpp.

300{ return FromEigen(ToEigen() + v.ToEigen()); }

◆ operator+=() [1/2]

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

Add another vector to this vector.

Definition at line 286 of file mxvk_math.h.

286 {
287 x += v.x;
288 y += v.y;
289 z += v.z;
290 return *this;
291 }

◆ operator+=() [2/2]

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

Add another vector to this vector.

Definition at line 303 of file mxvk_math_eigen.hpp.

303 {
304 FromEigen(ToEigen() + v.ToEigen(), *this);
305 return *this;
306 }

◆ operator-() [1/2]

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

Subtract two vectors component-wise.

Definition at line 294 of file mxvk_math.h.

294{ return {x - v.x, y - v.y, z - v.z}; }

◆ operator-() [2/2]

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

Subtract two vectors component-wise.

Definition at line 309 of file mxvk_math_eigen.hpp.

309{ return FromEigen(ToEigen() - v.ToEigen()); }

◆ operator-=() [1/2]

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

Subtract another vector from this vector.

Definition at line 297 of file mxvk_math.h.

297 {
298 x -= v.x;
299 y -= v.y;
300 z -= v.z;
301 return *this;
302 }

◆ operator-=() [2/2]

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

Subtract another vector from this vector.

Definition at line 312 of file mxvk_math_eigen.hpp.

312 {
313 FromEigen(ToEigen() - v.ToEigen(), *this);
314 return *this;
315 }

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

349 {
350 std::ostringstream out;
351 out << name << '<' << x << ',' << y << ',' << z << '>';
352 return out.str();
353 }

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

358 {
359 std::ostringstream out;
360 out << name << '<' << x << ',' << y << ',' << z << '>';
361 return out.str();
362 }

◆ Scale() [1/2]

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

Return a scaled copy of this vector.

Definition at line 308 of file mxvk_math.h.

308{ return *this * k; }

◆ Scale() [2/2]

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

Return a scaled copy of this vector.

Definition at line 321 of file mxvk_math_eigen.hpp.

321{ return *this * k; }

◆ ScaleThis() [1/2]

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

Scale this vector in place.

Definition at line 311 of file mxvk_math.h.

311 {
312 x *= k;
313 y *= k;
314 z *= k;
315 }

◆ ScaleThis() [2/2]

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

Scale this vector in place.

Definition at line 324 of file mxvk_math_eigen.hpp.

324{ FromEigen(ToEigen() * k, *this); }

◆ Set() [1/2]

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

Set all vector coordinates.

Definition at line 274 of file mxvk_math.h.

274 {
275 x = x_value;
276 y = y_value;
277 z = z_value;
278 }

◆ Set() [2/2]

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

Set all vector coordinates.

Definition at line 291 of file mxvk_math_eigen.hpp.

291 {
292 x = x_value;
293 y = y_value;
294 z = z_value;
295 }

Member Data Documentation

◆ x

float mxvk::vec3D::x = 0.0f

X coordinate.

Definition at line 259 of file mxvk_math.h.

◆ y

float mxvk::vec3D::y = 0.0f

Y coordinate.

Definition at line 262 of file mxvk_math.h.

◆ z

float mxvk::vec3D::z = 0.0f

Z coordinate.

Definition at line 265 of file mxvk_math.h.


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