]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - src/rrts.cc
Add steered1/2 code
[hubacji1/rrts.git] / src / rrts.cc
1 #include <algorithm>
2 #include "rrts.h"
3
4 #include "reeds_shepp.h"
5
6 template <typename T> int sgn(T val) {
7         return (T(0) < val) - (val < T(0));
8 }
9
10 RRTNode::RRTNode()
11 {
12 }
13
14 RRTNode::RRTNode(const BicycleCar &bc)
15 {
16     this->x(bc.x());
17     this->y(bc.y());
18     this->h(bc.h());
19     this->sp(bc.sp());
20     this->st(bc.st());
21 }
22
23 bool RRTNode::operator==(const RRTNode& n)
24 {
25         if (this == &n)
26                 return true;
27         return false;
28 }
29
30 Obstacle::Obstacle()
31 {
32 }
33
34 double RRTS::elapsed()
35 {
36         std::chrono::duration<double> dt;
37         dt = std::chrono::duration_cast<std::chrono::duration<double>>(
38                 std::chrono::high_resolution_clock::now()
39                 - this->tstart_
40         );
41         this->scnt_ = dt.count();
42         return this->scnt_;
43 }
44
45 void RRTS::log_path_cost()
46 {
47         this->log_path_cost_.push_back(cc(this->goals().front()));
48         this->log_path_time_ += 0.1;
49 }
50
51 bool RRTS::should_stop()
52 {
53         // the following counters must be updated, do not comment
54         this->icnt_++;
55         this->elapsed();
56         // current iteration stop conditions
57         if (this->should_finish()) return true;
58         if (this->should_break()) return true;
59         // but continue by default
60         return false;
61 }
62
63 bool RRTS::should_finish()
64 {
65         // decide finish conditions (maybe comment some lines)
66         //if (this->icnt_ > 999) return true;
67         if (this->scnt_ > 2) return true;
68         if (this->finishit) return true;
69         //if (this->gf()) return true;
70         // but continue by default
71         return false;
72 }
73
74 bool RRTS::should_break()
75 {
76         // decide break conditions (maybe comment some lines)
77         //if (this->scnt_ - this->pcnt_ > 2) return true;
78         // but continue by default
79         return false;
80 }
81
82 bool RRTS::should_continue()
83 {
84         // decide the stop conditions (maybe comment some lines)
85         // it is exact opposite of `should_stop`
86         //if (this->icnt_ > 999) return false;
87         if (this->scnt_ > 10) return false;
88         if (this->gf()) return false;
89         // and reset pause counter if should continue
90         this->pcnt_ = this->scnt_;
91         return true;
92 }
93
94 void RRTS::store_node(RRTNode n)
95 {
96         this->nodes().push_back(n);
97 }
98
99 // RRT procedures
100 std::tuple<bool, unsigned int, unsigned int>
101 RRTS::collide(std::vector<std::tuple<double, double>> &poly)
102 {
103         for (auto &o: this->obstacles())
104                 if (std::get<0>(::collide(poly, o.poly())))
105                         return ::collide(poly, o.poly());
106         return std::make_tuple(false, 0, 0);
107 }
108
109 std::tuple<bool, unsigned int, unsigned int>
110 RRTS::collide_steered_from(RRTNode &f)
111 {
112         auto fbc = BicycleCar();
113         fbc.x(f.x());
114         fbc.y(f.y());
115         fbc.h(f.h());
116         std::vector<std::tuple<double, double>> s;
117         s.push_back(std::make_tuple(fbc.x(), fbc.y()));
118         for (auto &n: this->steered()) {
119                 auto nbc = BicycleCar();
120                 nbc.x(n.x());
121                 nbc.y(n.y());
122                 nbc.h(n.h());
123                 s.push_back(std::make_tuple(nbc.lfx(), nbc.lfy()));
124                 s.push_back(std::make_tuple(nbc.lrx(), nbc.lry()));
125                 s.push_back(std::make_tuple(nbc.rrx(), nbc.rry()));
126                 s.push_back(std::make_tuple(nbc.rfx(), nbc.rfy()));
127         }
128         auto col = this->collide(s);
129         auto strip_from = this->steered().size() - std::get<1>(col) / 4;
130         if (std::get<0>(col) && strip_from > 0) {
131                 while (strip_from-- > 0) {
132                         this->steered().pop_back();
133                 }
134                 return this->collide_steered_from(f);
135         }
136         return col;
137 }
138
139 std::tuple<bool, unsigned int, unsigned int>
140 RRTS::collide_two_nodes(RRTNode &f, RRTNode &t)
141 {
142         auto fbc = BicycleCar();
143         fbc.x(f.x());
144         fbc.y(f.y());
145         fbc.h(f.h());
146         auto tbc = BicycleCar();
147         tbc.x(f.x());
148         tbc.y(f.y());
149         tbc.h(f.h());
150         std::vector<std::tuple<double, double>> p;
151         p.push_back(std::make_tuple(fbc.lfx(), fbc.lfy()));
152         p.push_back(std::make_tuple(fbc.lrx(), fbc.lry()));
153         p.push_back(std::make_tuple(fbc.rrx(), fbc.rry()));
154         p.push_back(std::make_tuple(fbc.rfx(), fbc.rfy()));
155         p.push_back(std::make_tuple(tbc.lfx(), tbc.lfy()));
156         p.push_back(std::make_tuple(tbc.lrx(), tbc.lry()));
157         p.push_back(std::make_tuple(tbc.rrx(), tbc.rry()));
158         p.push_back(std::make_tuple(tbc.rfx(), tbc.rfy()));
159         return this->collide(p);
160 }
161
162 double RRTS::cost_build(RRTNode &f, RRTNode &t)
163 {
164         double cost = 0;
165         cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
166         return cost;
167 }
168
169 double RRTS::cost_search(RRTNode &f, RRTNode &t)
170 {
171         double cost = 0;
172         cost = sqrt(pow(t.y() - f.y(), 2) + pow(t.x() - f.x(), 2));
173         return cost;
174 }
175
176 void RRTS::sample()
177 {
178         double x = 0;
179         double y = 0;
180         double h = 0;
181         switch (this->sample_dist_type()) {
182         case 1: // uniform
183                 x = this->udx_(this->gen_);
184                 y = this->udy_(this->gen_);
185                 h = this->udh_(this->gen_);
186                 break;
187         case 2: // uniform circle
188         {
189                 // see https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409
190                 double R = sqrt(
191                         pow(
192                                 this->nodes().front().x()
193                                 - this->goals().front().x(),
194                                 2
195                         )
196                         + pow(
197                                 this->nodes().front().y()
198                                 - this->goals().front().y(),
199                                 2
200                         )
201                 );
202                 double a = atan2(
203                         this->goals().front().y() - this->nodes().front().y(),
204                         this->goals().front().x() - this->nodes().front().x()
205                 );
206                 double cx = this->goals().front().x() - R/2 * cos(a);
207                 double cy = this->goals().front().y() - R/2 * sin(a);
208                 double r = R * sqrt(this->udx_(this->gen_));
209                 double theta = this->udy_(this->gen_) * 2 * M_PI;
210                 x = cx + r * cos(theta);
211                 y = cy + r * sin(theta);
212                 h = this->udh_(this->gen_);
213         }
214                 break;
215         case 3: {
216                 if (
217                         this->steered1_.size() == 0
218                         && this->steered2_.size() == 0
219                 ) {
220                         x = this->nodes().front().x();
221                         y = this->nodes().front().y();
222                         h = this->nodes().front().h();
223                 } else {
224                         this->udi1_ = std::uniform_int_distribution<unsigned int>(
225                                 0,
226                                 this->steered1_.size() - 1
227                         );
228                         this->udi2_ = std::uniform_int_distribution<unsigned int>(
229                                 0,
230                                 this->steered2_.size() - 1
231                         );
232                         auto ind1 = this->udi1_(this->gen_);
233                         auto ind2 = this->udi2_(this->gen_);
234                         if (
235                                 this->steered2_.size() == 0
236                         ) {
237                                 auto n1 = this->steered1_[ind1];
238                                 x = n1->x();
239                                 y = n1->y();
240                                 h = n1->h();
241                         } else if (
242                                 this->steered1_.size() == 0
243                         ) {
244                                 auto n2 = this->steered2_[ind2];
245                                 x = n2->x();
246                                 y = n2->y();
247                                 h = n2->h();
248                         } else {
249                                 auto n1 = this->steered1_[ind1];
250                                 auto n2 = this->steered2_[ind2];
251                                 auto which = this->udx_(this->gen_);
252                                 if (which > 0.5) {
253                                         x = n1->x();
254                                         y = n1->y();
255                                         h = n1->h();
256                                 } else {
257                                         x = n2->x();
258                                         y = n2->y();
259                                         h = n2->h();
260                                 }
261                         }
262                 }
263                 break;
264                 }
265         default: // normal
266                 x = this->ndx_(this->gen_);
267                 y = this->ndy_(this->gen_);
268                 h = this->ndh_(this->gen_);
269         }
270         this->samples().push_back(RRTNode());
271         this->samples().back().x(x);
272         this->samples().back().y(y);
273         this->samples().back().h(h);
274 }
275
276 RRTNode *RRTS::nn(RRTNode &t)
277 {
278         RRTNode *nn = &this->nodes().front();
279         double cost = this->cost_search(*nn, t);
280         for (auto &f: this->nodes()) {
281                 if (this->cost_search(f, t) < cost) {
282                         nn = &f;
283                         cost = this->cost_search(f, t);
284                 }
285         }
286         return nn;
287 }
288
289 std::vector<RRTNode *> RRTS::nv(RRTNode &t)
290 {
291         std::vector<RRTNode *> nv;
292         double cost = std::min(GAMMA(this->nodes().size()), ETA);
293         for (auto &f: this->nodes())
294                 if (this->cost_search(f, t) < cost)
295                         nv.push_back(&f);
296         return nv;
297 }
298
299 int cb_rs_steer(double q[4], void *user_data)
300 {
301         std::vector<RRTNode> *nodes = (std::vector<RRTNode> *) user_data;
302         nodes->push_back(RRTNode());
303         nodes->back().x(q[0]);
304         nodes->back().y(q[1]);
305         nodes->back().h(q[2]);
306         nodes->back().sp(q[3]);
307         if (nodes->back().sp() == 0) {
308                 nodes->back().set_t(RRTNodeType::cusp);
309         } else if (nodes->size() >= 2) {
310                 RRTNode* lln = nodes->back().p();
311                 RRTNode* ln = &nodes->back();
312                 if (lln != nullptr && ln != nullptr && sgn(lln->sp()) != sgn(ln->sp()))
313                         ln->set_t(RRTNodeType::cusp);
314         }
315         return 0;
316 }
317
318 void RRTS::steer(RRTNode &f, RRTNode &t)
319 {
320         this->steered().clear();
321         double q0[] = {f.x(), f.y(), f.h()};
322         double q1[] = {t.x(), t.y(), t.h()};
323         ReedsSheppStateSpace rsss(this->bc.mtr());
324         rsss.sample(q0, q1, 0.2, cb_rs_steer, &this->steered());
325 }
326
327 void RRTS::steer1(RRTNode &f, RRTNode &t)
328 {
329     return this->steer(f, t);
330 }
331
332 void RRTS::steer2(RRTNode &f, RRTNode &t)
333 {
334     return this->steer(f, t);
335 }
336
337 void RRTS::join_steered(RRTNode *f)
338 {
339         while (this->steered().size() > 0) {
340                 this->store_node(this->steered().front());
341                 RRTNode *t = &this->nodes().back();
342                 t->p(f);
343                 t->c(this->cost_build(*f, *t));
344                 this->steered().erase(this->steered().begin());
345                 f = t;
346         }
347 }
348
349 bool RRTS::goal_found(RRTNode &f)
350 {
351         auto &g = this->goals().front();
352         double cost = this->cost_build(f, g);
353         double edist = sqrt(
354                 pow(f.x() - g.x(), 2)
355                 + pow(f.y() - g.y(), 2)
356         );
357         double adist = std::abs(f.h() - g.h());
358         if (edist < 0.05 && adist < M_PI / 32) {
359                 if (g.p() == nullptr || cc(f) + cost < cc(g)) {
360                         g.p(&f);
361                         g.c(cost);
362                 }
363                 return true;
364         }
365         return false;
366 }
367
368 // RRT* procedures
369 bool RRTS::connect()
370 {
371         RRTNode *t = &this->steered().front();
372         RRTNode *f = this->nn(this->samples().back());
373         double cost = this->cost_search(*f, *t);
374         for (auto n: this->nv(*t)) {
375                 if (
376                         !std::get<0>(this->collide_two_nodes(*n, *t))
377                         && this->cost_search(*n, *t) < cost
378                 ) {
379                         f = n;
380                         cost = this->cost_search(*n, *t);
381                 }
382         }
383         this->store_node(this->steered().front());
384         t = &this->nodes().back();
385         t->p(f);
386         t->c(this->cost_build(*f, *t));
387         t->set_t(RRTNodeType::connected);
388         return true;
389 }
390
391 void RRTS::rewire()
392 {
393         RRTNode *f = &this->nodes().back();
394         for (auto n: this->nv(*f)) {
395                 if (
396                         !std::get<0>(this->collide_two_nodes(*f, *n))
397                         && cc(*f) + this->cost_search(*f, *n) < cc(*n)
398                 ) {
399                         n->p(f);
400                         n->c(this->cost_build(*f, *n));
401                 }
402         }
403 }
404
405 // API
406 void RRTS::init()
407 {
408 }
409
410 void RRTS::deinit()
411 {
412         this->nodes().clear();
413         this->samples().clear();
414         this->steered().clear();
415         this->store_node(RRTNode()); // root
416         this->icnt_ = 0;
417         this->scnt_ = 0;
418         this->pcnt_ = 0;
419         this->gf_ = false;
420 }
421
422 std::vector<RRTNode *> RRTS::path()
423 {
424         std::vector<RRTNode *> path;
425         if (this->goals().size() == 0)
426                 return path;
427         RRTNode *goal = &this->goals().back();
428         if (goal->p() == nullptr)
429                 return path;
430         while (goal != nullptr) {
431                 path.push_back(goal);
432                 goal = goal->p();
433         }
434         std::reverse(path.begin(), path.end());
435         return path;
436 }
437
438 bool RRTS::next()
439 {
440         if (this->icnt_ == 0)
441                 this->tstart_ = std::chrono::high_resolution_clock::now();
442         bool next = true;
443         if (this->scnt_ > this->log_path_time_)
444             this->log_path_cost();
445         if (this->should_stop())
446                 return false;
447         if (this->samples().size() == 0) {
448                 this->samples().push_back(RRTNode());
449                 this->samples().back().x(this->goals().front().x());
450                 this->samples().back().y(this->goals().front().y());
451                 this->samples().back().h(this->goals().front().h());
452         } else {
453                 this->sample();
454         }
455         this->steer1(
456                 *this->nn(this->samples().back()),
457                 this->samples().back()
458         );
459         if (this->steered().size() == 0)
460                 return next;
461         auto col = this->collide_steered_from(
462                 *this->nn(this->samples().back())
463         );
464         if (std::get<0>(col)) {
465                 auto rcnt = this->steered().size() - std::get<1>(col);
466                 while (rcnt-- > 0) {
467                         this->steered().pop_back();
468                 }
469         }
470         if (!this->connect())
471                 return next;
472         this->rewire();
473         unsigned scnt = this->steered().size();
474         this->join_steered(&this->nodes().back());
475         RRTNode *just_added = &this->nodes().back();
476         while (scnt > 0) {
477                 // store all the steered1 nodes
478                 this->steered1_.push_back(just_added);
479                 scnt--;
480                 auto &g = this->goals().front();
481                 this->steer2(*just_added, g);
482                 auto col = this->collide_steered_from(*just_added);
483                 if (std::get<0>(col)) {
484                         auto rcnt = this->steered().size() - std::get<1>(col);
485                         while (rcnt-- > 0) {
486                                 this->steered().pop_back();
487                         }
488                 }
489                 this->join_steered(just_added);
490                 // store all the steered2 nodes
491                 RRTNode* jap = &this->nodes().back();
492                 while (jap != just_added) {
493                         this->steered2_.push_back(jap);
494                         jap = jap->p();
495                 }
496                 this->gf(this->goal_found(this->nodes().back()));
497                 just_added = just_added->p();
498         }
499         return next;
500 }
501
502 void RRTS::set_sample_normal(
503         double mx, double dx,
504         double my, double dy,
505         double mh, double dh
506 )
507 {
508         this->ndx_ = std::normal_distribution<double>(mx, dx);
509         this->ndy_ = std::normal_distribution<double>(my, dy);
510         this->ndh_ = std::normal_distribution<double>(mh, dh);
511 }
512 void RRTS::set_sample_uniform(
513         double xmin, double xmax,
514         double ymin, double ymax,
515         double hmin, double hmax
516 )
517 {
518         this->udx_ = std::uniform_real_distribution<double>(xmin,xmax);
519         this->udy_ = std::uniform_real_distribution<double>(ymin,ymax);
520         this->udh_ = std::uniform_real_distribution<double>(hmin,hmax);
521 }
522 void RRTS::set_sample_uniform_circle()
523 {
524         this->udx_ = std::uniform_real_distribution<double>(0, 1);
525         this->udy_ = std::uniform_real_distribution<double>(0, 1);
526         this->udh_ = std::uniform_real_distribution<double>(0, 2 * M_PI);
527 }
528 void RRTS::set_sample(
529         double x1, double x2,
530         double y1, double y2,
531         double h1, double h2
532 )
533 {
534         switch (this->sample_dist_type()) {
535         case 1: // uniform
536                 x1 += this->nodes().front().x();
537                 x2 += this->nodes().front().x();
538                 y1 += this->nodes().front().y();
539                 y2 += this->nodes().front().y();
540                 this->set_sample_uniform(x1, x2, y1, y2, h1, h2);
541                 break;
542         case 2: // uniform circle
543                 this->set_sample_uniform_circle();
544                 break;
545         case 3: // uniform index of node in nodes
546                 this->set_sample_uniform_circle();
547                 break;
548         default: // normal
549                 this->set_sample_normal(x1, x2, y1, y2, h1, h2);
550         }
551 }
552
553 Json::Value RRTS::json()
554 {
555         Json::Value jvo;
556         {
557                 jvo["time"] = this->scnt();
558         }
559         {
560                 jvo["iterations"] = this->icnt();
561         }
562         {
563                 jvo["init"][0] = this->nodes().front().x();
564                 jvo["init"][1] = this->nodes().front().y();
565                 jvo["init"][2] = this->nodes().front().h();
566         }
567         {
568                 jvo["path_cost_before_opt"] = this->path_cost_before_opt_;
569         }
570         {
571                 if (this->path().size() > 0) {
572                         jvo["cost"] = cc(*this->path().back());
573                         jvo["entry"][0] = this->goals().front().x();
574                         jvo["entry"][1] = this->goals().front().y();
575                         jvo["entry"][2] = this->goals().front().h();
576                         jvo["goal"][0] = this->goals().back().x();
577                         jvo["goal"][1] = this->goals().back().y();
578                         jvo["goal"][2] = this->goals().back().h();
579                 }
580         }
581         {
582                 unsigned int cu = 0;
583                 unsigned int co = 0;
584                 unsigned int pcnt = 0;
585                 for (auto n: this->path()) {
586                         jvo["path"][pcnt][0] = n->x();
587                         jvo["path"][pcnt][1] = n->y();
588                         jvo["path"][pcnt][2] = n->h();
589                         if (n->t(RRTNodeType::cusp))
590                                 cu++;
591                         if (n->t(RRTNodeType::connected))
592                                 co++;
593                         pcnt++;
594                 }
595                 jvo["cusps-in-path"] = cu;
596                 jvo["connecteds-in-path"] = co;
597         }
598         {
599                 unsigned int gcnt = 0;
600                 for (auto g: this->goals()) {
601                         jvo["goals"][gcnt][0] = g.x();
602                         jvo["goals"][gcnt][1] = g.y();
603                         jvo["goals"][gcnt][2] = g.h();
604                         gcnt++;
605                 }
606         }
607         {
608                 unsigned int ocnt = 0;
609                 for (auto o: this->obstacles()) {
610                         unsigned int ccnt = 0;
611                         for (auto c: o.poly()) {
612                                 jvo["obst"][ocnt][ccnt][0] = std::get<0>(c);
613                                 jvo["obst"][ocnt][ccnt][1] = std::get<1>(c);
614                                 ccnt++;
615                         }
616                         ocnt++;
617                 }
618         }
619         {
620                 jvo["nodes"] = (unsigned int) this->nodes().size();
621         }
622         {
623                 unsigned int cnt = 0;
624                 for (auto i: this->log_path_cost_)
625                         jvo["log_path_cost"][cnt++] = i;
626         }
627         //{
628         //        unsigned int ncnt = 0;
629         //        for (auto n: this->nodes()) {
630         //                jvo["nodes_x"][ncnt] = n.x();
631         //                jvo["nodes_y"][ncnt] = n.y();
632         //                //jvo["nodes_h"][ncnt] = n.h();
633         //                ncnt++;
634         //        }
635         //}
636         //{
637         //        unsigned int ncnt = 0;
638         //        for (auto n: this->steered1_) {
639         //                jvo["steered1_x"][ncnt] = n->x();
640         //                jvo["steered1_y"][ncnt] = n->y();
641         //                //jvo["nodes_h"][ncnt] = n.h();
642         //                ncnt++;
643         //        }
644         //        ncnt = 0;
645         //        for (auto n: this->steered2_) {
646         //                jvo["steered2_x"][ncnt] = n->x();
647         //                jvo["steered2_y"][ncnt] = n->y();
648         //                //jvo["nodes_h"][ncnt] = n.h();
649         //                ncnt++;
650         //        }
651         //}
652         return jvo;
653 }
654
655 void RRTS::json(Json::Value jvi)
656 {
657         assert(jvi["init"] != Json::nullValue);
658         assert(jvi["goals"] != Json::nullValue);
659         assert(jvi["obst"] != Json::nullValue);
660
661         this->nodes().front().x(jvi["init"][0].asDouble());
662         this->nodes().front().y(jvi["init"][1].asDouble());
663         this->nodes().front().h(jvi["init"][2].asDouble());
664         {
665                 RRTNode* gp = nullptr;
666                 if (jvi["entry"] != Json::nullValue) {
667                         this->entry_set = true;
668                         this->entry.x = jvi["entry"][0].asDouble();
669                         this->entry.y = jvi["entry"][1].asDouble();
670                         this->entry.b = jvi["entry"][2].asDouble();
671                         this->entry.e = jvi["entry"][3].asDouble();
672                         RRTNode tmp_node;
673                         tmp_node.x(this->entry.x);
674                         tmp_node.y(this->entry.y);
675                         tmp_node.h((this->entry.b + this->entry.e) / 2.0);
676                         this->goals().push_back(tmp_node);
677                         this->goals().back().p(gp);
678                         gp = &this->goals().back();
679                 }
680                 for (auto g: jvi["goals"]) {
681                         RRTNode tmp_node;
682                         tmp_node.x(g[0].asDouble());
683                         tmp_node.y(g[1].asDouble());
684                         tmp_node.h(g[2].asDouble());
685                         this->goals().push_back(tmp_node);
686                         this->goals().back().p(gp);
687                         gp = &this->goals().back();
688                 }
689                 this->goals().front().set_t(RRTNodeType::cusp);
690                 this->goals().back().set_t(RRTNodeType::cusp);
691         }
692         {
693                 Obstacle tmp_obstacle;
694                 for (auto o: jvi["obst"]) {
695                         tmp_obstacle.poly().clear();
696                         for (auto c: o) {
697                                 double tmp_x = c[0].asDouble();
698                                 double tmp_y = c[1].asDouble();
699                                 auto tmp_tuple = std::make_tuple(tmp_x, tmp_y);
700                                 tmp_obstacle.poly().push_back(tmp_tuple);
701                         }
702                         this->obstacles().push_back(tmp_obstacle);
703                 }
704         }
705         {
706                 double edist_init_goal = sqrt(
707                         pow(
708                                 this->nodes().front().x()
709                                 - this->goals().front().x(),
710                                 2
711                         )
712                         + pow(
713                                 this->nodes().front().y()
714                                 - this->goals().front().y(),
715                                 2
716                         )
717                 );
718                 this->set_sample(
719                         this->nodes().front().x(), edist_init_goal,
720                         this->nodes().front().y(), edist_init_goal,
721                         0, 2 * M_PI
722                 );
723         }
724 }
725
726 RRTS::RRTS()
727         : gen_(std::random_device{}())
728 {
729         this->goals().reserve(100);
730         this->nodes().reserve(4000000);
731         this->samples().reserve(1000);
732         this->steered().reserve(20000);
733         this->store_node(RRTNode()); // root
734 }
735
736 double cc(RRTNode &t)
737 {
738         RRTNode *n = &t;
739         double cost = 0;
740         while (n != nullptr) {
741                 cost += n->c();
742                 n = n->p();
743         }
744         return cost;
745 }