Control Libraries 7.4.0
Loading...
Searching...
No Matches
Velocity.hpp
1#pragma once
2
3#include <chrono>
4#include "state_representation/units/Distance.hpp"
5#include "state_representation/units/Angle.hpp"
6
7using namespace std::chrono_literals;
8
9namespace state_representation::units {
10template<class T>
11class Velocity;
12
13using LinearVelocity = Velocity<Distance>;
14using AngularVelocity = Velocity<Angle>;
15
16inline namespace literals {
22constexpr LinearVelocity operator ""_m_s(long double n);
23
29constexpr LinearVelocity operator ""_m_h(long double n);
30
36constexpr LinearVelocity operator ""_m_ms(long double n);
37
43constexpr LinearVelocity operator ""_km_h(long double n);
44
50constexpr LinearVelocity operator ""_km_s(long double n);
51
57constexpr LinearVelocity operator ""_km_ms(long double n);
58
64constexpr LinearVelocity operator ""_mm_h(long double n);
65
71constexpr AngularVelocity operator ""_rad_s(long double n);
72
78constexpr AngularVelocity operator ""_deg_s(long double n);
79}
80
81template<class T>
82class Velocity {
83private:
84 long double value;
85
86public:
91 constexpr Velocity(long double n = 0.0);
92
97 constexpr Velocity(const Velocity<T>& vel);
98
103 constexpr long double get_value() const;
104
109 constexpr Velocity<T>& operator-();
110
116 constexpr Velocity<T>& operator+=(const Velocity<T>& rhs);
117
123 constexpr Velocity<T> operator+(const Velocity<T>& rhs) const;
124
130 constexpr Velocity<T>& operator-=(const Velocity<T>& rhs);
131
137 constexpr Velocity<T> operator-(const Velocity<T>& rhs) const;
138
144 constexpr Velocity<T>& operator*=(double lambda);
145
151 constexpr Velocity<T> operator*(double lambda) const;
152
158 constexpr Velocity<T>& operator/=(double lambda);
159
165 constexpr Velocity<T> operator/(double lambda) const;
166
172 constexpr bool operator==(const Velocity<T>& rhs) const;
173
179 constexpr bool operator!=(const Velocity<T>& rhs) const;
180
186 constexpr bool operator>(const Velocity<T>& rhs) const;
187
193 constexpr bool operator>=(const Velocity<T>& rhs) const;
194
200 constexpr bool operator<(const Velocity<T>& rhs) const;
201
207 constexpr bool operator<=(const Velocity<T>& rhs) const;
208
215 friend constexpr double operator/(const Velocity<T>& lhs, const Velocity<T>& rhs) {
216 return lhs.value / rhs.value;
217 }
218
224 friend constexpr Velocity<T> operator*(double lambda, const Velocity<T>& rhs) {
225 return Velocity<T>(lambda * rhs.value);
226 }
227
234 template<class Rep, class DurationRatio>
235 friend constexpr Velocity<T> operator/(const T& lhs, const std::chrono::duration<Rep, DurationRatio>& rhs);
236};
237
238template<class T>
239constexpr Velocity<T>::Velocity(long double n):
240 value(n) {}
241
242template<class T>
243constexpr Velocity<T>::Velocity(const Velocity<T>& vel):
244 value(vel.value) {}
245
246template<class T>
247constexpr long double Velocity<T>::get_value() const {
248 return this->value;
249}
250
251template<class T>
253 this->value = -this->value;
254 return (*this);
255}
256
257template<class T>
259 this->value = this->value + rhs.value;
260 return (*this);
261}
262
263template<class T>
264constexpr Velocity<T> Velocity<T>::operator+(const Velocity<T>& rhs) const {
265 Velocity<T> result(*this);
266 result += rhs;
267 return result;
268}
269
270template<class T>
272 this->value = this->value - rhs.value;
273 return (*this);
274}
275
276template<class T>
277constexpr Velocity<T> Velocity<T>::operator-(const Velocity<T>& rhs) const {
278 Velocity<T> result(*this);
279 result -= rhs;
280 return result;
281}
282
283template<class T>
284constexpr Velocity<T>& Velocity<T>::operator*=(double lambda) {
285 this->value = this->value * lambda;
286 return (*this);
287}
288
289template<class T>
290constexpr Velocity<T> Velocity<T>::operator*(double lambda) const {
291 Velocity<T> result(*this);
292 result *= lambda;
293 return result;
294}
295
296template<class T>
297constexpr Velocity<T>& Velocity<T>::operator/=(double lambda) {
298 this->value = this->value / lambda;
299 return (*this);
300}
301
302template<class T>
303constexpr Velocity<T> Velocity<T>::operator/(double lambda) const {
304 Velocity<T> result(*this);
305 result /= lambda;
306 return result;
307}
308
309template<class T>
310constexpr bool Velocity<T>::operator==(const Velocity<T>& rhs) const {
311 return (abs(this->value - rhs.value) < 1e-4);
312}
313
314template<class T>
315constexpr bool Velocity<T>::operator!=(const Velocity<T>& rhs) const {
316 return !((*this) == rhs);
317}
318
319template<class T>
320constexpr bool Velocity<T>::operator>(const Velocity<T>& rhs) const {
321 return ((this->value - rhs.value) > 1e-4);
322}
323
324template<class T>
325constexpr bool Velocity<T>::operator>=(const Velocity<T>& rhs) const {
326 return (((*this) > rhs) or ((*this) == rhs));
327}
328
329template<class T>
330constexpr bool Velocity<T>::operator<(const Velocity<T>& rhs) const {
331 return ((rhs.value - this->value) > 1e-4);
332}
333
334template<class T>
335constexpr bool Velocity<T>::operator<=(const Velocity<T>& rhs) const {
336 return (((*this) < rhs) or ((*this) == rhs));
337}
338
339template<class T, class Rep, class DurationRatio>
340constexpr Velocity<T> operator/(const T& dist, const std::chrono::duration<Rep, DurationRatio>& rhs) {
341 const auto rhsInSeconds = std::chrono::duration_cast<std::chrono::seconds>(rhs);
342 return Velocity<T>(dist.get_value() / rhsInSeconds.count());
343}
344
345inline namespace literals {
346constexpr LinearVelocity operator ""_m_s(long double n) {
347 Distance d = 1.0_m;
348 auto t = 1.0s;
349 return n * (d / t);
350}
351
352constexpr LinearVelocity operator ""_m_h(long double n) {
353 Distance d = 1.0_m;
354 auto t = 1.0h;
355 return n * (d / t);
356}
357
358constexpr LinearVelocity operator ""_m_ms(long double n) {
359 Distance d = 1.0_m;
360 auto t = 1.0ms;
361 return n * (d / t);
362}
363
364constexpr LinearVelocity operator ""_km_h(long double n) {
365 Distance d = 1.0_km;
366 auto t = 1.0h;
367 return n * (d / t);
368}
369
370constexpr LinearVelocity operator ""_km_s(long double n) {
371 Distance d = 1.0_km;
372 auto t = 1.0s;
373 return n * (d / t);
374}
375
376constexpr LinearVelocity operator ""_km_ms(long double n) {
377 Distance d = 1.0_km;
378 auto t = 1.0ms;
379 return n * (d / t);
380}
381
382constexpr LinearVelocity operator ""_mm_h(long double n) {
383 Distance d = 1.0_mm;
384 auto t = 1.0h;
385 return n * (d / t);
386}
387
388constexpr AngularVelocity operator ""_rad_s(long double n) {
389 Angle a = 1.0_rad;
390 auto t = 1.0s;
391 return n * (a / t);
392}
393
394constexpr AngularVelocity operator ""_deg_s(long double n) {
395 Angle a = 1.0_deg;
396 auto t = 1.0s;
397 return n * (a / t);
398}
399}
400}
constexpr bool operator==(const Velocity< T > &rhs) const
Overload the == operator.
Definition Velocity.hpp:310
constexpr Velocity< T > & operator/=(double lambda)
Overload the /= operator with a scalar.
Definition Velocity.hpp:297
constexpr Velocity(long double n=0.0)
Constructor with a value in the base unit of T.
Definition Velocity.hpp:239
constexpr bool operator!=(const Velocity< T > &rhs) const
Overload the != operator.
Definition Velocity.hpp:315
constexpr bool operator<(const Velocity< T > &rhs) const
Overload the < operator.
Definition Velocity.hpp:330
constexpr Velocity< T > operator+(const Velocity< T > &rhs) const
Overload the + operator.
Definition Velocity.hpp:264
friend constexpr Velocity< T > operator*(double lambda, const Velocity< T > &rhs)
Overload the * operator with a scalar on the left side.
Definition Velocity.hpp:224
constexpr Velocity< T > & operator+=(const Velocity< T > &rhs)
Overload the += operator.
Definition Velocity.hpp:258
constexpr Velocity< T > & operator*=(double lambda)
Overload the *= operator with a scalar.
Definition Velocity.hpp:284
constexpr long double get_value() const
Getter of the value attribute.
Definition Velocity.hpp:247
constexpr Velocity< T > & operator-()
Overload the - operator.
Definition Velocity.hpp:252
constexpr bool operator>=(const Velocity< T > &rhs) const
Overload the > operator.
Definition Velocity.hpp:325
constexpr bool operator>(const Velocity< T > &rhs) const
Overload the > operator.
Definition Velocity.hpp:320
friend constexpr double operator/(const Velocity< T > &lhs, const Velocity< T > &rhs)
Overload the / operator between two Velocitys.
Definition Velocity.hpp:215
constexpr Velocity< T > & operator-=(const Velocity< T > &rhs)
Overload the -= operator.
Definition Velocity.hpp:271
constexpr bool operator<=(const Velocity< T > &rhs) const
Overload the < operator.
Definition Velocity.hpp:335
double dist(const CartesianState &s1, const CartesianState &s2, const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL)
Compute the distance between two Cartesian states.