Apollo 10.0
自动驾驶开放平台
angle.h
浏览该文件的文档.
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
32namespace apollo {
33namespace common {
34namespace math {
35
57template <typename T>
58class 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
183template <typename T>
185 lhs += rhs;
186 return lhs;
187}
188
195template <typename T>
197 lhs -= rhs;
198 return lhs;
199}
200
207template <typename T, typename Scalar>
208Angle<T> operator*(Angle<T> lhs, Scalar rhs) {
209 lhs *= rhs;
210 return lhs;
211}
212
219template <typename T, typename Scalar>
220Angle<T> operator*(Scalar lhs, Angle<T> rhs) {
221 rhs *= lhs;
222 return rhs;
223}
224
231template <typename T, typename Scalar>
232Angle<T> operator/(Angle<T> lhs, Scalar rhs) {
233 lhs /= rhs;
234 return lhs;
235}
236
243template <typename T>
244double operator/(Angle<T> lhs, Angle<T> rhs) {
245 return static_cast<double>(lhs.raw()) / rhs.raw();
246}
247
254template <typename T>
256 return lhs.raw() == rhs.raw();
257}
258
265template <typename T>
267 return !(lhs == rhs);
268}
269
270// Fast trigonometric functions. Single precision is sufficient for Angle16 and
271// Angle8.
272float sin(Angle16 a);
273float cos(Angle16 a);
274float tan(Angle16 a);
275float sin(Angle8 a);
276float cos(Angle8 a);
277float tan(Angle8 a);
278
279} // namespace math
280} // namespace common
281} // namespace apollo
The Angle class uses an integer to represent an angle, and supports commonly-used operations such as ...
Definition angle.h:58
Angle operator/=(Scalar s)
Divides angle by scalar
Definition angle.h:163
static constexpr double RAD_TO_RAW
Used for converting angle units
Definition angle.h:100
static Angle from_deg(const double value)
Constructs an Angle object from an angle in degrees (factory).
Definition angle.h:69
static constexpr double RAW_TO_DEG
Used for converting angle units
Definition angle.h:103
T raw() const
Getter of value_.
Definition angle.h:112
Angle operator-=(Angle other)
Subtracts another angle from the current one.
Definition angle.h:141
static Angle from_rad(const double value)
Constructs an Angle object from an angle in radians (factory).
Definition angle.h:78
double to_rad() const
Converts the internal representation to radians.
Definition angle.h:124
static constexpr T RAW_PI
Internal representation of pi
Definition angle.h:90
double to_deg() const
Converts the internal representation to degrees.
Definition angle.h:118
static constexpr T RAW_PI_2
Internal representation of pi/2
Definition angle.h:93
Angle operator+=(Angle other)
Sums another angle to the current one.
Definition angle.h:131
Angle operator*=(Scalar s)
Multiplies angle by scalar
Definition angle.h:152
static constexpr double RAW_TO_RAD
Used for converting angle units
Definition angle.h:106
static constexpr double DEG_TO_RAW
Used for converting angle units
Definition angle.h:97
Angle(const T value=0)
Constructs an Angle object from raw internal value.
Definition angle.h:87
bool operator==(Angle< T > lhs, Angle< T > rhs)
Tests two Angle objects for equality
Definition angle.h:255
Angle< T > operator-(Angle< T > lhs, Angle< T > rhs)
Subtracts two angles
Definition angle.h:196
Angle< T > operator/(Angle< T > lhs, Scalar rhs)
Divides an Angle by a scalar
Definition angle.h:232
float cos(Angle16 a)
Definition angle.cc:42
Angle< int8_t > Angle8
Definition angle.h:172
Angle< T > operator+(Angle< T > lhs, Angle< T > rhs)
Sums two angles
Definition angle.h:184
Angle< int16_t > Angle16
Definition angle.h:173
float sin(Angle16 a)
Definition angle.cc:25
bool operator!=(Angle< T > lhs, Angle< T > rhs)
Tests two Angle objects for inequality
Definition angle.h:266
Angle< int64_t > Angle64
Definition angle.h:175
Angle< int32_t > Angle32
Definition angle.h:174
float tan(Angle16 a)
Definition angle.cc:47
Angle< T > operator*(Angle< T > lhs, Scalar rhs)
Multiplies an Angle by a scalar
Definition angle.h:208
class register implement
Definition arena_queue.h:37