]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - src/test7.cc
Add multiple RRT* computing test skeleton
[hubacji1/iamcar2.git] / src / test7.cc
1 #include <chrono>
2 #include <iostream>
3 #include <jsoncpp/json/json.h>
4
5 #include "rrts.h"
6
7 std::chrono::high_resolution_clock::time_point TSTART_;
8 std::chrono::high_resolution_clock::time_point TEND_;
9 inline void TSTART() { TSTART_ = std::chrono::high_resolution_clock::now(); }
10 inline void TEND() { TEND_ = std::chrono::high_resolution_clock::now(); }
11 inline double TDIFF()
12 {
13         std::chrono::duration<double> DT_;
14         DT_ = std::chrono::duration_cast<std::chrono::duration<double>>(
15                 TEND_ - TSTART_
16         );
17         return DT_.count();
18 }
19 inline void TPRINT(const char *what)
20 {
21         std::cerr << what << ": " << TDIFF() << std::endl;
22 }
23
24 int main()
25 {
26         Json::Value jvi; // JSON input
27         Json::Value jvo; // JSON output
28         std::cin >> jvi;
29         if (jvi["init"] == Json::nullValue) {
30                 std::cerr << "I need `init` in JSON input scenario";
31                 std::cerr << std::endl;
32                 return 1;
33         }
34         if (jvi["goal"] == Json::nullValue) {
35                 std::cerr << "I need `goal` in JSON input scenario";
36                 std::cerr << std::endl;
37                 return 1;
38         }
39         if (jvi["goals"] == Json::nullValue) {
40                 std::cerr << "I need `goals` in JSON input scenario";
41                 std::cerr << std::endl;
42                 return 1;
43         }
44         if (jvi["obst"] == Json::nullValue) {
45                 std::cerr << "I need `obst` in JSON input scenario";
46                 std::cerr << std::endl;
47                 return 1;
48         }
49
50         RRTNode init_node;
51         init_node.x(jvi["init"][0].asDouble());
52         init_node.y(jvi["init"][1].asDouble());
53         init_node.h(jvi["init"][2].asDouble());
54         init_node.sp(2.7);
55         init_node.st(M_PI / 32); // only for sc2_4
56         init_node.next();
57
58         RRTS rrts;
59         rrts.nodes().front().x(init_node.x());
60         rrts.nodes().front().y(init_node.y());
61         rrts.nodes().front().h(init_node.h());
62         {
63                 RRTNode tmp_node;
64                 tmp_node.x(jvi["goal"][0].asDouble());
65                 tmp_node.y(jvi["goal"][1].asDouble());
66                 tmp_node.h(jvi["goal"][2].asDouble());
67                 rrts.goals().push_back(tmp_node);
68                 for (auto g: jvi["goals"]) {
69                         tmp_node.x(g[0].asDouble());
70                         tmp_node.y(g[1].asDouble());
71                         tmp_node.h(g[2].asDouble());
72                         rrts.goals().push_back(tmp_node);
73                 }
74         }
75         {
76                 Obstacle tmp_obstacle;
77                 for (auto o: jvi["obst"]) {
78                         tmp_obstacle.poly().clear();
79                         for (auto c: o) {
80                                 double tmp_x = c[0].asDouble();
81                                 double tmp_y = c[1].asDouble();
82                                 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
83                                 tmp_obstacle.poly().push_back(tmp_tuple);
84                         }
85                         rrts.obstacles().push_back(tmp_obstacle);
86                 }
87         }
88         {
89                 double edist_init_goal = sqrt(
90                         pow(
91                                 rrts.nodes().front().x()
92                                 - rrts.goals().front().x(),
93                                 2
94                         )
95                         + pow(
96                                 rrts.nodes().front().y()
97                                 - rrts.goals().front().y(),
98                                 2
99                         )
100                 );
101                 rrts.set_sample(
102                         rrts.nodes().front().x(), edist_init_goal,
103                         rrts.nodes().front().y(), edist_init_goal,
104                         0, 2 * M_PI
105                 );
106         }
107
108         TSTART();
109         while (rrts.next()) {}
110         TEND();
111
112         {
113                 jvo["time"] = TDIFF();
114         }
115         {
116                 jvo["iterations"] = rrts.icnt();
117         }
118         {
119                 jvo["init"][0] = rrts.nodes().front().x();
120                 jvo["init"][1] = rrts.nodes().front().y();
121                 jvo["init"][2] = rrts.nodes().front().h();
122         }
123         {
124                 if (rrts.path().size() > 0) {
125                         jvo["cost"] = cc(*rrts.path().back());
126                 } else {
127                         jvo["cost"] = -1;
128                 }
129         }
130         {
131                 if (rrts.path().size() > 0) {
132                         jvo["goal"][0] = rrts.path().back()->x();
133                         jvo["goal"][1] = rrts.path().back()->y();
134                         jvo["goal"][2] = rrts.path().back()->h();
135                 }
136                 unsigned int cu = 0;
137                 unsigned int co = 0;
138                 unsigned int pcnt = 0;
139                 for (auto n: rrts.path()) {
140                         jvo["path"][pcnt][0] = n->x();
141                         jvo["path"][pcnt][1] = n->y();
142                         jvo["path"][pcnt][2] = n->h();
143                         if (n->t(RRTNodeType::cusp))
144                                 cu++;
145                         if (n->t(RRTNodeType::connected))
146                                 co++;
147                         pcnt++;
148                 }
149                 jvo["cusps-in-path"] = cu;
150                 jvo["connecteds-in-path"] = co;
151         }
152         {
153                 unsigned int gcnt = 0;
154                 for (auto g: rrts.goals()) {
155                         jvo["goals"][gcnt][0] = g.x();
156                         jvo["goals"][gcnt][1] = g.y();
157                         jvo["goals"][gcnt][2] = g.h();
158                         gcnt++;
159                 }
160         }
161         {
162                 unsigned int ocnt = 0;
163                 for (auto o: rrts.obstacles()) {
164                         unsigned int ccnt = 0;
165                         for (auto c: o.poly()) {
166                                 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
167                                 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
168                                 ccnt++;
169                         }
170                         ocnt++;
171                 }
172         }
173         {
174                 jvo["nodes"] = (unsigned int) rrts.nodes().size();
175         }
176         //{
177         //        unsigned int ncnt = 0;
178         //        for (auto n: rrts.nodes()) {
179         //                jvo["nodes_x"][ncnt] = n.x();
180         //                jvo["nodes_y"][ncnt] = n.y();
181         //                //jvo["nodes_h"][ncnt] = n.h();
182         //                ncnt++;
183         //        }
184         //}
185         std::cout << jvo << std::endl;
186         return 0;
187 }