2 * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
4 * SPDX-License-Identifier: GPL-3.0-only
7 /*! \brief RRT* extensions.
9 * The extensions are used to implement or change the default behavior of the
13 * \defgroup ext-col Collision detection extensions
14 * \defgroup ext-store Node storage and searching tree extensions
15 * \defgroup ext-cost Cost extensions
16 * \defgroup ext-opt Path optimization extensions
17 * \defgroup ext-sampl Random sampling extensions
18 * \defgroup ext-steer Steering procedure extensions
19 * \defgroup ext-aux Auxilliary extensions
30 #define GRID 1 // in meters
31 #define GRID_WIDTH 40 // in meters
32 #define GRID_HEIGHT 40 // in meters
33 #define GRID_MAX_XI ((unsigned int) floor(GRID_WIDTH / GRID)) // min is 0
34 #define GRID_MAX_YI ((unsigned int) floor(GRID_HEIGHT / GRID)) // min is 0
37 #define GRID_MAX_HI 60
41 /*! \brief Use Dubins paths-based steering procedure.
44 * \see https://github.com/AndrewWalker/Dubins-Curves
46 class RRTExt19 : public virtual RRTS {
49 /*! \brief Finish when more than 1000 iterations.
53 class RRTExt18 : public virtual RRTS {
55 bool should_finish() const;
58 /*! \brief Finish when goal found or more than 1000 iterations.
62 class RRTExt17 : public virtual RRTS {
64 bool should_finish() const;
67 /*! \brief Use Reeds & Shepp steering procedure.
71 class RRTExt16 : public virtual RRTS {
73 void steer(RRTNode const& f, RRTNode const& t);
76 /*! \brief Log goal's cumulative cost each iteration.
80 class RRTExt15 : public virtual RRTS {
82 std::vector<double> log_path_cost_;
84 Json::Value json() const;
85 void json(Json::Value jvi);
89 /*! \brief Random sampling in the circuit between root and goal.
92 * \see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
94 class RRTExt14 : public virtual RRTS {
97 double circle_r_ = 0.0;
98 std::uniform_real_distribution<double> udr_;
99 std::uniform_real_distribution<double> udt_;
100 std::uniform_real_distribution<double> udh_;
107 /*! \brief Use Dijkstra algorithm to find path between interesting nodes.
109 * The search for interesting nodes starts at root, the last drivable nodes is
110 * added to interesting nodes until goal is reached.
114 class RRTExt13 : public virtual RRTS {
118 RRTNode* node = nullptr;
123 DijkstraNode(RRTNode* n);
125 class DijkstraNodeComparator {
127 int operator() (DijkstraNode const& n1, DijkstraNode const& n2);
129 class DijkstraNodeBackwardComparator {
131 int operator() (DijkstraNode const& n1, DijkstraNode const& n2);
133 std::vector<RRTNode*> opath_;
134 double ogoal_cc_ = 0.0;
136 std::vector<DijkstraNode> dn_;
137 void interesting_forward();
138 void interesting_backward();
139 void dijkstra_forward();
140 void dijkstra_backward();
144 Json::Value json() const;
145 void json(Json::Value jvi);
149 /* \brief Different `steer` procedures.
151 Use sampling in control input for `steer1`. Use R&S steering for `steer2`.
153 class RRTExt12 : public virtual RRTS {
155 void steer1(RRTNode &f, RRTNode &t);
164 class RRTExt11 : public virtual RRTS {
166 bool goal_found(RRTNode &f);
169 /*! \brief Reeds & Shepp (build) and Euclidean + abs angle (search).
171 * Use Reeds & Shepp path length for building tree data structure and Euclidean
172 * distance + (abs) heading difference + 0.1 * backward-forward direction
173 * changes for searching it.
176 * \see https://doi.org/10.1109/TITS.2015.2477355
178 class RRTExt10 : public virtual RRTS {
180 double cost_build(RRTNode const& f, RRTNode const& t) const;
181 double cost_search(RRTNode const& f, RRTNode const& t) const;
184 /* \brief Use grid data structure to store RRT nodes.
186 This approach speeds up the search process for the nearest neighbor and
187 the near vertices procedures.
189 class RRTExt9 : public virtual RRTS {
193 bool changed_ = false;
194 std::vector<RRTNode *> nodes_;
196 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
197 void store_node(RRTNode *n);
202 return this->changed_;
204 std::vector<RRTNode *> &nodes()
211 Cell grid_[GRID_MAX_XI][GRID_MAX_YI][GRID_MAX_HI];
217 double h_max_ = 2 * M_PI;
219 unsigned int xi(RRTNode n);
220 unsigned int yi(RRTNode n);
221 unsigned int hi(RRTNode n);
225 void store_node(RRTNode n);
226 RRTNode *nn(RRTNode &t);
227 std::vector<RRTNode *> nv(RRTNode &t);
230 /*! \brief Use 3D k-d tree data structure to store RRT nodes.
232 * This approach speeds up the search process for the nearest neighbor and the
233 * near vertices procedures. This extension implements 3D K-d tree.
236 * \see https://en.wikipedia.org/wiki/K-d_tree
238 class RRTExt8 : public virtual RRTS {
242 RRTNode* node = nullptr;
243 KdNode* left = nullptr;
244 KdNode* right = nullptr;
247 KdNode* kdroot_ = nullptr;
248 std::vector<KdNode> kdnodes_;
249 void store(RRTNode* n, KdNode*& ref, unsigned int lvl);
250 void find_nn(RRTNode const& t, KdNode const* const r, unsigned int lvl);
251 void find_nv(RRTNode const& t, KdNode const* const r, unsigned int lvl);
255 void store(RRTNode n);
256 void find_nn(RRTNode const& t);
257 void find_nv(RRTNode const& t);
260 /* \brief Use k-d tree data structure to store RRT nodes.
262 This approach speeds up the search process for the nearest neighbor and
263 the near vertices procedures. This extension implements 2D K-d tree.
265 \see https://en.wikipedia.org/wiki/K-d_tree
267 class RRTExt7 : public virtual RRTS {
271 RRTNode *node_ = nullptr;
272 KdNode *left_ = nullptr;
273 KdNode *right_ = nullptr;
276 RRTNode *node() const { return this->node_; }
277 KdNode *&left() { return this->left_; }
278 KdNode *&right() { return this->right_; }
281 KdNode *kdroot_ = nullptr;
282 void delete_kd_nodes(KdNode *n);
283 void store_node(RRTNode *n, KdNode *&r, int l);
284 void nn(RRTNode *&n, RRTNode &t, KdNode *r, int l, double &d);
288 void store_node(RRTNode n);
289 RRTNode *nn(RRTNode &t);
290 std::vector<RRTNode *> nv(RRTNode &t);
293 /*! \brief Reeds & Shepp (build, search).
295 * Use Reeds & Shepp path length for building tree data structure as well as for
300 class RRTExt6 : public virtual RRTS {
302 double cost_build(RRTNode const& f, RRTNode const& t) const;
303 double cost_search(RRTNode const& f, RRTNode const& t) const;
306 /* \brief Different costs extension.
308 Use different cost for bulding tree data structure and searching in the
309 structure. This one is complementary to `rrtext1.cc`.
311 class RRTExt5 : public virtual RRTS {
313 /* \brief Reeds and Shepp path length.
315 double cost_build(RRTNode &f, RRTNode &t);
316 /* \brief Euclidean distance.
318 double cost_search(RRTNode &f, RRTNode &t);
321 /* \brief Use grid data structure to store RRT nodes.
323 This approach speeds up the search process for the nearest neighbor and
324 the near vertices procedures.
326 class RRTExt4 : public virtual RRTS {
330 bool changed_ = false;
331 std::vector<RRTNode *> nodes_;
333 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
334 void store_node(RRTNode *n);
339 return this->changed_;
341 std::vector<RRTNode *> &nodes()
348 Cell grid_[GRID_MAX_XI][GRID_MAX_YI]; // [0, 0] is bottom left
354 unsigned int xi(RRTNode n);
355 unsigned int yi(RRTNode n);
359 void store_node(RRTNode n);
360 RRTNode *nn(RRTNode &t);
361 std::vector<RRTNode *> nv(RRTNode &t);
364 /* \brief Use Dijkstra algorithm to find the shorter path.
366 class RRTExt3 : public virtual RRTS {
370 std::vector<RRTNode *> orig_path_;
371 double orig_path_cost_ = 9999;
372 std::vector<RRTNode *> first_optimized_path_;
373 double first_optimized_path_cost_ = 9999;
374 void first_path_optimization();
375 void second_path_optimization();
378 void json(Json::Value jvi);
381 std::vector<RRTNode *> &orig_path()
383 return this->orig_path_;
385 double &orig_path_cost() { return this->orig_path_cost_; }
386 void orig_path_cost(double c) { this->orig_path_cost_ = c; }
387 std::vector<RRTNode *> &first_optimized_path()
389 return this->first_optimized_path_;
391 double &first_optimized_path_cost() {
392 return this->first_optimized_path_cost_;
394 void first_optimized_path_cost(double c) {
395 this->first_optimized_path_cost_ = c;
399 /*! \brief Use cute_c2 library for collision detection.
402 * \see https://github.com/RandyGaul/cute_headers/blob/master/cute_c2.h
404 class RRTExt2 : public virtual RRTS {
408 std::vector<c2Poly> c2_obstacles_;
409 bool collide(RRTNode const& n);
410 bool collide_steered();
413 Json::Value json() const;
414 void json(Json::Value jvi);
417 /* \brief Different costs extension.
419 Use different cost for bulding tree data structure and searching in the
422 class RRTExt1 : public virtual RRTS {
424 /* \brief Reeds and Shepp path length.
426 double cost_build(RRTNode &f, RRTNode &t);
427 /* \brief Matej's heuristics.
429 double cost_search(RRTNode &f, RRTNode &t);
433 #endif /* RRTS_RRTEXT_H */