ad_map_access
Latitude.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_LATITUDE_THROWS_EXCEPTION 1
46 
47 #if SAFE_DATATYPES_EXPLICIT_CONVERSION
48 
51 #define _AD_MAP_POINT_LATITUDE_EXPLICIT_CONVERSION_ explicit
52 #else
53 
56 #define _AD_MAP_POINT_LATITUDE_EXPLICIT_CONVERSION_
57 #endif
58 
65 class Latitude
66 {
67 public:
71  static const double cMinValue;
72 
76  static const double cMaxValue;
77 
82  static const double cPrecisionValue;
83 
91  : mLatitude(std::numeric_limits<double>::quiet_NaN())
92  {
93  }
94 
101  : mLatitude(iLatitude)
102  {
103  }
104 
108  Latitude(const Latitude &other) = default;
109 
113  Latitude(Latitude &&other) = default;
114 
122  Latitude &operator=(const Latitude &other) = default;
123 
131  Latitude &operator=(Latitude &&other) = default;
132 
140  bool operator==(const Latitude &other) const
141  {
142  ensureValid();
143  other.ensureValid();
144  return std::fabs(mLatitude - other.mLatitude) < cPrecisionValue;
145  }
146 
154  bool operator!=(const Latitude &other) const
155  {
156  return !operator==(other);
157  }
158 
168  bool operator>(const Latitude &other) const
169  {
170  ensureValid();
171  other.ensureValid();
172  return (mLatitude > other.mLatitude) && operator!=(other);
173  }
174 
184  bool operator<(const Latitude &other) const
185  {
186  ensureValid();
187  other.ensureValid();
188  return (mLatitude < other.mLatitude) && operator!=(other);
189  }
190 
200  bool operator>=(const Latitude &other) const
201  {
202  ensureValid();
203  other.ensureValid();
204  return ((mLatitude > other.mLatitude) || operator==(other));
205  }
206 
216  bool operator<=(const Latitude &other) const
217  {
218  ensureValid();
219  other.ensureValid();
220  return ((mLatitude < other.mLatitude) || operator==(other));
221  }
222 
233  Latitude operator+(const Latitude &other) const
234  {
235  ensureValid();
236  other.ensureValid();
237  Latitude const result(mLatitude + other.mLatitude);
238  result.ensureValid();
239  return result;
240  }
241 
252  Latitude &operator+=(const Latitude &other)
253  {
254  ensureValid();
255  other.ensureValid();
256  mLatitude += other.mLatitude;
257  ensureValid();
258  return *this;
259  }
260 
271  Latitude operator-(const Latitude &other) const
272  {
273  ensureValid();
274  other.ensureValid();
275  Latitude const result(mLatitude - other.mLatitude);
276  result.ensureValid();
277  return result;
278  }
279 
291  {
292  ensureValid();
293  other.ensureValid();
294  mLatitude -= other.mLatitude;
295  ensureValid();
296  return *this;
297  }
298 
309  Latitude operator*(const double &scalar) const
310  {
311  ensureValid();
312  Latitude const result(mLatitude * scalar);
313  result.ensureValid();
314  return result;
315  }
316 
327  Latitude operator/(const double &scalar) const
328  {
329  Latitude const scalarLatitude(scalar);
330  Latitude const result(operator/(scalarLatitude));
331  result.ensureValid();
332  return result;
333  }
334 
346  double operator/(const Latitude &other) const
347  {
348  ensureValid();
349  other.ensureValidNonZero();
350  double const result = mLatitude / other.mLatitude;
351  return result;
352  }
353 
363  {
364  ensureValid();
365  Latitude const result(-mLatitude);
366  result.ensureValid(); // LCOV_EXCL_BR_LINE Some types do not throw an exception
367  return result;
368  }
369 
377  {
378  return mLatitude;
379  }
380 
388  bool isValid() const
389  {
390  auto const valueClass = std::fpclassify(mLatitude);
391  return ((valueClass == FP_NORMAL) || (valueClass == FP_ZERO)) && (cMinValue <= mLatitude)
392  && (mLatitude <= cMaxValue);
393  }
394 
401  void ensureValid() const
402  {
403  if (!isValid())
404  {
405  spdlog::info("ensureValid(::ad::map::point::Latitude)>> {} value out of range", *this); // LCOV_EXCL_BR_LINE
406 #if (AD_MAP_POINT_LATITUDE_THROWS_EXCEPTION == 1)
407  throw std::out_of_range("Latitude value out of range"); // LCOV_EXCL_BR_LINE
408 #endif
409  }
410  }
411 
418  void ensureValidNonZero() const
419  {
420  ensureValid();
421  if (operator==(Latitude(0.))) // LCOV_EXCL_BR_LINE
422  {
423  spdlog::info("ensureValid(::ad::map::point::Latitude)>> {} value is zero", *this); // LCOV_EXCL_BR_LINE
424 #if (AD_MAP_POINT_LATITUDE_THROWS_EXCEPTION == 1)
425  throw std::out_of_range("Latitude value is zero"); // LCOV_EXCL_BR_LINE
426 #endif
427  }
428  }
429 
433  static Latitude getMin()
434  {
435  return Latitude(cMinValue);
436  }
437 
441  static Latitude getMax()
442  {
443  return Latitude(cMaxValue);
444  }
445 
450  {
451  return Latitude(cPrecisionValue);
452  }
453 
454 private:
458  double mLatitude;
459 };
460 
461 } // namespace point
462 } // namespace map
463 } // namespace ad
475 inline ::ad::map::point::Latitude operator*(const double &other, ::ad::map::point::Latitude const &value)
476 {
477  return value.operator*(other);
478 }
479 
483 namespace std {
484 
488 inline ::ad::map::point::Latitude fabs(const ::ad::map::point::Latitude other)
489 {
490  ::ad::map::point::Latitude const result(std::fabs(static_cast<double>(other)));
491  return result;
492 }
493 
502 template <> class numeric_limits<::ad::map::point::Latitude> : public numeric_limits<double>
503 {
504 public:
508  static inline ::ad::map::point::Latitude lowest()
509  {
510  return ::ad::map::point::Latitude::getMin();
511  }
515  static inline ::ad::map::point::Latitude max()
516  {
517  return ::ad::map::point::Latitude::getMax();
518  }
519 
523  static inline ::ad::map::point::Latitude epsilon()
524  {
525  return ::ad::map::point::Latitude::getPrecision();
526  }
527 };
528 
529 } // namespace std
530 
534 #ifndef GEN_GUARD_AD_MAP_POINT_LATITUDE
535 #define GEN_GUARD_AD_MAP_POINT_LATITUDE
536 
539 namespace ad {
543 namespace map {
549 namespace point {
550 
560 inline std::ostream &operator<<(std::ostream &os, Latitude const &_value)
561 {
562  return os << double(_value);
563 }
564 
565 } // namespace point
566 } // namespace map
567 } // namespace ad
568 
569 namespace std {
573 inline std::string to_string(::ad::map::point::Latitude const &value)
574 {
575  return to_string(static_cast<double>(value));
576 }
577 } // namespace std
578 #endif // GEN_GUARD_AD_MAP_POINT_LATITUDE
ad
namespace ad
Definition: GeometryStoreItem.hpp:28
ad::map::point::Latitude::Latitude
_AD_MAP_POINT_LATITUDE_EXPLICIT_CONVERSION_ Latitude(double const iLatitude)
standard constructor
Definition: Latitude.hpp:100
ad::map::point::operator<<
std::ostream & operator<<(std::ostream &os, Altitude const &_value)
standard ostream operator
Definition: Altitude.hpp:562
ad::map::point::Latitude::operator>=
bool operator>=(const Latitude &other) const
standard comparison operator
Definition: Latitude.hpp:200
ad::map::point::Latitude::ensureValidNonZero
void ensureValidNonZero() const
ensure that the Latitude is valid and non zero
Definition: Latitude.hpp:418
ad::map::point::Latitude::operator<
bool operator<(const Latitude &other) const
standard comparison operator
Definition: Latitude.hpp:184
ad::map::point::Latitude::operator==
bool operator==(const Latitude &other) const
standard comparison operator
Definition: Latitude.hpp:140
std::numeric_limits<::ad::map::point::Latitude >::epsilon
static inline ::ad::map::point::Latitude epsilon()
Definition: Latitude.hpp:523
ad::map::point::Latitude::operator<=
bool operator<=(const Latitude &other) const
standard comparison operator
Definition: Latitude.hpp:216
ad::map::point::Latitude::cMinValue
static const double cMinValue
constant defining the minimum valid Latitude value (used in isValid())
Definition: Latitude.hpp:71
ad::map::point::Latitude::ensureValid
void ensureValid() const
ensure that the Latitude is valid
Definition: Latitude.hpp:401
ad::map::point::Latitude::getMin
static Latitude getMin()
get minimum valid Latitude (i.e. cMinValue)
Definition: Latitude.hpp:433
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
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::Latitude::operator+=
Latitude & operator+=(const Latitude &other)
standard arithmetic operator
Definition: Latitude.hpp:252
ad::map::point::Latitude::operator=
Latitude & operator=(const Latitude &other)=default
standard assignment operator
ad::map::point::Latitude::operator-
Latitude operator-(const Latitude &other) const
standard arithmetic operator
Definition: Latitude.hpp:271
ad::map::point::Latitude::cPrecisionValue
static const double cPrecisionValue
constant defining the assumed Latitude value accuracy (used in comparison operator==(),...
Definition: Latitude.hpp:82
ad::map::point::Latitude::operator-=
Latitude operator-=(const Latitude &other)
standard arithmetic operator
Definition: Latitude.hpp:290
ad::map::point::Latitude::operator-
Latitude operator-() const
standard arithmetic operator
Definition: Latitude.hpp:362
_AD_MAP_POINT_LATITUDE_EXPLICIT_CONVERSION_
#define _AD_MAP_POINT_LATITUDE_EXPLICIT_CONVERSION_
Enable/Disable explicit conversion. Currently set to "implicit conversion allowed".
Definition: Latitude.hpp:56
ad::map::point::Latitude::operator+
Latitude operator+(const Latitude &other) const
standard arithmetic operator
Definition: Latitude.hpp:233
ad::map::point::Latitude::operator*
Latitude operator*(const double &scalar) const
standard arithmetic operator
Definition: Latitude.hpp:309
ad::map::point::Latitude
DataType Latitude.
Definition: Latitude.hpp:65
std::numeric_limits<::ad::map::point::Latitude >::max
static inline ::ad::map::point::Latitude max()
Definition: Latitude.hpp:515
std::numeric_limits<::ad::map::point::Latitude >::lowest
static inline ::ad::map::point::Latitude lowest()
Definition: Latitude.hpp:508
ad::map::point::Latitude::operator/
Latitude operator/(const double &scalar) const
standard arithmetic operator
Definition: Latitude.hpp:327
ad::map::point::Latitude::isValid
bool isValid() const
Definition: Latitude.hpp:388
ad::map::point::Latitude::getMax
static Latitude getMax()
get maximum valid Latitude (i.e. cMaxValue)
Definition: Latitude.hpp:441
ad::map::point::Latitude::operator!=
bool operator!=(const Latitude &other) const
standard comparison operator
Definition: Latitude.hpp:154
ad::map::point::Latitude::operator>
bool operator>(const Latitude &other) const
standard comparison operator
Definition: Latitude.hpp:168
ad::map::point::Latitude::cMaxValue
static const double cMaxValue
constant defining the maximum valid Latitude value (used in isValid())
Definition: Latitude.hpp:76
ad::map::point::Latitude::Latitude
Latitude()
default constructor
Definition: Latitude.hpp:90
ad::map::point::Latitude::operator/
double operator/(const Latitude &other) const
standard arithmetic operator
Definition: Latitude.hpp:346
ad::map::point::Latitude::getPrecision
static Latitude getPrecision()
get assumed accuracy of Latitude (i.e. cPrecisionValue)
Definition: Latitude.hpp:449
operator*
inline ::ad::map::point::Latitude operator*(const double &other, ::ad::map::point::Latitude const &value)
standard arithmetic operator
Definition: Latitude.hpp:475
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