imunano33  0.1.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 };
66 template <std::size_t D, typename T = double> class Vector {
67 public:
68  // makes sure that type is numeric
69  static_assert(std::is_arithmetic<T>::value, "Vector type must be numeric");
70 
71 #ifdef SVECTOR_EXPERIMENTAL_COMPARE
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 
81  template <std::size_t D1, std::size_t D2, typename T1, typename T2>
82  friend bool operator>=(const Vector<D1, T1> &, const Vector<D2, T2> &);
83 #endif
84 
85  typedef
86  typename std::array<T, D>::iterator iterator;
87  typedef typename std::array<T, D>::const_iterator
89  typedef typename std::array<T, D>::reverse_iterator
91  typedef typename std::array<T, D>::const_reverse_iterator
93 
99  Vector() { this->m_components.fill(0); }
100 
113  Vector(const std::initializer_list<T> args) {
114  // in case length of args < dimensions
115  this->m_components.fill(0);
116 
117  std::size_t counter = 0;
118  for (const auto &num : args) {
119  if (counter >= D) {
120  break;
121  }
122 
123  this->m_components[counter] = num;
124  counter++;
125  }
126  }
127 
133  Vector(const Vector<D, T> &other) {
134  for (std::size_t i = 0; i < D; i++) {
135  this->m_components[i] = other[i];
136  }
137  }
138 
144  Vector(Vector<D, T> &&) noexcept = default;
145 
151  Vector<D, T> &operator=(const Vector<D, T> &other) {
152  // check if assigning to self
153  if (this == &other) {
154  return *this;
155  }
156 
157  for (std::size_t i = 0; i < D; i++) {
158  this->m_components[i] = other[i];
159  }
160 
161  return *this;
162  }
163 
169  Vector<D, T> &operator=(Vector<D, T> &&) noexcept = default;
170 
176  virtual ~Vector() = default;
177 
185  virtual std::string toString() const {
186  std::string str = "<";
187  for (std::size_t i = 0; i < D - 1; i++) {
188  str += std::to_string(this->m_components[i]);
189  str += ", ";
190  }
191 
192  str += std::to_string(this->m_components[D - 1]);
193  str += ">";
194 
195  return str;
196  }
197 
198 #ifdef SVECTOR_USE_CLASS_OPERATORS
215  Vector<D, T> operator+(const Vector<D, T> &other) const {
216  Vector<D, T> tmp;
217  for (std::size_t i = 0; i < D; i++) {
218  tmp[i] = this->m_components[i] + other[i];
219  }
220 
221  return tmp;
222  }
223 
240  Vector<D, T> operator-(const Vector<D, T> &other) const {
241  Vector<D, T> tmp;
242  for (std::size_t i = 0; i < D; i++) {
243  tmp[i] = this->m_components[i] - other[i];
244  }
245 
246  return tmp;
247  }
248 
262  Vector<D, T> operator*(const T other) const {
263  Vector<D, T> tmp;
264  for (std::size_t i = 0; i < D; i++) {
265  tmp[i] = this->m_components[i] * other;
266  }
267 
268  return tmp;
269  }
270 
284  Vector<D, T> operator/(const T other) const {
285  Vector<D, T> tmp;
286  for (std::size_t i = 0; i < D; i++) {
287  tmp[i] = this->m_components[i] / other;
288  }
289 
290  return tmp;
291  }
292 
306  bool operator==(const Vector<D, T> &other) const {
307  for (std::size_t i = 0; i < D; i++) {
308  if (this->m_components[i] != other[i]) {
309  return false;
310  }
311  }
312 
313  return true;
314  }
315 
329  bool operator!=(const Vector<D, T> &other) const {
330  return !((*this) == other);
331  }
332 #endif
333 
344  Vector<D, T> tmp;
345  for (std::size_t i = 0; i < D; i++) {
346  tmp[i] = -this->m_components[i];
347  }
348 
349  return tmp;
350  }
351 
361  Vector<D, T> tmp;
362  for (std::size_t i = 0; i < D; i++) {
363  tmp[i] = +this->m_components[i];
364  }
365 
366  return tmp;
367  }
368 
377  for (std::size_t i = 0; i < D; i++) {
378  this->m_components[i] += other[i];
379  }
380 
381  return *this;
382  }
383 
392  for (std::size_t i = 0; i < D; i++) {
393  this->m_components[i] -= other[i];
394  }
395 
396  return *this;
397  }
398 
406  Vector<D, T> &operator*=(const T other) {
407  for (std::size_t i = 0; i < D; i++) {
408  this->m_components[i] *= other;
409  }
410 
411  return *this;
412  }
413 
421  Vector<D, T> &operator/=(const T other) {
422  for (std::size_t i = 0; i < D; i++) {
423  this->m_components[i] /= other;
424  }
425 
426  return *this;
427  }
428 
441  T dot(const Vector<D, T> &other) const {
442  T result = 0;
443 
444  for (std::size_t i = 0; i < D; i++) {
445  result += this->m_components[i] * other[i];
446  }
447 
448  return result;
449  }
450 
458  T magn() const {
459  T sum_of_squares = 0;
460 
461  for (const auto &i : this->m_components) {
462  sum_of_squares += i * i;
463  }
464 
465  return std::sqrt(sum_of_squares);
466  };
467 
478  Vector<D, T> normalize() const { return (*this) / this->magn(); }
479 
485  constexpr std::size_t numDimensions() const { return D; }
486 
492  bool isZero() const { return this->magn() == 0; }
493 
504  const T &operator[](const std::size_t index) const {
505  return this->m_components[index];
506  }
507 
515  T &operator[](const std::size_t index) { return this->m_components[index]; }
516 
529  const T &at(const std::size_t index) const {
530  return this->m_components.at(index);
531  }
532 
542  T &at(const std::size_t index) { return this->m_components.at(index); }
543 
554  iterator begin() noexcept { return iterator{this->m_components.begin()}; }
555 
564  const_iterator begin() const noexcept {
565  return const_iterator{this->m_components.begin()};
566  }
567 
581  iterator end() noexcept { return iterator{this->m_components.end()}; }
582 
594  const_iterator end() const noexcept {
595  return const_iterator{this->m_components.end()};
596  }
597 
610  return reverse_iterator{this->m_components.rbegin()};
611  }
612 
622  const_reverse_iterator rbegin() const noexcept {
623  return const_reverse_iterator{this->m_components.rbegin()};
624  }
625 
640  reverse_iterator rend() noexcept {
641  return reverse_iterator{this->m_components.rend()};
642  }
643 
657  const_reverse_iterator rend() const noexcept {
658  return const_reverse_iterator{this->m_components.rend()};
659  }
660 
661 protected:
662  std::array<T, D> m_components;
663 
664 #ifdef SVECTOR_EXPERIMENTAL_COMPARE
665 private:
686  template <std::size_t D2, typename T2>
687  std::int8_t compare(const Vector<D2, T2> &other) const noexcept {
688  std::size_t min_dim = std::min(D, D2);
689  std::size_t counter = 0;
690 
691 // to suppress MSVC warning
692 #ifdef _MSC_VER
693  if constexpr (D != D2) {
694 #else
695  if (D != D2) {
696 #endif
697  // check dimensions first
698  return D < D2 ? -1 : 1;
699  }
700 
701  // compare one by one
702  for (std::size_t i = 0; i < min_dim; i++) {
703  if (this->m_components[i] == other[i]) {
704  counter++;
705  } else if (this->m_components[i] < other[i]) {
706  return -1;
707  } else {
708  return 1;
709  }
710  }
711 
712  if (counter != D || counter != D2) {
713  return -1;
714  }
715 
716  // means two vectors are equal
717  return 0;
718  }
719 #endif
720 };
721 
722 typedef Vector<2> Vec2_;
723 
727 class Vector2D : public Vec2_ {
728 public:
729  using Vec2_::Vector;
730 
737  Vector2D(const double x, const double y) {
738  this->m_components[0] = x;
739  this->m_components[1] = y;
740  }
741 
745  Vector2D(const Vec2_ &other) {
746  this->m_components[0] = other[0];
747  this->m_components[1] = other[1];
748  }
749 
757  double x() const { return this->m_components[0]; }
758 
766  void x(const double &newX) { this->m_components[0] = newX; }
767 
775  double y() const { return this->m_components[1]; }
776 
784  void y(const double &newY) { this->m_components[1] = newY; }
785 
795  double angle() const { return std::atan2(this->y(), this->x()); }
796 
808  Vector2D rotate(const double ang) const {
809  //
810  // Rotation matrix:
811  //
812  // | cos(ang) -sin(ang) | |x|
813  // | sin(ang) cos(ang) | |y|
814  //
815 
816  const double xPrime = this->x() * std::cos(ang) - this->y() * std::sin(ang);
817  const double yPrime = this->x() * std::sin(ang) + this->y() * std::cos(ang);
818 
819  return Vector2D{xPrime, yPrime};
820  }
821 
834  template <typename T> T componentsAs() const {
835  return T{this->x(), this->y()};
836  }
837 };
838 
839 typedef Vector<3> Vec3_;
840 
844 class Vector3D : public Vec3_ {
845 public:
846  using Vec3_::Vector;
847 
855  Vector3D(const double x, const double y, const double z) {
856  this->m_components[0] = x;
857  this->m_components[1] = y;
858  this->m_components[2] = z;
859  }
860 
864  Vector3D(const Vec3_ &other) {
865  this->m_components[0] = other[0];
866  this->m_components[1] = other[1];
867  this->m_components[2] = other[2];
868  }
869 
877  double x() const { return this->m_components[0]; }
878 
886  void x(const double &newX) { this->m_components[0] = newX; }
887 
895  double y() const { return this->m_components[1]; }
896 
904  void y(const double &newY) { this->m_components[1] = newY; }
905 
913  double z() const { return this->m_components[2]; }
914 
922  void z(const double &newZ) { this->m_components[2] = newZ; }
923 
931  Vector3D cross(const Vector3D &other) const {
932  const double newx = this->y() * other.z() - this->z() * other.y();
933  const double newy = this->z() * other.x() - this->x() * other.z();
934  const double newz = this->x() * other.y() - this->y() * other.x();
935 
936  return Vector3D{newx, newy, newz};
937  }
938 
951  template <typename T> T componentsAs() const {
952  return T{this->x(), this->y(), this->z()};
953  }
954 
967  template <typename T> T anglesAs() const {
968  return T{this->getAlpha(), this->getBeta(), this->getGamma()};
969  }
970 
986  template <AngleDir D> double angle() const {
987  switch (D) {
988  case ALPHA:
989  return this->getAlpha();
990  case BETA:
991  return this->getBeta();
992  default:
993  return this->getGamma();
994  }
995  }
996 
1014  template <AngleDir D> Vector3D rotate(const double &ang) const {
1015  switch (D) {
1016  case ALPHA:
1017  return this->rotateAlpha(ang);
1018  case BETA:
1019  return this->rotateBeta(ang);
1020  default:
1021  return this->rotateGamma(ang);
1022  }
1023  }
1024 
1025 private:
1033  double getAlpha() const { return std::acos(this->x() / this->magn()); }
1034 
1042  double getBeta() const { return std::acos(this->y() / this->magn()); }
1043 
1051  double getGamma() const { return std::acos(this->z() / this->magn()); }
1052 
1056  Vector3D rotateAlpha(const double &ang) const {
1065  const double xPrime = this->x();
1066  const double yPrime = this->y() * std::cos(ang) - this->z() * std::sin(ang);
1067  const double zPrime = this->y() * std::sin(ang) + this->z() * std::cos(ang);
1068 
1069  return Vector3D{xPrime, yPrime, zPrime};
1070  }
1071 
1075  Vector3D rotateBeta(const double &ang) const {
1084  const double xPrime = this->x() * std::cos(ang) + this->z() * std::sin(ang);
1085  const double yPrime = this->y();
1086  const double zPrime =
1087  -this->x() * std::sin(ang) + this->z() * std::cos(ang);
1088 
1089  return Vector3D{xPrime, yPrime, zPrime};
1090  }
1091 
1095  Vector3D rotateGamma(const double &ang) const {
1104  const double xPrime = this->x() * std::cos(ang) - this->y() * std::sin(ang);
1105  const double yPrime = this->x() * std::sin(ang) + this->y() * std::cos(ang);
1106  const double zPrime = this->z();
1107 
1108  return Vector3D{xPrime, yPrime, zPrime};
1109  }
1110 };
1111 
1121 template <std::size_t D, typename T>
1122 Vector<D, T> makeVector(std::array<T, D> array) {
1123  Vector<D, T> vec;
1124  for (std::size_t i = 0; i < D; i++) {
1125  vec[i] = array[i];
1126  }
1127 
1128  return vec;
1129 }
1130 
1147 template <std::size_t D, typename T>
1148 Vector<D, T> makeVector(std::vector<T> vector) {
1149  Vector<D, T> vec;
1150  for (std::size_t i = 0; i < std::min(D, vector.size()); i++) {
1151  vec[i] = vector[i];
1152  }
1153 
1154  return vec;
1155 }
1156 
1174 template <std::size_t D, typename T>
1175 Vector<D, T> makeVector(const std::initializer_list<T> args) {
1176  Vector<D, T> vec(args);
1177  return vec;
1178 }
1179 
1187 inline double x(const Vector2D &v) { return v[0]; }
1188 
1195 inline void x(Vector2D &v, const double xValue) { v[0] = xValue; }
1196 
1204 inline double x(const Vector3D &v) { return v[0]; }
1205 
1212 inline void x(Vector3D &v, const double xValue) { v[0] = xValue; }
1213 
1221 inline double y(const Vector2D &v) { return v[1]; }
1222 
1229 inline void y(Vector2D &v, const double yValue) { v[1] = yValue; }
1230 
1238 inline double y(const Vector3D &v) { return v[1]; }
1239 
1246 inline void y(Vector3D &v, const double yValue) { v[1] = yValue; }
1247 
1255 inline double z(const Vector3D &v) { return v[2]; }
1256 
1263 inline void z(Vector3D &v, const double zValue) { v[2] = zValue; }
1264 
1278 template <typename T, std::size_t D>
1279 inline T dot(const Vector<D, T> &lhs, const Vector<D, T> &rhs) {
1280  T result = 0;
1281 
1282  for (std::size_t i = 0; i < D; i++) {
1283  result += lhs[i] * rhs[i];
1284  }
1285 
1286  return result;
1287 }
1288 
1299 template <typename T, std::size_t D> inline T magn(const Vector<D, T> &v) {
1300  T sum_of_squares = 0;
1301 
1302  for (std::size_t i = 0; i < D; i++) {
1303  sum_of_squares += v[i] * v[i];
1304  }
1305 
1306  return std::sqrt(sum_of_squares);
1307 }
1308 
1324 template <typename T, std::size_t D>
1326  return v / magn(v);
1327 }
1328 
1337 template <typename T, std::size_t D> inline bool isZero(const Vector<D, T> &v) {
1338  return magn(v) == 0;
1339 }
1340 
1350 inline double angle(const Vector2D &v) { return std::atan2(y(v), x(v)); }
1351 
1364 inline Vector2D rotate(const Vector2D &v, const double ang) {
1365  //
1366  // Rotation matrix:
1367  //
1368  // | cos(ang) -sin(ang) | |x|
1369  // | sin(ang) cos(ang) | |y|
1370  //
1371 
1372  const double xPrime = x(v) * std::cos(ang) - y(v) * std::sin(ang);
1373  const double yPrime = x(v) * std::sin(ang) + y(v) * std::cos(ang);
1374 
1375  return Vector2D{xPrime, yPrime};
1376 }
1377 
1386 inline Vector3D cross(const Vector3D &lhs, const Vector3D &rhs) {
1387  const double newx = y(lhs) * z(rhs) - z(lhs) * y(rhs);
1388  const double newy = z(lhs) * x(rhs) - x(lhs) * z(rhs);
1389  const double newz = x(lhs) * y(rhs) - y(lhs) * x(rhs);
1390 
1391  return Vector3D{newx, newy, newz};
1392 }
1393 
1406 inline double alpha(const Vector3D &v) { return std::acos(x(v) / magn(v)); }
1407 
1420 inline double beta(const Vector3D &v) { return std::acos(y(v) / magn(v)); }
1421 
1434 inline double gamma(const Vector3D &v) { return std::acos(z(v) / magn(v)); }
1435 
1446 inline Vector3D rotateAlpha(const Vector3D &v, const double &ang) {
1447  //
1448  // Rotation matrix:
1449  //
1450  // |1 0 0 | |x|
1451  // |0 cos(ang) −sin(ang)| |y|
1452  // |0 sin(ang) cos(ang)| |z|
1453  //
1454 
1455  const double xPrime = x(v);
1456  const double yPrime = y(v) * std::cos(ang) - z(v) * std::sin(ang);
1457  const double zPrime = y(v) * std::sin(ang) + z(v) * std::cos(ang);
1458 
1459  return Vector3D{xPrime, yPrime, zPrime};
1460 }
1461 
1472 inline Vector3D rotateBeta(const Vector3D &v, const double &ang) {
1473  //
1474  // Rotation matrix:
1475  //
1476  // | cos(ang) 0 sin(ang)| |x|
1477  // | 0 1 0 | |y|
1478  // |−sin(ang) 0 cos(ang)| |z|
1479  //
1480 
1481  const double xPrime = x(v) * std::cos(ang) + z(v) * std::sin(ang);
1482  const double yPrime = y(v);
1483  const double zPrime = -x(v) * std::sin(ang) + z(v) * std::cos(ang);
1484 
1485  return Vector3D{xPrime, yPrime, zPrime};
1486 }
1487 
1498 inline Vector3D rotateGamma(const Vector3D &v, const double &ang) {
1499  //
1500  // Rotation matrix:
1501  //
1502  // |cos(ang) −sin(ang) 0| |x|
1503  // |sin(ang) cos(ang) 0| |y|
1504  // | 0 0 1| |z|
1505  //
1506 
1507  const double xPrime = x(v) * std::cos(ang) - y(v) * std::sin(ang);
1508  const double yPrime = x(v) * std::sin(ang) + y(v) * std::cos(ang);
1509  const double zPrime = z(v);
1510 
1511  return Vector3D{xPrime, yPrime, zPrime};
1512 }
1513 
1514 #ifndef SVECTOR_USE_CLASS_OPERATORS
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 
1564 template <typename T, std::size_t D>
1566  const Vector<D, T> &rhs) {
1567  Vector<D, T> tmp;
1568  for (std::size_t i = 0; i < D; i++) {
1569  tmp[i] = lhs[i] - rhs[i];
1570  }
1571 
1572  return tmp;
1573 }
1574 
1593 template <typename T, typename T2, std::size_t D>
1594 inline Vector<D, T> operator*(const Vector<D, T> &lhs, const T2 rhs) {
1595  Vector<D, T> tmp;
1596  for (std::size_t i = 0; i < D; i++) {
1597  tmp[i] = lhs[i] * rhs;
1598  }
1599 
1600  return tmp;
1601 }
1602 
1621 template <typename T, typename T2, std::size_t D>
1622 inline Vector<D, T> operator/(const Vector<D, T> &lhs, const T2 rhs) {
1623  Vector<D, T> tmp;
1624  for (std::size_t i = 0; i < D; i++) {
1625  tmp[i] = lhs[i] / rhs;
1626  }
1627 
1628  return tmp;
1629 }
1630 
1647 template <typename T, std::size_t D>
1648 inline bool operator==(const Vector<D, T> &lhs, const Vector<D, T> &rhs) {
1649  for (std::size_t i = 0; i < D; i++) {
1650  if (lhs[i] != rhs[i]) {
1651  return false;
1652  }
1653  }
1654 
1655  return true;
1656 }
1657 
1674 template <typename T, std::size_t D>
1675 inline bool operator!=(const Vector<D, T> &lhs, const Vector<D, T> &rhs) {
1676  return !(lhs == rhs);
1677 }
1678 #endif
1679 
1680 #ifdef SVECTOR_EXPERIMENTAL_COMPARE
1681 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
1682 bool operator<(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
1683  return lhs.compare(rhs) < 0;
1684 }
1685 
1686 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
1687 bool operator>(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
1688  return lhs.compare(rhs) > 0;
1689 }
1690 
1691 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
1692 bool operator<=(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
1693  return lhs.compare(rhs) <= 0;
1694 }
1695 
1696 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
1697 bool operator>=(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
1698  return lhs.compare(rhs) >= 0;
1699 }
1700 #endif
1701 } // namespace svector
1702 
1703 #endif
1704 
bool operator==(const Quaternion &lhs, const Quaternion &rhs)
Equality of two quaternions.
Definition: quaternion.hpp:212
bool operator!=(const Quaternion &lhs, const Quaternion &rhs)
Inequality of two quaternions.
Definition: quaternion.hpp:224
Quaternion operator*(const Quaternion &lhs, const Quaternion &rhs)
Product of two quaternions.
Definition: quaternion.hpp:187
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:1187
double alpha(const Vector3D &v)
Gets α angle.
Definition: simplevectors.hpp:1406
Vector3D cross(const Vector3D &lhs, const Vector3D &rhs)
Cross product of two vectors.
Definition: simplevectors.hpp:1386
double angle(const Vector2D &v)
Gets the angle of a 2D vector in radians.
Definition: simplevectors.hpp:1350
Vector3D rotateAlpha(const Vector3D &v, const double &ang)
Rotates around x-axis.
Definition: simplevectors.hpp:1446
Vector3D rotateGamma(const Vector3D &v, const double &ang)
Rotates around z-axis.
Definition: simplevectors.hpp:1498
Vector2D rotate(const Vector2D &v, const double ang)
Rotates a 2D vector by a certain angle.
Definition: simplevectors.hpp:1364
double beta(const Vector3D &v)
Gets β angle.
Definition: simplevectors.hpp:1420
double y(const Vector2D &v)
Gets the y-component of a 2D vector.
Definition: simplevectors.hpp:1221
Vector3D rotateBeta(const Vector3D &v, const double &ang)
Rotates around y-axis.
Definition: simplevectors.hpp:1472
double z(const Vector3D &v)
Gets the z-component of a 3D vector.
Definition: simplevectors.hpp:1255
Vector< D, T > makeVector(std::array< T, D > array)
Creates a vector from an std::array.
Definition: simplevectors.hpp:1122
Vector< D, T > operator/(const Vector< D, T > &lhs, const T2 rhs)
Scalar division.
Definition: simplevectors.hpp:1622
double gamma(const Vector3D &v)
Gets γ angle.
Definition: simplevectors.hpp:1434
A base vector representation.
Definition: simplevectors.hpp:66
std::array< T, D >::const_reverse_iterator const_reverse_iterator
An std::array::const_reverse_iterator.
Definition: simplevectors.hpp:92
const T & at(const std::size_t index) const
Value of a certain component of a vector.
Definition: simplevectors.hpp:529
Vector()
No-argument constructor.
Definition: simplevectors.hpp:99
std::array< T, D >::const_iterator const_iterator
An std::array::const_iterator.
Definition: simplevectors.hpp:88
Vector< D, T > operator+() const
Positive of a vector.
Definition: simplevectors.hpp:360
reverse_iterator rend() noexcept
Reverse iterator to first element - 1.
Definition: simplevectors.hpp:640
Vector(Vector< D, T > &&) noexcept=default
Move constructor.
reverse_iterator rbegin() noexcept
Reverse iterator to last element.
Definition: simplevectors.hpp:609
T magn() const
Magnitude.
Definition: simplevectors.hpp:458
Vector< D, T > & operator+=(const Vector< D, T > &other)
In-place addition.
Definition: simplevectors.hpp:376
const_reverse_iterator rbegin() const noexcept
Const reverse iterator to last element.
Definition: simplevectors.hpp:622
iterator begin() noexcept
Iterator of first element.
Definition: simplevectors.hpp:554
std::array< T, D >::iterator iterator
An std::array::iterator.
Definition: simplevectors.hpp:69
const_iterator end() const noexcept
Const interator of last element + 1.
Definition: simplevectors.hpp:594
Vector< D, T > normalize() const
Normalizes a vector.
Definition: simplevectors.hpp:478
Vector< D, T > & operator-=(const Vector< D, T > &other)
In-place subtraction.
Definition: simplevectors.hpp:391
const_reverse_iterator rend() const noexcept
Const reverse iterator to first element - 1.
Definition: simplevectors.hpp:657
virtual std::string toString() const
Returns string form of vector.
Definition: simplevectors.hpp:185
Vector< D, T > operator-() const
Negative of a vector.
Definition: simplevectors.hpp:343
Vector(const std::initializer_list< T > args)
Initializes a vector given initializer list.
Definition: simplevectors.hpp:113
Vector< D, T > & operator=(Vector< D, T > &&) noexcept=default
Move assignment operator.
iterator end() noexcept
Interator of last element + 1.
Definition: simplevectors.hpp:581
Vector(const Vector< D, T > &other)
Copy constructor.
Definition: simplevectors.hpp:133
std::array< T, D > m_components
An array of components for the vector.
Definition: simplevectors.hpp:662
T dot(const Vector< D, T > &other) const
Dot product.
Definition: simplevectors.hpp:441
Vector< D, T > & operator/=(const T other)
In-place scalar division.
Definition: simplevectors.hpp:421
Vector< D, T > & operator*=(const T other)
In-place scalar multiplication.
Definition: simplevectors.hpp:406
std::array< T, D >::reverse_iterator reverse_iterator
An std::array::reverse_iterator.
Definition: simplevectors.hpp:90
T & operator[](const std::size_t index)
Sets value of a certain component.
Definition: simplevectors.hpp:515
bool isZero() const
Determines whether the current vector is a zero vector.
Definition: simplevectors.hpp:492
const_iterator begin() const noexcept
Const interator of first element.
Definition: simplevectors.hpp:564
T & at(const std::size_t index)
Sets value of a certain component.
Definition: simplevectors.hpp:542
constexpr std::size_t numDimensions() const
Gets the number of dimensions.
Definition: simplevectors.hpp:485
const T & operator[](const std::size_t index) const
Value of a certain component of a vector.
Definition: simplevectors.hpp:504
A simple 2D vector representation.
Definition: simplevectors.hpp:727
double angle() const
Angle of vector.
Definition: simplevectors.hpp:795
T componentsAs() const
Converts vector to another object.
Definition: simplevectors.hpp:834
double x() const
Gets x-component.
Definition: simplevectors.hpp:757
Vector2D rotate(const double ang) const
Rotates vector by a certain angle.
Definition: simplevectors.hpp:808
Vector2D(const Vec2_ &other)
Copy constructor for base class.
Definition: simplevectors.hpp:745
void y(const double &newY)
Sets y-component.
Definition: simplevectors.hpp:784
Vector2D(const double x, const double y)
Initializes a vector given xy components.
Definition: simplevectors.hpp:737
void x(const double &newX)
Sets x-component.
Definition: simplevectors.hpp:766
double y() const
Gets y-component.
Definition: simplevectors.hpp:775
A simple 3D vector representation.
Definition: simplevectors.hpp:844
T anglesAs() const
Converts angles to another object.
Definition: simplevectors.hpp:967
Vector3D(const double x, const double y, const double z)
Initializes a vector given xyz components.
Definition: simplevectors.hpp:855
Vector3D rotate(const double &ang) const
Rotates vector around a certain axis by a certain angle.
Definition: simplevectors.hpp:1014
Vector3D(const Vec3_ &other)
Copy constructor for the base class.
Definition: simplevectors.hpp:864
void z(const double &newZ)
Sets z-component.
Definition: simplevectors.hpp:922
Vector3D cross(const Vector3D &other) const
Cross product of two vectors.
Definition: simplevectors.hpp:931
T componentsAs() const
Converts vector to another object.
Definition: simplevectors.hpp:951
double z() const
Gets z-component.
Definition: simplevectors.hpp:913
void y(const double &newY)
Sets y-component.
Definition: simplevectors.hpp:904
double angle() const
Gets a specific angle of the vector.
Definition: simplevectors.hpp:986
void x(const double &newX)
Sets x-component.
Definition: simplevectors.hpp:886
double x() const
Gets x-component.
Definition: simplevectors.hpp:877
double y() const
Gets y-component.
Definition: simplevectors.hpp:895