mathutils.h
1 /********************************************************************************
2  * FARSA Utilities Library *
3  * Copyright (C) 2007-2012 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21  ********************************************************************************/
22 
23 #ifndef MATHUTILS_H
24 #define MATHUTILS_H
25 
26 #include "utilitiesconfig.h"
27 
28 #include <QDebug>
29 #include <QtGlobal>
30 #include <QMetaType>
31 #include <cmath>
32 
33 // On Windows we need the float.h header to use isnan and similar functions
34 #ifdef WIN32
35  #include <float.h>
36 #endif
37 
38 namespace farsa {
39 
45 typedef float real;
46 
47 #define PI_GRECO 3.14159265358979323846f
48 
60 inline FARSA_UTIL_TEMPLATE real toRad(real x)
61 {
62  return x * PI_GRECO / 180.0f;
63 }
64 
72 inline FARSA_UTIL_TEMPLATE real normalizeRad02pi(real x)
73 {
74  return x - (floor(x / (2.0 * PI_GRECO)) * 2.0 * PI_GRECO);
75 }
76 
84 inline FARSA_UTIL_TEMPLATE real normalizeRad(real x)
85 {
86  const real n = normalizeRad02pi(x);
87  return (n > PI_GRECO) ? (n - 2.0 * PI_GRECO) : n;
88 }
89 
97 inline FARSA_UTIL_TEMPLATE real toDegree(real x)
98 {
99  return x * 180.0f / PI_GRECO;
100 }
101 
109 inline FARSA_UTIL_TEMPLATE real normalizeDegree0360(real x)
110 {
111  return x - (floor(x / 360.0) * 360.0);
112 }
113 
121 inline FARSA_UTIL_TEMPLATE real normalizeDegree(real x)
122 {
123  const real n = normalizeDegree0360(x);
124  return (n > 180.0) ? (n - 360.0) : n;
125 }
126 
135 template<class T, class U>
136 FARSA_UTIL_TEMPLATE const T min(const T& t1, const U& t2)
137 {
138  if (t1 < t2) {
139  return t1;
140  } else {
141  return (T)t2;
142  }
143 }
144 
153 template<class T, class U>
154 FARSA_UTIL_TEMPLATE const T max(const T& t1, const U& t2)
155 {
156  if (t1 > t2) {
157  return t1;
158  } else {
159  return (T)t2;
160  }
161 }
162 
173 inline FARSA_UTIL_TEMPLATE real ramp(real minv, real maxv, real value)
174 {
175  if (value > maxv) {
176  return maxv;
177  } else if (value < minv) {
178  return minv;
179  } else {
180  return value;
181  }
182 }
183 
201 inline FARSA_UTIL_TEMPLATE float linearMap(float x, float min = -10, float max = 10, float outMin = -1, float outMax = 1)
202 {
203  float m = ( outMax-outMin )/( max-min );
204  float q = outMin - m*min;
205  float ret = m*x+q;
206  if (ret < outMin) {
207  return outMin;
208  } else if (ret > outMax) {
209  return outMax;
210  } else {
211  return ret;
212  }
213 }
214 
232 inline FARSA_UTIL_TEMPLATE float invLinearMap(float x, float min = -10, float max = 10, float outMin = -1, float outMax = 1)
233 {
234  double m = -( outMax-outMin )/( max-min );
235  double q = outMax - m*min;
236  double ret = m*x+q;
237  if (ret < outMin) {
238  return outMin;
239  } else if (ret > outMax) {
240  return outMax;
241  } else {
242  return ret;
243  }
244 }
245 
251 #ifdef WIN32
252  #define isnan(x) _isnan(x)
253  #define isinf(x) (!_finite(x))
254 #else
255  #define isnan(x) std::isnan(x)
256  #define isinf(x) std::isinf(x)
257 #endif
258 
265 #define FARSA_TEST_NAN(__V__) if (isnan(__V__)) { qFatal("Variable " #__V__ " has and invalid value: NaN in %s at line %d", __FILE__, __LINE__); }
266 
273 #define FARSA_TEST_INF(__V__) if (isinf(__V__)) { qFatal("Variable " #__V__ " has and invalid value: Infinite in %s at line %d", __FILE__, __LINE__); }
274 
281 #define FARSA_TEST_INVALID(__V__) if (isnan(__V__)) { qFatal("Variable " #__V__ " has and invalid value: NaN in %s at line %d", __FILE__, __LINE__); } else if (isinf(__V__)) { qFatal("Variable " #__V__ " has and invalid value: Infinite in %s at line %d", __FILE__, __LINE__); }
282 
288 #ifdef FARSA_DEBUG
289  #define FARSA_DEBUG_TEST_NAN(__V__) FARSA_TEST_NAN(__V__)
290 #else
291  #define FARSA_DEBUG_TEST_NAN(__V__)
292 #endif
293 
299 #ifdef FARSA_DEBUG
300  #define FARSA_DEBUG_TEST_INF(__V__) FARSA_TEST_INF(__V__)
301 #else
302  #define FARSA_DEBUG_TEST_INF(__V__)
303 #endif
304 
310 #ifdef FARSA_DEBUG
311  #define FARSA_DEBUG_TEST_INVALID(__V__) FARSA_TEST_INVALID(__V__)
312 #else
313  #define FARSA_DEBUG_TEST_INVALID(__V__)
314 #endif
315 
316 } // end namespace farsa
317 
318 #endif
This file contains the common type defitions used on the whole framework.
FARSA_UTIL_TEMPLATE real toRad(real x)
Converts degrees to radians.
Definition: mathutils.h:60
FARSA_UTIL_TEMPLATE float linearMap(float x, float min=-10, float max=10, float outMin=-1, float outMax=1)
Linear mapping from [min,max] to [outMin, outMax].
Definition: mathutils.h:201
FARSA_UTIL_TEMPLATE real normalizeRad(real x)
Restricts the angle between -PI_GRECO and PI_GRECO.
Definition: mathutils.h:84
FARSA_UTIL_TEMPLATE real ramp(real minv, real maxv, real value)
Clamps the value of a variable in the given range.
Definition: mathutils.h:173
FARSA_UTIL_TEMPLATE real normalizeDegree0360(real x)
Restricts the angle between 0.0° and 360°
Definition: mathutils.h:109
FARSA_UTIL_TEMPLATE real normalizeRad02pi(real x)
Restricts the angle between 0.0 and 2.0 * PI_GRECO.
Definition: mathutils.h:72
A macro to deprecate functions.
FARSA_UTIL_TEMPLATE float invLinearMap(float x, float min=-10, float max=10, float outMin=-1, float outMax=1)
Inversed Linear mapping from [min,max] to [outMin, outMax].
Definition: mathutils.h:232
FARSA_UTIL_TEMPLATE const T max(const T &t1, const U &t2)
Template for max calculation.
Definition: mathutils.h:154
FARSA_UTIL_TEMPLATE real toDegree(real x)
Converts radians to degrees.
Definition: mathutils.h:97
float real
Abstraction on the type of real numbers.
Definition: mathutils.h:45
FARSA_UTIL_TEMPLATE const T min(const T &t1, const U &t2)
Template for min calculation.
Definition: mathutils.h:136
FARSA_UTIL_TEMPLATE real normalizeDegree(real x)
Restricts the angle between -180° and 180°
Definition: mathutils.h:121