NumCpp  2.8.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.0;
81  minutes_ = static_cast<uint8>(std::floor(decMinutes));
82  seconds_ = (decMinutes - static_cast<double>(minutes_)) * 60.0;
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_ =
100  static_cast<double>(degreesWhole_) + static_cast<double>(minutes_) / 60.0 + seconds_ / 3600.0;
101  degrees_ *= sign_ == Sign::NEGATIVE ? -1 : 1;
102 
103  radians_ = deg2rad(degrees_);
104  }
105 
106  //============================================================================
111  Sign sign() const noexcept
112  {
113  return sign_;
114  }
115 
116  //============================================================================
121  double degrees() const noexcept
122  {
123  return degrees_;
124  }
125 
126  //============================================================================
131  double radians() const noexcept
132  {
133  return radians_;
134  }
135 
136  //============================================================================
141  uint8 degreesWhole() const noexcept
142  {
143  return degreesWhole_;
144  }
145 
146  //============================================================================
151  uint8 minutes() const noexcept
152  {
153  return minutes_;
154  }
155 
156  //============================================================================
161  double seconds() const noexcept
162  {
163  return seconds_;
164  }
165 
166  //============================================================================
171  std::string str() const
172  {
173  std::string strSign = sign_ == Sign::NEGATIVE ? "-" : "+";
174  std::string out = "Dec dms: " + strSign + utils::num2str(degreesWhole_) + " degrees, " +
175  utils::num2str(minutes_) + " minutes, ";
176  out += utils::num2str(seconds_) + " seconds\nDec degrees = " + utils::num2str(degrees_) + '\n';
177  return out;
178  }
179 
180  //============================================================================
183  void print() const
184  {
185  std::cout << *this;
186  }
187 
188  //============================================================================
195  bool operator==(const Dec& inRhs) const noexcept
196  {
197  return utils::essentiallyEqual(degrees_, inRhs.degrees_);
198  }
199 
200  //============================================================================
207  bool operator!=(const Dec& inRhs) const noexcept
208  {
209  return !(*this == inRhs);
210  }
211 
212  //============================================================================
220  friend std::ostream& operator<<(std::ostream& inStream, const Dec& inDec)
221  {
222  inStream << inDec.str();
223  return inStream;
224  }
225 
226  private:
227  //====================================Attributes==============================
228  Sign sign_{ Sign::POSITIVE };
229  uint8 degreesWhole_{ 0 };
230  uint8 minutes_{ 0 };
231  double seconds_{ 0.0 };
232  double degrees_{ 0.0 };
233  double radians_{ 0.0 };
234  };
235  } // namespace coordinates
236 } // 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:161
bool operator==(const Dec &inRhs) const noexcept
Definition: Dec.hpp:195
bool operator!=(const Dec &inRhs) const noexcept
Definition: Dec.hpp:207
Dec(double inDegrees)
Definition: Dec.hpp:67
friend std::ostream & operator<<(std::ostream &inStream, const Dec &inDec)
Definition: Dec.hpp:220
std::string str() const
Definition: Dec.hpp:171
void print() const
Definition: Dec.hpp:183
uint8 degreesWhole() const noexcept
Definition: Dec.hpp:141
Sign sign() const noexcept
Definition: Dec.hpp:111
double degrees() const noexcept
Definition: Dec.hpp:121
uint8 minutes() const noexcept
Definition: Dec.hpp:151
Dec(Sign inSign, uint8 inDegrees, uint8 inMinutes, double inSeconds) noexcept
Definition: Dec.hpp:93
double radians() const noexcept
Definition: Dec.hpp:131
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