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
31namespace armarx::math
32{
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
167 angleMod2PI(float value)
168 {
169 return fmod(value, 0, 2 * M_PI);
170 }
171
172 static float
173 angleModPI(float value)
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
#define M_PI
Definition MathTools.h:17
static int Sign(double value)
Definition MathUtils.h:37
static float VectorMax(const std::vector< float > &vec)
Definition MathUtils.h:143
static double LimitMinMax(double min, double max, double value)
Definition MathUtils.h:55
static float angleModPI(float value)
Definition MathUtils.h:173
static bool CheckMinMax(double min, double max, double value)
Definition MathUtils.h:102
static float angleMod2PI(float value)
Definition MathUtils.h:167
static float Lerp(float a, float b, float f)
Definition MathUtils.h:185
static Eigen::Vector3f LimitMinMax(const Eigen::Vector3f &min, const Eigen::Vector3f &max, const Eigen::Vector3f &value)
Definition MathUtils.h:61
static std::vector< float > VectorAbsDiff(const std::vector< float > &v1, const std::vector< float > &v2)
Definition MathUtils.h:130
static bool CheckMinMax(const Eigen::Vector3f &min, const Eigen::Vector3f &max, const Eigen::Vector3f &value)
Definition MathUtils.h:108
static float LimitMinMax(float min, float max, float value)
Definition MathUtils.h:49
static bool CheckMinMax(float min, float max, float value)
Definition MathUtils.h:96
static float LerpClamp(float a, float b, float f)
Definition MathUtils.h:191
static float fmod(float value, float boundLow, float boundHigh)
Definition MathUtils.h:156
static float ILerp(float a, float b, float f)
Definition MathUtils.h:197
static double LimitTo(double value, double absThreshold)
Definition MathUtils.h:71
static float AngleLerp(float a, float b, float f)
Definition MathUtils.h:209
static int LimitMinMax(int min, int max, int value)
Definition MathUtils.h:43
static Eigen::Vector3f LimitTo(const Eigen::Vector3f &val, float maxNorm)
Definition MathUtils.h:79
static std::vector< float > VectorSubtract(const std::vector< float > &v1, const std::vector< float > &v2)
Definition MathUtils.h:117
static float angleModX(float value, float center)
Definition MathUtils.h:179
static float ILerpClamp(float a, float b, float f)
Definition MathUtils.h:203
static bool CheckMinMax(int min, int max, int value)
Definition MathUtils.h:90
static float AngleDelta(float angle1, float angle2)
Definition MathUtils.h:216
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
double norm(const Point &a)
Definition point.hpp:102