]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - api/rrts.h
Merge branch 'feature/uniform-circle-sampling'
[hubacji1/rrts.git] / api / rrts.h
1 #ifndef RRTS_H
2 #define RRTS_H
3
4 #include <chrono>
5 #include <functional>
6 #include <jsoncpp/json/json.h>
7 #include <random>
8 #include <vector>
9 #include "bcar.h"
10
11 #define ETA 1.0 // for steer, nv
12 #define GAMMA(cV) ({ \
13         __typeof__ (cV) _cV = (cV); \
14         pow(log(_cV) / _cV, 1.0 / 3.0); \
15 })
16
17 /*! \brief Possible type of RRT node.
18
19 \param cusp The node that is cusp (change in direction).
20 \param connected The node that branches generated steered path.
21 */
22 class RRTNodeType {
23         public:
24                 static const unsigned int cusp = 1 << 0;
25                 static const unsigned int connected = 1 << 1;
26 };
27
28 /*! \brief RRT node basic class.
29
30 \param c Cumulative cost from RRT data structure root.
31 \param p Pointer to parent RRT node.
32 \param t Type of the RRT node (RRTNodeType).
33 // -- from BicycleCar
34 \param x Horizontal coordinate of rear axle center.
35 \param y Vertical coordinate of rear axle center.
36 \param h Heading of the car in the interval [-pi,+pi] radians.
37 \param sp Speed of the car.
38 \param st Steering of the car.
39 */
40 class RRTNode {
41         private:
42                 double c_ = 0;
43                 RRTNode *p_ = nullptr;
44                 unsigned int t_ = 0;
45                 // -- from BicycleCar
46                 // coordinates
47                 double x_ = 0;
48                 double y_ = 0;
49                 double h_ = 0;
50                 // moving
51                 double sp_ = 0;
52                 double st_ = 0;
53         public:
54                 // getters, setters
55                 double c() const { return this->c_; }
56                 void c(double c) { this->c_ = c; }
57
58                 RRTNode *p() const { return this->p_; }
59                 void p(RRTNode *p) { this->p_ = p; }
60
61                 bool t(unsigned int flag) { return this->t_ & flag; }
62                 void set_t(unsigned int flag) { this->t_ |= flag; }
63                 void clear_t(unsigned int flag) { this->t_ &= ~flag; }
64
65                 // -- from BicycleCar
66                 // getters, setters
67                 double x() const { return this->x_; }
68                 void x(double x) { this->x_ = x; }
69
70                 double y() const { return this->y_; }
71                 void y(double y) { this->y_ = y; }
72
73                 double h() const { return this->h_; }
74                 void h(double h)
75                 {
76                         while (h < -M_PI)
77                                 h += 2 * M_PI;
78                         while (h > +M_PI)
79                                 h -= 2 * M_PI;
80                         this->h_ = h;
81                 }
82
83                 double sp() const { return this->sp_; }
84                 void sp(double sp) { this->sp_ = sp; }
85
86                 double st() const { return this->st_; }
87                 void st(double st) { this->st_ = st; }
88
89                 RRTNode();
90                 RRTNode(const BicycleCar &bc);
91                 bool operator==(const RRTNode& n);
92 };
93
94 /*! \brief Polygon obstacle basic class.
95
96 \param poly Border polygon of the obstacle.
97 */
98 class Obstacle {
99         private:
100                 std::vector<std::tuple<double, double>> poly_;
101         public:
102                 // getters, setters
103                 std::vector<std::tuple<double, double>> &poly()
104                 {
105                         return this->poly_;
106                 }
107
108                 Obstacle();
109 };
110
111 /*! \brief RRT* algorithm basic class.
112
113 \param icnt RRT algorithm iterations counter.
114 \param goals The vector of goal nodes.
115 \param nodes The vector of all nodes in RRT data structure.
116 \param samples The vector of all samples of RRT algorithm.
117 \param sample_dist_type Random distribution type for sampling function (0 -
118 normal, 1 - uniform, 2 - uniform circle)
119 */
120 class RRTS {
121         private:
122                 unsigned int icnt_ = 0;
123                 std::chrono::high_resolution_clock::time_point tstart_;
124                 double scnt_ = 0;
125                 double pcnt_ = 0;
126                 bool gf_ = false;
127                 int sample_dist_type_ = 0;
128
129                 std::vector<RRTNode> goals_;
130                 std::vector<RRTNode> nodes_;
131                 std::vector<Obstacle> obstacles_;
132                 std::vector<RRTNode> samples_;
133                 std::vector<RRTNode> steered_;
134
135                 /*! \brief Update and return elapsed time.
136                 */
137                 double elapsed();
138                 /*! \brief Set normal distribution for sampling.
139                 */
140                 void set_sample_normal(
141                         double x1, double x2,
142                         double y1, double y2,
143                         double h1, double h2
144                 );
145                 /*! \brief Set uniform distribution for sampling.
146                 */
147                 void set_sample_uniform(
148                         double x1, double x2,
149                         double y1, double y2,
150                         double h1, double h2
151                 );
152                 /*! \brief Set uniform circle distribution for sampling.
153                 */
154                 void set_sample_uniform_circle();
155         protected:
156                 BicycleCar bc;
157                 /*! \brief Store RRT node to tree data structure.
158                 */
159                 virtual void store_node(RRTNode n);
160
161                 // RRT procedures
162                 std::tuple<bool, unsigned int, unsigned int>
163                 collide(std::vector<std::tuple<double, double>> &poly);
164                 virtual std::tuple<bool, unsigned int, unsigned int>
165                 collide_steered_from(RRTNode &f);
166                 virtual std::tuple<bool, unsigned int, unsigned int>
167                 collide_two_nodes(RRTNode &f, RRTNode &t);
168                 void sample();
169                         std::default_random_engine gen_;
170                         std::normal_distribution<double> ndx_;
171                         std::normal_distribution<double> ndy_;
172                         std::normal_distribution<double> ndh_;
173                         std::uniform_real_distribution<double> udx_;
174                         std::uniform_real_distribution<double> udy_;
175                         std::uniform_real_distribution<double> udh_;
176                 virtual RRTNode *nn(RRTNode &t);
177                 virtual std::vector<RRTNode *> nv(RRTNode &t);
178                 void steer(RRTNode &f, RRTNode &t);
179                 /*! \brief Join steered nodes to RRT data structure
180
181                 \param f RRT node to join steered nodes to.
182                 */
183                 void join_steered(RRTNode *f);
184                 virtual bool goal_found(RRTNode &f);
185                 // RRT* procedures
186                 bool connect();
187                 void rewire();
188         public:
189                 /*! \brief Initialize RRT algorithm if needed.
190                 */
191                 virtual void init();
192                 /*! \brief Deinitialize RRT algorithm if needed.
193                 */
194                 virtual void deinit();
195                 /*! \brief Return path found by RRT*.
196                 */
197                 virtual std::vector<RRTNode *> path();
198                 /*! \brief Return ``true`` if algorithm should stop.
199
200                 Update counters (iteration, seconds, ...) and return if
201                 the current iteration should be the last one.
202                 */
203                 bool should_stop();
204                 /*! \brief Return ``true`` if the algorithm should finish.
205
206                 Finish means that the algorithm will not be resumed.
207                 */
208                 bool should_finish();
209                 /*! \brief Return ``true`` if the algorithm shoud break.
210
211                 Break means that the algorithm can be resumed.
212                 */
213                 bool should_break();
214                 /*! \brief Return ``true`` if algorithm should continue.
215
216                 `pcnt_` is set to `scnt_`, so the difference is 0 and it can
217                 start from scratch. After the `should_continue` is called,
218                 there must be `while (rrts.next()) {}` loop.
219                 */
220                 bool should_continue();
221                 /*! \brief Run next RRT* iteration.
222                 */
223                 bool next();
224                 /*! \brief Set sampling info.
225
226                 Based on `sample_dist_type`, set proper distribution
227                 parameters. The distribution parameters are relative to `front`
228                 node in `nodes` (init).
229
230                 For normal sampling:
231                 \param x1 Mean x value.
232                 \param x2 Standard deviation of x.
233                 \param y1 Mean y value.
234                 \param y2 Standard deviation of y.
235                 \param h1 Mean h value.
236                 \param h2 Standard deviation of h.
237
238                 For uniform sampling:
239                 \param x1 Minimum x value.
240                 \param x2 Maximum x value.
241                 \param y1 Minimum y value.
242                 \param y2 Maximum y value.
243                 \param h1 Minimum h value.
244                 \param h2 Maximum h value.
245
246                 For uniform circle sampling:
247                 \param x1 Ignored.
248                 \param x2 Ignored.
249                 \param y1 Ignored.
250                 \param y2 Ignored.
251                 \param h1 Ignored.
252                 \param h2 Ignored.
253                 */
254                 void set_sample(
255                         double x1, double x2,
256                         double y1, double y2,
257                         double h1, double h2
258                 );
259                 /*! \brief Generate JSON output.
260                 */
261                 Json::Value json();
262                 /*! \brief Load JSON input.
263                 */
264                 void json(Json::Value jvi);
265
266                 // RRT procedures
267                 virtual double cost_build(RRTNode &f, RRTNode &t);
268                 virtual double cost_search(RRTNode &f, RRTNode &t);
269
270                 // getters, setters
271                 unsigned int icnt() const { return this->icnt_; }
272                 double scnt() const { return this->scnt_; }
273                 bool gf() const { return this->gf_; }
274                 void gf(bool f) { this->gf_ = f; }
275                 int sample_dist_type() const { return this->sample_dist_type_;}
276                 void sample_dist_type(int t) { this->sample_dist_type_ = t; }
277                 std::vector<RRTNode> &goals() { return this->goals_; }
278                 std::vector<RRTNode> &nodes() { return this->nodes_; }
279                 std::vector<Obstacle> &obstacles() { return this->obstacles_; }
280                 std::vector<RRTNode> &samples() { return this->samples_; }
281                 std::vector<RRTNode> &steered() { return this->steered_; }
282
283                 RRTS();
284 };
285
286 /*! \brief Compute cumulative cost of RRT node.
287
288 \param t RRT node to compute cumulative cost to.
289 */
290 double cc(RRTNode &t);
291
292 #endif /* RRTS_H */