1 /*! \brief RRT* extensions.
3 * The extensions are used to implement or change the default behavior of the
7 * \defgroup ext-col Collision detection extensions
8 * \defgroup ext-store Node storage and searching tree extensions
9 * \defgroup ext-cost Cost extensions
10 * \defgroup ext-opt Path optimization extensions
11 * \defgroup ext-sampl Random sampling extensions
12 * \defgroup ext-steer Steering procedure extensions
13 * \defgroup ext-aux Auxilliary extensions
24 #define GRID 1 // in meters
25 #define GRID_WIDTH 40 // in meters
26 #define GRID_HEIGHT 40 // in meters
27 #define GRID_MAX_XI ((unsigned int) floor(GRID_WIDTH / GRID)) // min is 0
28 #define GRID_MAX_YI ((unsigned int) floor(GRID_HEIGHT / GRID)) // min is 0
31 #define GRID_MAX_HI 60
35 /*! \brief Finish when more than 1000 iterations.
39 class RRTExt18 : public virtual RRTS {
41 bool should_finish() const;
44 /*! \brief Finish when goal found or more than 1000 iterations.
48 class RRTExt17 : public virtual RRTS {
50 bool should_finish() const;
53 /*! \brief Use Reeds & Shepp steering procedure.
57 class RRTExt16 : public virtual RRTS {
59 void steer(RRTNode const& f, RRTNode const& t);
62 /*! \brief Log goal's cumulative cost each iteration.
66 class RRTExt15 : public virtual RRTS {
68 std::vector<double> log_path_cost_;
70 Json::Value json() const;
71 void json(Json::Value jvi);
75 /*! \brief Random sampling in the circuit between root and goal.
78 * \see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
80 class RRTExt14 : public virtual RRTS {
83 double circle_r_ = 0.0;
84 std::uniform_real_distribution<double> udr_;
85 std::uniform_real_distribution<double> udt_;
86 std::uniform_real_distribution<double> udh_;
93 /*! \brief Use Dijkstra algorithm to find path between interesting nodes.
95 * The search for interesting nodes starts at root, the last drivable nodes is
96 * added to interesting nodes until goal is reached.
100 class RRTExt13 : public virtual RRTS {
104 RRTNode* node = nullptr;
108 DijkstraNode(RRTNode* n);
110 class DijkstraNodeComparator {
112 int operator() (DijkstraNode const& n1, DijkstraNode const& n2);
114 std::vector<RRTNode*> opath_;
115 double ogoal_cc_ = 0.0;
117 std::vector<DijkstraNode> dn_;
118 void pick_interesting();
119 void dijkstra_forward();
120 void dijkstra_backward();
124 Json::Value json() const;
125 void json(Json::Value jvi);
129 /* \brief Different `steer` procedures.
131 Use sampling in control input for `steer1`. Use R&S steering for `steer2`.
133 class RRTExt12 : public virtual RRTS {
135 void steer1(RRTNode &f, RRTNode &t);
144 class RRTExt11 : public virtual RRTS {
146 bool goal_found(RRTNode &f);
149 /*! \brief Reeds & Shepp (build) and Euclidean + abs angle (search).
151 * Use Reeds & Shepp path length for building tree data structure and Euclidean
152 * distance plus (abs) heading difference for searching it.
155 * \see https://doi.org/10.1109/TITS.2015.2477355
157 class RRTExt10 : public virtual RRTS {
159 double cost_build(RRTNode const& f, RRTNode const& t) const;
160 double cost_search(RRTNode const& f, RRTNode const& t) const;
163 /* \brief Use grid data structure to store RRT nodes.
165 This approach speeds up the search process for the nearest neighbor and
166 the near vertices procedures.
168 class RRTExt9 : public virtual RRTS {
172 bool changed_ = false;
173 std::vector<RRTNode *> nodes_;
175 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
176 void store_node(RRTNode *n);
181 return this->changed_;
183 std::vector<RRTNode *> &nodes()
190 Cell grid_[GRID_MAX_XI][GRID_MAX_YI][GRID_MAX_HI];
196 double h_max_ = 2 * M_PI;
198 unsigned int xi(RRTNode n);
199 unsigned int yi(RRTNode n);
200 unsigned int hi(RRTNode n);
204 void store_node(RRTNode n);
205 RRTNode *nn(RRTNode &t);
206 std::vector<RRTNode *> nv(RRTNode &t);
209 /*! \brief Use 3D k-d tree data structure to store RRT nodes.
211 * This approach speeds up the search process for the nearest neighbor and the
212 * near vertices procedures. This extension implements 3D K-d tree.
215 * \see https://en.wikipedia.org/wiki/K-d_tree
217 class RRTExt8 : public virtual RRTS {
221 RRTNode* node = nullptr;
222 KdNode* left = nullptr;
223 KdNode* right = nullptr;
226 KdNode* kdroot_ = nullptr;
227 std::vector<KdNode> kdnodes_;
228 void store(RRTNode* n, KdNode*& ref, unsigned int lvl);
229 void find_nn(RRTNode const& t, KdNode const* const r, unsigned int lvl);
230 void find_nv(RRTNode const& t, KdNode const* const r, unsigned int lvl);
234 void store(RRTNode n);
235 void find_nn(RRTNode const& t);
236 void find_nv(RRTNode const& t);
239 /* \brief Use k-d tree data structure to store RRT nodes.
241 This approach speeds up the search process for the nearest neighbor and
242 the near vertices procedures. This extension implements 2D K-d tree.
244 \see https://en.wikipedia.org/wiki/K-d_tree
246 class RRTExt7 : public virtual RRTS {
250 RRTNode *node_ = nullptr;
251 KdNode *left_ = nullptr;
252 KdNode *right_ = nullptr;
255 RRTNode *node() const { return this->node_; }
256 KdNode *&left() { return this->left_; }
257 KdNode *&right() { return this->right_; }
260 KdNode *kdroot_ = nullptr;
261 void delete_kd_nodes(KdNode *n);
262 void store_node(RRTNode *n, KdNode *&r, int l);
263 void nn(RRTNode *&n, RRTNode &t, KdNode *r, int l, double &d);
267 void store_node(RRTNode n);
268 RRTNode *nn(RRTNode &t);
269 std::vector<RRTNode *> nv(RRTNode &t);
272 /*! \brief Reeds & Shepp (build, search).
274 * Use Reeds & Shepp path length for building tree data structure as well as for
279 class RRTExt6 : public virtual RRTS {
281 double cost_build(RRTNode const& f, RRTNode const& t) const;
282 double cost_search(RRTNode const& f, RRTNode const& t) const;
285 /* \brief Different costs extension.
287 Use different cost for bulding tree data structure and searching in the
288 structure. This one is complementary to `rrtext1.cc`.
290 class RRTExt5 : public virtual RRTS {
292 /* \brief Reeds and Shepp path length.
294 double cost_build(RRTNode &f, RRTNode &t);
295 /* \brief Euclidean distance.
297 double cost_search(RRTNode &f, RRTNode &t);
300 /* \brief Use grid data structure to store RRT nodes.
302 This approach speeds up the search process for the nearest neighbor and
303 the near vertices procedures.
305 class RRTExt4 : public virtual RRTS {
309 bool changed_ = false;
310 std::vector<RRTNode *> nodes_;
312 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
313 void store_node(RRTNode *n);
318 return this->changed_;
320 std::vector<RRTNode *> &nodes()
327 Cell grid_[GRID_MAX_XI][GRID_MAX_YI]; // [0, 0] is bottom left
333 unsigned int xi(RRTNode n);
334 unsigned int yi(RRTNode n);
338 void store_node(RRTNode n);
339 RRTNode *nn(RRTNode &t);
340 std::vector<RRTNode *> nv(RRTNode &t);
343 /* \brief Use Dijkstra algorithm to find the shorter path.
345 class RRTExt3 : public virtual RRTS {
349 std::vector<RRTNode *> orig_path_;
350 double orig_path_cost_ = 9999;
351 std::vector<RRTNode *> first_optimized_path_;
352 double first_optimized_path_cost_ = 9999;
353 void first_path_optimization();
354 void second_path_optimization();
357 void json(Json::Value jvi);
360 std::vector<RRTNode *> &orig_path()
362 return this->orig_path_;
364 double &orig_path_cost() { return this->orig_path_cost_; }
365 void orig_path_cost(double c) { this->orig_path_cost_ = c; }
366 std::vector<RRTNode *> &first_optimized_path()
368 return this->first_optimized_path_;
370 double &first_optimized_path_cost() {
371 return this->first_optimized_path_cost_;
373 void first_optimized_path_cost(double c) {
374 this->first_optimized_path_cost_ = c;
378 /*! \brief Use cute_c2 library for collision detection.
381 * \see https://github.com/RandyGaul/cute_headers/blob/master/cute_c2.h
383 class RRTExt2 : public virtual RRTS {
387 std::vector<c2Poly> c2_obstacles_;
388 bool collide(RRTNode const& n);
389 bool collide_steered();
392 Json::Value json() const;
393 void json(Json::Value jvi);
396 /* \brief Different costs extension.
398 Use different cost for bulding tree data structure and searching in the
401 class RRTExt1 : public virtual RRTS {
403 /* \brief Reeds and Shepp path length.
405 double cost_build(RRTNode &f, RRTNode &t);
406 /* \brief Matej's heuristics.
408 double cost_search(RRTNode &f, RRTNode &t);
412 #endif /* RRTS_RRTEXT_H */