t
Loading...
Searching...
No Matches
Vector3.hpp
1#include "primitives/BufferAttribute.hpp"
2#include <algorithm>
3#include <cmath>
4#include <stdexcept>
5
6#ifndef VECTOR3_HPP
7#define VECTOR3_HPP
8
9namespace t {
10
20class Vector3 {
21public:
22 double x;
23 double y;
24 double z;
35 static Vector3
37 int index) {
38 return Vector3(
39 bufferAttribute.array.at(index * bufferAttribute.itemSize),
40 bufferAttribute.array.at(index * bufferAttribute.itemSize + 1),
41 bufferAttribute.array.at(index * bufferAttribute.itemSize + 2));
42 }
43
52 // TODO: make this method an instance method
53 static Vector3 cross(const Vector3 &a, const Vector3 &b) {
54 return Vector3(a.y * b.z - b.y * a.z, a.z * b.x - b.z * a.x,
55 a.x * b.y - b.x * a.y);
56 }
57
66 // TODO: make this method an instance method
67 static double dot(const Vector3 &a, const Vector3 &b) {
68 return a.x * b.x + a.y * b.y + a.z * b.z;
69 }
70
83 static Vector3 reflect(const Vector3 &incident,
84 const Vector3 &surfaceOrientation) {
85 return incident -
86 2.0 * dot(surfaceOrientation, incident) * surfaceOrientation;
87 }
88
96 Vector3(double _x, double _y, double _z) : x(_x), y(_y), z(_z) {}
97
103 Vector3 clone() const { return Vector3(x, y, z); }
104
111 Vector3 &copy(const Vector3 &source) {
112 x = source.x;
113 y = source.y;
114 z = source.z;
115
116 return *this;
117 }
118
126 Vector3 &set(double _x, double _y, double _z) {
127 x = _x;
128 y = _y;
129 z = _z;
130
131 return *this;
132 }
133
142 double length() const { return std::sqrt(x * x + y * y + z * z); }
143
149 Vector3 unit() const { return *this / length(); }
150
160 *this /= length();
161 return *this;
162 }
163
172 return Vector3(std::abs(x), std::abs(y), std::abs(z));
173 }
174
184 x = std::abs(x);
185 y = std::abs(y);
186 z = std::abs(z);
187
188 return *this;
189 }
190
199 Vector3 &clamp(const Vector3 &min, const Vector3 &max) {
200 x = std::clamp(x, min.x, max.x);
201 y = std::clamp(y, min.y, max.y);
202 z = std::clamp(z, min.z, max.z);
203
204 return *this;
205 }
206
215 Vector3 &clamp(double min, double max) {
216 x = std::clamp(x, min, max);
217 y = std::clamp(y, min, max);
218 z = std::clamp(z, min, max);
219
220 return *this;
221 }
222
230 double operator[](std::size_t index) const {
231 switch (index) {
232 case 0:
233 return x;
234 case 1:
235 return y;
236 case 2:
237 return z;
238 default:
239 throw std::invalid_argument("Index to Vector3 must be 0, 1, or 2.");
240 }
241 }
242
250 x += v.x;
251 y += v.y;
252 z += v.z;
253
254 return *this;
255 }
256
263 Vector3 &operator+=(double s) {
264 x += s;
265 y += s;
266 z += s;
267
268 return *this;
269 }
270
278 x -= v.x;
279 y -= v.y;
280 z -= v.z;
281
282 return *this;
283 }
284
291 Vector3 &operator-=(double s) {
292 x -= s;
293 y -= s;
294 z -= s;
295
296 return *this;
297 }
298
310 x *= v.x;
311 y *= v.y;
312 z *= v.z;
313
314 return *this;
315 }
316
323 Vector3 &operator*=(double s) {
324 x *= s;
325 y *= s;
326 z *= s;
327
328 return *this;
329 }
330
339 x /= v.x;
340 y /= v.y;
341 z /= v.z;
342
343 return *this;
344 }
345
352 Vector3 &operator/=(double s) {
353 x /= s;
354 y /= s;
355 z /= s;
356
357 return *this;
358 }
359
365 friend bool operator==(const Vector3 &a, const Vector3 &b) = default;
366
372 friend Vector3 operator-(const Vector3 &a) { return a * -1; }
373
381 friend Vector3 operator+(const Vector3 &a, const Vector3 &b) {
382 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
383 }
384
392 friend Vector3 operator+(const Vector3 &a, double s) {
393 return Vector3(a.x + s, a.y + s, a.z + s);
394 }
395
403 friend Vector3 operator+(double s, const Vector3 &a) { return a + s; }
404
412 friend Vector3 operator-(const Vector3 &a, const Vector3 &b) {
413 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
414 }
415
424 friend Vector3 operator-(const Vector3 &a, double s) {
425 return Vector3(a.x - s, a.y - s, a.z - s);
426 }
427
438 friend Vector3 operator-(double s, const Vector3 &a) {
439 return Vector3(s - a.x, s - a.y, s - a.z);
440 }
441
451 friend Vector3 operator*(const Vector3 &a, const Vector3 &b) {
452 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
453 }
454
463 friend Vector3 operator*(const Vector3 &a, double s) {
464 return Vector3(a.x * s, a.y * s, a.z * s);
465 }
466
475 friend Vector3 operator*(double s, const Vector3 &a) { return a * s; }
476
484 friend Vector3 operator/(const Vector3 &a, const Vector3 &b) {
485 return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
486 }
487
495 friend Vector3 operator/(const Vector3 &a, double s) {
496 return Vector3(a.x / s, a.y / s, a.z / s);
497 }
498
508 friend Vector3 operator/(double s, const Vector3 &a) {
509 return Vector3(s / a.x, s / a.y, s / a.z);
510 }
511};
512
513} // namespace t
514
515#endif // VECTOR3_HPP
A class representing attributes, such as vertex positions, face indices, and vertex normals.
Definition BufferAttribute.hpp:27
int itemSize
The number of values of the array that are associated with a particular vertex e.g.
Definition BufferAttribute.hpp:31
std::vector< BufferType > array
The array holding the data of the attribute.
Definition BufferAttribute.hpp:30
The 3D vector class.
Definition Vector3.hpp:20
Vector3 & operator/=(const Vector3 &v)
Divides the components of this vector by the corresponding components of another 3D vector.
Definition Vector3.hpp:338
double length() const
Returns the length of this 3D vector.
Definition Vector3.hpp:142
friend bool operator==(const Vector3 &a, const Vector3 &b)=default
Returns whether if two 3D vectors are equal.
friend Vector3 operator-(const Vector3 &a)
Returns the negation of this 3D vector.
Definition Vector3.hpp:372
Vector3 clone() const
Returns a copy of this 3D vector.
Definition Vector3.hpp:103
friend Vector3 operator+(const Vector3 &a, const Vector3 &b)
Adds two 3D vectors.
Definition Vector3.hpp:381
friend Vector3 operator-(double s, const Vector3 &a)
Subtracts a 3D vector from a constant 3D vector.
Definition Vector3.hpp:438
friend Vector3 operator/(double s, const Vector3 &a)
Returns the element-wise division of a constant 3D vector and a 3D vector.
Definition Vector3.hpp:508
Vector3 absolute() const
Returns a new 3D vector with the absolute values of the components of this 3D vector.
Definition Vector3.hpp:171
friend Vector3 operator-(const Vector3 &a, const Vector3 &b)
Subtracts two 3D vectors.
Definition Vector3.hpp:412
friend Vector3 operator+(const Vector3 &a, double s)
Adds a constant to all components of a 3D vector.
Definition Vector3.hpp:392
static double dot(const Vector3 &a, const Vector3 &b)
Returns the dot product of two vectors.
Definition Vector3.hpp:67
static Vector3 cross(const Vector3 &a, const Vector3 &b)
Returns the cross product of two vectors.
Definition Vector3.hpp:53
double z
The z component of this 3D vector.
Definition Vector3.hpp:24
Vector3 & clamp(double min, double max)
Clamps the components of this 3D vector to the specified minimum and maximum values.
Definition Vector3.hpp:215
Vector3 & operator+=(const Vector3 &v)
Adds the specified 3D vector to this vector.
Definition Vector3.hpp:249
Vector3 & operator/=(double s)
Divides the components of this 3D vector by the specified constant.
Definition Vector3.hpp:352
friend Vector3 operator/(const Vector3 &a, const Vector3 &b)
Returns the element-wise division of two 3D vectors.
Definition Vector3.hpp:484
Vector3 & clamp(const Vector3 &min, const Vector3 &max)
Clamps the components of this 3D vector to the specified minimum and maximum 3D vectors component-wis...
Definition Vector3.hpp:199
Vector3 & normalize()
Normalizes this 3D vector.
Definition Vector3.hpp:159
friend Vector3 operator*(const Vector3 &a, double s)
Multiplies all components of a 3D vector with a constant.
Definition Vector3.hpp:463
Vector3 & operator-=(double s)
Subtracts the specified constant from all components in this 3D vector.
Definition Vector3.hpp:291
Vector3 & abs()
Sets the components of this 3D vector to their absolute values.
Definition Vector3.hpp:183
double y
The y component of this 3D vector.
Definition Vector3.hpp:23
Vector3 & operator-=(const Vector3 &v)
Subtracts the specified 3D vector from this vector.
Definition Vector3.hpp:277
friend Vector3 operator*(const Vector3 &a, const Vector3 &b)
Returns the element-wise product of two 3D vectors, also known as the Hadamard product.
Definition Vector3.hpp:451
friend Vector3 operator/(const Vector3 &a, double s)
Divides the components of a 3D vector by a constant.
Definition Vector3.hpp:495
static Vector3 reflect(const Vector3 &incident, const Vector3 &surfaceOrientation)
Returns the reflection of an incident vector off a surface that has the specified orientation.
Definition Vector3.hpp:83
Vector3 unit() const
Returns the unit vector of this 3D vector.
Definition Vector3.hpp:149
Vector3 & operator+=(double s)
Adds the specified constant to all components in this 3D vector.
Definition Vector3.hpp:263
Vector3 & set(double _x, double _y, double _z)
Sets the components of this vector.
Definition Vector3.hpp:126
double x
The x component of this 3D vector.
Definition Vector3.hpp:22
Vector3 & operator*=(const Vector3 &v)
Multiplies the components of this vector with the corresponding components of the specified 3D vector...
Definition Vector3.hpp:309
friend Vector3 operator*(double s, const Vector3 &a)
Multiplies all components of a 3D vector with a constant.
Definition Vector3.hpp:475
static Vector3 fromBufferAttribute(const BufferAttribute< double > &bufferAttribute, int index)
Returns 3 consecutive values at the specified index in a BufferAttribute at the specified in a Vector...
Definition Vector3.hpp:36
Vector3 & copy(const Vector3 &source)
Copies the components of the specified 3D vector to this 3D vector.
Definition Vector3.hpp:111
friend Vector3 operator+(double s, const Vector3 &a)
Adds a constant to all components of a 3D vector.
Definition Vector3.hpp:403
Vector3 & operator*=(double s)
Multiplies all components of this 3D vector with the specified constant.
Definition Vector3.hpp:323
Vector3(double _x, double _y, double _z)
Creates a new 3D vector with the specified components.
Definition Vector3.hpp:96
double operator[](std::size_t index) const
Returns the component at the specified index of this 3D vector.
Definition Vector3.hpp:230
friend Vector3 operator-(const Vector3 &a, double s)
Subtracts a constant from all components of a 3D vector.
Definition Vector3.hpp:424
The t software 3D graphics library namespace.
Definition algorithms.hpp:12