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 {
48 void steer(RRTNode const &f, RRTNode const &t);
51 /*! \brief Finish when more than 1000 iterations.
55 class RRTExt18 : public virtual RRTS {
57 bool should_finish() const;
60 /*! \brief Finish when goal found or more than 1000 iterations.
64 class RRTExt17 : public virtual RRTS {
66 bool should_finish() const;
69 /*! \brief Use Reeds & Shepp steering procedure.
73 class RRTExt16 : public virtual RRTS {
75 void steer(RRTNode const& f, RRTNode const& t);
78 /*! \brief Log goal's cumulative cost each iteration.
82 class RRTExt15 : public virtual RRTS {
84 std::vector<double> log_path_cost_;
86 Json::Value json() const;
87 void json(Json::Value jvi);
91 /*! \brief Random sampling in the circuit between root and goal.
94 * \see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
96 class RRTExt14 : public virtual RRTS {
99 double circle_r_ = 0.0;
100 std::uniform_real_distribution<double> udr_;
101 std::uniform_real_distribution<double> udt_;
102 std::uniform_real_distribution<double> udh_;
109 /*! \brief Use Dijkstra algorithm to find path between interesting nodes.
111 * The search for interesting nodes starts at root, the last drivable nodes is
112 * added to interesting nodes until goal is reached.
116 class RRTExt13 : public virtual RRTS {
120 RRTNode* node = nullptr;
125 DijkstraNode(RRTNode* n);
127 class DijkstraNodeComparator {
129 int operator() (DijkstraNode const& n1, DijkstraNode const& n2);
131 class DijkstraNodeBackwardComparator {
133 int operator() (DijkstraNode const& n1, DijkstraNode const& n2);
135 std::vector<RRTNode*> opath_;
136 double ogoal_cc_ = 0.0;
138 std::vector<DijkstraNode> dn_;
139 void interesting_forward();
140 void interesting_backward();
141 void dijkstra_forward();
142 void dijkstra_backward();
146 Json::Value json() const;
147 void json(Json::Value jvi);
151 /* \brief Different `steer` procedures.
153 Use sampling in control input for `steer1`. Use R&S steering for `steer2`.
155 class RRTExt12 : public virtual RRTS {
157 void steer1(RRTNode &f, RRTNode &t);
166 class RRTExt11 : public virtual RRTS {
168 bool goal_found(RRTNode &f);
171 /*! \brief Reeds & Shepp (build) and Euclidean + abs angle (search).
173 * Use Reeds & Shepp path length for building tree data structure and Euclidean
174 * distance + (abs) heading difference + 0.1 * backward-forward direction
175 * changes for searching it.
178 * \see https://doi.org/10.1109/TITS.2015.2477355
180 class RRTExt10 : public virtual RRTS {
182 double cost_build(RRTNode const& f, RRTNode const& t) const;
183 double cost_search(RRTNode const& f, RRTNode const& t) const;
186 /* \brief Use grid data structure to store RRT nodes.
188 This approach speeds up the search process for the nearest neighbor and
189 the near vertices procedures.
191 class RRTExt9 : public virtual RRTS {
195 bool changed_ = false;
196 std::vector<RRTNode *> nodes_;
198 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
199 void store_node(RRTNode *n);
204 return this->changed_;
206 std::vector<RRTNode *> &nodes()
213 Cell grid_[GRID_MAX_XI][GRID_MAX_YI][GRID_MAX_HI];
219 double h_max_ = 2 * M_PI;
221 unsigned int xi(RRTNode n);
222 unsigned int yi(RRTNode n);
223 unsigned int hi(RRTNode n);
227 void store_node(RRTNode n);
228 RRTNode *nn(RRTNode &t);
229 std::vector<RRTNode *> nv(RRTNode &t);
232 /*! \brief Use 3D k-d tree data structure to store RRT nodes.
234 * This approach speeds up the search process for the nearest neighbor and the
235 * near vertices procedures. This extension implements 3D K-d tree.
238 * \see https://en.wikipedia.org/wiki/K-d_tree
240 class RRTExt8 : public virtual RRTS {
244 RRTNode* node = nullptr;
245 KdNode* left = nullptr;
246 KdNode* right = nullptr;
249 KdNode* kdroot_ = nullptr;
250 std::vector<KdNode> kdnodes_;
251 void store(RRTNode* n, KdNode*& ref, unsigned int lvl);
252 void find_nn(RRTNode const& t, KdNode const* const r, unsigned int lvl);
253 void find_nv(RRTNode const& t, KdNode const* const r, unsigned int lvl);
257 void store(RRTNode n);
258 void find_nn(RRTNode const& t);
259 void find_nv(RRTNode const& t);
262 /* \brief Use k-d tree data structure to store RRT nodes.
264 This approach speeds up the search process for the nearest neighbor and
265 the near vertices procedures. This extension implements 2D K-d tree.
267 \see https://en.wikipedia.org/wiki/K-d_tree
269 class RRTExt7 : public virtual RRTS {
273 RRTNode *node_ = nullptr;
274 KdNode *left_ = nullptr;
275 KdNode *right_ = nullptr;
278 RRTNode *node() const { return this->node_; }
279 KdNode *&left() { return this->left_; }
280 KdNode *&right() { return this->right_; }
283 KdNode *kdroot_ = nullptr;
284 void delete_kd_nodes(KdNode *n);
285 void store_node(RRTNode *n, KdNode *&r, int l);
286 void nn(RRTNode *&n, RRTNode &t, KdNode *r, int l, double &d);
290 void store_node(RRTNode n);
291 RRTNode *nn(RRTNode &t);
292 std::vector<RRTNode *> nv(RRTNode &t);
295 /*! \brief Reeds & Shepp (build, search).
297 * Use Reeds & Shepp path length for building tree data structure as well as for
302 class RRTExt6 : public virtual RRTS {
304 double cost_build(RRTNode const& f, RRTNode const& t) const;
305 double cost_search(RRTNode const& f, RRTNode const& t) const;
308 /* \brief Different costs extension.
310 Use different cost for bulding tree data structure and searching in the
311 structure. This one is complementary to `rrtext1.cc`.
313 class RRTExt5 : public virtual RRTS {
315 /* \brief Reeds and Shepp path length.
317 double cost_build(RRTNode &f, RRTNode &t);
318 /* \brief Euclidean distance.
320 double cost_search(RRTNode &f, RRTNode &t);
323 /* \brief Use grid data structure to store RRT nodes.
325 This approach speeds up the search process for the nearest neighbor and
326 the near vertices procedures.
328 class RRTExt4 : public virtual RRTS {
332 bool changed_ = false;
333 std::vector<RRTNode *> nodes_;
335 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
336 void store_node(RRTNode *n);
341 return this->changed_;
343 std::vector<RRTNode *> &nodes()
350 Cell grid_[GRID_MAX_XI][GRID_MAX_YI]; // [0, 0] is bottom left
356 unsigned int xi(RRTNode n);
357 unsigned int yi(RRTNode n);
361 void store_node(RRTNode n);
362 RRTNode *nn(RRTNode &t);
363 std::vector<RRTNode *> nv(RRTNode &t);
366 /* \brief Use Dijkstra algorithm to find the shorter path.
368 class RRTExt3 : public virtual RRTS {
372 std::vector<RRTNode *> orig_path_;
373 double orig_path_cost_ = 9999;
374 std::vector<RRTNode *> first_optimized_path_;
375 double first_optimized_path_cost_ = 9999;
376 void first_path_optimization();
377 void second_path_optimization();
380 void json(Json::Value jvi);
383 std::vector<RRTNode *> &orig_path()
385 return this->orig_path_;
387 double &orig_path_cost() { return this->orig_path_cost_; }
388 void orig_path_cost(double c) { this->orig_path_cost_ = c; }
389 std::vector<RRTNode *> &first_optimized_path()
391 return this->first_optimized_path_;
393 double &first_optimized_path_cost() {
394 return this->first_optimized_path_cost_;
396 void first_optimized_path_cost(double c) {
397 this->first_optimized_path_cost_ = c;
401 /*! \brief Use cute_c2 library for collision detection.
404 * \see https://github.com/RandyGaul/cute_headers/blob/master/cute_c2.h
406 class RRTExt2 : public virtual RRTS {
410 std::vector<c2Poly> c2_obstacles_;
411 bool collide(RRTNode const& n);
412 bool collide_steered();
415 Json::Value json() const;
416 void json(Json::Value jvi);
419 /* \brief Different costs extension.
421 Use different cost for bulding tree data structure and searching in the
424 class RRTExt1 : public virtual RRTS {
426 /* \brief Reeds and Shepp path length.
428 double cost_build(RRTNode &f, RRTNode &t);
429 /* \brief Matej's heuristics.
431 double cost_search(RRTNode &f, RRTNode &t);
435 #endif /* RRTS_RRTEXT_H */