Gazebo Math

API Reference

6.15.1
gz/math/Helpers.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 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 GZ_MATH_FUNCTIONS_HH_
18 #define GZ_MATH_FUNCTIONS_HH_
19 
20 #include <algorithm>
21 #include <chrono>
22 #include <cmath>
23 #include <cstdint>
24 #include <iomanip>
25 #include <iostream>
26 #include <limits>
27 #include <regex>
28 #include <sstream>
29 #include <string>
30 #include <tuple>
31 #include <utility>
32 #include <vector>
33 
34 #include <gz/math/config.hh>
35 #include "gz/math/Export.hh"
36 
39 template <typename T>
41 
44 #define IGN_DBL_MAX gz::math::DPRCT_MAX_D
45 
48 #define IGN_DBL_MIN gz::math::DPRCT_MIN_D
49 
52 #define IGN_DBL_LOW gz::math::DPRCT_LOW_D
53 
56 #define IGN_DBL_INF gz::math::DPRCT_INF_D
57 
60 #define IGN_FLT_MAX gz::math::DPRCT_MAX_F
61 
64 #define IGN_FLT_MIN gz::math::DPRCT_MIN_F
65 
68 #define IGN_FLT_LOW gz::math::DPRCT_LOW_F
69 
72 #define IGN_FLT_INF gz::math::DPRCT_INF_F
73 
76 #define IGN_UINT16_MAX gz::math::DPRCT_MAX_UI16
77 
80 #define IGN_UINT16_MIN gz::math::DPRCT_MIN_UI16
81 
85 #define IGN_UINT16_LOW gz::math::DPRCT_LOW_UI16
86 
89 #define IGN_UINT16_INF gz::math::DPRCT_INF_UI16
90 
93 #define IGN_INT16_MAX gz::math::DPRCT_MAX_I16
94 
97 #define IGN_INT16_MIN gz::math::DPRCT_MIN_I16
98 
102 #define IGN_INT16_LOW gz::math::DPRCT_LOW_I16
103 
106 #define IGN_INT16_INF gz::math::DPRCT_INF_I16
107 
110 #define IGN_UINT32_MAX gz::math::DPRCT_MAX_UI32
111 
114 #define IGN_UINT32_MIN gz::math::DPRCT_MIN_UI32
115 
119 #define IGN_UINT32_LOW gz::math::DPRCT_LOW_UI32
120 
123 #define IGN_UINT32_INF gz::math::DPRCT_INF_UI32
124 
127 #define IGN_INT32_MAX gz::math::DPRCT_MAX_I32
128 
131 #define IGN_INT32_MIN gz::math::DPRCT_MIN_I32
132 
136 #define IGN_INT32_LOW gz::math::DPRCT_LOW_I32
137 
140 #define IGN_INT32_INF gz::math::DPRCT_INF_I32
141 
144 #define IGN_UINT64_MAX gz::math::DPRCT_MAX_UI64
145 
148 #define IGN_UINT64_MIN gz::math::DPRCT_MIN_UI64
149 
153 #define IGN_UINT64_LOW gz::math::DPRCT_LOW_UI64
154 
157 #define IGN_UINT64_INF gz::math::DPRCT_INF_UI64
158 
161 #define IGN_INT64_MAX gz::math::DPRCT_MAX_I64
162 
165 #define IGN_INT64_MIN gz::math::DPRCT_MIN_I64
166 
170 #define IGN_INT64_LOW gz::math::DPRCT_LOW_I64
171 
174 #define IGN_INT64_INF gz::math::DPRCT_INF_I64
175 
178 #ifdef M_PI
179 #define IGN_PI M_PI
180 #define IGN_PI_2 M_PI_2
181 #define IGN_PI_4 M_PI_4
182 #define IGN_SQRT2 M_SQRT2
183 #else
184 #define IGN_PI 3.14159265358979323846
185 #define IGN_PI_2 1.57079632679489661923
186 #define IGN_PI_4 0.78539816339744830962
187 #define IGN_SQRT2 1.41421356237309504880
188 #endif
189 
193 #if defined __FLT_EVAL_METHOD__ && __FLT_EVAL_METHOD__ == 2
194 #define IGN_FP_VOLATILE volatile
195 #else
196 #define IGN_FP_VOLATILE
197 #endif
198 
201 #define IGN_SPHERE_VOLUME(_radius) (4.0*IGN_PI*std::pow(_radius, 3)/3.0)
202 
206 #define IGN_CYLINDER_VOLUME(_r, _l) (_l * IGN_PI * std::pow(_r, 2))
207 
212 #define IGN_BOX_VOLUME(_x, _y, _z) (_x *_y * _z)
213 
216 #define IGN_BOX_VOLUME_V(_v) (_v.X() *_v.Y() * _v.Z())
217 
218 namespace ignition
219 {
221  namespace math
222  {
223  // Inline bracket to help doxygen filtering.
224  inline namespace IGNITION_MATH_VERSION_NAMESPACE {
225  //
227  static const size_t IGN_ZERO_SIZE_T = 0u;
228 
230  static const size_t IGN_ONE_SIZE_T = 1u;
231 
233  static const size_t IGN_TWO_SIZE_T = 2u;
234 
236  static const size_t IGN_THREE_SIZE_T = 3u;
237 
239  static const size_t IGN_FOUR_SIZE_T = 4u;
240 
242  static const size_t IGN_FIVE_SIZE_T = 5u;
243 
245  static const size_t IGN_SIX_SIZE_T = 6u;
246 
248  static const size_t IGN_SEVEN_SIZE_T = 7u;
249 
251  static const size_t IGN_EIGHT_SIZE_T = 8u;
252 
254  static const size_t IGN_NINE_SIZE_T = 9u;
255 
257  static const double MAX_D = std::numeric_limits<double>::max();
258 
260  static const double MIN_D = std::numeric_limits<double>::min();
261 
264 
267 
270 
272  static const float MAX_F = std::numeric_limits<float>::max();
273 
275  static const float MIN_F = std::numeric_limits<float>::min();
276 
279 
282 
285 
287  static const uint16_t MAX_UI16 = std::numeric_limits<uint16_t>::max();
288 
290  static const uint16_t MIN_UI16 = std::numeric_limits<uint16_t>::min();
291 
295 
298 
300  static const int16_t MAX_I16 = std::numeric_limits<int16_t>::max();
301 
303  static const int16_t MIN_I16 = std::numeric_limits<int16_t>::min();
304 
308 
311 
313  static const uint32_t MAX_UI32 = std::numeric_limits<uint32_t>::max();
314 
316  static const uint32_t MIN_UI32 = std::numeric_limits<uint32_t>::min();
317 
321 
324 
326  static const int32_t MAX_I32 = std::numeric_limits<int32_t>::max();
327 
329  static const int32_t MIN_I32 = std::numeric_limits<int32_t>::min();
330 
334 
337 
339  static const uint64_t MAX_UI64 = std::numeric_limits<uint64_t>::max();
340 
342  static const uint64_t MIN_UI64 = std::numeric_limits<uint64_t>::min();
343 
347 
350 
352  static const int64_t MAX_I64 = std::numeric_limits<int64_t>::max();
353 
355  static const int64_t MIN_I64 = std::numeric_limits<int64_t>::min();
356 
360 
363 
366 
367  // variables created to deprecate macros in this file
368  static const double IGN_DEPRECATED(3) DPRCT_MAX_D = MAX_D;
369  static const double IGN_DEPRECATED(3) DPRCT_MIN_D = MIN_D;
370  static const double IGN_DEPRECATED(3) DPRCT_LOW_D = LOW_D;
371  static const double IGN_DEPRECATED(3) DPRCT_INF_D = INF_D;
372  static const float IGN_DEPRECATED(3) DPRCT_MAX_F = MAX_F;
373  static const float IGN_DEPRECATED(3) DPRCT_MIN_F = MIN_F;
374  static const float IGN_DEPRECATED(3) DPRCT_LOW_F = LOW_F;
375  static const float IGN_DEPRECATED(3) DPRCT_INF_F = INF_F;
376  static const uint16_t IGN_DEPRECATED(3) DPRCT_MAX_UI16 = MAX_UI16;
377  static const uint16_t IGN_DEPRECATED(3) DPRCT_MIN_UI16 = MIN_UI16;
378  static const uint16_t IGN_DEPRECATED(3) DPRCT_LOW_UI16 = LOW_UI16;
379  static const uint16_t IGN_DEPRECATED(3) DPRCT_INF_UI16 = INF_UI16;
380  static const int16_t IGN_DEPRECATED(3) DPRCT_MAX_I16 = MAX_I16;
381  static const int16_t IGN_DEPRECATED(3) DPRCT_MIN_I16 = MIN_I16;
382  static const int16_t IGN_DEPRECATED(3) DPRCT_LOW_I16 = LOW_I16;
383  static const int16_t IGN_DEPRECATED(3) DPRCT_INF_I16 = INF_I16;
384  static const uint32_t IGN_DEPRECATED(3) DPRCT_MAX_UI32 = MAX_UI32;
385  static const uint32_t IGN_DEPRECATED(3) DPRCT_MIN_UI32 = MIN_UI32;
386  static const uint32_t IGN_DEPRECATED(3) DPRCT_LOW_UI32 = LOW_UI32;
387  static const uint32_t IGN_DEPRECATED(3) DPRCT_INF_UI32 = INF_UI32;
388  static const int32_t IGN_DEPRECATED(3) DPRCT_MAX_I32 = MAX_I32;
389  static const int32_t IGN_DEPRECATED(3) DPRCT_MIN_I32 = MIN_I32;
390  static const int32_t IGN_DEPRECATED(3) DPRCT_LOW_I32 = LOW_I32;
391  static const int32_t IGN_DEPRECATED(3) DPRCT_INF_I32 = INF_I32;
392  static const uint64_t IGN_DEPRECATED(3) DPRCT_MAX_UI64 = MAX_UI64;
393  static const uint64_t IGN_DEPRECATED(3) DPRCT_MIN_UI64 = MIN_UI64;
394  static const uint64_t IGN_DEPRECATED(3) DPRCT_LOW_UI64 = LOW_UI64;
395  static const uint64_t IGN_DEPRECATED(3) DPRCT_INF_UI64 = INF_UI64;
396  static const int64_t IGN_DEPRECATED(3) DPRCT_MAX_I64 = MAX_I64;
397  static const int64_t IGN_DEPRECATED(3) DPRCT_MIN_I64 = MIN_I64;
398  static const int64_t IGN_DEPRECATED(3) DPRCT_LOW_I64 = LOW_I64;
399  static const int64_t IGN_DEPRECATED(3) DPRCT_INF_I64 = INF_I64;
400 
405  template<typename T>
406  inline T clamp(T _v, T _min, T _max)
407  {
408  return std::max(std::min(_v, _max), _min);
409  }
410 
414  inline bool isnan(float _v)
415  {
416  return (std::isnan)(_v);
417  }
418 
422  inline bool isnan(double _v)
423  {
424  return (std::isnan)(_v);
425  }
426 
430  inline float fixnan(float _v)
431  {
432  return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
433  }
434 
438  inline double fixnan(double _v)
439  {
440  return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
441  }
442 
446  inline bool isEven(const int _v)
447  {
448  return !(_v % 2);
449  }
450 
454  inline bool isEven(const unsigned int _v)
455  {
456  return !(_v % 2);
457  }
458 
462  inline bool isOdd(const int _v)
463  {
464  return (_v % 2) != 0;
465  }
466 
470  inline bool isOdd(const unsigned int _v)
471  {
472  return (_v % 2) != 0;
473  }
474 
481  template<typename T>
482  inline int sgn(T _value)
483  {
484  return (T(0) < _value) - (_value < T(0));
485  }
486 
493  template<typename T>
494  inline int signum(T _value)
495  {
496  return sgn(_value);
497  }
498 
502  template<typename T>
503  inline T mean(const std::vector<T> &_values)
504  {
505  T sum = 0;
506  for (unsigned int i = 0; i < _values.size(); ++i)
507  sum += _values[i];
508  return sum / _values.size();
509  }
510 
514  template<typename T>
515  inline T variance(const std::vector<T> &_values)
516  {
517  T avg = mean<T>(_values);
518 
519  T sum = 0;
520  for (unsigned int i = 0; i < _values.size(); ++i)
521  sum += (_values[i] - avg) * (_values[i] - avg);
522  return sum / _values.size();
523  }
524 
528  template<typename T>
529  inline T max(const std::vector<T> &_values)
530  {
532  for (unsigned int i = 0; i < _values.size(); ++i)
533  if (_values[i] > max)
534  max = _values[i];
535  return max;
536  }
537 
541  template<typename T>
542  inline T min(const std::vector<T> &_values)
543  {
545  for (unsigned int i = 0; i < _values.size(); ++i)
546  if (_values[i] < min)
547  min = _values[i];
548  return min;
549  }
550 
555  template<typename T>
556  inline bool equal(const T &_a, const T &_b,
557  const T &_epsilon = T(1e-6))
558  {
559  IGN_FP_VOLATILE T diff = std::abs(_a - _b);
560  return diff <= _epsilon;
561  }
562 
567  template<typename T>
568  inline bool lessOrNearEqual(const T &_a, const T &_b,
569  const T &_epsilon = 1e-6)
570  {
571  return _a < _b + _epsilon;
572  }
573 
578  template<typename T>
579  inline bool greaterOrNearEqual(const T &_a, const T &_b,
580  const T &_epsilon = 1e-6)
581  {
582  return _a > _b - _epsilon;
583  }
584 
589  template<typename T>
590  inline T precision(const T &_a, const unsigned int &_precision)
591  {
592  auto p = std::pow(10, _precision);
593  return static_cast<T>(std::round(_a * p) / p);
594  }
595 
599  template<typename T>
600  inline void sort2(T &_a, T &_b)
601  {
602  using std::swap;
603  if (_b < _a)
604  swap(_a, _b);
605  }
606 
611  template<typename T>
612  inline void sort3(T &_a, T &_b, T &_c)
613  {
614  // _a <= _b
615  sort2(_a, _b);
616  // _a <= _c, _b <= _c
617  sort2(_b, _c);
618  // _a <= _b <= _c
619  sort2(_a, _b);
620  }
621 
625  template<typename T>
626  inline void appendToStream(std::ostream &_out, T _number)
627  {
628  if (std::fpclassify(_number) == FP_ZERO)
629  {
630  _out << 0;
631  }
632  else
633  {
634  _out << _number;
635  }
636  }
637 
641  template<>
642  inline void appendToStream(std::ostream &_out, int _number)
643  {
644  _out << _number;
645  }
646 
650  inline bool isPowerOfTwo(unsigned int _x)
651  {
652  return ((_x != 0) && ((_x & (~_x + 1)) == _x));
653  }
654 
660  inline unsigned int roundUpPowerOfTwo(unsigned int _x)
661  {
662  if (_x == 0)
663  return 1;
664 
665  if (isPowerOfTwo(_x))
666  return _x;
667 
668  while (_x & (_x - 1))
669  _x = _x & (_x - 1);
670 
671  _x = _x << 1;
672 
673  return _x;
674  }
675 
686  inline int roundUpMultiple(int _num, int _multiple)
687  {
688  if (_multiple == 0)
689  return _num;
690 
691  int remainder = std::abs(_num) % _multiple;
692  if (remainder == 0)
693  return _num;
694 
695  if (_num < 0)
696  return -(std::abs(_num) - remainder);
697  else
698  return _num + _multiple - remainder;
699  }
700 
704  inline int parseInt(const std::string &_input)
705  {
706  // Return NAN_I if it is empty
707  if (_input.empty())
708  {
709  return NAN_I;
710  }
711  // Return 0 if it is all spaces
712  else if (_input.find_first_not_of(' ') == std::string::npos)
713  {
714  return 0;
715  }
716 
717  // Otherwise try standard library
718  try
719  {
720  return std::stoi(_input);
721  }
722  // if that fails, return NAN_I
723  catch(...)
724  {
725  return NAN_I;
726  }
727  }
728 
733  inline double parseFloat(const std::string &_input)
734  {
735  // Return NAN_D if it is empty
736  if (_input.empty())
737  {
738  return NAN_D;
739  }
740  // Return 0 if it is all spaces
741  else if (_input.find_first_not_of(' ') == std::string::npos)
742  {
743  return 0;
744  }
745 
746  // Otherwise try standard library
747  try
748  {
749  return std::stod(_input);
750  }
751  // if that fails, return NAN_D
752  catch(...)
753  {
754  return NAN_D;
755  }
756  }
757 
764  const std::chrono::steady_clock::time_point &_time)
765  {
766  auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
767  _time.time_since_epoch());
768  auto now_s = std::chrono::duration_cast<std::chrono::seconds>(
769  _time.time_since_epoch());
770  int64_t seconds = now_s.count();
771  int64_t nanoseconds = std::chrono::duration_cast
772  <std::chrono::nanoseconds>(now_ns - now_s).count();
773  return {seconds, nanoseconds};
774  }
775 
782  inline std::chrono::steady_clock::time_point secNsecToTimePoint(
783  const uint64_t &_sec, const uint64_t &_nanosec)
784  {
785  auto duration = std::chrono::seconds(_sec) + std::chrono::nanoseconds(
786  _nanosec);
787  std::chrono::steady_clock::time_point result;
789  result += duration_cast<std::chrono::steady_clock::duration>(duration);
790  return result;
791  }
792 
799  inline std::chrono::steady_clock::duration secNsecToDuration(
800  const uint64_t &_sec, const uint64_t &_nanosec)
801  {
803  _nanosec);
804  }
805 
812  const std::chrono::steady_clock::duration &_dur)
813  {
814  auto s = std::chrono::duration_cast<std::chrono::seconds>(_dur);
815  auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(_dur-s);
816  return {s.count(), ns.count()};
817  }
818 
819  // TODO(anyone): Replace this with std::chrono::days.
822 
830  template<class...Durations, class DurationIn>
831  std::tuple<Durations...> breakDownDurations(DurationIn d) {
832  std::tuple<Durations...> retval;
833  using discard = int[];
834  (void)discard{0, (void((
835  (std::get<Durations>(retval) =
836  std::chrono::duration_cast<Durations>(d)),
837  (d -= std::chrono::duration_cast<DurationIn>(
838  std::get<Durations>(retval))))), 0)...};
839  return retval;
840  }
841 
846  const std::chrono::steady_clock::time_point &_point)
847  {
848  auto duration = _point - secNsecToTimePoint(0, 0);
849  auto cleanDuration = breakDownDurations<days,
854  duration);
855  std::ostringstream output_string;
856  output_string << std::setw(2) << std::setfill('0')
857  << std::get<0>(cleanDuration).count() << " "
858  << std::setw(2) << std::setfill('0')
859  << std::get<1>(cleanDuration).count() << ":"
860  << std::setw(2) << std::setfill('0')
861  << std::get<2>(cleanDuration).count() << ":"
862  << std::setfill('0') << std::setw(6)
864  << std::get<3>(cleanDuration).count() +
865  std::get<4>(cleanDuration).count()/1000.0;
866  return output_string.str();
867  }
868 
873  const std::chrono::steady_clock::duration &_duration)
874  {
875  auto cleanDuration = breakDownDurations<days,
880  _duration);
881  std::ostringstream outputString;
882  outputString << std::setw(2) << std::setfill('0')
883  << std::get<0>(cleanDuration).count() << " "
884  << std::setw(2) << std::setfill('0')
885  << std::get<1>(cleanDuration).count() << ":"
886  << std::setw(2) << std::setfill('0')
887  << std::get<2>(cleanDuration).count() << ":"
888  << std::setfill('0') << std::setw(6)
890  << std::get<3>(cleanDuration).count() +
891  std::get<4>(cleanDuration).count()/1000.0;
892  return outputString.str();
893  }
894 
895  // The following regex takes a time string in the general format of
896  // "dd hh:mm:ss.nnn" where n is milliseconds, if just one number is
897  // provided, it is assumed to be seconds
898  static const std::regex time_regex(
899  "^([0-9]+ ){0,1}" // day:
900  // Any positive integer
901 
902  "(?:([1-9]:|[0-1][0-9]:|2[0-3]:){0,1}" // hour:
903  // 1 - 9:
904  // 01 - 19:
905  // 20 - 23:
906 
907  "([0-9]:|[0-5][0-9]:)){0,1}" // minute:
908  // 0 - 9:
909  // 00 - 59:
910 
911  "(?:([0-9]|[0-5][0-9]){0,1}" // second:
912  // 0 - 9
913  // 00 - 59
914 
915  "(\\.[0-9]{1,3}){0,1})$"); // millisecond:
916  // .0 - .9
917  // .00 - .99
918  // .000 - 0.999
919 
920 
925  inline bool isTimeString(const std::string &_timeString)
926  {
927  std::smatch matches;
928 
929  // `matches` should always be a size of 6 as there are 6 matching
930  // groups in the regex.
931  // 1. The whole regex
932  // 2. The days
933  // 3. The hours
934  // 4. The minutes
935  // 5. The seconds
936  // 6. The milliseconds
937  // Note that the space will remain in the day match, the colon
938  // will remain in the hour and minute matches, and the period will
939  // remain in the millisecond match
940  if (!std::regex_search(_timeString, matches, time_regex) ||
941  matches.size() != 6)
942  return false;
943 
944  std::string dayString = matches[1];
945 
946  // Days are the only unbounded number, so check first to see if stoi
947  // runs successfully
948  if (!dayString.empty())
949  {
950  // Erase the space
951  dayString.erase(dayString.length() - 1);
952  try
953  {
954  // We are only checking if it sucessfully parses,
955  // and are not concerned with the actual value.
956  auto day = std::stoi(dayString);
957  (void) day;
958  }
959  catch (const std::out_of_range &)
960  {
961  return false;
962  }
963  }
964 
965  return true;
966  }
967 
977  const std::string &_timeString,
978  uint64_t & numberDays, uint64_t & numberHours,
979  uint64_t & numberMinutes, uint64_t & numberSeconds,
980  uint64_t & numberMilliseconds)
981  {
982  std::smatch matches;
983 
984  // `matches` should always be a size of 6 as there are 6 matching
985  // groups in the regex.
986  // 1. The whole regex
987  // 2. The days
988  // 3. The hours
989  // 4. The minutes
990  // 5. The seconds
991  // 6. The milliseconds
992  // We can also index them as such below.
993  // Note that the space will remain in the day match, the colon
994  // will remain in the hour and minute matches, and the period will
995  // remain in the millisecond match
996  if (!std::regex_search(_timeString, matches, time_regex) ||
997  matches.size() != 6)
998  return false;
999 
1000  std::string dayString = matches[1];
1001  std::string hourString = matches[2];
1002  std::string minuteString = matches[3];
1003  std::string secondString = matches[4];
1004  std::string millisecondString = matches[5];
1005 
1006  // Days are the only unbounded number, so check first to see if stoi
1007  // runs successfully
1008  if (!dayString.empty())
1009  {
1010  // Erase the space
1011  dayString.erase(dayString.length() - 1);
1012  try
1013  {
1014  numberDays = std::stoi(dayString);
1015  }
1016  catch (const std::out_of_range &)
1017  {
1018  return false;
1019  }
1020  }
1021 
1022  if (!hourString.empty())
1023  {
1024  // Erase the colon
1025  hourString.erase(hourString.length() - 1);
1026  numberHours = std::stoi(hourString);
1027  }
1028 
1029  if (!minuteString.empty())
1030  {
1031  // Erase the colon
1032  minuteString.erase(minuteString.length() - 1);
1033  numberMinutes = std::stoi(minuteString);
1034  }
1035 
1036  if (!secondString.empty())
1037  {
1038  numberSeconds = std::stoi(secondString);
1039  }
1040 
1041  if (!millisecondString.empty())
1042  {
1043  // Erase the period
1044  millisecondString.erase(0, 1);
1045 
1046  // Multiplier because "4" = 400 ms, "04" = 40 ms, and "004" = 4 ms
1047  numberMilliseconds = std::stoi(millisecondString) *
1048  static_cast<uint64_t>(1000 / pow(10, millisecondString.length()));
1049  }
1050  return true;
1051  }
1052 
1059  inline std::chrono::steady_clock::duration stringToDuration(
1060  const std::string &_timeString)
1061  {
1062  using namespace std::chrono_literals;
1063  std::chrono::steady_clock::duration duration{
1064  std::chrono::steady_clock::duration::zero()};
1065 
1066  if (_timeString.empty())
1067  return duration;
1068 
1069  uint64_t numberDays = 0;
1070  uint64_t numberHours = 0;
1071  uint64_t numberMinutes = 0;
1072  uint64_t numberSeconds = 0;
1073  uint64_t numberMilliseconds = 0;
1074 
1075  if (!splitTimeBasedOnTimeRegex(_timeString, numberDays, numberHours,
1076  numberMinutes, numberSeconds,
1077  numberMilliseconds))
1078  {
1079  return duration;
1080  }
1081 
1082  // TODO(anyone): Replace below day conversion with std::chrono::days.
1084  duration = std::chrono::steady_clock::duration::zero();
1085  auto delta = std::chrono::milliseconds(numberMilliseconds) +
1086  std::chrono::seconds(numberSeconds) +
1087  std::chrono::minutes(numberMinutes) +
1088  std::chrono::hours(numberHours) +
1089  std::chrono::hours(24 * numberDays);
1090  duration += delta;
1091 
1092  return duration;
1093  }
1094 
1101  inline std::chrono::steady_clock::time_point stringToTimePoint(
1102  const std::string &_timeString)
1103  {
1104  using namespace std::chrono_literals;
1105  std::chrono::steady_clock::time_point timePoint{-1s};
1106 
1107  if (_timeString.empty())
1108  return timePoint;
1109 
1110  uint64_t numberDays = 0;
1111  uint64_t numberHours = 0;
1112  uint64_t numberMinutes = 0;
1113  uint64_t numberSeconds = 0;
1114  uint64_t numberMilliseconds = 0;
1115 
1116  if (!splitTimeBasedOnTimeRegex(_timeString, numberDays, numberHours,
1117  numberMinutes, numberSeconds,
1118  numberMilliseconds))
1119  {
1120  return timePoint;
1121  }
1122 
1123  // TODO(anyone): Replace below day conversion with std::chrono::days.
1125  timePoint = math::secNsecToTimePoint(0, 0);
1126  auto duration = std::chrono::milliseconds(numberMilliseconds) +
1127  std::chrono::seconds(numberSeconds) +
1128  std::chrono::minutes(numberMinutes) +
1129  std::chrono::hours(numberHours) +
1130  std::chrono::hours(24 * numberDays);
1131  timePoint += duration;
1132 
1133  return timePoint;
1134  }
1135 
1136  // Degrade precision on Windows, which cannot handle 'long double'
1137  // values properly. See the implementation of Unpair.
1138  // 32 bit ARM processors also define 'long double' to be the same
1139  // size as 'double', and must also be degraded
1140 #if defined _MSC_VER || defined __arm__
1141  using PairInput = uint16_t;
1142  using PairOutput = uint32_t;
1143 #else
1144  using PairInput = uint32_t;
1145  using PairOutput = uint64_t;
1146 #endif
1147 
1157  PairOutput IGNITION_MATH_VISIBLE Pair(
1158  const PairInput _a, const PairInput _b);
1159 
1171  std::tuple<PairInput, PairInput> IGNITION_MATH_VISIBLE Unpair(
1172  const PairOutput _key);
1173  }
1174  }
1175 }
1176 
1177 #endif
static const uint16_t DPRCT_INF_UI16
Definition: gz/math/Helpers.hh:379
static const double DPRCT_MIN_D
Definition: gz/math/Helpers.hh:369
static const size_t IGN_FIVE_SIZE_T
size_t type with a value of 5
Definition: gz/math/Helpers.hh:242
static const uint16_t LOW_UI16
16bit unsigned integer lowest value. This is equivalent to IGN_UINT16_MIN, and is defined here for co...
Definition: gz/math/Helpers.hh:294
static const float LOW_F
Float low value, equivalent to -MAX_F.
Definition: gz/math/Helpers.hh:278
static const uint64_t MAX_UI64
64bit unsigned integer maximum value
Definition: gz/math/Helpers.hh:339
T setprecision(T... args)
bool isEven(const int _v)
Check if parameter is even.
Definition: gz/math/Helpers.hh:446
static const int64_t LOW_I64
64bit unsigned integer lowest value. This is equivalent to IGN_INT64_MIN, and is defined here for com...
Definition: gz/math/Helpers.hh:359
static const int32_t INF_I32
32-bit unsigned integer positive infinite value
Definition: gz/math/Helpers.hh:336
Definition: gz/math/AdditivelySeparableScalarField3.hh:27
STL class.
void sort2(T &_a, T &_b)
Sort two numbers, such that _a <= _b.
Definition: gz/math/Helpers.hh:600
static const int32_t LOW_I32
32bit unsigned integer lowest value. This is equivalent to IGN_INT32_MIN, and is defined here for com...
Definition: gz/math/Helpers.hh:333
static const uint32_t LOW_UI32
32bit unsigned integer lowest value. This is equivalent to IGN_UINT32_MIN, and is defined here for co...
Definition: gz/math/Helpers.hh:320
int parseInt(const std::string &_input)
parse string into an integer
Definition: gz/math/Helpers.hh:704
static const size_t IGN_THREE_SIZE_T
size_t type with a value of 3
Definition: gz/math/Helpers.hh:236
std::pair< int64_t, int64_t > timePointToSecNsec(const std::chrono::steady_clock::time_point &_time)
Convert a std::chrono::steady_clock::time_point to a seconds and nanoseconds pair.
Definition: gz/math/Helpers.hh:763
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: gz/math/Helpers.hh:542
static const int64_t DPRCT_MIN_I64
Definition: gz/math/Helpers.hh:397
float fixnan(float _v)
Fix a nan value.
Definition: gz/math/Helpers.hh:430
static const uint64_t DPRCT_MAX_UI64
Definition: gz/math/Helpers.hh:392
static const uint32_t INF_UI32
32-bit unsigned integer positive infinite value
Definition: gz/math/Helpers.hh:323
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition: gz/math/Helpers.hh:556
static const uint16_t DPRCT_MIN_UI16
Definition: gz/math/Helpers.hh:377
static const uint64_t DPRCT_LOW_UI64
Definition: gz/math/Helpers.hh:394
std::chrono::steady_clock::time_point secNsecToTimePoint(const uint64_t &_sec, const uint64_t &_nanosec)
Convert seconds and nanoseconds to std::chrono::steady_clock::time_point.
Definition: gz/math/Helpers.hh:782
static const int16_t DPRCT_MAX_I16
Definition: gz/math/Helpers.hh:380
std::chrono::steady_clock::time_point stringToTimePoint(const std::string &_timeString)
Convert a string to a std::chrono::steady_clock::time_point.
Definition: gz/math/Helpers.hh:1101
std::tuple< Durations... > breakDownDurations(DurationIn d)
break down durations NOTE: the template arguments must be properly ordered according to magnitude and...
Definition: gz/math/Helpers.hh:831
T quiet_NaN(T... args)
std::tuple< PairInput, PairInput > Unpair(const PairOutput _key)
The reverse of the Pair function. Accepts a key, produced from the Pair function, and returns a tuple...
STL class.
T size(T... args)
static const uint64_t DPRCT_INF_UI64
Definition: gz/math/Helpers.hh:395
static const int32_t MAX_I32
32bit unsigned integer maximum value
Definition: gz/math/Helpers.hh:326
static const int16_t DPRCT_MIN_I16
Definition: gz/math/Helpers.hh:381
static const int64_t INF_I64
64-bit unsigned integer positive infinite value
Definition: gz/math/Helpers.hh:362
static const uint64_t LOW_UI64
64bit unsigned integer lowest value. This is equivalent to IGN_UINT64_MIN, and is defined here for co...
Definition: gz/math/Helpers.hh:346
static const int64_t DPRCT_LOW_I64
Definition: gz/math/Helpers.hh:398
static const size_t IGN_EIGHT_SIZE_T
size_t type with a value of 8
Definition: gz/math/Helpers.hh:251
T setfill(T... args)
static const double INF_D
Double positive infinite value.
Definition: gz/math/Helpers.hh:266
bool isOdd(const int _v)
Check if parameter is odd.
Definition: gz/math/Helpers.hh:462
T fpclassify(T... args)
T find_first_not_of(T... args)
std::chrono::steady_clock::duration secNsecToDuration(const uint64_t &_sec, const uint64_t &_nanosec)
Convert seconds and nanoseconds to std::chrono::steady_clock::duration.
Definition: gz/math/Helpers.hh:799
bool isPowerOfTwo(unsigned int _x)
Is this a power of 2?
Definition: gz/math/Helpers.hh:650
static const double LOW_D
Double low value, equivalent to -MAX_D.
Definition: gz/math/Helpers.hh:263
constexpr T IGN_MASSMATRIX3_DEFAULT_TOLERANCE
The default tolerance value used by MassMatrix3::IsValid(), MassMatrix3::IsPositive(),...
Definition: gz/math/Helpers.hh:40
static const uint64_t INF_UI64
64-bit unsigned integer positive infinite value
Definition: gz/math/Helpers.hh:349
#define IGN_FP_VOLATILE
Define IGN_FP_VOLATILE for FP equality comparisons Use volatile parameters when checking floating poi...
Definition: gz/math/Helpers.hh:196
T lowest(T... args)
static const float DPRCT_MIN_F
Definition: gz/math/Helpers.hh:373
uint64_t PairOutput
Definition: gz/math/Helpers.hh:1145
static const float DPRCT_INF_F
Definition: gz/math/Helpers.hh:375
std::chrono::steady_clock::duration stringToDuration(const std::string &_timeString)
Convert a string to a std::chrono::steady_clock::duration.
Definition: gz/math/Helpers.hh:1059
bool greaterOrNearEqual(const T &_a, const T &_b, const T &_epsilon=1e-6)
inequality test, within a tolerance
Definition: gz/math/Helpers.hh:579
void appendToStream(std::ostream &_out, T _number)
Append a number to a stream. Makes sure "-0" is returned as "0".
Definition: gz/math/Helpers.hh:626
T isnan(T... args)
bool isnan(float _v)
check if a float is NaN
Definition: gz/math/Helpers.hh:414
PairOutput Pair(const PairInput _a, const PairInput _b)
A pairing function that maps two values to a unique third value. This is an implement of Szudzik's fu...
T stoi(T... args)
static const uint16_t DPRCT_LOW_UI16
Definition: gz/math/Helpers.hh:378
static const float INF_F
float positive infinite value
Definition: gz/math/Helpers.hh:281
uint32_t PairInput
Definition: gz/math/Helpers.hh:1144
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: gz/math/Helpers.hh:590
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition: gz/math/Helpers.hh:233
int sgn(T _value)
The signum function.
Definition: gz/math/Helpers.hh:482
static const uint16_t DPRCT_MAX_UI16
Definition: gz/math/Helpers.hh:376
bool isTimeString(const std::string &_timeString)
Check if the given string represents a time. An example time string is "0 00:00:00....
Definition: gz/math/Helpers.hh:925
static const uint16_t INF_UI16
16-bit unsigned integer positive infinite value
Definition: gz/math/Helpers.hh:297
T infinity(T... args)
STL class.
T isinf(T... args)
static const double DPRCT_INF_D
Definition: gz/math/Helpers.hh:371
static const size_t IGN_SIX_SIZE_T
size_t type with a value of 6
Definition: gz/math/Helpers.hh:245
static const int16_t DPRCT_LOW_I16
Definition: gz/math/Helpers.hh:382
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: gz/math/Helpers.hh:365
static const int32_t DPRCT_MAX_I32
Definition: gz/math/Helpers.hh:388
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: gz/math/Helpers.hh:660
static const int64_t DPRCT_INF_I64
Definition: gz/math/Helpers.hh:399
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: gz/math/Helpers.hh:406
static const std::regex time_regex("^([0-9]+ ){0,1}" "(?:([1-9]:|[0-1][0-9]:|2[0-3]:){0,1}" "([0-9]:|[0-5][0-9]:)){0,1}" "(?:([0-9]|[0-5][0-9]){0,1}" "(\\.[0-9]{1,3}){0,1})$")
static const uint32_t DPRCT_MAX_UI32
Definition: gz/math/Helpers.hh:384
T erase(T... args)
static const float DPRCT_LOW_F
Definition: gz/math/Helpers.hh:374
static const uint64_t DPRCT_MIN_UI64
Definition: gz/math/Helpers.hh:393
static const uint32_t MAX_UI32
32bit unsigned integer maximum value
Definition: gz/math/Helpers.hh:313
static const int16_t INF_I16
16-bit unsigned integer positive infinite value
Definition: gz/math/Helpers.hh:310
static const int32_t DPRCT_LOW_I32
Definition: gz/math/Helpers.hh:390
static const double DPRCT_LOW_D
Definition: gz/math/Helpers.hh:370
static const uint32_t DPRCT_LOW_UI32
Definition: gz/math/Helpers.hh:386
std::pair< int64_t, int64_t > durationToSecNsec(const std::chrono::steady_clock::duration &_dur)
Convert a std::chrono::steady_clock::duration to a seconds and nanoseconds pair.
Definition: gz/math/Helpers.hh:811
static const float DPRCT_MAX_F
Definition: gz/math/Helpers.hh:372
std::string durationToString(const std::chrono::steady_clock::duration &_duration)
Convert a std::chrono::steady_clock::duration to a string.
Definition: gz/math/Helpers.hh:872
static const int64_t DPRCT_MAX_I64
Definition: gz/math/Helpers.hh:396
static const double MIN_D
Double min value. This value will be similar to 2.22507e-308.
Definition: gz/math/Helpers.hh:260
T regex_search(T... args)
T swap(T... args)
T min(T... args)
static const int16_t LOW_I16
16bit unsigned integer lowest value. This is equivalent to IGN_INT16_MIN, and is defined here for com...
Definition: gz/math/Helpers.hh:307
static const size_t IGN_NINE_SIZE_T
size_t type with a value of 9
Definition: gz/math/Helpers.hh:254
static const uint32_t MIN_UI32
32bit unsigned integer minimum value
Definition: gz/math/Helpers.hh:316
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: gz/math/Helpers.hh:515
static const uint32_t DPRCT_MIN_UI32
Definition: gz/math/Helpers.hh:385
T round(T... args)
static const float MIN_F
Float minimum value. This value will be similar to 1.17549e-38.
Definition: gz/math/Helpers.hh:275
T stod(T... args)
static const int64_t MAX_I64
64bit unsigned integer maximum value
Definition: gz/math/Helpers.hh:352
T duration_cast(T... args)
static const size_t IGN_FOUR_SIZE_T
size_t type with a value of 4
Definition: gz/math/Helpers.hh:239
bool lessOrNearEqual(const T &_a, const T &_b, const T &_epsilon=1e-6)
inequality test, within a tolerance
Definition: gz/math/Helpers.hh:568
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: gz/math/Helpers.hh:227
bool splitTimeBasedOnTimeRegex(const std::string &_timeString, uint64_t &numberDays, uint64_t &numberHours, uint64_t &numberMinutes, uint64_t &numberSeconds, uint64_t &numberMilliseconds)
Split a std::chrono::steady_clock::duration to a string.
Definition: gz/math/Helpers.hh:976
static const double MAX_D
Double maximum value. This value will be similar to 1.79769e+308.
Definition: gz/math/Helpers.hh:257
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: gz/math/Helpers.hh:230
std::string timePointToString(const std::chrono::steady_clock::time_point &_point)
Convert a std::chrono::steady_clock::time_point to a string.
Definition: gz/math/Helpers.hh:845
static const double DPRCT_MAX_D
Definition: gz/math/Helpers.hh:368
void sort3(T &_a, T &_b, T &_c)
Sort three numbers, such that _a <= _b <= _c.
Definition: gz/math/Helpers.hh:612
static const uint32_t DPRCT_INF_UI32
Definition: gz/math/Helpers.hh:387
T fixed(T... args)
static const float MAX_F
Float maximum value. This value will be similar to 3.40282e+38.
Definition: gz/math/Helpers.hh:272
T empty(T... args)
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: gz/math/Helpers.hh:503
int signum(T _value)
The signum function.
Definition: gz/math/Helpers.hh:494
static const int32_t DPRCT_INF_I32
Definition: gz/math/Helpers.hh:391
static const int16_t DPRCT_INF_I16
Definition: gz/math/Helpers.hh:383
static const int64_t MIN_I64
64bit unsigned integer minimum value
Definition: gz/math/Helpers.hh:355
static const size_t IGN_SEVEN_SIZE_T
size_t type with a value of 7
Definition: gz/math/Helpers.hh:248
T setw(T... args)
static const uint16_t MIN_UI16
16bit unsigned integer minimum value
Definition: gz/math/Helpers.hh:290
T max(T... args)
int roundUpMultiple(int _num, int _multiple)
Round a number up to the nearest multiple. For example, if the input number is 12 and the multiple is...
Definition: gz/math/Helpers.hh:686
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: gz/math/Helpers.hh:269
static const int16_t MAX_I16
16bit unsigned integer maximum value
Definition: gz/math/Helpers.hh:300
static const uint16_t MAX_UI16
16bit unsigned integer maximum value
Definition: gz/math/Helpers.hh:287
static const int16_t MIN_I16
16bit unsigned integer minimum value
Definition: gz/math/Helpers.hh:303
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: gz/math/Helpers.hh:529
double parseFloat(const std::string &_input)
parse string into float
Definition: gz/math/Helpers.hh:733
static const int32_t MIN_I32
32bit unsigned integer minimum value
Definition: gz/math/Helpers.hh:329
std::chrono::duration< uint64_t, std::ratio< 86400 > > days
This will exist in C++-20.
Definition: gz/math/Helpers.hh:821
static const int32_t DPRCT_MIN_I32
Definition: gz/math/Helpers.hh:389
static const uint64_t MIN_UI64
64bit unsigned integer minimum value
Definition: gz/math/Helpers.hh:342
static const float NAN_F
Returns the representation of a quiet not a number (NAN)
Definition: gz/math/Helpers.hh:284
T pow(T... args)