math.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef CARTOGRAPHER_COMMON_MATH_H_
00018 #define CARTOGRAPHER_COMMON_MATH_H_
00019 
00020 #include <cmath>
00021 #include <vector>
00022 
00023 #include "Eigen/Core"
00024 #include "cartographer/common/port.h"
00025 #include "ceres/ceres.h"
00026 
00027 namespace cartographer {
00028 namespace common {
00029 
00030 // Clamps 'value' to be in the range ['min', 'max'].
00031 template <typename T>
00032 T Clamp(const T value, const T min, const T max) {
00033   if (value > max) {
00034     return max;
00035   }
00036   if (value < min) {
00037     return min;
00038   }
00039   return value;
00040 }
00041 
00042 // Calculates 'base'^'exponent'.
00043 template <typename T>
00044 constexpr T Power(T base, int exponent) {
00045   return (exponent != 0) ? base * Power(base, exponent - 1) : T(1);
00046 }
00047 
00048 // Calculates a^2.
00049 template <typename T>
00050 constexpr T Pow2(T a) {
00051   return Power(a, 2);
00052 }
00053 
00054 // Converts from degrees to radians.
00055 constexpr double DegToRad(double deg) { return M_PI * deg / 180.; }
00056 
00057 // Converts form radians to degrees.
00058 constexpr double RadToDeg(double rad) { return 180. * rad / M_PI; }
00059 
00060 // Bring the 'difference' between two angles into [-pi; pi].
00061 template <typename T>
00062 T NormalizeAngleDifference(T difference) {
00063   const T kPi = T(M_PI);
00064   while (difference > kPi) difference -= 2. * kPi;
00065   while (difference < -kPi) difference += 2. * kPi;
00066   return difference;
00067 }
00068 
00069 template <typename T>
00070 T atan2(const Eigen::Matrix<T, 2, 1>& vector) {
00071   return ceres::atan2(vector.y(), vector.x());
00072 }
00073 
00074 template <typename T>
00075 inline void QuaternionProduct(const double* const z, const T* const w,
00076                               T* const zw) {
00077   zw[0] = z[0] * w[0] - z[1] * w[1] - z[2] * w[2] - z[3] * w[3];
00078   zw[1] = z[0] * w[1] + z[1] * w[0] + z[2] * w[3] - z[3] * w[2];
00079   zw[2] = z[0] * w[2] - z[1] * w[3] + z[2] * w[0] + z[3] * w[1];
00080   zw[3] = z[0] * w[3] + z[1] * w[2] - z[2] * w[1] + z[3] * w[0];
00081 }
00082 
00083 }  // namespace common
00084 }  // namespace cartographer
00085 
00086 #endif  // CARTOGRAPHER_COMMON_MATH_H_


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35