]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - incl/rrts.hh
603c0a8cc58a7168a8479fc14b04387977226619
[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         Ter ter_;
77         std::default_random_engine gen_;
78         std::vector<RRTNode> nodes_;
79         std::vector<RRTNode> steered_;
80         std::vector<RRTNode*> path_;
81         RRTNode* nn_ = nullptr;
82         std::vector<RRTNode*> nv_;
83         double cost_ = 0.0;
84         double eta_ = 0.5;
85         double time_ = 0.0;
86         double last_goal_cc_ = 0.0;
87         std::vector<RRTNode> last_path_;
88         void recompute_cc(RRTNode* g);
89         void recompute_path_cc();
90         double min_gamma_eta() const;
91         bool should_continue() const;
92         void join_steered(RRTNode* f);
93         RRTNode& nn();
94         bool connect();
95         void rewire();
96         bool goal_drivable_from(RRTNode const& f);
97         virtual void store(RRTNode n);
98         virtual double cost_build(RRTNode const& f, RRTNode const& t) const;
99         virtual double cost_search(RRTNode const& f, RRTNode const& t) const;
100         virtual void find_nn(RRTNode const& t);
101         virtual void find_nv(RRTNode const& t);
102         virtual void compute_path();
103         virtual void steer(RRTNode const& f, RRTNode const& t) = 0;
104         virtual bool collide_steered() = 0;
105         virtual RRTNode sample() = 0;
106         virtual bool should_finish() const = 0;
107 public:
108         RRTS();
109
110         /*! Get iterations counter. */
111         unsigned int icnt() const;
112
113         /*! Set iterations counter. */
114         void icnt(unsigned int i);
115
116         /*! Return elapsed time. */
117         double scnt() const;
118
119         /*! Generate JSON output. */
120         Json::Value json() const;
121
122         /*! Load JSON input. */
123         void json(Json::Value jvi);
124
125         /*! Run next RRT* iteration. */
126         virtual bool next();
127
128         /*! Reset the algorithm. */
129         virtual void reset();
130 };
131
132 } // namespace rrts
133 #endif /* RRTS_RRTS_H */