]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - base/main.cc
Add JSON input to main program
[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                         new RRTNode(
31                                 jvi["init"][0].asFloat(),
32                                 jvi["init"][1].asFloat(),
33                                 jvi["init"][2].asFloat()),
34                         new RRTNode(
35                                 jvi["goal"][0].asFloat(),
36                                 jvi["goal"][1].asFloat(),
37                                 jvi["goal"][2].asFloat()));
38         std::vector<CircleObstacle> co;
39         std::vector<SegmentObstacle> so;
40         for (auto o: jvi["obst"]) {
41                 if (o["circle"] != Json::nullValue) {
42                         co.push_back(CircleObstacle(
43                                                 o["circle"][0].asFloat(),
44                                                 o["circle"][1].asFloat(),
45                                                 o["circle"][2].asFloat()));
46                 }
47                 if (o["segment"] != Json::nullValue) {
48                         so.push_back(SegmentObstacle(
49                                 new RRTNode(
50                                         o["segment"][0][0].asFloat(),
51                                         o["segment"][0][1].asFloat(),
52                                         0),
53                                 new RRTNode(
54                                         o["segment"][1][0].asFloat(),
55                                         o["segment"][1][1].asFloat(),
56                                         0)));
57                 }
58         }
59         p.link_obstacles(&co, &so);
60
61         p.tstart();
62         while (!p.goal_found())
63                 p.next();
64         p.tend();
65
66         std::cerr << "Elapsed is " << p.elapsed() << std::endl;
67         std::cerr << "Goal found is " << p.goal_found() << std::endl;
68         std::cerr << "`tlog` size is " << p.tlog().size() << std::endl;
69
70         Json::Value jvo; // JSON output
71         int i = 0;
72         int j = 0;
73         for (auto traj: p.tlog()) {
74                 i = 0;
75                 for (auto n: traj) {
76                         jvo["traj"][j][i][0] = n->x();
77                         jvo["traj"][j][i][1] = n->y();
78                         jvo["traj"][j][i][2] = n->h();
79                         i++;
80                 }
81                 j++;
82         }
83         std::cout << jvo << std::endl;
84         return 0;
85 }