]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/main.cc
Use OpenGL enabling macro
[hubacji1/iamcar.git] / base / main.cc
1 /*
2 This file is part of I am car.
3
4 I am car is nree 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 #include <iostream>
19 #include <jsoncpp/json/json.h>
20 #include <signal.h>
21 #include "compile.h"
22 #include "obstacle.h"
23 #include "rrtplanner.h"
24 // OpenGL
25 #include <GL/gl.h>
26 #include <GL/glu.h>
27 #include <SDL2/SDL.h>
28
29 #define USE_GL
30 #define USE_INTERRUPT
31
32 bool run_planner = true;
33
34 SDL_Window* gw = NULL;
35 SDL_GLContext gc;
36
37 bool init();
38 bool initGL();
39
40 void hint(int)
41 {
42         run_planner = false;
43 }
44
45 int main()
46 {
47 #ifdef USE_GL
48         init();
49 #endif
50
51         Json::Value jvi; // JSON input
52         Json::Value jvo; // JSON output
53         unsigned int i = 0;
54         unsigned int j = 0;
55         std::cin >> jvi;
56         std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
57
58         PLANNER p(
59                         new RRTNode(
60                                 jvi["init"][0].asFloat(),
61                                 jvi["init"][1].asFloat(),
62                                 jvi["init"][2].asFloat()),
63                         new RRTNode(
64                                 jvi["goal"][0].asFloat(),
65                                 jvi["goal"][1].asFloat(),
66                                 jvi["goal"][2].asFloat()));
67         std::vector<CircleObstacle> co;
68         std::vector<SegmentObstacle> so;
69         for (auto o: jvi["obst"]) {
70                 if (o["circle"] != Json::nullValue) {
71                         co.push_back(CircleObstacle(
72                                                 o["circle"][0].asFloat(),
73                                                 o["circle"][1].asFloat(),
74                                                 o["circle"][2].asFloat()));
75                 }
76                 if (o["segment"] != Json::nullValue) {
77                         so.push_back(SegmentObstacle(
78                                 new RRTNode(
79                                         o["segment"][0][0].asFloat(),
80                                         o["segment"][0][1].asFloat(),
81                                         0),
82                                 new RRTNode(
83                                         o["segment"][1][0].asFloat(),
84                                         o["segment"][1][1].asFloat(),
85                                         0)));
86                 }
87         }
88         p.link_obstacles(&co, &so);
89         p.ocost(p.root());
90         p.ocost(p.goal());
91
92         RRTNode *nn;
93         std::vector<RRTNode *> tr;
94
95 #ifdef USE_INTERRUPT
96         signal(SIGINT, hint);
97         signal(SIGTERM, hint);
98         p.tstart();
99         while (run_planner) {
100                 p.next();
101                 p.tend();
102 #ifdef USE_GL
103                 p.glplot();
104 #endif
105         }
106         if (!p.goal_found()) {
107 #if NNVERSION > 1
108                 nn = p.nn(p.iy_, p.goal(), p.cost);
109 #else
110                 nn = p.nn(p.nodes(), p.goal(), p.cost);
111 #endif
112                 tr = p.findt(nn);
113                 p.tlog(tr);
114         }
115 #if JSONLOGEDGES > 0
116         p.logr(p.root());
117 #endif
118 #else // NOT USE_INTERRUPT
119         while(!p.goal_found()) {
120                 p.tstart();
121                 p.tend();
122                 while (p.elapsed() < TMAX) {
123                         p.next();
124                         p.tend();
125                 }
126 #if NNVERSION > 1
127                 nn = p.nn(p.iy_, p.goal(), p.cost);
128 #else
129                 nn = p.nn(p.nodes(), p.goal(), p.cost);
130 #endif
131                 tr = p.findt(nn);
132                 p.tlog(tr);
133 #if JSONLOGEDGES > 0
134                 p.logr(p.root());
135 #endif
136                 p.rebase(nn);
137         }
138 #endif // USE_INTERRUPT
139
140         std::cerr << "Elapsed is " << p.elapsed() << std::endl;
141         std::cerr << "Goal found is " << p.goal_found() << std::endl;
142         std::cerr << "#nodes is " << p.nodes().size() << std::endl;
143         std::cerr << "#samples is " << p.samples().size() << std::endl;
144         std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
145         std::cerr << "trajectories costs:" << std::endl;
146         for (j = 0; j < p.clog().size(); j++)
147                 std::cerr << "- " << p.clog()[j] << std::endl;
148         std::cerr << "RRT #nodes:" << std::endl;
149         for (j = 0; j < p.nlog().size(); j++)
150                 std::cerr << "- " << p.nlog()[j] << std::endl;
151         std::cerr << "trajectories seconds:" << std::endl;
152         for (j = 0; j < p.slog().size(); j++)
153                 std::cerr << "- " << p.slog()[j] << std::endl;
154         std::cerr << "RRT edges (from root) log size: " << p.rlog().size();
155         std::cerr << std::endl;
156         for (auto edges: p.rlog())
157                 std::cerr << "- " << edges.size() << std::endl;
158
159         // JSON output
160         jvo["elap"] = p.elapsed();
161         // log cost
162         for (j = 0; j < p.clog().size(); j++)
163                 jvo["cost"][j] = p.clog()[j];
164         // log #nodes
165         for (j = 0; j < p.nlog().size(); j++)
166                 jvo["node"][j] = p.nlog()[j];
167         // log seconds
168         for (j = 0; j < p.slog().size(); j++)
169                 jvo["secs"][j] = p.slog()[j];
170         // log traj
171         i = 0;
172         j = 0;
173         for (auto traj: p.tlog()) {
174                 i = 0;
175                 for (auto n: traj) {
176                         jvo["traj"][j][i][0] = n->x();
177                         jvo["traj"][j][i][1] = n->y();
178                         jvo["traj"][j][i][2] = n->h();
179                         i++;
180                 }
181                 j++;
182         }
183 #if JSONLOGEDGES > 0
184         // log edges
185         i = 0;
186         j = 0;
187         for (auto edges: p.rlog()) {
188                 j = 0;
189                 for (auto e: edges) {
190                         jvo["edge"][i][j][0][0] = e->init()->x();
191                         jvo["edge"][i][j][0][1] = e->init()->y();
192                         jvo["edge"][i][j][1][0] = e->goal()->x();
193                         jvo["edge"][i][j][1][1] = e->goal()->y();
194                         j++;
195                 }
196                 i++;
197         }
198 #endif
199 #if JSONLOGSAMPLES > 0
200         // log samples
201         i = 0;
202         j = 0;
203         for (auto s: p.samples()) {
204                 jvo["samp"][j][0] = s->x();
205                 jvo["samp"][j][1] = s->y();
206                 jvo["samp"][j][2] = s->h();
207                 j++;
208         }
209 #endif
210         // print output
211         std::cout << jvo << std::endl;
212
213 #ifdef USE_GL
214         SDL_DestroyWindow(gw);
215         SDL_Quit();
216 #endif
217
218         // free mem
219         for (auto o: so) {
220                 delete o.init();
221                 delete o.goal();
222         }
223         return 0;
224 }
225
226 bool init()
227 {
228         if (SDL_Init(SDL_INIT_VIDEO) < 0) {
229                 std::cerr << "SDL could not initialize! SDL_Error: ";
230                 std::cerr << SDL_GetError();
231                 std::cerr << std::endl;
232                 return false;
233         }
234         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
235         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
236         gw = SDL_CreateWindow(
237                         "I am car",
238                         SDL_WINDOWPOS_UNDEFINED,
239                         SDL_WINDOWPOS_UNDEFINED,
240                         SCREEN_WIDTH,
241                         SCREEN_HEIGHT,
242                         SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
243         if (gw == NULL) {
244                 std::cerr << "Window could not be created! SDL_Error: ";
245                 std::cerr << SDL_GetError();
246                 std::cerr << std::endl;
247                 return false;
248         }
249         gc = SDL_GL_CreateContext(gw);
250         if (gc == NULL) {
251                 std::cerr << "OpenGL context couldn't be created! SDL Error: ";
252                 std::cerr << SDL_GetError();
253                 std::cerr << std::endl;
254                 return false;
255         }
256         if (SDL_GL_SetSwapInterval(1) < 0) {
257                 std::cerr << "Warning: Unable to set VSync! SDL Error: ";
258                 std::cerr << SDL_GetError();
259                 std::cerr << std::endl;
260                 return false;
261         }
262         if (!initGL()) {
263                 std::cerr << "Unable to initialize OpenGL!";
264                 std::cerr << std::endl;
265                 return false;
266         }
267         return true;
268 }
269
270 bool initGL()
271 {
272         GLenum error = GL_NO_ERROR;
273         glMatrixMode(GL_PROJECTION);
274         glLoadIdentity();
275         error = glGetError();
276         if (error != GL_NO_ERROR) {
277                 std::cerr << "Error initializing OpenGL! ";
278                 std::cerr << gluErrorString(error);
279                 std::cerr << std::endl;
280                 return false;
281         }
282         glMatrixMode(GL_MODELVIEW);
283         glLoadIdentity();
284         error = glGetError();
285         if (error != GL_NO_ERROR) {
286                 std::cerr << "Error initializing OpenGL! ";
287                 std::cerr << gluErrorString(error);
288                 std::cerr << std::endl;
289                 return false;
290         }
291         glClearColor(1, 1, 1, 1);
292         error = glGetError();
293         if (error != GL_NO_ERROR) {
294                 std::cerr << "Error initializing OpenGL! ";
295                 std::cerr << gluErrorString(error);
296                 std::cerr << std::endl;
297                 return false;
298         }
299         return true;
300 }