]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - incl/rrtbase.h
Merge branch 'feature/multiple-interesting'
[hubacji1/iamcar.git] / incl / rrtbase.h
1 /*
2 This file is part of I am car.
3
4 I am car is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef RRTBASE_H
19 #define RRTBASE_H
20
21 #include <chrono>
22 #include <cmath>
23 #include <pthread.h>
24 #include <queue>
25 #include <random>
26 #include <vector>
27 #include "obstacle.h"
28 #include "rrtnode.h"
29 #include "sample.h"
30 #include "slotplanner.h"
31
32 #define NOFNODES 20000
33
34 #define IXSIZE 100
35 #define IXSTEP (1.0 * ((VMAX) - (VMIN)) / IXSIZE)
36 #define IXI(x) ({ __typeof__ (x) _x = (x); \
37                 std::abs((int) floor(_x / IXSTEP)); })
38 #define IYSIZE 100
39 #define IYSTEP (1.0 * ((HMAX) - (HMIN)) / IYSIZE)
40 #define IYI(y) ({ __typeof__ (y) _y = (y); \
41                 std::abs((int) floor(_y / IYSTEP)); })
42
43 class Cell {
44         private:
45                 std::vector<RRTNode *> nodes_;
46                 pthread_mutex_t m_;
47                 bool changed_ = false;
48         public:
49                 Cell();
50
51                 // getter
52                 bool changed();
53                 std::vector<RRTNode *> nodes();
54
55                 // other
56                 void add_node(RRTNode *n);
57 };
58
59 class RRTBase {
60         private:
61                 RRTNode *root_ = nullptr;
62                 RRTNode *goal_ = nullptr;
63                 std::vector<RRTNode *> goals_;
64
65                 std::vector<RRTNode *> nodes_;
66                 std::vector<RRTNode *> dnodes_;
67                 std::queue<RRTNode *> firsts_;
68                 PolygonObstacle frame_;
69                 std::vector<RRTNode *> samples_;
70                 std::vector<CircleObstacle> *cobstacles_;
71                 std::vector<SegmentObstacle> *sobstacles_;
72
73                 bool goal_found_ = false;
74                 std::chrono::high_resolution_clock::time_point tstart_;
75                 std::chrono::high_resolution_clock::time_point tend_;
76
77                 std::vector<float> clog_; // costs of trajectories
78                 std::vector<float> nlog_; // #nodes of RRT
79                 std::vector<std::vector<RRTEdge *>> rlog_; // log tree
80                 std::vector<float> slog_; // seconds of trajectories
81                 std::vector<std::vector<RRTNode *>> tlog_; // trajectories
82                 std::vector<RRTNode *> slot_cusp_; // cusp nodes in slot
83
84                 std::default_random_engine gen_;
85         public:
86                 const float GOAL_FOUND_DISTANCE = 0.2;
87                 const float GOAL_FOUND_ANGLE = M_PI / 32;
88
89                 ~RRTBase();
90                 RRTBase();
91                 RRTBase(RRTNode *init, RRTNode *goal);
92
93                 // getter
94                 RRTNode *root();
95                 RRTNode *goal();
96                 std::vector<RRTNode *> &goals();
97                 std::vector<RRTNode *> &nodes();
98                 std::vector<RRTNode *> &dnodes();
99                 std::queue<RRTNode *> &firsts();
100                 PolygonObstacle &frame();
101                 std::vector<RRTNode *> &samples();
102                 std::vector<RRTNode *> iy_[IYSIZE];
103                 Cell ixy_[IXSIZE][IYSIZE];
104                 std::vector<CircleObstacle> *co();
105                 std::vector<SegmentObstacle> *so();
106                 std::vector<float> &clog();
107                 std::vector<float> &nlog();
108                 std::vector<std::vector<RRTEdge *>> &rlog();
109                 std::vector<float> &slog();
110                 std::vector<std::vector<RRTNode *>> &tlog();
111                 std::vector<RRTNode *> &slot_cusp();
112                 bool goal_found();
113                 float elapsed();
114                 std::vector<RRTNode *> traj_cusp();
115
116                 // setter
117                 void root(RRTNode *node);
118                 void goal(RRTNode *node);
119                 void goals(std::vector<RRTNode *> g);
120                 bool logr(RRTNode *root);
121                 float ocost(RRTNode *n);
122                 bool tlog(std::vector<RRTNode *> t);
123                 void tstart();
124                 void tend();
125                 bool link_obstacles(
126                                 std::vector<CircleObstacle> *cobstacles,
127                                 std::vector<SegmentObstacle> *sobstacles);
128                 bool add_iy(RRTNode *n);
129                 bool add_ixy(RRTNode *n);
130                 bool goal_found(bool f);
131                 void slot_cusp(std::vector<RRTNode *> sc);
132
133                 // other
134                 bool glplot();
135                 bool goal_found(
136                                 RRTNode *node,
137                                 float (*cost)(RRTNode *, RRTNode *));
138                 bool goal_found(
139                         RRTNode *node,
140                         RRTNode *goal
141                 );
142                 bool collide(RRTNode *init, RRTNode *goal);
143                 bool optp_dijkstra(
144                                 std::vector<RRTNode *> &cusps,
145                                 std::vector<int> &npi);
146                 bool optp_rrp(
147                                 std::vector<RRTNode *> &cusps,
148                                 std::vector<int> &npi);
149                 bool optp_smart(
150                                 std::vector<RRTNode *> &cusps,
151                                 std::vector<int> &npi);
152                 bool opt_path();
153                 bool rebase(RRTNode *nr);
154                 std::vector<RRTNode *> findt();
155                 std::vector<RRTNode *> findt(RRTNode *n);
156
157                 // RRT Framework
158                 SamplingInfo samplingInfo_;
159                 bool useSamplingInfo_ = false;
160                 RRTNode *sample();
161                 float cost(RRTNode *init, RRTNode *goal);
162                 RRTNode *nn(RRTNode *rs);
163                 std::vector<RRTNode *> nv(RRTNode *node, float dist);
164                 std::vector<RRTNode *> steer(RRTNode *init, RRTNode *goal);
165                 std::vector<RRTNode *> steer(
166                                 RRTNode *init,
167                                 RRTNode *goal,
168                                 float step);
169
170                 // virtuals - implemented by child classes
171                 virtual bool next() = 0;
172 };
173
174 #endif