]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - src/bcar.cc
Add license info to all source files
[hubacji1/bcar.git] / src / bcar.cc
1 /*
2  * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
3  *
4  * SPDX-License-Identifier: GPL-3.0-only
5  */
6
7 #include <cmath>
8 #include "bcar.hh"
9
10 namespace bcar {
11
12 Point::Point()
13 {
14 }
15
16 Point::Point(double x, double y) : x_(x), y_(y)
17 {
18 }
19
20 double
21 Point::x() const
22 {
23         return this->x_;
24 }
25
26 void
27 Point::x(double x)
28 {
29         this->x_ = x;
30 }
31
32 double
33 Point::y() const
34 {
35         return this->y_;
36 }
37
38 void
39 Point::y(double y)
40 {
41         this->y_ = y;
42 }
43
44 double
45 Point::min_angle_between(Point const& p1, Point const& p2) const
46 {
47         double d1x = p1.x() - this->x();
48         double d1y = p1.y() - this->y();
49         double d2x = p2.x() - p1.x();
50         double d2y = p2.y() - p1.y();
51
52         double dot = d1x*d2x + d1y*d2y;
53         double d1 = sqrt(d1x*d1x + d1y*d1y);
54         double d2 = sqrt(d2x*d2x + d2y*d2y);
55
56         double delta = acos(dot / (d1 * d2));
57         return std::min(delta, M_PI - delta);
58 }
59
60 bool
61 Point::inside_of(std::vector<Point> const& poly) const
62 {
63         unsigned int num = poly.size();
64         unsigned int j = num - 1;
65         bool c = false;
66         for (unsigned int i = 0; i < num; i++) {
67                 if (this->x() == poly[i].x() && this->y() == poly[i].y()) {
68                         return true;
69                 }
70                 if ((poly[i].y() > this->y()) != (poly[j].y() > this->y())) {
71                         auto slope1 = this->x() - poly[i].x();
72                         slope1 *= poly[j].y() - poly[i].y();
73                         auto slope2 = poly[j].x() - poly[i].x();
74                         slope2 *= this->y() - poly[i].y();
75                         auto slope = slope1 - slope2;
76                         if (slope == 0.0) {
77                                 return true;
78                         }
79                         if ((slope < 0.0) != (poly[j].y() < poly[i].y())) {
80                                 c = !c;
81                         }
82                 }
83                 j = i;
84         }
85         return c;
86 }
87
88 bool
89 Point::on_right_side_of(Line const& li) const
90 {
91         auto x1 = li.b().x();
92         auto y1 = li.b().y();
93         auto x2 = li.e().x();
94         auto y2 = li.e().y();
95         auto x3 = this->x_;
96         auto y3 = this->y_;
97         if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
98                 return false;
99         } else {
100                 return true;
101         }
102 }
103
104 void
105 Point::translate(Point const& p)
106 {
107         this->x_ += p.x();
108         this->y_ += p.y();
109 }
110
111 void
112 Point::rotate(Point const& c, double const angl)
113 {
114         double px = this->x();
115         double py = this->y();
116         px -= c.x();
117         py -= c.y();
118         double nx = px * cos(angl) - py * sin(angl);
119         double ny = px * sin(angl) + py * cos(angl);
120         this->x(nx + c.x());
121         this->y(ny + c.y());
122 }
123
124 void
125 Point::reflect(Line const& li)
126 {
127         this->rotate(li.b(), -li.h());
128         this->y_ -= li.b().y();
129         this->y_ *= -1.0;
130         this->y_ += li.b().y();
131         this->rotate(li.b(), li.h());
132 }
133
134 double
135 Point::edist(Point const& p) const
136 {
137         return sqrt(pow(p.x() - this->x_, 2.0) + pow(p.y() - this->y_, 2.0));
138 }
139
140 bool
141 Point::operator==(Point const& p)
142 {
143         return this->x() == p.x() && this->y() == p.y();
144 }
145
146 std::ostream&
147 operator<<(std::ostream& out, Point const& p)
148 {
149         out << "[" << p.x() << "," << p.y() << "]";
150         return out;
151 }
152
153 Line::Line(Point const& b, Point const& e): b_(b), e_(e)
154 {
155 }
156
157 Point
158 Line::b() const&
159 {
160         return this->b_;
161 }
162
163 Point
164 Line::e() const&
165 {
166         return this->e_;
167 }
168
169 Point
170 Line::m() const
171 {
172         return Point((this->b_.x() + this->e_.x()) / 2.0,
173                 (this->b_.y() + this->e_.y()) / 2.0);
174 }
175
176 Point
177 Line::i1() const&
178 {
179         return this->i1_;
180 }
181
182 Point
183 Line::i2() const&
184 {
185         return this->i2_;
186 }
187
188 bool
189 Line::intersects_with(Line const& li)
190 {
191         auto x1 = this->b_.x();
192         auto y1 = this->b_.y();
193         auto x2 = this->e_.x();
194         auto y2 = this->e_.y();
195         auto x3 = li.b().x();
196         auto y3 = li.b().y();
197         auto x4 = li.e().x();
198         auto y4 = li.e().y();
199         double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
200         if (deno == 0.0) {
201                 return false;
202         }
203         double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
204         t /= deno;
205         double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
206         u *= -1.0;
207         u /= deno;
208         if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
209                 return false;
210         }
211         this->i1_.x(x1 + t * (x2 - x1));
212         this->i1_.y(y1 + t * (y2 - y1));
213         return true;
214 }
215
216 bool
217 Line::intersects_with(Point const& c, double const r)
218 {
219         auto x1 = this->b_.x();
220         auto y1 = this->b_.y();
221         auto x2 = this->e_.x();
222         auto y2 = this->e_.y();
223         auto cx = c.x();
224         auto cy = c.y();
225         x2 -= cx;
226         x1 -= cx;
227         y2 -= cy;
228         y1 -= cy;
229         if (y1 == y2) {
230                 y1 += 0.00001;
231         }
232         double dx = x2 - x1;
233         double dy = y2 - y1;
234         double dr = sqrt(dx*dx + dy*dy);
235         double D = x1*y2 - x2*y1;
236         if (r*r * dr*dr - D*D < 0.0) {
237                 return false;
238         }
239         // intersection coordinates
240         double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
241         ix1 += cx;
242         double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
243         ix2 += cx;
244         double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
245         iy1 += cy;
246         double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
247         iy2 += cy;
248         this->i1_.x(ix1);
249         this->i1_.y(iy1);
250         this->i2_.x(ix2);
251         this->i2_.y(iy2);
252         return true;
253 }
254
255 double
256 Line::len() const
257 {
258         return this->b_.edist(this->e_);
259 }
260
261 double
262 Line::h() const
263 {
264         return atan2(this->e_.y() - this->b_.y(), this->e_.x() - this->b_.x());
265 }
266
267 std::ostream&
268 operator<<(std::ostream& out, Line const& li)
269 {
270         out << "[" << li.b_ << "," << li.e_ << "]";
271         return out;
272 }
273
274 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
275 {
276 }
277
278 double
279 Pose::h() const
280 {
281         return this->h_;
282 }
283
284 void
285 Pose::h(double h)
286 {
287         while (h < -M_PI) {
288                 h += 2 * M_PI;
289         }
290         while (h > +M_PI) {
291                 h -= 2 * M_PI;
292         }
293         this->h_ = h;
294 }
295
296 void
297 Pose::set_pose(Pose const& p)
298 {
299         this->x(p.x());
300         this->y(p.y());
301         this->h(p.h());
302 }
303
304 void
305 Pose::rotate(Point const& c, double const angl)
306 {
307         Point::rotate(c, angl);
308         this->h(this->h() + angl);
309 }
310
311 void
312 Pose::reflect(Line const& li)
313 {
314         Point::reflect(li);
315         double dh = li.h() - this->h();
316         this->h(this->h() + 2.0 * dh);
317 }
318
319 bool
320 Pose::operator==(Pose const& p)
321 {
322         return this->x() == p.x() && this->y() == p.y() && this->h() == p.h();
323 }
324
325 std::ostream&
326 operator<<(std::ostream& out, Pose const& p)
327 {
328         out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
329         return out;
330 }
331
332 void
333 PoseRange::set_xyh()
334 {
335         double clen = 10.0;
336         double bpbx = this->bp_.x() - clen * cos(this->bp_.h());
337         double bpby = this->bp_.y() - clen * sin(this->bp_.h());
338         double bpfx = this->bp_.x() + clen * cos(this->bp_.h());
339         double bpfy = this->bp_.y() + clen * sin(this->bp_.h());
340         Line li1(Point(bpbx, bpby), Point(bpfx, bpfy));
341         double epbx = this->ep_.x() - clen * cos(this->ep_.h());
342         double epby = this->ep_.y() - clen * sin(this->ep_.h());
343         double epfx = this->ep_.x() + clen * cos(this->ep_.h());
344         double epfy = this->ep_.y() + clen * sin(this->ep_.h());
345         Line li2(Point(epbx, epby), Point(epfx, epfy));
346         li1.intersects_with(li2);
347         this->x(li1.i1().x());
348         this->y(li1.i1().y());
349         double bh = this->b();
350         while (bh < 0.0) {
351                 bh += 2.0 * M_PI;
352         }
353         this->bp_.h(bh);
354         double eh = this->e();
355         while (eh < 0.0) {
356                 eh += 2.0 * M_PI;
357         }
358         this->ep_.h(eh);
359         this->h((this->b() + this->e()) / 2.0);
360 }
361
362 PoseRange::PoseRange(Pose bp, Pose ep) : bp_(bp), ep_(ep)
363 {
364         if (this->bp_ == this->ep_) {
365                 this->set_pose(this->ep_);
366         } else {
367                 this->set_xyh();
368         }
369 }
370
371 PoseRange::PoseRange(double x, double y, double b, double e)
372                 : PoseRange(Pose(x, y, b), Pose(x, y, e))
373 {
374 }
375
376 Pose
377 PoseRange::bp() const
378 {
379         return this->bp_;
380 }
381
382 Pose
383 PoseRange::ep() const
384 {
385         return this->ep_;
386 }
387
388 double
389 PoseRange::b() const
390 {
391         return std::min(this->bp_.h(), this->ep_.h());
392 }
393
394 double
395 PoseRange::e() const
396 {
397         return std::max(this->bp_.h(), this->ep_.h());
398 }
399
400 void
401 PoseRange::translate(Point const& p)
402 {
403         this->bp_.translate(p);
404         this->ep_.translate(p);
405         this->set_xyh();
406 }
407
408 void
409 PoseRange::rotate(Point const& c, double const angl)
410 {
411         this->bp_.rotate(c, angl);
412         this->ep_.rotate(c, angl);
413         this->set_xyh();
414 }
415
416 void
417 PoseRange::reflect(Line const& li)
418 {
419         this->bp_.reflect(li);
420         this->ep_.reflect(li);
421         this->set_xyh();
422 }
423
424 std::ostream&
425 operator<<(std::ostream& out, PoseRange const& p)
426 {
427         out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
428         out << "]";
429         return out;
430 }
431
432 double
433 CarSize::ctc() const
434 {
435         return this->curb_to_curb_;
436 }
437
438 void
439 CarSize::ctc(double ctc)
440 {
441         this->curb_to_curb_ = ctc;
442 }
443
444 double
445 CarSize::wb() const
446 {
447         return this->wheelbase_;
448 }
449
450 void
451 CarSize::wb(double wb)
452 {
453         this->wheelbase_ = wb;
454 }
455
456 double
457 CarSize::w() const
458 {
459         return this->width_;
460 }
461
462 void
463 CarSize::w(double w)
464 {
465         this->width_ = w;
466 }
467
468 double
469 CarSize::len() const
470 {
471         return this->length_;
472 }
473
474 void
475 CarSize::len(double len)
476 {
477         this->length_ = len;
478 }
479
480 double
481 CarSize::df() const
482 {
483         return this->distance_to_front_;
484 }
485
486 void
487 CarSize::df(double df)
488 {
489         this->distance_to_front_ = df;
490 }
491
492 double
493 CarSize::dr() const
494 {
495         return this->len() - this->df();
496 }
497
498 double
499 CarSize::mtr() const
500 {
501         auto ctc2 = pow(this->ctc() / 2.0, 2.0);
502         auto wb2 = pow(this->wb(), 2.0);
503         return sqrt(ctc2 - wb2) - this->w() / 2.0;
504 }
505
506 double
507 CarSize::iradi() const
508 {
509         return this->mtr() - this->w() / 2;
510 }
511
512 double
513 CarSize::ofradi() const
514 {
515         auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
516         auto df2 = pow(this->df(), 2.0);
517         return sqrt(mtrw2 + df2);
518 }
519
520 double
521 CarSize::orradi() const
522 {
523         auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
524         auto dr2 = pow(this->dr(), 2.0);
525         return sqrt(mtrw2 + dr2);
526 }
527
528 double
529 CarSize::perfect_parking_slot_len() const
530 {
531         auto r = this->ctc() / 2.0;
532         auto l = this->wb();
533         auto k = this->df() - this->wb();
534         auto w = this->w();
535         auto r2l2 = r * r - l * l;
536         auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
537         return this->len() + sqrt(s) - l - k;
538 }
539
540 double
541 CarMove::sp() const
542 {
543         return this->speed_;
544 }
545
546 void
547 CarMove::sp(double sp)
548 {
549         this->speed_ = sp;
550 }
551
552 double
553 CarMove::st() const
554 {
555         return this->steer_;
556 }
557
558 void
559 CarMove::st(double st)
560 {
561         this->steer_ = st;
562 }
563
564 bool
565 BicycleCar::drivable(Pose const& p) const
566 {
567         return this->drivable(PoseRange(p, p));
568 }
569
570 bool
571 BicycleCar::drivable(PoseRange const& p) const
572 {
573         double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
574         while (a_1 < -M_PI)
575                 a_1 += 2 * M_PI;
576         while (a_1 > +M_PI)
577                 a_1 -= 2 * M_PI;
578         double h_d = p.h() - this->h();
579         while (h_d < -M_PI)
580                 h_d += 2 * M_PI;
581         while (h_d > +M_PI)
582                 h_d -= 2 * M_PI;
583         double a_2 = 0;
584         if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
585                 return true;
586         } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
587                 BicycleCar z(*this); // zone border
588                 z.h(p.e());
589                 h_d = p.h() - this->h();
590                 z.rotate(this->ccl(), h_d);
591                 // assert z.h() == p.h()
592                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
593                         return true;
594                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
595                 while (a_2 < -M_PI)
596                         a_2 += 2 * M_PI;
597                 while (a_2 > +M_PI)
598                         a_2 -= 2 * M_PI;
599                 if (z.h() >= a_2 && a_2 >= this->h())
600                         return true;
601         } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
602                 BicycleCar z(*this); // zone border
603                 z.h(p.e());
604                 h_d = p.h() - this->h();
605                 z.rotate(this->ccl(), h_d);
606                 // assert z.h() == p.h()
607                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
608                         return true;
609                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
610                 a_2 -= M_PI;
611                 while (a_2 < -M_PI)
612                         a_2 += 2 * M_PI;
613                 while (a_2 > +M_PI)
614                         a_2 -= 2 * M_PI;
615                 if (this->h() >= a_2 && a_2 >= z.h())
616                         return true;
617         } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
618                 BicycleCar z(*this); // zone border
619                 z.h(p.b());
620                 h_d = p.h() - this->h();
621                 z.rotate(this->ccr(), h_d);
622                 // assert z.h() == p.h()
623                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
624                         return true;
625                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
626                 while (a_2 < -M_PI)
627                         a_2 += 2 * M_PI;
628                 while (a_2 > +M_PI)
629                         a_2 -= 2 * M_PI;
630                 if (this->h() >= a_2 && a_2 >= z.h())
631                         return true;
632         } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
633                 BicycleCar z(*this); // zone border
634                 z.h(p.b());
635                 h_d = p.h() - this->h();
636                 z.rotate(this->ccr(), h_d);
637                 // assert z.h() == p.h()
638                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
639                         return true;
640                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
641                 a_2 -= M_PI;
642                 while (a_2 < -M_PI)
643                         a_2 += 2 * M_PI;
644                 while (a_2 > +M_PI)
645                         a_2 -= 2 * M_PI;
646                 if (z.h() >= a_2 && a_2 >= this->h())
647                         return true;
648         } else {
649                 // Not happenning, as ``-pi <= a <= pi``.
650         }
651         return false;
652 }
653
654 void
655 BicycleCar::set_max_steer()
656 {
657         this->st(atan(this->wb() / this->mtr()));
658 }
659
660 double
661 BicycleCar::lfx() const
662 {
663         double lfx = this->x();
664         lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
665         lfx += this->df() * cos(this->h());
666         return lfx;
667 }
668
669 double
670 BicycleCar::lfy() const
671 {
672         double lfy = this->y();
673         lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
674         lfy += this->df() * sin(this->h());
675         return lfy;
676 }
677
678 double
679 BicycleCar::lrx() const
680 {
681         double lrx = this->x();
682         lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
683         lrx += -this->dr() * cos(this->h());
684         return lrx;
685 }
686
687 double
688 BicycleCar::lry() const
689 {
690         double lry = this->y();
691         lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
692         lry += -this->dr() * sin(this->h());
693         return lry;
694 }
695
696 double
697 BicycleCar::rrx() const
698 {
699         double rrx = this->x();
700         rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
701         rrx += -this->dr() * cos(this->h());
702         return rrx;
703 }
704
705 double
706 BicycleCar::rry() const
707 {
708         double rry = this->y();
709         rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
710         rry += -this->dr() * sin(this->h());
711         return rry;
712 }
713
714 double
715 BicycleCar::rfx() const
716 {
717         double rfx = this->x();
718         rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
719         rfx += this->df() * cos(this->h());
720         return rfx;
721 }
722
723 double
724 BicycleCar::rfy() const
725 {
726         double rfy = this->y();
727         rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
728         rfy += this->df() * sin(this->h());
729         return rfy;
730 }
731
732 Point
733 BicycleCar::lf() const
734 {
735         return Point(this->lfx(), this->lfy());
736 }
737
738 Point
739 BicycleCar::lr() const
740 {
741         return Point(this->lrx(), this->lry());
742 }
743
744 Point
745 BicycleCar::rr() const
746 {
747         return Point(this->rrx(), this->rry());
748 }
749
750 Point
751 BicycleCar::rf() const
752 {
753         return Point(this->rfx(), this->rfy());
754 }
755
756 Line
757 BicycleCar::left() const
758 {
759         return Line(this->lr(), this->lf());
760 }
761
762 Line
763 BicycleCar::rear() const
764 {
765         return Line(this->lr(), this->rr());
766 }
767
768 Line
769 BicycleCar::right() const
770 {
771         return Line(this->rr(), this->rf());
772 }
773
774 Line
775 BicycleCar::front() const
776 {
777         return Line(this->rf(), this->lf());
778 }
779
780 double
781 BicycleCar::ralx() const
782 {
783         double lrx = this->x();
784         lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
785         return lrx;
786 }
787 double
788 BicycleCar::raly() const
789 {
790         double lry = this->y();
791         lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
792         return lry;
793 }
794
795 double
796 BicycleCar::rarx() const
797 {
798         double rrx = this->x();
799         rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
800         return rrx;
801 }
802
803 double
804 BicycleCar::rary() const
805 {
806         double rry = this->y();
807         rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
808         return rry;
809 }
810
811 Point
812 BicycleCar::ccl() const
813 {
814         return Point(
815                 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
816                 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
817         );
818 }
819
820 Point
821 BicycleCar::ccr() const
822 {
823         return Point(
824                 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
825                 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
826         );
827 }
828
829 void
830 BicycleCar::next()
831 {
832         this->x(this->x() + this->sp() * cos(this->h()));
833         this->y(this->y() + this->sp() * sin(this->h()));
834         this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
835 }
836
837 } // namespace bcar