simplevectors  0.3.9
Simple vector implementations in C++
embed.hpp
Go to the documentation of this file.
1 
38 #ifndef INCLUDE_SVECTOR_EMBED_HPP_
39 #define INCLUDE_SVECTOR_EMBED_HPP_
40 
41 #include <cmath> // std::acos, std::atan2, std::cos, std::sin, std::sqrt
42 #include <string> // std::string
43 
44 namespace svector {
48 struct Vec2D {
54  Vec2D() : x{0}, y{0} {};
55 
62  Vec2D(const double xOther, const double yOther) : x{xOther}, y{yOther} {}
63 
67  Vec2D(const Vec2D &other) = default;
68 
74  Vec2D(Vec2D &&) noexcept = default;
75 
79  Vec2D &operator=(const Vec2D &other) {
80  // check if assigning to self
81  if (this == &other) {
82  return *this;
83  }
84 
85  x = other.x;
86  y = other.y;
87  return *this;
88  }
89 
95  Vec2D &operator=(Vec2D &&) noexcept = default;
96 
102  virtual ~Vec2D() = default;
103 
111  Vec2D &operator+=(const Vec2D &other) {
112  x += other.x;
113  y += other.y;
114  return *this;
115  }
116 
124  Vec2D &operator-=(const Vec2D &other) {
125  x -= other.x;
126  y -= other.y;
127  return *this;
128  }
129 
137  Vec2D &operator*=(const double &other) {
138  x *= other;
139  y *= other;
140  return *this;
141  }
142 
150  Vec2D &operator/=(const double &other) {
151  x /= other;
152  y /= other;
153  return *this;
154  }
155 
156  double x;
157  double y;
158 };
159 
163 struct Vec3D {
169  Vec3D() : x{0}, y{0}, z{0} {}
170 
178  Vec3D(const double xOther, const double yOther, const double zOther)
179  : x{xOther}, y{yOther}, z{zOther} {}
180 
184  Vec3D(const Vec3D &other) = default;
185 
191  Vec3D(Vec3D &&) noexcept = default;
192 
196  Vec3D &operator=(const Vec3D &other) {
197  // check if assigning to self
198  if (this == &other) {
199  return *this;
200  }
201 
202  x = other.x;
203  y = other.y;
204  z = other.z;
205  return *this;
206  }
207 
213  Vec3D &operator=(Vec3D &&) noexcept = default;
214 
220  virtual ~Vec3D() = default;
221 
229  Vec3D &operator+=(const Vec3D &other) {
230  x += other.x;
231  y += other.y;
232  z += other.z;
233  return *this;
234  }
235 
243  Vec3D &operator-=(const Vec3D &other) {
244  x -= other.x;
245  y -= other.y;
246  z -= other.z;
247  return *this;
248  }
249 
257  Vec3D &operator*=(const double &other) {
258  x *= other;
259  y *= other;
260  z *= other;
261  return *this;
262  }
263 
271  Vec3D &operator/=(const double &other) {
272  x /= other;
273  y /= other;
274  z /= other;
275  return *this;
276  }
277 
278  double x;
279  double y;
280  double z;
281 };
282 
290 inline double x(const Vec2D &v) { return v.x; }
291 
298 inline void x(Vec2D &v, const double xValue) { v.x = xValue; }
299 
307 inline double x(const Vec3D &v) { return v.x; }
308 
315 inline void x(Vec3D &v, const double xValue) { v.x = xValue; }
316 
324 inline double y(const Vec2D &v) { return v.y; }
325 
332 inline void y(Vec2D &v, const double yValue) { v.y = yValue; }
333 
341 inline double y(const Vec3D &v) { return v.y; }
342 
349 inline void y(Vec3D &v, const double yValue) { v.y = yValue; }
350 
358 inline double z(const Vec3D &v) { return v.z; }
359 
366 inline void z(Vec3D &v, const double zValue) { v.z = zValue; }
367 
377 inline std::string toString(const Vec2D &vec) {
378  std::string s{"<"};
379  s = s + std::to_string(vec.x) + ", " + std::to_string(vec.y) + ">";
380  return s;
381 }
382 
394 inline Vec2D operator+(const Vec2D &lhs, const Vec2D &rhs) {
395  return Vec2D{lhs.x + rhs.x, lhs.y + rhs.y};
396 }
397 
409 inline Vec2D operator-(const Vec2D &lhs, const Vec2D &rhs) {
410  return Vec2D{lhs.x - rhs.x, lhs.y - rhs.y};
411 }
412 
424 inline Vec2D operator-(const Vec2D &vec) { return Vec2D{-vec.x, -vec.y}; }
425 
436 inline Vec2D operator+(const Vec2D &vec) { return Vec2D{+vec.x, +vec.y}; }
437 
449 inline Vec2D operator*(const Vec2D &lhs, const double rhs) {
450  return Vec2D{lhs.x * rhs, lhs.y * rhs};
451 }
452 
464 inline Vec2D operator/(const Vec2D &lhs, const double rhs) {
465  return Vec2D{lhs.x / rhs, lhs.y / rhs};
466 }
467 
476 inline bool operator==(const Vec2D &lhs, const Vec2D &rhs) {
477  return lhs.x == rhs.x && lhs.y == rhs.y;
478 }
479 
488 inline bool operator!=(const Vec2D &lhs, const Vec2D &rhs) {
489  return !(lhs == rhs);
490 }
491 
500 inline double dot(const Vec2D &lhs, const Vec2D &rhs) {
501  return lhs.x * rhs.x + lhs.y * rhs.y;
502 }
503 
511 inline double magn(const Vec2D &vec) {
512  return std::sqrt(vec.x * vec.x + vec.y * vec.y);
513 }
514 
524 inline double angle(const Vec2D &vec) { return std::atan2(vec.y, vec.x); }
525 
538 inline Vec2D normalize(const Vec2D &vec) { return vec / magn(vec); }
539 
545 inline bool isZero(const Vec2D &vec) { return magn(vec) == 0; }
546 
559 inline Vec2D rotate(const Vec2D &vec, const double ang) {
560  //
561  // Rotation matrix:
562  //
563  // | cos(ang) -sin(ang) | |x|
564  // | sin(ang) cos(ang) | |y|
565  //
566 
567  const double xPrime = vec.x * std::cos(ang) - vec.y * std::sin(ang);
568  const double yPrime = vec.x * std::sin(ang) + vec.y * std::cos(ang);
569 
570  return Vec2D{xPrime, yPrime};
571 }
572 
582 inline std::string toString(const Vec3D &vec) {
583  std::string s{"<"};
584  s = s + std::to_string(vec.x) + ", " + std::to_string(vec.y) + ", " +
585  std::to_string(vec.z) + ">";
586  return s;
587 }
588 
600 inline Vec3D operator+(const Vec3D &lhs, const Vec3D &rhs) {
601  return Vec3D{lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z};
602 }
603 
615 inline Vec3D operator-(const Vec3D &lhs, const Vec3D &rhs) {
616  return Vec3D{lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z};
617 }
618 
630 inline Vec3D operator-(const Vec3D &vec) {
631  return Vec3D{-vec.x, -vec.y, -vec.z};
632 }
633 
644 inline Vec3D operator+(const Vec3D &vec) {
645  return Vec3D{+vec.x, +vec.y, +vec.z};
646 }
647 
659 inline Vec3D operator*(const Vec3D &lhs, const double rhs) {
660  return Vec3D{lhs.x * rhs, lhs.y * rhs, lhs.z * rhs};
661 }
662 
674 inline Vec3D operator/(const Vec3D &lhs, const double rhs) {
675  return Vec3D{lhs.x / rhs, lhs.y / rhs, lhs.z / rhs};
676 }
677 
686 inline bool operator==(const Vec3D &lhs, const Vec3D &rhs) {
687  return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
688 }
689 
698 inline bool operator!=(const Vec3D &lhs, const Vec3D &rhs) {
699  return !(lhs == rhs);
700 }
701 
710 inline double dot(const Vec3D &lhs, const Vec3D &rhs) {
711  return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
712 }
713 
722 inline Vec3D cross(const Vec3D &lhs, const Vec3D &rhs) {
723  const double newx = lhs.y * rhs.z - lhs.z * rhs.y;
724  const double newy = lhs.z * rhs.x - lhs.x * rhs.z;
725  const double newz = lhs.x * rhs.y - lhs.y * rhs.x;
726 
727  return Vec3D{newx, newy, newz};
728 }
729 
737 inline double magn(const Vec3D &vec) {
738  return std::sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
739 }
740 
753 inline Vec3D normalize(const Vec3D &vec) { return vec / magn(vec); }
754 
760 inline bool isZero(const Vec3D &vec) { return magn(vec) == 0; }
761 
774 inline double alpha(const Vec3D &vec) { return std::acos(vec.x / magn(vec)); }
775 
788 inline double beta(const Vec3D &vec) { return std::acos(vec.y / magn(vec)); }
789 
802 inline double gamma(const Vec3D &vec) { return std::acos(vec.z / magn(vec)); }
803 
814 inline Vec3D rotateAlpha(const Vec3D &vec, const double ang) {
815  //
816  // Rotation matrix:
817  //
818  // |1 0 0 | |x|
819  // |0 cos(ang) −sin(ang)| |y|
820  // |0 sin(ang) cos(ang)| |z|
821  //
822 
823  const double xPrime = vec.x;
824  const double yPrime = vec.y * std::cos(ang) - vec.z * std::sin(ang);
825  const double zPrime = vec.y * std::sin(ang) + vec.z * std::cos(ang);
826 
827  return Vec3D{xPrime, yPrime, zPrime};
828 }
829 
840 inline Vec3D rotateBeta(const Vec3D &vec, const double ang) {
841  //
842  // Rotation matrix:
843  //
844  // | cos(ang) 0 sin(ang)| |x|
845  // | 0 1 0 | |y|
846  // |−sin(ang) 0 cos(ang)| |z|
847  //
848 
849  const double xPrime = vec.x * std::cos(ang) + vec.z * std::sin(ang);
850  const double yPrime = vec.y;
851  const double zPrime = -vec.x * std::sin(ang) + vec.z * std::cos(ang);
852 
853  return Vec3D{xPrime, yPrime, zPrime};
854 }
855 
866 inline Vec3D rotateGamma(const Vec3D &vec, const double ang) {
867  //
868  // Rotation matrix:
869  //
870  // |cos(ang) −sin(ang) 0| |x|
871  // |sin(ang) cos(ang) 0| |y|
872  // | 0 0 1| |z|
873  //
874 
875  const double xPrime = vec.x * std::cos(ang) - vec.y * std::sin(ang);
876  const double yPrime = vec.x * std::sin(ang) + vec.y * std::cos(ang);
877  const double zPrime = vec.z;
878 
879  return Vec3D{xPrime, yPrime, zPrime};
880 }
881 } // namespace svector
882 
883 #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
std::string toString(const Vec2D &vec)
String form.
Definition: embed.hpp:377
A minimal 2D vector representation.
Definition: embed.hpp:48
Vec2D & operator/=(const double &other)
In-place scalar division.
Definition: embed.hpp:150
Vec2D()
No-argument constructor.
Definition: embed.hpp:54
Vec2D & operator=(Vec2D &&) noexcept=default
Move assignment operator.
double y
The y-component of the 2D vector.
Definition: embed.hpp:157
Vec2D & operator*=(const double &other)
In-place scalar multiplication.
Definition: embed.hpp:137
Vec2D & operator-=(const Vec2D &other)
In-place subtraction.
Definition: embed.hpp:124
double x
The x-component of the 2D vector.
Definition: embed.hpp:156
Vec2D(Vec2D &&) noexcept=default
Move constructor.
Vec2D(const Vec2D &other)=default
Copy constructor.
Vec2D(const double xOther, const double yOther)
Initializes a vector given xy components.
Definition: embed.hpp:62
A minimal 3D vector representation.
Definition: embed.hpp:163
Vec3D(const Vec3D &other)=default
Copy constructor.
double z
The z-component of the 3D vector.
Definition: embed.hpp:280
Vec3D & operator-=(const Vec3D &other)
In-place subtraction.
Definition: embed.hpp:243
Vec3D()
No-argument constructor.
Definition: embed.hpp:169
double y
The y-component of the 3D vector.
Definition: embed.hpp:279
Vec3D & operator*=(const double &other)
In-place scalar multiplication.
Definition: embed.hpp:257
Vec3D(Vec3D &&) noexcept=default
Move constructor.
Vec3D(const double xOther, const double yOther, const double zOther)
Initializes a vector given xyz components.
Definition: embed.hpp:178
double x
The x-component of the 3D vector.
Definition: embed.hpp:278
Vec3D & operator/=(const double &other)
In-place scalar division.
Definition: embed.hpp:271
Vec3D & operator=(Vec3D &&) noexcept=default
Move assignment operator.