6 #include <jsoncpp/json/json.h>
11 #define ETA 1.0 // for steer, nv
12 #define GAMMA(cV) ({ \
13 __typeof__ (cV) _cV = (cV); \
14 pow(log(_cV) / _cV, 1.0 / 3.0); \
17 /*! \brief Possible type of RRT node.
19 \param cusp The node that is cusp (change in direction).
20 \param connected The node that branches generated steered path.
24 static const unsigned int cusp = 1 << 0;
25 static const unsigned int connected = 1 << 1;
28 /*! \brief RRT node basic class.
30 \param c Cumulative cost from RRT data structure root.
31 \param p Pointer to parent RRT node.
32 \param ch The vector of pointers to children RRT nodes.
34 class RRTNode : public BicycleCar {
37 RRTNode *p_ = nullptr;
41 double c() const { return this->c_; }
42 void c(double c) { this->c_ = c; }
44 RRTNode *p() const { return this->p_; }
45 void p(RRTNode *p) { this->p_ = p; }
47 bool t(unsigned int flag) { return this->t_ & flag; }
48 void set_t(unsigned int flag) { this->t_ |= flag; }
49 void clear_t(unsigned int flag) { this->t_ &= ~flag; }
52 RRTNode(const BicycleCar &bc);
55 /*! \brief Polygon obstacle basic class.
57 \param poly Border polygon of the obstacle.
61 std::vector<std::tuple<double, double>> poly_;
64 std::vector<std::tuple<double, double>> &poly()
72 /*! \brief RRT* algorithm basic class.
74 \param icnt RRT algorithm iterations counter.
75 \param goals The vector of goal nodes.
76 \param nodes The vector of all nodes in RRT data structure.
77 \param samples The vector of all samples of RRT algorithm.
81 unsigned int icnt_ = 0;
82 std::chrono::high_resolution_clock::time_point tstart_;
86 std::vector<RRTNode> goals_;
87 std::vector<RRTNode> nodes_;
88 std::vector<Obstacle> obstacles_;
89 std::vector<RRTNode> samples_;
90 std::vector<RRTNode> steered_;
92 /*! \brief Update and return elapsed time.
96 /*! \brief Store RRT node to tree data structure.
98 virtual void store_node(RRTNode n);
101 std::tuple<bool, unsigned int, unsigned int>
102 collide(std::vector<std::tuple<double, double>> &poly);
103 virtual std::tuple<bool, unsigned int, unsigned int>
104 collide_steered_from(RRTNode &f);
105 virtual std::tuple<bool, unsigned int, unsigned int>
106 collide_two_nodes(RRTNode &f, RRTNode &t);
108 std::default_random_engine gen_;
109 std::normal_distribution<double> ndx_;
110 std::normal_distribution<double> ndy_;
111 std::normal_distribution<double> ndh_;
112 virtual RRTNode *nn(RRTNode &t);
113 virtual std::vector<RRTNode *> nv(RRTNode &t);
114 void steer(RRTNode &f, RRTNode &t);
115 /*! \brief Join steered nodes to RRT data structure
117 \param f RRT node to join steered nodes to.
119 void join_steered(RRTNode *f);
120 bool goal_found(RRTNode &f);
125 /*! \brief Initialize RRT algorithm if needed.
128 /*! \brief Deinitialize RRT algorithm if needed.
130 virtual void deinit();
131 /*! \brief Return path found by RRT*.
133 virtual std::vector<RRTNode *> path();
134 /*! \brief Return ``true`` if algorithm should stop.
136 Update counters (iteration, seconds, ...) and return if
137 the current iteration should be the last one.
140 /*! \brief Run next RRT* iteration.
143 /*! \brief Set sampling info.
145 There is normal distribution sampling for `x`, `y`, and
146 `h` parameters of RRT node.
148 \param mx Mean x value.
149 \param dx Standard deviation of x.
150 \param my Mean y value.
151 \param dy Standard deviation of y.
152 \param mh Mean h value.
153 \param dh Standard deviation of h.
156 double mx, double dx,
157 double my, double dy,
160 /*! \brief Generate JSON output.
165 virtual double cost_build(RRTNode &f, RRTNode &t);
166 virtual double cost_search(RRTNode &f, RRTNode &t);
169 unsigned int icnt() const { return this->icnt_; }
170 double scnt() const { return this->scnt_; }
171 bool gf() const { return this->gf_; }
172 void gf(bool f) { this->gf_ = f; }
173 std::vector<RRTNode> &goals() { return this->goals_; }
174 std::vector<RRTNode> &nodes() { return this->nodes_; }
175 std::vector<Obstacle> &obstacles() { return this->obstacles_; }
176 std::vector<RRTNode> &samples() { return this->samples_; }
177 std::vector<RRTNode> &steered() { return this->steered_; }
182 /*! \brief Compute cumulative cost of RRT node.
184 \param t RRT node to compute cumulative cost to.
186 double cc(RRTNode &t);