4 std::vector<RRTNode *> RRTExt3::first_path_optimization()
6 if (this->orig_path().size() == 0) {
7 this->orig_path_ = RRTS::path();
8 if (this->orig_path().size() == 0)
9 return this->orig_path();
11 this->orig_path_cost(cc(*this->orig_path().back()));
13 class DijkstraNode : public RRTNode {
15 DijkstraNode *s = nullptr;
27 DijkstraNode(const RRTNode& n) {
35 DijkstraNode *p_ = nullptr;
36 DijkstraNode *p() const { return this->p_; }
37 void p(DijkstraNode *p) { this->p_ = p; }
39 class DijkstraNodeComparator {
42 const DijkstraNode &n1,
43 const DijkstraNode &n2
49 std::vector<DijkstraNode> dn;
50 dn.reserve(this->orig_path().size());
51 unsigned int dncnt = 0;
52 for (auto n: this->orig_path()) {
54 n->t(RRTNodeType::cusp)
55 || n->t(RRTNodeType::connected)
57 dn.push_back(DijkstraNode(*n));
58 dn.back().cc = cc(*n);
59 dn.back().s = &dn.back();
61 dn.back().i = dncnt++;
64 dn.push_back(DijkstraNode(*this->orig_path().back()));
65 dn.back().cc = cc(*this->orig_path().back());
66 dn.back().s = &dn.back();
67 dn.back().n = this->orig_path().back();
68 dn.back().i = dncnt++;
71 std::vector<DijkstraNode>,
72 DijkstraNodeComparator
77 DijkstraNode f = pq.top();
79 for (unsigned int i = f.i + 1; i < dncnt; i++) {
80 double cost = f.cc + this->cost_search(f, dn[i]);
81 this->steer(f, dn[i]);
82 if (this->steered().size() == 0)
83 break; // TODO why not continue?
84 if (std::get<0>(this->collide_steered_from(f)))
86 if (cost < dn[i].cc) {
94 DijkstraNode *ln = nullptr;
99 if (ln == nullptr || ln->p() == nullptr)
100 return this->orig_path();
101 while (ln->p() != nullptr) {
103 RRTNode *f = ln->p()->n;
105 if (std::get<0>(this->collide_steered_from(*f)))
106 return this->orig_path();
107 this->join_steered(f);
108 t->p(&this->nodes().back());
109 t->c(this->cost_build(this->nodes().back(), *t));
115 std::vector<RRTNode *> RRTExt3::second_path_optimization()
117 if (this->first_optimized_path().size() == 0) {
118 return this->orig_path();
120 class DijkstraNode : public RRTNode {
122 DijkstraNode *s = nullptr;
123 RRTNode *n = nullptr;
134 DijkstraNode(const RRTNode& n) {
142 DijkstraNode *p_ = nullptr;
143 DijkstraNode *p() const { return this->p_; }
144 void p(DijkstraNode *p) { this->p_ = p; }
146 class DijkstraNodeComparator {
149 const DijkstraNode &n1,
150 const DijkstraNode &n2
153 return n1.cc > n2.cc;
156 std::vector<DijkstraNode> dn;
157 dn.reserve(this->orig_path().size());
158 unsigned int dncnt = 0;
159 for (auto n: this->orig_path()) {
161 n->t(RRTNodeType::cusp)
162 || n->t(RRTNodeType::connected)
164 dn.push_back(DijkstraNode(*n));
165 dn.back().cc = cc(*n);
166 dn.back().s = &dn.back();
168 dn.back().i = dncnt++;
171 dn.push_back(DijkstraNode(*this->orig_path().back()));
172 dn.back().cc = cc(*this->orig_path().back());
173 dn.back().s = &dn.back();
174 dn.back().n = this->orig_path().back();
175 dn.back().i = dncnt++;
178 std::vector<DijkstraNode>,
179 DijkstraNodeComparator
184 while (!pq.empty()) {
185 DijkstraNode t = pq.top();
187 for (unsigned int i = t.i - 1; i > 0; i--) {
188 double cost = dn[i].cc + this->cost_search(dn[i], t);
189 this->steer(dn[i], t);
190 if (this->steered().size() == 0)
191 break; // TODO why not continue?
192 if (std::get<0>(this->collide_steered_from(dn[i])))
202 DijkstraNode *fn = nullptr;
209 if (fn == nullptr || fn->p() == nullptr)
210 return this->first_optimized_path();
211 DijkstraNode *ln = &dn.back();
212 while (ln->p() != fn) {
214 RRTNode *f = ln->p()->n;
216 if (std::get<0>(this->collide_steered_from(*f)))
217 return this->first_optimized_path();
218 this->join_steered(f);
219 t->p(&this->nodes().back());
220 t->c(this->cost_build(this->nodes().back(), *t));
226 std::vector<RRTNode *> RRTExt3::path()
228 this->first_optimized_path_ = this->first_path_optimization();
229 if (this->first_optimized_path_.size() > 0)
230 this->first_optimized_path_cost(
231 cc(*this->first_optimized_path_.back())
234 return this->orig_path();
235 return this->second_path_optimization();
238 Json::Value RRTExt3::json()
240 Json::Value jvo = RRTS::json();
241 jvo["orig_path_cost"] = this->orig_path_cost();
245 unsigned int pcnt = 0;
246 for (auto n: this->orig_path()) {
247 jvo["orig_path"][pcnt][0] = n->x();
248 jvo["orig_path"][pcnt][1] = n->y();
249 jvo["orig_path"][pcnt][2] = n->h();
250 if (n->t(RRTNodeType::cusp))
252 if (n->t(RRTNodeType::connected))
256 jvo["orig_cusps-in-path"] = cu;
257 jvo["orig_connecteds-in-path"] = co;
262 void RRTExt3::json(Json::Value jvi)
264 return RRTS::json(jvi);