All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 _GAZEBO_MATH_FUNCTIONS_HH_
18 #define _GAZEBO_MATH_FUNCTIONS_HH_
19 
20 #include <boost/math/special_functions/fpclassify.hpp>
21 #include <boost/math/special_functions/round.hpp>
22 #include <algorithm>
23 #include <cmath>
24 #include <limits>
25 #include <string>
26 #include <iostream>
27 #include <vector>
28 
30 #define GZ_DBL_MAX std::numeric_limits<double>::max()
31 
33 #define GZ_DBL_MIN std::numeric_limits<double>::min()
34 
36 #define GZ_FLT_MAX std::numeric_limits<float>::max()
37 
39 #define GZ_FLT_MIN std::numeric_limits<float>::min()
40 
42 #define GZ_UINT32_MAX std::numeric_limits<uint32_t>::max()
43 
45 #define GZ_UINT32_MIN std::numeric_limits<uint32_t>::min()
46 
47 
48 namespace gazebo
49 {
50  namespace math
51  {
56 
58  static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
59 
61  static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
62 
67  template<typename T>
68  inline T clamp(T _v, T _min, T _max)
69  {
70  return std::max(std::min(_v, _max), _min);
71  }
72 
76  inline bool isnan(float _v)
77  {
78  return (boost::math::isnan)(_v);
79  }
80 
84  inline bool isnan(double _v)
85  {
86  return (boost::math::isnan)(_v);
87  }
88 
92  inline float fixnan(float _v)
93  {
94  return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
95  }
96 
100  inline double fixnan(double _v)
101  {
102  return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
103  }
104 
108  template<typename T>
109  inline T mean(const std::vector<T> &_values)
110  {
111  T sum = 0;
112  for (unsigned int i = 0; i < _values.size(); ++i)
113  sum += _values[i];
114  return sum / _values.size();
115  }
116 
120  template<typename T>
121  inline T variance(const std::vector<T> &_values)
122  {
123  T avg = mean<T>(_values);
124 
125  T sum = 0;
126  for (unsigned int i = 0; i < _values.size(); ++i)
127  sum += (_values[i] - avg) * (_values[i] - avg);
128  return sum / _values.size();
129  }
130 
134  template<typename T>
135  inline T max(const std::vector<T> &_values)
136  {
138  for (unsigned int i = 0; i < _values.size(); ++i)
139  if (_values[i] > max)
140  max = _values[i];
141  return max;
142  }
143 
147  template<typename T>
148  inline T min(const std::vector<T> &_values)
149  {
151  for (unsigned int i = 0; i < _values.size(); ++i)
152  if (_values[i] < min)
153  min = _values[i];
154  return min;
155  }
156 
161  template<typename T>
162  inline bool equal(const T &_a, const T &_b,
163  const T &_epsilon = 1e-6)
164  {
165  return std::fabs(_a - _b) <= _epsilon;
166  }
167 
172  template<typename T>
173  inline T precision(const T &_a, const unsigned int &_precision)
174  {
175  return boost::math::round(_a * pow(10, _precision)) / pow(10, _precision);
176  }
177 
181  inline bool isPowerOfTwo(unsigned int _x)
182  {
183  return ((_x != 0) && ((_x & (~_x + 1)) == _x));
184  }
185 
189  inline int parseInt(const std::string& _input)
190  {
191  const char *p = _input.c_str();
192  if (!*p || *p == '?')
193  return NAN_I;
194 
195  int s = 1;
196  while (*p == ' ')
197  p++;
198 
199  if (*p == '-')
200  {
201  s = -1;
202  p++;
203  }
204 
205  double acc = 0;
206  while (*p >= '0' && *p <= '9')
207  acc = acc * 10 + *p++ - '0';
208 
209  if (*p)
210  {
211  std::cerr << "Invalid int numeric format[" << _input << "]\n";
212  return 0.0;
213  }
214 
215  return s * acc;
216  }
217 
222  inline double parseFloat(const std::string& _input)
223  {
224  const char *p = _input.c_str();
225  if (!*p || *p == '?')
226  return NAN_D;
227  int s = 1;
228  while (*p == ' ')
229  p++;
230 
231  if (*p == '-')
232  {
233  s = -1;
234  p++;
235  }
236 
237  double acc = 0;
238  while (*p >= '0' && *p <= '9')
239  acc = acc * 10 + *p++ - '0';
240 
241  if (*p == '.')
242  {
243  double k = 0.1;
244  p++;
245  while (*p >= '0' && *p <= '9')
246  {
247  acc += (*p++ - '0') * k;
248  k *= 0.1;
249  }
250  }
251  if (*p == 'e')
252  {
253  int es = 1;
254  int f = 0;
255  p++;
256  if (*p == '-')
257  {
258  es = -1;
259  p++;
260  }
261  else if (*p == '+')
262  {
263  es = 1;
264  p++;
265  }
266  while (*p >= '0' && *p <= '9')
267  f = f * 10 + *p++ - '0';
268 
269  acc *= pow(10, f*es);
270  }
271 
272  if (*p)
273  {
274  std::cerr << "Invalid double numeric format[" << _input << "]\n";
275  return 0.0;
276  }
277  return s * acc;
278  }
280  }
281 }
282 #endif