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