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;
109 DijkstraNode(RRTNode* n);
111 class DijkstraNodeComparator {
113 int operator() (DijkstraNode const& n1, DijkstraNode const& n2);
115 class DijkstraNodeBackwardComparator {
117 int operator() (DijkstraNode const& n1, DijkstraNode const& n2);
119 std::vector<RRTNode*> opath_;
120 double ogoal_cc_ = 0.0;
122 std::vector<DijkstraNode> dn_;
123 void interesting_forward();
124 void interesting_backward();
125 void dijkstra_forward();
126 void dijkstra_backward();
130 Json::Value json() const;
131 void json(Json::Value jvi);
135 /* \brief Different `steer` procedures.
137 Use sampling in control input for `steer1`. Use R&S steering for `steer2`.
139 class RRTExt12 : public virtual RRTS {
141 void steer1(RRTNode &f, RRTNode &t);
150 class RRTExt11 : public virtual RRTS {
152 bool goal_found(RRTNode &f);
155 /*! \brief Reeds & Shepp (build) and Euclidean + abs angle (search).
157 * Use Reeds & Shepp path length for building tree data structure and Euclidean
158 * distance plus (abs) heading difference for searching it.
161 * \see https://doi.org/10.1109/TITS.2015.2477355
163 class RRTExt10 : public virtual RRTS {
165 double cost_build(RRTNode const& f, RRTNode const& t) const;
166 double cost_search(RRTNode const& f, RRTNode const& t) const;
169 /* \brief Use grid data structure to store RRT nodes.
171 This approach speeds up the search process for the nearest neighbor and
172 the near vertices procedures.
174 class RRTExt9 : public virtual RRTS {
178 bool changed_ = false;
179 std::vector<RRTNode *> nodes_;
181 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
182 void store_node(RRTNode *n);
187 return this->changed_;
189 std::vector<RRTNode *> &nodes()
196 Cell grid_[GRID_MAX_XI][GRID_MAX_YI][GRID_MAX_HI];
202 double h_max_ = 2 * M_PI;
204 unsigned int xi(RRTNode n);
205 unsigned int yi(RRTNode n);
206 unsigned int hi(RRTNode n);
210 void store_node(RRTNode n);
211 RRTNode *nn(RRTNode &t);
212 std::vector<RRTNode *> nv(RRTNode &t);
215 /*! \brief Use 3D k-d tree data structure to store RRT nodes.
217 * This approach speeds up the search process for the nearest neighbor and the
218 * near vertices procedures. This extension implements 3D K-d tree.
221 * \see https://en.wikipedia.org/wiki/K-d_tree
223 class RRTExt8 : public virtual RRTS {
227 RRTNode* node = nullptr;
228 KdNode* left = nullptr;
229 KdNode* right = nullptr;
232 KdNode* kdroot_ = nullptr;
233 std::vector<KdNode> kdnodes_;
234 void store(RRTNode* n, KdNode*& ref, unsigned int lvl);
235 void find_nn(RRTNode const& t, KdNode const* const r, unsigned int lvl);
236 void find_nv(RRTNode const& t, KdNode const* const r, unsigned int lvl);
240 void store(RRTNode n);
241 void find_nn(RRTNode const& t);
242 void find_nv(RRTNode const& t);
245 /* \brief Use k-d tree data structure to store RRT nodes.
247 This approach speeds up the search process for the nearest neighbor and
248 the near vertices procedures. This extension implements 2D K-d tree.
250 \see https://en.wikipedia.org/wiki/K-d_tree
252 class RRTExt7 : public virtual RRTS {
256 RRTNode *node_ = nullptr;
257 KdNode *left_ = nullptr;
258 KdNode *right_ = nullptr;
261 RRTNode *node() const { return this->node_; }
262 KdNode *&left() { return this->left_; }
263 KdNode *&right() { return this->right_; }
266 KdNode *kdroot_ = nullptr;
267 void delete_kd_nodes(KdNode *n);
268 void store_node(RRTNode *n, KdNode *&r, int l);
269 void nn(RRTNode *&n, RRTNode &t, KdNode *r, int l, double &d);
273 void store_node(RRTNode n);
274 RRTNode *nn(RRTNode &t);
275 std::vector<RRTNode *> nv(RRTNode &t);
278 /*! \brief Reeds & Shepp (build, search).
280 * Use Reeds & Shepp path length for building tree data structure as well as for
285 class RRTExt6 : public virtual RRTS {
287 double cost_build(RRTNode const& f, RRTNode const& t) const;
288 double cost_search(RRTNode const& f, RRTNode const& t) const;
291 /* \brief Different costs extension.
293 Use different cost for bulding tree data structure and searching in the
294 structure. This one is complementary to `rrtext1.cc`.
296 class RRTExt5 : public virtual RRTS {
298 /* \brief Reeds and Shepp path length.
300 double cost_build(RRTNode &f, RRTNode &t);
301 /* \brief Euclidean distance.
303 double cost_search(RRTNode &f, RRTNode &t);
306 /* \brief Use grid data structure to store RRT nodes.
308 This approach speeds up the search process for the nearest neighbor and
309 the near vertices procedures.
311 class RRTExt4 : public virtual RRTS {
315 bool changed_ = false;
316 std::vector<RRTNode *> nodes_;
318 void nn(RRTNode *t, RRTNode **nn, RRTS *p);
319 void store_node(RRTNode *n);
324 return this->changed_;
326 std::vector<RRTNode *> &nodes()
333 Cell grid_[GRID_MAX_XI][GRID_MAX_YI]; // [0, 0] is bottom left
339 unsigned int xi(RRTNode n);
340 unsigned int yi(RRTNode n);
344 void store_node(RRTNode n);
345 RRTNode *nn(RRTNode &t);
346 std::vector<RRTNode *> nv(RRTNode &t);
349 /* \brief Use Dijkstra algorithm to find the shorter path.
351 class RRTExt3 : public virtual RRTS {
355 std::vector<RRTNode *> orig_path_;
356 double orig_path_cost_ = 9999;
357 std::vector<RRTNode *> first_optimized_path_;
358 double first_optimized_path_cost_ = 9999;
359 void first_path_optimization();
360 void second_path_optimization();
363 void json(Json::Value jvi);
366 std::vector<RRTNode *> &orig_path()
368 return this->orig_path_;
370 double &orig_path_cost() { return this->orig_path_cost_; }
371 void orig_path_cost(double c) { this->orig_path_cost_ = c; }
372 std::vector<RRTNode *> &first_optimized_path()
374 return this->first_optimized_path_;
376 double &first_optimized_path_cost() {
377 return this->first_optimized_path_cost_;
379 void first_optimized_path_cost(double c) {
380 this->first_optimized_path_cost_ = c;
384 /*! \brief Use cute_c2 library for collision detection.
387 * \see https://github.com/RandyGaul/cute_headers/blob/master/cute_c2.h
389 class RRTExt2 : public virtual RRTS {
393 std::vector<c2Poly> c2_obstacles_;
394 bool collide(RRTNode const& n);
395 bool collide_steered();
398 Json::Value json() const;
399 void json(Json::Value jvi);
402 /* \brief Different costs extension.
404 Use different cost for bulding tree data structure and searching in the
407 class RRTExt1 : public virtual RRTS {
409 /* \brief Reeds and Shepp path length.
411 double cost_build(RRTNode &f, RRTNode &t);
412 /* \brief Matej's heuristics.
414 double cost_search(RRTNode &f, RRTNode &t);
418 #endif /* RRTS_RRTEXT_H */