Ignition Math

API Reference

6.8.0
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 IGNITION_MATH_FUNCTIONS_HH_
18 #define IGNITION_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 <ignition/math/config.hh>
35 #include "ignition/math/Export.hh"
36 
39 template <typename T>
41 
44 #define IGN_DBL_MAX ignition::math::DPRCT_MAX_D
45 
48 #define IGN_DBL_MIN ignition::math::DPRCT_MIN_D
49 
52 #define IGN_DBL_LOW ignition::math::DPRCT_LOW_D
53 
56 #define IGN_DBL_INF ignition::math::DPRCT_INF_D
57 
60 #define IGN_FLT_MAX ignition::math::DPRCT_MAX_F
61 
64 #define IGN_FLT_MIN ignition::math::DPRCT_MIN_F
65 
68 #define IGN_FLT_LOW ignition::math::DPRCT_LOW_F
69 
72 #define IGN_FLT_INF ignition::math::DPRCT_INF_F
73 
76 #define IGN_UINT16_MAX ignition::math::DPRCT_MAX_UI16
77 
80 #define IGN_UINT16_MIN ignition::math::DPRCT_MIN_UI16
81 
85 #define IGN_UINT16_LOW ignition::math::DPRCT_LOW_UI16
86 
89 #define IGN_UINT16_INF ignition::math::DPRCT_INF_UI16
90 
93 #define IGN_INT16_MAX ignition::math::DPRCT_MAX_I16
94 
97 #define IGN_INT16_MIN ignition::math::DPRCT_MIN_I16
98 
102 #define IGN_INT16_LOW ignition::math::DPRCT_LOW_I16
103 
106 #define IGN_INT16_INF ignition::math::DPRCT_INF_I16
107 
110 #define IGN_UINT32_MAX ignition::math::DPRCT_MAX_UI32
111 
114 #define IGN_UINT32_MIN ignition::math::DPRCT_MIN_UI32
115 
119 #define IGN_UINT32_LOW ignition::math::DPRCT_LOW_UI32
120 
123 #define IGN_UINT32_INF ignition::math::DPRCT_INF_UI32
124 
127 #define IGN_INT32_MAX ignition::math::DPRCT_MAX_I32
128 
131 #define IGN_INT32_MIN ignition::math::DPRCT_MIN_I32
132 
136 #define IGN_INT32_LOW ignition::math::DPRCT_LOW_I32
137 
140 #define IGN_INT32_INF ignition::math::DPRCT_INF_I32
141 
144 #define IGN_UINT64_MAX ignition::math::DPRCT_MAX_UI64
145 
148 #define IGN_UINT64_MIN ignition::math::DPRCT_MIN_UI64
149 
153 #define IGN_UINT64_LOW ignition::math::DPRCT_LOW_UI64
154 
157 #define IGN_UINT64_INF ignition::math::DPRCT_INF_UI64
158 
161 #define IGN_INT64_MAX ignition::math::DPRCT_MAX_I64
162 
165 #define IGN_INT64_MIN ignition::math::DPRCT_MIN_I64
166 
170 #define IGN_INT64_LOW ignition::math::DPRCT_LOW_I64
171 
174 #define IGN_INT64_INF ignition::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  inline bool isPowerOfTwo(unsigned int _x)
626  {
627  return ((_x != 0) && ((_x & (~_x + 1)) == _x));
628  }
629 
635  inline unsigned int roundUpPowerOfTwo(unsigned int _x)
636  {
637  if (_x == 0)
638  return 1;
639 
640  if (isPowerOfTwo(_x))
641  return _x;
642 
643  while (_x & (_x - 1))
644  _x = _x & (_x - 1);
645 
646  _x = _x << 1;
647 
648  return _x;
649  }
650 
661  inline int roundUpMultiple(int _num, int _multiple)
662  {
663  if (_multiple == 0)
664  return _num;
665 
666  int remainder = std::abs(_num) % _multiple;
667  if (remainder == 0)
668  return _num;
669 
670  if (_num < 0)
671  return -(std::abs(_num) - remainder);
672  else
673  return _num + _multiple - remainder;
674  }
675 
679  inline int parseInt(const std::string &_input)
680  {
681  // Return NAN_I if it is empty
682  if (_input.empty())
683  {
684  return NAN_I;
685  }
686  // Return 0 if it is all spaces
687  else if (_input.find_first_not_of(' ') == std::string::npos)
688  {
689  return 0;
690  }
691 
692  // Otherwise try standard library
693  try
694  {
695  return std::stoi(_input);
696  }
697  // if that fails, return NAN_I
698  catch(...)
699  {
700  return NAN_I;
701  }
702  }
703 
708  inline double parseFloat(const std::string &_input)
709  {
710  // Return NAN_D if it is empty
711  if (_input.empty())
712  {
713  return NAN_D;
714  }
715  // Return 0 if it is all spaces
716  else if (_input.find_first_not_of(' ') == std::string::npos)
717  {
718  return 0;
719  }
720 
721  // Otherwise try standard library
722  try
723  {
724  return std::stod(_input);
725  }
726  // if that fails, return NAN_D
727  catch(...)
728  {
729  return NAN_D;
730  }
731  }
732 
739  const std::chrono::steady_clock::time_point &_time)
740  {
742  _time.time_since_epoch());
744  _time.time_since_epoch());
745  int64_t seconds = now_s.count();
746  int64_t nanoseconds = std::chrono::duration_cast
747  <std::chrono::nanoseconds>(now_ns - now_s).count();
748  return {seconds, nanoseconds};
749  }
750 
757  inline std::chrono::steady_clock::time_point secNsecToTimePoint(
758  const uint64_t &_sec, const uint64_t &_nanosec)
759  {
760  auto duration = std::chrono::seconds(_sec) + std::chrono::nanoseconds(
761  _nanosec);
762  std::chrono::steady_clock::time_point result;
764  result += duration_cast<std::chrono::steady_clock::duration>(duration);
765  return result;
766  }
767 
774  inline std::chrono::steady_clock::duration secNsecToDuration(
775  const uint64_t &_sec, const uint64_t &_nanosec)
776  {
778  _nanosec);
779  }
780 
787  const std::chrono::steady_clock::duration &_dur)
788  {
791  return {s.count(), ns.count()};
792  }
793 
794  // TODO(anyone): Replace this with std::chrono::days.
797 
805  template<class...Durations, class DurationIn>
806  std::tuple<Durations...> breakDownDurations(DurationIn d) {
807  std::tuple<Durations...> retval;
808  using discard = int[];
809  (void)discard{0, (void((
810  (std::get<Durations>(retval) =
811  std::chrono::duration_cast<Durations>(d)),
812  (d -= std::chrono::duration_cast<DurationIn>(
813  std::get<Durations>(retval))))), 0)...};
814  return retval;
815  }
816 
821  const std::chrono::steady_clock::time_point &_point)
822  {
823  auto duration = _point - secNsecToTimePoint(0, 0);
824  auto cleanDuration = breakDownDurations<days,
829  duration);
830  std::ostringstream output_string;
831  output_string << std::setw(2) << std::setfill('0')
832  << std::get<0>(cleanDuration).count() << " "
833  << std::setw(2) << std::setfill('0')
834  << std::get<1>(cleanDuration).count() << ":"
835  << std::setw(2) << std::setfill('0')
836  << std::get<2>(cleanDuration).count() << ":"
837  << std::setfill('0') << std::setw(6)
839  << std::get<3>(cleanDuration).count() +
840  std::get<4>(cleanDuration).count()/1000.0;
841  return output_string.str();
842  }
843 
848  const std::chrono::steady_clock::duration &_duration)
849  {
850  auto cleanDuration = breakDownDurations<days,
855  _duration);
856  std::ostringstream outputString;
857  outputString << std::setw(2) << std::setfill('0')
858  << std::get<0>(cleanDuration).count() << " "
859  << std::setw(2) << std::setfill('0')
860  << std::get<1>(cleanDuration).count() << ":"
861  << std::setw(2) << std::setfill('0')
862  << std::get<2>(cleanDuration).count() << ":"
863  << std::setfill('0') << std::setw(6)
865  << std::get<3>(cleanDuration).count() +
866  std::get<4>(cleanDuration).count()/1000.0;
867  return outputString.str();
868  }
869 
870  // The following regex takes a time string in the general format of
871  // "dd hh:mm:ss.nnn" where n is milliseconds, if just one number is
872  // provided, it is assumed to be seconds
873  static const std::regex time_regex(
874  "^([0-9]+ ){0,1}" // day:
875  // Any positive integer
876 
877  "(?:([1-9]:|[0-1][0-9]:|2[0-3]:){0,1}" // hour:
878  // 1 - 9:
879  // 01 - 19:
880  // 20 - 23:
881 
882  "([0-9]:|[0-5][0-9]:)){0,1}" // minute:
883  // 0 - 9:
884  // 00 - 59:
885 
886  "(?:([0-9]|[0-5][0-9]){0,1}" // second:
887  // 0 - 9
888  // 00 - 59
889 
890  "(\\.[0-9]{1,3}){0,1})$"); // millisecond:
891  // .0 - .9
892  // .00 - .99
893  // .000 - 0.999
894 
895 
905  const std::string &_timeString,
906  uint64_t & numberDays, uint64_t & numberHours,
907  uint64_t & numberMinutes, uint64_t & numberSeconds,
908  uint64_t & numberMilliseconds)
909  {
910  std::smatch matches;
911 
912  // `matches` should always be a size of 6 as there are 6 matching
913  // groups in the regex.
914  // 1. The whole regex
915  // 2. The days
916  // 3. The hours
917  // 4. The minutes
918  // 5. The seconds
919  // 6. The milliseconds
920  // We can also index them as such below.
921  // Note that the space will remain in the day match, the colon
922  // will remain in the hour and minute matches, and the period will
923  // remain in the millisecond match
924  if (!std::regex_search(_timeString, matches, time_regex) ||
925  matches.size() != 6)
926  return false;
927 
928  std::string dayString = matches[1];
929  std::string hourString = matches[2];
930  std::string minuteString = matches[3];
931  std::string secondString = matches[4];
932  std::string millisecondString = matches[5];
933 
934  // Days are the only unbounded number, so check first to see if stoi
935  // runs successfully
936  if (!dayString.empty())
937  {
938  // Erase the space
939  dayString.erase(dayString.length() - 1);
940  try
941  {
942  numberDays = std::stoi(dayString);
943  }
944  catch (const std::out_of_range &)
945  {
946  return false;
947  }
948  }
949 
950  if (!hourString.empty())
951  {
952  // Erase the colon
953  hourString.erase(hourString.length() - 1);
954  numberHours = std::stoi(hourString);
955  }
956 
957  if (!minuteString.empty())
958  {
959  // Erase the colon
960  minuteString.erase(minuteString.length() - 1);
961  numberMinutes = std::stoi(minuteString);
962  }
963 
964  if (!secondString.empty())
965  {
966  numberSeconds = std::stoi(secondString);
967  }
968 
969  if (!millisecondString.empty())
970  {
971  // Erase the period
972  millisecondString.erase(0, 1);
973 
974  // Multiplier because "4" = 400 ms, "04" = 40 ms, and "004" = 4 ms
975  numberMilliseconds = std::stoi(millisecondString) *
976  static_cast<uint64_t>(1000 / pow(10, millisecondString.length()));
977  }
978  return true;
979  }
980 
987  inline std::chrono::steady_clock::duration stringToDuration(
988  const std::string &_timeString)
989  {
990  using namespace std::chrono_literals;
991  std::chrono::steady_clock::duration duration{
992  std::chrono::steady_clock::duration::zero()};
993 
994  if (_timeString.empty())
995  return duration;
996 
997  uint64_t numberDays = 0;
998  uint64_t numberHours = 0;
999  uint64_t numberMinutes = 0;
1000  uint64_t numberSeconds = 0;
1001  uint64_t numberMilliseconds = 0;
1002 
1003  if (!splitTimeBasedOnTimeRegex(_timeString, numberDays, numberHours,
1004  numberMinutes, numberSeconds,
1005  numberMilliseconds))
1006  {
1007  return duration;
1008  }
1009 
1010  // TODO(anyone): Replace below day conversion with std::chrono::days.
1012  duration = std::chrono::steady_clock::duration::zero();
1013  auto delta = std::chrono::milliseconds(numberMilliseconds) +
1014  std::chrono::seconds(numberSeconds) +
1015  std::chrono::minutes(numberMinutes) +
1016  std::chrono::hours(numberHours) +
1017  std::chrono::hours(24 * numberDays);
1018  duration += delta;
1019 
1020  return duration;
1021  }
1022 
1029  inline std::chrono::steady_clock::time_point stringToTimePoint(
1030  const std::string &_timeString)
1031  {
1032  using namespace std::chrono_literals;
1033  std::chrono::steady_clock::time_point timePoint{-1s};
1034 
1035  if (_timeString.empty())
1036  return timePoint;
1037 
1038  uint64_t numberDays = 0;
1039  uint64_t numberHours = 0;
1040  uint64_t numberMinutes = 0;
1041  uint64_t numberSeconds = 0;
1042  uint64_t numberMilliseconds = 0;
1043 
1044  if (!splitTimeBasedOnTimeRegex(_timeString, numberDays, numberHours,
1045  numberMinutes, numberSeconds,
1046  numberMilliseconds))
1047  {
1048  return timePoint;
1049  }
1050 
1051  // TODO(anyone): Replace below day conversion with std::chrono::days.
1053  timePoint = math::secNsecToTimePoint(0, 0);
1054  auto duration = std::chrono::milliseconds(numberMilliseconds) +
1055  std::chrono::seconds(numberSeconds) +
1056  std::chrono::minutes(numberMinutes) +
1057  std::chrono::hours(numberHours) +
1058  std::chrono::hours(24 * numberDays);
1059  timePoint += duration;
1060 
1061  return timePoint;
1062  }
1063 
1064  // Degrade precision on Windows, which cannot handle 'long double'
1065  // values properly. See the implementation of Unpair.
1066  // 32 bit ARM processors also define 'long double' to be the same
1067  // size as 'double', and must also be degraded
1068 #if defined _MSC_VER || defined __arm__
1069  using PairInput = uint16_t;
1070  using PairOutput = uint32_t;
1071 #else
1072  using PairInput = uint32_t;
1073  using PairOutput = uint64_t;
1074 #endif
1075 
1085  PairOutput IGNITION_MATH_VISIBLE Pair(
1086  const PairInput _a, const PairInput _b);
1087 
1099  std::tuple<PairInput, PairInput> IGNITION_MATH_VISIBLE Unpair(
1100  const PairOutput _key);
1101  }
1102  }
1103 }
1104 
1105 #endif
static const size_t IGN_NINE_SIZE_T
size_t type with a value of 9
Definition: Helpers.hh:254
std::chrono::steady_clock::time_point stringToTimePoint(const std::string &_timeString)
Convert a string to a std::chrono::steady_clock::time_point.
Definition: Helpers.hh:1029
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...
static const double DPRCT_LOW_D
Definition: Helpers.hh:370
T empty(T... args)
static const size_t IGN_SIX_SIZE_T
size_t type with a value of 6
Definition: Helpers.hh:245
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:365
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: Helpers.hh:333
static const float INF_F
float positive infinite value
Definition: Helpers.hh:281
static const double LOW_D
Double low value, equivalent to -MAX_D.
Definition: Helpers.hh:263
std::chrono::steady_clock::duration stringToDuration(const std::string &_timeString)
Convert a string to a std::chrono::steady_clock::duration.
Definition: Helpers.hh:987
static const int64_t DPRCT_INF_I64
Definition: Helpers.hh:399
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: Helpers.hh:774
static const uint64_t MIN_UI64
64bit unsigned integer minimum value
Definition: Helpers.hh:342
static const uint32_t MIN_UI32
32bit unsigned integer minimum value
Definition: Helpers.hh:316
static const uint32_t DPRCT_MAX_UI32
Definition: Helpers.hh:384
static const uint16_t DPRCT_LOW_UI16
Definition: Helpers.hh:378
static const float DPRCT_LOW_F
Definition: Helpers.hh:374
std::chrono::duration< uint64_t, std::ratio< 86400 > > days
This will exist in C++-20.
Definition: Helpers.hh:796
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:590
static const size_t IGN_EIGHT_SIZE_T
size_t type with a value of 8
Definition: Helpers.hh:251
static const int32_t DPRCT_MIN_I32
Definition: Helpers.hh:389
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:503
static const int32_t INF_I32
32-bit unsigned integer positive infinite value
Definition: Helpers.hh:336
T swap(T... args)
static const float NAN_F
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:284
static const int16_t MAX_I16
16bit unsigned integer maximum value
Definition: Helpers.hh:300
static const uint16_t MAX_UI16
16bit unsigned integer maximum value
Definition: Helpers.hh:287
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:414
T stod(T... args)
static const uint64_t MAX_UI64
64bit unsigned integer maximum value
Definition: Helpers.hh:339
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: Helpers.hh:346
static const int64_t DPRCT_LOW_I64
Definition: Helpers.hh:398
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:635
static const uint32_t MAX_UI32
32bit unsigned integer maximum value
Definition: Helpers.hh:313
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:529
T duration_cast(T... args)
static const int16_t DPRCT_MAX_I16
Definition: Helpers.hh:380
static const int64_t MAX_I64
64bit unsigned integer maximum value
Definition: Helpers.hh:352
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition: Helpers.hh:556
std::string durationToString(const std::chrono::steady_clock::duration &_duration)
Convert a std::chrono::steady_clock::duration to a string.
Definition: Helpers.hh:847
static const double MIN_D
Double min value. This value will be similar to 2.22507e-308.
Definition: Helpers.hh:260
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: Helpers.hh:230
static const int16_t DPRCT_MIN_I16
Definition: Helpers.hh:381
T setw(T... args)
bool isOdd(const int _v)
Check if parameter is odd.
Definition: Helpers.hh:462
static const float DPRCT_INF_F
Definition: Helpers.hh:375
bool greaterOrNearEqual(const T &_a, const T &_b, const T &_epsilon=1e-6)
inequality test, within a tolerance
Definition: Helpers.hh:579
STL class.
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:515
T min(T... args)
static const int64_t DPRCT_MAX_I64
Definition: Helpers.hh:396
static const size_t IGN_FOUR_SIZE_T
size_t type with a value of 4
Definition: Helpers.hh:239
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: Helpers.hh:786
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})$")
std::tuple< Durations... > breakDownDurations(DurationIn d)
break down durations NOTE: the template arguments must be properly ordered according to magnitude and...
Definition: Helpers.hh:806
static const uint32_t INF_UI32
32-bit unsigned integer positive infinite value
Definition: Helpers.hh:323
static const size_t IGN_FIVE_SIZE_T
size_t type with a value of 5
Definition: Helpers.hh:242
static const uint64_t INF_UI64
64-bit unsigned integer positive infinite value
Definition: Helpers.hh:349
T lowest(T... args)
static const float LOW_F
Float low value, equivalent to -MAX_F.
Definition: Helpers.hh:278
static const float DPRCT_MIN_F
Definition: Helpers.hh:373
uint64_t PairOutput
Definition: Helpers.hh:1073
static const double DPRCT_INF_D
Definition: Helpers.hh:371
static const int16_t DPRCT_LOW_I16
Definition: Helpers.hh:382
#define IGN_FP_VOLATILE
Define IGN_FP_VOLATILE for FP equality comparisons Use volatile parameters when checking floating poi...
Definition: Helpers.hh:196
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: Helpers.hh:738
T erase(T... args)
static const uint16_t DPRCT_MAX_UI16
Definition: Helpers.hh:376
T setfill(T... args)
static const uint16_t DPRCT_MIN_UI16
Definition: Helpers.hh:377
bool isPowerOfTwo(unsigned int _x)
Is this a power of 2?
Definition: Helpers.hh:625
static const uint16_t INF_UI16
16-bit unsigned integer positive infinite value
Definition: Helpers.hh:297
uint32_t PairInput
Definition: Helpers.hh:1072
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: Helpers.hh:757
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:227
static const uint16_t MIN_UI16
16bit unsigned integer minimum value
Definition: Helpers.hh:290
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: Helpers.hh:294
static const uint32_t DPRCT_INF_UI32
Definition: Helpers.hh:387
static const int16_t MIN_I16
16bit unsigned integer minimum value
Definition: Helpers.hh:303
T infinity(T... args)
static const int32_t MIN_I32
32bit unsigned integer minimum value
Definition: Helpers.hh:329
T max(T... args)
static const uint32_t DPRCT_LOW_UI32
Definition: Helpers.hh:386
static const int64_t MIN_I64
64bit unsigned integer minimum value
Definition: Helpers.hh:355
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: Helpers.hh:904
T count(T... args)
static const int32_t DPRCT_INF_I32
Definition: Helpers.hh:391
T fixed(T... args)
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&#39;s fu...
static const float MAX_F
Float maximum value. This value will be similar to 3.40282e+38.
Definition: Helpers.hh:272
static const uint64_t DPRCT_MIN_UI64
Definition: Helpers.hh:393
int sgn(T _value)
The signum function.
Definition: Helpers.hh:482
T size(T... args)
static const double DPRCT_MIN_D
Definition: Helpers.hh:369
int signum(T _value)
The signum function.
Definition: Helpers.hh:494
static const uint32_t DPRCT_MIN_UI32
Definition: Helpers.hh:385
STL class.
static const int16_t DPRCT_INF_I16
Definition: Helpers.hh:383
bool isEven(const int _v)
Check if parameter is even.
Definition: Helpers.hh:446
T pow(T... args)
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:269
static const float DPRCT_MAX_F
Definition: Helpers.hh:372
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: Helpers.hh:307
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: Helpers.hh:359
static const double MAX_D
Double maximum value. This value will be similar to 1.79769e+308.
Definition: Helpers.hh:257
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition: Helpers.hh:233
static const int64_t INF_I64
64-bit unsigned integer positive infinite value
Definition: Helpers.hh:362
T regex_search(T... args)
static const float MIN_F
Float minimum value. This value will be similar to 1.17549e-38.
Definition: Helpers.hh:275
T isnan(T... args)
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:708
constexpr T IGN_MASSMATRIX3_DEFAULT_TOLERANCE
The default tolerance value used by MassMatrix3::IsValid(), MassMatrix3::IsPositive(), and MassMatrix3::ValidMoments()
Definition: Helpers.hh:40
static const int16_t INF_I16
16-bit unsigned integer positive infinite value
Definition: Helpers.hh:310
T quiet_NaN(T... args)
static const double INF_D
Double positive infinite value.
Definition: Helpers.hh:266
void sort2(T &_a, T &_b)
Sort two numbers, such that _a <= _b.
Definition: Helpers.hh:600
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: Helpers.hh:320
static const size_t IGN_THREE_SIZE_T
size_t type with a value of 3
Definition: Helpers.hh:236
static const size_t IGN_SEVEN_SIZE_T
size_t type with a value of 7
Definition: Helpers.hh:248
static const uint64_t DPRCT_MAX_UI64
Definition: Helpers.hh:392
T isinf(T... args)
Definition: Angle.hh:42
T setprecision(T... args)
static const int64_t DPRCT_MIN_I64
Definition: Helpers.hh:397
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:430
std::string timePointToString(const std::chrono::steady_clock::time_point &_point)
Convert a std::chrono::steady_clock::time_point to a string.
Definition: Helpers.hh:820
static const double DPRCT_MAX_D
Definition: Helpers.hh:368
static const uint16_t DPRCT_INF_UI16
Definition: Helpers.hh:379
T stoi(T... args)
static const int32_t DPRCT_LOW_I32
Definition: Helpers.hh:390
static const uint64_t DPRCT_LOW_UI64
Definition: Helpers.hh:394
static const int32_t DPRCT_MAX_I32
Definition: Helpers.hh:388
T round(T... args)
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:679
bool lessOrNearEqual(const T &_a, const T &_b, const T &_epsilon=1e-6)
inequality test, within a tolerance
Definition: Helpers.hh:568
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:542
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:406
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: Helpers.hh:661
void sort3(T &_a, T &_b, T &_c)
Sort three numbers, such that _a <= _b <= _c.
Definition: Helpers.hh:612
static const uint64_t DPRCT_INF_UI64
Definition: Helpers.hh:395
static const int32_t MAX_I32
32bit unsigned integer maximum value
Definition: Helpers.hh:326