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 2012 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  template<typename T>
93  inline T mean(const std::vector<T> &_values)
94  {
95  T sum = 0;
96  for (unsigned int i = 0; i < _values.size(); ++i)
97  sum += _values[i];
98  return sum / _values.size();
99  }
100 
104  template<typename T>
105  inline T variance(const std::vector<T> &_values)
106  {
107  T avg = mean<T>(_values);
108 
109  T sum = 0;
110  for (unsigned int i = 0; i < _values.size(); ++i)
111  sum += (_values[i] - avg) * (_values[i] - avg);
112  return sum / _values.size();
113  }
114 
118  template<typename T>
119  inline T max(const std::vector<T> &_values)
120  {
122  for (unsigned int i = 0; i < _values.size(); ++i)
123  if (_values[i] > max)
124  max = _values[i];
125  return max;
126  }
127 
131  template<typename T>
132  inline T min(const std::vector<T> &_values)
133  {
135  for (unsigned int i = 0; i < _values.size(); ++i)
136  if (_values[i] < min)
137  min = _values[i];
138  return min;
139  }
140 
145  template<typename T>
146  inline bool equal(const T &_a, const T &_b,
147  const T &_epsilon = 1e-6)
148  {
149  return std::fabs(_a - _b) <= _epsilon;
150  }
151 
156  template<typename T>
157  inline T precision(const T &_a, const unsigned int &_precision)
158  {
159  return boost::math::round(_a * pow(10, _precision)) / pow(10, _precision);
160  }
161 
165  inline bool isPowerOfTwo(unsigned int _x)
166  {
167  return ((_x != 0) && ((_x & (~_x + 1)) == _x));
168  }
169 
173  inline int parseInt(const std::string& _input)
174  {
175  const char *p = _input.c_str();
176  if (!*p || *p == '?')
177  return NAN_I;
178 
179  int s = 1;
180  while (*p == ' ')
181  p++;
182 
183  if (*p == '-')
184  {
185  s = -1;
186  p++;
187  }
188 
189  double acc = 0;
190  while (*p >= '0' && *p <= '9')
191  acc = acc * 10 + *p++ - '0';
192 
193  if (*p)
194  {
195  std::cerr << "Invalid int numeric format[" << _input << "]\n";
196  return 0.0;
197  }
198 
199  return s * acc;
200  }
201 
206  inline double parseFloat(const std::string& _input)
207  {
208  const char *p = _input.c_str();
209  if (!*p || *p == '?')
210  return NAN_D;
211  int s = 1;
212  while (*p == ' ')
213  p++;
214 
215  if (*p == '-')
216  {
217  s = -1;
218  p++;
219  }
220 
221  double acc = 0;
222  while (*p >= '0' && *p <= '9')
223  acc = acc * 10 + *p++ - '0';
224 
225  if (*p == '.')
226  {
227  double k = 0.1;
228  p++;
229  while (*p >= '0' && *p <= '9')
230  {
231  acc += (*p++ - '0') * k;
232  k *= 0.1;
233  }
234  }
235  if (*p == 'e')
236  {
237  int es = 1;
238  int f = 0;
239  p++;
240  if (*p == '-')
241  {
242  es = -1;
243  p++;
244  }
245  else if (*p == '+')
246  {
247  es = 1;
248  p++;
249  }
250  while (*p >= '0' && *p <= '9')
251  f = f * 10 + *p++ - '0';
252 
253  acc *= pow(10, f*es);
254  }
255 
256  if (*p)
257  {
258  std::cerr << "Invalid double numeric format[" << _input << "]\n";
259  return 0.0;
260  }
261  return s * acc;
262  }
264  }
265 }
266 #endif