simplevectors  0.3.9
Simple vector implementations in C++
vector3d.hpp
Go to the documentation of this file.
1 
10 #ifndef INCLUDE_SVECTOR_VECTOR3D_HPP_
11 #define INCLUDE_SVECTOR_VECTOR3D_HPP_
12 
13 #include <cmath> // std::acos, std::cos, std::sin
14 
15 #include "simplevectors/core/units.hpp" // svector::AngleDir
16 #include "simplevectors/core/vector.hpp" // svector::Vector
17 
18 namespace svector {
19 // COMBINER_PY_START
20 
21 typedef Vector<3> Vec3_;
22 
26 class Vector3D : public Vec3_ {
27 public:
28  using Vec3_::Vector;
29 
37  Vector3D(const double x, const double y, const double z) {
38  this->m_components[0] = x;
39  this->m_components[1] = y;
40  this->m_components[2] = z;
41  }
42 
46  Vector3D(const Vec3_ &other) {
47  this->m_components[0] = other[0];
48  this->m_components[1] = other[1];
49  this->m_components[2] = other[2];
50  }
51 
59  double x() const { return this->m_components[0]; }
60 
68  void x(const double &newX) { this->m_components[0] = newX; }
69 
77  double y() const { return this->m_components[1]; }
78 
86  void y(const double &newY) { this->m_components[1] = newY; }
87 
95  double z() const { return this->m_components[2]; }
96 
104  void z(const double &newZ) { this->m_components[2] = newZ; }
105 
113  Vector3D cross(const Vector3D &other) const {
114  const double newx = this->y() * other.z() - this->z() * other.y();
115  const double newy = this->z() * other.x() - this->x() * other.z();
116  const double newz = this->x() * other.y() - this->y() * other.x();
117 
118  return Vector3D{newx, newy, newz};
119  }
120 
133  template <typename T> T componentsAs() const {
134  return T{this->x(), this->y(), this->z()};
135  }
136 
149  template <typename T> T anglesAs() const {
150  return T{this->getAlpha(), this->getBeta(), this->getGamma()};
151  }
152 
168  template <AngleDir D> double angle() const {
169  switch (D) {
170  case ALPHA:
171  return this->getAlpha();
172  case BETA:
173  return this->getBeta();
174  default:
175  return this->getGamma();
176  }
177  }
178 
196  template <AngleDir D> Vector3D rotate(const double &ang) const {
197  switch (D) {
198  case ALPHA:
199  return this->rotateAlpha(ang);
200  case BETA:
201  return this->rotateBeta(ang);
202  default:
203  return this->rotateGamma(ang);
204  }
205  }
206 
207 private:
215  double getAlpha() const { return std::acos(this->x() / this->magn()); }
216 
224  double getBeta() const { return std::acos(this->y() / this->magn()); }
225 
233  double getGamma() const { return std::acos(this->z() / this->magn()); }
234 
238  Vector3D rotateAlpha(const double &ang) const {
247  const double xPrime = this->x();
248  const double yPrime = this->y() * std::cos(ang) - this->z() * std::sin(ang);
249  const double zPrime = this->y() * std::sin(ang) + this->z() * std::cos(ang);
250 
251  return Vector3D{xPrime, yPrime, zPrime};
252  }
253 
257  Vector3D rotateBeta(const double &ang) const {
266  const double xPrime = this->x() * std::cos(ang) + this->z() * std::sin(ang);
267  const double yPrime = this->y();
268  const double zPrime =
269  -this->x() * std::sin(ang) + this->z() * std::cos(ang);
270 
271  return Vector3D{xPrime, yPrime, zPrime};
272  }
273 
277  Vector3D rotateGamma(const double &ang) const {
286  const double xPrime = this->x() * std::cos(ang) - this->y() * std::sin(ang);
287  const double yPrime = this->x() * std::sin(ang) + this->y() * std::cos(ang);
288  const double zPrime = this->z();
289 
290  return Vector3D{xPrime, yPrime, zPrime};
291  }
292 };
293 // COMBINER_PY_END
294 } // namespace svector
295 
296 #endif
Enums for core library.
@ ALPHA
Angle between positive x-axis and vector.
Definition: units.hpp:24
@ BETA
Angle between positive y-axis and vector.
Definition: units.hpp:25
Contains a base vector representation.
A base vector representation.
Definition: vector.hpp:34
Vector()
No-argument constructor.
Definition: vector.hpp:67
T magn() const
Magnitude.
Definition: vector.hpp:426
std::array< T, D > m_components
An array of components for the vector.
Definition: vector.hpp:630
A simple 3D vector representation.
Definition: vector3d.hpp:26
T anglesAs() const
Converts angles to another object.
Definition: vector3d.hpp:149
Vector3D(const double x, const double y, const double z)
Initializes a vector given xyz components.
Definition: vector3d.hpp:37
Vector3D rotate(const double &ang) const
Rotates vector around a certain axis by a certain angle.
Definition: vector3d.hpp:196
Vector3D(const Vec3_ &other)
Copy constructor for the base class.
Definition: vector3d.hpp:46
void z(const double &newZ)
Sets z-component.
Definition: vector3d.hpp:104
Vector3D cross(const Vector3D &other) const
Cross product of two vectors.
Definition: vector3d.hpp:113
T componentsAs() const
Converts vector to another object.
Definition: vector3d.hpp:133
double z() const
Gets z-component.
Definition: vector3d.hpp:95
void y(const double &newY)
Sets y-component.
Definition: vector3d.hpp:86
double angle() const
Gets a specific angle of the vector.
Definition: vector3d.hpp:168
void x(const double &newX)
Sets x-component.
Definition: vector3d.hpp:68
double x() const
Gets x-component.
Definition: vector3d.hpp:59
double y() const
Gets y-component.
Definition: vector3d.hpp:77