3 #include <jsoncpp/json/json.h>
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(); }
13 std::chrono::duration<double> DT_;
14 DT_ = std::chrono::duration_cast<std::chrono::duration<double>>(
19 inline void TPRINT(const char *what)
21 std::cerr << what << ": " << TDIFF() << std::endl;
26 Json::Value jvi; // JSON input
27 Json::Value jvo; // JSON output
29 if (jvi["init"] == Json::nullValue) {
30 std::cerr << "I need `init` in JSON input scenario";
31 std::cerr << std::endl;
34 if (jvi["goal"] == Json::nullValue) {
35 std::cerr << "I need `goal` in JSON input scenario";
36 std::cerr << std::endl;
39 if (jvi["goals"] == Json::nullValue) {
40 std::cerr << "I need `goals` in JSON input scenario";
41 std::cerr << std::endl;
44 if (jvi["obst"] == Json::nullValue) {
45 std::cerr << "I need `obst` in JSON input scenario";
46 std::cerr << std::endl;
50 std::vector<RRTNode> final_path;
51 double final_path_cost = 9999;
53 init_node.x(jvi["init"][0].asDouble());
54 init_node.y(jvi["init"][1].asDouble());
55 init_node.h(jvi["init"][2].asDouble());
57 init_node.st(M_PI / 32); // only for sc2_4
61 rrts.nodes().front().x(init_node.x());
62 rrts.nodes().front().y(init_node.y());
63 rrts.nodes().front().h(init_node.h());
66 tmp_node.x(jvi["goal"][0].asDouble());
67 tmp_node.y(jvi["goal"][1].asDouble());
68 tmp_node.h(jvi["goal"][2].asDouble());
69 rrts.goals().push_back(tmp_node);
70 for (auto g: jvi["goals"]) {
71 tmp_node.x(g[0].asDouble());
72 tmp_node.y(g[1].asDouble());
73 tmp_node.h(g[2].asDouble());
74 rrts.goals().push_back(tmp_node);
78 Obstacle tmp_obstacle;
79 for (auto o: jvi["obst"]) {
80 tmp_obstacle.poly().clear();
82 double tmp_x = c[0].asDouble();
83 double tmp_y = c[1].asDouble();
84 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
85 tmp_obstacle.poly().push_back(tmp_tuple);
87 rrts.obstacles().push_back(tmp_obstacle);
91 double edist_init_goal = sqrt(
93 rrts.nodes().front().x()
94 - rrts.goals().front().x(),
98 rrts.nodes().front().y()
99 - rrts.goals().front().y(),
104 rrts.nodes().front().x(), edist_init_goal,
105 rrts.nodes().front().y(), edist_init_goal,
111 while (rrts.next()) {}
113 if (rrts.path().size() > 0) {
114 if (cc(*rrts.path().back()) < final_path_cost) {
115 final_path_cost = cc(*rrts.path().back());
117 for (auto n: rrts.path()) {
118 final_path.push_back(RRTNode());
119 final_path.back().x(n->x());
120 final_path.back().y(n->y());
121 final_path.back().h(n->h());
125 std::cerr << "finished 0, cost is " << final_path_cost << std::endl;
127 jvo[rcnt++] = rrts.json();
128 for (int i = 1; i < 5; i++) {
131 rrts2.goals() = rrts.goals();
132 rrts2.obstacles() = rrts.obstacles();
133 rrts2.nodes().front().x(init_node.x());
134 rrts2.nodes().front().y(init_node.y());
135 rrts2.nodes().front().h(init_node.h());
137 double edist_init_goal = sqrt(
139 rrts2.nodes().front().x()
140 - rrts2.goals().front().x(),
144 rrts2.nodes().front().y()
145 - rrts2.goals().front().y(),
150 rrts2.nodes().front().x(), edist_init_goal,
151 rrts2.nodes().front().y(), edist_init_goal,
156 while (rrts2.next()) {}
158 if (rrts2.path().size() > 0) {
159 if (cc(*rrts2.path().back()) < final_path_cost) {
160 final_path_cost = cc(*rrts2.path().back());
162 for (auto n: rrts2.path()) {
163 final_path.push_back(RRTNode());
164 final_path.back().x(n->x());
165 final_path.back().y(n->y());
166 final_path.back().h(n->h());
170 std::cerr << "finished " << i << ", cost is " << final_path_cost;
171 std::cerr << std::endl;
172 jvo[rcnt++] = rrts2.json();
174 std::cout << jvo << std::endl;