2 This file is part of I am car.
4 I am car is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
24 #include "rrtplanner.h"
27 #define KUWATA2008_CCOST co3
28 #define KUWATA2008_DCOST co1
30 LaValle1998::LaValle1998(RRTNode *init, RRTNode *goal):
37 srand(static_cast<unsigned>(time(0)));
40 bool LaValle1998::next()
42 RRTNode *rs = this->sample();
43 this->samples().push_back(rs);
44 RRTNode *nn = this->nn(this->root(), rs, this->cost);
46 for (auto ns: this->steer(nn, rs)) {
47 this->nodes().push_back(ns);
48 pn->add_child(ns, this->cost(pn, ns));
49 if (this->collide(pn, ns)) {
50 pn->children().pop_back();
54 if (this->goal_found(pn, this->cost)) {
55 this->tlog(this->findt());
59 return this->goal_found();
62 Kuwata2008::Kuwata2008(RRTNode *init, RRTNode *goal):
67 cost(KUWATA2008_DCOST)
69 srand(static_cast<unsigned>(time(0)));
72 bool Kuwata2008::next()
75 if (this->samples().size() == 0) {
80 this->samples().push_back(rs);
81 float heur = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
82 if (this->goal_found()) {
84 this->cost = &KUWATA2008_CCOST;
86 this->cost = &KUWATA2008_DCOST;
89 this->cost = &KUWATA2008_CCOST;
91 this->cost = &KUWATA2008_DCOST;
93 RRTNode *nn = this->nn(this->root(), rs, this->cost);
95 std::vector<RRTNode *> newly_added;
96 for (auto ns: this->steer(nn, rs)) {
97 this->nodes().push_back(ns);
98 pn->add_child(ns, KUWATA2008_DCOST(pn, ns));
99 if (this->collide(pn, ns)) {
100 pn->children().pop_back();
104 newly_added.push_back(pn);
105 if (this->goal_found(pn, &KUWATA2008_DCOST)) {
106 this->tlog(this->findt());
110 if (this->samples().size() > 1) {
111 for (auto na: newly_added) {
113 for (auto ns: this->steer(na, this->goal())) {
114 this->nodes().push_back(ns);
115 pn->add_child(ns, KUWATA2008_DCOST(pn, ns));
116 if (this->collide(pn, ns)) {
117 pn->children().pop_back();
121 if (this->goal_found(pn, &KUWATA2008_DCOST)) {
122 this->tlog(this->findt());
128 return this->goal_found();
131 Karaman2011::Karaman2011(RRTNode *init, RRTNode *goal):
139 srand(static_cast<unsigned>(time(0)));
142 bool Karaman2011::next()
144 RRTNode *rs = this->sample();
145 this->samples().push_back(rs);
146 RRTNode *nn = this->nn(this->root(), rs, this->cost);
148 std::vector<RRTNode *> nvs;
150 RRTNode *op; // old parent
151 float od; // old direct cost
152 float oc; // old cumulative cost
153 for (auto ns: this->steer(nn, rs)) {
154 nvs = this->nv(this->root(), ns, this->cost, MIN(
155 GAMMA_RRTSTAR(this->nodes().size()),
157 this->nodes().push_back(ns);
159 pn->add_child(ns, this->cost(pn, ns));
160 if (this->collide(pn, ns)) {
161 pn->children().pop_back();
167 if (!connected || (nv->ccost() + this->cost(nv, ns) <
172 nv->add_child(ns, this->cost(nv, ns));
173 if (this->collide(nv, ns)) {
174 nv->children().pop_back();
178 } else if (connected) {
179 op->children().pop_back();
189 if (ns->ccost() + this->cost(ns, nv) < nv->ccost()) {
193 ns->add_child(nv, this->cost(ns, nv));
194 if (this->collide(ns, nv)) {
195 ns->children().pop_back();
205 if (this->goal_found(pn, this->cost)) {
206 this->tlog(this->findt());
210 return this->goal_found();
213 T1::T1(RRTNode *init, RRTNode *goal):
221 srand(static_cast<unsigned>(time(0)));
227 if (this->samples().size() == 0)
231 this->samples().push_back(rs);
232 RRTNode *nn = this->nn(this->root(), rs, this->cost);
234 std::vector<RRTNode *> nvs;
236 RRTNode *op; // old parent
237 float od; // old direct cost
238 float oc; // old cumulative cost
239 std::vector<RRTNode *> steered = this->steer(nn, rs);
240 // RRT* for first node
241 RRTNode *ns = steered[0];
243 nvs = this->nv(this->root(), ns, this->cost, MIN(
244 GAMMA_RRTSTAR(this->nodes().size()),
246 this->nodes().push_back(ns);
248 pn->add_child(ns, this->cost(pn, ns));
249 if (this->collide(pn, ns)) {
250 pn->children().pop_back();
256 if (!connected || (nv->ccost() + this->cost(nv, ns) <
261 nv->add_child(ns, this->cost(nv, ns));
262 if (this->collide(nv, ns)) {
263 nv->children().pop_back();
267 } else if (connected) {
268 op->children().pop_back();
278 if (ns->ccost() + this->cost(ns, nv) < nv->ccost()) {
282 ns->add_child(nv, this->cost(ns, nv));
283 if (this->collide(ns, nv)) {
284 ns->children().pop_back();
294 if (this->goal_found(pn, this->cost)) {
295 this->tlog(this->findt());
299 for (i = 1; i < steered.size(); i++) {
301 this->nodes().push_back(ns);
302 pn->add_child(ns, this->cost(pn, ns));
303 if (this->collide(pn, ns)) {
304 pn->children().pop_back();
308 if (this->goal_found(pn, this->cost)) {
309 this->tlog(this->findt());
313 return this->goal_found();