ad_physics
Distance.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_DISTANCE_THROWS_EXCEPTION 1
40 
41 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
42 
45 #define _AD_PHYSICS_DISTANCE_EXPLICIT_CONVERSION_ explicit
46 #else
47 
50 #define _AD_PHYSICS_DISTANCE_EXPLICIT_CONVERSION_
51 #endif
52 
59 class DistanceSquared;
60 
67 class Distance
68 {
69 public:
73  static const double cMinValue;
74 
78  static const double cMaxValue;
79 
84  static const double cPrecisionValue;
85 
93  : mDistance(std::numeric_limits<double>::quiet_NaN())
94  {
95  }
96 
103  : mDistance(iDistance)
104  {
105  }
106 
110  Distance(const Distance &other) = default;
111 
115  Distance(Distance &&other) = default;
116 
124  Distance &operator=(const Distance &other) = default;
125 
133  Distance &operator=(Distance &&other) = default;
134 
142  bool operator==(const Distance &other) const
143  {
144  ensureValid();
145  other.ensureValid();
146  return std::fabs(mDistance - other.mDistance) < cPrecisionValue;
147  }
148 
156  bool operator!=(const Distance &other) const
157  {
158  return !operator==(other);
159  }
160 
170  bool operator>(const Distance &other) const
171  {
172  ensureValid();
173  other.ensureValid();
174  return (mDistance > other.mDistance) && operator!=(other);
175  }
176 
186  bool operator<(const Distance &other) const
187  {
188  ensureValid();
189  other.ensureValid();
190  return (mDistance < other.mDistance) && operator!=(other);
191  }
192 
202  bool operator>=(const Distance &other) const
203  {
204  ensureValid();
205  other.ensureValid();
206  return ((mDistance > other.mDistance) || operator==(other));
207  }
208 
218  bool operator<=(const Distance &other) const
219  {
220  ensureValid();
221  other.ensureValid();
222  return ((mDistance < other.mDistance) || operator==(other));
223  }
224 
235  Distance operator+(const Distance &other) const
236  {
237  ensureValid();
238  other.ensureValid();
239  Distance const result(mDistance + other.mDistance);
240  result.ensureValid();
241  return result;
242  }
243 
254  Distance &operator+=(const Distance &other)
255  {
256  ensureValid();
257  other.ensureValid();
258  mDistance += other.mDistance;
259  ensureValid();
260  return *this;
261  }
262 
273  Distance operator-(const Distance &other) const
274  {
275  ensureValid();
276  other.ensureValid();
277  Distance const result(mDistance - other.mDistance);
278  result.ensureValid();
279  return result;
280  }
281 
293  {
294  ensureValid();
295  other.ensureValid();
296  mDistance -= other.mDistance;
297  ensureValid();
298  return *this;
299  }
300 
312  DistanceSquared operator*(const Distance &other) const;
313 
324  Distance operator*(const double &scalar) const
325  {
326  ensureValid();
327  Distance const result(mDistance * scalar);
328  result.ensureValid();
329  return result;
330  }
331 
342  Distance operator/(const double &scalar) const
343  {
344  Distance const scalarDistance(scalar);
345  Distance const result(operator/(scalarDistance));
346  result.ensureValid();
347  return result;
348  }
349 
361  double operator/(const Distance &other) const
362  {
363  ensureValid();
364  other.ensureValidNonZero();
365  double const result = mDistance / other.mDistance;
366  return result;
367  }
368 
378  {
379  ensureValid();
380  Distance const result(-mDistance);
381  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
382  return result;
383  }
384 
392  {
393  return mDistance;
394  }
395 
403  bool isValid() const
404  {
405  auto const valueClass = std::fpclassify(mDistance);
406  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mDistance)
407  && (mDistance <= cMaxValue);
408  }
409 
416  void ensureValid() const
417  {
418  if (!isValid())
419  {
420  spdlog::info("ensureValid(::ad::physics::Distance)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
421 #if (AD_PHYSICS_DISTANCE_THROWS_EXCEPTION == 1)
422  throw std::out_of_range("Distance value out of range"); // LCOV_EXCL_BR_LINE
423 #endif
424  }
425  }
426 
433  void ensureValidNonZero() const
434  {
435  ensureValid();
436  if (operator==(Distance(0.))) // LCOV_EXCL_BR_LINE
437  {
438  spdlog::info("ensureValid(::ad::physics::Distance)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
439 #if (AD_PHYSICS_DISTANCE_THROWS_EXCEPTION == 1)
440  throw std::out_of_range("Distance value is zero"); // LCOV_EXCL_BR_LINE
441 #endif
442  }
443  }
444 
448  static Distance getMin()
449  {
450  return Distance(cMinValue);
451  }
452 
456  static Distance getMax()
457  {
458  return Distance(cMaxValue);
459  }
460 
465  {
466  return Distance(cPrecisionValue);
467  }
468 
469 private:
473  double mDistance;
474 };
475 
476 } // namespace physics
477 } // namespace ad
489 inline ::ad::physics::Distance operator*(const double &other, ::ad::physics::Distance const &value)
490 {
491  return value.operator*(other);
492 }
493 
497 namespace std {
498 
502 inline ::ad::physics::Distance fabs(const ::ad::physics::Distance other)
503 {
504  ::ad::physics::Distance const result(std::fabs(static_cast<double>(other)));
505  return result;
506 }
507 
516 template <> class numeric_limits<::ad::physics::Distance> : public numeric_limits<double>
517 {
518 public:
522  static inline ::ad::physics::Distance lowest()
523  {
524  return ::ad::physics::Distance::getMin();
525  }
529  static inline ::ad::physics::Distance max()
530  {
531  return ::ad::physics::Distance::getMax();
532  }
533 
537  static inline ::ad::physics::Distance epsilon()
538  {
539  return ::ad::physics::Distance::getPrecision();
540  }
541 };
542 
543 } // namespace std
544 
548 #ifndef GEN_GUARD_AD_PHYSICS_DISTANCE
549 #define GEN_GUARD_AD_PHYSICS_DISTANCE
550 
553 namespace ad {
557 namespace physics {
558 
568 inline std::ostream &operator<<(std::ostream &os, Distance const &_value)
569 {
570  return os << double(_value);
571 }
572 
573 } // namespace physics
574 } // namespace ad
575 
576 namespace std {
580 inline std::string to_string(::ad::physics::Distance const &value)
581 {
582  return to_string(static_cast<double>(value));
583 }
584 } // namespace std
585 #endif // GEN_GUARD_AD_PHYSICS_DISTANCE
ad
namespace ad
Definition: Acceleration.hpp:30
ad::physics::Distance
DataType Distance.
Definition: Distance.hpp:67
ad::physics::Distance::getPrecision
static Distance getPrecision()
get assumed accuracy of Distance (i.e. cPrecisionValue)
Definition: Distance.hpp:464
ad::physics::DistanceSquared
DataType DistanceSquared.
Definition: DistanceSquared.hpp:67
ad::physics::Distance::operator*
DistanceSquared operator*(const Distance &other) const
standard arithmetic operator
ad::physics::Distance::ensureValid
void ensureValid() const
ensure that the Distance is valid
Definition: Distance.hpp:416
ad::physics::Distance::operator-
Distance operator-(const Distance &other) const
standard arithmetic operator
Definition: Distance.hpp:273
ad::physics::Distance::getMax
static Distance getMax()
get maximum valid Distance (i.e. cMaxValue)
Definition: Distance.hpp:456
_AD_PHYSICS_DISTANCE_EXPLICIT_CONVERSION_
#define _AD_PHYSICS_DISTANCE_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: Distance.hpp:50
ad::physics::Distance::operator/
double operator/(const Distance &other) const
standard arithmetic operator
Definition: Distance.hpp:361
std::numeric_limits<::ad::physics::Distance >::max
static inline ::ad::physics::Distance max()
Definition: Distance.hpp:529
ad::physics::Distance::operator-=
Distance operator-=(const Distance &other)
standard arithmetic operator
Definition: Distance.hpp:292
operator*
inline ::ad::physics::Distance operator*(const double &other, ::ad::physics::Distance const &value)
standard arithmetic operator
Definition: Distance.hpp:489
ad::physics::Distance::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed Distance value accuracy (used in comparison operator==(),...
Definition: Distance.hpp:84
ad::physics::Distance::operator-
Distance operator-() const
standard arithmetic operator
Definition: Distance.hpp:377
ad::physics::Distance::operator==
bool operator==(const Distance &other) const
standard comparison operator
Definition: Distance.hpp:142
ad::physics::Distance::operator>
bool operator>(const Distance &other) const
standard comparison operator
Definition: Distance.hpp:170
ad::physics::Distance::cMaxValue
static const double cMaxValue
constant defining the maximum valid Distance value (used in isValid())
Definition: Distance.hpp:78
ad::physics::Distance::operator<
bool operator<(const Distance &other) const
standard comparison operator
Definition: Distance.hpp:186
ad::physics::Distance::cMinValue
static const double cMinValue
constant defining the minimum valid Distance value (used in isValid())
Definition: Distance.hpp:73
ad::physics::Distance::isValid
bool isValid() const
Definition: Distance.hpp:403
ad::physics::Distance::operator<=
bool operator<=(const Distance &other) const
standard comparison operator
Definition: Distance.hpp:218
std::fabs
inline ::ad::physics::Weight fabs(const ::ad::physics::Weight other)
overload of the std::fabs for Weight
Definition: Weight.hpp:480
ad::physics::Distance::operator=
Distance & operator=(const Distance &other)=default
standard assignment operator
std::numeric_limits<::ad::physics::Distance >::lowest
static inline ::ad::physics::Distance lowest()
Definition: Distance.hpp:522
ad::physics::Distance::operator/
Distance operator/(const double &scalar) const
standard arithmetic operator
Definition: Distance.hpp:342
ad::physics::Distance::ensureValidNonZero
void ensureValidNonZero() const
ensure that the Distance is valid and non zero
Definition: Distance.hpp:433
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
ad::physics::Distance::operator+
Distance operator+(const Distance &other) const
standard arithmetic operator
Definition: Distance.hpp:235
ad::physics::Distance::Distance
_AD_PHYSICS_DISTANCE_EXPLICIT_CONVERSION_ Distance(double const iDistance)
standard constructor
Definition: Distance.hpp:102
ad::physics::Distance::operator+=
Distance & operator+=(const Distance &other)
standard arithmetic operator
Definition: Distance.hpp:254
ad::physics::operator<<
std::ostream & operator<<(std::ostream &os, Acceleration const &_value)
standard ostream operator
Definition: Acceleration.hpp:547
ad::physics::Distance::getMin
static Distance getMin()
get minimum valid Distance (i.e. cMinValue)
Definition: Distance.hpp:448
ad::physics::Distance::operator>=
bool operator>=(const Distance &other) const
standard comparison operator
Definition: Distance.hpp:202
ad::physics::Distance::operator!=
bool operator!=(const Distance &other) const
standard comparison operator
Definition: Distance.hpp:156
std::numeric_limits<::ad::physics::Distance >::epsilon
static inline ::ad::physics::Distance epsilon()
Definition: Distance.hpp:537
ad::physics::Distance::Distance
Distance()
default constructor
Definition: Distance.hpp:92
ad::physics::Distance::operator*
Distance operator*(const double &scalar) const
standard arithmetic operator
Definition: Distance.hpp:324