ad_physics
AngularVelocity.hpp
Go to the documentation of this file.
1 /*
2  * ----------------- BEGIN LICENSE BLOCK ---------------------------------
3  *
4  * Copyright (C) 2018-2020 Intel Corporation
5  *
6  * SPDX-License-Identifier: MIT
7  *
8  * ----------------- END LICENSE BLOCK -----------------------------------
9  */
10 
18 #pragma once
19 
20 #include <cmath>
21 #include <iostream>
22 #include <limits>
23 #include <sstream>
24 #include <stdexcept>
25 #include "spdlog/fmt/ostr.h"
26 #include "spdlog/spdlog.h"
30 namespace ad {
34 namespace physics {
35 
39 #define AD_PHYSICS_ANGULARVELOCITY_THROWS_EXCEPTION 1
40 
41 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
42 
45 #define _AD_PHYSICS_ANGULARVELOCITY_EXPLICIT_CONVERSION_ explicit
46 #else
47 
50 #define _AD_PHYSICS_ANGULARVELOCITY_EXPLICIT_CONVERSION_
51 #endif
52 
60 {
61 public:
65  static const double cMinValue;
66 
70  static const double cMaxValue;
71 
76  static const double cPrecisionValue;
77 
85  : mAngularVelocity(std::numeric_limits<double>::quiet_NaN())
86  {
87  }
88 
95  : mAngularVelocity(iAngularVelocity)
96  {
97  }
98 
102  AngularVelocity(const AngularVelocity &other) = default;
103 
107  AngularVelocity(AngularVelocity &&other) = default;
108 
116  AngularVelocity &operator=(const AngularVelocity &other) = default;
117 
125  AngularVelocity &operator=(AngularVelocity &&other) = default;
126 
134  bool operator==(const AngularVelocity &other) const
135  {
136  ensureValid();
137  other.ensureValid();
138  return std::fabs(mAngularVelocity - other.mAngularVelocity) < cPrecisionValue;
139  }
140 
148  bool operator!=(const AngularVelocity &other) const
149  {
150  return !operator==(other);
151  }
152 
162  bool operator>(const AngularVelocity &other) const
163  {
164  ensureValid();
165  other.ensureValid();
166  return (mAngularVelocity > other.mAngularVelocity) && operator!=(other);
167  }
168 
178  bool operator<(const AngularVelocity &other) const
179  {
180  ensureValid();
181  other.ensureValid();
182  return (mAngularVelocity < other.mAngularVelocity) && operator!=(other);
183  }
184 
194  bool operator>=(const AngularVelocity &other) const
195  {
196  ensureValid();
197  other.ensureValid();
198  return ((mAngularVelocity > other.mAngularVelocity) || operator==(other));
199  }
200 
210  bool operator<=(const AngularVelocity &other) const
211  {
212  ensureValid();
213  other.ensureValid();
214  return ((mAngularVelocity < other.mAngularVelocity) || operator==(other));
215  }
216 
228  {
229  ensureValid();
230  other.ensureValid();
231  AngularVelocity const result(mAngularVelocity + other.mAngularVelocity);
232  result.ensureValid();
233  return result;
234  }
235 
247  {
248  ensureValid();
249  other.ensureValid();
250  mAngularVelocity += other.mAngularVelocity;
251  ensureValid();
252  return *this;
253  }
254 
266  {
267  ensureValid();
268  other.ensureValid();
269  AngularVelocity const result(mAngularVelocity - other.mAngularVelocity);
270  result.ensureValid();
271  return result;
272  }
273 
285  {
286  ensureValid();
287  other.ensureValid();
288  mAngularVelocity -= other.mAngularVelocity;
289  ensureValid();
290  return *this;
291  }
292 
303  AngularVelocity operator*(const double &scalar) const
304  {
305  ensureValid();
306  AngularVelocity const result(mAngularVelocity * scalar);
307  result.ensureValid();
308  return result;
309  }
310 
321  AngularVelocity operator/(const double &scalar) const
322  {
323  AngularVelocity const scalarAngularVelocity(scalar);
324  AngularVelocity const result(operator/(scalarAngularVelocity));
325  result.ensureValid();
326  return result;
327  }
328 
340  double operator/(const AngularVelocity &other) const
341  {
342  ensureValid();
343  other.ensureValidNonZero();
344  double const result = mAngularVelocity / other.mAngularVelocity;
345  return result;
346  }
347 
357  {
358  ensureValid();
359  AngularVelocity const result(-mAngularVelocity);
360  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
361  return result;
362  }
363 
371  {
372  return mAngularVelocity;
373  }
374 
382  bool isValid() const
383  {
384  auto const valueClass = std::fpclassify(mAngularVelocity);
385  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mAngularVelocity)
386  && (mAngularVelocity <= cMaxValue);
387  }
388 
395  void ensureValid() const
396  {
397  if (!isValid())
398  {
399  spdlog::info("ensureValid(::ad::physics::AngularVelocity)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
400 #if (AD_PHYSICS_ANGULARVELOCITY_THROWS_EXCEPTION == 1)
401  throw std::out_of_range("AngularVelocity value out of range"); // LCOV_EXCL_BR_LINE
402 #endif
403  }
404  }
405 
412  void ensureValidNonZero() const
413  {
414  ensureValid();
415  if (operator==(AngularVelocity(0.))) // LCOV_EXCL_BR_LINE
416  {
417  spdlog::info("ensureValid(::ad::physics::AngularVelocity)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
418 #if (AD_PHYSICS_ANGULARVELOCITY_THROWS_EXCEPTION == 1)
419  throw std::out_of_range("AngularVelocity value is zero"); // LCOV_EXCL_BR_LINE
420 #endif
421  }
422  }
423 
428  {
429  return AngularVelocity(cMinValue);
430  }
431 
436  {
437  return AngularVelocity(cMaxValue);
438  }
439 
444  {
446  }
447 
448 private:
452  double mAngularVelocity;
453 };
454 
455 } // namespace physics
456 } // namespace ad
468 inline ::ad::physics::AngularVelocity operator*(const double &other, ::ad::physics::AngularVelocity const &value)
469 {
470  return value.operator*(other);
471 }
472 
476 namespace std {
477 
481 inline ::ad::physics::AngularVelocity fabs(const ::ad::physics::AngularVelocity other)
482 {
483  ::ad::physics::AngularVelocity const result(std::fabs(static_cast<double>(other)));
484  return result;
485 }
486 
495 template <> class numeric_limits<::ad::physics::AngularVelocity> : public numeric_limits<double>
496 {
497 public:
501  static inline ::ad::physics::AngularVelocity lowest()
502  {
503  return ::ad::physics::AngularVelocity::getMin();
504  }
508  static inline ::ad::physics::AngularVelocity max()
509  {
510  return ::ad::physics::AngularVelocity::getMax();
511  }
512 
516  static inline ::ad::physics::AngularVelocity epsilon()
517  {
518  return ::ad::physics::AngularVelocity::getPrecision();
519  }
520 };
521 
522 } // namespace std
523 
527 #ifndef GEN_GUARD_AD_PHYSICS_ANGULARVELOCITY
528 #define GEN_GUARD_AD_PHYSICS_ANGULARVELOCITY
529 
532 namespace ad {
536 namespace physics {
537 
547 inline std::ostream &operator<<(std::ostream &os, AngularVelocity const &_value)
548 {
549  return os << double(_value);
550 }
551 
552 } // namespace physics
553 } // namespace ad
554 
555 namespace std {
559 inline std::string to_string(::ad::physics::AngularVelocity const &value)
560 {
561  return to_string(static_cast<double>(value));
562 }
563 } // namespace std
564 #endif // GEN_GUARD_AD_PHYSICS_ANGULARVELOCITY
ad::physics::AngularVelocity::isValid
bool isValid() const
Definition: AngularVelocity.hpp:382
ad
namespace ad
Definition: Acceleration.hpp:30
ad::physics::AngularVelocity::operator/
double operator/(const AngularVelocity &other) const
standard arithmetic operator
Definition: AngularVelocity.hpp:340
ad::physics::AngularVelocity::ensureValid
void ensureValid() const
ensure that the AngularVelocity is valid
Definition: AngularVelocity.hpp:395
ad::physics::AngularVelocity::operator>=
bool operator>=(const AngularVelocity &other) const
standard comparison operator
Definition: AngularVelocity.hpp:194
std::numeric_limits<::ad::physics::AngularVelocity >::max
static inline ::ad::physics::AngularVelocity max()
Definition: AngularVelocity.hpp:508
ad::physics::AngularVelocity::ensureValidNonZero
void ensureValidNonZero() const
ensure that the AngularVelocity is valid and non zero
Definition: AngularVelocity.hpp:412
ad::physics::AngularVelocity::cMaxValue
static const double cMaxValue
constant defining the maximum valid AngularVelocity value (used in isValid())
Definition: AngularVelocity.hpp:70
ad::physics::AngularVelocity::operator*
AngularVelocity operator*(const double &scalar) const
standard arithmetic operator
Definition: AngularVelocity.hpp:303
operator*
inline ::ad::physics::AngularVelocity operator*(const double &other, ::ad::physics::AngularVelocity const &value)
standard arithmetic operator
Definition: AngularVelocity.hpp:468
std::numeric_limits<::ad::physics::AngularVelocity >::lowest
static inline ::ad::physics::AngularVelocity lowest()
Definition: AngularVelocity.hpp:501
std::numeric_limits<::ad::physics::AngularVelocity >::epsilon
static inline ::ad::physics::AngularVelocity epsilon()
Definition: AngularVelocity.hpp:516
ad::physics::AngularVelocity::operator<
bool operator<(const AngularVelocity &other) const
standard comparison operator
Definition: AngularVelocity.hpp:178
ad::physics::AngularVelocity::AngularVelocity
_AD_PHYSICS_ANGULARVELOCITY_EXPLICIT_CONVERSION_ AngularVelocity(double const iAngularVelocity)
standard constructor
Definition: AngularVelocity.hpp:94
ad::physics::AngularVelocity::getMax
static AngularVelocity getMax()
get maximum valid AngularVelocity (i.e. cMaxValue)
Definition: AngularVelocity.hpp:435
ad::physics::AngularVelocity::operator+
AngularVelocity operator+(const AngularVelocity &other) const
standard arithmetic operator
Definition: AngularVelocity.hpp:227
ad::physics::AngularVelocity::operator-
AngularVelocity operator-(const AngularVelocity &other) const
standard arithmetic operator
Definition: AngularVelocity.hpp:265
ad::physics::AngularVelocity::operator>
bool operator>(const AngularVelocity &other) const
standard comparison operator
Definition: AngularVelocity.hpp:162
ad::physics::AngularVelocity::operator<=
bool operator<=(const AngularVelocity &other) const
standard comparison operator
Definition: AngularVelocity.hpp:210
ad::physics::AngularVelocity::operator=
AngularVelocity & operator=(const AngularVelocity &other)=default
standard assignment operator
ad::physics::AngularVelocity::operator/
AngularVelocity operator/(const double &scalar) const
standard arithmetic operator
Definition: AngularVelocity.hpp:321
ad::physics::AngularVelocity::operator!=
bool operator!=(const AngularVelocity &other) const
standard comparison operator
Definition: AngularVelocity.hpp:148
ad::physics::AngularVelocity::operator-
AngularVelocity operator-() const
standard arithmetic operator
Definition: AngularVelocity.hpp:356
std::fabs
inline ::ad::physics::Weight fabs(const ::ad::physics::Weight other)
overload of the std::fabs for Weight
Definition: Weight.hpp:480
std::fabs
inline ::ad::physics::Acceleration fabs(const ::ad::physics::Acceleration other)
overload of the std::fabs for Acceleration
Definition: Acceleration.hpp:481
ad::physics::AngularVelocity::operator==
bool operator==(const AngularVelocity &other) const
standard comparison operator
Definition: AngularVelocity.hpp:134
std::to_string
std::string to_string(::ad::physics::Acceleration const &value)
overload of the std::to_string for Acceleration
Definition: Acceleration.hpp:559
ad::physics::AngularVelocity::operator-=
AngularVelocity operator-=(const AngularVelocity &other)
standard arithmetic operator
Definition: AngularVelocity.hpp:284
ad::physics::AngularVelocity
DataType AngularVelocity.
Definition: AngularVelocity.hpp:59
ad::physics::AngularVelocity::getPrecision
static AngularVelocity getPrecision()
get assumed accuracy of AngularVelocity (i.e. cPrecisionValue)
Definition: AngularVelocity.hpp:443
ad::physics::AngularVelocity::getMin
static AngularVelocity getMin()
get minimum valid AngularVelocity (i.e. cMinValue)
Definition: AngularVelocity.hpp:427
_AD_PHYSICS_ANGULARVELOCITY_EXPLICIT_CONVERSION_
#define _AD_PHYSICS_ANGULARVELOCITY_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: AngularVelocity.hpp:50
ad::physics::AngularVelocity::cMinValue
static const double cMinValue
constant defining the minimum valid AngularVelocity value (used in isValid())
Definition: AngularVelocity.hpp:65
ad::physics::AngularVelocity::operator+=
AngularVelocity & operator+=(const AngularVelocity &other)
standard arithmetic operator
Definition: AngularVelocity.hpp:246
ad::physics::AngularVelocity::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed AngularVelocity value accuracy (used in comparison operator==(),...
Definition: AngularVelocity.hpp:76
ad::physics::operator<<
std::ostream & operator<<(std::ostream &os, Acceleration const &_value)
standard ostream operator
Definition: Acceleration.hpp:547
ad::physics::AngularVelocity::AngularVelocity
AngularVelocity()
default constructor
Definition: AngularVelocity.hpp:84