lsst.sphgeom  22.0.1-5-g1e25446+dfc68d4d09
NormalizedAngle.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2014-2015 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #ifndef LSST_SPHGEOM_NORMALIZEDANGLE_H_
24 #define LSST_SPHGEOM_NORMALIZEDANGLE_H_
25 
28 
29 #include <limits>
30 
31 #include "Angle.h"
32 
33 
34 namespace lsst {
35 namespace sphgeom {
36 
37 // Forward declarations
38 class LonLat;
39 class Vector3d;
40 
44 public:
45  static NormalizedAngle nan() {
46  return NormalizedAngle(std::numeric_limits<double>::quiet_NaN());
47  }
48 
49  static NormalizedAngle fromDegrees(double a) {
50  return NormalizedAngle(a * RAD_PER_DEG);
51  }
52 
53  static NormalizedAngle fromRadians(double a) {
54  return NormalizedAngle(a);
55  }
56 
64  static NormalizedAngle between(NormalizedAngle const & a,
65  NormalizedAngle const & b);
66 
69  static NormalizedAngle center(NormalizedAngle const & a,
70  NormalizedAngle const & b);
71 
73  NormalizedAngle() = default;
74 
76  NormalizedAngle(NormalizedAngle const & a) = default;
77 
79  explicit NormalizedAngle(Angle const & a) {
80  *this = NormalizedAngle(a.asRadians());
81  }
82 
85  explicit NormalizedAngle(double a) {
86  // For really large |a|, the error in this reduction can exceed 2*PI
87  // (because PI is only an approximation to π).
88  if (a < 0.0) {
89  _a = Angle(std::fmod(a, 2.0 * PI) + 2.0 * PI);
90  } else if (a > 2 * PI) {
91  _a = Angle(std::fmod(a, 2.0 * PI));
92  } else {
93  _a = Angle(a);
94  }
95  }
96 
99  NormalizedAngle(LonLat const &, LonLat const &);
100 
103  NormalizedAngle(Vector3d const &, Vector3d const &);
104 
108  operator Angle const & () const { return _a; }
109 
110  // Comparison operators
111  bool operator==(Angle const & a) const { return _a == a; }
112  bool operator!=(Angle const & a) const { return _a != a; }
113  bool operator<(Angle const & a) const { return _a < a; }
114  bool operator>(Angle const & a) const { return _a > a; }
115  bool operator<=(Angle const & a) const { return _a <= a; }
116  bool operator>=(Angle const & a) const { return _a >= a; }
117 
118  // Arithmetic operators
119  Angle operator-() const { return Angle(-_a); }
120  Angle operator+(Angle const & a) const { return _a + a; }
121  Angle operator-(Angle const & a) const { return _a - a; }
122  Angle operator*(double a) const { return _a * a; }
123  Angle operator/(double a) const { return _a / a; }
124  double operator/(Angle const & a) const { return _a / a; }
125 
127  double asDegrees() const { return _a.asDegrees(); }
128 
130  double asRadians() const { return _a.asRadians(); }
131 
133  bool isNan() const { return _a.isNan(); }
134 
142  NormalizedAngle x;
143  double d = a.asRadians() - asRadians();
144  x._a = Angle((d < 0.0) ? 2.0 * PI + d : d);
145  return x;
146  }
147 
148 private:
149  Angle _a;
150 };
151 
152 inline NormalizedAngle const & abs(NormalizedAngle const & a) { return a; }
153 
154 }} // namespace lsst::sphgeom
155 
156 #endif // LSST_SPHGEOM_NORMALIZEDANGLE_H_
This file declares a class for representing angles.
Definition: Angle.h:43
bool isNan() const
isNan returns true if the angle value is NaN.
Definition: Angle.h:91
double asDegrees() const
asDegrees returns the value of this angle in units of degrees.
Definition: Angle.h:82
double asRadians() const
asRadians returns the value of this angle in units of radians.
Definition: Angle.h:85
Definition: LonLat.h:48
Definition: NormalizedAngle.h:43
bool isNan() const
isNan returns true if the angle value is NaN.
Definition: NormalizedAngle.h:133
NormalizedAngle(double a)
Definition: NormalizedAngle.h:85
double asRadians() const
asRadians returns the value of this angle in units of radians.
Definition: NormalizedAngle.h:130
NormalizedAngle getAngleTo(NormalizedAngle const &a) const
Definition: NormalizedAngle.h:141
NormalizedAngle(Angle const &a)
This constructor creates a normalized copy of a.
Definition: NormalizedAngle.h:79
NormalizedAngle()=default
This constructor creates a NormalizedAngle with a value of zero.
double asDegrees() const
asDegrees returns the value of this angle in units of degrees.
Definition: NormalizedAngle.h:127
static NormalizedAngle between(NormalizedAngle const &a, NormalizedAngle const &b)
Definition: NormalizedAngle.cc:35
static NormalizedAngle center(NormalizedAngle const &a, NormalizedAngle const &b)
Definition: NormalizedAngle.cc:45
NormalizedAngle(NormalizedAngle const &a)=default
This constructor creates a copy of a.
Vector3d is a vector in ℝ³ with components stored in double precision.
Definition: Vector3d.h:44