Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Vector.h
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_VECTOR_H
13 #define INCLUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_VECTOR_H
14 
16 
17 #include <array>
18 #include <algorithm>
19 
20 namespace Scine {
21 namespace Molassembler {
22 namespace Temple {
23 
25 struct Vector {
28  std::array<double, 3> data;
30 
33  constexpr Vector() : data({{0, 0, 0}}) {}
34  constexpr Vector(std::array<double, 3> positions) : data(positions) {}
35  constexpr Vector(double x, double y, double z) : data({{x, y, z}}) {}
37 
40 
44  PURITY_WEAK constexpr double dot(const Vector& other) const noexcept {
45  return (
46  this->data[0] * other.data[0]
47  + this->data[1] * other.data[1]
48  + this->data[2] * other.data[2]
49  );
50  }
51 
56  PURITY_WEAK constexpr Vector cross(const Vector& other) const noexcept {
57  return Vector {
58  {{
59  this -> data[1] * other.data[2] - this -> data[2] * other.data[1],
60  this -> data[2] * other.data[0] - this -> data[0] * other.data[2],
61  this -> data[0] * other.data[1] - this -> data[1] * other.data[0]
62  }}
63  };
64  }
65 
70  PURITY_WEAK constexpr double norm() const {
71  return Math::sqrt(
72  this -> data[0] * this -> data[0]
73  + this -> data[1] * this -> data[1]
74  + this -> data[2] * this -> data[2]
75  );
76  }
77 
78  PURITY_WEAK constexpr Vector operator + (const Vector& other) const noexcept {
79  return Vector {
80  {{
81  this->data[0] + other.data[0],
82  this->data[1] + other.data[1],
83  this->data[2] + other.data[2]
84  }}
85  };
86  }
87 
88  PURITY_WEAK constexpr Vector operator - (const Vector& other) const noexcept {
89  return Vector {
90  {{
91  this->data[0] - other.data[0],
92  this->data[1] - other.data[1],
93  this->data[2] - other.data[2]
94  }}
95  };
96  }
97 
98  PURITY_WEAK constexpr Vector operator * (const double constant) const noexcept {
99  return Vector {
100  {{
101  constant * this->data[0],
102  constant * this->data[1],
103  constant * this->data[2]
104  }}
105  };
106  }
107 
112  PURITY_WEAK constexpr Vector operator / (const double constant) const {
113  if(constant == 0) {
114  throw "Constexpr::Vector divided by zero!";
115  }
116 
117  return Vector {
118  {{
119  this->data[0] / constant,
120  this->data[1] / constant,
121  this->data[2] / constant
122  }}
123  };
124  }
125 
127  PURITY_WEAK constexpr Vector operator - () const noexcept {
128  return Vector {
129  {{
130  - this -> data[0],
131  - this -> data[1],
132  - this -> data[2]
133  }}
134  };
135  }
137 };
138 
143 PURITY_WEAK constexpr double angle(const Vector& a, const Vector& b) {
144  return Math::acos(
145  std::clamp(
146  a.dot(b) / (
147  a.norm() * b.norm()
148  ),
149  -1.0,
150  1.0
151  )
152  );
153 }
154 
155 } // namespace Temple
156 } // namespace Molassembler
157 } // namespace Scine
158 
159 #endif
constexpr math implementations
#define PURITY_WEAK
Definition: Preprocessor.h:36
Constexpr three-dimensional vector math class.
Definition: Vector.h:25
PURITY_WEAK constexpr double angle(const Vector &a, const Vector &b)
Constexpr binary angle in radians calculation.
Definition: Vector.h:143