NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Coordinate.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <cmath>
31 #include <iostream>
32 #include <string>
33 
37 #include "NumCpp/Core/Types.hpp"
39 #include "NumCpp/Functions/dot.hpp"
41 #include "NumCpp/NdArray.hpp"
42 #include "NumCpp/Utils/sqr.hpp"
43 
44 namespace nc
45 {
46  namespace coordinates
47  {
48  //================================================================================
50  class Coordinate
51  {
52  public:
53  //============================================================================
56  Coordinate() = default;
57 
58  //============================================================================
64  Coordinate(double inRaDegrees, double inDecDegrees) :
65  ra_(inRaDegrees),
66  dec_(inDecDegrees)
67  {
68  polarToCartesian();
69  }
70 
71  //============================================================================
82  Coordinate(uint8 inRaHours,
83  uint8 inRaMinutes,
84  double inRaSeconds,
85  Sign inSign,
86  uint8 inDecDegreesWhole,
87  uint8 inDecMinutes,
88  double inDecSeconds) :
89  ra_(inRaHours, inRaMinutes, inRaSeconds),
90  dec_(inSign, inDecDegreesWhole, inDecMinutes, inDecSeconds)
91  {
92  polarToCartesian();
93  }
94 
95  //============================================================================
101  Coordinate(const RA& inRA, const Dec& inDec) noexcept :
102  ra_(inRA),
103  dec_(inDec)
104  {
105  polarToCartesian();
106  }
107 
108  //============================================================================
115  Coordinate(double inX, double inY, double inZ) noexcept :
116  x_(inX),
117  y_(inY),
118  z_(inZ)
119  {
120  cartesianToPolar();
121  }
122 
123  //============================================================================
128  Coordinate(const NdArray<double>& inCartesianVector)
129  {
130  if (inCartesianVector.size() != 3)
131  {
132  THROW_INVALID_ARGUMENT_ERROR("NdArray input must be of length 3.");
133  }
134 
135  x_ = inCartesianVector[0];
136  y_ = inCartesianVector[1];
137  z_ = inCartesianVector[2];
138 
139  cartesianToPolar();
140  }
141 
142  //============================================================================
147  const Dec& dec() const noexcept
148  {
149  return dec_;
150  }
151 
152  //============================================================================
157  const RA& ra() const noexcept
158  {
159  return ra_;
160  }
161 
162  //============================================================================
167  double x() const noexcept
168  {
169  return x_;
170  }
171 
172  //============================================================================
177  double y() const noexcept
178  {
179  return y_;
180  }
181 
182  //============================================================================
187  double z() const noexcept
188  {
189  return z_;
190  }
191 
192  //============================================================================
198  {
199  NdArray<double> out = { x_, y_, z_ };
200  return out;
201  }
202 
203  //============================================================================
210  double degreeSeperation(const Coordinate& inOtherCoordinate) const
211  {
212  return rad2deg(radianSeperation(inOtherCoordinate));
213  }
214 
215  //============================================================================
223  double degreeSeperation(const NdArray<double>& inVector) const
224  {
225  return rad2deg(radianSeperation(inVector));
226  }
227 
228  //============================================================================
235  double radianSeperation(const Coordinate& inOtherCoordinate) const
236  {
237  return std::acos(dot(xyz(), inOtherCoordinate.xyz()).item());
238  }
239 
240  //============================================================================
248  double radianSeperation(const NdArray<double>& inVector) const
249  {
250  if (inVector.size() != 3)
251  {
252  THROW_INVALID_ARGUMENT_ERROR("input vector must be of length 3.");
253  }
254 
255  return std::acos(dot(xyz(), inVector.flatten()).item());
256  }
257 
258  //============================================================================
263  std::string str() const
264  {
265  std::string returnStr;
266  returnStr = ra_.str();
267  returnStr += dec_.str();
268  returnStr += "Cartesian = " + xyz().str();
269  return returnStr;
270  }
271 
272  //============================================================================
275  void print() const
276  {
277  std::cout << *this;
278  }
279 
280  //============================================================================
287  bool operator==(const Coordinate& inRhs) const noexcept
288  {
289  return ra_ == inRhs.ra_ && dec_ == inRhs.dec_;
290  }
291 
292  //============================================================================
299  bool operator!=(const Coordinate& inRhs) const noexcept
300  {
301  return !(*this == inRhs);
302  }
303 
304  //============================================================================
312  friend std::ostream& operator<<(std::ostream& inStream, const Coordinate& inCoord)
313  {
314  inStream << inCoord.str();
315  return inStream;
316  }
317 
318  private:
319  //====================================Attributes==============================
320  RA ra_{};
321  Dec dec_{};
322  double x_{ 1.0 };
323  double y_{ 0.0 };
324  double z_{ 0.0 };
325 
326  //============================================================================
329  void cartesianToPolar() noexcept
330  {
331  double degreesRa = rad2deg(std::atan2(y_, x_));
332  if (degreesRa < 0)
333  {
334  degreesRa += 360;
335  }
336  ra_ = RA(degreesRa);
337 
338  const double r = std::sqrt(utils::sqr(x_) + utils::sqr(y_) + utils::sqr(z_));
339  const double degreesDec = rad2deg(std::asin(z_ / r));
340  dec_ = Dec(degreesDec);
341  }
342 
343  //============================================================================
346  void polarToCartesian() noexcept
347  {
348  const double raRadians = deg2rad(ra_.degrees());
349  const double decRadians = deg2rad(dec_.degrees());
350 
351  x_ = std::cos(raRadians) * std::cos(decRadians);
352  y_ = std::sin(raRadians) * std::cos(decRadians);
353  z_ = std::sin(decRadians);
354  }
355  };
356  } // namespace coordinates
357 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
size_type size() const noexcept
Definition: NdArrayCore.hpp:4289
NdArray< dtype > flatten() const
Definition: NdArrayCore.hpp:2751
std::string str() const
Definition: NdArrayCore.hpp:4346
Holds a full coordinate object.
Definition: Coordinate.hpp:51
NdArray< double > xyz() const
Definition: Coordinate.hpp:197
std::string str() const
Definition: Coordinate.hpp:263
double radianSeperation(const Coordinate &inOtherCoordinate) const
Definition: Coordinate.hpp:235
double z() const noexcept
Definition: Coordinate.hpp:187
double degreeSeperation(const NdArray< double > &inVector) const
Definition: Coordinate.hpp:223
Coordinate(const NdArray< double > &inCartesianVector)
Definition: Coordinate.hpp:128
double y() const noexcept
Definition: Coordinate.hpp:177
Coordinate(uint8 inRaHours, uint8 inRaMinutes, double inRaSeconds, Sign inSign, uint8 inDecDegreesWhole, uint8 inDecMinutes, double inDecSeconds)
Definition: Coordinate.hpp:82
Coordinate(const RA &inRA, const Dec &inDec) noexcept
Definition: Coordinate.hpp:101
bool operator!=(const Coordinate &inRhs) const noexcept
Definition: Coordinate.hpp:299
bool operator==(const Coordinate &inRhs) const noexcept
Definition: Coordinate.hpp:287
Coordinate(double inRaDegrees, double inDecDegrees)
Definition: Coordinate.hpp:64
double degreeSeperation(const Coordinate &inOtherCoordinate) const
Definition: Coordinate.hpp:210
Coordinate(double inX, double inY, double inZ) noexcept
Definition: Coordinate.hpp:115
friend std::ostream & operator<<(std::ostream &inStream, const Coordinate &inCoord)
Definition: Coordinate.hpp:312
const Dec & dec() const noexcept
Definition: Coordinate.hpp:147
const RA & ra() const noexcept
Definition: Coordinate.hpp:157
double x() const noexcept
Definition: Coordinate.hpp:167
double radianSeperation(const NdArray< double > &inVector) const
Definition: Coordinate.hpp:248
void print() const
Definition: Coordinate.hpp:275
Holds a Declination object.
Definition: Dec.hpp:55
std::string str() const
Definition: Dec.hpp:171
double degrees() const noexcept
Definition: Dec.hpp:121
Holds a right ascension object.
Definition: RA.hpp:47
std::string str() const
Definition: RA.hpp:146
double degrees() const noexcept
Definition: RA.hpp:106
Sign
Struct Enum for positive or negative Dec angle.
Definition: Dec.hpp:47
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:44
Definition: Coordinate.hpp:45
NdArray< dtype > dot(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition: dot.hpp:47
constexpr auto deg2rad(dtype inValue) noexcept
Definition: deg2rad.hpp:47
auto sin(dtype inValue) noexcept
Definition: sin.hpp:49
auto cos(dtype inValue) noexcept
Definition: cos.hpp:49
constexpr auto rad2deg(dtype inValue) noexcept
Definition: rad2deg.hpp:48
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48
std::uint8_t uint8
Definition: Types.hpp:42