simplevectors  0.3.9
Simple vector implementations in C++
embed.h
Go to the documentation of this file.
1 
38 #ifndef INCLUDE_SVECTOR_EMBED_HPP_
39 #define INCLUDE_SVECTOR_EMBED_HPP_
40 
41 #include <math.h> // acosf, atan2f, cosf, sinf, sqrtf
42 
43 namespace svector {
50 struct EmbVec2D {
56  EmbVec2D() : x{0}, y{0} {};
57 
64  EmbVec2D(const float xOther, const float yOther) : x{xOther}, y{yOther} {}
65 
69  EmbVec2D(const EmbVec2D &other) = default;
70 
76  EmbVec2D(EmbVec2D &&) noexcept = default;
77 
81  EmbVec2D &operator=(const EmbVec2D &other) {
82  // check if assigning to self
83  if (this == &other) {
84  return *this;
85  }
86 
87  x = other.x;
88  y = other.y;
89  return *this;
90  }
91 
97  EmbVec2D &operator=(EmbVec2D &&) noexcept = default;
98 
104  virtual ~EmbVec2D() = default;
105 
113  EmbVec2D &operator+=(const EmbVec2D &other) {
114  x += other.x;
115  y += other.y;
116  return *this;
117  }
118 
126  EmbVec2D &operator-=(const EmbVec2D &other) {
127  x -= other.x;
128  y -= other.y;
129  return *this;
130  }
131 
139  EmbVec2D &operator*=(const float &other) {
140  x *= other;
141  y *= other;
142  return *this;
143  }
144 
152  EmbVec2D &operator/=(const float &other) {
153  x /= other;
154  y /= other;
155  return *this;
156  }
157 
158  float x;
159  float y;
160 };
161 
168 struct EmbVec3D {
174  EmbVec3D() : x{0}, y{0}, z{0} {}
175 
183  EmbVec3D(const float xOther, const float yOther, const float zOther)
184  : x{xOther}, y{yOther}, z{zOther} {}
185 
189  EmbVec3D(const EmbVec3D &other) = default;
190 
196  EmbVec3D(EmbVec3D &&) noexcept = default;
197 
201  EmbVec3D &operator=(const EmbVec3D &other) {
202  // check if assigning to self
203  if (this == &other) {
204  return *this;
205  }
206 
207  x = other.x;
208  y = other.y;
209  z = other.z;
210  return *this;
211  }
212 
218  EmbVec3D &operator=(EmbVec3D &&) noexcept = default;
219 
225  virtual ~EmbVec3D() = default;
226 
234  EmbVec3D &operator+=(const EmbVec3D &other) {
235  x += other.x;
236  y += other.y;
237  z += other.z;
238  return *this;
239  }
240 
248  EmbVec3D &operator-=(const EmbVec3D &other) {
249  x -= other.x;
250  y -= other.y;
251  z -= other.z;
252  return *this;
253  }
254 
262  EmbVec3D &operator*=(const float &other) {
263  x *= other;
264  y *= other;
265  z *= other;
266  return *this;
267  }
268 
276  EmbVec3D &operator/=(const float &other) {
277  x /= other;
278  y /= other;
279  z /= other;
280  return *this;
281  }
282 
283  float x;
284  float y;
285  float z;
286 };
287 
295 inline float x(const EmbVec2D &v) { return v.x; }
296 
303 inline void x(EmbVec2D &v, const float xValue) { v.x = xValue; }
304 
312 inline float x(const EmbVec3D &v) { return v.x; }
313 
320 inline void x(EmbVec3D &v, const float xValue) { v.x = xValue; }
321 
329 inline float y(const EmbVec2D &v) { return v.y; }
330 
337 inline void y(EmbVec2D &v, const float yValue) { v.y = yValue; }
338 
346 inline float y(const EmbVec3D &v) { return v.y; }
347 
354 inline void y(EmbVec3D &v, const float yValue) { v.y = yValue; }
355 
363 inline float z(const EmbVec3D &v) { return v.z; }
364 
371 inline void z(EmbVec3D &v, const float zValue) { v.z = zValue; }
372 
384 inline EmbVec2D operator+(const EmbVec2D &lhs, const EmbVec2D &rhs) {
385  return EmbVec2D{lhs.x + rhs.x, lhs.y + rhs.y};
386 }
387 
399 inline EmbVec2D operator-(const EmbVec2D &lhs, const EmbVec2D &rhs) {
400  return EmbVec2D{lhs.x - rhs.x, lhs.y - rhs.y};
401 }
402 
414 inline EmbVec2D operator-(const EmbVec2D &vec) {
415  return EmbVec2D{-vec.x, -vec.y};
416 }
417 
428 inline EmbVec2D operator+(const EmbVec2D &vec) {
429  return EmbVec2D{+vec.x, +vec.y};
430 }
431 
443 inline EmbVec2D operator*(const EmbVec2D &lhs, const float rhs) {
444  return EmbVec2D{lhs.x * rhs, lhs.y * rhs};
445 }
446 
458 inline EmbVec2D operator/(const EmbVec2D &lhs, const float rhs) {
459  return EmbVec2D{lhs.x / rhs, lhs.y / rhs};
460 }
461 
470 inline bool operator==(const EmbVec2D &lhs, const EmbVec2D &rhs) {
471  return lhs.x == rhs.x && lhs.y == rhs.y;
472 }
473 
482 inline bool operator!=(const EmbVec2D &lhs, const EmbVec2D &rhs) {
483  return !(lhs == rhs);
484 }
485 
494 inline float dot(const EmbVec2D &lhs, const EmbVec2D &rhs) {
495  return lhs.x * rhs.x + lhs.y * rhs.y;
496 }
497 
505 inline float magn(const EmbVec2D &vec) {
506  return sqrtf(vec.x * vec.x + vec.y * vec.y);
507 }
508 
518 inline float angle(const EmbVec2D &vec) { return atan2f(vec.y, vec.x); }
519 
532 inline EmbVec2D normalize(const EmbVec2D &vec) { return vec / magn(vec); }
533 
539 inline bool isZero(const EmbVec2D &vec) { return magn(vec) == 0; }
540 
553 inline EmbVec2D rotate(const EmbVec2D &vec, const float ang) {
554  //
555  // Rotation matrix:
556  //
557  // | cos(ang) -sin(ang) | |x|
558  // | sin(ang) cos(ang) | |y|
559  //
560 
561  const auto xPrime = vec.x * cosf(ang) - vec.y * sinf(ang);
562  const auto yPrime = vec.x * sinf(ang) + vec.y * cosf(ang);
563 
564  return EmbVec2D{xPrime, yPrime};
565 }
566 
578 inline EmbVec3D operator+(const EmbVec3D &lhs, const EmbVec3D &rhs) {
579  return EmbVec3D{lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z};
580 }
581 
593 inline EmbVec3D operator-(const EmbVec3D &lhs, const EmbVec3D &rhs) {
594  return EmbVec3D{lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z};
595 }
596 
608 inline EmbVec3D operator-(const EmbVec3D &vec) {
609  return EmbVec3D{-vec.x, -vec.y, -vec.z};
610 }
611 
622 inline EmbVec3D operator+(const EmbVec3D &vec) {
623  return EmbVec3D{+vec.x, +vec.y, +vec.z};
624 }
625 
637 inline EmbVec3D operator*(const EmbVec3D &lhs, const float rhs) {
638  return EmbVec3D{lhs.x * rhs, lhs.y * rhs, lhs.z * rhs};
639 }
640 
652 inline EmbVec3D operator/(const EmbVec3D &lhs, const float rhs) {
653  return EmbVec3D{lhs.x / rhs, lhs.y / rhs, lhs.z / rhs};
654 }
655 
664 inline bool operator==(const EmbVec3D &lhs, const EmbVec3D &rhs) {
665  return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
666 }
667 
676 inline bool operator!=(const EmbVec3D &lhs, const EmbVec3D &rhs) {
677  return !(lhs == rhs);
678 }
679 
688 inline float dot(const EmbVec3D &lhs, const EmbVec3D &rhs) {
689  return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
690 }
691 
700 inline EmbVec3D cross(const EmbVec3D &lhs, const EmbVec3D &rhs) {
701  const float newx = lhs.y * rhs.z - lhs.z * rhs.y;
702  const float newy = lhs.z * rhs.x - lhs.x * rhs.z;
703  const float newz = lhs.x * rhs.y - lhs.y * rhs.x;
704 
705  return EmbVec3D{newx, newy, newz};
706 }
707 
715 inline float magn(const EmbVec3D &vec) {
716  return sqrtf(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
717 }
718 
731 inline EmbVec3D normalize(const EmbVec3D &vec) { return vec / magn(vec); }
732 
738 inline bool isZero(const EmbVec3D &vec) { return magn(vec) == 0; }
739 
752 inline float alpha(const EmbVec3D &vec) { return acosf(vec.x / magn(vec)); }
753 
766 inline float beta(const EmbVec3D &vec) { return acosf(vec.y / magn(vec)); }
767 
780 inline float gamma(const EmbVec3D &vec) { return acosf(vec.z / magn(vec)); }
781 
792 inline EmbVec3D rotateAlpha(const EmbVec3D &vec, const float ang) {
793  //
794  // Rotation matrix:
795  //
796  // |1 0 0 | |x|
797  // |0 cos(ang) −sin(ang)| |y|
798  // |0 sin(ang) cos(ang)| |z|
799  //
800 
801  const auto xPrime = vec.x;
802  const auto yPrime = vec.y * cosf(ang) - vec.z * sinf(ang);
803  const auto zPrime = vec.y * sinf(ang) + vec.z * cosf(ang);
804 
805  return EmbVec3D{xPrime, yPrime, zPrime};
806 }
807 
818 inline EmbVec3D rotateBeta(const EmbVec3D &vec, const float ang) {
819  //
820  // Rotation matrix:
821  //
822  // | cos(ang) 0 sin(ang)| |x|
823  // | 0 1 0 | |y|
824  // |−sin(ang) 0 cos(ang)| |z|
825  //
826 
827  const auto xPrime = vec.x * cosf(ang) + vec.z * sinf(ang);
828  const auto yPrime = vec.y;
829  const auto zPrime = -vec.x * sinf(ang) + vec.z * cosf(ang);
830 
831  return EmbVec3D{xPrime, yPrime, zPrime};
832 }
833 
844 inline EmbVec3D rotateGamma(const EmbVec3D &vec, const float ang) {
845  //
846  // Rotation matrix:
847  //
848  // |cos(ang) −sin(ang) 0| |x|
849  // |sin(ang) cos(ang) 0| |y|
850  // | 0 0 1| |z|
851  //
852 
853  const auto xPrime = vec.x * cosf(ang) - vec.y * sinf(ang);
854  const auto yPrime = vec.x * sinf(ang) + vec.y * cosf(ang);
855  const auto zPrime = vec.z;
856 
857  return EmbVec3D{xPrime, yPrime, zPrime};
858 }
859 } // namespace svector
860 
861 #endif
bool operator==(const EmbVec2D &lhs, const EmbVec2D &rhs)
Compares equality of two vectors.
Definition: embed.h:470
float magn(const EmbVec2D &vec)
Gets the magnitude of the vector.
Definition: embed.h:505
EmbVec2D operator-(const EmbVec2D &lhs, const EmbVec2D &rhs)
Vector subtraction.
Definition: embed.h:399
float alpha(const EmbVec3D &vec)
Gets α angle.
Definition: embed.h:752
EmbVec3D rotateGamma(const EmbVec3D &vec, const float ang)
Rotates around z-axis.
Definition: embed.h:844
EmbVec3D rotateAlpha(const EmbVec3D &vec, const float ang)
Rotates around x-axis.
Definition: embed.h:792
float z(const EmbVec3D &v)
Gets the z-component of a 3D vector.
Definition: embed.h:363
float x(const EmbVec2D &v)
Gets the x-component of a 2D vector.
Definition: embed.h:295
float y(const EmbVec2D &v)
Gets the y-component of a 2D vector.
Definition: embed.h:329
EmbVec2D normalize(const EmbVec2D &vec)
Normalizes a vector.
Definition: embed.h:532
EmbVec3D rotateBeta(const EmbVec3D &vec, const float ang)
Rotates around y-axis.
Definition: embed.h:818
bool operator!=(const EmbVec2D &lhs, const EmbVec2D &rhs)
Compares inequality of two vectors.
Definition: embed.h:482
float beta(const EmbVec3D &vec)
Gets β angle.
Definition: embed.h:766
float dot(const EmbVec2D &lhs, const EmbVec2D &rhs)
Calculates the dot product of two vectors.
Definition: embed.h:494
EmbVec2D operator+(const EmbVec2D &lhs, const EmbVec2D &rhs)
Vector addition.
Definition: embed.h:384
bool isZero(const EmbVec2D &vec)
Determines whether a vector is a zero vector.
Definition: embed.h:539
float gamma(const EmbVec3D &vec)
Gets γ angle.
Definition: embed.h:780
EmbVec2D operator/(const EmbVec2D &lhs, const float rhs)
Scalar division.
Definition: embed.h:458
EmbVec2D operator*(const EmbVec2D &lhs, const float rhs)
Scalar multiplication.
Definition: embed.h:443
EmbVec3D cross(const EmbVec3D &lhs, const EmbVec3D &rhs)
Cross product of two vectors.
Definition: embed.h:700
EmbVec2D rotate(const EmbVec2D &vec, const float ang)
Rotates vector by a certain angle.
Definition: embed.h:553
float angle(const EmbVec2D &vec)
Gets the angle of a 2D vector in radians.
Definition: embed.h:518
A minimal 2D vector representation.
Definition: embed.h:50
EmbVec2D & operator*=(const float &other)
In-place scalar multiplication.
Definition: embed.h:139
float y
The y-component of the 2D vector.
Definition: embed.h:159
EmbVec2D(const EmbVec2D &other)=default
Copy constructor.
EmbVec2D(EmbVec2D &&) noexcept=default
Move constructor.
EmbVec2D & operator-=(const EmbVec2D &other)
In-place subtraction.
Definition: embed.h:126
EmbVec2D & operator=(EmbVec2D &&) noexcept=default
Move assignment operator.
float x
The x-component of the 2D vector.
Definition: embed.h:158
EmbVec2D()
No-argument constructor.
Definition: embed.h:56
EmbVec2D(const float xOther, const float yOther)
Initializes a vector given xy components.
Definition: embed.h:64
EmbVec2D & operator/=(const float &other)
In-place scalar division.
Definition: embed.h:152
A minimal 3D vector representation.
Definition: embed.h:168
EmbVec3D(EmbVec3D &&) noexcept=default
Move constructor.
EmbVec3D(const float xOther, const float yOther, const float zOther)
Initializes a vector given xyz components.
Definition: embed.h:183
EmbVec3D & operator-=(const EmbVec3D &other)
In-place subtraction.
Definition: embed.h:248
EmbVec3D()
No-argument constructor.
Definition: embed.h:174
float x
The x-component of the 3D vector.
Definition: embed.h:283
EmbVec3D(const EmbVec3D &other)=default
Copy constructor.
float z
The z-component of the 3D vector.
Definition: embed.h:285
EmbVec3D & operator*=(const float &other)
In-place scalar multiplication.
Definition: embed.h:262
float y
The y-component of the 3D vector.
Definition: embed.h:284
EmbVec3D & operator=(EmbVec3D &&) noexcept=default
Move assignment operator.
EmbVec3D & operator/=(const float &other)
In-place scalar division.
Definition: embed.h:276