Parasol Planning Library (PPL)
MPUtils.h
Go to the documentation of this file.
1 #ifndef PMPL_MP_UTILS_H_
2 #define PMPL_MP_UTILS_H_
3 
4 #include <functional>
5 #include <iostream>
6 #include <limits>
7 #include <map>
8 #include <memory>
9 #include <string>
10 
11 #ifndef _PARALLEL
12 #include <algorithm> // Apparently STAPL missed this in the file below.
13 #include <containers/sequential/graph/algorithms/connected_components.h>
14 #endif
15 
16 #include "Vector.h"
17 using namespace mathtool;
18 
19 #include "IOUtils.h"
20 
21 #include <boost/math/special_functions/binomial.hpp>
22 #include <boost/math/special_functions/factorials.hpp>
23 
24 class Cfg;
25 class Robot;
26 
29 
30 /*-------------------------------- Constants ---------------------------------*/
31 
32 #define MAX_INT std::numeric_limits<int>::max()
33 #define MAX_DBL std::numeric_limits<double>::max()
34 
37 template<typename T>
38 const T
39 Epsilon(const T& _t1, const T& _t2) {
40  static constexpr T tenEpsilon = T(10) * std::numeric_limits<T>::epsilon();
41  return std::min(std::abs(_t1), std::abs(_t2)) * tenEpsilon;
42 }
43 
44 /*------------------------- Random Number Generation -------------------------*/
45 
48 double DRand();
49 
52 long LRand();
53 
56 long MRand();
57 
61 double GRand(bool _reset = false);
62 
64 double GaussianDistribution(double _mean, double _stdev);
65 
67 void SRand(const unsigned long _seed);
68 
69 /*------------------------------ Geometry Utils ------------------------------*/
70 
72 double Normalize(const double& _a);
73 
79 double TriangleHeight(const Point3d& _a, const Point3d& _b, const Point3d& _c);
80 
87 bool PtInTriangle(const Point2d& _a, const Point2d& _b, const Point2d& _c,
88  const Point2d & _p);
89 
98 bool PtInTriangle(const Point2d& _a, const Point2d& _b, const Point2d& _c,
99  const Point2d& _p, double& _u, double& _v);
100 
103 Point3d GetPtFromBarycentricCoords(const Point3d& _a, const Point3d& _b,
104  const Point3d& _c, double _u, double _v);
105 
109 double NormalizeTheta(double _theta);
110 
111 /*---------------------------- Comparators -----------------------------------*/
112 
118 template <typename T, typename U>
120 
121  const bool operator()(const std::pair<T, U>& _a, const std::pair<T, U>& _b)
122  const noexcept {
123  return _a.second < _b.second;
124  }
125 
126 };
127 
133 template <typename T, typename U>
135 
136  const bool operator()(const std::pair<T, U>& _a, const std::pair<T, U>& _b)
137  const noexcept {
138  return _a.second > _b.second;
139  }
140 
141 };
142 
143 /*------------------------------ Cfg Utilities -------------------------------*/
144 
151 template<class CfgType, class Environment>
152 bool
153 IsWithinResolution(const CfgType& _cfg1, const CfgType& _cfg2,
154  Environment* _env) {
155  CfgType diff = _cfg1 - _cfg2;
156  return diff->PositionMagnitude() <= _env->GetPositionRes()
157  && diff->OrientationMagnitude() <= _env->GetOrientationRes();
158 }
159 
160 
167 template<class CfgType>
168 CfgType
169 ClosestPtOnLineSegment(const CfgType& _ref, const CfgType& _p1,
170  const CfgType& _p2) {
171  CfgType b = _p2 - _p1;
172  CfgType c = _ref - _p1;
173 
174  double bDotC = 0;
175  double bSquared = 0;
176 
177  for(auto itb = b.GetData().begin(), itc = c.GetData().begin();
178  itb != b.GetData().end(); ++itb, ++itc) {
179  bDotC += (*itb) * (*itc);
180  bSquared += (*itb) * (*itb);
181  }
182 
183  if(bDotC <= 0)
184  return _p1;
185  else if(bDotC >= bSquared)
186  return _p2;
187  else
188  return b * (bDotC / bSquared) + _p1;
189 }
190 
191 /*---------------------------- Statistical p-tests --------------------------*/
192 
202 bool
203 binom_test(double _alpha, size_t _n, size_t _k, double _p);
204 
205 
206 /*---------------------------- Distributions --------------------------*/
207 
214 double
215 binomial(size_t _n, size_t _k, double _p);
216 
217 /*----------------------------- Other Random Stuff ---------------------------*/
218 
223 struct NullOutputIterator : std::iterator<std::output_iterator_tag,
224  NullOutputIterator> {
225 
226  template<typename T>
227  void operator=(const T&) { }
228 
229  NullOutputIterator& operator++() {return *this;}
230  NullOutputIterator operator++(int) {return *this;}
231  NullOutputIterator& operator*() {return *this;}
232 
233 };
234 
235 
241 template <class RandomIterator, class T, class Compare = std::less<T>>
242 RandomIterator
243 BinarySearch(RandomIterator _begin, RandomIterator _end, const T& _value,
244  Compare _comparator = Compare()) {
245  auto start = _begin;
246  auto end = _end;
247 
248  while(start != end) {
249  const size_t mid = (size_t) (end - start) / 2;
250  RandomIterator middle = start;
251  std::advance(middle, mid);
252 
253  if(_value == *middle)
254  return middle;
255  if(_comparator(_value, *middle))
256  end = middle;
257  else
258  start = middle;
259  }
260  return _end;
261 }
262 
264 std::vector<Cfg> LoadPath(const std::string &_filename, Robot* _robot);
265 
266 
267 template <typename T>
268 T
269 Identity(const T& _t) noexcept {
270  return _t;
271 }
272 
273 /*----------------------------------------------------------------------------*/
274 
276 
277 #endif
CfgType ClosestPtOnLineSegment(const CfgType &_ref, const CfgType &_p1, const CfgType &_p2)
Definition: MPUtils.h:169
double TriangleHeight(const Point3d &_a, const Point3d &_b, const Point3d &_c)
Definition: MPUtils.cpp:68
RandomIterator BinarySearch(RandomIterator _begin, RandomIterator _end, const T &_value, Compare _comparator=Compare())
Definition: MPUtils.h:243
long LRand()
Definition: MPUtils.cpp:9
long MRand()
Definition: MPUtils.cpp:10
double DRand()
Definition: MPUtils.cpp:8
bool binom_test(double _alpha, size_t _n, size_t _k, double _p)
Definition: MPUtils.cpp:159
Point3d GetPtFromBarycentricCoords(const Point3d &_a, const Point3d &_b, const Point3d &_c, double _u, double _v)
Definition: MPUtils.cpp:144
double GaussianDistribution(double _mean, double _stdev)
Same as GRand, but one can specify the mean and stdev of the distribution.
Definition: MPUtils.cpp:44
double GRand(bool _reset=false)
Definition: MPUtils.cpp:13
double NormalizeTheta(double _theta)
Definition: MPUtils.cpp:151
bool IsWithinResolution(const CfgType &_cfg1, const CfgType &_cfg2, Environment *_env)
Definition: MPUtils.h:153
std::vector< Cfg > LoadPath(const std::string &_filename, Robot *_robot)
Loads a configuration path from a file for a dynamic obstacle.
Definition: MPUtils.cpp:187
double Normalize(const double &_a)
Normalize a value into the range [-1,1).
Definition: MPUtils.cpp:58
bool PtInTriangle(const Point2d &_a, const Point2d &_b, const Point2d &_c, const Point2d &_p)
Definition: MPUtils.cpp:81
double binomial(size_t _n, size_t _k, double _p)
Definition: MPUtils.cpp:178
void SRand(const unsigned long _seed)
Use seedval as the seed.
Definition: MPUtils.cpp:50
T Identity(const T &_t) noexcept
Definition: MPUtils.h:269
const T Epsilon(const T &_t1, const T &_t2)
Definition: MPUtils.h:39
Definition: Cfg.h:38
Definition: Environment.h:137
double GetOrientationRes() const noexcept
Get the orientation resolution.
Definition: Environment.cpp:600
double GetPositionRes() const noexcept
Get the position resolution.
Definition: Environment.cpp:586
Definition: Robot.h:31
Definition: Cfg.h:23
Definition: MPUtils.h:134
const bool operator()(const std::pair< T, U > &_a, const std::pair< T, U > &_b) const noexcept
Definition: MPUtils.h:136
Definition: MPUtils.h:119
const bool operator()(const std::pair< T, U > &_a, const std::pair< T, U > &_b) const noexcept
Definition: MPUtils.h:121
Definition: MPUtils.h:224
NullOutputIterator & operator*()
Definition: MPUtils.h:231
NullOutputIterator operator++(int)
Definition: MPUtils.h:230
void operator=(const T &)
Definition: MPUtils.h:227
NullOutputIterator & operator++()
Definition: MPUtils.h:229