t
Loading...
Searching...
No Matches
Vector4.hpp
1#include "math/Vector3.hpp"
2
3#ifndef VECTOR4_HPP
4#define VECTOR4_HPP
5
6namespace t {
7
8#define m matrix4.elements
9#define v vector4
10
20class Vector4 {
21public:
22 double x;
23 double y;
24 double z;
25 double w;
35 Vector4(double _x, double _y, double _z, double _w)
36 : x(_x), y(_y), z(_z), w(_w) {}
37
45 Vector4(const Vector3 &vector3, double w)
46 : Vector4(vector3.x, vector3.y, vector3.z, w) {}
47
53 Vector4 clone() const { return Vector4(x, y, z, w); }
54
61 Vector4 &copy(const Vector4 &source) {
62 x = source.x;
63 y = source.y;
64 z = source.z;
65 w = source.w;
66
67 return *this;
68 }
69
79 Vector4 &set(double _x, double _y, double _z, double _w) {
80 x = _x;
81 y = _y;
82 z = _z;
83 w = _w;
84
85 return *this;
86 }
87
98 Vector3 toVector3() const { return Vector3(x, y, z) / w; }
99
108 double length() const { return std::sqrt(x * x + y * y + z * z + w * w); }
109
115 Vector4 unit() const { return *this / length(); }
116
126 *this /= length();
127 return *this;
128 }
129
138 return Vector4(std::abs(x), std::abs(y), std::abs(z), std::abs(w));
139 }
140
150 x = std::abs(x);
151 y = std::abs(y);
152 z = std::abs(z);
153 w = std::abs(w);
154
155 return *this;
156 }
157
166 Vector4 &clamp(const Vector4 &min, const Vector4 &max) {
167 x = std::clamp(x, min.x, max.x);
168 y = std::clamp(y, min.y, max.y);
169 z = std::clamp(z, min.z, max.z);
170 w = std::clamp(w, min.w, max.w);
171
172 return *this;
173 }
174
183 Vector4 &clamp(double min, double max) {
184 x = std::clamp(x, min, max);
185 y = std::clamp(y, min, max);
186 z = std::clamp(z, min, max);
187 w = std::clamp(w, min, max);
188
189 return *this;
190 }
191
199 double operator[](std::size_t index) const {
200 switch (index) {
201 case 0:
202 return x;
203 case 1:
204 return y;
205 case 2:
206 return z;
207 case 3:
208 return w;
209 default:
210 throw std::invalid_argument("Index to Vector4 must be 0, 1, 2, or 3.");
211 }
212 }
213
221 x += v.x;
222 y += v.y;
223 z += v.z;
224 w += v.w;
225
226 return *this;
227 }
228
235 Vector4 &operator+=(double s) {
236 x += s;
237 y += s;
238 z += s;
239 w += s;
240
241 return *this;
242 }
243
251 x -= v.x;
252 y -= v.y;
253 z -= v.z;
254 w -= v.w;
255
256 return *this;
257 }
258
265 Vector4 &operator-=(double s) {
266 x -= s;
267 y -= s;
268 z -= s;
269 w -= s;
270
271 return *this;
272 }
273
282 x *= v.x;
283 y *= v.y;
284 z *= v.z;
285 w *= v.w;
286
287 return *this;
288 }
289
296 Vector4 &operator*=(double s) {
297 x *= s;
298 y *= s;
299 z *= s;
300 w *= s;
301
302 return *this;
303 }
304
313 x /= v.x;
314 y /= v.y;
315 z /= v.z;
316 w /= v.w;
317
318 return *this;
319 }
320
327 Vector4 &operator/=(double s) {
328 x /= s;
329 y /= s;
330 z /= s;
331 w /= s;
332
333 return *this;
334 }
335
341 friend bool operator==(const Vector4 &a, const Vector4 &b) = default;
342
348 friend Vector4 operator-(const Vector4 &a) { return a * -1; }
349
357 friend Vector4 operator+(const Vector4 &a, const Vector4 &b) {
358 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
359 }
360
368 friend Vector4 operator+(const Vector4 &a, double s) {
369 return Vector4(a.x + s, a.y + s, a.z + s, a.w + s);
370 }
371
379 friend Vector4 operator+(double s, const Vector4 &a) { return a + s; }
380
388 friend Vector4 operator-(const Vector4 &a, const Vector4 &b) {
389 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
390 }
391
400 friend Vector4 operator-(const Vector4 &a, double s) {
401 return Vector4(a.x - s, a.y - s, a.z - s, a.w - s);
402 }
403
414 friend Vector4 operator-(double s, const Vector4 &a) {
415 return Vector4(s - a.x, s - a.y, s - a.z, s - a.w);
416 }
417
427 friend Vector4 operator*(const Vector4 &a, const Vector4 &b) {
428 return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
429 }
430
439 friend Vector4 operator*(const Vector4 &a, double s) {
440 return Vector4(a.x * s, a.y * s, a.z * s, a.w * s);
441 }
442
451 friend Vector4 operator*(double s, const Vector4 &a) { return a * s; }
452
460 friend Vector4 operator/(const Vector4 &a, const Vector4 &b) {
461 return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
462 }
463
471 friend Vector4 operator/(const Vector4 &a, double s) {
472 return Vector4(a.x / s, a.y / s, a.z / s, a.w / s);
473 }
474
484 friend Vector4 operator/(double s, const Vector4 &a) {
485 return Vector4(s / a.x, s / a.y, s / a.z, s / a.w);
486 }
487};
488
489#undef m
490#undef v
491
492} // namespace t
493
494#endif // VECTOR4_HPP
The 3D vector class.
Definition Vector3.hpp:20
The 4D vector class.
Definition Vector4.hpp:20
double y
The y component of this 4D vector.
Definition Vector4.hpp:23
Vector4(double _x, double _y, double _z, double _w)
Creates a new 4D vector with the specified components.
Definition Vector4.hpp:35
Vector4 & operator*=(const Vector4 &v)
Multiplies the components of this vector with the corresponding components of the specified 4D vector...
Definition Vector4.hpp:281
Vector4 & operator-=(double s)
Subtracts the specified constant from all components in this 4D vector.
Definition Vector4.hpp:265
Vector4 & operator/=(const Vector4 &v)
Divides the components of this vector by the corresponding components of another 4D vector.
Definition Vector4.hpp:312
friend Vector4 operator/(const Vector4 &a, double s)
Divides the components of a 4D vector by a constant.
Definition Vector4.hpp:471
Vector4 & abs()
Sets the components of this 4D vector to their absolute values.
Definition Vector4.hpp:149
friend Vector4 operator+(double s, const Vector4 &a)
Adds a constant to all components of a 4D vector.
Definition Vector4.hpp:379
Vector4 & copy(const Vector4 &source)
Copies the components of the specified 4D vector to this 4D vector.
Definition Vector4.hpp:61
double operator[](std::size_t index) const
Returns the component at the specified index of this 4D vector.
Definition Vector4.hpp:199
Vector4 & operator/=(double s)
Divides the components of this 4D vector by the specified constant.
Definition Vector4.hpp:327
friend Vector4 operator*(const Vector4 &a, const Vector4 &b)
Returns the element-wise product of two 4D vectors, also known as the Hadamard product.
Definition Vector4.hpp:427
Vector4 unit() const
Returns the unit vector of this 4D vector.
Definition Vector4.hpp:115
Vector4 clone() const
Returns a copy of this 4D vector.
Definition Vector4.hpp:53
Vector4 & normalize()
Normalizes this 4D vector.
Definition Vector4.hpp:125
Vector4(const Vector3 &vector3, double w)
Creates a new 4D vector with the same x, y, and z components as the specified 3D vector and the speci...
Definition Vector4.hpp:45
double x
The x component of this 4D vector.
Definition Vector4.hpp:22
Vector4 & clamp(const Vector4 &min, const Vector4 &max)
Clamps the components of this 4D vector to the specified minimum and maximum 4D vectors component-wis...
Definition Vector4.hpp:166
friend Vector4 operator-(const Vector4 &a, double s)
Subtracts a constant from all components of a 4D vector.
Definition Vector4.hpp:400
friend bool operator==(const Vector4 &a, const Vector4 &b)=default
Returns whether if two 4D vectors are equal.
Vector4 & operator*=(double s)
Multiplies all components of this 4D vector with the specified constant.
Definition Vector4.hpp:296
Vector4 absolute() const
Returns a new 4D vector with the absolute values of the components of this 4D vector.
Definition Vector4.hpp:137
friend Vector4 operator-(const Vector4 &a, const Vector4 &b)
Subtracts two 4D vectors.
Definition Vector4.hpp:388
Vector4 & operator+=(const Vector4 &v)
Adds the specified 4D vector to this vector.
Definition Vector4.hpp:220
friend Vector4 operator/(double s, const Vector4 &a)
Returns the element-wise division of a constant 4D vector and a 4D vector.
Definition Vector4.hpp:484
double z
The w component of this 4D vector.
Definition Vector4.hpp:24
friend Vector4 operator-(const Vector4 &a)
Returns the negation of this 4D vector.
Definition Vector4.hpp:348
friend Vector4 operator*(double s, const Vector4 &a)
Multiplies all components of a 4D vector with a constant.
Definition Vector4.hpp:451
Vector4 & set(double _x, double _y, double _z, double _w)
Sets the components of this 4D vector.
Definition Vector4.hpp:79
friend Vector4 operator-(double s, const Vector4 &a)
Subtracts a 4D vector from a constant 4D vector.
Definition Vector4.hpp:414
friend Vector4 operator/(const Vector4 &a, const Vector4 &b)
Returns the element-wise division of two 4D vectors.
Definition Vector4.hpp:460
Vector4 & operator-=(const Vector4 &v)
Subtracts the specified 4D vector from this vector.
Definition Vector4.hpp:250
Vector4 & clamp(double min, double max)
Clamps the components of this 4D vector to the specified minimum and maximum values.
Definition Vector4.hpp:183
Vector3 toVector3() const
Returns the 3D vector represented by the x, y, and z components of this 4D vector with perspective di...
Definition Vector4.hpp:98
friend Vector4 operator+(const Vector4 &a, double s)
Adds a constant to all components of a 4D vector.
Definition Vector4.hpp:368
friend Vector4 operator+(const Vector4 &a, const Vector4 &b)
Adds two 4D vectors.
Definition Vector4.hpp:357
Vector4 & operator+=(double s)
Adds the specified constant to all components in this 4D vector.
Definition Vector4.hpp:235
double length() const
Returns the length of this 4D vector.
Definition Vector4.hpp:108
friend Vector4 operator*(const Vector4 &a, double s)
Multiplies all components of a 4D vector with a constant.
Definition Vector4.hpp:439
double w
The z component of this 4D vector.
Definition Vector4.hpp:25
The t software 3D graphics library namespace.
Definition algorithms.hpp:12