hermite  0.0.1
simplevectors.hpp
Go to the documentation of this file.
1 
29 #ifndef INCLUDE_SVECTORS_SIMPLEVECTORS_HPP_
30 #define INCLUDE_SVECTORS_SIMPLEVECTORS_HPP_
31 
32 #include <algorithm>
33 #include <array>
34 #include <cmath>
35 #include <cstddef>
36 #include <cstdint>
37 #include <initializer_list>
38 #include <string>
39 #include <type_traits>
40 #include <vector>
41 
42 namespace svector {
51 enum AngleDir {
53  BETA,
54  GAMMA
55 };
63 template <std::size_t D, typename T = double> class Vector {
64 public:
65  // makes sure that type is numeric
66  static_assert(std::is_arithmetic<T>::value, "Vector type must be numeric");
67 
68 #ifdef SVECTOR_EXPERIMENTAL_COMPARE
69  template <std::size_t D1, std::size_t D2, typename T1, typename T2>
70  friend bool operator<(const Vector<D1, T1> &, const Vector<D2, T2> &);
71 
72  template <std::size_t D1, std::size_t D2, typename T1, typename T2>
73  friend bool operator<=(const Vector<D1, T1> &, const Vector<D2, T2> &);
74 
75  template <std::size_t D1, std::size_t D2, typename T1, typename T2>
76  friend bool operator>(const Vector<D1, T1> &, const Vector<D2, T2> &);
77 
78  template <std::size_t D1, std::size_t D2, typename T1, typename T2>
79  friend bool operator>=(const Vector<D1, T1> &, const Vector<D2, T2> &);
80 #endif
81 
82  typedef
83  typename std::array<T, D>::iterator iterator;
84  typedef typename std::array<T, D>::const_iterator
86  typedef typename std::array<T, D>::reverse_iterator
88  typedef typename std::array<T, D>::const_reverse_iterator
90 
96  Vector() { this->m_components.fill(0); }
97 
110  Vector(const std::initializer_list<T> args) {
111  // in case length of args < dimensions
112  this->m_components.fill(0);
113 
114  std::size_t counter = 0;
115  for (const auto &num : args) {
116  if (counter >= D) {
117  break;
118  }
119 
120  this->m_components[counter] = num;
121  counter++;
122  }
123  }
124 
130  Vector(const Vector<D, T> &other) {
131  for (std::size_t i = 0; i < D; i++) {
132  this->m_components[i] = other[i];
133  }
134  }
135 
141  Vector(Vector<D, T> &&) noexcept = default;
142 
148  Vector<D, T> &operator=(const Vector<D, T> &other) {
149  // check if assigning to self
150  if (this == &other) {
151  return *this;
152  }
153 
154  for (std::size_t i = 0; i < D; i++) {
155  this->m_components[i] = other[i];
156  }
157 
158  return *this;
159  }
160 
166  Vector<D, T> &operator=(Vector<D, T> &&) noexcept = default;
167 
173  virtual ~Vector() = default;
174 
182  virtual std::string toString() const {
183  std::string str = "<";
184  for (std::size_t i = 0; i < D - 1; i++) {
185  str += std::to_string(this->m_components[i]);
186  str += ", ";
187  }
188 
189  str += std::to_string(this->m_components[D - 1]);
190  str += ">";
191 
192  return str;
193  }
194 
195 #ifdef SVECTOR_USE_CLASS_OPERATORS
212  Vector<D, T> operator+(const Vector<D, T> &other) const {
213  Vector<D, T> tmp;
214  for (std::size_t i = 0; i < D; i++) {
215  tmp[i] = this->m_components[i] + other[i];
216  }
217 
218  return tmp;
219  }
220 
237  Vector<D, T> operator-(const Vector<D, T> &other) const {
238  Vector<D, T> tmp;
239  for (std::size_t i = 0; i < D; i++) {
240  tmp[i] = this->m_components[i] - other[i];
241  }
242 
243  return tmp;
244  }
245 
259  Vector<D, T> operator*(const T other) const {
260  Vector<D, T> tmp;
261  for (std::size_t i = 0; i < D; i++) {
262  tmp[i] = this->m_components[i] * other;
263  }
264 
265  return tmp;
266  }
267 
281  Vector<D, T> operator/(const T other) const {
282  Vector<D, T> tmp;
283  for (std::size_t i = 0; i < D; i++) {
284  tmp[i] = this->m_components[i] / other;
285  }
286 
287  return tmp;
288  }
289 
303  bool operator==(const Vector<D, T> &other) const {
304  for (std::size_t i = 0; i < D; i++) {
305  if (this->m_components[i] != other[i]) {
306  return false;
307  }
308  }
309 
310  return true;
311  }
312 
326  bool operator!=(const Vector<D, T> &other) const {
327  return !((*this) == other);
328  }
329 #endif
330 
341  Vector<D, T> tmp;
342  for (std::size_t i = 0; i < D; i++) {
343  tmp[i] = -this->m_components[i];
344  }
345 
346  return tmp;
347  }
348 
358  Vector<D, T> tmp;
359  for (std::size_t i = 0; i < D; i++) {
360  tmp[i] = +this->m_components[i];
361  }
362 
363  return tmp;
364  }
365 
374  for (std::size_t i = 0; i < D; i++) {
375  this->m_components[i] += other[i];
376  }
377 
378  return *this;
379  }
380 
389  for (std::size_t i = 0; i < D; i++) {
390  this->m_components[i] -= other[i];
391  }
392 
393  return *this;
394  }
395 
403  Vector<D, T> &operator*=(const T other) {
404  for (std::size_t i = 0; i < D; i++) {
405  this->m_components[i] *= other;
406  }
407 
408  return *this;
409  }
410 
418  Vector<D, T> &operator/=(const T other) {
419  for (std::size_t i = 0; i < D; i++) {
420  this->m_components[i] /= other;
421  }
422 
423  return *this;
424  }
425 
438  T dot(const Vector<D, T> &other) const {
439  T result = 0;
440 
441  for (std::size_t i = 0; i < D; i++) {
442  result += this->m_components[i] * other[i];
443  }
444 
445  return result;
446  }
447 
455  T magn() const {
456  T sum_of_squares = 0;
457 
458  for (const auto &i : this->m_components) {
459  sum_of_squares += i * i;
460  }
461 
462  return std::sqrt(sum_of_squares);
463  };
464 
475  Vector<D, T> normalize() const { return (*this) / this->magn(); }
476 
482  constexpr std::size_t numDimensions() const { return D; }
483 
489  bool isZero() const { return this->magn() == 0; }
490 
501  const T &operator[](const std::size_t index) const {
502  return this->m_components[index];
503  }
504 
512  T &operator[](const std::size_t index) { return this->m_components[index]; }
513 
526  const T &at(const std::size_t index) const {
527  return this->m_components.at(index);
528  }
529 
539  T &at(const std::size_t index) { return this->m_components.at(index); }
540 
551  iterator begin() noexcept { return iterator{this->m_components.begin()}; }
552 
561  const_iterator begin() const noexcept {
562  return const_iterator{this->m_components.begin()};
563  }
564 
578  iterator end() noexcept { return iterator{this->m_components.end()}; }
579 
591  const_iterator end() const noexcept {
592  return const_iterator{this->m_components.end()};
593  }
594 
607  return reverse_iterator{this->m_components.rbegin()};
608  }
609 
619  const_reverse_iterator rbegin() const noexcept {
620  return const_reverse_iterator{this->m_components.rbegin()};
621  }
622 
637  reverse_iterator rend() noexcept {
638  return reverse_iterator{this->m_components.rend()};
639  }
640 
654  const_reverse_iterator rend() const noexcept {
655  return const_reverse_iterator{this->m_components.rend()};
656  }
657 
658 protected:
659  std::array<T, D> m_components;
660 
661 #ifdef SVECTOR_EXPERIMENTAL_COMPARE
662 private:
680  template <std::size_t D2, typename T2>
681  std::int8_t compare(const Vector<D2, T2> &other) const noexcept {
682  std::size_t min_dim = std::min(D, D2);
683  std::size_t counter = 0;
684 
685 // to suppress MSVC warning
686 #ifdef _MSC_VER
687  if constexpr (D != D2) {
688 #else
689  if (D != D2) {
690 #endif
691  // check dimensions first
692  return D < D2 ? -1 : 1;
693  }
694 
695  // compare one by one
696  for (std::size_t i = 0; i < min_dim; i++) {
697  if (this->m_components[i] == other[i]) {
698  counter++;
699  } else if (this->m_components[i] < other[i]) {
700  return -1;
701  } else {
702  return 1;
703  }
704  }
705 
706  if (counter != D || counter != D2) {
707  return -1;
708  }
709 
710  // means two vectors are equal
711  return 0;
712  }
713 #endif
714 };
715 
716 typedef Vector<2> Vec2_;
717 
721 class Vector2D : public Vec2_ {
722 public:
723  using Vec2_::Vector;
724 
731  Vector2D(const double x, const double y) {
732  this->m_components[0] = x;
733  this->m_components[1] = y;
734  }
735 
739  Vector2D(const Vec2_ &other) {
740  this->m_components[0] = other[0];
741  this->m_components[1] = other[1];
742  }
743 
751  double x() const { return this->m_components[0]; }
752 
760  void x(const double &newX) { this->m_components[0] = newX; }
761 
769  double y() const { return this->m_components[1]; }
770 
778  void y(const double &newY) { this->m_components[1] = newY; }
779 
789  double angle() const { return std::atan2(this->y(), this->x()); }
790 
802  Vector2D rotate(const double ang) const {
803  //
804  // Rotation matrix:
805  //
806  // | cos(ang) -sin(ang) | |x|
807  // | sin(ang) cos(ang) | |y|
808  //
809 
810  const double xPrime = this->x() * std::cos(ang) - this->y() * std::sin(ang);
811  const double yPrime = this->x() * std::sin(ang) + this->y() * std::cos(ang);
812 
813  return Vector2D{xPrime, yPrime};
814  }
815 
828  template <typename T> T componentsAs() const {
829  return T{this->x(), this->y()};
830  }
831 };
832 
833 typedef Vector<3> Vec3_;
834 
838 class Vector3D : public Vec3_ {
839 public:
840  using Vec3_::Vector;
841 
849  Vector3D(const double x, const double y, const double z) {
850  this->m_components[0] = x;
851  this->m_components[1] = y;
852  this->m_components[2] = z;
853  }
854 
858  Vector3D(const Vec3_ &other) {
859  this->m_components[0] = other[0];
860  this->m_components[1] = other[1];
861  this->m_components[2] = other[2];
862  }
863 
871  double x() const { return this->m_components[0]; }
872 
880  void x(const double &newX) { this->m_components[0] = newX; }
881 
889  double y() const { return this->m_components[1]; }
890 
898  void y(const double &newY) { this->m_components[1] = newY; }
899 
907  double z() const { return this->m_components[2]; }
908 
916  void z(const double &newZ) { this->m_components[2] = newZ; }
917 
925  Vector3D cross(const Vector3D &other) const {
926  const double newx = this->y() * other.z() - this->z() * other.y();
927  const double newy = this->z() * other.x() - this->x() * other.z();
928  const double newz = this->x() * other.y() - this->y() * other.x();
929 
930  return Vector3D{newx, newy, newz};
931  }
932 
945  template <typename T> T componentsAs() const {
946  return T{this->x(), this->y(), this->z()};
947  }
948 
961  template <typename T> T anglesAs() const {
962  return T{this->getAlpha(), this->getBeta(), this->getGamma()};
963  }
964 
980  template <AngleDir D> double angle() const {
981  switch (D) {
982  case ALPHA:
983  return this->getAlpha();
984  case BETA:
985  return this->getBeta();
986  default:
987  return this->getGamma();
988  }
989  }
990 
1008  template <AngleDir D> Vector3D rotate(const double &ang) const {
1009  switch (D) {
1010  case ALPHA:
1011  return this->rotateAlpha(ang);
1012  case BETA:
1013  return this->rotateBeta(ang);
1014  default:
1015  return this->rotateGamma(ang);
1016  }
1017  }
1018 
1019 private:
1027  double getAlpha() const { return std::acos(this->x() / this->magn()); }
1028 
1036  double getBeta() const { return std::acos(this->y() / this->magn()); }
1037 
1045  double getGamma() const { return std::acos(this->z() / this->magn()); }
1046 
1050  Vector3D rotateAlpha(const double &ang) const {
1059  const double xPrime = this->x();
1060  const double yPrime = this->y() * std::cos(ang) - this->z() * std::sin(ang);
1061  const double zPrime = this->y() * std::sin(ang) + this->z() * std::cos(ang);
1062 
1063  return Vector3D{xPrime, yPrime, zPrime};
1064  }
1065 
1069  Vector3D rotateBeta(const double &ang) const {
1078  const double xPrime = this->x() * std::cos(ang) + this->z() * std::sin(ang);
1079  const double yPrime = this->y();
1080  const double zPrime =
1081  -this->x() * std::sin(ang) + this->z() * std::cos(ang);
1082 
1083  return Vector3D{xPrime, yPrime, zPrime};
1084  }
1085 
1089  Vector3D rotateGamma(const double &ang) const {
1098  const double xPrime = this->x() * std::cos(ang) - this->y() * std::sin(ang);
1099  const double yPrime = this->x() * std::sin(ang) + this->y() * std::cos(ang);
1100  const double zPrime = this->z();
1101 
1102  return Vector3D{xPrime, yPrime, zPrime};
1103  }
1104 };
1105 
1113 template <std::size_t D, typename T>
1114 Vector<D, T> makeVector(std::array<T, D> array) {
1115  Vector<D, T> vec;
1116  for (std::size_t i = 0; i < D; i++) {
1117  vec[i] = array[i];
1118  }
1119 
1120  return vec;
1121 }
1122 
1137 template <std::size_t D, typename T>
1138 Vector<D, T> makeVector(std::vector<T> vector) {
1139  Vector<D, T> vec;
1140  for (std::size_t i = 0; i < std::min(D, vector.size()); i++) {
1141  vec[i] = vector[i];
1142  }
1143 
1144  return vec;
1145 }
1146 
1162 template <std::size_t D, typename T>
1163 Vector<D, T> makeVector(const std::initializer_list<T> args) {
1164  Vector<D, T> vec(args);
1165  return vec;
1166 }
1167 
1175 inline double x(const Vector2D &v) { return v[0]; }
1176 
1183 inline void x(Vector2D &v, const double xValue) { v[0] = xValue; }
1184 
1192 inline double x(const Vector3D &v) { return v[0]; }
1193 
1200 inline void x(Vector3D &v, const double xValue) { v[0] = xValue; }
1201 
1209 inline double y(const Vector2D &v) { return v[1]; }
1210 
1217 inline void y(Vector2D &v, const double yValue) { v[1] = yValue; }
1218 
1226 inline double y(const Vector3D &v) { return v[1]; }
1227 
1234 inline void y(Vector3D &v, const double yValue) { v[1] = yValue; }
1235 
1243 inline double z(const Vector3D &v) { return v[2]; }
1244 
1251 inline void z(Vector3D &v, const double zValue) { v[2] = zValue; }
1252 
1263 template <typename T, std::size_t D>
1264 inline T dot(const Vector<D, T> &lhs, const Vector<D, T> &rhs) {
1265  T result = 0;
1266 
1267  for (std::size_t i = 0; i < D; i++) {
1268  result += lhs[i] * rhs[i];
1269  }
1270 
1271  return result;
1272 }
1273 
1281 template <typename T, std::size_t D> inline T magn(const Vector<D, T> &v) {
1282  T sum_of_squares = 0;
1283 
1284  for (std::size_t i = 0; i < D; i++) {
1285  sum_of_squares += v[i] * v[i];
1286  }
1287 
1288  return std::sqrt(sum_of_squares);
1289 }
1290 
1303 template <typename T, std::size_t D>
1305  return v / magn(v);
1306 }
1307 
1313 template <typename T, std::size_t D> inline bool isZero(const Vector<D, T> &v) {
1314  return magn(v) == 0;
1315 }
1316 
1326 inline double angle(const Vector2D &v) { return std::atan2(y(v), x(v)); }
1327 
1340 inline Vector2D rotate(const Vector2D &v, const double ang) {
1341  //
1342  // Rotation matrix:
1343  //
1344  // | cos(ang) -sin(ang) | |x|
1345  // | sin(ang) cos(ang) | |y|
1346  //
1347 
1348  const double xPrime = x(v) * std::cos(ang) - y(v) * std::sin(ang);
1349  const double yPrime = x(v) * std::sin(ang) + y(v) * std::cos(ang);
1350 
1351  return Vector2D{xPrime, yPrime};
1352 }
1353 
1362 inline Vector3D cross(const Vector3D &lhs, const Vector3D &rhs) {
1363  const double newx = y(lhs) * z(rhs) - z(lhs) * y(rhs);
1364  const double newy = z(lhs) * x(rhs) - x(lhs) * z(rhs);
1365  const double newz = x(lhs) * y(rhs) - y(lhs) * x(rhs);
1366 
1367  return Vector3D{newx, newy, newz};
1368 }
1369 
1382 inline double alpha(const Vector3D &v) { return std::acos(x(v) / magn(v)); }
1383 
1396 inline double beta(const Vector3D &v) { return std::acos(y(v) / magn(v)); }
1397 
1410 inline double gamma(const Vector3D &v) { return std::acos(z(v) / magn(v)); }
1411 
1422 inline Vector3D rotateAlpha(const Vector3D &v, const double &ang) {
1423  //
1424  // Rotation matrix:
1425  //
1426  // |1 0 0 | |x|
1427  // |0 cos(ang) −sin(ang)| |y|
1428  // |0 sin(ang) cos(ang)| |z|
1429  //
1430 
1431  const double xPrime = x(v);
1432  const double yPrime = y(v) * std::cos(ang) - z(v) * std::sin(ang);
1433  const double zPrime = y(v) * std::sin(ang) + z(v) * std::cos(ang);
1434 
1435  return Vector3D{xPrime, yPrime, zPrime};
1436 }
1437 
1448 inline Vector3D rotateBeta(const Vector3D &v, const double &ang) {
1449  //
1450  // Rotation matrix:
1451  //
1452  // | cos(ang) 0 sin(ang)| |x|
1453  // | 0 1 0 | |y|
1454  // |−sin(ang) 0 cos(ang)| |z|
1455  //
1456 
1457  const double xPrime = x(v) * std::cos(ang) + z(v) * std::sin(ang);
1458  const double yPrime = y(v);
1459  const double zPrime = -x(v) * std::sin(ang) + z(v) * std::cos(ang);
1460 
1461  return Vector3D{xPrime, yPrime, zPrime};
1462 }
1463 
1474 inline Vector3D rotateGamma(const Vector3D &v, const double &ang) {
1475  //
1476  // Rotation matrix:
1477  //
1478  // |cos(ang) −sin(ang) 0| |x|
1479  // |sin(ang) cos(ang) 0| |y|
1480  // | 0 0 1| |z|
1481  //
1482 
1483  const double xPrime = x(v) * std::cos(ang) - y(v) * std::sin(ang);
1484  const double yPrime = x(v) * std::sin(ang) + y(v) * std::cos(ang);
1485  const double zPrime = z(v);
1486 
1487  return Vector3D{xPrime, yPrime, zPrime};
1488 }
1489 
1490 #ifndef SVECTOR_USE_CLASS_OPERATORS
1507 template <typename T, std::size_t D>
1509  const Vector<D, T> &rhs) {
1510  Vector<D, T> tmp;
1511  for (std::size_t i = 0; i < D; i++) {
1512  tmp[i] = lhs[i] + rhs[i];
1513  }
1514 
1515  return tmp;
1516 }
1517 
1534 template <typename T, std::size_t D>
1536  const Vector<D, T> &rhs) {
1537  Vector<D, T> tmp;
1538  for (std::size_t i = 0; i < D; i++) {
1539  tmp[i] = lhs[i] - rhs[i];
1540  }
1541 
1542  return tmp;
1543 }
1544 
1559 template <typename T, typename T2, std::size_t D>
1560 inline Vector<D, T> operator*(const Vector<D, T> &lhs, const T2 rhs) {
1561  Vector<D, T> tmp;
1562  for (std::size_t i = 0; i < D; i++) {
1563  tmp[i] = lhs[i] * rhs;
1564  }
1565 
1566  return tmp;
1567 }
1568 
1583 template <typename T, typename T2, std::size_t D>
1584 inline Vector<D, T> operator/(const Vector<D, T> &lhs, const T2 rhs) {
1585  Vector<D, T> tmp;
1586  for (std::size_t i = 0; i < D; i++) {
1587  tmp[i] = lhs[i] / rhs;
1588  }
1589 
1590  return tmp;
1591 }
1592 
1606 template <typename T, std::size_t D>
1607 inline bool operator==(const Vector<D, T> &lhs, const Vector<D, T> &rhs) {
1608  for (std::size_t i = 0; i < D; i++) {
1609  if (lhs[i] != rhs[i]) {
1610  return false;
1611  }
1612  }
1613 
1614  return true;
1615 }
1616 
1630 template <typename T, std::size_t D>
1631 inline bool operator!=(const Vector<D, T> &lhs, const Vector<D, T> &rhs) {
1632  return !(lhs == rhs);
1633 }
1634 #endif
1635 
1636 #ifdef SVECTOR_EXPERIMENTAL_COMPARE
1637 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
1638 bool operator<(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
1639  return lhs.compare(rhs) < 0;
1640 }
1641 
1642 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
1643 bool operator>(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
1644  return lhs.compare(rhs) > 0;
1645 }
1646 
1647 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
1648 bool operator<=(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
1649  return lhs.compare(rhs) <= 0;
1650 }
1651 
1652 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
1653 bool operator>=(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
1654  return lhs.compare(rhs) >= 0;
1655 }
1656 #endif
1657 } // namespace svector
1658 
1659 #endif
bool operator==(const Vector< D, T > &lhs, const Vector< D, T > &rhs)
Compares equality of two vectors.
Definition: simplevectors.hpp:1607
AngleDir
Angle enumerator.
Definition: simplevectors.hpp:51
@ ALPHA
Angle between positive x-axis and vector.
Definition: simplevectors.hpp:52
@ GAMMA
Angle between positive z-axis and vector.
Definition: simplevectors.hpp:54
@ BETA
Angle between positive y-axis and vector.
Definition: simplevectors.hpp:53
double x(const Vector2D &v)
Gets the x-component of a 2D vector.
Definition: simplevectors.hpp:1175
double alpha(const Vector3D &v)
Gets α angle.
Definition: simplevectors.hpp:1382
Vector3D cross(const Vector3D &lhs, const Vector3D &rhs)
Cross product of two vectors.
Definition: simplevectors.hpp:1362
double angle(const Vector2D &v)
Gets the angle of a 2D vector in radians.
Definition: simplevectors.hpp:1326
Vector3D rotateAlpha(const Vector3D &v, const double &ang)
Rotates around x-axis.
Definition: simplevectors.hpp:1422
Vector3D rotateGamma(const Vector3D &v, const double &ang)
Rotates around z-axis.
Definition: simplevectors.hpp:1474
Vector< D, T > operator*(const Vector< D, T > &lhs, const T2 rhs)
Scalar multiplication.
Definition: simplevectors.hpp:1560
Vector2D rotate(const Vector2D &v, const double ang)
Rotates a 2D vector by a certain angle.
Definition: simplevectors.hpp:1340
double beta(const Vector3D &v)
Gets β angle.
Definition: simplevectors.hpp:1396
double y(const Vector2D &v)
Gets the y-component of a 2D vector.
Definition: simplevectors.hpp:1209
Vector3D rotateBeta(const Vector3D &v, const double &ang)
Rotates around y-axis.
Definition: simplevectors.hpp:1448
double z(const Vector3D &v)
Gets the z-component of a 3D vector.
Definition: simplevectors.hpp:1243
Vector< D, T > makeVector(std::array< T, D > array)
Creates a vector from an std::array.
Definition: simplevectors.hpp:1114
Vector< D, T > operator/(const Vector< D, T > &lhs, const T2 rhs)
Scalar division.
Definition: simplevectors.hpp:1584
bool operator!=(const Vector< D, T > &lhs, const Vector< D, T > &rhs)
Compares inequality of two vectors.
Definition: simplevectors.hpp:1631
double gamma(const Vector3D &v)
Gets γ angle.
Definition: simplevectors.hpp:1410
A base vector representation.
Definition: simplevectors.hpp:63
std::array< T, D >::const_reverse_iterator const_reverse_iterator
An std::array::const_reverse_iterator.
Definition: simplevectors.hpp:89
const T & at(const std::size_t index) const
Value of a certain component of a vector.
Definition: simplevectors.hpp:526
Vector()
No-argument constructor.
Definition: simplevectors.hpp:96
std::array< T, D >::const_iterator const_iterator
An std::array::const_iterator.
Definition: simplevectors.hpp:85
Vector< D, T > operator+() const
Positive of a vector.
Definition: simplevectors.hpp:357
reverse_iterator rend() noexcept
Reverse iterator to first element - 1.
Definition: simplevectors.hpp:637
Vector(Vector< D, T > &&) noexcept=default
Move constructor.
reverse_iterator rbegin() noexcept
Reverse iterator to last element.
Definition: simplevectors.hpp:606
T magn() const
Magnitude.
Definition: simplevectors.hpp:455
Vector< D, T > & operator+=(const Vector< D, T > &other)
In-place addition.
Definition: simplevectors.hpp:373
const_reverse_iterator rbegin() const noexcept
Const reverse iterator to last element.
Definition: simplevectors.hpp:619
iterator begin() noexcept
Iterator of first element.
Definition: simplevectors.hpp:551
std::array< T, D >::iterator iterator
An std::array::iterator.
Definition: simplevectors.hpp:66
const_iterator end() const noexcept
Const interator of last element + 1.
Definition: simplevectors.hpp:591
Vector< D, T > normalize() const
Normalizes a vector.
Definition: simplevectors.hpp:475
Vector< D, T > & operator-=(const Vector< D, T > &other)
In-place subtraction.
Definition: simplevectors.hpp:388
const_reverse_iterator rend() const noexcept
Const reverse iterator to first element - 1.
Definition: simplevectors.hpp:654
virtual std::string toString() const
Returns string form of vector.
Definition: simplevectors.hpp:182
Vector< D, T > operator-() const
Negative of a vector.
Definition: simplevectors.hpp:340
Vector(const std::initializer_list< T > args)
Initializes a vector given initializer list.
Definition: simplevectors.hpp:110
Vector< D, T > & operator=(Vector< D, T > &&) noexcept=default
Move assignment operator.
iterator end() noexcept
Interator of last element + 1.
Definition: simplevectors.hpp:578
Vector(const Vector< D, T > &other)
Copy constructor.
Definition: simplevectors.hpp:130
std::array< T, D > m_components
An array of components for the vector.
Definition: simplevectors.hpp:659
T dot(const Vector< D, T > &other) const
Dot product.
Definition: simplevectors.hpp:438
Vector< D, T > & operator/=(const T other)
In-place scalar division.
Definition: simplevectors.hpp:418
Vector< D, T > & operator*=(const T other)
In-place scalar multiplication.
Definition: simplevectors.hpp:403
std::array< T, D >::reverse_iterator reverse_iterator
An std::array::reverse_iterator.
Definition: simplevectors.hpp:87
T & operator[](const std::size_t index)
Sets value of a certain component.
Definition: simplevectors.hpp:512
bool isZero() const
Determines whether the current vector is a zero vector.
Definition: simplevectors.hpp:489
const_iterator begin() const noexcept
Const interator of first element.
Definition: simplevectors.hpp:561
T & at(const std::size_t index)
Sets value of a certain component.
Definition: simplevectors.hpp:539
constexpr std::size_t numDimensions() const
Gets the number of dimensions.
Definition: simplevectors.hpp:482
const T & operator[](const std::size_t index) const
Value of a certain component of a vector.
Definition: simplevectors.hpp:501
A simple 2D vector representation.
Definition: simplevectors.hpp:721
double angle() const
Angle of vector.
Definition: simplevectors.hpp:789
T componentsAs() const
Converts vector to another object.
Definition: simplevectors.hpp:828
double x() const
Gets x-component.
Definition: simplevectors.hpp:751
Vector2D rotate(const double ang) const
Rotates vector by a certain angle.
Definition: simplevectors.hpp:802
Vector2D(const Vec2_ &other)
Copy constructor for base class.
Definition: simplevectors.hpp:739
void y(const double &newY)
Sets y-component.
Definition: simplevectors.hpp:778
Vector2D(const double x, const double y)
Initializes a vector given xy components.
Definition: simplevectors.hpp:731
void x(const double &newX)
Sets x-component.
Definition: simplevectors.hpp:760
double y() const
Gets y-component.
Definition: simplevectors.hpp:769
A simple 3D vector representation.
Definition: simplevectors.hpp:838
T anglesAs() const
Converts angles to another object.
Definition: simplevectors.hpp:961
Vector3D(const double x, const double y, const double z)
Initializes a vector given xyz components.
Definition: simplevectors.hpp:849
Vector3D rotate(const double &ang) const
Rotates vector around a certain axis by a certain angle.
Definition: simplevectors.hpp:1008
Vector3D(const Vec3_ &other)
Copy constructor for the base class.
Definition: simplevectors.hpp:858
void z(const double &newZ)
Sets z-component.
Definition: simplevectors.hpp:916
Vector3D cross(const Vector3D &other) const
Cross product of two vectors.
Definition: simplevectors.hpp:925
T componentsAs() const
Converts vector to another object.
Definition: simplevectors.hpp:945
double z() const
Gets z-component.
Definition: simplevectors.hpp:907
void y(const double &newY)
Sets y-component.
Definition: simplevectors.hpp:898
double angle() const
Gets a specific angle of the vector.
Definition: simplevectors.hpp:980
void x(const double &newX)
Sets x-component.
Definition: simplevectors.hpp:880
double x() const
Gets x-component.
Definition: simplevectors.hpp:871
double y() const
Gets y-component.
Definition: simplevectors.hpp:889