]> rtime.felk.cvut.cz Git - hubacji1/psp.git/blob - src/psp.cc
23217588faf20cf10226122c3edf05484f876fc6
[hubacji1/psp.git] / src / psp.cc
1 #include <cmath>
2 #include <list>
3 #include <queue>
4 #include "psp.h"
5
6 bool PSPlanner::collide()
7 {
8         std::vector<std::tuple<double, double>> bc;
9         bc.push_back(std::make_tuple(this->cc().lfx(), this->cc().lfy()));
10         bc.push_back(std::make_tuple(this->cc().lrx(), this->cc().lry()));
11         bc.push_back(std::make_tuple(this->cc().rrx(), this->cc().rry()));
12         bc.push_back(std::make_tuple(this->cc().rfx(), this->cc().rfy()));
13         bc.push_back(std::make_tuple(this->cc().lfx(), this->cc().lfy()));
14         std::vector<std::tuple<double, double>> ps;
15         ps.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
16         ps.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
17         ps.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
18         ps.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
19         return std::get<0>(::collide(bc, ps));
20 }
21
22 bool PSPlanner::forward()
23 {
24         if (this->ps().parallel())
25                 return false;
26         else
27                 return true;
28         double heading = atan2(
29                 this->ps().y2() - this->ps().y1(),
30                 this->ps().x2() - this->ps().x1()
31         );
32         while (heading < 0) heading += 2 * M_PI;
33         double h = this->gc().h();
34         while (h < 0) h += 2 * M_PI;
35         if (std::abs(heading - h) < M_PI / 4)
36                 return true;
37         return false;
38 }
39
40 void PSPlanner::gc_to_4()
41 {
42         double angl_slot = atan2(
43                 this->ps().y3() - this->ps().y4(),
44                 this->ps().x3() - this->ps().x4()
45         );
46         double angl_delta = M_PI / 2;
47         if (this->ps().right())
48                 angl_delta = -M_PI / 2;
49         double x = this->ps().x4();
50         double y = this->ps().y4();
51         x += (this->gc().dr() + 0.01) * cos(angl_slot);
52         y += (this->gc().dr() + 0.01) * sin(angl_slot);
53         x += (this->gc().w() / 2 + 0.01) * cos(angl_slot + angl_delta);
54         y += (this->gc().w() / 2 + 0.01) * sin(angl_slot + angl_delta);
55         this->gc().x(x);
56         this->gc().y(y);
57         this->gc().h(angl_slot);
58 }
59
60 std::tuple<double, double, double, double> circle_line_intersection(
61         double cx, double cy, double r,
62         double x1, double y1,
63         double x2, double y2
64 )
65 {
66         double t = (y2 - y1) / (x2 - x1);
67         //double a = 1 + pow(t, 2);
68         //double b = - 2 * cx - 2 * pow(t, 2) * x1 + 2 * t * y1 - 2 * t * cy;
69         //double c = pow(cx, 2) + pow(t, 2) * pow(x1, 2) - 2 * t * y1 * x1
70         //        + pow(y1, 2) + 2 * t * cy * x1 - 2 * y1 * cy + pow(cy, 2)
71         //        - pow(r, 2);
72         double a = 1 + pow(t, 2);
73         double b = - 2 * cx + 2 * t * (-t * x1 + y1) - 2 * cy * t;
74         double c = pow(cx, 2) + pow(cy, 2) - pow(r, 2);
75         c += pow(-t * x1 + y1, 2);
76         c += 2 * cy * t * x1 - 2 * cy * y1;
77         double D = pow(b, 2) - 4 * a * c;
78         if (D < 0)
79                 return std::make_tuple(cx, cy, cx, cy);
80         double res_x1 = (-b + sqrt(D)) / (2 * a);
81         double res_y1 = t * (res_x1 - x1) + y1;
82         double res_x2 = (-b - sqrt(D)) / (2 * a);
83         double res_y2 = t * (res_x2 - x1) + y1;
84         return std::make_tuple(res_x1, res_y1, res_x2, res_y2);
85 }
86
87 double edist(double x1, double y1, double x2, double y2)
88 {
89         return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
90 }
91
92 void PSPlanner::guess_gc()
93 {
94         double x = this->ps().x1();
95         double y = this->ps().y1();
96         double h = this->ps().heading();
97         double dts = + M_PI / 2; // direction to slot
98         if (this->ps().right())
99                 dts = - M_PI / 2;
100         if (this->ps().parallel()) {
101                 x += (this->gc().w() / 2 + 0.01) * cos(h + dts);
102                 x += (this->gc().dr() + 0.01) * cos(h);
103                 y += (this->gc().w() / 2 + 0.01) * sin(h + dts);
104                 y += (this->gc().dr() + 0.01) * sin(h);
105         } else {
106                 // Forward parking
107                 double entry_width = edist(
108                         this->ps().x1(), this->ps().y1(),
109                         this->ps().x4(), this->ps().y4()
110                 );
111                 x += entry_width / 2 * cos(h);
112                 y += entry_width / 2 * sin(h);
113                 h = atan2(
114                         this->ps().y2() - this->ps().y1(),
115                         this->ps().x2() - this->ps().x1()
116                 );
117                 while (h < 0) h += 2 * M_PI;
118
119                 //// This is for backward parking only.
120                 //double entry_width = edist(
121                 //        this->ps().x1(), this->ps().y1(),
122                 //        this->ps().x4(), this->ps().y4()
123                 //);
124                 //double dist_l =
125                 //        this->gc().orradi()
126                 //        - (this->gc().mtr() + this->gc().w() / 2)
127                 //;
128                 //double move1 = dist_l + this->gc().w() / 2;
129                 //double dist_r = entry_width - this->gc().w() - dist_l;
130                 //double move2 = sqrt(
131                 //        pow(this->gc().iradi(), 2)
132                 //        - pow(this->gc().iradi() - dist_r, 2)
133                 //);
134                 //move2 -= this->gc().dr() / 2; // workaround
135                 //x += move1 * cos(h);
136                 //y += move1 * sin(h);
137                 //dts = atan2(
138                 //        this->ps().y2() - this->ps().y1(),
139                 //        this->ps().x2() - this->ps().x1()
140                 //);
141                 //while (dts < 0) dts += 2 * M_PI;
142                 //x += move2 * cos(h + dts);
143                 //y += move2 * sin(h + dts);
144                 //h += dts - M_PI / 2;
145         }
146         while (h > M_PI)
147                 h -= 2 * M_PI;
148         while (h <= -M_PI)
149                 h += 2 * M_PI;
150         this->gc().x(x);
151         this->gc().y(y);
152         this->gc().h(h);
153 }
154
155 std::vector<BicycleCar> PSPlanner::last_maneuver()
156 {
157         std::vector<BicycleCar> lm;
158         if (this->ps().parallel()) {
159                 // zig-zag out from the slot
160                 this->cc() = BicycleCar(this->gc());
161                 this->cc().sp(0.1);
162                 while (!this->left()) {
163                         while (!this->collide() && !this->left()) {
164                                 this->cc().next();
165                                 lm.push_back(BicycleCar(this->cc()));
166                         }
167                         if (this->left() && !this->collide()) {
168                                 break;
169                         } else {
170                                 lm.pop_back();
171                                 this->cc().sp(this->cc().sp() * -1);
172                                 this->cc().next();
173                                 this->cc().st(this->cc().st() * -1);
174                                 this->c_++;
175                                 lm.push_back(BicycleCar(this->cc()));
176                         }
177                 }
178                 if (this->cc().st() < 0) {
179                         this->c_++;
180                         lm.push_back(BicycleCar(this->cc()));
181                 }
182         } else {
183                 // go 1 m forward
184                 this->cc().sp(0.1);
185                 BicycleCar orig_cc(this->cc());
186                 for (unsigned int i = 0; i < 10; i++) {
187                         this->cc().next();
188                         lm.push_back(BicycleCar(this->cc()));
189                 }
190                 this->cc() = BicycleCar(orig_cc);
191         }
192         return lm;
193 }
194
195 bool PSPlanner::left()
196 {
197         double lfx = this->cc().lfx();
198         double lfy = this->cc().lfy();
199         double lrx = this->cc().lrx();
200         double lry = this->cc().lry();
201         double rrx = this->cc().rrx();
202         double rry = this->cc().rry();
203         double rfx = this->cc().rfx();
204         double rfy = this->cc().rfy();
205         double lfs = sgn(
206                 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
207                 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
208         );
209         double lrs = sgn(
210                 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
211                 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
212         );
213         double rrs = sgn(
214                 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
215                 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
216         );
217         double rfs = sgn(
218                 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
219                 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
220         );
221         if (this->ps().parallel())
222                 return lfs == rfs && (lfs != lrs || lfs != rrs);
223         else if (!this->forward())
224                 return lfs == rfs && (lfs != lrs || lfs != rrs);
225         else
226                 return lrs == rrs && (lrs != lfs || lrs != rfs);
227 }
228
229 bool PSPlanner::parked()
230 {
231         std::vector<std::tuple<double, double>> slot;
232         slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
233         slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
234         slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
235         slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
236         return inside(this->gc().lfx(), this->gc().lfy(), slot)
237                 && inside(this->gc().lrx(), this->gc().lry(), slot)
238                 && inside(this->gc().rrx(), this->gc().rry(), slot)
239                 && inside(this->gc().rfx(), this->gc().rfy(), slot);
240 }
241
242 std::vector<BicycleCar> PSPlanner::possible_goals(
243         unsigned int cnt,
244         double dist
245 )
246 {
247         std::vector<BicycleCar> pi;
248         if (this->ps().parallel())
249                 this->cc().sp(1);
250         else
251                 this->cc().sp(-1);
252         this->cc().sp(this->cc().sp() * dist);
253         BicycleCar orig_cc(this->cc());
254         for (unsigned int i = 0; i < cnt; i++) {
255                 this->cc().next();
256                 pi.push_back(BicycleCar(this->cc()));
257         }
258         this->cc() = BicycleCar(orig_cc);
259         if (this->ps().parallel()) {
260                 this->cc().st(0);
261                 for (unsigned int i = 0; i < cnt; i++) {
262                         this->cc().next();
263                         pi.push_back(BicycleCar(this->cc()));
264                 }
265                 this->cc() = BicycleCar(orig_cc);
266         } else {
267                 if (!this->ps().right()) {
268                         this->cc().set_max_steer();
269                         for (unsigned int i = 0; i < cnt; i++) {
270                                 this->cc().next();
271                                 pi.push_back(BicycleCar(this->cc()));
272                         }
273                 } else {
274                         this->cc().set_max_steer();
275                         this->cc().st(this->cc().st() * -1);
276                         for (unsigned int i = 0; i < cnt; i++) {
277                                 this->cc().next();
278                                 pi.push_back(BicycleCar(this->cc()));
279                         }
280                 }
281                 this->cc() = BicycleCar(orig_cc);
282         }
283         return pi;
284 }
285
286 // find entry
287 void PSPlanner::fe()
288 {
289         this->c_ = 0;
290         if (this->ps().parallel()) {
291                 return this->fe_parallel();
292         } else {
293                 this->guess_gc();
294                 this->cc() = BicycleCar(this->gc());
295                 //this->cc().set_max_steer();
296                 //if (this->ps().right())
297                 //        this->cc().st(this->cc().st() * -1);
298                 this->cc().sp(-0.2);
299         }
300 }
301
302 double angle_between_closer_point(
303         double sx, double sy, // common start point
304         double cx, double cy, // common middle point
305         double x1, double y1, // first ending point
306         double x2, double y2 // second ending point
307 ) {
308         if (edist(sx, sy, x1, y1) < edist(sx, sy, x2, y2))
309                 return ::angle_between_three_points(sx, sy, cx, cy, x1, y1);
310         else
311                 return ::angle_between_three_points(sx, sy, cx, cy, x2, y2);
312 }
313
314 void PSPlanner::fe_parallel()
315 {
316         BicycleCar bco = BicycleCar(this->gc());
317         this->cc() = BicycleCar();
318         this->cc().sp(-0.01);
319         this->cc().set_max_steer();
320         if (!this->ps().right())
321                 this->cc().st(this->cc().st() * -1);
322         this->cc().h(this->ps().heading());
323         double angl_in_slot = this->ps().heading() - M_PI / 4;
324         if (!this->ps().right())
325                 angl_in_slot += M_PI / 2;
326         this->cc().x(
327                 this->ps().x4()
328                 + this->cc().w()/2 * cos(
329                         this->ps().heading()
330                         + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
331                 )
332                 + (this->cc().df() + 0.01) * cos(
333                         this->ps().heading() + M_PI
334                 )
335         );
336         this->cc().y(
337                 this->ps().y4()
338                 + this->cc().w()/2 * sin(
339                         this->ps().heading()
340                         + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
341                 )
342                 + (this->cc().df() + 0.01) * sin(
343                         this->ps().heading() + M_PI
344                 )
345         );
346
347         std::queue<BicycleCar, std::list<BicycleCar>> q;
348         while (!this->collide()) {
349                 q.push(this->cc());
350                 this->cc().rotate(
351                         this->ps().x4(),
352                         this->ps().y4() - 0.01,
353                         ((this->ps().right()) ? 0.001 : -0.001)
354                 );
355         }
356         // BFS - find entry current car `cc` and corresponding goal car `gc`
357         unsigned int iter_cntr = 0;
358         while (!q.empty() && iter_cntr < 30) {
359                 this->cc() = BicycleCar(q.front());
360                 q.pop();
361                 if (this->ps().right() && this->cc().sp() < 0) {
362                         double cclx = this->cc().ccl().x();
363                         double ccly = this->cc().ccl().y();
364                         double ccl_lr = edist(
365                                 cclx, ccly,
366                                 this->cc().lrx(), this->cc().lry()
367                         );
368                         double ccl_rr = edist(
369                                 cclx, ccly,
370                                 this->cc().rrx(), this->cc().rry()
371                         );
372                         double ccl_p1 = edist(
373                                 cclx, ccly,
374                                 this->ps().x1(), this->ps().y1()
375                         );
376                         if (ccl_rr < ccl_p1) {
377                                 // pass parking slot
378                                 continue;
379                         } else if (ccl_rr >= ccl_p1 && ccl_lr < ccl_p1) {
380                                 // partially out of parking slot
381                                 auto cli1 = ::intersect(
382                                         cclx, ccly, ccl_p1,
383                                         this->cc().lrx(), this->cc().lry(),
384                                         this->cc().rrx(), this->cc().rry()
385                                 );
386                                 double a1 = ::angle_between_closer_point(
387                                         this->ps().x1(), this->ps().y1(),
388                                         cclx, ccly,
389                                         std::get<1>(cli1), std::get<2>(cli1),
390                                         std::get<3>(cli1), std::get<4>(cli1)
391                                 );
392                                 auto cli2 = ::intersect(
393                                         cclx, ccly, ccl_rr,
394                                         this->ps().x2(), this->ps().y2(),
395                                         this->ps().x3(), this->ps().y3()
396                                 );
397                                 double a2 = angle_between_closer_point(
398                                         this->cc().rrx(), this->cc().rry(),
399                                         cclx, ccly,
400                                         std::get<1>(cli2), std::get<2>(cli2),
401                                         std::get<3>(cli2), std::get<4>(cli2)
402                                 );
403                                 if (std::get<0>(cli1) && (
404                                         !std::get<0>(cli2)
405                                         || a1 < a2
406                                 )) {
407                                         this->cc().rotate(cclx, ccly, -a1);
408                                         if (::right_side_of_line(
409                                                 this->cc().x(), this->cc().y(),
410
411                                                 this->cc().x()
412                                                 + cos(this->ps().heading()),
413                                                 this->cc().y()
414                                                 + sin(this->ps().heading()),
415
416                                                 this->cc().x()
417                                                 + cos(this->cc().h()),
418                                                 this->cc().y()
419                                                 + sin(this->cc().h())
420                                         )) {
421                                                 continue;
422                                         }
423                                 } else if (std::get<0>(cli2) && (
424                                         !std::get<0>(cli1)
425                                         || a2 < a1
426                                 )) {
427                                         this->cc().rotate(cclx, ccly, -a2);
428                                 } else {
429                                         continue;
430                                 }
431                         } else if (ccl_lr >= ccl_p1) {
432                                 // in parking slot
433                                 auto cli1 = ::intersect(
434                                         cclx, ccly, ccl_lr,
435                                         this->ps().x1(), this->ps().y1(),
436                                         this->ps().x2(), this->ps().y2()
437                                 );
438                                 double a1 = angle_between_closer_point(
439                                         this->cc().lrx(), this->cc().lry(),
440                                         cclx, ccly,
441                                         std::get<1>(cli1), std::get<2>(cli1),
442                                         std::get<3>(cli1), std::get<4>(cli1)
443                                 );
444                                 auto cli2 = ::intersect(
445                                         cclx, ccly, ccl_rr,
446                                         this->ps().x2(), this->ps().y2(),
447                                         this->ps().x3(), this->ps().y3()
448                                 );
449                                 double a2 = angle_between_closer_point(
450                                         this->cc().rrx(), this->cc().rry(),
451                                         cclx, ccly,
452                                         std::get<1>(cli2), std::get<2>(cli2),
453                                         std::get<3>(cli2), std::get<4>(cli2)
454                                 );
455                                 if (std::get<0>(cli1) && (
456                                         !std::get<0>(cli2)
457                                         || a1 < a2
458                                 )) {
459                                         this->cc().rotate(cclx, ccly, -a1);
460                                         if (::right_side_of_line(
461                                                 this->cc().x(), this->cc().y(),
462
463                                                 this->cc().x()
464                                                 + cos(this->ps().heading()),
465                                                 this->cc().y()
466                                                 + sin(this->ps().heading()),
467
468                                                 this->cc().x()
469                                                 + cos(this->cc().h()),
470                                                 this->cc().y()
471                                                 + sin(this->cc().h())
472                                         )) {
473                                                 continue;
474                                         }
475                                 } else if (std::get<0>(cli2) && (
476                                         !std::get<0>(cli1)
477                                         || a2 < a1
478                                 )) {
479                                         this->cc().rotate(cclx, ccly, -a2);
480                                 } else {
481                                         continue;
482                                 }
483                         }
484                 } else if (this->ps().right() && this->cc().sp() > 0) {
485                         double ccrx = this->cc().ccr().x();
486                         double ccry = this->cc().ccr().y();
487                         double ccr_lf = edist(
488                                 ccrx, ccry,
489                                 this->cc().lfx(), this->cc().lfy()
490                         );
491                         double ccr_rf = edist(
492                                 ccrx, ccry,
493                                 this->cc().rfx(), this->cc().rfy()
494                         );
495                         {
496                                 auto clif = ::intersect(
497                                         ccrx, ccry, ccr_lf,
498                                         this->ps().x1(), this->ps().y1(),
499                                         this->ps().x4(), this->ps().y4()
500                                 );
501                                 double af = std::abs(
502                                         this->ps().heading()
503                                         - this->cc().h()
504                                 );
505                                 double xf = this->ps().x4();
506                                 double yf = this->ps().y4();
507                                 if (std::get<0>(clif)) {
508                                         xf = std::get<1>(clif);
509                                         yf = std::get<2>(clif);
510                                         if (
511                                                 edist(
512                                                         this->ps().x4(),
513                                                         this->ps().y4(),
514                                                         std::get<3>(clif),
515                                                         std::get<4>(clif)
516                                                 ) < edist(
517                                                         this->ps().x4(),
518                                                         this->ps().y4(),
519                                                         xf, yf
520                                                 )
521                                         ) {
522                                                 xf = std::get<3>(clif);
523                                                 yf = std::get<4>(clif);
524                                         }
525                                         af = ::angle_between_three_points(
526                                                 this->cc().lfx(),
527                                                 this->cc().lfy(),
528                                                 ccrx, ccry,
529                                                 xf, yf
530                                         );
531                                 }
532                                 auto tmp_cc = BicycleCar(this->cc());
533                                 this->cc().rotate(ccrx, ccry, -af);
534                                 if (
535                                         !this->collide()
536                                         && this->parked()
537                                         && (
538                                                 edist(
539                                                         this->ps().x1(),
540                                                         this->ps().y1(),
541                                                         xf, yf
542                                                 ) < edist(
543                                                         this->ps().x1(),
544                                                         this->ps().y1(),
545                                                         this->ps().x4(),
546                                                         this->ps().y4()
547                                                 )
548                                                 || !std::get<0>(clif)
549                                         )
550                                 ) {
551                                         this->cc().sp(this->cc().sp() * -1);
552                                         this->gc() = BicycleCar(this->cc());
553                                         goto successfinish;
554                                 } else {
555                                         this->cc() = BicycleCar(tmp_cc);
556                                 }
557                         }
558                         auto cli1 = ::intersect(
559                                 ccrx, ccry, ccr_rf,
560                                 this->ps().x3(), this->ps().y3(),
561                                 this->ps().x4(), this->ps().y4()
562                         );
563                         double a1 = angle_between_closer_point(
564                                 this->cc().rfx(), this->cc().rfy(),
565                                 ccrx, ccry,
566                                 std::get<1>(cli1), std::get<2>(cli1),
567                                 std::get<3>(cli1), std::get<4>(cli1)
568                         );
569                         auto cli2 = ::intersect(
570                                 ccrx, ccry, ccr_rf,
571                                 this->ps().x2(), this->ps().y2(),
572                                 this->ps().x3(), this->ps().y3()
573                         );
574                         double a2 = angle_between_closer_point(
575                                 this->cc().rfx(), this->cc().rfy(),
576                                 ccrx, ccry,
577                                 std::get<1>(cli2), std::get<2>(cli2),
578                                 std::get<3>(cli2), std::get<4>(cli2)
579                         );
580                         auto cli3 = ::intersect(
581                                 ccrx, ccry, ccr_lf,
582                                 this->ps().x3(), this->ps().y3(),
583                                 this->ps().x4(), this->ps().y4()
584                         );
585                         double a3 = angle_between_closer_point(
586                                 this->cc().lfx(), this->cc().lfy(),
587                                 ccrx, ccry,
588                                 std::get<1>(cli3), std::get<2>(cli3),
589                                 std::get<3>(cli3), std::get<4>(cli3)
590                         );
591                         if (std::get<0>(cli1) && (
592                             (!std::get<0>(cli2) && !std::get<0>(cli3))
593                             || (a1 < a2 && !std::get<0>(cli3))
594                             || (a1 < a3 && !std::get<0>(cli2))
595                             || (a1 < a2 && a1 < a3)
596                         )) {
597                             this->cc().rotate(ccrx, ccry, -a1);
598                         } else if (std::get<0>(cli2) && (
599                             (!std::get<0>(cli1) && !std::get<0>(cli3))
600                             || (a2 < a1 && !std::get<0>(cli3))
601                             || (a2 < a3 && !std::get<0>(cli1))
602                             || (a2 < a1 && a2 < a3)
603                         )) {
604                             this->cc().rotate(ccrx, ccry, -a2);
605                         } else if (std::get<0>(cli3) && (
606                             (!std::get<0>(cli1) && !std::get<0>(cli2))
607                             || (a3 < a1 && !std::get<0>(cli2))
608                             || (a3 < a2 && !std::get<0>(cli1))
609                             || (a3 < a1 && a3 < a2)
610                         )) {
611                             this->cc().rotate(ccrx, ccry, -a3);
612                         } else {
613                             continue;
614                         }
615                 } else {
616                         // TODO left parking slot (both forward, backward)
617                 }
618                 this->cc().sp(this->cc().sp() * -1);
619                 this->cc().next();
620                 this->gc() = BicycleCar(this->cc());
621                 if (this->parked())
622                         goto successfinish;
623                 this->cc().st(this->cc().st() * -1);
624                 q.push(BicycleCar(this->cc()));
625                 if (sgn(this->cc().st()) == sgn(q.front().st()))
626                         iter_cntr++;
627         }
628         // fallback to fer
629         this->gc() = BicycleCar(bco);
630 successfinish:
631         return this->fer_parallel();
632 }
633
634 void PSPlanner::fe_perpendicular()
635 {
636         // TODO Try multiple angles when going from parking slot.
637         //
638         //      Do not use just the maximum steer angle. Test angles
639         //      until the whole current car `cc` is out of the parking
640         //      slot `ps`.
641         //
642         //      Another approach could be testing angles from the
643         //      beginning of the escape parkig slot maneuver.
644         if (this->forward())
645                 this->cc().sp(-0.01);
646         else
647                 this->cc().sp(0.01);
648         while (!this->left())
649                 this->cc().next();
650         return;
651 }
652
653 void PSPlanner::fer()
654 {
655         this->c_ = 0;
656         if (this->ps().parallel()) {
657                 this->guess_gc();
658                 this->cc() = BicycleCar(this->gc());
659                 this->cc().set_max_steer();
660                 if (!this->ps().right())
661                         this->cc().st(this->cc().st() * -1);
662                 this->cc().sp(0.01);
663                 return this->fer_parallel();
664         } else {
665                 return this->fer_perpendicular();
666         }
667 }
668
669 void PSPlanner::fer_parallel()
670 {
671         this->cusps_.clear();
672         while (!this->left()) {
673                 while (!this->collide() && !this->left())
674                         this->cc().next();
675                 if (this->left() && !this->collide()) {
676                         break;
677                 } else {
678                         this->cc().sp(this->cc().sp() * -1);
679                         this->cc().next();
680                         this->cc().st(this->cc().st() * -1);
681                         this->c_++;
682                         this->cusps_.push_back(this->cc());
683                 }
684         }
685         if (this->cc().st() < 0) {
686                 this->c_++;
687                 this->cusps_.push_back(this->cc());
688         }
689 }
690
691 void PSPlanner::fer_perpendicular()
692 {
693         bool delta_use[] = {true, true, true};
694         double cc_h = this->cc().h();
695         double x;
696         double y;
697         // check inner radius
698         if (this->forward()) {
699                 x = this->ps().x1();
700                 y = this->ps().y1();
701         } else {
702                 x = this->ps().x4();
703                 y = this->ps().y4();
704         }
705         double x1;
706         double y1;
707         if (this->ps().right()) {
708                 x1 = this->cc().ccr().x();
709                 y1 = this->cc().ccr().y();
710         } else {
711                 x1 = this->cc().ccl().x();
712                 y1 = this->cc().ccl().y();
713         }
714         double IR = this->cc().iradi();
715         double a = 1;
716         double b;
717         if (this->forward())
718                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
719         else
720                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
721         double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
722         double D = pow(b, 2) - 4 * a * c;
723         double delta;
724         delta = -b - sqrt(D);
725         delta /= 2 * a;
726         double delta_1 = delta;
727         if (D < 0)
728                 delta_use[0] = false;
729         // check outer radius
730         if (this->forward()) {
731                 x = this->ps().x4();
732                 y = this->ps().y4();
733         } else {
734                 x = this->ps().x1();
735                 y = this->ps().y1();
736         }
737         IR = this->cc().ofradi();
738         a = 1;
739         if (this->forward())
740                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
741         else
742                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
743         c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
744         D = pow(b, 2) - 4 * a * c;
745         if (this->forward()) {
746                 delta = -b + sqrt(D);
747                 delta /= 2 * a;
748         }
749         double delta_2 = delta;
750         if (D < 0)
751                 delta_use[1] = false;
752         delta = -b - sqrt(D);
753         delta /= 2 * a;
754         double delta_3 = delta;
755         if (D < 0)
756                 delta_use[2] = false;
757         if (delta_use[0] && delta_use[1] && delta_use[2])
758                 delta = std::max(delta_1, std::max(delta_2, delta_3));
759         else if (delta_use[0] && delta_use[1])
760                 delta = std::max(delta_1, delta_2);
761         else if (delta_use[0] && delta_use[2])
762                 delta = std::max(delta_1, delta_3);
763         else if (delta_use[1] && delta_use[2])
764                 delta = std::max(delta_2, delta_3);
765         else if (delta_use[0])
766                 delta = delta_1;
767         else if (delta_use[1])
768                 delta = delta_2;
769         else if (delta_use[2])
770                 delta = delta_3;
771         else
772                 return;
773         // current car `cc` can get out of slot with max steer
774         this->cc().x(this->cc().x() + delta * cos(cc_h));
775         this->cc().y(this->cc().y() + delta * sin(cc_h));
776         this->cc().h(cc_h);
777         // get current car `cc` out of slot
778         if (this->forward())
779                 this->cc().sp(-0.01);
780         else
781                 this->cc().sp(0.01);
782         this->cc().set_max_steer();
783         if (this->ps().right())
784                 this->cc().st(this->cc().st() * -1);
785         while (!this->left()) {
786                 while (!this->collide() && !this->left())
787                         this->cc().next();
788                 if (this->left() && !this->collide()) {
789                         break;
790                 } else {
791                         this->cc().sp(this->cc().sp() * -1);
792                         this->cc().next();
793                         this->cc().st(this->cc().st() * -1);
794                 }
795         }
796 }
797
798 PSPlanner::PSPlanner()
799 {
800 }