imunano33  0.1.1
mathutil.hpp
Go to the documentation of this file.
1 
6 #ifndef INCLUDE_IMUNANO33_MATHUTIL_HPP_
7 #define INCLUDE_IMUNANO33_MATHUTIL_HPP_
8 
9 #ifdef IMUNANO33_EMBED
10 #include <float.h>
11 #include <math.h>
12 #else
13 #include <algorithm>
14 #include <cmath>
15 #include <limits>
16 #endif
17 
18 #ifdef IMUNANO33_EMBED
19 #include "imunano33/sv_embed.hpp"
20 #else
22 #endif
23 #include "imunano33/unit.hpp"
24 
25 namespace imunano33 {
26 #ifdef IMUNANO33_EMBED
27 using Vector3D =
29 #else
30 using std::fabs;
31 using svector::Vector3D;
32 #endif
33 
37 class MathUtil {
38 public:
48  static bool nearZero(const num_t num) { return nearZero(num, NEAR_ZERO); }
49 
59  static bool nearZero(const num_t num, const num_t tol) {
60  return fabs(num) < tol;
61  }
62 
72  static bool nearZero(const Vector3D &vec) { return nearZero(vec, NEAR_ZERO); }
73 
83  static bool nearZero(const Vector3D &vec, const num_t tol) {
84 #ifdef IMUNANO33_EMBED
85  return fabs(vec.x) < tol && fabs(vec.y) < tol && fabs(vec.z) < tol;
86 #else
87  return std::none_of(vec.begin(), vec.end(), [tol](const num_t &el) {
88  return std::fabs(el) >= tol;
89  });
90 #endif
91  }
92 
106  static bool nearEq(const num_t num1, const num_t num2) {
107  return nearEq(num1, num2, NEAR_ZERO);
108  }
109 
123  static bool nearEq(const num_t num1, const num_t num2, const num_t tol) {
124  return fabs(num1 - num2) < tol;
125  }
126 
141  template <typename T> static T clamp(const T &num, const T &lo, const T &hi) {
142  return num < lo ? lo : num > hi ? hi : num;
143  }
144 
145 private:
146 #ifdef IMUNANO33_EMBED
147  static constexpr num_t NEAR_ZERO = FLT_EPSILON;
148 #else
149  static constexpr num_t NEAR_ZERO = std::numeric_limits<num_t>::epsilon();
150 #endif
151 };
152 
153 } // namespace imunano33
154 
155 #endif
A minimized version of vectors for embedded devices without access to the STL (such as on an Arduino,...
Contains imunano33::TempUnit and imunano33::PressureUnit enums, in addition to number type.
double num_t
Alias to number type depending on embed.
Definition: unit.hpp:34
Utility static methods for math calculations.
Definition: mathutil.hpp:37
static T clamp(const T &num, const T &lo, const T &hi)
Restricts num between lo and hi.
Definition: mathutil.hpp:141
static bool nearZero(const num_t num)
Determines if number is near zero with given precision.
Definition: mathutil.hpp:48
static bool nearZero(const Vector3D &vec, const num_t tol)
Determines if vector is near zero with given precision.
Definition: mathutil.hpp:83
static bool nearEq(const num_t num1, const num_t num2, const num_t tol)
Determines if num1 is nearly equal to num2.
Definition: mathutil.hpp:123
static bool nearZero(const num_t num, const num_t tol)
Determines if number is near zero with given precision.
Definition: mathutil.hpp:59
static bool nearZero(const Vector3D &vec)
Determines if vector is near zero with given precision.
Definition: mathutil.hpp:72
static bool nearEq(const num_t num1, const num_t num2)
Determines if num1 is nearly equal to num2.
Definition: mathutil.hpp:106
iterator begin() noexcept
Iterator of first element.
Definition: simplevectors.hpp:554
iterator end() noexcept
Interator of last element + 1.
Definition: simplevectors.hpp:581
A simple 3D vector representation.
Definition: simplevectors.hpp:844
double z() const
Gets z-component.
Definition: simplevectors.hpp:913
double x() const
Gets x-component.
Definition: simplevectors.hpp:877
double y() const
Gets y-component.
Definition: simplevectors.hpp:895
A minimal 3D vector representation.
Definition: sv_embed.hpp:168