math_utils.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_CORE_MATH_UTILS_H
2 #define SLAM_CTOR_CORE_MATH_UTILS_H
3 
4 #include <cmath>
5 #include <limits>
6 
7 // Workaround to enable compiling on gcc-4.9
8 #if __GNUC__ < 5
9 #define CONSTEXPR
10 #else
11 #define CONSTEXPR constexpr
12 #endif
13 
14 // TODO: add unit tests for math utils
15 
16 template <typename T>
17 CONSTEXPR inline bool are_equal(const T& a, const T& b, const T& eps) {
18  return std::abs(a - b) <= eps;
19 }
20 
21 // FIXME: are_equal(infinity, 0) is true
22 CONSTEXPR inline bool are_equal(double a, double b) {
23  // cmp doubles according to http://realtimecollisiondetection.net/blog/?p=89
24  double eps_scale = std::max(1.0, std::max(std::abs(a), std::abs(b)));
25  constexpr double Eps = 1e-7; // num_limits::epsilon is too small
26  return are_equal(a, b, Eps * eps_scale);
27 }
28 
29 CONSTEXPR inline bool less(double a, double b) {
30  constexpr double Eps = std::numeric_limits<double>::epsilon();
31  return a < b + Eps;
32 }
33 
34 CONSTEXPR inline bool is_multiple_of(double value, double factor) {
35  // TODO: verify numerical safety/bounds
36  double ratio = value / factor;
37  return are_equal(ratio, std::trunc(ratio));
38 }
39 
40 inline double bound_value(double left, double v, double right) {
41  return std::max(left, std::min(right, v));
42 }
43 
44 inline bool less_or_equal(double a, double b) {
45  return are_equal(a, b) || less(a, b);
46 }
47 
48 inline bool are_strictly_ordered(double a, double b, double c) {
49  return less(a, b) && less(b, c);
50 }
51 
52 inline bool are_ordered(double a, double b, double c) {
53  return less_or_equal(a, b) && less_or_equal(b, c);
54 }
55 
56 constexpr inline double deg2rad(double angle_deg) {
57  return angle_deg * M_PI / 180;
58 }
59 
60 constexpr inline double rad2deg(double angle_rad) {
61  return angle_rad * 180 / M_PI;
62 }
63 
64 template <unsigned N>
65 CONSTEXPR int ge_pow(int i) {
66  int ge_p = 1;
67  while (ge_p < i) {
68  ge_p *= N;
69  }
70  return ge_p;
71 }
72 
73 #endif
CONSTEXPR bool is_multiple_of(double value, double factor)
Definition: math_utils.h:34
CONSTEXPR bool are_equal(const T &a, const T &b, const T &eps)
Definition: math_utils.h:17
CONSTEXPR int ge_pow(int i)
Definition: math_utils.h:65
constexpr double rad2deg(double angle_rad)
Definition: math_utils.h:60
bool are_ordered(double a, double b, double c)
Definition: math_utils.h:52
double bound_value(double left, double v, double right)
Definition: math_utils.h:40
bool less_or_equal(double a, double b)
Definition: math_utils.h:44
#define CONSTEXPR
Definition: math_utils.h:9
CONSTEXPR bool less(double a, double b)
Definition: math_utils.h:29
constexpr double deg2rad(double angle_deg)
Definition: math_utils.h:56
bool are_strictly_ordered(double a, double b, double c)
Definition: math_utils.h:48


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25