lsst.afw  13.0-100-ge0459d59f+2
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 <iostream>
5 #include <type_traits>
6 
7 #include <cmath>
8 
9 #include "boost/math/constants/constants.hpp"
10 
11 namespace lsst {
12 namespace afw {
13 namespace geom {
14 
15 /*
16  * None of C99, C++98, and C++11 define M_PI, so we'll do it ourselves
17  */
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wunused-variable"
20 double constexpr PI = boost::math::constants::pi<double>();
22 double constexpr TWOPI = boost::math::constants::pi<double>() * 2.0;
23 double constexpr HALFPI = boost::math::constants::pi<double>() * 0.5;
24 double constexpr ONE_OVER_PI = 1.0 / boost::math::constants::pi<double>();
25 // sqrt is not a constexpr on OS X
26 double const SQRTPI = sqrt(boost::math::constants::pi<double>());
27 double const INVSQRTPI = 1.0 / sqrt(boost::math::constants::pi<double>());
28 double constexpr ROOT2 = boost::math::constants::root_two<double>(); // sqrt(2)
29 #pragma clang diagnostic pop
30 
31 // These shouldn't be necessary if the Angle class is used, but sometimes you just need
32 // them. Better to define them once here than have *180/PI throughout the code...
33 inline constexpr double degToRad(double x) noexcept { return x * PI / 180.; }
34 inline constexpr double radToDeg(double x) noexcept { return x * 180. / PI; }
35 inline constexpr double radToArcsec(double x) noexcept { return x * 3600. * 180. / PI; }
36 inline constexpr double radToMas(double x) noexcept { return x * 1000. * 3600. * 180. / PI; }
37 inline constexpr double arcsecToRad(double x) noexcept { return (x / 3600.) * PI / 180.; }
38 inline constexpr double masToRad(double x) noexcept { return (x / (1000. * 3600.)) * PI / 180.; }
39 
40 class Angle;
52 class AngleUnit final {
53  friend class Angle;
54  template <typename T>
55  friend constexpr Angle operator*(T lhs, AngleUnit rhs) noexcept;
56 
57 public:
65  // Current implementation does not throw exceptions, but somebody may want
66  // to add input validation later
67  explicit constexpr AngleUnit(double val) : _val(val) {}
68 
77  constexpr bool operator==(AngleUnit const& rhs) const noexcept;
78 
79 private:
80  double _val;
81 };
82 
83 inline constexpr bool AngleUnit::operator==(AngleUnit const& rhs) const noexcept {
84  return (_val == rhs._val);
85 }
86 
87 AngleUnit constexpr radians = AngleUnit(1.0);
88 AngleUnit constexpr degrees = AngleUnit(PI / 180.0);
89 AngleUnit constexpr hours = AngleUnit(PI * 15.0 / 180.0);
90 AngleUnit constexpr arcminutes = AngleUnit(PI / 60 / 180.0);
91 AngleUnit constexpr arcseconds = AngleUnit(PI / 180.0 / 3600.0);
92 
102 class Angle final {
103  friend class AngleUnit;
104 
105 public:
111  explicit constexpr Angle(double val, AngleUnit units = radians) noexcept : _val(val* units._val) {}
112 
114  constexpr Angle() noexcept : _val(0) {}
115 
117  constexpr Angle(Angle const& other) noexcept = default;
118 
120  constexpr Angle(Angle&& other) noexcept = default;
121 
123  Angle& operator=(Angle const& other) noexcept = default;
124 
126  Angle& operator=(Angle&& other) noexcept = default;
127 
129  constexpr operator double() const noexcept { return _val; }
130 
136  constexpr double asAngularUnits(AngleUnit const& units) const noexcept { return _val / units._val; }
137 
139  constexpr double asRadians() const noexcept { return asAngularUnits(radians); }
140 
142  constexpr double asDegrees() const noexcept { return asAngularUnits(degrees); }
143 
145  constexpr double asHours() const noexcept { return asAngularUnits(hours); }
146 
148  constexpr double asArcminutes() const noexcept { return asAngularUnits(arcminutes); }
149 
151  constexpr double asArcseconds() const noexcept { return asAngularUnits(arcseconds); }
152 
165  Angle wrap() const noexcept;
166 
179  Angle wrapCtr() const noexcept;
180 
193  Angle wrapNear(Angle const& refAng) const noexcept;
194 
203  Angle separation(Angle const& other) const noexcept;
204 
205 #define ANGLE_OPUP_TYPE(OP, TYPE) \
206  Angle& operator OP(TYPE const& d) noexcept { \
207  _val OP d; \
208  return *this; \
209  }
210 
212  ANGLE_OPUP_TYPE(*=, double)
216 
217  ANGLE_OPUP_TYPE(+=, double)
219  ANGLE_OPUP_TYPE(+=, int)
221 
222  ANGLE_OPUP_TYPE(-=, double)
224  ANGLE_OPUP_TYPE(-=, int)
226 
227 #undef ANGLE_OPUP_TYPE
228 
229 #define ANGLE_COMP(OP) \
230  constexpr bool operator OP(const Angle& rhs) const noexcept { return _val OP rhs._val; }
231 
233  ANGLE_COMP(==)
237 
238  ANGLE_COMP(<=)
244 
245 #undef ANGLE_COMP
246 
247 private:
248  double _val;
249 };
250 
251 /*
252  * Operators for Angles.
253  */
254 #define ANGLE_OP(OP) \
255  inline constexpr Angle operator OP(Angle a, Angle d) noexcept { \
256  return Angle(static_cast<double>(a) OP static_cast<double>(d)); \
257  }
258 
259 // We need both int and double versions to avoid ambiguous overloading due to
260 // implicit conversion of Angle to double
261 #define ANGLE_OP_TYPE(OP, TYPE) \
262  inline constexpr Angle operator OP(Angle a, TYPE d) noexcept { \
263  return Angle(static_cast<double>(a) OP d); \
264  } \
265  \
266  inline constexpr Angle operator OP(TYPE d, Angle a) noexcept { \
267  return Angle(d OP static_cast<double>(a)); \
268  }
269 
291 
296 ANGLE_OP_TYPE(*, double)
299 
300 #undef ANGLE_OP
301 #undef ANGLE_OP_TYPE
302 
308 inline constexpr Angle operator-(Angle angle) { return Angle(-static_cast<double>(angle)); }
309 
310 // Apparently @relatesalso doesn't work with grouping
316 inline constexpr Angle operator/(Angle a, int d) noexcept { return Angle(static_cast<double>(a) / d); }
317 
323 inline constexpr Angle operator/(Angle a, double d) noexcept { return Angle(static_cast<double>(a) / d); }
324 
325 // Division is different. Don't allow division by an Angle
326 template <typename T>
327 constexpr double operator/(T const lhs, Angle rhs) noexcept = delete;
328 
343 std::ostream& operator<<(std::ostream& s, Angle a);
344 
346 template <typename T>
347 inline constexpr bool isAngle(T) noexcept {
348  return std::is_base_of<Angle, T>::value;
349 };
350 
361 template <typename T>
362 inline constexpr Angle operator*(T lhs, AngleUnit rhs) noexcept {
363  static_assert(std::is_arithmetic<T>::value,
364  "Only numeric types may be multiplied by an AngleUnit to create an Angle!");
365  return Angle(lhs * rhs._val);
366 }
367 
368 // Inline method definitions, placed last in order to benefit from Angle's full API
369 
370 inline Angle Angle::wrap() const noexcept {
371  double wrapped = std::fmod(_val, TWOPI);
372  // wrapped is in the range (-TWOPI, TWOPI)
373  if (wrapped < 0.0) wrapped += TWOPI;
374  // if wrapped is small enough, adding 2 pi gives 2 pi
375  if (wrapped >= TWOPI) wrapped = 0.0;
376  return wrapped * radians;
377 }
378 
379 inline Angle Angle::wrapCtr() const noexcept {
380  double wrapped = std::fmod(_val, TWOPI);
381  // wrapped is in the range [-TWOPI, TWOPI]
382  if (wrapped < -PI) {
383  wrapped += TWOPI;
384  if (wrapped >= PI) {
385  // handle roundoff error, however unlikely
386  wrapped = -PI;
387  }
388  } else if (wrapped >= PI) {
389  wrapped -= TWOPI;
390  if (wrapped < -PI) {
391  // handle roundoff error, however unlikely
392  wrapped = -PI;
393  }
394  }
395  return wrapped * radians;
396 }
397 
398 inline Angle Angle::wrapNear(Angle const& refAng) const noexcept {
399  // compute (this - refAng).wrapCtr() + refAng
400  // which is correct except for roundoff error at the edges
401  double const refAngRad = refAng.asRadians();
402  double wrapped = (*this - refAng).wrapCtr().asRadians() + refAngRad;
403 
404  // roundoff can cause slightly out-of-range values; fix those as bast we can;
405  // note that both conditionals are wanted, since either or both may execute
406  // (though if/else could be used if the lower limit was squishy for radians)
407  if (wrapped - refAngRad >= PI) {
408  wrapped -= TWOPI;
409  }
410  if (wrapped - refAngRad < -PI) {
411  wrapped += TWOPI;
412  }
413  return wrapped * radians;
414 }
415 
416 inline Angle Angle::separation(Angle const& other) const noexcept { return (*this - other).wrapCtr(); }
417 }
418 }
419 }
420 #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:347
AngleUnit constexpr arcminutes
constant with units of arcminutes
Definition: Angle.h:90
friend class Angle
Definition: Angle.h:53
double constexpr HALFPI
Definition: Angle.h:23
Angle wrapNear(Angle const &refAng) const noexcept
Wrap this angle to a value x such that -π ≤ x - refAng ≤ π, approximately.
Definition: Angle.h:398
std::ostream & operator<<(std::ostream &os, lsst::afw::geom::AffineTransform const &transform)
constexpr double masToRad(double x) noexcept
Definition: Angle.h:38
AngleUnit constexpr radians
constant with units of radians
Definition: Angle.h:87
double const INVSQRTPI
Definition: Angle.h:27
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:88
constexpr double asArcseconds() const noexcept
Return an Angle&#39;s value in arcseconds.
Definition: Angle.h:151
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:52
double constexpr ONE_OVER_PI
Definition: Angle.h:24
#define ANGLE_COMP(OP)
Definition: Angle.h:229
double constexpr PI
The ratio of a circle&#39;s circumference to diameter.
Definition: Angle.h:21
constexpr Angle(double val, AngleUnit units=radians) noexcept
Construct an Angle with the specified value (interpreted in the given units).
Definition: Angle.h:111
constexpr double asArcminutes() const noexcept
Return an Angle&#39;s value in arcminutes.
Definition: Angle.h:148
A class representing an angle.
Definition: Angle.h:102
constexpr Angle operator/(Angle a, int d) noexcept
Ratio of an angle and a scalar.
Definition: Angle.h:316
A base class for image defects.
Definition: cameraGeom.dox:3
Angle wrap() const noexcept
Wrap this angle to the range [0, 2π).
Definition: Angle.h:370
constexpr AngleUnit(double val)
Define a new angle unit.
Definition: Angle.h:67
constexpr bool operator==(AngleUnit const &rhs) const noexcept
Test if two units are the same.
Definition: Angle.h:83
double constexpr ROOT2
Definition: Angle.h:28
Angle wrapCtr() const noexcept
Wrap this angle to the range [-π, π).
Definition: Angle.h:379
double x
constexpr double asAngularUnits(AngleUnit const &units) const noexcept
Return an Angle&#39;s value in the specified units.
Definition: Angle.h:136
AngleUnit constexpr hours
constant with units of hours
Definition: Angle.h:89
#define ANGLE_OP(OP)
Definition: Angle.h:254
constexpr double degToRad(double x) noexcept
Definition: Angle.h:33
constexpr Angle() noexcept
Construct the zero angle.
Definition: Angle.h:114
AngleUnit constexpr arcseconds
constant with units of arcseconds
Definition: Angle.h:91
constexpr double radToArcsec(double x) noexcept
Definition: Angle.h:35
constexpr Angle operator-(Angle a, Angle d) noexcept
Difference of two angles.
Definition: Angle.h:281
constexpr double asRadians() const noexcept
Return an Angle&#39;s value in radians.
Definition: Angle.h:139
constexpr double radToDeg(double x) noexcept
Definition: Angle.h:34
Angle separation(Angle const &other) const noexcept
The signed difference between two Angles.
Definition: Angle.h:416
constexpr double arcsecToRad(double x) noexcept
Definition: Angle.h:37
constexpr double asDegrees() const noexcept
Return an Angle&#39;s value in degrees.
Definition: Angle.h:142
constexpr double radToMas(double x) noexcept
Definition: Angle.h:36
#define ANGLE_OPUP_TYPE(OP, TYPE)
Definition: Angle.h:205
double const SQRTPI
Definition: Angle.h:26
double constexpr TWOPI
Definition: Angle.h:22
#define ANGLE_OP_TYPE(OP, TYPE)
Definition: Angle.h:261
constexpr double asHours() const noexcept
Return an Angle&#39;s value in hours.
Definition: Angle.h:145
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:362