simplevectors  0.3.9
Simple vector implementations in C++
functions.hpp
Go to the documentation of this file.
1 
13 #ifndef INCLUDE_SVECTOR_FUNCTIONS_HPP_
14 #define INCLUDE_SVECTOR_FUNCTIONS_HPP_
15 
16 #include <algorithm> // std::min
17 #include <array> // std::array
18 #include <cmath> // std::atan2, std::acos, std::sqrt
19 #include <cstddef> // std::size_t
20 #include <initializer_list> // std::initializer_list
21 #include <vector> // std::vector
22 
26 
27 namespace svector {
28 // COMBINER_PY_START
29 
39 template <std::size_t D, typename T>
40 Vector<D, T> makeVector(std::array<T, D> array) {
41  Vector<D, T> vec;
42  for (std::size_t i = 0; i < D; i++) {
43  vec[i] = array[i];
44  }
45 
46  return vec;
47 }
48 
65 template <std::size_t D, typename T>
66 Vector<D, T> makeVector(std::vector<T> vector) {
67  Vector<D, T> vec;
68  for (std::size_t i = 0; i < std::min(D, vector.size()); i++) {
69  vec[i] = vector[i];
70  }
71 
72  return vec;
73 }
74 
92 template <std::size_t D, typename T>
93 Vector<D, T> makeVector(const std::initializer_list<T> args) {
94  Vector<D, T> vec(args);
95  return vec;
96 }
97 
105 inline double x(const Vector2D &v) { return v[0]; }
106 
113 inline void x(Vector2D &v, const double xValue) { v[0] = xValue; }
114 
122 inline double x(const Vector3D &v) { return v[0]; }
123 
130 inline void x(Vector3D &v, const double xValue) { v[0] = xValue; }
131 
139 inline double y(const Vector2D &v) { return v[1]; }
140 
147 inline void y(Vector2D &v, const double yValue) { v[1] = yValue; }
148 
156 inline double y(const Vector3D &v) { return v[1]; }
157 
164 inline void y(Vector3D &v, const double yValue) { v[1] = yValue; }
165 
173 inline double z(const Vector3D &v) { return v[2]; }
174 
181 inline void z(Vector3D &v, const double zValue) { v[2] = zValue; }
182 
196 template <typename T, std::size_t D>
197 inline T dot(const Vector<D, T> &lhs, const Vector<D, T> &rhs) {
198  T result = 0;
199 
200  for (std::size_t i = 0; i < D; i++) {
201  result += lhs[i] * rhs[i];
202  }
203 
204  return result;
205 }
206 
217 template <typename T, std::size_t D> inline T magn(const Vector<D, T> &v) {
218  T sum_of_squares = 0;
219 
220  for (std::size_t i = 0; i < D; i++) {
221  sum_of_squares += v[i] * v[i];
222  }
223 
224  return std::sqrt(sum_of_squares);
225 }
226 
242 template <typename T, std::size_t D>
244  return v / magn(v);
245 }
246 
255 template <typename T, std::size_t D> inline bool isZero(const Vector<D, T> &v) {
256  return magn(v) == 0;
257 }
258 
268 inline double angle(const Vector2D &v) { return std::atan2(y(v), x(v)); }
269 
282 inline Vector2D rotate(const Vector2D &v, const double ang) {
283  //
284  // Rotation matrix:
285  //
286  // | cos(ang) -sin(ang) | |x|
287  // | sin(ang) cos(ang) | |y|
288  //
289 
290  const double xPrime = x(v) * std::cos(ang) - y(v) * std::sin(ang);
291  const double yPrime = x(v) * std::sin(ang) + y(v) * std::cos(ang);
292 
293  return Vector2D{xPrime, yPrime};
294 }
295 
304 inline Vector3D cross(const Vector3D &lhs, const Vector3D &rhs) {
305  const double newx = y(lhs) * z(rhs) - z(lhs) * y(rhs);
306  const double newy = z(lhs) * x(rhs) - x(lhs) * z(rhs);
307  const double newz = x(lhs) * y(rhs) - y(lhs) * x(rhs);
308 
309  return Vector3D{newx, newy, newz};
310 }
311 
324 inline double alpha(const Vector3D &v) { return std::acos(x(v) / magn(v)); }
325 
338 inline double beta(const Vector3D &v) { return std::acos(y(v) / magn(v)); }
339 
352 inline double gamma(const Vector3D &v) { return std::acos(z(v) / magn(v)); }
353 
364 inline Vector3D rotateAlpha(const Vector3D &v, const double &ang) {
365  //
366  // Rotation matrix:
367  //
368  // |1 0 0 | |x|
369  // |0 cos(ang) −sin(ang)| |y|
370  // |0 sin(ang) cos(ang)| |z|
371  //
372 
373  const double xPrime = x(v);
374  const double yPrime = y(v) * std::cos(ang) - z(v) * std::sin(ang);
375  const double zPrime = y(v) * std::sin(ang) + z(v) * std::cos(ang);
376 
377  return Vector3D{xPrime, yPrime, zPrime};
378 }
379 
390 inline Vector3D rotateBeta(const Vector3D &v, const double &ang) {
391  //
392  // Rotation matrix:
393  //
394  // | cos(ang) 0 sin(ang)| |x|
395  // | 0 1 0 | |y|
396  // |−sin(ang) 0 cos(ang)| |z|
397  //
398 
399  const double xPrime = x(v) * std::cos(ang) + z(v) * std::sin(ang);
400  const double yPrime = y(v);
401  const double zPrime = -x(v) * std::sin(ang) + z(v) * std::cos(ang);
402 
403  return Vector3D{xPrime, yPrime, zPrime};
404 }
405 
416 inline Vector3D rotateGamma(const Vector3D &v, const double &ang) {
417  //
418  // Rotation matrix:
419  //
420  // |cos(ang) −sin(ang) 0| |x|
421  // |sin(ang) cos(ang) 0| |y|
422  // | 0 0 1| |z|
423  //
424 
425  const double xPrime = x(v) * std::cos(ang) - y(v) * std::sin(ang);
426  const double yPrime = x(v) * std::sin(ang) + y(v) * std::cos(ang);
427  const double zPrime = z(v);
428 
429  return Vector3D{xPrime, yPrime, zPrime};
430 }
431 
432 #ifndef SVECTOR_USE_CLASS_OPERATORS
452 template <typename T, std::size_t D>
454  const Vector<D, T> &rhs) {
455  Vector<D, T> tmp;
456  for (std::size_t i = 0; i < D; i++) {
457  tmp[i] = lhs[i] + rhs[i];
458  }
459 
460  return tmp;
461 }
462 
482 template <typename T, std::size_t D>
484  const Vector<D, T> &rhs) {
485  Vector<D, T> tmp;
486  for (std::size_t i = 0; i < D; i++) {
487  tmp[i] = lhs[i] - rhs[i];
488  }
489 
490  return tmp;
491 }
492 
511 template <typename T, typename T2, std::size_t D>
512 inline Vector<D, T> operator*(const Vector<D, T> &lhs, const T2 rhs) {
513  Vector<D, T> tmp;
514  for (std::size_t i = 0; i < D; i++) {
515  tmp[i] = lhs[i] * rhs;
516  }
517 
518  return tmp;
519 }
520 
539 template <typename T, typename T2, std::size_t D>
540 inline Vector<D, T> operator/(const Vector<D, T> &lhs, const T2 rhs) {
541  Vector<D, T> tmp;
542  for (std::size_t i = 0; i < D; i++) {
543  tmp[i] = lhs[i] / rhs;
544  }
545 
546  return tmp;
547 }
548 
565 template <typename T, std::size_t D>
566 inline bool operator==(const Vector<D, T> &lhs, const Vector<D, T> &rhs) {
567  for (std::size_t i = 0; i < D; i++) {
568  if (lhs[i] != rhs[i]) {
569  return false;
570  }
571  }
572 
573  return true;
574 }
575 
592 template <typename T, std::size_t D>
593 inline bool operator!=(const Vector<D, T> &lhs, const Vector<D, T> &rhs) {
594  return !(lhs == rhs);
595 }
596 #endif
597 
598 #ifdef SVECTOR_EXPERIMENTAL_COMPARE
599 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
600 bool operator<(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
601  return lhs.compare(rhs) < 0;
602 }
603 
604 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
605 bool operator>(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
606  return lhs.compare(rhs) > 0;
607 }
608 
609 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
610 bool operator<=(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
611  return lhs.compare(rhs) <= 0;
612 }
613 
614 template <std::size_t D1, std::size_t D2, typename T1, typename T2>
615 bool operator>=(const Vector<D1, T1> &lhs, const Vector<D2, T2> &rhs) {
616  return lhs.compare(rhs) >= 0;
617 }
618 #endif
619 // COMBINER_PY_END
620 } // namespace svector
621 
622 #endif
Contains a base vector representation.
Contains a 2D vector representation.
Contains a 3D vector representation.
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
Vector< D, T > makeVector(std::array< T, D > array)
Creates a vector from an std::array.
Definition: functions.hpp:40
A base vector representation.
Definition: vector.hpp:34
A simple 2D vector representation.
Definition: vector2d.hpp:25
A simple 3D vector representation.
Definition: vector3d.hpp:26