ad_physics
Angle.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_ANGLE_THROWS_EXCEPTION 1
40 
41 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
42 
45 #define _AD_PHYSICS_ANGLE_EXPLICIT_CONVERSION_ explicit
46 #else
47 
50 #define _AD_PHYSICS_ANGLE_EXPLICIT_CONVERSION_
51 #endif
52 
59 class Angle
60 {
61 public:
65  static const double cMinValue;
66 
70  static const double cMaxValue;
71 
76  static const double cPrecisionValue;
77 
85  : mAngle(std::numeric_limits<double>::quiet_NaN())
86  {
87  }
88 
95  : mAngle(iAngle)
96  {
97  }
98 
102  Angle(const Angle &other) = default;
103 
107  Angle(Angle &&other) = default;
108 
116  Angle &operator=(const Angle &other) = default;
117 
125  Angle &operator=(Angle &&other) = default;
126 
134  bool operator==(const Angle &other) const
135  {
136  ensureValid();
137  other.ensureValid();
138  return std::fabs(mAngle - other.mAngle) < cPrecisionValue;
139  }
140 
148  bool operator!=(const Angle &other) const
149  {
150  return !operator==(other);
151  }
152 
162  bool operator>(const Angle &other) const
163  {
164  ensureValid();
165  other.ensureValid();
166  return (mAngle > other.mAngle) && operator!=(other);
167  }
168 
178  bool operator<(const Angle &other) const
179  {
180  ensureValid();
181  other.ensureValid();
182  return (mAngle < other.mAngle) && operator!=(other);
183  }
184 
194  bool operator>=(const Angle &other) const
195  {
196  ensureValid();
197  other.ensureValid();
198  return ((mAngle > other.mAngle) || operator==(other));
199  }
200 
210  bool operator<=(const Angle &other) const
211  {
212  ensureValid();
213  other.ensureValid();
214  return ((mAngle < other.mAngle) || operator==(other));
215  }
216 
227  Angle operator+(const Angle &other) const
228  {
229  ensureValid();
230  other.ensureValid();
231  Angle const result(mAngle + other.mAngle);
232  result.ensureValid();
233  return result;
234  }
235 
246  Angle &operator+=(const Angle &other)
247  {
248  ensureValid();
249  other.ensureValid();
250  mAngle += other.mAngle;
251  ensureValid();
252  return *this;
253  }
254 
265  Angle operator-(const Angle &other) const
266  {
267  ensureValid();
268  other.ensureValid();
269  Angle const result(mAngle - other.mAngle);
270  result.ensureValid();
271  return result;
272  }
273 
284  Angle operator-=(const Angle &other)
285  {
286  ensureValid();
287  other.ensureValid();
288  mAngle -= other.mAngle;
289  ensureValid();
290  return *this;
291  }
292 
303  Angle operator*(const double &scalar) const
304  {
305  ensureValid();
306  Angle const result(mAngle * scalar);
307  result.ensureValid();
308  return result;
309  }
310 
321  Angle operator/(const double &scalar) const
322  {
323  Angle const scalarAngle(scalar);
324  Angle const result(operator/(scalarAngle));
325  result.ensureValid();
326  return result;
327  }
328 
340  double operator/(const Angle &other) const
341  {
342  ensureValid();
343  other.ensureValidNonZero();
344  double const result = mAngle / other.mAngle;
345  return result;
346  }
347 
356  Angle operator-() const
357  {
358  ensureValid();
359  Angle const result(-mAngle);
360  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
361  return result;
362  }
363 
371  {
372  return mAngle;
373  }
374 
382  bool isValid() const
383  {
384  auto const valueClass = std::fpclassify(mAngle);
385  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mAngle) && (mAngle <= cMaxValue);
386  }
387 
394  void ensureValid() const
395  {
396  if (!isValid())
397  {
398  spdlog::info("ensureValid(::ad::physics::Angle)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
399 #if (AD_PHYSICS_ANGLE_THROWS_EXCEPTION == 1)
400  throw std::out_of_range("Angle value out of range"); // LCOV_EXCL_BR_LINE
401 #endif
402  }
403  }
404 
411  void ensureValidNonZero() const
412  {
413  ensureValid();
414  if (operator==(Angle(0.))) // LCOV_EXCL_BR_LINE
415  {
416  spdlog::info("ensureValid(::ad::physics::Angle)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
417 #if (AD_PHYSICS_ANGLE_THROWS_EXCEPTION == 1)
418  throw std::out_of_range("Angle value is zero"); // LCOV_EXCL_BR_LINE
419 #endif
420  }
421  }
422 
426  static Angle getMin()
427  {
428  return Angle(cMinValue);
429  }
430 
434  static Angle getMax()
435  {
436  return Angle(cMaxValue);
437  }
438 
443  {
444  return Angle(cPrecisionValue);
445  }
446 
447 private:
451  double mAngle;
452 };
453 
454 } // namespace physics
455 } // namespace ad
467 inline ::ad::physics::Angle operator*(const double &other, ::ad::physics::Angle const &value)
468 {
469  return value.operator*(other);
470 }
471 
475 namespace std {
476 
480 inline ::ad::physics::Angle fabs(const ::ad::physics::Angle other)
481 {
482  ::ad::physics::Angle const result(std::fabs(static_cast<double>(other)));
483  return result;
484 }
485 
494 template <> class numeric_limits<::ad::physics::Angle> : public numeric_limits<double>
495 {
496 public:
500  static inline ::ad::physics::Angle lowest()
501  {
502  return ::ad::physics::Angle::getMin();
503  }
507  static inline ::ad::physics::Angle max()
508  {
509  return ::ad::physics::Angle::getMax();
510  }
511 
515  static inline ::ad::physics::Angle epsilon()
516  {
517  return ::ad::physics::Angle::getPrecision();
518  }
519 };
520 
521 } // namespace std
522 
526 #ifndef GEN_GUARD_AD_PHYSICS_ANGLE
527 #define GEN_GUARD_AD_PHYSICS_ANGLE
528 
531 namespace ad {
535 namespace physics {
536 
546 inline std::ostream &operator<<(std::ostream &os, Angle const &_value)
547 {
548  return os << double(_value);
549 }
550 
551 } // namespace physics
552 } // namespace ad
553 
554 namespace std {
558 inline std::string to_string(::ad::physics::Angle const &value)
559 {
560  return to_string(static_cast<double>(value));
561 }
562 } // namespace std
563 #endif // GEN_GUARD_AD_PHYSICS_ANGLE
ad
namespace ad
Definition: Acceleration.hpp:30
ad::physics::Angle::getMin
static Angle getMin()
get minimum valid Angle (i.e. cMinValue)
Definition: Angle.hpp:426
std::numeric_limits<::ad::physics::Angle >::epsilon
static inline ::ad::physics::Angle epsilon()
Definition: Angle.hpp:515
ad::physics::Angle
DataType Angle.
Definition: Angle.hpp:59
ad::physics::Angle::operator-
Angle operator-() const
standard arithmetic operator
Definition: Angle.hpp:356
ad::physics::Angle::isValid
bool isValid() const
Definition: Angle.hpp:382
std::numeric_limits<::ad::physics::Angle >::lowest
static inline ::ad::physics::Angle lowest()
Definition: Angle.hpp:500
ad::physics::Angle::Angle
Angle()
default constructor
Definition: Angle.hpp:84
ad::physics::Angle::getMax
static Angle getMax()
get maximum valid Angle (i.e. cMaxValue)
Definition: Angle.hpp:434
ad::physics::Angle::operator/
Angle operator/(const double &scalar) const
standard arithmetic operator
Definition: Angle.hpp:321
ad::physics::Angle::cMaxValue
static const double cMaxValue
constant defining the maximum valid Angle value (used in isValid())
Definition: Angle.hpp:70
ad::physics::Angle::operator<=
bool operator<=(const Angle &other) const
standard comparison operator
Definition: Angle.hpp:210
ad::physics::Angle::operator-
Angle operator-(const Angle &other) const
standard arithmetic operator
Definition: Angle.hpp:265
ad::physics::Angle::operator*
Angle operator*(const double &scalar) const
standard arithmetic operator
Definition: Angle.hpp:303
ad::physics::Angle::ensureValidNonZero
void ensureValidNonZero() const
ensure that the Angle is valid and non zero
Definition: Angle.hpp:411
_AD_PHYSICS_ANGLE_EXPLICIT_CONVERSION_
#define _AD_PHYSICS_ANGLE_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: Angle.hpp:50
ad::physics::Angle::operator!=
bool operator!=(const Angle &other) const
standard comparison operator
Definition: Angle.hpp:148
ad::physics::Angle::operator==
bool operator==(const Angle &other) const
standard comparison operator
Definition: Angle.hpp:134
ad::physics::Angle::operator<
bool operator<(const Angle &other) const
standard comparison operator
Definition: Angle.hpp:178
ad::physics::Angle::ensureValid
void ensureValid() const
ensure that the Angle is valid
Definition: Angle.hpp:394
ad::physics::Angle::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed Angle value accuracy (used in comparison operator==(),...
Definition: Angle.hpp:76
ad::physics::Angle::Angle
_AD_PHYSICS_ANGLE_EXPLICIT_CONVERSION_ Angle(double const iAngle)
standard constructor
Definition: Angle.hpp:94
ad::physics::Angle::operator+=
Angle & operator+=(const Angle &other)
standard arithmetic operator
Definition: Angle.hpp:246
ad::physics::Angle::operator=
Angle & operator=(const Angle &other)=default
standard assignment operator
ad::physics::Angle::operator>
bool operator>(const Angle &other) const
standard comparison operator
Definition: Angle.hpp:162
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
std::to_string
std::string to_string(::ad::physics::Acceleration const &value)
overload of the std::to_string for Acceleration
Definition: Acceleration.hpp:559
std::numeric_limits<::ad::physics::Angle >::max
static inline ::ad::physics::Angle max()
Definition: Angle.hpp:507
ad::physics::Angle::cMinValue
static const double cMinValue
constant defining the minimum valid Angle value (used in isValid())
Definition: Angle.hpp:65
ad::physics::Angle::operator+
Angle operator+(const Angle &other) const
standard arithmetic operator
Definition: Angle.hpp:227
ad::physics::Angle::operator-=
Angle operator-=(const Angle &other)
standard arithmetic operator
Definition: Angle.hpp:284
ad::physics::operator<<
std::ostream & operator<<(std::ostream &os, Acceleration const &_value)
standard ostream operator
Definition: Acceleration.hpp:547
ad::physics::Angle::operator>=
bool operator>=(const Angle &other) const
standard comparison operator
Definition: Angle.hpp:194
operator*
inline ::ad::physics::Angle operator*(const double &other, ::ad::physics::Angle const &value)
standard arithmetic operator
Definition: Angle.hpp:467
ad::physics::Angle::getPrecision
static Angle getPrecision()
get assumed accuracy of Angle (i.e. cPrecisionValue)
Definition: Angle.hpp:442
ad::physics::Angle::operator/
double operator/(const Angle &other) const
standard arithmetic operator
Definition: Angle.hpp:340