Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
angle.h
Go to the documentation of this file.
1 
18 #ifndef ION_MATH_ANGLE_H_
19 #define ION_MATH_ANGLE_H_
20 
21 #include <cmath>
22 #include <istream> // NOLINT
23 #include <ostream> // NOLINT
24 
25 #include "ion/base/stringutils.h"
26 
27 namespace ion {
28 namespace math {
29 
32 template <typename T>
33 class Angle {
34  public:
36  Angle() : radians_(0) {}
37 
40  template <typename U>
41  explicit Angle(const Angle<U> other)
42  : radians_(static_cast<T>(other.Radians())) {}
43 
45  static Angle FromRadians(const T& angle) {
46  return Angle(angle);
47  }
48 
50  static Angle FromDegrees(const T& angle) {
51  return Angle(DegreesToRadians(angle));
52  }
53 
55  T Radians() const { return radians_; }
56  T Degrees() const { return RadiansToDegrees(radians_); }
57 
59 
61  const Angle operator-() const { return Angle::FromRadians(-radians_); }
62 
64  void operator+=(const Angle& a) { radians_ += a.radians_; }
65  void operator-=(const Angle& a) { radians_ -= a.radians_; }
66  void operator*=(T s) { radians_ *= s; }
67  void operator/=(T s) { radians_ /= s; }
68 
70  friend const Angle operator+(const Angle& a0, const Angle& a1) {
71  return FromRadians(a0.radians_ + a1.radians_);
72  }
73  friend const Angle operator-(const Angle& a0, const Angle& a1) {
74  return FromRadians(a0.radians_ - a1.radians_);
75  }
76  friend const Angle operator*(const Angle& a, T s) {
77  return FromRadians(a.radians_ * s);
78  }
79  friend const Angle operator*(T s, const Angle& a) {
80  return FromRadians(s * a.radians_);
81  }
82  friend const Angle operator/(const Angle& a, T s) {
83  return FromRadians(a.radians_ / s);
84  }
85 
87  friend bool operator==(const Angle& a0, const Angle& a1) {
88  return a0.radians_ == a1.radians_;
89  }
90  friend bool operator!=(const Angle& a0, const Angle& a1) {
91  return a0.radians_ != a1.radians_;
92  }
93 
95  friend bool operator<(const Angle& a0, const Angle& a1) {
96  return a0.radians_ < a1.radians_;
97  }
98  friend bool operator>(const Angle& a0, const Angle& a1) {
99  return a0.radians_ > a1.radians_;
100  }
101  friend bool operator<=(const Angle& a0, const Angle& a1) {
102  return a0.radians_ <= a1.radians_;
103  }
104  friend bool operator>=(const Angle& a0, const Angle& a1) {
105  return a0.radians_ >= a1.radians_;
106  }
107 
108  private:
109  explicit Angle(const T angle_rad) {
110  radians_ = angle_rad;
111  }
112 
113  static T RadiansToDegrees(const T& radians) {
114  static const T kRadToDeg = 180 / static_cast<T>(M_PI);
115  return radians * kRadToDeg;
116  }
117 
118  static T DegreesToRadians(const T& degrees) {
119  static const T kDegToRad = static_cast<T>(M_PI) / 180;
120  return degrees * kDegToRad;
121  }
122 
123  T radians_;
125 };
126 
127 
129 template <typename T>
130 std::ostream& operator<<(std::ostream& out, const Angle<T>& a) {
131  return out << a.Degrees() << " deg";
132 }
133 
134 template <typename T>
135 std::istream& operator>>(std::istream& in, Angle<T>& a) {
136  T angle;
137  if (in >> angle) {
138  if (base::GetExpectedString(in, "deg")) {
139  a = Angle<T>::FromDegrees(angle);
140  } else {
141  in.clear(); // Resets "deg" failure.
142  if (base::GetExpectedString(in, "rad"))
143  a = Angle<T>::FromRadians(angle);
144  }
145  }
146 
147  return in;
148 }
149 
151 
154 
155 
158 
159 } // namespace math
160 } // namespace ion
161 
162 #endif // ION_MATH_ANGLE_H_
friend bool operator!=(const Angle &a0, const Angle &a1)
Definition: angle.h:90
void operator-=(const Angle &a)
Definition: angle.h:65
Angle< float > Anglef
Type-specific typedefs.
Definition: angle.h:156
friend const Angle operator*(T s, const Angle &a)
Definition: angle.h:79
void operator+=(const Angle &a)
Self-modifying operators.
Definition: angle.h:64
friend bool operator>=(const Angle &a0, const Angle &a1)
Definition: angle.h:104
A simple class to represent angles.
Definition: angle.h:33
T Radians() const
Get the angle in degrees or radians.
Definition: angle.h:55
const Angle operator-() const
Unary negation operator.
Definition: angle.h:61
friend bool operator<=(const Angle &a0, const Angle &a1)
Definition: angle.h:101
void operator/=(T s)
Definition: angle.h:67
friend const Angle operator-(const Angle &a0, const Angle &a1)
Definition: angle.h:73
T Degrees() const
Definition: angle.h:56
std::istream & GetExpectedString(std::istream &in, const std::string &expected)
Attempts to read a string from the stream and returns the stream.
Definition: stringutils.h:228
friend const Angle operator+(const Angle &a0, const Angle &a1)
Binary operators.
Definition: angle.h:70
static Angle FromRadians(const T &angle)
Create a angle from radians (no conversion).
Definition: angle.h:45
friend const Angle operator/(const Angle &a, T s)
Definition: angle.h:82
friend bool operator==(const Angle &a0, const Angle &a1)
Exact equality and inequality comparisons.
Definition: angle.h:87
static Angle FromDegrees(const T &angle)
Create a angle from degrees (requires conversion).
Definition: angle.h:50
friend const Angle operator*(const Angle &a, T s)
Definition: angle.h:76
Angle(const Angle< U > other)
Copy constructor from an instance of any value type that is compatible (via static_cast) with this in...
Definition: angle.h:41
Angle()
The default constructor creates an angle of 0 (in any unit).
Definition: angle.h:36
std::istream & operator>>(std::istream &in, Angle< T > &a)
Definition: angle.h:135
Angle< double > Angled
Definition: angle.h:157
friend bool operator<(const Angle &a0, const Angle &a1)
Comparisons.
Definition: angle.h:95
void operator*=(T s)
Definition: angle.h:66
friend bool operator>(const Angle &a0, const Angle &a1)
Definition: angle.h:98