]> rtime.felk.cvut.cz Git - hubacji1/psp.git/blob - src/psp.cc
cff29ffcbe75b09c193a835091859e83ae33871a
[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 smaller_angle_of_two(
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         double a1 = ::angle_between_three_points(sx, sy, cx, cy, x1, y1);
309         double a2 = ::angle_between_three_points(sx, sy, cx, cy, x2, y2);
310         return std::min(a1, a2);
311 }
312
313 void PSPlanner::fe_parallel()
314 {
315         BicycleCar bco = BicycleCar(this->gc());
316         this->cc() = BicycleCar();
317         this->cc().sp(-0.01);
318         this->cc().set_max_steer();
319         if (!this->ps().right())
320                 this->cc().st(this->cc().st() * -1);
321         this->cc().h(this->ps().heading());
322         double angl_in_slot = this->ps().heading() - M_PI / 4;
323         if (!this->ps().right())
324                 angl_in_slot += M_PI / 2;
325         this->cc().x(
326                 this->ps().x4()
327                 + this->cc().w()/2 * cos(
328                         this->ps().heading()
329                         + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
330                 )
331                 + (this->cc().df() + 0.01) * cos(
332                         this->ps().heading() + M_PI
333                 )
334         );
335         this->cc().y(
336                 this->ps().y4()
337                 + this->cc().w()/2 * sin(
338                         this->ps().heading()
339                         + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
340                 )
341                 + (this->cc().df() + 0.01) * sin(
342                         this->ps().heading() + M_PI
343                 )
344         );
345
346         std::queue<BicycleCar, std::list<BicycleCar>> q;
347         while (!this->collide()) {
348                 q.push(this->cc());
349                 this->cc().rotate(
350                         this->ps().x4(),
351                         this->ps().y4() - 0.01,
352                         ((this->ps().right()) ? 0.001 : -0.001)
353                 );
354         }
355         // BFS - find entry current car `cc` and corresponding goal car `gc`
356         unsigned int iter_cntr = 0;
357         while (!q.empty() && iter_cntr < 30) {
358                 this->cc() = BicycleCar(q.front());
359                 q.pop();
360                 if (this->ps().right() && this->cc().sp() < 0) {
361                         double cclx = this->cc().ccl().x();
362                         double ccly = this->cc().ccl().y();
363                         double ccl_lr = edist(
364                                 cclx, ccly,
365                                 this->cc().lrx(), this->cc().lry()
366                         );
367                         double ccl_rr = edist(
368                                 cclx, ccly,
369                                 this->cc().rrx(), this->cc().rry()
370                         );
371                         double ccl_p1 = edist(
372                                 cclx, ccly,
373                                 this->ps().x1(), this->ps().y1()
374                         );
375                         if (ccl_rr < ccl_p1) {
376                                 // pass parking slot
377                                 continue;
378                         } else if (ccl_rr >= ccl_p1 && ccl_lr < ccl_p1) {
379                                 // partially out of parking slot
380                                 auto cli1 = ::intersect(
381                                         cclx, ccly, ccl_p1,
382                                         this->cc().lrx(), this->cc().lry(),
383                                         this->cc().rrx(), this->cc().rry()
384                                 );
385                                 double a1 = ::smaller_angle_of_two(
386                                         this->ps().x1(), this->ps().y1(),
387                                         cclx, ccly,
388                                         std::get<1>(cli1), std::get<2>(cli1),
389                                         std::get<3>(cli1), std::get<4>(cli1)
390                                 );
391                                 auto cli2 = ::intersect(
392                                         cclx, ccly, ccl_rr,
393                                         this->ps().x2(), this->ps().y2(),
394                                         this->ps().x3(), this->ps().y3()
395                                 );
396                                 double a2 = smaller_angle_of_two(
397                                         this->cc().rrx(), this->cc().rry(),
398                                         cclx, ccly,
399                                         std::get<1>(cli2), std::get<2>(cli2),
400                                         std::get<3>(cli2), std::get<4>(cli2)
401                                 );
402                                 if (std::get<0>(cli1) && (
403                                         !std::get<0>(cli2)
404                                         || a1 < a2
405                                 )) {
406                                         this->cc().rotate(cclx, ccly, -a1);
407                                 } else if (std::get<0>(cli2) && (
408                                         !std::get<0>(cli1)
409                                         || a2 < a1
410                                 )) {
411                                         this->cc().rotate(cclx, ccly, -a2);
412                                 } else {
413                                         continue;
414                                 }
415                         } else if (ccl_lr >= ccl_p1) {
416                                 // in parking slot
417                                 auto cli1 = ::intersect(
418                                         cclx, ccly, ccl_lr,
419                                         this->ps().x1(), this->ps().y1(),
420                                         this->ps().x2(), this->ps().y2()
421                                 );
422                                 double a1 = smaller_angle_of_two(
423                                         this->cc().lrx(), this->cc().lry(),
424                                         cclx, ccly,
425                                         std::get<1>(cli1), std::get<2>(cli1),
426                                         std::get<3>(cli1), std::get<4>(cli1)
427                                 );
428                                 auto cli2 = ::intersect(
429                                         cclx, ccly, ccl_rr,
430                                         this->ps().x2(), this->ps().y2(),
431                                         this->ps().x3(), this->ps().y3()
432                                 );
433                                 double a2 = smaller_angle_of_two(
434                                         this->cc().rrx(), this->cc().rry(),
435                                         cclx, ccly,
436                                         std::get<1>(cli2), std::get<2>(cli2),
437                                         std::get<3>(cli2), std::get<4>(cli2)
438                                 );
439                                 if (std::get<0>(cli1) && (
440                                         !std::get<0>(cli2)
441                                         || a1 < a2
442                                 )) {
443                                         this->cc().rotate(cclx, ccly, -a1);
444                                 } else if (std::get<0>(cli2) && (
445                                         !std::get<0>(cli1)
446                                         || a2 < a1
447                                 )) {
448                                         this->cc().rotate(cclx, ccly, -a2);
449                                 } else {
450                                         continue;
451                                 }
452                         }
453                 } else if (this->ps().right() && this->cc().sp() > 0) {
454                         double ccrx = this->cc().ccr().x();
455                         double ccry = this->cc().ccr().y();
456                         double ccr_lf = edist(
457                                 ccrx, ccry,
458                                 this->cc().lfx(), this->cc().lfy()
459                         );
460                         double ccr_rf = edist(
461                                 ccrx, ccry,
462                                 this->cc().rfx(), this->cc().rfy()
463                         );
464                         {
465                                 auto clif = ::intersect(
466                                         ccrx, ccry, ccr_lf,
467                                         this->ps().x1(), this->ps().y1(),
468                                         this->ps().x4(), this->ps().y4()
469                                 );
470                                 double af = std::abs(
471                                         this->ps().heading()
472                                         - this->cc().h()
473                                 );
474                                 double xf = this->ps().x4();
475                                 double yf = this->ps().y4();
476                                 if (std::get<0>(clif)) {
477                                         xf = std::get<1>(clif);
478                                         yf = std::get<2>(clif);
479                                         if (
480                                                 edist(
481                                                         this->ps().x4(),
482                                                         this->ps().y4(),
483                                                         std::get<3>(clif),
484                                                         std::get<4>(clif)
485                                                 ) < edist(
486                                                         this->ps().x4(),
487                                                         this->ps().y4(),
488                                                         xf, yf
489                                                 )
490                                         ) {
491                                                 xf = std::get<3>(clif);
492                                                 yf = std::get<4>(clif);
493                                         }
494                                         af = ::angle_between_three_points(
495                                                 this->cc().lfx(),
496                                                 this->cc().lfy(),
497                                                 ccrx, ccry,
498                                                 xf, yf
499                                         );
500                                 }
501                                 auto tmp_cc = BicycleCar(this->cc());
502                                 this->cc().rotate(ccrx, ccry, -af);
503                                 if (
504                                         !this->collide()
505                                         && (
506                                                 edist(
507                                                         this->ps().x1(),
508                                                         this->ps().y1(),
509                                                         xf, yf
510                                                 ) < edist(
511                                                         this->ps().x1(),
512                                                         this->ps().y1(),
513                                                         this->ps().x4(),
514                                                         this->ps().y4()
515                                                 )
516                                                 || !std::get<0>(clif)
517                                         )
518                                 ) {
519                                         this->cc().sp(this->cc().sp() * -1);
520                                         this->gc() = BicycleCar(this->cc());
521                                         goto successfinish;
522                                 } else {
523                                         this->cc() = BicycleCar(tmp_cc);
524                                 }
525                         }
526                         // TODO (ccr, rf) x (p3, p4)
527                         // TODO (ccr, rf) x (p2, p3)
528                         // TODO (ccr, lf) x (p3, p4)
529 {
530         double rf = sqrt(
531                 pow(this->cc().lfy() - ccry, 2)
532                 + pow(this->cc().lfx() - ccrx, 2)
533         );
534         auto clif = ::intersect(
535                 ccrx, ccry, rf,
536                 this->ps().x1(), this->ps().y1(),
537                 this->ps().x4(), this->ps().y4()
538         );
539         if (std::get<0>(clif)) {
540                 double xf = std::get<1>(clif);
541                 double yf = std::get<2>(clif);
542                 if (
543                         edist(
544                                 this->ps().x4(),
545                                 this->ps().y4(),
546                                 std::get<3>(clif),
547                                 std::get<4>(clif)
548                         ) < edist(
549                                 this->ps().x4(),
550                                 this->ps().y4(),
551                                 xf, yf
552                         )
553                 ) {
554                         xf = std::get<3>(clif);
555                         yf = std::get<4>(clif);
556                 }
557                 auto af = ::angle_between_three_points(
558                         this->cc().lfx(),
559                         this->cc().lfy(),
560                         ccrx, ccry,
561                         xf, yf
562                 );
563                 auto tmp_cc = BicycleCar(this->cc());
564                 this->cc().rotate(ccrx, ccry, -af);
565                 if (
566                         !this->collide()
567                         && (edist(
568                                 this->ps().x1(), this->ps().y1(),
569                                 xf, yf
570                         ) < edist(
571                                 this->ps().x1(), this->ps().y1(),
572                                 this->ps().x4(), this->ps().y4()
573                         ))
574                 ) {
575                         this->cc().sp(-0.01);
576                         this->cc().set_max_steer();
577                         this->cc().st(this->cc().st() * -1);
578                         this->gc() = BicycleCar(this->cc());
579                         goto successfinish;
580                 } else {
581                         this->cc() = BicycleCar(tmp_cc);
582                 }
583         } else {
584                 // should be parked and found in previous iteration or continue
585                 // with the parking process
586         }
587 }
588                         double r1 = sqrt(
589                             pow(this->cc().rfy() - ccry, 2)
590                             + pow(this->cc().rfx() - ccrx, 2)
591                         );
592                         auto cli1 = ::intersect(
593                             ccrx, ccry, r1,
594                             this->ps().x3(), this->ps().y3(),
595                             this->ps().x4(), this->ps().y4()
596                         );
597                         double a11 = ::angle_between_three_points(
598                             this->cc().lrx(), this->cc().lry(),
599                             ccrx, ccry,
600                             std::get<1>(cli1), std::get<2>(cli1)
601                         );
602                         double a12 = ::angle_between_three_points(
603                             this->cc().lrx(), this->cc().lry(),
604                             ccrx, ccry,
605                             std::get<3>(cli1), std::get<4>(cli1)
606                         );
607                         double a1 = std::min(a11, a12);
608
609                         double r2 = sqrt(
610                             pow(this->cc().lfy() - ccry, 2)
611                             + pow(this->cc().lfx() - ccrx, 2)
612                         );
613                         auto cli2 = ::intersect(
614                             ccrx, ccry, r2,
615                             this->ps().x3(), this->ps().y3(),
616                             this->ps().x4(), this->ps().y4()
617                         );
618                         double a21 = ::angle_between_three_points(
619                             this->cc().rrx(), this->cc().rry(),
620                             ccrx, ccry,
621                             std::get<1>(cli2), std::get<2>(cli2)
622                         );
623                         double a22 = ::angle_between_three_points(
624                             this->cc().rrx(), this->cc().rry(),
625                             ccrx, ccry,
626                             std::get<3>(cli2), std::get<4>(cli2)
627                         );
628                         double a2 = std::min(a21, a22);
629
630                         double r3 = sqrt(
631                             pow(this->cc().rfy() - ccry, 2)
632                             + pow(this->cc().rfx() - ccrx, 2)
633                         );
634                         auto cli3 = ::intersect(
635                             ccrx, ccry, r3,
636                             this->ps().x3(), this->ps().y3(),
637                             this->ps().x2(), this->ps().y2()
638                         );
639                         double a31 = ::angle_between_three_points(
640                             this->cc().rrx(), this->cc().rry(),
641                             ccrx, ccry,
642                             std::get<1>(cli3), std::get<2>(cli3)
643                         );
644                         double a32 = ::angle_between_three_points(
645                             this->cc().rrx(), this->cc().rry(),
646                             ccrx, ccry,
647                             std::get<3>(cli3), std::get<4>(cli3)
648                         );
649                         double a3 = std::min(a31, a32);
650
651                         if (std::get<0>(cli1) && (
652                             (!std::get<0>(cli2) && !std::get<0>(cli3))
653                             || (a1 < a2 && !std::get<0>(cli3))
654                             || (a1 < a3 && !std::get<0>(cli2))
655                             || (a1 < a2 && a1 < a3)
656                         )) {
657                             this->cc().rotate(ccrx, ccry, -a1);
658                         } else if (std::get<0>(cli2) && (
659                             (!std::get<0>(cli1) && !std::get<0>(cli3))
660                             || (a2 < a1 && !std::get<0>(cli3))
661                             || (a2 < a3 && !std::get<0>(cli1))
662                             || (a2 < a1 && a2 < a3)
663                         )) {
664                             this->cc().rotate(ccrx, ccry, -a2);
665                         } else if (std::get<0>(cli3) && (
666                             (!std::get<0>(cli1) && !std::get<0>(cli2))
667                             || (a3 < a1 && !std::get<0>(cli2))
668                             || (a3 < a2 && !std::get<0>(cli1))
669                             || (a3 < a1 && a3 < a2)
670                         )) {
671                             this->cc().rotate(ccrx, ccry, -a3);
672                         } else {
673                             continue;
674                         }
675                 } else {
676                         // TODO left parking slot (both forward, backward)
677                 }
678                 this->cc().sp(this->cc().sp() * -1);
679                 this->cc().next();
680                 this->gc() = BicycleCar(this->cc());
681                 if (this->parked())
682                         goto successfinish;
683                 this->cc().st(this->cc().st() * -1);
684                 q.push(BicycleCar(this->cc()));
685                 if (sgn(this->cc().st()) == sgn(q.front().st()))
686                         iter_cntr++;
687         }
688         // fallback to fer
689         this->gc() = BicycleCar(bco);
690 successfinish:
691         return this->fer_parallel();
692 }
693
694 void PSPlanner::fe_perpendicular()
695 {
696         // TODO Try multiple angles when going from parking slot.
697         //
698         //      Do not use just the maximum steer angle. Test angles
699         //      until the whole current car `cc` is out of the parking
700         //      slot `ps`.
701         //
702         //      Another approach could be testing angles from the
703         //      beginning of the escape parkig slot maneuver.
704         if (this->forward())
705                 this->cc().sp(-0.01);
706         else
707                 this->cc().sp(0.01);
708         while (!this->left())
709                 this->cc().next();
710         return;
711 }
712
713 void PSPlanner::fer()
714 {
715         this->c_ = 0;
716         if (this->ps().parallel()) {
717                 this->guess_gc();
718                 this->cc() = BicycleCar(this->gc());
719                 this->cc().set_max_steer();
720                 if (!this->ps().right())
721                         this->cc().st(this->cc().st() * -1);
722                 this->cc().sp(0.01);
723                 return this->fer_parallel();
724         } else {
725                 return this->fer_perpendicular();
726         }
727 }
728
729 void PSPlanner::fer_parallel()
730 {
731         this->cusps_.clear();
732         while (!this->left()) {
733                 while (!this->collide() && !this->left())
734                         this->cc().next();
735                 if (this->left() && !this->collide()) {
736                         break;
737                 } else {
738                         this->cc().sp(this->cc().sp() * -1);
739                         this->cc().next();
740                         this->cc().st(this->cc().st() * -1);
741                         this->c_++;
742                         this->cusps_.push_back(this->cc());
743                 }
744         }
745         if (this->cc().st() < 0) {
746                 this->c_++;
747                 this->cusps_.push_back(this->cc());
748         }
749 }
750
751 void PSPlanner::fer_perpendicular()
752 {
753         bool delta_use[] = {true, true, true};
754         double cc_h = this->cc().h();
755         double x;
756         double y;
757         // check inner radius
758         if (this->forward()) {
759                 x = this->ps().x1();
760                 y = this->ps().y1();
761         } else {
762                 x = this->ps().x4();
763                 y = this->ps().y4();
764         }
765         double x1;
766         double y1;
767         if (this->ps().right()) {
768                 x1 = this->cc().ccr().x();
769                 y1 = this->cc().ccr().y();
770         } else {
771                 x1 = this->cc().ccl().x();
772                 y1 = this->cc().ccl().y();
773         }
774         double IR = this->cc().iradi();
775         double a = 1;
776         double b;
777         if (this->forward())
778                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
779         else
780                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
781         double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
782         double D = pow(b, 2) - 4 * a * c;
783         double delta;
784         delta = -b - sqrt(D);
785         delta /= 2 * a;
786         double delta_1 = delta;
787         if (D < 0)
788                 delta_use[0] = false;
789         // check outer radius
790         if (this->forward()) {
791                 x = this->ps().x4();
792                 y = this->ps().y4();
793         } else {
794                 x = this->ps().x1();
795                 y = this->ps().y1();
796         }
797         IR = this->cc().ofradi();
798         a = 1;
799         if (this->forward())
800                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
801         else
802                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
803         c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
804         D = pow(b, 2) - 4 * a * c;
805         if (this->forward()) {
806                 delta = -b + sqrt(D);
807                 delta /= 2 * a;
808         }
809         double delta_2 = delta;
810         if (D < 0)
811                 delta_use[1] = false;
812         delta = -b - sqrt(D);
813         delta /= 2 * a;
814         double delta_3 = delta;
815         if (D < 0)
816                 delta_use[2] = false;
817         if (delta_use[0] && delta_use[1] && delta_use[2])
818                 delta = std::max(delta_1, std::max(delta_2, delta_3));
819         else if (delta_use[0] && delta_use[1])
820                 delta = std::max(delta_1, delta_2);
821         else if (delta_use[0] && delta_use[2])
822                 delta = std::max(delta_1, delta_3);
823         else if (delta_use[1] && delta_use[2])
824                 delta = std::max(delta_2, delta_3);
825         else if (delta_use[0])
826                 delta = delta_1;
827         else if (delta_use[1])
828                 delta = delta_2;
829         else if (delta_use[2])
830                 delta = delta_3;
831         else
832                 return;
833         // current car `cc` can get out of slot with max steer
834         this->cc().x(this->cc().x() + delta * cos(cc_h));
835         this->cc().y(this->cc().y() + delta * sin(cc_h));
836         this->cc().h(cc_h);
837         // get current car `cc` out of slot
838         if (this->forward())
839                 this->cc().sp(-0.01);
840         else
841                 this->cc().sp(0.01);
842         this->cc().set_max_steer();
843         if (this->ps().right())
844                 this->cc().st(this->cc().st() * -1);
845         while (!this->left()) {
846                 while (!this->collide() && !this->left())
847                         this->cc().next();
848                 if (this->left() && !this->collide()) {
849                         break;
850                 } else {
851                         this->cc().sp(this->cc().sp() * -1);
852                         this->cc().next();
853                         this->cc().st(this->cc().st() * -1);
854                 }
855         }
856 }
857
858 PSPlanner::PSPlanner()
859 {
860 }