Tetrapod Project
angle_utils.cpp
Go to the documentation of this file.
1 /*******************************************************************/
2 /* AUTHOR: Paal Arthur S. Thorseth */
3 /* ORGN: Dept of Eng Cybernetics, NTNU Trondheim */
4 /* FILE: angle_utils.cpp */
5 /* DATE: Feb 9, 2021 */
6 /* */
7 /* Copyright (C) 2021 Paal Arthur S. Thorseth, */
8 /* Adrian B. Ghansah */
9 /* */
10 /* This program is free software: you can redistribute it */
11 /* and/or modify it under the terms of the GNU General */
12 /* Public License as published by the Free Software Foundation, */
13 /* either version 3 of the License, or (at your option) any */
14 /* later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty */
18 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
19 /* See the GNU General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU General Public */
22 /* License along with this program. If not, see */
23 /* <https://www.gnu.org/licenses/>. */
24 /* */
25 /*******************************************************************/
26 
27 #include <math_utils/angle_utils.h>
28 
29 namespace math_utils
30 {
31 
32 // Convert deg to rad
33 double degToRad(const double &_deg)
34 {
35  return (_deg/180.0) * PI;
36 }
37 
38 // Convert rad to deg
39 double radToDeg(const double &_rad)
40 {
41  return (_rad/PI) * 180;
42 }
43 
44 // Wrap angle to the interval [-pi, pi)
45 double wrapAngleToPi(const double &_rad)
46 {
47  double ret = std::fmod(_rad + PI, 2 * PI);
48 
49  if (ret < 0)
50  {
51  ret += 2 * PI;
52  }
53 
54  return ret - PI;
55 }
56 
57 
58 // Wrap angle to the interval [0, 2pi)
59 double wrapAngleTo2Pi(const double &_rad)
60 {
61  double ret = std::fmod(_rad, 2 * PI);
62 
63  if (ret < 0)
64  {
65  ret += 2 * PI;
66  }
67 
68  return ret;
69 }
70 
71 // Angle difference
72 double angleDiff(const double &_ang1, const double &_ang2)
73 {
74  double diff = std::fmod(_ang2 - _ang1 + PI, 2 * PI);
75 
76  if (diff < 0)
77  {
78  diff += 2*PI;
79  }
80 
81  return diff - PI;
82 }
83 
84 
85 }// namespace angle_utils
double wrapAngleToPi(const double &_rad)
The wrapAngleToPi function wraps an input angle to the interval [-pi, pi).
Definition: angle_utils.cpp:45
double degToRad(const double &_deg)
The degToRad function converts degrees to radians.
Definition: angle_utils.cpp:33
double angleDiff(const double &_ang1, const double &_ang2)
The angleDiff function returns the difference in angle between two given angles in the interval [0,...
Definition: angle_utils.cpp:72
double radToDeg(const double &_rad)
The radToDeg function converts radians to degrees.
Definition: angle_utils.cpp:39
static constexpr double PI
Define PI.
Definition: angle_utils.h:44
double wrapAngleTo2Pi(const double &_rad)
The wrapAngleTo2Pi function wraps an input angle to the interval [0, 2pi).
Definition: angle_utils.cpp:59