Version: SMASH-2.0
threevector.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2020
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_THREEVECTOR_H_
11 #define SRC_INCLUDE_SMASH_THREEVECTOR_H_
12 
13 #include <array>
14 #include <cmath>
15 #include <ostream>
16 
17 #include "constants.h"
18 
19 namespace smash {
20 
31 class ThreeVector {
32  public:
34  ThreeVector() : x_({0., 0., 0.}) {}
35 
44  ThreeVector(double y1, double y2, double y3) : x_({y1, y2, y3}) {}
45 
47  double &operator[](std::size_t i) { return x_[i]; }
49  double operator[](std::size_t i) const { return x_[i]; }
50 
52  double inline x1() const;
54  void inline set_x1(double x);
56  double inline x2() const;
58  void inline set_x2(double y);
60  double inline x3() const;
62  void inline set_x3(double z);
64  double inline sqr() const;
66  double inline abs() const;
68  double inline get_phi() const;
70  double inline get_theta() const;
91  void inline rotate(double phi, double theta, double psi);
96  void inline rotate_around_y(double theta);
101  void inline rotate_around_z(double theta);
106  void inline rotate_z_axis_to(ThreeVector &r);
108  ThreeVector inline operator-() const;
110  ThreeVector inline operator+=(const ThreeVector &v);
112  ThreeVector inline operator-=(const ThreeVector &v);
114  ThreeVector inline operator*=(const double &a);
116  ThreeVector inline operator/=(const double &a);
117 
119  bool operator==(const ThreeVector &rhs) const { return x_ == rhs.x_; }
121  bool operator!=(const ThreeVector &rhs) const { return x_ != rhs.x_; }
122 
127  ThreeVector inline cross_product(const ThreeVector &b) const;
129  using iterator = std::array<double, 3>::iterator;
131  using const_iterator = std::array<double, 3>::const_iterator;
132 
139  iterator begin() { return x_.begin(); }
140 
142  iterator end() { return x_.end(); }
143 
145  const_iterator begin() const { return x_.begin(); }
147  const_iterator end() const { return x_.end(); }
148 
150  const_iterator cbegin() const { return x_.cbegin(); }
152  const_iterator cend() const { return x_.cend(); }
153 
154  private:
156  std::array<double, 3> x_;
157 };
158 
163 std::ostream &operator<<(std::ostream &, const ThreeVector &);
164 
165 double inline ThreeVector::x1() const { return x_[0]; }
166 
167 void inline ThreeVector::set_x1(const double x) { x_[0] = x; }
168 
169 double inline ThreeVector::x2() const { return x_[1]; }
170 
171 void inline ThreeVector::set_x2(const double y) { x_[1] = y; }
172 
173 double inline ThreeVector::x3() const { return x_[2]; }
174 
175 void inline ThreeVector::set_x3(const double z) { x_[2] = z; }
176 
178  ThreeVector neg(-x_[0], -x_[1], -x_[2]);
179  return neg;
180 }
181 
183  x_[0] += v.x_[0];
184  x_[1] += v.x_[1];
185  x_[2] += v.x_[2];
186  return *this;
187 }
188 
191  a += b;
192  return a;
193 }
194 
196  x_[0] -= v.x_[0];
197  x_[1] -= v.x_[1];
198  x_[2] -= v.x_[2];
199  return *this;
200 }
201 
204  a -= b;
205  return a;
206 }
207 
208 ThreeVector inline ThreeVector::operator*=(const double &a) {
209  x_[0] *= a;
210  x_[1] *= a;
211  x_[2] *= a;
212  return *this;
213 }
214 
216 inline ThreeVector operator*(ThreeVector a, const double &b) {
217  a *= b;
218  return a;
219 }
220 
222 inline ThreeVector operator*(const double &a, ThreeVector b) {
223  b *= a;
224  return b;
225 }
226 
231 inline double operator*(ThreeVector a, const ThreeVector &b) {
232  return a.x1() * b.x1() + a.x2() * b.x2() + a.x3() * b.x3();
233 }
234 
240  return ThreeVector(x_[1] * b.x3() - x_[2] * b.x2(),
241  x_[2] * b.x1() - x_[0] * b.x3(),
242  x_[0] * b.x2() - x_[1] * b.x1());
243 }
244 
245 ThreeVector inline ThreeVector::operator/=(const double &a) {
246  const double a_inv = 1.0 / a;
247  x_[0] *= a_inv;
248  x_[1] *= a_inv;
249  x_[2] *= a_inv;
250  return *this;
251 }
252 
254 ThreeVector inline operator/(ThreeVector a, const double &b) {
255  a /= b;
256  return a;
257 }
258 
259 double inline ThreeVector::sqr() const { return (*this) * (*this); }
260 
261 double inline ThreeVector::abs() const { return std::sqrt((*this) * (*this)); }
262 
263 double inline ThreeVector::get_phi() const {
264  if (std::abs(x1()) < really_small && std::abs(x2()) < really_small) {
265  return 0.;
266  } else {
267  return std::atan2(x2(), x1());
268  }
269 }
270 
271 double inline ThreeVector::get_theta() const {
272  double r = abs();
273  return (r > 0.) ? std::acos(x3() / r) : 0.;
274 }
275 
276 void inline ThreeVector::rotate(double phi, double theta, double psi) {
277  // Compute the cosine and sine for each angle.
278  const double cos_phi = std::cos(phi);
279  const double sin_phi = std::sin(phi);
280  const double cos_theta = std::cos(theta);
281  const double sin_theta = std::sin(theta);
282  const double cos_psi = std::cos(psi);
283  const double sin_psi = std::sin(psi);
284  // Get original coordinates.
285  std::array<double, 3> x_old = x_;
286  // Compute new coordinates.
287  x_[0] = (cos_phi * cos_psi - sin_phi * cos_theta * sin_psi) * x_old[0] +
288  (-cos_phi * sin_psi - sin_phi * cos_theta * cos_psi) * x_old[1] +
289  (sin_phi * sin_theta) * x_old[2];
290  x_[1] = (sin_phi * cos_psi + cos_phi * cos_theta * sin_psi) * x_old[0] +
291  (-sin_phi * sin_psi + cos_phi * cos_theta * cos_psi) * x_old[1] +
292  (-cos_phi * sin_theta) * x_old[2];
293  x_[2] = (sin_theta * sin_psi) * x_old[0] + (sin_theta * cos_psi) * x_old[1] +
294  (cos_theta)*x_old[2];
295 }
296 
297 void inline ThreeVector::rotate_around_y(double theta) {
298  const double cost = std::cos(theta);
299  const double sint = std::sin(theta);
300  // Get original coordinates.
301  std::array<double, 3> x_old = x_;
302  // Compute new coordinates.
303  x_[0] = cost * x_old[0] + sint * x_old[2];
304  // x_[1] is unchanged
305  x_[2] = -sint * x_old[0] + cost * x_old[2];
306 }
307 
308 void inline ThreeVector::rotate_around_z(double theta) {
309  const double cost = std::cos(theta);
310  const double sint = std::sin(theta);
311  // Get original coordinates.
312  std::array<double, 3> x_old = x_;
313  // Compute new coordinates.
314  x_[0] = cost * x_old[0] - sint * x_old[1];
315  x_[1] = sint * x_old[0] + cost * x_old[1];
316  // x_[2] is unchanged
317 }
318 
322 }
323 
324 } // namespace smash
325 
326 #endif // SRC_INCLUDE_SMASH_THREEVECTOR_H_
smash
Definition: action.h:24
smash::operator+
EnergyMomentumTensor operator+(EnergyMomentumTensor a, const EnergyMomentumTensor &b)
Direct addition operator.
Definition: energymomentumtensor.h:162
smash::ThreeVector::end
const_iterator end() const
const overload of the above
Definition: threevector.h:147
smash::ThreeVector::set_x2
void set_x2(double y)
set second component
Definition: threevector.h:171
smash::ThreeVector::operator==
bool operator==(const ThreeVector &rhs) const
Definition: threevector.h:119
smash::ThreeVector::const_iterator
std::array< double, 3 >::const_iterator const_iterator
iterates over the components
Definition: threevector.h:131
smash::operator/
EnergyMomentumTensor operator/(EnergyMomentumTensor a, const double b)
Direct division operator.
Definition: energymomentumtensor.h:206
smash::ThreeVector::operator!=
bool operator!=(const ThreeVector &rhs) const
Definition: threevector.h:121
smash::ThreeVector::operator+=
ThreeVector operator+=(const ThreeVector &v)
increase this vector by
Definition: threevector.h:182
smash::ThreeVector::rotate
void rotate(double phi, double theta, double psi)
Rotate vector by the given Euler angles phi, theta, psi.
Definition: threevector.h:276
smash::ThreeVector::x3
double x3() const
Definition: threevector.h:173
smash::ThreeVector::set_x3
void set_x3(double z)
set third component
Definition: threevector.h:175
smash::ThreeVector::x_
std::array< double, 3 > x_
the internal storage of the components.
Definition: threevector.h:156
smash::ThreeVector::rotate_around_y
void rotate_around_y(double theta)
Rotate the vector around the y axis by the given angle theta.
Definition: threevector.h:297
smash::ThreeVector::cbegin
const_iterator cbegin() const
Definition: threevector.h:150
smash::operator-
EnergyMomentumTensor operator-(EnergyMomentumTensor a, const EnergyMomentumTensor &b)
Direct subtraction operator.
Definition: energymomentumtensor.h:176
smash::operator<<
std::ostream & operator<<(std::ostream &out, const ActionPtr &action)
Definition: action.h:518
smash::ThreeVector::iterator
std::array< double, 3 >::iterator iterator
iterates over the components
Definition: threevector.h:129
smash::ThreeVector::sqr
double sqr() const
Definition: threevector.h:259
smash::ThreeVector::rotate_around_z
void rotate_around_z(double theta)
Rotate the vector around the z axis by the given angle theta.
Definition: threevector.h:308
smash::ThreeVector::cross_product
ThreeVector cross_product(const ThreeVector &b) const
Definition: threevector.h:239
smash::really_small
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:37
smash::ThreeVector
Definition: threevector.h:31
smash::ThreeVector::cend
const_iterator cend() const
Definition: threevector.h:152
smash::ThreeVector::operator/=
ThreeVector operator/=(const double &a)
divide this vector by
Definition: threevector.h:245
smash::ThreeVector::operator[]
double operator[](std::size_t i) const
const overload of the above.
Definition: threevector.h:49
smash::ThreeVector::x1
double x1() const
Definition: threevector.h:165
smash::ThreeVector::operator-
ThreeVector operator-() const
negation: Returns
Definition: threevector.h:177
smash::ThreeVector::set_x1
void set_x1(double x)
set first component
Definition: threevector.h:167
smash::ThreeVector::rotate_z_axis_to
void rotate_z_axis_to(ThreeVector &r)
Rotate the z-axis onto the vector r.
Definition: threevector.h:319
smash::ThreeVector::operator[]
double & operator[](std::size_t i)
access the component at offset i.
Definition: threevector.h:47
smash::ThreeVector::abs
double abs() const
Definition: threevector.h:261
smash::ThreeVector::get_phi
double get_phi() const
Definition: threevector.h:263
smash::ThreeVector::end
iterator end()
Definition: threevector.h:142
smash::ThreeVector::get_theta
double get_theta() const
Definition: threevector.h:271
smash::ThreeVector::ThreeVector
ThreeVector(double y1, double y2, double y3)
Constructor for ThreeVector that takes 3 doubles to set up a ThreeVector with desired values for the ...
Definition: threevector.h:44
constants.h
smash::ThreeVector::begin
const_iterator begin() const
const overload of the above
Definition: threevector.h:145
smash::ThreeVector::ThreeVector
ThreeVector()
default constructor (nulls all components)
Definition: threevector.h:34
smash::operator*
EnergyMomentumTensor operator*(EnergyMomentumTensor a, const double b)
Direct multiplication operator.
Definition: energymomentumtensor.h:189
smash::ThreeVector::operator*=
ThreeVector operator*=(const double &a)
scale this vector by
Definition: threevector.h:208
smash::ThreeVector::operator-=
ThreeVector operator-=(const ThreeVector &v)
decrease this vector by
Definition: threevector.h:195
smash::ThreeVector::begin
iterator begin()
Definition: threevector.h:139
smash::ThreeVector::x2
double x2() const
Definition: threevector.h:169