All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Helpers.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef _IGNITION_MATH_FUNCTIONS_HH_
18 #define _IGNITION_MATH_FUNCTIONS_HH_
19 
20 #define _USE_MATH_DEFINES
21 #include <cmath>
22 #include <algorithm>
23 #include <limits>
24 #include <string>
25 #include <iostream>
26 #include <vector>
27 
29 #define IGN_DBL_MAX std::numeric_limits<double>::max()
30 
32 #define IGN_DBL_MIN std::numeric_limits<double>::min()
33 
35 #define IGN_FLT_MAX std::numeric_limits<float>::max()
36 
38 #define IGN_FLT_MIN std::numeric_limits<float>::min()
39 
41 #define IGN_UINT32_MAX std::numeric_limits<uint32_t>::max()
42 
44 #define IGN_UINT32_MIN std::numeric_limits<uint32_t>::min()
45 
47 #define IGN_INT32_MAX std::numeric_limits<int32_t>::max()
48 
50 #define IGN_INT32_MIN std::numeric_limits<int32_t>::min()
51 
54 #ifdef M_PI
55 #define IGN_PI M_PI
56 #define IGN_PI_2 M_PI_2
57 #define IGN_PI_4 M_PI_4
58 #else
59 #define IGN_PI 3.14159265358979323846
60 #define IGN_PI_2 1.57079632679489661923
61 #define IGN_PI_4 0.78539816339744830962
62 #endif
63 
64 namespace ignition
65 {
67  namespace math
68  {
70  static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
71 
73  static const float NAN_F = std::numeric_limits<float>::quiet_NaN();
74 
76  static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
77 
82  template<typename T>
83  inline T clamp(T _v, T _min, T _max)
84  {
85  return std::max(std::min(_v, _max), _min);
86  }
87 
91  inline bool isnan(float _v)
92  {
93  return (std::isnan)(_v);
94  }
95 
99  inline bool isnan(double _v)
100  {
101  return (std::isnan)(_v);
102  }
103 
107  inline float fixnan(float _v)
108  {
109  return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
110  }
111 
115  inline double fixnan(double _v)
116  {
117  return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
118  }
119 
123  template<typename T>
124  inline T mean(const std::vector<T> &_values)
125  {
126  T sum = 0;
127  for (unsigned int i = 0; i < _values.size(); ++i)
128  sum += _values[i];
129  return sum / _values.size();
130  }
131 
135  template<typename T>
136  inline T variance(const std::vector<T> &_values)
137  {
138  T avg = mean<T>(_values);
139 
140  T sum = 0;
141  for (unsigned int i = 0; i < _values.size(); ++i)
142  sum += (_values[i] - avg) * (_values[i] - avg);
143  return sum / _values.size();
144  }
145 
149  template<typename T>
150  inline T max(const std::vector<T> &_values)
151  {
153  for (unsigned int i = 0; i < _values.size(); ++i)
154  if (_values[i] > max)
155  max = _values[i];
156  return max;
157  }
158 
162  template<typename T>
163  inline T min(const std::vector<T> &_values)
164  {
166  for (unsigned int i = 0; i < _values.size(); ++i)
167  if (_values[i] < min)
168  min = _values[i];
169  return min;
170  }
171 
176  template<typename T>
177  inline bool equal(const T &_a, const T &_b,
178  const T &_epsilon = 1e-6)
179  {
180  return std::abs(_a - _b) <= _epsilon;
181  }
182 
187  template<typename T>
188  inline T precision(const T &_a, const unsigned int &_precision)
189  {
190  return std::round(_a * pow(10, _precision)) / pow(10, _precision);
191  }
192 
196  inline bool isPowerOfTwo(unsigned int _x)
197  {
198  return ((_x != 0) && ((_x & (~_x + 1)) == _x));
199  }
200 
206  inline unsigned int roundUpPowerOfTwo(unsigned int _x)
207  {
208  if (_x == 0)
209  return 1;
210 
211  if (isPowerOfTwo(_x))
212  return _x;
213 
214  while (_x & (_x - 1))
215  _x = _x & (_x - 1);
216 
217  _x = _x << 1;
218 
219  return _x;
220  }
221 
225  inline int parseInt(const std::string &_input)
226  {
227  const char *p = _input.c_str();
228  if (!*p || *p == '?')
229  return NAN_I;
230 
231  int s = 1;
232  while (*p == ' ')
233  p++;
234 
235  if (*p == '-')
236  {
237  s = -1;
238  p++;
239  }
240 
241  double acc = 0;
242  while (*p >= '0' && *p <= '9')
243  acc = acc * 10 + *p++ - '0';
244 
245  if (*p)
246  {
247  std::cerr << "Invalid int numeric format[" << _input << "]\n";
248  return NAN_I;
249  }
250 
251  return static_cast<int>(s * acc);
252  }
253 
258  inline double parseFloat(const std::string &_input)
259  {
260  const char *p = _input.c_str();
261  if (!*p || *p == '?')
262  return NAN_D;
263  int s = 1;
264  while (*p == ' ')
265  p++;
266 
267  if (*p == '-')
268  {
269  s = -1;
270  p++;
271  }
272 
273  double acc = 0;
274  while (*p >= '0' && *p <= '9')
275  acc = acc * 10 + *p++ - '0';
276 
277  if (*p == '.')
278  {
279  double k = 0.1;
280  p++;
281  while (*p >= '0' && *p <= '9')
282  {
283  acc += (*p++ - '0') * k;
284  k *= 0.1;
285  }
286  }
287  if (*p == 'e')
288  {
289  int es = 1;
290  int f = 0;
291  p++;
292  if (*p == '-')
293  {
294  es = -1;
295  p++;
296  }
297  else if (*p == '+')
298  {
299  es = 1;
300  p++;
301  }
302  while (*p >= '0' && *p <= '9')
303  f = f * 10 + *p++ - '0';
304 
305  acc *= pow(10, f*es);
306  }
307 
308  if (*p)
309  {
310  std::cerr << "Invalid double numeric format[" << _input << "]\n";
311  return NAN_D;
312  }
313  return s * acc;
314  }
315  }
316 }
317 
326 #if defined _WIN32 || defined __CYGWIN__
327  #ifdef BUILDING_DLL
328  #ifdef __GNUC__
329  #define IGNITION_VISIBLE __attribute__ ((dllexport))
330  #else
331  #define IGNITION_VISIBLE __declspec(dllexport)
332  #endif
333  #else
334  #ifdef __GNUC__
335  #define IGNITION_VISIBLE __attribute__ ((dllimport))
336  #else
337  #define IGNITION_VISIBLE __declspec(dllimport)
338  #endif
339  #endif
340  #define IGNITION_HIDDEN
341 #else
342  #if __GNUC__ >= 4
343  #define IGNITION_VISIBLE __attribute__ ((visibility ("default")))
344  #define IGNITION_HIDDEN __attribute__ ((visibility ("hidden")))
345  #else
346  #define IGNITION_VISIBLE
347  #define IGNITION_HIDDEN
348  #endif
349 #endif
350 
351 #endif
static const float NAN_F
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:73
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:70
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:188
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:124
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:91
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:206
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:150
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:136
bool isPowerOfTwo(unsigned int _x)
Is this a power of 2?
Definition: Helpers.hh:196
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:76
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:258
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Helpers.hh:177
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:107
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:225
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:163
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:83
bool isnan(double _v)
check if a double is NaN
Definition: Helpers.hh:99