]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/main.cc
e22945d1d277ef912db80a83779c4e45ce8a2426
[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 "obstacle.h"
21 #include "rrtplanner.h"
22
23 #define TMAX 10
24
25 int main()
26 {
27         Json::Value jvi; // JSON input
28         std::cin >> jvi;
29         std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
30
31         LaValle1998 p(
32         //Karaman2011 p(
33                         new RRTNode(
34                                 jvi["init"][0].asFloat(),
35                                 jvi["init"][1].asFloat(),
36                                 jvi["init"][2].asFloat()),
37                         new RRTNode(
38                                 jvi["goal"][0].asFloat(),
39                                 jvi["goal"][1].asFloat(),
40                                 jvi["goal"][2].asFloat()));
41         std::vector<CircleObstacle> co;
42         std::vector<SegmentObstacle> so;
43         for (auto o: jvi["obst"]) {
44                 if (o["circle"] != Json::nullValue) {
45                         co.push_back(CircleObstacle(
46                                                 o["circle"][0].asFloat(),
47                                                 o["circle"][1].asFloat(),
48                                                 o["circle"][2].asFloat()));
49                 }
50                 if (o["segment"] != Json::nullValue) {
51                         so.push_back(SegmentObstacle(
52                                 new RRTNode(
53                                         o["segment"][0][0].asFloat(),
54                                         o["segment"][0][1].asFloat(),
55                                         0),
56                                 new RRTNode(
57                                         o["segment"][1][0].asFloat(),
58                                         o["segment"][1][1].asFloat(),
59                                         0)));
60                 }
61         }
62         p.link_obstacles(&co, &so);
63
64         p.tstart();
65         while (!p.goal_found() && p.elapsed() < 10) {
66                 p.next();
67                 p.tend();
68         }
69
70         std::cerr << "Elapsed is " << p.elapsed() << std::endl;
71         std::cerr << "Goal found is " << p.goal_found() << std::endl;
72         std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
73
74         Json::Value jvo; // JSON output
75         int i = 0;
76         int j = 0;
77         // log traj
78         i = 0;
79         j = 0;
80         for (auto traj: p.tlog()) {
81                 i = 0;
82                 for (auto n: traj) {
83                         jvo["traj"][j][i][0] = n->x();
84                         jvo["traj"][j][i][1] = n->y();
85                         jvo["traj"][j][i][2] = n->h();
86                         i++;
87                 }
88                 j++;
89         }
90         // log edges
91         i = 0;
92         j = 0;
93         std::vector<RRTNode *> s; // DFS stack
94         std::vector<RRTNode *> r; // reset visited_
95         RRTNode *tmp;
96         s.push_back(p.root());
97         while(s.size() > 0) {
98                 tmp = s.back();
99                 s.pop_back();
100                 if (!tmp->visit()) {
101                         r.push_back(tmp);
102                         for (auto ch: tmp->children()) {
103                                 s.push_back(ch);
104                                 jvo["edge"][j][0][0] = tmp->x();
105                                 jvo["edge"][j][0][1] = tmp->y();
106                                 jvo["edge"][j][1][0] = ch->x();
107                                 jvo["edge"][j][1][1] = ch->y();
108                                 j++;
109                         }
110                 }
111         }
112         for (auto n: r)
113                 n->visit(false);
114         // log samples
115         i = 0;
116         j = 0;
117         for (auto s: p.samples()) {
118                 jvo["samp"][j][0] = s->x();
119                 jvo["samp"][j][1] = s->y();
120                 jvo["samp"][j][2] = s->h();
121                 j++;
122         }
123         // print output
124         std::cout << jvo << std::endl;
125
126         // free mem
127         for (auto o: so) {
128                 delete o.init();
129                 delete o.goal();
130         }
131         for (auto n: p.nodes())
132                 delete n;
133         for (auto s: p.samples())
134                 delete s;
135         delete p.root();
136         delete p.goal();
137         return 0;
138 }