soo
3D game math library
Loading...
Searching...
No Matches
vector3.h
1// By notchcamo.
2
3#pragma once
4
5#include <format>
6#include <string>
7
8#include "exception.h"
9#include "util.h"
10
14namespace soo
15{
20 template <std::floating_point T>
21 class Vector3
22 {
23 public:
24 T x, y, z;
25
26 public:
27 // Constructors.
28
29 Vector3() = default;
30 explicit Vector3(const T value) noexcept : x(value), y(value), z(value) {}
31 Vector3(const T x, const T y, const T z) noexcept : x(x), y(y), z(z) {}
32 Vector3(const Vector3& other) noexcept : x(other.x), y(other.y), z(other.z) {}
33 Vector3(Vector3&& other) noexcept : x(std::move(other.x)), y(std::move(other.y)), z(std::move(other.z)) {}
34
35 ~Vector3() noexcept = default;
36
37 // Operator Overloadings.
38
39 Vector3& operator=(const Vector3& rhs) noexcept
40 {
41 if (*this != rhs)
42 {
43 x = rhs.x;
44 y = rhs.y;
45 z = rhs.z;
46 }
47
48 return *this;
49 }
50 Vector3& operator=(Vector3&& rhs) noexcept
51 {
52 if (*this != rhs)
53 {
54 x = std::move(rhs.x);
55 y = std::move(rhs.y);
56 z = std::move(rhs.z);
57 }
58
59 return *this;
60 }
61 Vector3 operator+(const Vector3& rhs) const noexcept
62 {
63 return Vector3(x + rhs.x, y + rhs.y, z + rhs.z);
64 }
65 Vector3 operator-(const Vector3& rhs) const noexcept
66 {
67 return Vector3(x - rhs.x, y - rhs.y, z - rhs.z);
68 }
69 Vector3 operator*(const T scalar) const noexcept
70 {
71 return Vector3(x * scalar, y * scalar, z * scalar);
72 }
73 Vector3 operator/(const T scalar) const
74 {
75 if (util::isZero(scalar))
76 {
78 }
79
80 const T inv_scalar{static_cast<T>(1.0) / scalar};
81 return Vector3(x * inv_scalar, y * inv_scalar, z * inv_scalar);
82 }
83 Vector3& operator+=(const Vector3& rhs) noexcept
84 {
85 x += rhs.x;
86 y += rhs.y;
87 z += rhs.z;
88 return *this;
89 }
90 Vector3 operator-=(const Vector3& rhs) noexcept
91 {
92 x -= rhs.x;
93 y -= rhs.y;
94 z -= rhs.z;
95 return *this;
96 }
97 Vector3& operator*=(const T scalar) noexcept
98 {
99 x *= scalar;
100 y *= scalar;
101 z *= scalar;
102 return *this;
103 }
104 Vector3& operator/=(const T scalar)
105 {
106 if (util::isZero(scalar))
107 {
109 }
110
111 const T inv_scalar{static_cast<T>(1.0) / scalar};
112 x *= inv_scalar;
113 y *= inv_scalar;
114 z *= inv_scalar;
115 return *this;
116 }
117 bool operator==(const Vector3& rhs) const
118 {
119 return util::isEqual(x, rhs.x) &&
120 util::isEqual(y, rhs.y) &&
121 util::isEqual(z, rhs.z);
122 }
123
124 // Getters & Setters
125
126 T getX() const noexcept { return x; }
127 T getY() const noexcept { return y; }
128 T getZ() const noexcept { return z; }
129
130 void setX(const T value) noexcept { x = value; }
131 void setY(const T value) noexcept { y = value; }
132 void setZ(const T value) noexcept { z = value; }
133
134 // Other methods.
135
139 std::string toString() const
140 {
141 return std::format("[{:f}, {:f}, {:f}]", x, y, z);
142 }
143
149 T getMagnitude() const
150 {
151 return std::sqrt(x * x + y * y + z * z);
152 }
153
157 T getMagnitudeSquared() const noexcept
158 {
159 return x * x + y * y + z * z;
160 }
161
166 {
167 const T magnitude{getMagnitude()};
168 if (util::isZero(magnitude))
169 {
170 return;
171 }
172
173 const T inv_magnitude{static_cast<T>(1.0) / magnitude};
174 x *= inv_magnitude;
175 y *= inv_magnitude;
176 z *= inv_magnitude;
177 }
178
182 Vector3 getNormalized() const
183 {
184 return Vector3(x, y, z).normalize();
185 }
186
190 T dot(const Vector3& rhs) const noexcept
191 {
192 return x * rhs.x + y * rhs.y + z * rhs.z;
193 }
194
198 Vector3 cross(const Vector3& rhs) const noexcept
199 {
200 return Vector3(y * rhs.z - z *rhs.y,
201 z * rhs.x - x * rhs.z,
202 x * rhs.y - y * rhs.x);
203 }
204
205 // Static members.
206
210 static const Vector3 ZERO;
211 };
212
213 // Static member definitions.
214
215 template <std::floating_point T>
216 const Vector3<T> Vector3<T>::ZERO{0, 0, 0};
217
218 // Type aliases.
219
224
229
234} // namespace soo.
3D vector class.
Definition vector3.h:22
Vector3 getNormalized() const
Definition vector3.h:182
T dot(const Vector3 &rhs) const noexcept
Definition vector3.h:190
static const Vector3 ZERO
Definition vector3.h:210
std::string toString() const
Definition vector3.h:139
void normalize()
Definition vector3.h:165
T getMagnitudeSquared() const noexcept
Definition vector3.h:157
T getMagnitude() const
Definition vector3.h:149
Vector3 cross(const Vector3 &rhs) const noexcept
Definition vector3.h:198
Definition exception.h:15
bool isZero(const T num, const T tolerance=DEFAULT_TOLERANCE)
Definition util.h:66
bool isEqual(const T a, const T b, const T tolerance=DEFAULT_TOLERANCE)
Definition util.h:23
Namespace for math-on-demand.
Definition exception.h:13
Vector3< long double > Vector3L
Definition vector3.h:233
Vector3< double > Vector3d
Definition vector3.h:228
Vector3< float > Vector3f
Definition vector3.h:223