ad_physics
DistanceSquared.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_DISTANCESQUARED_THROWS_EXCEPTION 1
40 
41 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
42 
45 #define _AD_PHYSICS_DISTANCESQUARED_EXPLICIT_CONVERSION_ explicit
46 #else
47 
50 #define _AD_PHYSICS_DISTANCESQUARED_EXPLICIT_CONVERSION_
51 #endif
52 
59 class Distance;
60 
68 {
69 public:
73  static const double cMinValue;
74 
78  static const double cMaxValue;
79 
84  static const double cPrecisionValue;
85 
93  : mDistanceSquared(std::numeric_limits<double>::quiet_NaN())
94  {
95  }
96 
103  : mDistanceSquared(iDistanceSquared)
104  {
105  }
106 
110  DistanceSquared(const DistanceSquared &other) = default;
111 
115  DistanceSquared(DistanceSquared &&other) = default;
116 
124  DistanceSquared &operator=(const DistanceSquared &other) = default;
125 
133  DistanceSquared &operator=(DistanceSquared &&other) = default;
134 
142  bool operator==(const DistanceSquared &other) const
143  {
144  ensureValid();
145  other.ensureValid();
146  return std::fabs(mDistanceSquared - other.mDistanceSquared) < cPrecisionValue;
147  }
148 
156  bool operator!=(const DistanceSquared &other) const
157  {
158  return !operator==(other);
159  }
160 
170  bool operator>(const DistanceSquared &other) const
171  {
172  ensureValid();
173  other.ensureValid();
174  return (mDistanceSquared > other.mDistanceSquared) && operator!=(other);
175  }
176 
186  bool operator<(const DistanceSquared &other) const
187  {
188  ensureValid();
189  other.ensureValid();
190  return (mDistanceSquared < other.mDistanceSquared) && operator!=(other);
191  }
192 
202  bool operator>=(const DistanceSquared &other) const
203  {
204  ensureValid();
205  other.ensureValid();
206  return ((mDistanceSquared > other.mDistanceSquared) || operator==(other));
207  }
208 
218  bool operator<=(const DistanceSquared &other) const
219  {
220  ensureValid();
221  other.ensureValid();
222  return ((mDistanceSquared < other.mDistanceSquared) || operator==(other));
223  }
224 
236  {
237  ensureValid();
238  other.ensureValid();
239  DistanceSquared const result(mDistanceSquared + other.mDistanceSquared);
240  result.ensureValid();
241  return result;
242  }
243 
255  {
256  ensureValid();
257  other.ensureValid();
258  mDistanceSquared += other.mDistanceSquared;
259  ensureValid();
260  return *this;
261  }
262 
274  {
275  ensureValid();
276  other.ensureValid();
277  DistanceSquared const result(mDistanceSquared - other.mDistanceSquared);
278  result.ensureValid();
279  return result;
280  }
281 
293  {
294  ensureValid();
295  other.ensureValid();
296  mDistanceSquared -= other.mDistanceSquared;
297  ensureValid();
298  return *this;
299  }
300 
311  DistanceSquared operator*(const double &scalar) const
312  {
313  ensureValid();
314  DistanceSquared const result(mDistanceSquared * scalar);
315  result.ensureValid();
316  return result;
317  }
318 
329  DistanceSquared operator/(const double &scalar) const
330  {
331  DistanceSquared const scalarDistanceSquared(scalar);
332  DistanceSquared const result(operator/(scalarDistanceSquared));
333  result.ensureValid();
334  return result;
335  }
336 
348  double operator/(const DistanceSquared &other) const
349  {
350  ensureValid();
351  other.ensureValidNonZero();
352  double const result = mDistanceSquared / other.mDistanceSquared;
353  return result;
354  }
355 
365  {
366  ensureValid();
367  DistanceSquared const result(-mDistanceSquared);
368  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
369  return result;
370  }
371 
379  {
380  return mDistanceSquared;
381  }
382 
390  bool isValid() const
391  {
392  auto const valueClass = std::fpclassify(mDistanceSquared);
393  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mDistanceSquared)
394  && (mDistanceSquared <= cMaxValue);
395  }
396 
403  void ensureValid() const
404  {
405  if (!isValid())
406  {
407  spdlog::info("ensureValid(::ad::physics::DistanceSquared)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
408 #if (AD_PHYSICS_DISTANCESQUARED_THROWS_EXCEPTION == 1)
409  throw std::out_of_range("DistanceSquared value out of range"); // LCOV_EXCL_BR_LINE
410 #endif
411  }
412  }
413 
420  void ensureValidNonZero() const
421  {
422  ensureValid();
423  if (operator==(DistanceSquared(0.))) // LCOV_EXCL_BR_LINE
424  {
425  spdlog::info("ensureValid(::ad::physics::DistanceSquared)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
426 #if (AD_PHYSICS_DISTANCESQUARED_THROWS_EXCEPTION == 1)
427  throw std::out_of_range("DistanceSquared value is zero"); // LCOV_EXCL_BR_LINE
428 #endif
429  }
430  }
431 
436  {
437  return DistanceSquared(cMinValue);
438  }
439 
444  {
445  return DistanceSquared(cMaxValue);
446  }
447 
452  {
454  }
455 
456 private:
460  double mDistanceSquared;
461 };
462 
463 } // namespace physics
464 } // namespace ad
476 inline ::ad::physics::DistanceSquared operator*(const double &other, ::ad::physics::DistanceSquared const &value)
477 {
478  return value.operator*(other);
479 }
480 
484 namespace std {
485 
489 inline ::ad::physics::DistanceSquared fabs(const ::ad::physics::DistanceSquared other)
490 {
491  ::ad::physics::DistanceSquared const result(std::fabs(static_cast<double>(other)));
492  return result;
493 }
494 
503 template <> class numeric_limits<::ad::physics::DistanceSquared> : public numeric_limits<double>
504 {
505 public:
509  static inline ::ad::physics::DistanceSquared lowest()
510  {
511  return ::ad::physics::DistanceSquared::getMin();
512  }
516  static inline ::ad::physics::DistanceSquared max()
517  {
518  return ::ad::physics::DistanceSquared::getMax();
519  }
520 
524  static inline ::ad::physics::DistanceSquared epsilon()
525  {
526  return ::ad::physics::DistanceSquared::getPrecision();
527  }
528 };
529 
536 
537 } // namespace std
538 
542 #ifndef GEN_GUARD_AD_PHYSICS_DISTANCESQUARED
543 #define GEN_GUARD_AD_PHYSICS_DISTANCESQUARED
544 
547 namespace ad {
551 namespace physics {
552 
562 inline std::ostream &operator<<(std::ostream &os, DistanceSquared const &_value)
563 {
564  return os << double(_value);
565 }
566 
567 } // namespace physics
568 } // namespace ad
569 
570 namespace std {
574 inline std::string to_string(::ad::physics::DistanceSquared const &value)
575 {
576  return to_string(static_cast<double>(value));
577 }
578 } // namespace std
579 #endif // GEN_GUARD_AD_PHYSICS_DISTANCESQUARED
ad::physics::DistanceSquared::getMin
static DistanceSquared getMin()
get minimum valid DistanceSquared (i.e. cMinValue)
Definition: DistanceSquared.hpp:435
ad
namespace ad
Definition: Acceleration.hpp:30
ad::physics::Distance
DataType Distance.
Definition: Distance.hpp:67
std::numeric_limits<::ad::physics::DistanceSquared >::epsilon
static inline ::ad::physics::DistanceSquared epsilon()
Definition: DistanceSquared.hpp:524
ad::physics::DistanceSquared::operator*
DistanceSquared operator*(const double &scalar) const
standard arithmetic operator
Definition: DistanceSquared.hpp:311
ad::physics::DistanceSquared
DataType DistanceSquared.
Definition: DistanceSquared.hpp:67
ad::physics::DistanceSquared::operator>=
bool operator>=(const DistanceSquared &other) const
standard comparison operator
Definition: DistanceSquared.hpp:202
ad::physics::DistanceSquared::DistanceSquared
DistanceSquared()
default constructor
Definition: DistanceSquared.hpp:92
ad::physics::DistanceSquared::operator-
DistanceSquared operator-(const DistanceSquared &other) const
standard arithmetic operator
Definition: DistanceSquared.hpp:273
_AD_PHYSICS_DISTANCESQUARED_EXPLICIT_CONVERSION_
#define _AD_PHYSICS_DISTANCESQUARED_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: DistanceSquared.hpp:50
ad::physics::DistanceSquared::isValid
bool isValid() const
Definition: DistanceSquared.hpp:390
ad::physics::DistanceSquared::operator=
DistanceSquared & operator=(const DistanceSquared &other)=default
standard assignment operator
ad::physics::DistanceSquared::operator>
bool operator>(const DistanceSquared &other) const
standard comparison operator
Definition: DistanceSquared.hpp:170
ad::physics::DistanceSquared::getPrecision
static DistanceSquared getPrecision()
get assumed accuracy of DistanceSquared (i.e. cPrecisionValue)
Definition: DistanceSquared.hpp:451
ad::physics::DistanceSquared::operator/
DistanceSquared operator/(const double &scalar) const
standard arithmetic operator
Definition: DistanceSquared.hpp:329
ad::physics::DistanceSquared::getMax
static DistanceSquared getMax()
get maximum valid DistanceSquared (i.e. cMaxValue)
Definition: DistanceSquared.hpp:443
std::numeric_limits<::ad::physics::DistanceSquared >::max
static inline ::ad::physics::DistanceSquared max()
Definition: DistanceSquared.hpp:516
ad::physics::DistanceSquared::cMinValue
static const double cMinValue
constant defining the minimum valid DistanceSquared value (used in isValid())
Definition: DistanceSquared.hpp:73
ad::physics::DistanceSquared::cMaxValue
static const double cMaxValue
constant defining the maximum valid DistanceSquared value (used in isValid())
Definition: DistanceSquared.hpp:78
ad::physics::DistanceSquared::operator-
DistanceSquared operator-() const
standard arithmetic operator
Definition: DistanceSquared.hpp:364
ad::physics::DistanceSquared::operator/
double operator/(const DistanceSquared &other) const
standard arithmetic operator
Definition: DistanceSquared.hpp:348
ad::physics::DistanceSquared::operator==
bool operator==(const DistanceSquared &other) const
standard comparison operator
Definition: DistanceSquared.hpp:142
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::DistanceSquared::operator<=
bool operator<=(const DistanceSquared &other) const
standard comparison operator
Definition: DistanceSquared.hpp:218
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::DistanceSquared::operator+=
DistanceSquared & operator+=(const DistanceSquared &other)
standard arithmetic operator
Definition: DistanceSquared.hpp:254
ad::physics::DistanceSquared::ensureValidNonZero
void ensureValidNonZero() const
ensure that the DistanceSquared is valid and non zero
Definition: DistanceSquared.hpp:420
ad::physics::DistanceSquared::operator<
bool operator<(const DistanceSquared &other) const
standard comparison operator
Definition: DistanceSquared.hpp:186
ad::physics::DistanceSquared::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed DistanceSquared value accuracy (used in comparison operator==(),...
Definition: DistanceSquared.hpp:84
ad::physics::operator<<
std::ostream & operator<<(std::ostream &os, Acceleration const &_value)
standard ostream operator
Definition: Acceleration.hpp:547
ad::physics::DistanceSquared::ensureValid
void ensureValid() const
ensure that the DistanceSquared is valid
Definition: DistanceSquared.hpp:403
ad::physics::DistanceSquared::operator+
DistanceSquared operator+(const DistanceSquared &other) const
standard arithmetic operator
Definition: DistanceSquared.hpp:235
std::numeric_limits<::ad::physics::DistanceSquared >::lowest
static inline ::ad::physics::DistanceSquared lowest()
Definition: DistanceSquared.hpp:509
operator*
inline ::ad::physics::DistanceSquared operator*(const double &other, ::ad::physics::DistanceSquared const &value)
standard arithmetic operator
Definition: DistanceSquared.hpp:476
ad::physics::DistanceSquared::operator-=
DistanceSquared operator-=(const DistanceSquared &other)
standard arithmetic operator
Definition: DistanceSquared.hpp:292
ad::physics::DistanceSquared::DistanceSquared
_AD_PHYSICS_DISTANCESQUARED_EXPLICIT_CONVERSION_ DistanceSquared(double const iDistanceSquared)
standard constructor
Definition: DistanceSquared.hpp:102
ad::physics::DistanceSquared::operator!=
bool operator!=(const DistanceSquared &other) const
standard comparison operator
Definition: DistanceSquared.hpp:156