]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - api/rrts.h
Add cumulative cost function
[hubacji1/rrts.git] / api / rrts.h
1 #ifndef RRTS_H
2 #define RRTS_H
3
4 #include <functional>
5 #include <random>
6 #include <vector>
7 #include "bcar.h"
8
9 /*! \brief RRT node basic class.
10
11 \param c Cumulative cost from RRT data structure root.
12 \param p Pointer to parent RRT node.
13 \param ch The vector of pointers to children RRT nodes.
14 */
15 class RRTNode : public BicycleCar {
16         private:
17                 double c_ = 0;
18                 RRTNode *p_ = nullptr;
19         public:
20                 // getters, setters
21                 double c() const { return this->c_; }
22                 void c(double c) { this->c_ = c; }
23
24                 RRTNode *p() const { return this->p_; }
25                 void p(RRTNode *p) { this->p_ = p; }
26
27                 RRTNode();
28 };
29
30 /*! \brief RRT* algorithm basic class.
31
32 \param icnt RRT algorithm iterations counter.
33 \param goals The vector of goal nodes.
34 \param nodes The vector of all nodes in RRT data structure.
35 \param samples The vector of all samples of RRT algorithm.
36 */
37 class RRTS {
38         private:
39                 unsigned int icnt_ = 0;
40
41                 std::vector<RRTNode> goals_;
42                 std::vector<RRTNode> nodes_;
43                 std::vector<RRTNode> samples_;
44                 std::vector<RRTNode> steered_;
45
46                 // RRT procedures
47                 bool collide();
48                 double cost(RRTNode &f, RRTNode &t);
49                 void sample();
50                         std::default_random_engine gen_;
51                         std::normal_distribution<double> ndx_;
52                         std::normal_distribution<double> ndy_;
53                         std::normal_distribution<double> ndh_;
54                 RRTNode *nn(RRTNode &t);
55                 std::vector<RRTNode *> nv(RRTNode &t);
56                 void steer(RRTNode &f, RRTNode &t);
57                 // RRT* procedures
58                 bool connect();
59                 void rewire();
60         public:
61                 /*! \brief Return path found by RRT*.
62                 */
63                 std::vector<RRTNode *> path();
64                 /*! \brief Run next RRT* iteration.
65                 */
66                 bool next();
67                 /*! \brief Set sampling info.
68
69                 There is normal distribution sampling for `x`, `y`, and
70                 `h` parameters of RRT node.
71
72                 \param mx Mean x value.
73                 \param dx Standard deviation of x.
74                 \param my Mean y value.
75                 \param dy Standard deviation of y.
76                 \param mh Mean h value.
77                 \param dh Standard deviation of h.
78                 */
79                 void set_sample(
80                         double mx, double dx,
81                         double my, double dy,
82                         double mh, double dh
83                 );
84
85                 // getters, setters
86                 std::vector<RRTNode> &goals() { return this->goals_; }
87                 std::vector<RRTNode> &nodes() { return this->nodes_; }
88                 std::vector<RRTNode> &samples() { return this->samples_; }
89                 std::vector<RRTNode> &steered() { return this->steered_; }
90
91                 RRTS();
92 };
93
94 /*! \brief Compute cumulative cost of RRT node.
95
96 \param t RRT node to compute cumulative cost to.
97 */
98 double cc(RRTNode &t);
99
100 #endif /* RRTS_H */