HighMap library (C++)
Loading...
Searching...
No Matches
algebra.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
22#pragma once
23#include <cmath>
24#include <stdexcept>
25#include <vector>
26
27namespace hmap
28{
29
39template <typename T> struct Vec2
40{
41 T x, y;
48 Vec2() : x(0), y(0)
49 {
50 }
51
59 Vec2(T x, T y) : x(x), y(y)
60 {
61 }
62
77 Vec2(const std::vector<T> &vec) : x(0), y(0)
78 {
79 if (vec.size() != 2)
80 {
81 throw std::invalid_argument("Vector must contain exactly two elements.");
82 }
83 x = vec[0];
84 y = vec[1];
85 }
86
95 bool operator==(const Vec2 &other_vec) const
96 {
97 return ((this->x == other_vec.x) && (this->y == other_vec.y));
98 }
99
108 bool operator!=(const Vec2 &other_vec) const
109 {
110 return ((this->x != other_vec.x) || (this->y != other_vec.y));
111 }
112
122 Vec2 &operator/=(const T value)
123 {
124 this->x /= value;
125 this->y /= value;
126 return *this;
127 }
128
138 Vec2 operator/(const Vec2 &other_vec) const
139 {
140 Vec2 out;
141 out.x = this->x / other_vec.x;
142 out.y = this->y / other_vec.y;
143 return out;
144 }
145
156 Vec2 operator*(const Vec2 &other_vec) const
157 {
158 Vec2 out;
159 out.x = this->x * other_vec.x;
160 out.y = this->y * other_vec.y;
161 return out;
162 }
163
173 Vec2 operator+(const Vec2 &other_vec) const
174 {
175 Vec2 out;
176 out.x = this->x + other_vec.x;
177 out.y = this->y + other_vec.y;
178 return out;
179 }
180
190 Vec2 operator-(const Vec2 &other_vec) const
191 {
192 Vec2 out;
193 out.x = this->x - other_vec.x;
194 out.y = this->y - other_vec.y;
195 return out;
196 }
197
207 Vec2 operator*(T scalar) const
208 {
209 return Vec2(x * scalar, y * scalar);
210 }
211
224 friend Vec2 operator*(T scalar, const Vec2 &vec)
225 {
226 return Vec2(scalar * vec.x, scalar * vec.y);
227 }
228
239 friend float dot(const Vec2 v1, const Vec2 v2)
240 {
241 return v1.x * v2.x + v1.y * v2.y;
242 }
243
249 T magnitude() const
250 {
251 return std::sqrt(x * x + y * y);
252 }
253
261 {
262 T mag = magnitude();
263 if (mag > 0) // Avoid division by zero
264 {
265 x /= mag;
266 y /= mag;
267 }
268 }
269};
270
280template <typename T> struct Vec3
281{
282 T x, y, z;
289 Vec3() : x(0), y(0), z(0)
290 {
291 }
292
301 Vec3(T x, T y, T z) : x(x), y(y), z(z)
302 {
303 }
304
321 Vec3(const std::vector<T> &vec) : x(0), y(0), z(0)
322 {
323 if (vec.size() != 3)
324 {
325 throw std::invalid_argument(
326 "Vector must contain exactly three elements.");
327 }
328 x = vec[0];
329 y = vec[1];
330 z = vec[2];
331 }
332
341 bool operator==(const Vec3 &other_vec) const
342 {
343 return ((this->x == other_vec.x) && (this->y == other_vec.y) &&
344 (this->z == other_vec.z));
345 }
346
355 bool operator!=(const Vec3 &other_vec) const
356 {
357 return ((this->x != other_vec.x) || (this->y != other_vec.y) ||
358 (this->z != other_vec.z));
359 }
360
370 Vec3 &operator/=(const T value)
371 {
372 this->x /= value;
373 this->y /= value;
374 this->z /= value;
375 return *this;
376 }
377
387 Vec3 operator/(const Vec3 &other_vec) const
388 {
389 Vec3 out;
390 out.x = this->x / other_vec.x;
391 out.y = this->y / other_vec.y;
392 out.z = this->z / other_vec.z;
393 return out;
394 }
395
406 Vec3 operator*(const Vec3 &other_vec) const
407 {
408 Vec3 out;
409 out.x = this->x * other_vec.x;
410 out.y = this->y * other_vec.y;
411 out.z = this->z * other_vec.z;
412 return out;
413 }
414
424 Vec3 operator+(const Vec3 &other_vec) const
425 {
426 Vec3 out;
427 out.x = this->x + other_vec.x;
428 out.y = this->y + other_vec.y;
429 out.z = this->z + other_vec.z;
430 return out;
431 }
432
442 Vec3 operator-(const Vec3 &other_vec) const
443 {
444 Vec3 out;
445 out.x = this->x - other_vec.x;
446 out.y = this->y - other_vec.y;
447 out.z = this->z - other_vec.z;
448 return out;
449 }
450
460 Vec3 operator*(T scalar) const
461 {
462 return Vec3(x * scalar, y * scalar, z * scalar);
463 }
464
477 friend Vec3 operator*(T scalar, const Vec3 &vec)
478 {
479 return Vec3(scalar * vec.x, scalar * vec.y, scalar * vec.z);
480 }
481
492 friend Vec3 cross(const Vec3 v1, const Vec3 v2)
493 {
494 Vec3 out;
495 out.x = v1.y * v2.z - v1.z * v2.y;
496 out.y = v1.z * v2.x - v1.x * v2.z;
497 out.z = v1.x * v2.y - v1.y * v2.x;
498 return out;
499 }
500
511 friend float dot(const Vec3 v1, const Vec3 v2)
512 {
513 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
514 }
515
521 T magnitude() const
522 {
523 return std::sqrt(x * x + y * y + z * z);
524 }
525
533 {
534 T mag = magnitude();
535 if (mag > 0) // Avoid division by zero
536 {
537 x /= mag;
538 y /= mag;
539 z /= mag;
540 }
541 }
542
548 T sum() const
549 {
550 return x + y + z;
551 }
552};
553
563template <typename T> struct Vec4
564{
565 T a, b, c, d;
573 Vec4() : a(0), b(0), c(0), d(0)
574 {
575 }
576
586 Vec4(T a, T b, T c, T d) : a(a), b(b), c(c), d(d)
587 {
588 }
589
607 Vec4(const std::vector<T> &vec) : a(0), b(0), c(0), d(0)
608 {
609 if (vec.size() != 4)
610 {
611 throw std::invalid_argument("Vector must contain exactly four elements.");
612 }
613 a = vec[0];
614 b = vec[1];
615 c = vec[2];
616 d = vec[3];
617 }
618
627 bool operator==(const Vec4 &other_vec) const
628 {
629 return ((this->a == other_vec.a) && (this->b == other_vec.b) &&
630 (this->c == other_vec.c) && (this->d == other_vec.d));
631 }
632
641 bool operator!=(const Vec4 &other_vec) const
642 {
643 return ((this->a != other_vec.a) || (this->b != other_vec.b) ||
644 (this->c != other_vec.c) || (this->d != other_vec.d));
645 }
646
656 Vec4 &operator/=(const T value)
657 {
658 this->a /= value;
659 this->b /= value;
660 this->c /= value;
661 this->d /= value;
662 return *this;
663 }
664
674 Vec4 operator/(const Vec4 &other_vec) const
675 {
676 Vec4 out;
677 out.a = this->a / other_vec.a;
678 out.b = this->b / other_vec.b;
679 out.c = this->c / other_vec.c;
680 out.d = this->d / other_vec.d;
681 return out;
682 }
683
694 Vec4 operator*(const Vec4 &other_vec) const
695 {
696 Vec4 out;
697 out.a = this->a * other_vec.a;
698 out.b = this->b * other_vec.b;
699 out.c = this->c * other_vec.c;
700 out.d = this->d * other_vec.d;
701 return out;
702 }
703
713 Vec4 operator+(const Vec4 &other_vec) const
714 {
715 Vec4 out;
716 out.a = this->a + other_vec.a;
717 out.b = this->b + other_vec.b;
718 out.c = this->c + other_vec.c;
719 out.d = this->d + other_vec.d;
720 return out;
721 }
722
732 Vec4 operator-(const Vec4 &other_vec) const
733 {
734 Vec4 out;
735 out.a = this->a - other_vec.a;
736 out.b = this->b - other_vec.b;
737 out.c = this->c - other_vec.c;
738 out.d = this->d - other_vec.d;
739 return out;
740 }
741
751 Vec4 operator*(T scalar) const
752 {
753 return Vec4(a * scalar, b * scalar, c * scalar, d * scalar);
754 }
755
768 friend Vec4 operator*(T scalar, const Vec4 &vec)
769 {
770 return Vec4(scalar * vec.a, scalar * vec.b, scalar * vec.c, scalar * vec.d);
771 }
772
786 Vec4<T> adjust(float da, float db, float dc, float dd)
787 {
788 return Vec4<T>(this->a + da, this->b + db, this->c + dc, this->d + dd);
789 }
790
801 friend float dot(const Vec4 v1, const Vec4 v2)
802 {
803 return v1.a * v2.a + v1.b * v2.b + v1.c * v2.c + v1.d * v2.d;
804 }
805};
806
816template <typename T> struct Mat
817{
818 std::vector<T> vector;
834 {
835 this->vector.resize(shape.x * shape.y);
836 }
837
848 T &operator()(int i, int j)
849 {
850 return this->vector[i * this->shape.y + j];
851 }
852
864 const T &operator()(int i, int j) const
865 {
866 return this->vector[i * this->shape.y + j];
867 }
868};
869
882template <typename T> Vec3<T> normalized_vec3(T x, T y, T z)
883{
884 Vec3<T> v = Vec3<T>(x, y, z);
885 v.normalize();
886 return v;
887}
888
889} // namespace hmap
Definition algebra.hpp:28
Vec3< T > normalized_vec3(T x, T y, T z)
Constructs a normalized 3D vector.
Definition algebra.hpp:882
Mat class for basic manipulation of 2D matrices.
Definition algebra.hpp:817
Mat(Vec2< int > shape)
Constructor to initialize a matrix with a given shape.
Definition algebra.hpp:833
Vec2< int > shape
Dimensions of the matrix (rows x columns).
Definition algebra.hpp:820
T & operator()(int i, int j)
Access operator to get a reference to the element at (i, j).
Definition algebra.hpp:848
std::vector< T > vector
1D vector storing matrix elements in row-major order.
Definition algebra.hpp:818
const T & operator()(int i, int j) const
Const access operator to get the value of the element at (i, j).
Definition algebra.hpp:864
Vec2 class for basic manipulation of 2D vectors.
Definition algebra.hpp:40
T y
The x and y components of the vector.
Definition algebra.hpp:41
Vec2 operator/(const Vec2 &other_vec) const
Division operator.
Definition algebra.hpp:138
bool operator==(const Vec2 &other_vec) const
Equality operator.
Definition algebra.hpp:95
Vec2 operator+(const Vec2 &other_vec) const
Addition operator.
Definition algebra.hpp:173
T x
Definition algebra.hpp:41
bool operator!=(const Vec2 &other_vec) const
Inequality operator.
Definition algebra.hpp:108
void normalize()
Normalize the vector to have a magnitude of 1.
Definition algebra.hpp:260
Vec2 operator*(T scalar) const
Scalar multiplication (Vec2 * scalar).
Definition algebra.hpp:207
Vec2(const std::vector< T > &vec)
Constructs a Vec2 object from a std::vector.
Definition algebra.hpp:77
friend Vec2 operator*(T scalar, const Vec2 &vec)
Scalar multiplication (scalar * Vec2).
Definition algebra.hpp:224
Vec2 operator-(const Vec2 &other_vec) const
Subtraction operator.
Definition algebra.hpp:190
T magnitude() const
Calculate the magnitude (length) of the vector.
Definition algebra.hpp:249
friend float dot(const Vec2 v1, const Vec2 v2)
Friend function to calculate the dot product of two vectors.
Definition algebra.hpp:239
Vec2()
Default constructor initializing the vector to (0, 0).
Definition algebra.hpp:48
Vec2 operator*(const Vec2 &other_vec) const
Multiplication operator.
Definition algebra.hpp:156
Vec2(T x, T y)
Parameterized constructor initializing the vector to given values.
Definition algebra.hpp:59
Vec2 & operator/=(const T value)
Division-assignment operator.
Definition algebra.hpp:122
Vec3 class for basic manipulation of 3D vectors.
Definition algebra.hpp:281
bool operator==(const Vec3 &other_vec) const
Equality operator.
Definition algebra.hpp:341
Vec3 operator-(const Vec3 &other_vec) const
Subtraction operator.
Definition algebra.hpp:442
friend Vec3 cross(const Vec3 v1, const Vec3 v2)
Friend function to calculate the cross product of two vectors.
Definition algebra.hpp:492
Vec3 operator*(const Vec3 &other_vec) const
Multiplication operator.
Definition algebra.hpp:406
T y
Definition algebra.hpp:282
Vec3 operator*(T scalar) const
Scalar multiplication (Vec3 * scalar).
Definition algebra.hpp:460
T sum() const
Calculate the sum of the vector components.
Definition algebra.hpp:548
Vec3 operator+(const Vec3 &other_vec) const
Addition operator.
Definition algebra.hpp:424
T z
The x, y, and z components of the vector.
Definition algebra.hpp:282
Vec3(T x, T y, T z)
Parameterized constructor initializing the vector to given values.
Definition algebra.hpp:301
void normalize()
Normalize the vector to have a magnitude of 1.
Definition algebra.hpp:532
Vec3(const std::vector< T > &vec)
Constructs a Vec3 object from a std::vector.
Definition algebra.hpp:321
Vec3()
Default constructor initializing the vector to (0, 0, 0).
Definition algebra.hpp:289
T magnitude() const
Calculate the magnitude (length) of the vector.
Definition algebra.hpp:521
Vec3 & operator/=(const T value)
Division-assignment operator.
Definition algebra.hpp:370
friend Vec3 operator*(T scalar, const Vec3 &vec)
Scalar multiplication (scalar * Vec3).
Definition algebra.hpp:477
T x
Definition algebra.hpp:282
friend float dot(const Vec3 v1, const Vec3 v2)
Friend function to calculate the dot product of two vectors.
Definition algebra.hpp:511
bool operator!=(const Vec3 &other_vec) const
Inequality operator.
Definition algebra.hpp:355
Vec3 operator/(const Vec3 &other_vec) const
Division operator.
Definition algebra.hpp:387
Vec4 class for basic manipulation of 4D vectors.
Definition algebra.hpp:564
Vec4< T > adjust(float da, float db, float dc, float dd)
Adjusts the components of the vector by the given offsets.
Definition algebra.hpp:786
Vec4(const std::vector< T > &vec)
Constructs a Vec4 object from a std::vector.
Definition algebra.hpp:607
Vec4 & operator/=(const T value)
Division-assignment operator.
Definition algebra.hpp:656
bool operator==(const Vec4 &other_vec) const
Equality operator.
Definition algebra.hpp:627
friend Vec4 operator*(T scalar, const Vec4 &vec)
Scalar multiplication (scalar * Vec4).
Definition algebra.hpp:768
friend float dot(const Vec4 v1, const Vec4 v2)
Friend function to calculate the dot product of two vectors.
Definition algebra.hpp:801
Vec4 operator*(const Vec4 &other_vec) const
Multiplication operator.
Definition algebra.hpp:694
Vec4 operator+(const Vec4 &other_vec) const
Addition operator.
Definition algebra.hpp:713
T b
Definition algebra.hpp:565
Vec4 operator-(const Vec4 &other_vec) const
Subtraction operator.
Definition algebra.hpp:732
bool operator!=(const Vec4 &other_vec) const
Inequality operator.
Definition algebra.hpp:641
T d
The a, b, c, and d components of the vector.
Definition algebra.hpp:565
T a
Definition algebra.hpp:565
Vec4 operator*(T scalar) const
Scalar multiplication (Vec4 * scalar).
Definition algebra.hpp:751
T c
Definition algebra.hpp:565
Vec4 operator/(const Vec4 &other_vec) const
Division operator.
Definition algebra.hpp:674
Vec4(T a, T b, T c, T d)
Parameterized constructor initializing the vector to given values.
Definition algebra.hpp:586
Vec4()
Default constructor initializing the vector to (0, 0, 0, 0).
Definition algebra.hpp:573