Apollo  6.0
Open source self driving car software
angle.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2017 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
22 #pragma once
23 
24 #include <cmath>
25 #include <cstdint>
26 #include <limits>
27 
32 namespace apollo {
33 namespace common {
34 namespace math {
35 
57 template <typename T>
58 class Angle {
59  public:
60  static_assert(std::numeric_limits<T>::is_integer &&
61  std::numeric_limits<T>::is_signed,
62  "T must be a signed integer type");
63 
69  static Angle from_deg(const double value) {
70  return Angle(static_cast<T>(std::lround(value * DEG_TO_RAW)));
71  }
72 
78  static Angle from_rad(const double value) {
79  return Angle(static_cast<T>(std::lround(value * RAD_TO_RAW)));
80  }
81 
87  explicit Angle(const T value = 0) : value_(value) {}
88 
90  static constexpr T RAW_PI = std::numeric_limits<T>::min();
91 
93  static constexpr T RAW_PI_2 =
94  static_cast<T>(-(std::numeric_limits<T>::min() >> 1));
95 
97  static constexpr double DEG_TO_RAW = RAW_PI / -180.0;
98 
100  static constexpr double RAD_TO_RAW = RAW_PI * -M_1_PI;
101 
103  static constexpr double RAW_TO_DEG = -180.0 / RAW_PI;
104 
106  static constexpr double RAW_TO_RAD = -M_PI / RAW_PI;
107 
112  T raw() const { return value_; }
113 
118  double to_deg() const { return value_ * RAW_TO_DEG; }
119 
124  double to_rad() const { return value_ * RAW_TO_RAD; }
125 
132  value_ = static_cast<T>(value_ + other.value_);
133  return *this;
134  }
135 
142  value_ = static_cast<T>(value_ - other.value_);
143  return *this;
144  }
145 
151  template <typename Scalar>
152  Angle operator*=(Scalar s) {
153  value_ = static_cast<T>(std::lround(value_ * s));
154  return *this;
155  }
156 
162  template <typename Scalar>
163  Angle operator/=(Scalar s) {
164  value_ = static_cast<T>(std::lround(value_ / s));
165  return *this;
166  }
167 
168  private:
169  T value_;
170 };
171 
176 
183 template <typename T>
185  lhs += rhs;
186  return lhs;
187 }
188 
195 template <typename T>
197  lhs -= rhs;
198  return lhs;
199 }
200 
207 template <typename T, typename Scalar>
208 Angle<T> operator*(Angle<T> lhs, Scalar rhs) {
209  lhs *= rhs;
210  return lhs;
211 }
212 
219 template <typename T, typename Scalar>
220 Angle<T> operator*(Scalar lhs, Angle<T> rhs) {
221  rhs *= lhs;
222  return rhs;
223 }
224 
231 template <typename T, typename Scalar>
232 Angle<T> operator/(Angle<T> lhs, Scalar rhs) {
233  lhs /= rhs;
234  return lhs;
235 }
236 
243 template <typename T>
244 double operator/(Angle<T> lhs, Angle<T> rhs) {
245  return static_cast<double>(lhs.raw()) / rhs.raw();
246 }
247 
254 template <typename T>
255 bool operator==(Angle<T> lhs, Angle<T> rhs) {
256  return lhs.raw() == rhs.raw();
257 }
258 
265 template <typename T>
266 bool operator!=(Angle<T> lhs, Angle<T> rhs) {
267  return !(lhs == rhs);
268 }
269 
270 // Fast trigonometric functions. Single precision is sufficient for Angle16 and
271 // Angle8.
272 float sin(Angle16 a);
273 float cos(Angle16 a);
274 float tan(Angle16 a);
275 float sin(Angle8 a);
276 float cos(Angle8 a);
277 float tan(Angle8 a);
278 
279 } // namespace math
280 } // namespace common
281 } // namespace apollo
static constexpr double DEG_TO_RAW
Used for converting angle units.
Definition: angle.h:97
Angle operator-=(Angle other)
Subtracts another angle from the current one.
Definition: angle.h:141
float tan(Angle16 a)
float sin(Angle16 a)
Angle operator/=(Scalar s)
Divides angle by scalar.
Definition: angle.h:163
double to_rad() const
Converts the internal representation to radians.
Definition: angle.h:124
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
static Angle from_deg(const double value)
Constructs an Angle object from an angle in degrees (factory).
Definition: angle.h:69
Angle< T > operator*(Angle< T > lhs, Scalar rhs)
Multiplies an Angle by a scalar.
Definition: angle.h:208
static constexpr double RAW_TO_RAD
Used for converting angle units.
Definition: angle.h:106
Angle< T > operator-(Angle< T > lhs, Angle< T > rhs)
Subtracts two angles.
Definition: angle.h:196
The Angle class uses an integer to represent an angle, and supports commonly-used operations such as ...
Definition: angle.h:58
Angle< T > operator/(Angle< T > lhs, Scalar rhs)
Divides an Angle by a scalar.
Definition: angle.h:232
static constexpr T RAW_PI
Internal representation of pi.
Definition: angle.h:90
static constexpr double RAW_TO_DEG
Used for converting angle units.
Definition: angle.h:103
bool operator!=(Angle< T > lhs, Angle< T > rhs)
Tests two Angle objects for inequality.
Definition: angle.h:266
T raw() const
Getter of value_.
Definition: angle.h:112
static constexpr double RAD_TO_RAW
Used for converting angle units.
Definition: angle.h:100
double to_deg() const
Converts the internal representation to degrees.
Definition: angle.h:118
Angle< T > operator+(Angle< T > lhs, Angle< T > rhs)
Sums two angles.
Definition: angle.h:184
static Angle from_rad(const double value)
Constructs an Angle object from an angle in radians (factory).
Definition: angle.h:78
float cos(Angle16 a)
Angle operator*=(Scalar s)
Multiplies angle by scalar.
Definition: angle.h:152
bool operator==(Angle< T > lhs, Angle< T > rhs)
Tests two Angle objects for equality.
Definition: angle.h:255
apollo::cyber::base::std value
Angle operator+=(Angle other)
Sums another angle to the current one.
Definition: angle.h:131
Angle(const T value=0)
Constructs an Angle object from raw internal value.
Definition: angle.h:87
static constexpr T RAW_PI_2
Internal representation of pi/2.
Definition: angle.h:93