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/>.
22 #include "slotplanner.h"
24 ParallelSlot::ParallelSlot()
28 std::vector<std::vector<RRTNode *>> &ParallelSlot::cusp()
33 float ParallelSlot::DH() const
38 PolygonObstacle &ParallelSlot::slot()
43 SlotType ParallelSlot::slotType()
45 if (!this->slotType_) {
47 this->slot().bnodes()[0],
48 this->slot().bnodes()[1]
51 this->slot().bnodes()[1],
52 this->slot().bnodes()[2]
55 this->slotType_ = PERPENDICULAR;
57 this->slotType_ = PARALLEL;
59 return this->slotType_;
63 void ParallelSlot::DH(float dh)
69 void ParallelSlot::fip()
71 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
72 // RRTNode.s() works as iteration level
73 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
75 // TODO add init nodes
76 // for now just copy fpose()
77 bool left = false; // right parking slot
79 BicycleCar *CC = new BicycleCar(
80 this->fposecenter()->x(),
81 this->fposecenter()->y() - 0.01,
84 BicycleCar *B = new BicycleCar(
85 CC->x() - CC->width() / 2,
86 CC->y() - (CC->length() + CC->wheelbase()) / 2,
89 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
94 CC->x() + CC->width() / 2,
95 CC->y() - (CC->length() + CC->wheelbase()) / 2,
99 this->DH(di * 0.01 / CC->out_radi());
102 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
103 while (!this->slot().collide(c->frame())) {
105 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
108 delete c; // not in q and collide
113 if (this->DH() > 0 && c->rfx() <= 0 && c->rrx() <= 0) {
114 goto createcuspandfinish;
115 } else if (this->DH() < 0 && c->lfx() >= 0 && c->lrx() >= 0) {
116 goto createcuspandfinish;
117 } else if (c->s() < 9) {
118 BicycleCar *cc = this->flnc(c);
123 delete c; // not in q and collide
127 std::vector<RRTNode *> cusp;
129 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
132 std::reverse(cusp.begin(), cusp.end());
133 this->cusp().push_back(cusp);
134 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
138 void ParallelSlot::fipr(BicycleCar *B)
140 // TODO for right parallel parking also
141 // it's only for lpar scenario now
143 std::vector<RRTNode *> cusp;
144 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
145 // just copied from fip()
146 this->DH(-0.01 / B->out_radi());
149 while (c->lfx() < 0) {
150 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
151 BicycleCar *cc = this->flncr(c);
156 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
157 std::reverse(cusp.begin(), cusp.end());
158 this->cusp().push_back(cusp);
161 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
163 // TODO find last not colliding
164 // for now just copy flast()
166 if (int(B->s()) % 2 == 0)
167 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
169 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
172 p = B->move(cc, i * this->DH());
173 while (!this->slot().collide(p->frame())
175 (this->DH() > 0 && p->x() <= 0)
176 || (this->DH() < 0 && p->x() >= 0)
180 p = B->move(cc, i * this->DH());
183 p = B->move(cc, i * this->DH());
184 while (!this->slot().collide(p->frame())
186 (this->DH() > 0 && p->x() <= 0)
187 || (this->DH() < 0 && p->x() >= 0)
189 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
193 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
199 p = B->move(cc, i * this->DH());
202 return B->move(cc, (i - 1) * this->DH());
205 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
207 // TODO find last not colliding
208 // for now just copy flast()
210 if (int(B->s()) % 2 == 0)
211 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
213 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
216 p = B->move(cc, i * this->DH());
217 while (!this->slot().collide(p->frame())
219 (this->DH() > 0 && p->x() >= 0)
220 || (this->DH() < 0 && p->lfx() <= 0)
224 p = B->move(cc, i * this->DH());
227 p = B->move(cc, i * this->DH());
228 while (!this->slot().collide(p->frame())) {
229 if (this->DH() > 0 && p->x() <= 0) {
233 if (this->DH() < 0 && p->lfx() >= 0) {
239 p = B->move(cc, i * this->DH());
242 return B->move(cc, (i - 1) * this->DH());
245 RRTNode *ParallelSlot::fposecenter()
247 return this->slot().bnodes().front();
250 bool ParallelSlot::flast(
254 std::vector<RRTNode *> &cusp
257 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
260 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
262 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
265 p = B->move(cc, i * this->DH());
266 while (!this->slot().collide(p->frame())
268 (this->DH() > 0 && p->x() <= 0)
269 || (this->DH() < 0 && p->x() >= 0)
273 p = B->move(cc, i * this->DH());
276 p = B->move(cc, i * this->DH());
277 while (!this->slot().collide(p->frame())
279 (this->DH() > 0 && p->x() <= 0)
280 || (this->DH() < 0 && p->x() >= 0)
282 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
286 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
292 p = B->move(cc, i * this->DH());
295 p = B->move(cc, (i - 1) * this->DH());
296 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
299 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
304 return this->flast(p, !right, il + 1, cusp);
309 void ParallelSlot::fpose()
311 bool left = false; // right parking slot
313 BicycleCar *CC = new BicycleCar(
314 this->fposecenter()->x(),
315 this->fposecenter()->y() - 0.01,
318 BicycleCar *B = new BicycleCar(
319 CC->x() - CC->width() / 2,
320 CC->y() - (CC->length() + CC->wheelbase()) / 2,
323 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
328 CC->x() + CC->width() / 2,
329 CC->y() - (CC->length() + CC->wheelbase()) / 2,
333 this->DH(di * 0.01 / CC->out_radi());
336 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
337 while (!this->slot().collide(p->frame())) {
338 std::vector<RRTNode *> tmpcusp;
339 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
340 if (this->flast(p, left, 0, tmpcusp)) {
341 this->cusp().push_back(tmpcusp);
346 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
350 struct SamplingInfo ParallelSlot::getSamplingInfo()
352 struct SamplingInfo si;
353 BicycleCar *CC = new BicycleCar(
354 this->fposecenter()->x(),
355 this->fposecenter()->y() - 0.01,
358 si.x = this->slot().bnodes()[0]->x();
359 si.y = this->slot().bnodes()[0]->y();
360 si.r = CC->diag_radi();
361 si.h = M_PI / 2 - acos(EDIST( // TODO generalize
362 this->slot().bnodes()[0],
363 this->slot().bnodes()[1]