MathUtils.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::Core
17 * @author Simon Ottenhaus ( simon dot ottenhaus at kit dot edu )
18 * @date 2015
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #pragma once
24 
25 #include <math.h>
26 
27 #include <vector>
28 
29 #include <Eigen/Core>
30 
31 namespace armarx::math
32 {
33  class MathUtils
34  {
35  public:
36  static int
37  Sign(double value)
38  {
39  return value >= 0 ? 1 : -1;
40  }
41 
42  static int
43  LimitMinMax(int min, int max, int value)
44  {
45  return value < min ? min : (value > max ? max : value);
46  }
47 
48  static float
49  LimitMinMax(float min, float max, float value)
50  {
51  return value < min ? min : (value > max ? max : value);
52  }
53 
54  static double
55  LimitMinMax(double min, double max, double value)
56  {
57  return value < min ? min : (value > max ? max : value);
58  }
59 
60  static Eigen::Vector3f
61  LimitMinMax(const Eigen::Vector3f& min,
62  const Eigen::Vector3f& max,
63  const Eigen::Vector3f& value)
64  {
65  return Eigen::Vector3f(LimitMinMax(min(0), max(0), value(0)),
66  LimitMinMax(min(1), max(1), value(1)),
67  LimitMinMax(min(2), max(2), value(2)));
68  }
69 
70  static double
71  LimitTo(double value, double absThreshold)
72  {
73  return LimitMinMax(-absThreshold, absThreshold, value);
74  //int sign = (value >= 0) ? 1 : -1;
75  //return sign * std::min<double>(fabs(value), absThreshold);
76  }
77 
78  static Eigen::Vector3f
79  LimitTo(const Eigen::Vector3f& val, float maxNorm)
80  {
81  float norm = val.norm();
82  if (norm > maxNorm)
83  {
84  return val / norm * maxNorm;
85  }
86  return val;
87  }
88 
89  static bool
90  CheckMinMax(int min, int max, int value)
91  {
92  return value >= min && value <= max;
93  }
94 
95  static bool
96  CheckMinMax(float min, float max, float value)
97  {
98  return value >= min && value <= max;
99  }
100 
101  static bool
102  CheckMinMax(double min, double max, double value)
103  {
104  return value >= min && value <= max;
105  }
106 
107  static bool
108  CheckMinMax(const Eigen::Vector3f& min,
109  const Eigen::Vector3f& max,
110  const Eigen::Vector3f& value)
111  {
112  return CheckMinMax(min(0), max(0), value(0)) && CheckMinMax(min(1), max(1), value(1)) &&
113  CheckMinMax(min(2), max(2), value(2));
114  }
115 
116  static std::vector<float>
117  VectorSubtract(const std::vector<float>& v1, const std::vector<float>& v2)
118  {
119  std::vector<float> result;
120 
121  for (size_t i = 0; i < v1.size() && i < v2.size(); i++)
122  {
123  result.push_back(v1.at(i) - v2.at(i));
124  }
125 
126  return result;
127  }
128 
129  static std::vector<float>
130  VectorAbsDiff(const std::vector<float>& v1, const std::vector<float>& v2)
131  {
132  std::vector<float> result;
133 
134  for (size_t i = 0; i < v1.size() && i < v2.size(); i++)
135  {
136  result.push_back(std::fabs(v1.at(i) - v2.at(i)));
137  }
138 
139  return result;
140  }
141 
142  static float
143  VectorMax(const std::vector<float>& vec)
144  {
145  float max = vec.at(0);
146 
147  for (size_t i = 1; i < vec.size(); i++)
148  {
149  max = std::max(max, vec.at(i));
150  }
151 
152  return max;
153  }
154 
155  static float
156  fmod(float value, float boundLow, float boundHigh)
157  {
158  value = std::fmod(value - boundLow, boundHigh - boundLow) + boundLow;
159  if (value < boundLow)
160  {
161  value += boundHigh - boundLow;
162  }
163  return value;
164  }
165 
166  static float
168  {
169  return fmod(value, 0, 2 * M_PI);
170  }
171 
172  static float
174  {
175  return angleMod2PI(value + M_PI) - M_PI;
176  }
177 
178  static float
179  angleModX(float value, float center)
180  {
181  return angleMod2PI(value + M_PI - center) - M_PI + center;
182  }
183 
184  static float
185  Lerp(float a, float b, float f)
186  {
187  return a * (1 - f) + b * f;
188  }
189 
190  static float
191  LerpClamp(float a, float b, float f)
192  {
193  return Lerp(a, b, LimitMinMax(0.f, 1.f, f));
194  }
195 
196  static float
197  ILerp(float a, float b, float f)
198  {
199  return (f - a) / (b - a);
200  }
201 
202  static float
203  ILerpClamp(float a, float b, float f)
204  {
205  return LimitMinMax(0.f, 1.f, ILerp(a, b, f));
206  }
207 
208  static float
209  AngleLerp(float a, float b, float f)
210  {
211  b = fmod(b, a - M_PI, a + M_PI);
212  return Lerp(a, b, f);
213  }
214 
215  static float
216  AngleDelta(float angle1, float angle2)
217  {
218  return angleModPI(angle2 - angle1);
219  }
220  };
221 } // namespace armarx::math
armarx::math::MathUtils::LimitTo
static Eigen::Vector3f LimitTo(const Eigen::Vector3f &val, float maxNorm)
Definition: MathUtils.h:79
armarx::math::MathUtils::ILerp
static float ILerp(float a, float b, float f)
Definition: MathUtils.h:197
armarx::math::MathUtils::VectorMax
static float VectorMax(const std::vector< float > &vec)
Definition: MathUtils.h:143
armarx::math::MathUtils::AngleLerp
static float AngleLerp(float a, float b, float f)
Definition: MathUtils.h:209
armarx::math::MathUtils::AngleDelta
static float AngleDelta(float angle1, float angle2)
Definition: MathUtils.h:216
armarx::math::MathUtils::VectorSubtract
static std::vector< float > VectorSubtract(const std::vector< float > &v1, const std::vector< float > &v2)
Definition: MathUtils.h:117
armarx::math::MathUtils::LimitMinMax
static double LimitMinMax(double min, double max, double value)
Definition: MathUtils.h:55
armarx::math::MathUtils::fmod
static float fmod(float value, float boundLow, float boundHigh)
Definition: MathUtils.h:156
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:297
armarx::math::MathUtils::angleModX
static float angleModX(float value, float center)
Definition: MathUtils.h:179
armarx::math::MathUtils::CheckMinMax
static bool CheckMinMax(const Eigen::Vector3f &min, const Eigen::Vector3f &max, const Eigen::Vector3f &value)
Definition: MathUtils.h:108
armarx::math::MathUtils::angleMod2PI
static float angleMod2PI(float value)
Definition: MathUtils.h:167
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::math::MathUtils::LimitMinMax
static Eigen::Vector3f LimitMinMax(const Eigen::Vector3f &min, const Eigen::Vector3f &max, const Eigen::Vector3f &value)
Definition: MathUtils.h:61
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::math::MathUtils::CheckMinMax
static bool CheckMinMax(double min, double max, double value)
Definition: MathUtils.h:102
max
T max(T t1, T t2)
Definition: gdiam.h:51
armarx::math::MathUtils::angleModPI
static float angleModPI(float value)
Definition: MathUtils.h:173
armarx::math::MathUtils
Definition: MathUtils.h:33
armarx::math::MathUtils::LimitMinMax
static float LimitMinMax(float min, float max, float value)
Definition: MathUtils.h:49
armarx::math::MathUtils::LimitTo
static double LimitTo(double value, double absThreshold)
Definition: MathUtils.h:71
armarx::math::MathUtils::LimitMinMax
static int LimitMinMax(int min, int max, int value)
Definition: MathUtils.h:43
armarx::math::MathUtils::LerpClamp
static float LerpClamp(float a, float b, float f)
Definition: MathUtils.h:191
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:327
armarx::math::MathUtils::Sign
static int Sign(double value)
Definition: MathUtils.h:37
armarx::math::MathUtils::VectorAbsDiff
static std::vector< float > VectorAbsDiff(const std::vector< float > &v1, const std::vector< float > &v2)
Definition: MathUtils.h:130
armarx::math
Definition: LinearizeAngularTrajectory.cpp:28
armarx::math::MathUtils::ILerpClamp
static float ILerpClamp(float a, float b, float f)
Definition: MathUtils.h:203
armarx::math::MathUtils::CheckMinMax
static bool CheckMinMax(int min, int max, int value)
Definition: MathUtils.h:90
armarx::math::MathUtils::CheckMinMax
static bool CheckMinMax(float min, float max, float value)
Definition: MathUtils.h:96
armarx::math::MathUtils::Lerp
static float Lerp(float a, float b, float f)
Definition: MathUtils.h:185
norm
double norm(const Point &a)
Definition: point.hpp:102