math_utils.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020, Christoph Rösmann, All rights reserved.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  *
20  * Authors: Christoph Rösmann
21  *********************************************************************/
22 
23 #ifndef UTILS_MATH_UTILS_H_
24 #define UTILS_MATH_UTILS_H_
25 
26 #include <cmath>
27 
28 namespace mpc_local_planner {
29 
35 inline double average_angles(const std::vector<double>& angles)
36 {
37  double x = 0, y = 0;
38  for (std::vector<double>::const_iterator it = angles.begin(); it != angles.end(); ++it)
39  {
40  x += std::cos(*it);
41  y += std::sin(*it);
42  }
43  if (x == 0 && y == 0)
44  return 0;
45  else
46  return std::atan2(y, x);
47 }
48 
55 template <typename P1, typename P2>
56 inline double distance_points2d(const P1& point1, const P2& point2)
57 {
58  return std::sqrt(std::pow(point2.x - point1.x, 2) + std::pow(point2.y - point1.y, 2));
59 }
60 
62 inline double distance_points2d(double x1, double y1, double x2, double y2) { return std::sqrt(std::pow(x2 - x1, 2) + std::pow(y2 - y1, 2)); }
63 
70 template <typename V1, typename V2>
71 inline double cross2d(const V1& v1, const V2& v2)
72 {
73  return v1.x() * v2.y() - v2.x() * v1.y();
74 }
75 
81 inline double normalize_theta(double theta)
82 {
83  if (theta >= -M_PI && theta < M_PI) return theta;
84 
85  double multiplier = std::floor(theta / (2.0 * M_PI));
86  theta = theta - multiplier * 2.0 * M_PI;
87  if (theta >= M_PI) theta -= 2.0 * M_PI;
88  if (theta < -M_PI) theta += 2.0 * M_PI;
89 
90  return theta;
91 }
92 
100 inline double interpolate_angle(double angle1, double angle2, double factor)
101 {
102  return normalize_theta(angle1 + factor * normalize_theta(angle2 - angle1));
103 }
104 
105 } // namespace mpc_local_planner
106 
107 #endif // UTILS_MATH_UTILS_H_
Scalar * x
double interpolate_angle(double angle1, double angle2, double factor)
Return the interpolated angle between two angles [rad].
Definition: math_utils.h:100
double average_angles(const std::vector< double > &angles)
Return the average angle of an arbitrary number of given angles [rad].
Definition: math_utils.h:35
double distance_points2d(const P1 &point1, const P2 &point2)
Calculate Euclidean distance between two 2D point datatypes.
Definition: math_utils.h:56
double normalize_theta(double theta)
normalize angle to interval [-pi, pi)
Definition: math_utils.h:81
double cross2d(const V1 &v1, const V2 &v2)
Calculate the 2d cross product (returns length of the resulting vector along the z-axis in 3d) ...
Definition: math_utils.h:71
const T & y


mpc_local_planner
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:53:18