]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - incl/rrtext.hh
dba83fa258d144e52b947b6663185fec49197f36
[hubacji1/rrts.git] / incl / rrtext.hh
1 #ifndef RRTS_RRTEXT_H
2 #define RRTS_RRTEXT_H
3
4 #include "rrts.hh"
5
6 // ext2
7 #include "cute_c2.h"
8
9 // ext4
10 #define GRID 1 // in meters
11 #define GRID_WIDTH 40 // in meters
12 #define GRID_HEIGHT 40 // in meters
13 #define GRID_MAX_XI ((unsigned int) floor(GRID_WIDTH / GRID)) // min is 0
14 #define GRID_MAX_YI ((unsigned int) floor(GRID_HEIGHT / GRID)) // min is 0
15
16 // ext9
17 #define GRID_MAX_HI 60
18
19 namespace rrts {
20
21 class RRTExt18 : public virtual RRTS {
22 private:
23         bool should_finish() const;
24 };
25
26 class RRTExt17 : public virtual RRTS {
27 private:
28         bool should_finish() const;
29 };
30
31 class RRTExt16 : public virtual RRTS {
32 private:
33         void steer(RRTNode const& f, RRTNode const& t);
34 };
35
36 class RRTExt15 : public virtual RRTS {
37 private:
38         std::vector<double> log_path_cost_;
39 public:
40         Json::Value json() const;
41         void json(Json::Value jvi);
42         bool next();
43 };
44
45 /*! \brief Random sampling in the circuit between root and goal.
46  *
47  * \see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
48  */
49 class RRTExt14 : public virtual RRTS {
50 private:
51         Point circle_c_;
52         double circle_r_ = 0.0;
53         std::uniform_real_distribution<double> udr_;
54         std::uniform_real_distribution<double> udt_;
55         std::uniform_real_distribution<double> udh_;
56         RRTNode sample();
57 public:
58         RRTExt14();
59         void reset();
60 };
61
62 /*! Use Dijkstra-based path optimization, goal zone for interesting nodes. */
63 class RRTExt13 : public virtual RRTS {
64 private:
65         class DijkstraNode {
66         public:
67                 RRTNode* node = nullptr;
68                 unsigned int i = 0;
69                 bool v = false;
70                 bool vi();
71                 DijkstraNode(RRTNode* n);
72         };
73         class DijkstraNodeComparator {
74         public:
75                 int operator() (DijkstraNode const& n1, DijkstraNode const& n2);
76         };
77         std::vector<RRTNode*> opath_;
78         double ogoal_cc_ = 0.0;
79         double otime_ = 0.0;
80         std::vector<DijkstraNode> dn_;
81         void pick_interesting();
82         void dijkstra_forward();
83         void dijkstra_backward();
84         void compute_path();
85 public:
86         RRTExt13();
87         Json::Value json() const;
88         void json(Json::Value jvi);
89         void reset();
90 };
91
92 /*! \brief Different `steer` procedures.
93
94 Use sampling in control input for `steer1`. Use R&S steering for `steer2`.
95 */
96 class RRTExt12 : public virtual RRTS {
97         protected:
98                 void steer1(RRTNode &f, RRTNode &t);
99                 bool connect();
100         public:
101                 bool next();
102 };
103
104 /*! \brief Goal Zone.
105
106 */
107 class RRTExt11 : public virtual RRTS {
108         protected:
109                 bool goal_found(RRTNode &f);
110 };
111
112 /*! \brief Different costs extension.
113  *
114  * Use different cost for bulding tree data structure and searching in the
115  * structure. The cost function is from Elbanhawi, Mohamed, Milan Simic, and
116  * Reza Jazar. “Randomized Bidirectional B-Spline Parameterization Motion
117  * Planning.” IEEE Transactions on Intelligent Transportation Systems 17, no. 2
118  * (February 2016): 406–19. https://doi.org/10.1109/TITS.2015.2477355.
119  */
120 class RRTExt10 : public virtual RRTS {
121 protected:
122         double cost_build(RRTNode const& f, RRTNode const& t) const;
123         double cost_search(RRTNode const& f, RRTNode const& t) const;
124 };
125
126 /*! \brief Use grid data structure to store RRT nodes.
127
128 This approach speeds up the search process for the nearest neighbor and
129 the near vertices procedures.
130 */
131 class RRTExt9 : public virtual RRTS {
132         private:
133                 class Cell {
134                         private:
135                                 bool changed_ = false;
136                                 std::vector<RRTNode *> nodes_;
137                         public:
138                                 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
139                                 void store_node(RRTNode *n);
140
141                                 // getter, setter
142                                 bool changed() const
143                                 {
144                                         return this->changed_;
145                                 }
146                                 std::vector<RRTNode *> &nodes()
147                                 {
148                                         return this->nodes_;
149                                 }
150
151                                 Cell();
152                 };
153                 Cell grid_[GRID_MAX_XI][GRID_MAX_YI][GRID_MAX_HI];
154                 double x_min_ = 0;
155                 double x_max_ = 0;
156                 double y_min_ = 0;
157                 double y_max_ = 0;
158                 double h_min_ = 0;
159                 double h_max_ = 2 * M_PI;
160
161                 unsigned int xi(RRTNode n);
162                 unsigned int yi(RRTNode n);
163                 unsigned int hi(RRTNode n);
164         public:
165                 void init();
166                 void deinit();
167                 void store_node(RRTNode n);
168                 RRTNode *nn(RRTNode &t);
169                 std::vector<RRTNode *> nv(RRTNode &t);
170 };
171
172 /*! \brief Use k-d tree data structure to store RRT nodes.
173  *
174  * This approach speeds up the search process for the nearest neighbor and the
175  * near vertices procedures. This extension implements 3D K-d tree.
176  *
177  * \see https://en.wikipedia.org/wiki/K-d_tree
178  */
179 class RRTExt8 : public virtual RRTS {
180 private:
181         class KdNode {
182         public:
183                 RRTNode* node = nullptr;
184                 KdNode* left = nullptr;
185                 KdNode* right = nullptr;
186                 KdNode(RRTNode* n);
187         };
188         KdNode* kdroot_ = nullptr;
189         std::vector<KdNode> kdnodes_;
190         void store(RRTNode* n, KdNode*& ref, unsigned int lvl);
191         void find_nn(RRTNode const& t, KdNode const* const r, unsigned int lvl);
192         void find_nv(RRTNode const& t, KdNode const* const r, unsigned int lvl);
193 public:
194         RRTExt8();
195         void reset();
196         void store(RRTNode n);
197         void find_nn(RRTNode const& t);
198         void find_nv(RRTNode const& t);
199 };
200
201 /*! \brief Use k-d tree data structure to store RRT nodes.
202
203 This approach speeds up the search process for the nearest neighbor and
204 the near vertices procedures. This extension implements 2D K-d tree.
205
206 \see https://en.wikipedia.org/wiki/K-d_tree
207 */
208 class RRTExt7 : public virtual RRTS {
209         private:
210                 class KdNode {
211                         private:
212                                 RRTNode *node_ = nullptr;
213                                 KdNode *left_ = nullptr;
214                                 KdNode *right_ = nullptr;
215                         public:
216                                 // getter, setter
217                                 RRTNode *node() const { return this->node_; }
218                                 KdNode *&left() { return this->left_; }
219                                 KdNode *&right() { return this->right_; }
220                                 KdNode(RRTNode *n);
221                 };
222                 KdNode *kdroot_ = nullptr;
223                 void delete_kd_nodes(KdNode *n);
224                 void store_node(RRTNode *n, KdNode *&r, int l);
225                 void nn(RRTNode *&n, RRTNode &t, KdNode *r, int l, double &d);
226         public:
227                 void init();
228                 void deinit();
229                 void store_node(RRTNode n);
230                 RRTNode *nn(RRTNode &t);
231                 std::vector<RRTNode *> nv(RRTNode &t);
232 };
233
234 /*! \brief Reeds and Shepp cost for building and search.
235 */
236 class RRTExt6 : public virtual RRTS {
237         public:
238                 /*! \brief Reeds and Shepp path length.
239                 */
240                 double cost_build(RRTNode &f, RRTNode &t);
241                 /*! \brief Reeds and Shepp path length.
242                 */
243                 double cost_search(RRTNode &f, RRTNode &t);
244 };
245
246 /*! \brief Different costs extension.
247
248 Use different cost for bulding tree data structure and searching in the
249 structure. This one is complementary to `rrtext1.cc`.
250 */
251 class RRTExt5 : public virtual RRTS {
252         public:
253                 /*! \brief Reeds and Shepp path length.
254                 */
255                 double cost_build(RRTNode &f, RRTNode &t);
256                 /*! \brief Euclidean distance.
257                 */
258                 double cost_search(RRTNode &f, RRTNode &t);
259 };
260
261 /*! \brief Use grid data structure to store RRT nodes.
262
263 This approach speeds up the search process for the nearest neighbor and
264 the near vertices procedures.
265 */
266 class RRTExt4 : public virtual RRTS {
267         private:
268                 class Cell {
269                         private:
270                                 bool changed_ = false;
271                                 std::vector<RRTNode *> nodes_;
272                         public:
273                                 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
274                                 void store_node(RRTNode *n);
275
276                                 // getter, setter
277                                 bool changed() const
278                                 {
279                                         return this->changed_;
280                                 }
281                                 std::vector<RRTNode *> &nodes()
282                                 {
283                                         return this->nodes_;
284                                 }
285
286                                 Cell();
287                 };
288                 Cell grid_[GRID_MAX_XI][GRID_MAX_YI]; // [0, 0] is bottom left
289                 double x_min_ = 0;
290                 double x_max_ = 0;
291                 double y_min_ = 0;
292                 double y_max_ = 0;
293
294                 unsigned int xi(RRTNode n);
295                 unsigned int yi(RRTNode n);
296         public:
297                 void init();
298                 void deinit();
299                 void store_node(RRTNode n);
300                 RRTNode *nn(RRTNode &t);
301                 std::vector<RRTNode *> nv(RRTNode &t);
302 };
303
304 /*! \brief Use Dijkstra algorithm to find the shorter path.
305 */
306 class RRTExt3 : public virtual RRTS {
307         private:
308         public:
309                 void reset();
310                 std::vector<RRTNode *> orig_path_;
311                 double orig_path_cost_ = 9999;
312                 std::vector<RRTNode *> first_optimized_path_;
313                 double first_optimized_path_cost_ = 9999;
314                 void first_path_optimization();
315                 void second_path_optimization();
316                 void compute_path();
317                 Json::Value json();
318                 void json(Json::Value jvi);
319
320                 // getter, setter
321                 std::vector<RRTNode *> &orig_path()
322                 {
323                         return this->orig_path_;
324                 };
325                 double &orig_path_cost() { return this->orig_path_cost_; }
326                 void orig_path_cost(double c) { this->orig_path_cost_ = c; }
327                 std::vector<RRTNode *> &first_optimized_path()
328                 {
329                         return this->first_optimized_path_;
330                 };
331                 double &first_optimized_path_cost() {
332                         return this->first_optimized_path_cost_;
333                 }
334                 void first_optimized_path_cost(double c) {
335                         this->first_optimized_path_cost_ = c;
336                 }
337 };
338
339 /*! \brief Use cute_c2 for collision detection.
340
341 \see https://github.com/RandyGaul/cute_headers/blob/master/cute_c2.h
342 */
343 class RRTExt2 : public virtual RRTS {
344 private:
345         c2Poly c2_bc_;
346         c2x c2x_bc_;
347         std::vector<c2Poly> c2_obstacles_;
348         bool collide(RRTNode const& n);
349         bool collide_steered();
350 public:
351         RRTExt2();
352         Json::Value json() const;
353         void json(Json::Value jvi);
354 };
355
356 /*! \brief Different costs extension.
357
358 Use different cost for bulding tree data structure and searching in the
359 structure.
360 */
361 class RRTExt1 : public virtual RRTS {
362         public:
363                 /*! \brief Reeds and Shepp path length.
364                 */
365                 double cost_build(RRTNode &f, RRTNode &t);
366                 /*! \brief Matej's heuristics.
367                 */
368                 double cost_search(RRTNode &f, RRTNode &t);
369 };
370
371 } // namespace rrts
372 #endif /* RRTS_RRTEXT_H */