NumCpp  2.9.0
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
41 {
42  namespace coordinates
43  {
44  //================================================================================
46  enum class Sign
47  {
48  NEGATIVE = 0,
49  POSITIVE
50  };
51 
52  //================================================================================
54  class Dec
55  {
56  public:
57  //============================================================================
60  Dec() = default;
61 
62  //============================================================================
67  explicit Dec(double inDegrees) :
68  degrees_(inDegrees),
69  radians_(deg2rad(inDegrees))
70  {
71  if (inDegrees < -90 || inDegrees > 90)
72  {
73  THROW_INVALID_ARGUMENT_ERROR("input degrees must be of the range [-90, 90]");
74  }
75 
76  sign_ = degrees_ < 0 ? Sign::NEGATIVE : Sign::POSITIVE;
77  const double absDegrees = std::abs(degrees_);
78  degreesWhole_ = static_cast<uint8>(std::floor(absDegrees));
79 
80  const double decMinutes = (absDegrees - static_cast<double>(degreesWhole_)) * 60.;
81  minutes_ = static_cast<uint8>(std::floor(decMinutes));
82  seconds_ = (decMinutes - static_cast<double>(minutes_)) * 60.;
83  }
84 
85  //============================================================================
93  Dec(Sign inSign, uint8 inDegrees, uint8 inMinutes, double inSeconds) noexcept :
94  sign_(inSign),
95  degreesWhole_(inDegrees),
96  minutes_(inMinutes),
97  seconds_(inSeconds)
98  {
99  degrees_ = static_cast<double>(degreesWhole_) + static_cast<double>(minutes_) / 60. + seconds_ / 3600.;
100  degrees_ *= sign_ == Sign::NEGATIVE ? -1 : 1;
101 
102  radians_ = deg2rad(degrees_);
103  }
104 
105  //============================================================================
110  Sign sign() const noexcept
111  {
112  return sign_;
113  }
114 
115  //============================================================================
120  double degrees() const noexcept
121  {
122  return degrees_;
123  }
124 
125  //============================================================================
130  double radians() const noexcept
131  {
132  return radians_;
133  }
134 
135  //============================================================================
140  uint8 degreesWhole() const noexcept
141  {
142  return degreesWhole_;
143  }
144 
145  //============================================================================
150  uint8 minutes() const noexcept
151  {
152  return minutes_;
153  }
154 
155  //============================================================================
160  double seconds() const noexcept
161  {
162  return seconds_;
163  }
164 
165  //============================================================================
170  std::string str() const
171  {
172  std::string strSign = sign_ == Sign::NEGATIVE ? "-" : "+";
173  std::string out = "Dec dms: " + strSign + utils::num2str(degreesWhole_) + " degrees, " +
174  utils::num2str(minutes_) + " minutes, ";
175  out += utils::num2str(seconds_) + " seconds\nDec degrees = " + utils::num2str(degrees_) + '\n';
176  return out;
177  }
178 
179  //============================================================================
182  void print() const
183  {
184  std::cout << *this;
185  }
186 
187  //============================================================================
194  bool operator==(const Dec& inRhs) const noexcept
195  {
196  return utils::essentiallyEqual(degrees_, inRhs.degrees_);
197  }
198 
199  //============================================================================
206  bool operator!=(const Dec& inRhs) const noexcept
207  {
208  return !(*this == inRhs);
209  }
210 
211  //============================================================================
219  friend std::ostream& operator<<(std::ostream& inStream, const Dec& inDec)
220  {
221  inStream << inDec.str();
222  return inStream;
223  }
224 
225  private:
226  //====================================Attributes==============================
227  Sign sign_{ Sign::POSITIVE };
228  uint8 degreesWhole_{ 0 };
229  uint8 minutes_{ 0 };
230  double seconds_{ 0. };
231  double degrees_{ 0. };
232  double radians_{ 0. };
233  };
234  } // namespace coordinates
235 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Holds a Declination object.
Definition: Dec.hpp:55
double seconds() const noexcept
Definition: Dec.hpp:160
bool operator==(const Dec &inRhs) const noexcept
Definition: Dec.hpp:194
bool operator!=(const Dec &inRhs) const noexcept
Definition: Dec.hpp:206
Dec(double inDegrees)
Definition: Dec.hpp:67
friend std::ostream & operator<<(std::ostream &inStream, const Dec &inDec)
Definition: Dec.hpp:219
std::string str() const
Definition: Dec.hpp:170
void print() const
Definition: Dec.hpp:182
uint8 degreesWhole() const noexcept
Definition: Dec.hpp:140
Sign sign() const noexcept
Definition: Dec.hpp:110
double degrees() const noexcept
Definition: Dec.hpp:120
uint8 minutes() const noexcept
Definition: Dec.hpp:150
Dec(Sign inSign, uint8 inDegrees, uint8 inMinutes, double inSeconds) noexcept
Definition: Dec.hpp:93
double radians() const noexcept
Definition: Dec.hpp:130
Sign
Struct Enum for positive or negative Dec angle.
Definition: Dec.hpp:47
std::string num2str(dtype inNumber)
Definition: num2str.hpp:46
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:51
Definition: Coordinate.hpp:45
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