lsst.afw  13.0-39-g3deb52d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Angle.h
Go to the documentation of this file.
1 #if !defined(LSST_AFW_GEOM_ANGLE_H)
2 #define LSST_AFW_GEOM_ANGLE_H
3 
4 #include <limits>
5 #include <iostream>
6 #include <type_traits>
7 
8 #include <cmath>
9 
10 #include "boost/math/constants/constants.hpp"
11 
12 namespace lsst {
13 namespace afw {
14 namespace geom {
15 
16 /*
17  * None of C99, C++98, and C++11 define M_PI, so we'll do it ourselves
18  */
19 #pragma clang diagnostic push
20 #pragma clang diagnostic ignored "-Wunused-variable"
21 double constexpr PI = boost::math::constants::pi<double>();
23 double constexpr TWOPI = boost::math::constants::pi<double>() * 2.0;
24 double constexpr HALFPI = boost::math::constants::pi<double>() * 0.5;
25 double constexpr ONE_OVER_PI = 1.0 / boost::math::constants::pi<double>();
26 // sqrt is not a constexpr on OS X
27 double const SQRTPI = sqrt(boost::math::constants::pi<double>());
28 double const INVSQRTPI = 1.0 / sqrt(boost::math::constants::pi<double>());
29 double constexpr ROOT2 = boost::math::constants::root_two<double>(); // sqrt(2)
30 #pragma clang diagnostic pop
31 
32 // These shouldn't be necessary if the Angle class is used, but sometimes you just need
33 // them. Better to define them once here than have *180/PI throughout the code...
34 inline constexpr double degToRad(double x) noexcept { return x * PI / 180.; }
35 inline constexpr double radToDeg(double x) noexcept { return x * 180. / PI; }
36 inline constexpr double radToArcsec(double x) noexcept { return x * 3600. * 180. / PI; }
37 inline constexpr double radToMas(double x) noexcept { return x * 1000. * 3600. * 180. / PI; }
38 inline constexpr double arcsecToRad(double x) noexcept { return (x / 3600.) * PI / 180.; }
39 inline constexpr double masToRad(double x) noexcept { return (x / (1000. * 3600.)) * PI / 180.; }
40 
41 class Angle;
53 class AngleUnit final {
54  friend class Angle;
55  template <typename T>
56  friend constexpr Angle operator*(T lhs, AngleUnit rhs) noexcept;
57 
58 public:
66  // Current implementation does not throw exceptions, but somebody may want
67  // to add input validation later
68  explicit constexpr AngleUnit(double val) : _val(val) {}
69 
78  constexpr bool operator==(AngleUnit const& rhs) const noexcept;
79 
80 private:
81  double _val;
82 };
83 
84 inline constexpr bool AngleUnit::operator==(AngleUnit const& rhs) const noexcept {
85  return (_val == rhs._val);
86 }
87 
88 AngleUnit constexpr radians = AngleUnit(1.0);
89 AngleUnit constexpr degrees = AngleUnit(PI / 180.0);
90 AngleUnit constexpr hours = AngleUnit(PI * 15.0 / 180.0);
91 AngleUnit constexpr arcminutes = AngleUnit(PI / 60 / 180.0);
92 AngleUnit constexpr arcseconds = AngleUnit(PI / 180.0 / 3600.0);
93 
103 class Angle final {
104  friend class AngleUnit;
105 
106 public:
112  explicit constexpr Angle(double val, AngleUnit units = radians) noexcept : _val(val* units._val) {}
113 
115  constexpr Angle() noexcept : _val(0) {}
116 
118  constexpr Angle(Angle const& other) noexcept = default;
119 
121  constexpr Angle(Angle&& other) noexcept = default;
122 
124  Angle& operator=(Angle const& other) noexcept = default;
125 
127  Angle& operator=(Angle&& other) noexcept = default;
128 
130  constexpr operator double() const noexcept { return _val; }
131 
137  constexpr double asAngularUnits(AngleUnit const& units) const noexcept { return _val / units._val; }
138 
140  constexpr double asRadians() const noexcept { return asAngularUnits(radians); }
141 
143  constexpr double asDegrees() const noexcept { return asAngularUnits(degrees); }
144 
146  constexpr double asHours() const noexcept { return asAngularUnits(hours); }
147 
149  constexpr double asArcminutes() const noexcept { return asAngularUnits(arcminutes); }
150 
152  constexpr double asArcseconds() const noexcept { return asAngularUnits(arcseconds); }
153 
166  Angle wrap() const noexcept;
167 
180  Angle wrapCtr() const noexcept;
181 
195  Angle wrapNear(Angle const& refAng) const noexcept;
196 
205  Angle separation(Angle const& other) const noexcept;
206 
207 #define ANGLE_OPUP_TYPE(OP, TYPE) \
208  Angle& operator OP(TYPE const& d) noexcept { \
209  _val OP d; \
210  return *this; \
211  }
212 
214  ANGLE_OPUP_TYPE(*=, double)
218 
219  ANGLE_OPUP_TYPE(+=, double)
221  ANGLE_OPUP_TYPE(+=, int)
223 
224  ANGLE_OPUP_TYPE(-=, double)
226  ANGLE_OPUP_TYPE(-=, int)
228 
229 #undef ANGLE_OPUP_TYPE
230 
231 #define ANGLE_COMP(OP) \
232  constexpr bool operator OP(const Angle& rhs) const noexcept { return _val OP rhs._val; }
233 
235  ANGLE_COMP(==)
239 
240  ANGLE_COMP(<=)
246 
247 #undef ANGLE_COMP
248 
249 private:
250  double _val;
251 };
252 
253 /*
254  * Operators for Angles.
255  */
256 #define ANGLE_OP(OP) \
257  inline constexpr Angle operator OP(Angle a, Angle d) noexcept { \
258  return Angle(static_cast<double>(a) OP static_cast<double>(d)); \
259  }
260 
261 // We need both int and double versions to avoid ambiguous overloading due to
262 // implicit conversion of Angle to double
263 #define ANGLE_OP_TYPE(OP, TYPE) \
264  inline constexpr Angle operator OP(Angle a, TYPE d) noexcept { \
265  return Angle(static_cast<double>(a) OP d); \
266  } \
267  \
268  inline constexpr Angle operator OP(TYPE d, Angle a) noexcept { \
269  return Angle(d OP static_cast<double>(a)); \
270  }
271 
293 
298 ANGLE_OP_TYPE(*, double)
301 
302 #undef ANGLE_OP
303 #undef ANGLE_OP_TYPE
304 
310 inline constexpr Angle operator-(Angle angle) { return Angle(-static_cast<double>(angle)); }
311 
312 // Apparently @relatesalso doesn't work with grouping
318 inline constexpr Angle operator/(Angle a, int d) noexcept { return Angle(static_cast<double>(a) / d); }
319 
325 inline constexpr Angle operator/(Angle a, double d) noexcept { return Angle(static_cast<double>(a) / d); }
326 
327 // Division is different. Don't allow division by an Angle
328 template <typename T>
329 constexpr double operator/(T const lhs, Angle rhs) noexcept = delete;
330 
345 std::ostream& operator<<(std::ostream& s, Angle a);
346 
348 template <typename T>
349 inline constexpr bool isAngle(T) noexcept {
350  return std::is_base_of<Angle, T>::value;
351 };
352 
363 template <typename T>
364 inline constexpr Angle operator*(T lhs, AngleUnit rhs) noexcept {
365  static_assert(std::is_arithmetic<T>::value,
366  "Only numeric types may be multiplied by an AngleUnit to create an Angle!");
367  return Angle(lhs * rhs._val);
368 }
369 
370 // Inline method definitions, placed last in order to benefit from Angle's full API
371 
372 inline Angle Angle::wrap() const noexcept {
373  double wrapped = std::fmod(_val, TWOPI);
374  // wrapped is in the range (-TWOPI, TWOPI)
375  if (wrapped < 0.0) wrapped += TWOPI;
376  // if wrapped is small enough, adding 2 pi gives 2 pi
377  if (wrapped >= TWOPI) wrapped = 0.0;
378  return wrapped * radians;
379 }
380 
381 inline Angle Angle::wrapCtr() const noexcept {
382  double wrapped = std::fmod(_val, TWOPI);
383  // wrapped is in the range [-TWOPI, TWOPI]
384  if (wrapped < -PI) {
385  wrapped += TWOPI;
386  if (wrapped >= PI) {
387  // handle roundoff error, however unlikely
388  wrapped = -PI;
389  }
390  } else if (wrapped >= PI) {
391  wrapped -= TWOPI;
392  if (wrapped < -PI) {
393  // handle roundoff error, however unlikely
394  wrapped = -PI;
395  }
396  }
397  return wrapped * radians;
398 }
399 
400 inline Angle Angle::wrapNear(Angle const& refAng) const noexcept {
401  // compute (this - refAng).wrapCtr() + refAng
402  // which is correct except for roundoff error at the edges
403  double const refAngRad = refAng.asRadians();
404  double wrapped = (*this - refAng).wrapCtr().asRadians() + refAngRad;
405 
406  // roundoff can cause slightly out-of-range values; fix those
407  if (wrapped - refAngRad >= PI) {
408  wrapped -= TWOPI;
409  }
410  // maximum relative roundoff error for subtraction is 2 epsilon
411  if (wrapped - refAngRad < -PI) {
412  wrapped -= wrapped * 2.0 * std::numeric_limits<double>::epsilon();
413  }
414  return wrapped * radians;
415 }
416 
417 inline Angle Angle::separation(Angle const& other) const noexcept { return (*this - other).wrapCtr(); }
418 }
419 }
420 }
421 #endif // if !defined(LSST_AFW_GEOM_ANGLE_H)
constexpr bool isAngle(T) noexcept
Allow a user to check if they have an angle.
Definition: Angle.h:349
AngleUnit constexpr arcminutes
constant with units of arcminutes
Definition: Angle.h:91
lsst::afw::geom::Angle Angle
Definition: misc.h:36
double constexpr HALFPI
Definition: Angle.h:24
Angle wrapNear(Angle const &refAng) const noexcept
Wrap this angle to a value x such that -π ≤ x - refAng &lt; π.
Definition: Angle.h:400
std::ostream & operator<<(std::ostream &os, lsst::afw::geom::AffineTransform const &transform)
constexpr double masToRad(double x) noexcept
Definition: Angle.h:39
AngleUnit constexpr radians
constant with units of radians
Definition: Angle.h:88
double const INVSQRTPI
Definition: Angle.h:28
T _val
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:89
constexpr double asArcseconds() const noexcept
Return an Angle&#39;s value in arcseconds.
Definition: Angle.h:152
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:53
double constexpr ONE_OVER_PI
Definition: Angle.h:25
#define ANGLE_COMP(OP)
Definition: Angle.h:231
double constexpr PI
The ratio of a circle&#39;s circumference to diameter.
Definition: Angle.h:22
constexpr Angle(double val, AngleUnit units=radians) noexcept
Construct an Angle with the specified value (interpreted in the given units).
Definition: Angle.h:112
constexpr double asArcminutes() const noexcept
Return an Angle&#39;s value in arcminutes.
Definition: Angle.h:149
A class representing an angle.
Definition: Angle.h:103
constexpr Angle operator/(Angle a, int d) noexcept
Ratio of an angle and a scalar.
Definition: Angle.h:318
Angle wrap() const noexcept
Wrap this angle to the range [0, 2π).
Definition: Angle.h:372
constexpr AngleUnit(double val)
Define a new angle unit.
Definition: Angle.h:68
constexpr bool operator==(AngleUnit const &rhs) const noexcept
Test if two units are the same.
Definition: Angle.h:84
double constexpr ROOT2
Definition: Angle.h:29
Angle wrapCtr() const noexcept
Wrap this angle to the range [-π, π).
Definition: Angle.h:381
constexpr double asAngularUnits(AngleUnit const &units) const noexcept
Return an Angle&#39;s value in the specified units.
Definition: Angle.h:137
AngleUnit constexpr hours
constant with units of hours
Definition: Angle.h:90
double x
#define ANGLE_OP(OP)
Definition: Angle.h:256
constexpr double degToRad(double x) noexcept
Definition: Angle.h:34
constexpr Angle() noexcept
Construct the zero angle.
Definition: Angle.h:115
AngleUnit constexpr arcseconds
constant with units of arcseconds
Definition: Angle.h:92
constexpr double radToArcsec(double x) noexcept
Definition: Angle.h:36
constexpr Angle operator*(Angle a, Angle d) noexcept
Product of two angles.
Definition: Angle.h:291
constexpr Angle operator-(Angle a, Angle d) noexcept
Difference of two angles.
Definition: Angle.h:283
constexpr double asRadians() const noexcept
Return an Angle&#39;s value in radians.
Definition: Angle.h:140
constexpr double radToDeg(double x) noexcept
Definition: Angle.h:35
Angle separation(Angle const &other) const noexcept
The signed difference between two Angles.
Definition: Angle.h:417
constexpr double arcsecToRad(double x) noexcept
Definition: Angle.h:38
constexpr double asDegrees() const noexcept
Return an Angle&#39;s value in degrees.
Definition: Angle.h:143
constexpr double radToMas(double x) noexcept
Definition: Angle.h:37
#define ANGLE_OPUP_TYPE(OP, TYPE)
Definition: Angle.h:207
double const SQRTPI
Definition: Angle.h:27
double constexpr TWOPI
Definition: Angle.h:23
#define ANGLE_OP_TYPE(OP, TYPE)
Definition: Angle.h:263
constexpr double asHours() const noexcept
Return an Angle&#39;s value in hours.
Definition: Angle.h:146
friend constexpr Angle operator*(T lhs, AngleUnit rhs) noexcept
Use AngleUnit to convert a POD (e.g. int, double) to an Angle; e.g. 180*degrees.
Definition: Angle.h:364