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