Control Libraries 7.4.0
Loading...
Searching...
No Matches
Distance.hpp
1#pragma once
2
3#include <cmath>
4
5namespace state_representation::units {
6class Distance;
7
8inline namespace literals {
14constexpr Distance operator ""_m(long double n);
15
21constexpr Distance operator ""_km(long double n);
22
28constexpr Distance operator ""_dm(long double n);
29
35constexpr Distance operator ""_cm(long double n);
36
42constexpr Distance operator ""_mm(long double n);
43}
44
45class Distance {
46private:
47 long double value;
48
49public:
54 constexpr Distance(long double n = 0.0);
55
60 constexpr Distance(const Distance& dist);
61
66 constexpr long double get_value() const;
67
72 constexpr Distance& operator-();
73
79 constexpr Distance& operator+=(const Distance& rhs);
80
86 constexpr Distance operator+(const Distance& rhs) const;
87
93 constexpr Distance& operator-=(const Distance& rhs);
94
100 constexpr Distance operator-(const Distance& rhs) const;
101
107 constexpr Distance& operator*=(double lambda);
108
114 constexpr Distance operator*(double lambda) const;
115
121 constexpr Distance& operator/=(double lambda);
122
128 constexpr Distance operator/(double lambda) const;
129
135 constexpr bool operator==(const Distance& rhs) const;
136
142 constexpr bool operator!=(const Distance& rhs) const;
143
149 constexpr bool operator>(const Distance& rhs) const;
150
156 constexpr bool operator>=(const Distance& rhs) const;
157
163 constexpr bool operator<(const Distance& rhs) const;
164
170 constexpr bool operator<=(const Distance& rhs) const;
171
178 friend constexpr double operator/(const Distance& lhs, const Distance& rhs);
179
185 friend constexpr Distance operator*(double lambda, const Distance& rhs);
186
192 friend constexpr Distance literals::operator ""_m(long double n);
193
199 friend constexpr Distance literals::operator ""_km(long double n);
200
206 friend constexpr Distance literals::operator ""_dm(long double n);
207
213 friend constexpr Distance literals::operator ""_cm(long double n);
214
220 friend constexpr Distance literals::operator ""_mm(long double n);
221};
222
223constexpr Distance::Distance(long double n) :
224 value(n) {}
225
227 value(dist.value) {}
228
229constexpr long double Distance::get_value() const {
230 return this->value;
231}
232
234 this->value = -this->value;
235 return (*this);
236}
237
238constexpr Distance& Distance::operator+=(const Distance& rhs) {
239 this->value = this->value + rhs.value;
240 return (*this);
241}
242
243constexpr Distance Distance::operator+(const Distance& rhs) const {
244 Distance result(*this);
245 result += rhs;
246 return result;
247}
248
249constexpr Distance& Distance::operator-=(const Distance& rhs) {
250 this->value = this->value - rhs.value;
251 return (*this);
252}
253
254constexpr Distance Distance::operator-(const Distance& rhs) const {
255 Distance result(*this);
256 result -= rhs;
257 return result;
258}
259
260constexpr Distance& Distance::operator*=(double lambda) {
261 this->value = this->value * lambda;
262 return (*this);
263}
264
265constexpr Distance Distance::operator*(double lambda) const {
266 Distance result(*this);
267 result *= lambda;
268 return result;
269}
270
271constexpr Distance& Distance::operator/=(double lambda) {
272 this->value = this->value / lambda;
273 return (*this);
274}
275
276constexpr Distance Distance::operator/(double lambda) const {
277 Distance result(*this);
278 result /= lambda;
279 return result;
280}
281
282constexpr bool Distance::operator==(const Distance& rhs) const {
283 return (abs(this->value - rhs.value) < 1e-4);
284}
285
286constexpr bool Distance::operator!=(const Distance& rhs) const {
287 return !((*this) == rhs);
288}
289
290constexpr bool Distance::operator>(const Distance& rhs) const {
291 return ((this->value - rhs.value) > 1e-4);
292}
293
294constexpr bool Distance::operator>=(const Distance& rhs) const {
295 return (((*this) > rhs) or ((*this) == rhs));
296}
297
298constexpr bool Distance::operator<(const Distance& rhs) const {
299 return ((rhs.value - this->value) > 1e-4);
300}
301
302constexpr bool Distance::operator<=(const Distance& rhs) const {
303 return (((*this) < rhs) or ((*this) == rhs));
304}
305
306constexpr double operator/(const Distance& lhs, const Distance& rhs) {
307 return lhs.get_value() / rhs.get_value();
308}
309
310constexpr Distance operator*(double lambda, const Distance& rhs) {
311 return Distance(lambda * rhs.value);
312}
313
314inline namespace literals {
315constexpr Distance operator ""_m(long double n) {
316 return Distance(n);
317}
318
319constexpr Distance operator ""_km(long double n) {
320 return Distance(1e3 * n);
321}
322
323constexpr Distance operator ""_dm(long double n) {
324 return Distance(1e-1 * n);
325}
326
327constexpr Distance operator ""_cm(long double n) {
328 return Distance(1e-2 * n);
329}
330
331constexpr Distance operator ""_mm(long double n) {
332 return Distance(1e-3 * n);
333}
334}
335}
constexpr Distance & operator*=(double lambda)
Overload the *= operator with a scalar.
Definition Distance.hpp:260
constexpr Distance & operator+=(const Distance &rhs)
Overload the += operator.
Definition Distance.hpp:238
constexpr Distance & operator/=(double lambda)
Overload the /= operator with a scalar.
Definition Distance.hpp:271
constexpr Distance operator+(const Distance &rhs) const
Overload the + operator.
Definition Distance.hpp:243
constexpr bool operator>(const Distance &rhs) const
Overload the > operator.
Definition Distance.hpp:290
constexpr Distance & operator-()
Overload the - operator.
Definition Distance.hpp:233
constexpr bool operator<=(const Distance &rhs) const
Overload the < operator.
Definition Distance.hpp:302
constexpr bool operator!=(const Distance &rhs) const
Overload the != operator.
Definition Distance.hpp:286
constexpr Distance(long double n=0.0)
Constructor with a value in meter.
Definition Distance.hpp:223
constexpr Distance & operator-=(const Distance &rhs)
Overload the -= operator.
Definition Distance.hpp:249
constexpr long double get_value() const
Getter of the value attribute.
Definition Distance.hpp:229
constexpr bool operator<(const Distance &rhs) const
Overload the < operator.
Definition Distance.hpp:298
constexpr bool operator>=(const Distance &rhs) const
Overload the > operator.
Definition Distance.hpp:294
friend constexpr Distance operator*(double lambda, const Distance &rhs)
Overload the / operator with a scalar on the left side.
Definition Distance.hpp:310
friend constexpr double operator/(const Distance &lhs, const Distance &rhs)
Overload the / operator between two distances.
Definition Distance.hpp:306
constexpr bool operator==(const Distance &rhs) const
Overload the == operator.
Definition Distance.hpp:282
double dist(const CartesianState &s1, const CartesianState &s2, const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL)
Compute the distance between two Cartesian states.