7 this->orig_path().clear();
8 this->first_optimized_path().clear();
9 this->orig_path_cost_ = 9999.9;
10 this->first_optimized_path_cost_ = 9999.9;
13 void RRTExt13::first_path_optimization()
15 if (this->orig_path().size() == 0) {
16 this->orig_path_ = RRTS::path();
17 if (this->orig_path().size() == 0)
20 this->orig_path_cost(this->orig_path().back()->cc);
22 class DijkstraNode : public RRTNode {
24 DijkstraNode *s = nullptr;
36 DijkstraNode(const RRTNode& n) {
44 DijkstraNode *p_ = nullptr;
45 DijkstraNode *p() const { return this->p_; }
46 void p(DijkstraNode *p) { this->p_ = p; }
48 class DijkstraNodeComparator {
51 const DijkstraNode &n1,
52 const DijkstraNode &n2
58 std::vector<DijkstraNode> dn;
59 unsigned int ops = this->orig_path().size();
60 auto op = this->orig_path();
62 unsigned int dncnt = 0;
63 dn.push_back(DijkstraNode(*this->orig_path().front()));
64 dn.back().cc = this->orig_path().front()->cc;
65 dn.back().s = &dn.back();
66 dn.back().n = this->orig_path().front();
67 dn.back().i = dncnt++;
68 for (unsigned int i = 0; i < ops - 1; i++) {
69 auto ibc = BicycleCar();
73 for (unsigned int j = i + 1; j < ops; j++) {
74 auto jbc = BicycleCar();
78 if (!jbc.drivable(ibc)) {
79 dn.push_back(DijkstraNode(*op[j-1]));
80 dn.back().cc = op[j-1]->cc;
81 dn.back().s = &dn.back();
82 dn.back().n = op[j-1];
83 dn.back().i = dncnt++;
89 dn.push_back(DijkstraNode(*this->orig_path().back()));
90 dn.back().cc = this->orig_path().back()->cc;
91 dn.back().s = &dn.back();
92 dn.back().n = this->orig_path().back();
93 dn.back().i = dncnt++;
96 std::vector<DijkstraNode>,
97 DijkstraNodeComparator
101 while (!pq.empty()) {
102 DijkstraNode f = pq.top();
104 for (unsigned int i = f.i + 1; i < dncnt; i++) {
105 double cost = f.cc + this->cost_search(f, dn[i]);
106 this->steer(f, dn[i]);
107 if (this->steered().size() == 0)
108 break; // TODO why not continue?
109 if (std::get<0>(this->collide_steered_from(f)))
111 if (cost < dn[i].cc) {
119 DijkstraNode *ln = nullptr;
124 if (ln == nullptr || ln->p() == nullptr)
126 while (ln->p() != nullptr) {
128 RRTNode *f = ln->p()->n;
130 if (std::get<0>(this->collide_steered_from(*f)))
132 this->join_steered(f);
133 t->p(&this->nodes().back());
134 t->c(this->cost_build(this->nodes().back(), *t));
137 RRTS::compute_path();
140 void RRTExt13::second_path_optimization()
142 if (this->first_optimized_path().size() == 0) {
145 class DijkstraNode : public RRTNode {
147 DijkstraNode *s = nullptr;
148 RRTNode *n = nullptr;
159 DijkstraNode(const RRTNode& n) {
167 DijkstraNode *p_ = nullptr;
168 DijkstraNode *p() const { return this->p_; }
169 void p(DijkstraNode *p) { this->p_ = p; }
171 class DijkstraNodeComparator {
174 const DijkstraNode &n1,
175 const DijkstraNode &n2
178 return n1.cc > n2.cc;
181 std::vector<DijkstraNode> dn;
182 unsigned int ops = this->orig_path().size();
183 auto op = this->orig_path();
185 unsigned int dncnt = 0;
186 dn.push_back(DijkstraNode(*this->orig_path().front()));
187 dn.back().cc = this->orig_path().front()->cc;
188 dn.back().s = &dn.back();
189 dn.back().n = this->orig_path().front();
190 dn.back().i = dncnt++;
191 for (unsigned int i = ops - 1; i > 1; i--) {
192 auto ibc = BicycleCar();
196 for (unsigned int j = i - 1; j > 0; j--) {
197 auto jbc = BicycleCar();
201 if (!jbc.drivable(ibc)) {
202 dn.push_back(DijkstraNode(*op[j-1]));
203 dn.back().cc = op[j-1]->cc;
204 dn.back().s = &dn.back();
205 dn.back().n = op[j-1];
206 dn.back().i = dncnt++;
212 dn.push_back(DijkstraNode(*this->orig_path().back()));
213 dn.back().cc = this->orig_path().back()->cc;
214 dn.back().s = &dn.back();
215 dn.back().n = this->orig_path().back();
216 dn.back().i = dncnt++;
219 std::vector<DijkstraNode>,
220 DijkstraNodeComparator
225 while (!pq.empty()) {
226 DijkstraNode t = pq.top();
228 for (unsigned int i = t.i - 1; i > 0; i--) {
229 double cost = dn[i].cc + this->cost_search(dn[i], t);
230 this->steer(dn[i], t);
231 if (this->steered().size() == 0)
232 break; // TODO why not continue?
233 if (std::get<0>(this->collide_steered_from(dn[i])))
243 DijkstraNode *fn = nullptr;
250 if (fn == nullptr || fn->p() == nullptr)
252 DijkstraNode *ln = &dn.back();
253 while (ln->p() != fn) {
255 RRTNode *f = ln->p()->n;
257 if (std::get<0>(this->collide_steered_from(*f)))
259 this->join_steered(f);
260 t->p(&this->nodes().back());
261 t->c(this->cost_build(this->nodes().back(), *t));
264 RRTS::compute_path();
267 void RRTExt13::compute_path()
269 RRTS::compute_path();
270 auto tstart = std::chrono::high_resolution_clock::now();
271 this->first_path_optimization();
272 this->first_optimized_path_ = RRTS::path();
273 if (this->first_optimized_path_.size() > 0) {
274 this->first_optimized_path_cost(
275 this->first_optimized_path_.back()->cc
280 this->second_path_optimization();
281 auto tend = std::chrono::high_resolution_clock::now();
282 auto tdiff = std::chrono::duration_cast<std::chrono::duration<double>>(
284 this->log_opt_time_.push_back(tdiff.count());
287 Json::Value RRTExt13::json()
289 Json::Value jvo = RRTS::json();
290 jvo["orig_path_cost"] = this->orig_path_cost();
294 unsigned int pcnt = 0;
295 for (auto n: this->orig_path()) {
296 jvo["orig_path"][pcnt][0] = n->x();
297 jvo["orig_path"][pcnt][1] = n->y();
298 jvo["orig_path"][pcnt][2] = n->h();
299 if (n->t(RRTNodeType::cusp))
301 if (n->t(RRTNodeType::connected))
305 jvo["orig_cusps-in-path"] = cu;
306 jvo["orig_connecteds-in-path"] = co;
311 void RRTExt13::json(Json::Value jvi)
313 return RRTS::json(jvi);