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