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