HPC_Voxel_Engine 0.2.0
High-Performance C++ Voxel Engine
Loading...
Searching...
No Matches
MathUtils.h
Go to the documentation of this file.
1#pragma once
2#include <cmath>
3#include <iostream>
4
5namespace Core {
6
11struct Vec3 {
12 float x, y, z;
13
14 // --- Constructors ---
15 constexpr Vec3() : x(0.0f), y(0.0f), z(0.0f) {}
16 constexpr Vec3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
17
18 // --- Operator Overloads ---
19
20 [[nodiscard]] constexpr Vec3 operator+(const Vec3& other) const noexcept {
21 return Vec3(x + other.x, y + other.y, z + other.z);
22 }
23 [[nodiscard]] constexpr Vec3 operator-(const Vec3& other) const noexcept {
24 return Vec3(x - other.x, y - other.y, z - other.z);
25 }
26 constexpr Vec3& operator+=(const Vec3& other) noexcept {
27 x += other.x;
28 y += other.y;
29 z += other.z;
30 return *this;
31 }
32 constexpr Vec3& operator-=(const Vec3& other) noexcept {
33 x -= other.x;
34 y -= other.y;
35 z -= other.z;
36 return *this;
37 }
38 [[nodiscard]] constexpr Vec3 operator*(float scalar) const noexcept {
39 return Vec3(x * scalar, y * scalar, z * scalar);
40 }
41 [[nodiscard]] constexpr Vec3 operator/(float scalar) const noexcept {
42 return Vec3(x / scalar, y / scalar, z / scalar);
43 }
44
45 // --- geometric Utilities ---
46
47 [[nodiscard]] float squaredLength() const { return x * x + y * y + z * z; }
48
49 [[nodiscard]] float length() const { return std::sqrt(squaredLength()); }
50
51 [[nodiscard]] Vec3 normalize() const {
52 float len = length();
53 if (len < 1e-5f)
54 return Vec3(0.0f, 0.0f, 0.0f);
55 return Vec3(x / len, y / len, z / len);
56 }
57 [[nodiscard]] float SquaredDistanceFromPoint(const Core::Vec3& other) const {
58 Vec3 diff = *this - other;
59 return diff.squaredLength();
60 }
61 [[nodiscard]] float DistanceFromPoint(const Core::Vec3& other) const {
62 Vec3 diff = *this - other;
63 return diff.length();
64 }
65
66 // --- Dot and Cross Product ---
67
68 [[nodiscard]] constexpr float dot(const Vec3& other) const noexcept {
69 return x * other.x + y * other.y + z * other.z;
70 }
71 [[nodiscard]] constexpr Vec3 cross(const Vec3& other) const noexcept {
72 return Vec3(
73 y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x);
74 }
75
76 // --- Debug ---
77
78 void print() const { std::cout << "Vec3(" << x << ", " << y << ", " << z << ")\n"; }
79
80 friend std::ostream& operator<<(std::ostream& os, const Vec3& vec) {
81 os << "Vec3(" << vec.x << ", " << vec.y << ", " << vec.z << ")";
82 return os;
83 }
84};
85} // namespace Core
Definition camera.h:6
A 3-component vector structure (x, y, z) with standard math operations.
Definition MathUtils.h:11
constexpr Vec3 & operator+=(const Vec3 &other) noexcept
Definition MathUtils.h:26
float DistanceFromPoint(const Core::Vec3 &other) const
Definition MathUtils.h:61
constexpr Vec3 & operator-=(const Vec3 &other) noexcept
Definition MathUtils.h:32
float x
Definition MathUtils.h:12
float squaredLength() const
Definition MathUtils.h:47
constexpr Vec3 operator*(float scalar) const noexcept
Definition MathUtils.h:38
constexpr float dot(const Vec3 &other) const noexcept
Definition MathUtils.h:68
float z
Definition MathUtils.h:12
constexpr Vec3 operator-(const Vec3 &other) const noexcept
Definition MathUtils.h:23
float SquaredDistanceFromPoint(const Core::Vec3 &other) const
Definition MathUtils.h:57
constexpr Vec3 operator/(float scalar) const noexcept
Definition MathUtils.h:41
constexpr Vec3 cross(const Vec3 &other) const noexcept
Definition MathUtils.h:71
Vec3 normalize() const
Definition MathUtils.h:51
constexpr Vec3()
Definition MathUtils.h:15
constexpr Vec3 operator+(const Vec3 &other) const noexcept
Definition MathUtils.h:20
float y
Definition MathUtils.h:12
float length() const
Definition MathUtils.h:49
friend std::ostream & operator<<(std::ostream &os, const Vec3 &vec)
Definition MathUtils.h:80
void print() const
Definition MathUtils.h:78
constexpr Vec3(float _x, float _y, float _z)
Definition MathUtils.h:16