ls1-MarDyn
ls1-MarDyn molecular dynamics code
Quaternion.h
1#ifndef QUATERNION_H_
2#define QUATERNION_H_
3
4#include <cmath>
5#include <array>
6#include "utils/mardyn_assert.h"
11public:
12 Quaternion(double qw = 1., double qx = 1., double qy = 0., double qz = 0.)
13 : m_qw(qw), m_qx(qx), m_qy(qy), m_qz(qz) {
14 }
15
20 Quaternion(const double& alpha_rad, const std::array<double,3>& n);
21
22 double qw() const { return m_qw; }
23 double qx() const { return m_qx; }
24 double qy() const { return m_qy; }
25 double qz() const { return m_qz; }
26 double magnitude2() const {
27 return m_qw * m_qw + m_qx * m_qx + m_qy * m_qy + m_qz * m_qz;
28 }
29 void scale(double s) {
30 m_qw *= s;
31 m_qx *= s;
32 m_qy *= s;
33 m_qz *= s;
34
35 }
36 void scaleinv(double s) {
37 mardyn_assert(s != 0.);
38 scale( 1./ s);
39 }
40 void normalize() {
41 scaleinv(sqrt(magnitude2()));
42 }
43 void conjugate() {
44 m_qx = -m_qx;
45 m_qy = -m_qy;
46 m_qz = -m_qz;
47 }
48 void inverse() {
49 conjugate();
50 scaleinv(magnitude2());
51 }
52 void add(const Quaternion& q) {
53 m_qw += q.m_qw;
54 m_qx += q.m_qx;
55 m_qy += q.m_qy;
56 m_qz += q.m_qz;
57 }
58
59
60 void multiply_left(const Quaternion& q);
61
62 void operator *=(const Quaternion& q);
68 std::array<double, 3> rotate(const std::array<double, 3>& d) const;
69
75 void rotateInPlace(std::array<double, 3>& d) const {
76 std::array<double, 3> dcopy = d;
77 d = rotate(dcopy);
78 }
79 std::array<double, 3> rotateinv(const std::array<double, 3>& d) const;
80 void rotateinvInPlace(std::array<double, 3>& d) const {
81 std::array<double, 3> dcopy = d;
82 d = rotateinv(dcopy);
83 }
84 //void differentiate_dbl(const double w[3], Quaternion& dqdt) const;
85 void differentiate(const std::array<double, 3>& w, Quaternion& dqdt) const;
86 // { differentiate_dbl(w,dqdt); dqdt.scale(.5); }
87 void getRotMatrix(double R[3][3]) const;
88 void getRotinvMatrix(double R[3][3]) const;
89
90 bool isNormalized() const {
91 return fabs(magnitude2() - 1.0) <= 1e-15;
92 }
93 void check() const{
94 using std::isfinite;
95 mardyn_assert(std::isfinite(m_qw));
96 mardyn_assert(std::isfinite(m_qx));
97 mardyn_assert(std::isfinite(m_qy));
98 mardyn_assert(std::isfinite(m_qz));
99 }
100
101private:
102 double m_qw, m_qx, m_qy, m_qz; // components
103
104};
105
106#endif /*QUATERNION_H_*/
Definition: Quaternion.h:10
std::array< double, 3 > rotate(const std::array< double, 3 > &d) const
Definition: Quaternion.cpp:43
void rotateInPlace(std::array< double, 3 > &d) const
Definition: Quaternion.h:75