Apollo 11.0
自动驾驶开放平台
math_utils.cc
浏览该文件的文档.
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
18
19#include <cmath>
20#include <utility>
21
22namespace apollo {
23namespace common {
24namespace math {
25
26double Sqr(const double x) {
27 return x * x;
28}
29
30double CrossProd(const Vec2d& start_point, const Vec2d& end_point_1, const Vec2d& end_point_2) {
31 return (end_point_1 - start_point).CrossProd(end_point_2 - start_point);
32}
33
34double InnerProd(const Vec2d& start_point, const Vec2d& end_point_1, const Vec2d& end_point_2) {
35 return (end_point_1 - start_point).InnerProd(end_point_2 - start_point);
36}
37
38double CrossProd(const double x0, const double y0, const double x1, const double y1) {
39 return x0 * y1 - x1 * y0;
40}
41
42double InnerProd(const double x0, const double y0, const double x1, const double y1) {
43 return x0 * x1 + y0 * y1;
44}
45
46double WrapAngle(const double angle) {
47 const double new_angle = std::fmod(angle, M_PI * 2.0);
48 return new_angle < 0 ? new_angle + M_PI * 2.0 : new_angle;
49}
50
51double NormalizeAngle(const double angle) {
52 double a = std::fmod(angle + M_PI, 2.0 * M_PI);
53 if (a < 0.0) {
54 a += (2.0 * M_PI);
55 }
56 return a - M_PI;
57}
58
59double AngleDiff(const double from, const double to) {
60 return NormalizeAngle(to - from);
61}
62
63int RandomInt(const int s, const int t, unsigned int rand_seed) {
64 if (s >= t) {
65 return s;
66 }
67 return s + rand_r(&rand_seed) % (t - s + 1);
68}
69
70double RandomDouble(const double s, const double t, unsigned int rand_seed) {
71 return s + (t - s) / 16383.0 * (rand_r(&rand_seed) & 16383);
72}
73
74// Gaussian
75double Gaussian(const double u, const double std, const double x) {
76 return (1.0 / std::sqrt(2 * M_PI * std * std)) * std::exp(-(x - u) * (x - u) / (2 * std * std));
77}
78
79Eigen::Vector2d RotateVector2d(const Eigen::Vector2d& v_in, const double theta) {
80 const double cos_theta = std::cos(theta);
81 const double sin_theta = std::sin(theta);
82
83 auto x = cos_theta * v_in.x() - sin_theta * v_in.y();
84 auto y = sin_theta * v_in.x() + cos_theta * v_in.y();
85
86 return {x, y};
87}
88
89std::pair<double, double> Cartesian2Polar(double x, double y) {
90 double r = std::sqrt(x * x + y * y);
91 double theta = std::atan2(y, x);
92 return std::make_pair(r, theta);
93}
94
95double check_negative(double input_data) {
96 if (std::signbit(input_data)) {
97 input_data = -input_data;
98 }
99 return input_data;
100}
101
102int sign(double input_data) {
103 if (input_data > 0) {
104 return 1;
105 } else if (input_data < 0) {
106 return -1;
107 } else {
108 return 0;
109 }
110}
111
112} // namespace math
113} // namespace common
114} // namespace apollo
Math-related util functions.
double Sqr(const double x)
Definition math_utils.cc:26
int RandomInt(const int s, const int t, unsigned int rand_seed)
Get a random integer between two integer values by a random seed.
Definition math_utils.cc:65
int sign(double input_data)
double Gaussian(const double u, const double std, const double x)
Definition math_utils.cc:77
double CrossProd(const Vec2d &start_point, const Vec2d &end_point_1, const Vec2d &end_point_2)
Cross product between two 2-D vectors from the common start point, and end at two other points.
Definition math_utils.cc:28
double RandomDouble(const double s, const double t, unsigned int rand_seed)
Get a random double between two integer values by a random seed.
Definition math_utils.cc:72
double WrapAngle(const double angle)
Wrap angle to [0, 2 * PI).
Definition math_utils.cc:48
double InnerProd(const Vec2d &start_point, const Vec2d &end_point_1, const Vec2d &end_point_2)
Inner product between two 2-D vectors from the common start point, and end at two other points.
Definition math_utils.cc:33
std::pair< double, double > Cartesian2Polar(double x, double y)
Definition math_utils.cc:93
double AngleDiff(const double from, const double to)
Calculate the difference between angle from and to
Definition math_utils.cc:61
double check_negative(double input_data)
Definition math_utils.cc:99
double NormalizeAngle(const double angle)
Normalize angle to [-PI, PI).
Definition math_utils.cc:53
Eigen::Vector2d RotateVector2d(const Eigen::Vector2d &v_in, const double theta)
Definition math_utils.cc:82
class register implement
Definition arena_queue.h:37
Definition future.h:29