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