NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
Dec.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
31 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/Utils/num2str.hpp"
35 
36 #include <cmath>
37 #include <iostream>
38 #include <string>
39 
40 namespace nc
41 {
42  namespace coordinates
43  {
44  //================================================================================
46  enum class Sign { NEGATIVE = 0, POSITIVE };
47 
48  //================================================================================
50  class Dec
51  {
52  public:
53  //============================================================================
56  Dec() = default;
57 
58  //============================================================================
63  explicit Dec(double inDegrees) :
64  degrees_(inDegrees),
65  radians_(deg2rad(inDegrees))
66  {
67  if (inDegrees < -90 || inDegrees > 90)
68  {
69  THROW_INVALID_ARGUMENT_ERROR("input degrees must be of the range [-90, 90]");
70  }
71 
72  sign_ = degrees_ < 0 ? Sign::NEGATIVE : Sign::POSITIVE;
73  const double absDegrees = std::abs(degrees_);
74  degreesWhole_ = static_cast<uint8>(std::floor(absDegrees));
75 
76  const double decMinutes = (absDegrees - static_cast<double>(degreesWhole_)) * 60.0;
77  minutes_ = static_cast<uint8>(std::floor(decMinutes));
78  seconds_ = (decMinutes - static_cast<double>(minutes_)) * 60.0;
79  }
80 
81  //============================================================================
89  Dec(Sign inSign, uint8 inDegrees, uint8 inMinutes, double inSeconds) noexcept :
90  sign_(inSign),
91  degreesWhole_(inDegrees),
92  minutes_(inMinutes),
93  seconds_(inSeconds)
94  {
95  degrees_ = static_cast<double>(degreesWhole_) + static_cast<double>(minutes_) / 60.0 + seconds_ / 3600.0;
96  degrees_ *= sign_ == Sign::NEGATIVE ? -1 : 1;
97 
98  radians_ = deg2rad(degrees_);
99  }
100 
101  //============================================================================
106  Sign sign() const noexcept
107  {
108  return sign_;
109  }
110 
111  //============================================================================
116  double degrees() const noexcept
117  {
118  return degrees_;
119  }
120 
121  //============================================================================
126  double radians() const noexcept
127  {
128  return radians_;
129  }
130 
131  //============================================================================
136  uint8 degreesWhole() const noexcept
137  {
138  return degreesWhole_;
139  }
140 
141  //============================================================================
146  uint8 minutes() const noexcept
147  {
148  return minutes_;
149  }
150 
151  //============================================================================
156  double seconds() const noexcept
157  {
158  return seconds_;
159  }
160 
161  //============================================================================
166  std::string str() const
167  {
168  std::string strSign = sign_ == Sign::NEGATIVE ? "-" : "+";
169  std::string out = "Dec dms: " + strSign + utils::num2str(degreesWhole_) + " degrees, " + utils::num2str(minutes_) + " minutes, ";
170  out += utils::num2str(seconds_) + " seconds\nDec degrees = " + utils::num2str(degrees_) + '\n';
171  return out;
172  }
173 
174  //============================================================================
177  void print() const
178  {
179  std::cout << *this;
180  }
181 
182  //============================================================================
189  bool operator==(const Dec& inRhs) const noexcept
190  {
191  return utils::essentiallyEqual(degrees_, inRhs.degrees_);
192  }
193 
194  //============================================================================
201  bool operator!=(const Dec& inRhs) const noexcept
202  {
203  return !(*this == inRhs);
204  }
205 
206  //============================================================================
214  friend std::ostream& operator<<(std::ostream& inStream, const Dec& inDec)
215  {
216  inStream << inDec.str();
217  return inStream;
218  }
219 
220  private:
221  //====================================Attributes==============================
222  Sign sign_{ Sign::POSITIVE };
223  uint8 degreesWhole_{ 0 };
224  uint8 minutes_{ 0 };
225  double seconds_{ 0.0 };
226  double degrees_{ 0.0 };
227  double radians_{ 0.0 };
228  };
229  } // namespace coordinates
230 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Holds a Declination object.
Definition: Dec.hpp:51
double seconds() const noexcept
Definition: Dec.hpp:156
bool operator==(const Dec &inRhs) const noexcept
Definition: Dec.hpp:189
bool operator!=(const Dec &inRhs) const noexcept
Definition: Dec.hpp:201
Dec(double inDegrees)
Definition: Dec.hpp:63
friend std::ostream & operator<<(std::ostream &inStream, const Dec &inDec)
Definition: Dec.hpp:214
std::string str() const
Definition: Dec.hpp:166
void print() const
Definition: Dec.hpp:177
uint8 degreesWhole() const noexcept
Definition: Dec.hpp:136
Sign sign() const noexcept
Definition: Dec.hpp:106
double degrees() const noexcept
Definition: Dec.hpp:116
uint8 minutes() const noexcept
Definition: Dec.hpp:146
Dec(Sign inSign, uint8 inDegrees, uint8 inMinutes, double inSeconds) noexcept
Definition: Dec.hpp:89
double radians() const noexcept
Definition: Dec.hpp:126
Sign
Struct Enum for positive or negative Dec angle.
Definition: Dec.hpp:46
std::string num2str(dtype inNumber)
Definition: num2str.hpp:46
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:52
Definition: Coordinate.hpp:45
constexpr auto deg2rad(dtype inValue) noexcept
Definition: deg2rad.hpp:49
auto abs(dtype inValue) noexcept
Definition: abs.hpp:51
dtype floor(dtype inValue) noexcept
Definition: floor.hpp:48
std::uint8_t uint8
Definition: Types.hpp:42