NumCpp  2.10.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
Dec.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <cmath>
31 #include <iostream>
32 #include <string>
33 
35 #include "NumCpp/Core/Types.hpp"
38 #include "NumCpp/Utils/num2str.hpp"
39 
40 namespace nc::coordinates
41 {
42  //================================================================================
44  enum class Sign
45  {
46  NEGATIVE = 0,
47  POSITIVE
48  };
49 
50  //================================================================================
52  class Dec
53  {
54  public:
55  //============================================================================
58  Dec() = default;
59 
60  //============================================================================
65  explicit Dec(double inDegrees) :
66  degrees_(inDegrees),
67  radians_(deg2rad(inDegrees))
68  {
69  if (inDegrees < -90 || inDegrees > 90)
70  {
71  THROW_INVALID_ARGUMENT_ERROR("input degrees must be of the range [-90, 90]");
72  }
73 
74  sign_ = degrees_ < 0 ? Sign::NEGATIVE : Sign::POSITIVE;
75  const double absDegrees = std::abs(degrees_);
76  degreesWhole_ = static_cast<uint8>(std::floor(absDegrees));
77 
78  const double decMinutes = (absDegrees - static_cast<double>(degreesWhole_)) * 60.;
79  minutes_ = static_cast<uint8>(std::floor(decMinutes));
80  seconds_ = (decMinutes - static_cast<double>(minutes_)) * 60.;
81  }
82 
83  //============================================================================
91  Dec(Sign inSign, uint8 inDegrees, uint8 inMinutes, double inSeconds) noexcept :
92  sign_(inSign),
93  degreesWhole_(inDegrees),
94  minutes_(inMinutes),
95  seconds_(inSeconds)
96  {
97  degrees_ = static_cast<double>(degreesWhole_) + static_cast<double>(minutes_) / 60. + seconds_ / 3600.;
98  degrees_ *= sign_ == Sign::NEGATIVE ? -1 : 1;
99 
100  radians_ = deg2rad(degrees_);
101  }
102 
103  //============================================================================
108  [[nodiscard]] Sign sign() const noexcept
109  {
110  return sign_;
111  }
112 
113  //============================================================================
118  [[nodiscard]] double degrees() const noexcept
119  {
120  return degrees_;
121  }
122 
123  //============================================================================
128  [[nodiscard]] double radians() const noexcept
129  {
130  return radians_;
131  }
132 
133  //============================================================================
138  [[nodiscard]] uint8 degreesWhole() const noexcept
139  {
140  return degreesWhole_;
141  }
142 
143  //============================================================================
148  [[nodiscard]] uint8 minutes() const noexcept
149  {
150  return minutes_;
151  }
152 
153  //============================================================================
158  [[nodiscard]] double seconds() const noexcept
159  {
160  return seconds_;
161  }
162 
163  //============================================================================
168  [[nodiscard]] std::string str() const
169  {
170  std::string strSign = sign_ == Sign::NEGATIVE ? "-" : "+";
171  std::string out = "Dec dms: " + strSign + utils::num2str(degreesWhole_) + " degrees, " +
172  utils::num2str(minutes_) + " minutes, ";
173  out += utils::num2str(seconds_) + " seconds\nDec degrees = " + utils::num2str(degrees_) + '\n';
174  return out;
175  }
176 
177  //============================================================================
180  void print() const
181  {
182  std::cout << *this;
183  }
184 
185  //============================================================================
192  bool operator==(const Dec& inRhs) const noexcept
193  {
194  return utils::essentiallyEqual(degrees_, inRhs.degrees_);
195  }
196 
197  //============================================================================
204  bool operator!=(const Dec& inRhs) const noexcept
205  {
206  return !(*this == inRhs);
207  }
208 
209  //============================================================================
217  friend std::ostream& operator<<(std::ostream& inStream, const Dec& inDec)
218  {
219  inStream << inDec.str();
220  return inStream;
221  }
222 
223  private:
224  //====================================Attributes==============================
225  Sign sign_{ Sign::POSITIVE };
226  uint8 degreesWhole_{ 0 };
227  uint8 minutes_{ 0 };
228  double seconds_{ 0. };
229  double degrees_{ 0. };
230  double radians_{ 0. };
231  };
232 } // namespace nc::coordinates
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
Holds a Declination object.
Definition: Dec.hpp:53
double seconds() const noexcept
Definition: Dec.hpp:158
bool operator==(const Dec &inRhs) const noexcept
Definition: Dec.hpp:192
bool operator!=(const Dec &inRhs) const noexcept
Definition: Dec.hpp:204
Dec(double inDegrees)
Definition: Dec.hpp:65
friend std::ostream & operator<<(std::ostream &inStream, const Dec &inDec)
Definition: Dec.hpp:217
std::string str() const
Definition: Dec.hpp:168
void print() const
Definition: Dec.hpp:180
uint8 degreesWhole() const noexcept
Definition: Dec.hpp:138
Sign sign() const noexcept
Definition: Dec.hpp:108
double degrees() const noexcept
Definition: Dec.hpp:118
uint8 minutes() const noexcept
Definition: Dec.hpp:148
Dec(Sign inSign, uint8 inDegrees, uint8 inMinutes, double inSeconds) noexcept
Definition: Dec.hpp:91
double radians() const noexcept
Definition: Dec.hpp:128
Definition: Coordinate.hpp:45
Sign
Struct Enum for positive or negative Dec angle.
Definition: Dec.hpp:45
std::string num2str(dtype inNumber)
Definition: num2str.hpp:44
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:48
constexpr auto deg2rad(dtype inValue) noexcept
Definition: deg2rad.hpp:47
auto abs(dtype inValue) noexcept
Definition: abs.hpp:49
dtype floor(dtype inValue) noexcept
Definition: floor.hpp:48
std::uint8_t uint8
Definition: Types.hpp:42