]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - incl/rrtbase.h
Fix indexing overflow
[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 <array>
22 #include <chrono>
23 #include <cmath>
24 #include <pthread.h>
25 #include <queue>
26 #include <random>
27 #include <vector>
28 #include "obstacle.h"
29 #include "rrtnode.h"
30 #include "sample.h"
31 #include "slotplanner.h"
32
33 #define NOFNODES 20000
34
35 #define IXSIZE 100
36 #define IXSTEP (1.0 * ((HMAX) - (HMIN)) / IXSIZE)
37 #define IXI(x) ({ __typeof__ (x) _x = (x); \
38                 std::abs((int) floor(_x / IXSTEP)); })
39 #define IYSIZE 100
40 #define IYSTEP (1.0 * ((VMAX) - (VMIN)) / IYSIZE)
41 #define IYI(y) ({ \
42         __typeof__ (y) _y = (y); \
43         (int) floor((_y - VMIN) / IYSTEP); \
44 })
45
46 class Cell {
47         private:
48                 std::vector<RRTNode *> nodes_;
49                 pthread_mutex_t m_;
50                 bool changed_ = false;
51         public:
52                 Cell();
53
54                 // getter
55                 bool changed();
56                 std::vector<RRTNode *> nodes();
57
58                 // other
59                 void add_node(RRTNode *n);
60 };
61
62 class RRTBase {
63         private:
64                 RRTNode *root_ = nullptr;
65                 RRTNode *goal_ = nullptr;
66                 std::vector<RRTNode *> goals_;
67
68                 std::vector<RRTNode *> nodes_;
69                 std::vector<RRTNode *> dnodes_;
70                 std::queue<RRTNode *> firsts_;
71                 PolygonObstacle frame_;
72                 std::vector<RRTNode *> samples_;
73                 std::vector<CircleObstacle> *cobstacles_;
74                 std::vector<SegmentObstacle> *sobstacles_;
75
76                 bool goal_found_ = false;
77                 std::chrono::high_resolution_clock::time_point tstart_;
78                 std::chrono::high_resolution_clock::time_point tend_;
79
80                 std::vector<float> clog_; // costs of trajectories
81                 std::vector<float> nlog_; // #nodes of RRT
82                 std::vector<std::vector<RRTEdge *>> rlog_; // log tree
83                 std::vector<float> slog_; // seconds of trajectories
84                 std::vector<std::vector<RRTNode *>> tlog_; // trajectories
85                 std::vector<RRTNode *> slot_cusp_; // cusp nodes in slot
86         protected:
87                 std::default_random_engine gen_;
88                 std::normal_distribution<float> ndx_;
89                 std::normal_distribution<float> ndy_;
90                 std::normal_distribution<float> ndh_;
91         public:
92                 const float GOAL_FOUND_DISTANCE = 0.2;
93                 const float GOAL_FOUND_ANGLE = M_PI / 32;
94                 float HMIN = -20;
95                 float HMAX = 20;
96                 float VMIN = -5;
97                 float VMAX = 30;
98
99                 ~RRTBase();
100                 RRTBase();
101                 RRTBase(RRTNode *init, RRTNode *goal);
102
103                 // getter
104                 RRTNode *root();
105                 RRTNode *goal();
106                 std::vector<RRTNode *> &goals();
107                 std::vector<RRTNode *> &nodes();
108                 std::vector<RRTNode *> &dnodes();
109                 std::queue<RRTNode *> &firsts();
110                 PolygonObstacle &frame();
111                 std::vector<RRTNode *> &samples();
112                 std::array<std::vector<RRTNode *>, IYSIZE> iy_;
113                 Cell ixy_[IXSIZE][IYSIZE];
114                 std::vector<CircleObstacle> *co();
115                 std::vector<SegmentObstacle> *so();
116                 std::vector<float> &clog();
117                 std::vector<float> &nlog();
118                 std::vector<std::vector<RRTEdge *>> &rlog();
119                 std::vector<float> &slog();
120                 std::vector<std::vector<RRTNode *>> &tlog();
121                 std::vector<RRTNode *> &slot_cusp();
122                 bool goal_found();
123                 float elapsed();
124                 std::vector<RRTNode *> traj_cusp();
125
126                 // setter
127                 void root(RRTNode *node);
128                 void goal(RRTNode *node);
129                 void goals(std::vector<RRTNode *> g);
130                 bool logr(RRTNode *root);
131                 float ocost(RRTNode *n);
132                 bool tlog(std::vector<RRTNode *> t);
133                 void tstart();
134                 void tend();
135                 bool link_obstacles(
136                                 std::vector<CircleObstacle> *cobstacles,
137                                 std::vector<SegmentObstacle> *sobstacles);
138                 bool add_iy(RRTNode *n);
139                 bool add_ixy(RRTNode *n);
140                 bool goal_found(bool f);
141                 void slot_cusp(std::vector<RRTNode *> sc);
142
143                 // other
144                 bool glplot();
145                 bool goal_found(
146                                 RRTNode *node,
147                                 float (*cost)(RRTNode *, RRTNode *));
148                 bool goal_found(
149                         RRTNode *node,
150                         RRTNode *goal
151                 );
152                 bool collide(RRTNode *init, RRTNode *goal);
153                 bool optp_dijkstra(
154                                 std::vector<RRTNode *> &cusps,
155                                 std::vector<int> &npi);
156                 bool optp_rrp(
157                                 std::vector<RRTNode *> &cusps,
158                                 std::vector<int> &npi);
159                 bool optp_smart(
160                                 std::vector<RRTNode *> &cusps,
161                                 std::vector<int> &npi);
162                 bool opt_path();
163                 bool rebase(RRTNode *nr);
164                 std::vector<RRTNode *> findt();
165                 std::vector<RRTNode *> findt(RRTNode *n);
166                 int XI(RRTNode *n);
167                 int YI(RRTNode *n);
168
169                 // RRT Framework
170                 void setSamplingInfo(SamplingInfo si);
171                 RRTNode *sample();
172                 float cost(RRTNode *init, RRTNode *goal);
173                 RRTNode *nn(RRTNode *rs);
174                 std::vector<RRTNode *> nv(RRTNode *node, float dist);
175                 std::vector<RRTNode *> steer(RRTNode *init, RRTNode *goal);
176                 std::vector<RRTNode *> steer(
177                                 RRTNode *init,
178                                 RRTNode *goal,
179                                 float step);
180
181                 // virtuals - implemented by child classes
182                 virtual bool next() = 0;
183 };
184
185 #endif