]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - incl/rrts.hh
74077df208fb3c0030424e13a8986988c1954a26
[hubacji1/rrts.git] / incl / rrts.hh
1 /*
2  * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
3  *
4  * SPDX-License-Identifier: GPL-3.0-only
5  */
6
7 /*! \brief RRT* structure and default procedures.
8  *
9  * \file
10  */
11 #ifndef RRTS_RRTS_H
12 #define RRTS_RRTS_H
13
14 #include <chrono>
15 #include <functional>
16 #include <json/json.h>
17 #include <random>
18 #include <vector>
19 #include "bcar.hh"
20
21 namespace rrts {
22 using namespace bcar;
23
24 /*! Compute elapsed time class. */
25 class Ter {
26 private:
27         std::chrono::high_resolution_clock::time_point tstart_;
28 public:
29         void start();
30         double scnt() const;
31 };
32
33 /*! Store RRT node. */
34 class RRTNode : public virtual Pose, public virtual CarMove {
35 private:
36         double c_ = 0.0;
37         double cc_ = 0.0;
38         RRTNode* p_ = nullptr;
39         unsigned int cusp_ = 0;
40 public:
41         /*! Get cost to parent. */
42         double c() const;
43
44         /*! Set cost to parent. */
45         void c(double c);
46
47         /*! Get cumulative cost from root. */
48         double cc() const;
49
50         /*! Get parent node. */
51         RRTNode* p() const;
52
53         /*! Set parent node. */
54         void p(RRTNode& p);
55
56         /*! Get number of backward-forward direction changes. */
57         unsigned int cusp() const;
58
59         /*! Set number of backward-forward direction changes. */
60         void cusp(RRTNode const& p);
61
62         bool operator==(RRTNode const& n);
63 };
64
65 class RRTGoal : public virtual RRTNode, public virtual PoseRange {
66 public:
67         using PoseRange::PoseRange;
68 };
69
70 /*! RRT* algorithm basic class. */
71 class RRTS {
72 protected:
73         BicycleCar bc_;
74         RRTGoal goal_;
75         unsigned int icnt_ = 0;
76         unsigned int _imax = 1000;
77         Ter ter_;
78         std::default_random_engine gen_;
79         std::vector<RRTNode> nodes_;
80         std::vector<RRTNode> steered_;
81         std::vector<RRTNode*> path_;
82         RRTNode* nn_ = nullptr;
83         std::vector<RRTNode*> nv_;
84         double cost_ = 0.0;
85         double eta_ = 0.5;
86         double time_ = 0.0;
87         double last_goal_cc_ = 0.0;
88         std::vector<RRTNode> last_path_;
89         void recompute_cc(RRTNode* g);
90         void recompute_path_cc();
91         double min_gamma_eta() const;
92         bool should_continue() const;
93         void join_steered(RRTNode* f);
94         RRTNode& nn();
95         bool connect();
96         void rewire();
97         bool goal_drivable_from(RRTNode const& f);
98         virtual void store(RRTNode n);
99         virtual double cost_build(RRTNode const& f, RRTNode const& t) const;
100         virtual double cost_search(RRTNode const& f, RRTNode const& t) const;
101         virtual void find_nn(RRTNode const& t);
102         virtual void find_nv(RRTNode const& t);
103         virtual void compute_path();
104         virtual void steer(RRTNode const& f, RRTNode const& t) = 0;
105         virtual bool collide_steered() = 0;
106         virtual RRTNode sample() = 0;
107         virtual bool should_finish() const = 0;
108 public:
109         RRTS();
110
111         /*! Get iterations counter. */
112         unsigned int icnt() const;
113
114         /*! Set iterations counter. */
115         void icnt(unsigned int i);
116
117         /*! Return elapsed time. */
118         double scnt() const;
119
120         /*! Generate JSON output. */
121         Json::Value json() const;
122
123         /*! Load JSON input. */
124         void json(Json::Value jvi);
125
126         /*! Run next RRT* iteration. */
127         virtual bool next();
128
129         /*! Reset the algorithm. */
130         virtual void reset();
131 };
132
133 } // namespace rrts
134 #endif /* RRTS_RRTS_H */