math.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
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 
17 #ifndef CARTOGRAPHER_COMMON_MATH_H_
18 #define CARTOGRAPHER_COMMON_MATH_H_
19 
20 #include <cmath>
21 #include <vector>
22 
23 #include "Eigen/Core"
25 #include "ceres/ceres.h"
26 
27 namespace cartographer {
28 namespace common {
29 
30 // Clamps 'value' to be in the range ['min', 'max'].
31 template <typename T>
32 T Clamp(const T value, const T min, const T max) {
33  if (value > max) {
34  return max;
35  }
36  if (value < min) {
37  return min;
38  }
39  return value;
40 }
41 
42 // Calculates 'base'^'exponent'.
43 template <typename T>
44 constexpr T Power(T base, int exponent) {
45  return (exponent != 0) ? base * Power(base, exponent - 1) : T(1);
46 }
47 
48 // Calculates a^2.
49 template <typename T>
50 constexpr T Pow2(T a) {
51  return Power(a, 2);
52 }
53 
54 // Converts from degrees to radians.
55 constexpr double DegToRad(double deg) { return M_PI * deg / 180.; }
56 
57 // Converts form radians to degrees.
58 constexpr double RadToDeg(double rad) { return 180. * rad / M_PI; }
59 
60 // Bring the 'difference' between two angles into [-pi; pi].
61 template <typename T>
62 T NormalizeAngleDifference(T difference) {
63  const T kPi = T(M_PI);
64  while (difference > kPi) difference -= 2. * kPi;
65  while (difference < -kPi) difference += 2. * kPi;
66  return difference;
67 }
68 
69 template <typename T>
70 T atan2(const Eigen::Matrix<T, 2, 1>& vector) {
71  return ceres::atan2(vector.y(), vector.x());
72 }
73 
74 template <typename T>
75 inline void QuaternionProduct(const double* const z, const T* const w,
76  T* const zw) {
77  zw[0] = z[0] * w[0] - z[1] * w[1] - z[2] * w[2] - z[3] * w[3];
78  zw[1] = z[0] * w[1] + z[1] * w[0] + z[2] * w[3] - z[3] * w[2];
79  zw[2] = z[0] * w[2] - z[1] * w[3] + z[2] * w[0] + z[3] * w[1];
80  zw[3] = z[0] * w[3] + z[1] * w[2] - z[2] * w[1] + z[3] * w[0];
81 }
82 
83 } // namespace common
84 } // namespace cartographer
85 
86 #endif // CARTOGRAPHER_COMMON_MATH_H_
constexpr T Pow2(T a)
Definition: math.h:50
T atan2(const Eigen::Matrix< T, 2, 1 > &vector)
Definition: math.h:70
constexpr double RadToDeg(double rad)
Definition: math.h:58
constexpr T Power(T base, int exponent)
Definition: math.h:44
void QuaternionProduct(const double *const z, const T *const w, T *const zw)
Definition: math.h:75
constexpr double DegToRad(double deg)
Definition: math.h:55
T Clamp(const T value, const T min, const T max)
Definition: math.h:32
T NormalizeAngleDifference(T difference)
Definition: math.h:62


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58